/
promotion_adjustments_spec.rb
377 lines (312 loc) 路 15.3 KB
/
promotion_adjustments_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
require 'spec_helper'
describe "Promotion Adjustments" do
context "coupon promotions", :js => true do
before(:each) do
PAYMENT_STATES = Spree::Payment.state_machine.states.keys unless defined? PAYMENT_STATES
SHIPMENT_STATES = Spree::Shipment.state_machine.states.keys unless defined? SHIPMENT_STATES
ORDER_STATES = Spree::Order.state_machine.states.keys unless defined? ORDER_STATES
# creates a default shipping method which is required for checkout
create(:bogus_payment_method, :environment => 'test')
# creates a check payment method so we don't need to worry about cc details
create(:payment_method)
sm = create(:shipping_method, :zone => Spree::Zone.find_by_name('North America'))
sm.calculator.set_preference(:amount, 10)
user = create(:admin_user)
create(:product, :name => "RoR Mug", :price => "40")
create(:product, :name => "RoR Bag", :price => "20")
visit spree.admin_path
click_link "Promotions"
click_link "New Promotion"
end
let!(:address) { create(:address, :state => Spree::State.first) }
it "should allow an admin to create a flat rate discount coupon promo" do
fill_in "Name", :with => "Order's total > $30"
fill_in "Usage Limit", :with => "100"
select "Coupon code added", :from => "Event"
fill_in "Code", :with => "ORDER_38"
click_button "Create"
page.should have_content("Editing Promotion")
select "Item total", :from => "Add rule of type"
within('#rule_fields') { click_button "Add" }
fill_in "Order total meets these criteria", :with => "30"
within('#rule_fields') { click_button "Update" }
select "Create adjustment", :from => "Add action of type"
within('#action_fields') { click_button "Add" }
select "Flat Rate (per order)", :from => "Calculator"
within('#actions_container') { click_button "Update" }
within('.calculator-fields') { fill_in "Amount", :with => "5" }
within('#actions_container') { click_button "Update" }
visit spree.root_path
click_link "RoR Mug"
click_button "Add To Cart"
click_link "Checkout"
fill_in "Customer E-Mail", :with => "spree@example.com"
str_addr = "bill_address"
select "United States", :from => "order_#{str_addr}_attributes_country_id"
['firstname', 'lastname', 'address1', 'city', 'zipcode', 'phone'].each do |field|
fill_in "order_#{str_addr}_attributes_#{field}", :with => "#{address.send(field)}"
end
select "#{address.state.name}", :from => "order_#{str_addr}_attributes_state_id"
check "order_use_billing"
click_button "Save and Continue"
click_button "Save and Continue"
choose('Credit Card')
fill_in "card_number", :with => "4111111111111111"
fill_in "card_code", :with => "123"
fill_in "order_coupon_code", :with => "ORDER_38"
click_button "Save and Continue"
Spree::Order.last.adjustments.promotion.map(&:amount).sum.should == -5.0
end
it "should allow an admin to create a single user coupon promo with flat rate discount" do
fill_in "Name", :with => "Order's total > $30"
fill_in "Usage Limit", :with => "1"
select "Coupon code added", :from => "Event"
fill_in "Code", :with => "SINGLE_USE"
click_button "Create"
page.should have_content("Editing Promotion")
select "Item total", :from => "Add rule of type"
within('#rule_fields') { click_button "Add" }
fill_in "Order total meets these criteria", :with => "30"
within('#rule_fields') { click_button "Update" }
select "Create adjustment", :from => "Add action of type"
within('#action_fields') { click_button "Add" }
select "Flat Rate (per order)", :from => "Calculator"
within('#actions_container') { click_button "Update" }
within('#action_fields') { fill_in "Amount", :with => "5" }
within('#actions_container') { click_button "Update" }
visit spree.root_path
click_link "RoR Mug"
click_button "Add To Cart"
click_link "Checkout"
fill_in "Customer E-Mail", :with => "spree@example.com"
str_addr = "bill_address"
select "United States", :from => "order_#{str_addr}_attributes_country_id"
['firstname', 'lastname', 'address1', 'city', 'zipcode', 'phone'].each do |field|
fill_in "order_#{str_addr}_attributes_#{field}", :with => "#{address.send(field)}"
end
select "#{address.state.name}", :from => "order_#{str_addr}_attributes_state_id"
check "order_use_billing"
click_button "Save and Continue"
click_button "Save and Continue"
fill_in "order_coupon_code", :with => "SINGLE_USE"
choose('Credit Card')
fill_in "card_number", :with => "4111111111111111"
fill_in "card_code", :with => "123"
click_button "Save and Continue"
Spree::Order.first.total.to_f.should == 45.00
click_button "Place Order"
visit spree.root_path
click_link "RoR Mug"
click_button "Add To Cart"
click_link "Checkout"
fill_in "Customer E-Mail", :with => "spree@example.com"
str_addr = "bill_address"
select "United States", :from => "order_#{str_addr}_attributes_country_id"
['firstname', 'lastname', 'address1', 'city', 'zipcode', 'phone'].each do |field|
fill_in "order_#{str_addr}_attributes_#{field}", :with => "#{address.send(field)}"
end
select "#{address.state.name}", :from => "order_#{str_addr}_attributes_state_id"
check "order_use_billing"
click_button "Save and Continue"
click_button "Save and Continue"
choose('Credit Card')
fill_in "card_number", :with => "4111111111111111"
fill_in "card_code", :with => "123"
fill_in "order_coupon_code", :with => "SINGLE_USE"
click_button "Save and Continue"
Spree::Order.last.total.to_f.should == 50.00
end
it "should allow an admin to create an automatic promo with flat percent discount" do
fill_in "Name", :with => "Order's total > $30"
fill_in "Code", :with => ""
select "Order contents changed", :from => "Event"
click_button "Create"
page.should have_content("Editing Promotion")
select "Item total", :from => "Add rule of type"
within('#rule_fields') { click_button "Add" }
fill_in "Order total meets these criteria", :with => "30"
within('#rule_fields') { click_button "Update" }
select "Create adjustment", :from => "Add action of type"
within('#action_fields') { click_button "Add" }
select "Flat Percent", :from => "Calculator"
within('#actions_container') { click_button "Update" }
within('.calculator-fields') { fill_in "Flat Percent", :with => "10" }
within('#actions_container') { click_button "Update" }
visit spree.root_path
click_link "RoR Mug"
click_button "Add To Cart"
Spree::Order.last.total.to_f.should == 36.00
visit spree.root_path
click_link "RoR Bag"
click_button "Add To Cart"
Spree::Order.last.total.to_f.should == 54.00
end
it "should allow an admin to create an automatic promotion with free shipping (no code)" do
fill_in "Name", :with => "Free Shipping"
fill_in "Code", :with => ""
click_button "Create"
page.should have_content("Editing Promotion")
select "Item total", :from => "Add rule of type"
within('#rule_fields') { click_button "Add" }
fill_in "Order total meets these criteria", :with => "30"
within('#rule_fields') { click_button "Update" }
select "Create adjustment", :from => "Add action of type"
within('#action_fields') { click_button "Add" }
select "Free Shipping", :from => "Calculator"
within('#actions_container') { click_button "Update" }
visit spree.root_path
click_link "RoR Bag"
click_button "Add To Cart"
click_link "Checkout"
fill_in "Customer E-Mail", :with => "spree@example.com"
str_addr = "bill_address"
select "United States", :from => "order_#{str_addr}_attributes_country_id"
['firstname', 'lastname', 'address1', 'city', 'zipcode', 'phone'].each do |field|
fill_in "order_#{str_addr}_attributes_#{field}", :with => "#{address.send(field)}"
end
select "#{address.state.name}", :from => "order_#{str_addr}_attributes_state_id"
check "order_use_billing"
click_button "Save and Continue"
click_button "Save and Continue"
choose('Credit Card')
fill_in "card_number", :with => "4111111111111111"
fill_in "card_code", :with => "123"
click_button "Save and Continue"
Spree::Order.last.total.to_f.should == 30.00 # bag(20) + shipping(10)
page.should_not have_content("Free Shipping")
visit spree.root_path
click_link "RoR Mug"
click_button "Add To Cart"
click_link "Checkout"
str_addr = "bill_address"
select "United States", :from => "order_#{str_addr}_attributes_country_id"
['firstname', 'lastname', 'address1', 'city', 'zipcode', 'phone'].each do |field|
fill_in "order_#{str_addr}_attributes_#{field}", :with => "#{address.send(field)}"
end
select "#{address.state.name}", :from => "order_#{str_addr}_attributes_state_id"
check "order_use_billing"
click_button "Save and Continue"
click_button "Save and Continue"
choose('Credit Card')
fill_in "card_number", :with => "4111111111111111"
fill_in "card_code", :with => "123"
click_button "Save and Continue"
Spree::Order.last.total.to_f.should == 60.00 # bag(20) + mug(40) + free shipping(0)
page.should have_content("Free Shipping")
end
it "should allow an admin to create an automatic promo requiring a landing page to be visited" do
fill_in "Name", :with => "Deal"
select "Visit static content page", :from => "Event"
fill_in "Path", :with => "content/cvv"
click_button "Create"
page.should have_content("Editing Promotion")
select "Create adjustment", :from => "Add action of type"
within('#action_fields') { click_button "Add" }
select "Flat Rate (per order)", :from => "Calculator"
within('#actions_container') { click_button "Update" }
within('.calculator-fields') { fill_in "Amount", :with => "4" }
within('#actions_container') { click_button "Update" }
visit spree.root_path
click_link "RoR Mug"
click_button "Add To Cart"
Spree::Order.last.total.to_f.should == 40.00
visit "/content/cvv"
visit spree.root_path
click_link "RoR Mug"
click_button "Add To Cart"
Spree::Order.last.total.to_f.should == 76.00
end
it "should not allow an admin to create two automatic promo for the same specific product" do
create_per_product_promotion("RoR Mug", 5.0)
create_per_product_promotion("RoR Mug", 10.0)
Spree::Promotion.last.should_not be_valid
end
# Regression test for #1416
it "should allow an admin to create an automatic promo requiring a specific product to be bought" do
create_per_product_promotion("RoR Mug", 5.0)
create_per_product_promotion("RoR Bag", 10.0)
add_to_cart "RoR Mug"
add_to_cart "RoR Bag"
# first promotion should be effective on current order
first_promotion = Spree::Promotion.first
first_promotion.actions.first.calculator.compute(Spree::Order.last).should == 5.0
# second promotion should be effective on current order
second_promotion = Spree::Promotion.last
second_promotion.actions.first.calculator.compute(Spree::Order.last).should == 10.0
do_checkout()
# Mug discount ($5) is not taken into account due to #1526
# Only "best" discount is taken into account
Spree::Order.last.total.to_f.should == 60.0 # mug(40) + bag(20) - bag_discount(10) + shipping(10)
end
it "ceasing to be eligible for a promotion with item total rule then becoming eligible again" do
fill_in "Name", :with => "Spend over $50 and save $5"
select "Order contents changed", :from => "Event"
click_button "Create"
page.should have_content("Editing Promotion")
select "Item total", :from => "Add rule of type"
within('#rule_fields') { click_button "Add" }
fill_in "Order total meets these criteria", :with => "50"
within('#rule_fields') { click_button "Update" }
select "Create adjustment", :from => "Add action of type"
within('#action_fields') { click_button "Add" }
select "Flat Rate (per order)", :from => "Calculator"
within('#actions_container') { click_button "Update" }
within('.calculator-fields') { fill_in "Amount", :with => "5" }
within('#actions_container') { click_button "Update" }
visit spree.root_path
click_link "RoR Bag"
click_button "Add To Cart"
Spree::Order.last.total.to_f.should == 20.00
fill_in "order[line_items_attributes][0][quantity]", :with => "2"
click_button "Update"
Spree::Order.last.total.to_f.should == 40.00
Spree::Order.last.adjustments.eligible.promotion.count.should == 0
fill_in "order[line_items_attributes][0][quantity]", :with => "3"
click_button "Update"
Spree::Order.last.total.to_f.should == 55.00
Spree::Order.last.adjustments.eligible.promotion.count.should == 1
fill_in "order[line_items_attributes][0][quantity]", :with => "2"
click_button "Update"
Spree::Order.last.total.to_f.should == 40.00
Spree::Order.last.adjustments.eligible.promotion.count.should == 0
fill_in "order[line_items_attributes][0][quantity]", :with => "3"
click_button "Update"
Spree::Order.last.total.to_f.should == 55.00
end
it "only counting the most valuable promotion adjustment in an order" do
fill_in "Name", :with => "$5 off"
select "Order contents changed", :from => "Event"
click_button "Create"
page.should have_content("Editing Promotion")
select "Create adjustment", :from => "Add action of type"
within('#action_fields') { click_button "Add" }
select "Flat Rate (per order)", :from => "Calculator"
within('#actions_container') { click_button "Update" }
within('.calculator-fields') { fill_in "Amount", :with => "5" }
within('#actions_container') { click_button "Update" }
visit spree.admin_promotions_path
click_link "New Promotion"
fill_in "Name", :with => "10% off"
select "Order contents changed", :from => "Event"
click_button "Create"
page.should have_content("Editing Promotion")
select "Create adjustment", :from => "Add action of type"
within('#action_fields') { click_button "Add" }
select "Flat Percent", :from => "Calculator"
within('#actions_container') { click_button "Update" }
within('.calculator-fields') { fill_in "Flat Percent", :with => "10" }
within('#actions_container') { click_button "Update" }
visit spree.root_path
click_link "RoR Bag"
click_button "Add To Cart"
Spree::Order.last.total.to_f.should == 13.00
#Spree::Order.last.adjustments.promotion.count.should == 2
fill_in "order[line_items_attributes][0][quantity]", :with => "2"
click_button "Update"
Spree::Order.last.total.to_f.should == 31.00
fill_in "order[line_items_attributes][0][quantity]", :with => "3"
click_button "Update"
Spree::Order.last.total.to_f.should == 49.00
end
end
end