Skip to content

Commit 3eba059

Browse files
committed
fixup! WIP: high-level integration test for legacy promotion system
1 parent b06e1ea commit 3eba059

File tree

3 files changed

+92
-18
lines changed

3 files changed

+92
-18
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
module SolidusLegacyPromotions
2+
module SpreeInMemoryOrderUpdaterPatch
3+
def update_adjustment_total(persist:)
4+
update_adjustments(persist:)
5+
6+
all_items = line_items + shipments
7+
valid_adjustments = adjustments.select(&:eligible?).reject(&:marked_for_destruction?)
8+
order_tax_adjustments = valid_adjustments.select(&:tax?)
9+
10+
order.adjustment_total = all_items.sum(&:adjustment_total) + valid_adjustments.sum(&:amount)
11+
order.included_tax_total = all_items.sum(&:included_tax_total) + order_tax_adjustments.select(&:included?).sum(&:amount)
12+
order.additional_tax_total = all_items.sum(&:additional_tax_total) + order_tax_adjustments.reject(&:included?).sum(&:amount)
13+
14+
recalculate_order_total
15+
end
16+
17+
def update_item_totals(persist:)
18+
[*line_items, *shipments].each do |item|
19+
Spree::Config.item_total_class.new(item).recalculate!
20+
21+
# The cancellation_total isn't persisted anywhere but is included in
22+
# the adjustment_total.
23+
#
24+
# Core doesn't have "eligible" adjustments anymore, so we need to
25+
# override the adjustment_total calculation to exclude them for legacy
26+
# promotions.
27+
item.adjustment_total = item.adjustments.
28+
select(&:eligible?).
29+
reject(&:included?).
30+
reject(&:marked_for_destruction?).
31+
sum(&:amount)
32+
33+
next unless persist && item.changed?
34+
35+
item.update_columns(
36+
promo_total: item.promo_total,
37+
included_tax_total: item.included_tax_total,
38+
additional_tax_total: item.additional_tax_total,
39+
adjustment_total: item.adjustment_total,
40+
updated_at: Time.current
41+
)
42+
end
43+
end
44+
45+
Spree::InMemoryOrderUpdater.prepend self
46+
end
47+
end

legacy_promotions/app/patches/models/solidus_legacy_promotions/spree_order_updater_patch.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ def update_item_totals
4242
end
4343
end
4444

45-
# FIXME: We need to verify that this is the right thing to do?
46-
Spree::Config.order_recalculator_class.prepend self
45+
Spree::OrderUpdater.prepend self
4746
end
4847
end

legacy_promotions/spec/models/spree/promotion_integration_spec.rb

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -265,38 +265,66 @@
265265
end
266266

267267
context 'with the in-memory order updater' do
268-
subject { order.recalculate(persist: false) }
268+
subject { order.recalculate(persist:) }
269269

270270
around do |example|
271271
default_order_recalculator = Spree::Config.order_recalculator_class.to_s
272272

273-
Spree::Config.order_recalculator_class = 'Spree::InMemoryOrderUpdater'
273+
Spree::Config.order_recalculator_class = Spree::InMemoryOrderUpdater
274274

275275
example.run
276276

277277
Spree::Config.order_recalculator_class = default_order_recalculator
278278
end
279279

280-
it "makes the promotion ineligible" do
281-
expect { subject }
282-
.to change { order.adjustments.first.eligible }
283-
.from(true)
284-
.to(false)
280+
context "when not persisting updates" do
281+
let(:persist) { false }
285282

286-
expect(promo_adjustment.reload.eligible).to eq true
287-
end
283+
it "changes but does not persist the promotion as ineligible" do
284+
expect { subject }
285+
.to change { order.adjustments.first.eligible }
286+
.from(true)
287+
.to(false)
288+
289+
expect(promo_adjustment.reload.eligible).to eq true
290+
end
288291

289-
it "adjusts the promo_total" do
290-
expect { subject }.to change { order.promo_total }.from(-10).to(0)
292+
it "changes but does not persist the promo_total" do
293+
expect { subject }.to change { order.promo_total }.from(-10).to(0)
291294

292-
expect(order.reload.promo_total).to eq -10
295+
expect(order.reload.promo_total).to eq -10
296+
end
297+
298+
it "changes the total but does not persist the promo amount" do
299+
expect { subject }.to change { order.total }.from(30).to(40)
300+
301+
expect(order.reload.total).to eq 30
302+
end
293303
end
294304

295-
it "increases the total to remove the promo",
296-
pending: "This failure points to a real issue we have with the in-memory order updater" do
297-
expect { subject }.to change { order.total }.from(30).to(40)
305+
context "when persisting updates" do
306+
let(:persist) { true }
298307

299-
expect(order.reload.total).to eq 40
308+
it "makes the promotion ineligible" do
309+
expect { subject }
310+
.to change { order.adjustments.first.eligible }
311+
.from(true)
312+
.to(false)
313+
314+
expect(promo_adjustment.reload.eligible).to eq false
315+
end
316+
317+
it "adjusts the promo_total" do
318+
expect { subject }.to change { order.promo_total }.from(-10).to(0)
319+
320+
expect(order.reload.promo_total).to eq 0
321+
end
322+
323+
it "increases the total to remove the promo" do
324+
expect { subject }.to change { order.total }.from(30).to(40)
325+
326+
expect(order.reload.total).to eq 40
327+
end
300328
end
301329
end
302330
end

0 commit comments

Comments
 (0)