spree_core 3.1.5 → 3.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (215) hide show
  1. checksums.yaml +4 -4
  2. data/app/models/concerns/spree/ransackable_attributes.rb +6 -1
  3. data/app/models/concerns/spree/user_methods.rb +10 -0
  4. data/app/models/spree/legacy_user.rb +0 -8
  5. data/app/models/spree/order.rb +1 -1
  6. data/app/models/spree/product.rb +8 -0
  7. data/app/models/spree/product/scopes.rb +7 -3
  8. data/app/models/spree/stock_transfer.rb +1 -1
  9. data/config/locales/en.yml +2 -0
  10. data/lib/spree/core/version.rb +1 -1
  11. data/spree_core.gemspec +2 -2
  12. metadata +11 -208
  13. data/spec/fixtures/microdata.html +0 -22
  14. data/spec/fixtures/microdata_itemref.html +0 -15
  15. data/spec/fixtures/microdata_no_itemscope.html +0 -20
  16. data/spec/fixtures/thinking-cat.jpg +0 -0
  17. data/spec/helpers/base_helper_spec.rb +0 -200
  18. data/spec/helpers/products_helper_spec.rb +0 -296
  19. data/spec/lib/calculated_adjustments_spec.rb +0 -7
  20. data/spec/lib/i18n_spec.rb +0 -123
  21. data/spec/lib/search/base_spec.rb +0 -86
  22. data/spec/lib/spree/core/controller_helpers/auth_spec.rb +0 -103
  23. data/spec/lib/spree/core/controller_helpers/order_spec.rb +0 -110
  24. data/spec/lib/spree/core/controller_helpers/search_spec.rb +0 -17
  25. data/spec/lib/spree/core/controller_helpers/store_spec.rb +0 -72
  26. data/spec/lib/spree/core/controller_helpers/strong_parameters_spec.rb +0 -39
  27. data/spec/lib/spree/core/delegate_belongs_to_spec.rb +0 -22
  28. data/spec/lib/spree/core/importer/order_spec.rb +0 -605
  29. data/spec/lib/spree/core/number_generator_spec.rb +0 -175
  30. data/spec/lib/spree/core/token_generator_spec.rb +0 -24
  31. data/spec/lib/spree/core/validators/email_spec.rb +0 -53
  32. data/spec/lib/spree/core_spec.rb +0 -23
  33. data/spec/lib/spree/localized_number_spec.rb +0 -48
  34. data/spec/lib/spree/migrations_spec.rb +0 -36
  35. data/spec/lib/spree/money_spec.rb +0 -122
  36. data/spec/lib/tasks/exchanges_spec.rb +0 -136
  37. data/spec/mailers/order_mailer_spec.rb +0 -122
  38. data/spec/mailers/reimbursement_mailer_spec.rb +0 -47
  39. data/spec/mailers/shipment_mailer_spec.rb +0 -81
  40. data/spec/mailers/test_mailer_spec.rb +0 -38
  41. data/spec/models/option_type_prototype_spec.rb +0 -9
  42. data/spec/models/spree/ability_spec.rb +0 -251
  43. data/spec/models/spree/address_spec.rb +0 -291
  44. data/spec/models/spree/adjustable/adjuster/base_spec.rb +0 -10
  45. data/spec/models/spree/adjustable/adjuster/promotion_spec.rb +0 -211
  46. data/spec/models/spree/adjustable/adjuster/tax_spec.rb +0 -86
  47. data/spec/models/spree/adjustable/adjustments_updater_spec.rb +0 -26
  48. data/spec/models/spree/adjustment_spec.rb +0 -189
  49. data/spec/models/spree/app_configuration_spec.rb +0 -26
  50. data/spec/models/spree/asset_spec.rb +0 -28
  51. data/spec/models/spree/calculator/default_tax_spec.rb +0 -152
  52. data/spec/models/spree/calculator/flat_percent_item_total_spec.rb +0 -25
  53. data/spec/models/spree/calculator/flat_rate_spec.rb +0 -47
  54. data/spec/models/spree/calculator/flexi_rate_spec.rb +0 -41
  55. data/spec/models/spree/calculator/percent_on_line_item_spec.rb +0 -15
  56. data/spec/models/spree/calculator/price_sack_spec.rb +0 -30
  57. data/spec/models/spree/calculator/refunds/default_refund_amount_spec.rb +0 -51
  58. data/spec/models/spree/calculator/shipping.rb +0 -8
  59. data/spec/models/spree/calculator/shipping/flat_percent_item_total_spec.rb +0 -23
  60. data/spec/models/spree/calculator/shipping/flat_rate_spec.rb +0 -13
  61. data/spec/models/spree/calculator/shipping/flexi_rate_spec.rb +0 -52
  62. data/spec/models/spree/calculator/shipping/per_item_spec.rb +0 -20
  63. data/spec/models/spree/calculator/shipping/price_sack_spec.rb +0 -29
  64. data/spec/models/spree/calculator/tiered_flat_rate_spec.rb +0 -40
  65. data/spec/models/spree/calculator/tiered_percent_spec.rb +0 -51
  66. data/spec/models/spree/calculator_spec.rb +0 -69
  67. data/spec/models/spree/classification_spec.rb +0 -93
  68. data/spec/models/spree/concerns/display_money_spec.rb +0 -43
  69. data/spec/models/spree/concerns/user_methods_spec.rb +0 -55
  70. data/spec/models/spree/concerns/vat_price_calculation_spec.rb +0 -66
  71. data/spec/models/spree/country_spec.rb +0 -55
  72. data/spec/models/spree/credit_card_spec.rb +0 -328
  73. data/spec/models/spree/customer_return_spec.rb +0 -240
  74. data/spec/models/spree/exchange_spec.rb +0 -75
  75. data/spec/models/spree/gateway/bogus_simple.rb +0 -20
  76. data/spec/models/spree/gateway/bogus_spec.rb +0 -13
  77. data/spec/models/spree/gateway_spec.rb +0 -61
  78. data/spec/models/spree/image_spec.rb +0 -8
  79. data/spec/models/spree/inventory_unit_spec.rb +0 -225
  80. data/spec/models/spree/line_item_spec.rb +0 -327
  81. data/spec/models/spree/option_type_spec.rb +0 -14
  82. data/spec/models/spree/option_value_spec.rb +0 -18
  83. data/spec/models/spree/order/address_spec.rb +0 -50
  84. data/spec/models/spree/order/adjustments_spec.rb +0 -29
  85. data/spec/models/spree/order/callbacks_spec.rb +0 -42
  86. data/spec/models/spree/order/checkout_spec.rb +0 -782
  87. data/spec/models/spree/order/currency_updater_spec.rb +0 -32
  88. data/spec/models/spree/order/finalizing_spec.rb +0 -114
  89. data/spec/models/spree/order/helpers_spec.rb +0 -5
  90. data/spec/models/spree/order/payment_spec.rb +0 -214
  91. data/spec/models/spree/order/risk_assessment_spec.rb +0 -84
  92. data/spec/models/spree/order/shipments_spec.rb +0 -43
  93. data/spec/models/spree/order/state_machine_spec.rb +0 -211
  94. data/spec/models/spree/order/store_credit_spec.rb +0 -426
  95. data/spec/models/spree/order/tax_spec.rb +0 -84
  96. data/spec/models/spree/order/totals_spec.rb +0 -24
  97. data/spec/models/spree/order/updating_spec.rb +0 -18
  98. data/spec/models/spree/order/validations_spec.rb +0 -15
  99. data/spec/models/spree/order_contents_spec.rb +0 -297
  100. data/spec/models/spree/order_inventory_spec.rb +0 -238
  101. data/spec/models/spree/order_merger_spec.rb +0 -135
  102. data/spec/models/spree/order_spec.rb +0 -967
  103. data/spec/models/spree/order_updater_spec.rb +0 -305
  104. data/spec/models/spree/payment/gateway_options_spec.rb +0 -127
  105. data/spec/models/spree/payment/store_credit_spec.rb +0 -60
  106. data/spec/models/spree/payment_method/store_credit_spec.rb +0 -291
  107. data/spec/models/spree/payment_method_spec.rb +0 -103
  108. data/spec/models/spree/payment_spec.rb +0 -919
  109. data/spec/models/spree/preference_spec.rb +0 -80
  110. data/spec/models/spree/preferences/configuration_spec.rb +0 -30
  111. data/spec/models/spree/preferences/preferable_spec.rb +0 -348
  112. data/spec/models/spree/preferences/scoped_store_spec.rb +0 -58
  113. data/spec/models/spree/preferences/store_spec.rb +0 -46
  114. data/spec/models/spree/price_spec.rb +0 -128
  115. data/spec/models/spree/product/scopes_spec.rb +0 -183
  116. data/spec/models/spree/product_duplicator_spec.rb +0 -103
  117. data/spec/models/spree/product_filter_spec.rb +0 -26
  118. data/spec/models/spree/product_option_type_spec.rb +0 -9
  119. data/spec/models/spree/product_promotion_rule_spec.rb +0 -9
  120. data/spec/models/spree/product_property_spec.rb +0 -22
  121. data/spec/models/spree/product_spec.rb +0 -540
  122. data/spec/models/spree/promotion/actions/create_adjustment_spec.rb +0 -50
  123. data/spec/models/spree/promotion/actions/create_item_adjustments_spec.rb +0 -148
  124. data/spec/models/spree/promotion/actions/create_line_items_spec.rb +0 -86
  125. data/spec/models/spree/promotion/actions/free_shipping_spec.rb +0 -36
  126. data/spec/models/spree/promotion/rules/first_order_spec.rb +0 -75
  127. data/spec/models/spree/promotion/rules/item_total_spec.rb +0 -282
  128. data/spec/models/spree/promotion/rules/one_use_per_user_spec.rb +0 -42
  129. data/spec/models/spree/promotion/rules/option_value_spec.rb +0 -90
  130. data/spec/models/spree/promotion/rules/product_spec.rb +0 -143
  131. data/spec/models/spree/promotion/rules/taxon_spec.rb +0 -102
  132. data/spec/models/spree/promotion/rules/user_logged_in_spec.rb +0 -27
  133. data/spec/models/spree/promotion/rules/user_spec.rb +0 -45
  134. data/spec/models/spree/promotion_action_spec.rb +0 -10
  135. data/spec/models/spree/promotion_category_spec.rb +0 -17
  136. data/spec/models/spree/promotion_handler/cart_spec.rb +0 -102
  137. data/spec/models/spree/promotion_handler/coupon_spec.rb +0 -323
  138. data/spec/models/spree/promotion_handler/free_shipping_spec.rb +0 -48
  139. data/spec/models/spree/promotion_handler/page_spec.rb +0 -44
  140. data/spec/models/spree/promotion_rule_spec.rb +0 -29
  141. data/spec/models/spree/promotion_rule_taxon_spec.rb +0 -9
  142. data/spec/models/spree/promotion_rule_user_spec.rb +0 -9
  143. data/spec/models/spree/promotion_spec.rb +0 -644
  144. data/spec/models/spree/property_prototype_spec.rb +0 -9
  145. data/spec/models/spree/property_spec.rb +0 -5
  146. data/spec/models/spree/prototype_spec.rb +0 -5
  147. data/spec/models/spree/prototype_taxon_spec.rb +0 -9
  148. data/spec/models/spree/refund_reason_spec.rb +0 -7
  149. data/spec/models/spree/refund_spec.rb +0 -195
  150. data/spec/models/spree/reimbursement/credit_spec.rb +0 -36
  151. data/spec/models/spree/reimbursement/reimbursement_type_engine_spec.rb +0 -140
  152. data/spec/models/spree/reimbursement/reimbursement_type_validator_spec.rb +0 -83
  153. data/spec/models/spree/reimbursement_performer_spec.rb +0 -30
  154. data/spec/models/spree/reimbursement_spec.rb +0 -188
  155. data/spec/models/spree/reimbursement_tax_calculator_spec.rb +0 -63
  156. data/spec/models/spree/reimbursement_type/credit_spec.rb +0 -53
  157. data/spec/models/spree/reimbursement_type/exchange_spec.rb +0 -46
  158. data/spec/models/spree/reimbursement_type/original_payment_spec.rb +0 -55
  159. data/spec/models/spree/reimbursement_type/store_credit_spec.rb +0 -101
  160. data/spec/models/spree/return_authorization_reason_spec.rb +0 -7
  161. data/spec/models/spree/return_authorization_spec.rb +0 -230
  162. data/spec/models/spree/return_item/eligibility_validator/default_spec.rb +0 -77
  163. data/spec/models/spree/return_item/eligibility_validator/inventory_shipped_spec.rb +0 -58
  164. data/spec/models/spree/return_item/eligibility_validator/no_reimbursements_spec.rb +0 -61
  165. data/spec/models/spree/return_item/eligibility_validator/order_completed_spec.rb +0 -32
  166. data/spec/models/spree/return_item/eligibility_validator/rma_required_spec.rb +0 -29
  167. data/spec/models/spree/return_item/eligibility_validator/time_since_purchase_spec.rb +0 -35
  168. data/spec/models/spree/return_item/exchange_variant_eligibility/same_option_value_spec.rb +0 -65
  169. data/spec/models/spree/return_item/exchange_variant_eligibility/same_product_spec.rb +0 -43
  170. data/spec/models/spree/return_item_spec.rb +0 -731
  171. data/spec/models/spree/returns_calculator_spec.rb +0 -14
  172. data/spec/models/spree/role_spec.rb +0 -7
  173. data/spec/models/spree/shipment_spec.rb +0 -740
  174. data/spec/models/spree/shipping_calculator_spec.rb +0 -45
  175. data/spec/models/spree/shipping_category_spec.rb +0 -19
  176. data/spec/models/spree/shipping_method_spec.rb +0 -95
  177. data/spec/models/spree/shipping_rate_spec.rb +0 -140
  178. data/spec/models/spree/state_spec.rb +0 -29
  179. data/spec/models/spree/stock/availability_validator_spec.rb +0 -36
  180. data/spec/models/spree/stock/content_item_spec.rb +0 -31
  181. data/spec/models/spree/stock/coordinator_spec.rb +0 -51
  182. data/spec/models/spree/stock/differentiator_spec.rb +0 -39
  183. data/spec/models/spree/stock/estimator_spec.rb +0 -202
  184. data/spec/models/spree/stock/inventory_unit_builder_spec.rb +0 -38
  185. data/spec/models/spree/stock/package_spec.rb +0 -194
  186. data/spec/models/spree/stock/packer_spec.rb +0 -70
  187. data/spec/models/spree/stock/prioritizer_spec.rb +0 -125
  188. data/spec/models/spree/stock/quantifier_spec.rb +0 -126
  189. data/spec/models/spree/stock/splitter/backordered_spec.rb +0 -29
  190. data/spec/models/spree/stock/splitter/base_spec.rb +0 -21
  191. data/spec/models/spree/stock/splitter/shipping_category_spec.rb +0 -47
  192. data/spec/models/spree/stock/splitter/weight_spec.rb +0 -32
  193. data/spec/models/spree/stock_item_spec.rb +0 -428
  194. data/spec/models/spree/stock_location_spec.rb +0 -243
  195. data/spec/models/spree/stock_movement_spec.rb +0 -56
  196. data/spec/models/spree/stock_transfer_spec.rb +0 -50
  197. data/spec/models/spree/store_credit_event_spec.rb +0 -101
  198. data/spec/models/spree/store_credit_spec.rb +0 -786
  199. data/spec/models/spree/store_spec.rb +0 -78
  200. data/spec/models/spree/tax_category_spec.rb +0 -32
  201. data/spec/models/spree/tax_rate_spec.rb +0 -542
  202. data/spec/models/spree/taxon_spec.rb +0 -74
  203. data/spec/models/spree/taxonomy_spec.rb +0 -18
  204. data/spec/models/spree/tracker_spec.rb +0 -21
  205. data/spec/models/spree/user_spec.rb +0 -203
  206. data/spec/models/spree/variant_spec.rb +0 -795
  207. data/spec/models/spree/zone_member_spec.rb +0 -38
  208. data/spec/models/spree/zone_spec.rb +0 -470
  209. data/spec/spec_helper.rb +0 -77
  210. data/spec/support/big_decimal.rb +0 -5
  211. data/spec/support/concerns/adjustment_source.rb +0 -23
  212. data/spec/support/concerns/default_price.rb +0 -37
  213. data/spec/support/rake.rb +0 -13
  214. data/spec/support/test_gateway.rb +0 -2
  215. data/spec/validators/db_maximum_length_validator_spec.rb +0 -22
@@ -1,48 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module Spree
4
- module PromotionHandler
5
- describe FreeShipping, type: :model do
6
- let(:order) { create(:order) }
7
- let(:shipment) { create(:shipment, order: order ) }
8
-
9
- let(:promotion) { Promotion.create(name: "Free Shipping") }
10
- let(:calculator) { Calculator::FlatPercentItemTotal.new(preferred_flat_percent: 10) }
11
- let!(:action) { Promotion::Actions::FreeShipping.create(promotion: promotion) }
12
-
13
- subject { Spree::PromotionHandler::FreeShipping.new(order) }
14
-
15
- context "activates in Shipment level" do
16
- it "creates the adjustment" do
17
- expect { subject.activate }.to change { shipment.adjustments.count }.by(1)
18
- end
19
- end
20
-
21
- context "if promo has a code" do
22
- before do
23
- promotion.update_column(:code, "code")
24
- end
25
-
26
- it "does adjust the shipment when applied to order" do
27
- order.promotions << promotion
28
-
29
- expect { subject.activate }.to change { shipment.adjustments.count }
30
- end
31
-
32
- it "does not adjust the shipment when not applied to order" do
33
- expect { subject.activate }.to_not change { shipment.adjustments.count }
34
- end
35
- end
36
-
37
- context "if promo has a path" do
38
- before do
39
- promotion.update_column(:path, "path")
40
- end
41
-
42
- it "does not adjust the shipment" do
43
- expect { subject.activate }.to_not change { shipment.adjustments.count }
44
- end
45
- end
46
- end
47
- end
48
- end
@@ -1,44 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module Spree
4
- module PromotionHandler
5
- describe Page, :type => :model do
6
- let(:order) { create(:order_with_line_items, :line_items_count => 1) }
7
-
8
- let(:promotion) { Promotion.create(name: "10% off", :path => '10off') }
9
- before do
10
- calculator = Calculator::FlatPercentItemTotal.new(preferred_flat_percent: 10)
11
- action = Promotion::Actions::CreateItemAdjustments.create(:calculator => calculator)
12
- promotion.actions << action
13
- end
14
-
15
- it "activates at the right path" do
16
- expect(order.line_item_adjustments.count).to eq(0)
17
- Spree::PromotionHandler::Page.new(order, '10off').activate
18
- expect(order.line_item_adjustments.count).to eq(1)
19
- end
20
-
21
- context "when promotion is expired" do
22
- before do
23
- promotion.update_columns(
24
- :starts_at => 1.week.ago,
25
- :expires_at => 1.day.ago
26
- )
27
- end
28
-
29
- it "is not activated" do
30
- expect(order.line_item_adjustments.count).to eq(0)
31
- Spree::PromotionHandler::Page.new(order, '10off').activate
32
- expect(order.line_item_adjustments.count).to eq(0)
33
- end
34
- end
35
-
36
- it "does not activate at the wrong path" do
37
- expect(order.line_item_adjustments.count).to eq(0)
38
- Spree::PromotionHandler::Page.new(order, 'wrongpath').activate
39
- expect(order.line_item_adjustments.count).to eq(0)
40
- end
41
- end
42
- end
43
- end
44
-
@@ -1,29 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module Spree
4
- describe Spree::PromotionRule, :type => :model do
5
-
6
- class BadTestRule < Spree::PromotionRule; end
7
-
8
- class TestRule < Spree::PromotionRule
9
- def eligible?
10
- true
11
- end
12
- end
13
-
14
- it "should force developer to implement eligible? method" do
15
- expect { BadTestRule.new.eligible? }.to raise_error(ArgumentError)
16
- end
17
-
18
- it "validates unique rules for a promotion" do
19
- p1 = TestRule.new
20
- p1.promotion_id = 1
21
- p1.save
22
-
23
- p2 = TestRule.new
24
- p2.promotion_id = 1
25
- expect(p2).not_to be_valid
26
- end
27
-
28
- end
29
- end
@@ -1,9 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Spree::PromotionRuleTaxon do
4
- describe 'Validations' do
5
- it { is_expected.to validate_presence_of(:promotion_rule) }
6
- it { is_expected.to validate_presence_of(:taxon) }
7
- it { is_expected.to validate_uniqueness_of(:promotion_rule_id).scoped_to(:taxon_id).allow_nil }
8
- end
9
- end
@@ -1,9 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Spree::PromotionRuleUser do
4
- describe 'Validations' do
5
- it { is_expected.to validate_presence_of(:promotion_rule) }
6
- it { is_expected.to validate_presence_of(:user) }
7
- it { is_expected.to validate_uniqueness_of(:user_id).scoped_to(:promotion_rule_id).allow_nil }
8
- end
9
- end
@@ -1,644 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Spree::Promotion, :type => :model do
4
- let(:promotion) { Spree::Promotion.new }
5
-
6
- describe "validations" do
7
- before :each do
8
- @valid_promotion = Spree::Promotion.new :name => "A promotion"
9
- end
10
-
11
- it "valid_promotion is valid" do
12
- expect(@valid_promotion).to be_valid
13
- end
14
-
15
- it "validates usage limit" do
16
- @valid_promotion.usage_limit = -1
17
- expect(@valid_promotion).not_to be_valid
18
-
19
- @valid_promotion.usage_limit = 100
20
- expect(@valid_promotion).to be_valid
21
- end
22
-
23
- it "validates name" do
24
- @valid_promotion.name = nil
25
- expect(@valid_promotion).not_to be_valid
26
- end
27
- end
28
-
29
- describe 'scopes' do
30
- describe '.coupons' do
31
- let!(:promotion_without_code) { Spree::Promotion.create! name: 'test', code: '' }
32
- let!(:promotion_with_code) { Spree::Promotion.create! name: 'test1', code: 'code' }
33
-
34
- subject { Spree::Promotion.coupons }
35
-
36
- it 'is expected to not include promotion without code' do
37
- is_expected.to_not include(promotion_without_code)
38
- end
39
-
40
- it 'is expected to include promotion with code' do
41
- is_expected.to include(promotion_with_code)
42
- end
43
- end
44
-
45
- describe '.applied' do
46
- let!(:promotion_not_applied) { Spree::Promotion.create! name: 'test', code: '' }
47
- let(:order) { create(:order) }
48
- let!(:promotion_applied) do
49
- promotion = Spree::Promotion.create!(name: 'test1', code: '')
50
- promotion.orders << order
51
- promotion
52
- end
53
-
54
- subject { Spree::Promotion.applied }
55
-
56
- it 'is expected to not include promotion not applied' do
57
- is_expected.to_not include(promotion_not_applied)
58
- end
59
-
60
- it 'is expected to include promotion applied' do
61
- is_expected.to include(promotion_applied)
62
- end
63
- end
64
-
65
- describe '.advertised' do
66
- let!(:promotion_not_advertised) { Spree::Promotion.create! name: 'test', advertise: false }
67
- let!(:promotion_advertised) { Spree::Promotion.create! name: 'test1', advertise: true }
68
-
69
- subject { Spree::Promotion.advertised }
70
-
71
- it 'is expected to not include promotion not advertised' do
72
- is_expected.to_not include(promotion_not_advertised)
73
- end
74
-
75
- it 'is expected to include promotion advertised' do
76
- is_expected.to include(promotion_advertised)
77
- end
78
- end
79
- end
80
-
81
- describe "#destroy" do
82
- let(:promotion) { Spree::Promotion.create(:name => "delete me") }
83
-
84
- before(:each) do
85
- promotion.actions << Spree::Promotion::Actions::CreateAdjustment.new
86
- promotion.rules << Spree::Promotion::Rules::FirstOrder.new
87
- promotion.save!
88
- promotion.destroy
89
- end
90
-
91
- it "should delete actions" do
92
- expect(Spree::PromotionAction.count).to eq(0)
93
- end
94
-
95
- it "should delete rules" do
96
- expect(Spree::PromotionRule.count).to eq(0)
97
- end
98
- end
99
-
100
- describe "#save" do
101
- let(:promotion) { Spree::Promotion.create(:name => "delete me") }
102
-
103
- before(:each) do
104
- promotion.actions << Spree::Promotion::Actions::CreateAdjustment.new
105
- promotion.rules << Spree::Promotion::Rules::FirstOrder.new
106
- promotion.save!
107
- end
108
-
109
- it "should deeply autosave records and preferences" do
110
- promotion.actions[0].calculator.preferred_flat_percent = 10
111
- promotion.save!
112
- expect(Spree::Calculator.first.preferred_flat_percent).to eq(10)
113
- end
114
- end
115
-
116
- describe "#activate" do
117
- before do
118
- @action1 = Spree::Promotion::Actions::CreateAdjustment.create!
119
- @action2 = Spree::Promotion::Actions::CreateAdjustment.create!
120
- allow(@action1).to receive_messages perform: true
121
- allow(@action2).to receive_messages perform: true
122
-
123
- promotion.promotion_actions = [@action1, @action2]
124
- promotion.created_at = 2.days.ago
125
-
126
- @user = stub_model(Spree::LegacyUser, :email => "spree@example.com")
127
- @order = Spree::Order.create user: @user
128
- @payload = { :order => @order, :user => @user }
129
- end
130
-
131
- it "should check path if present" do
132
- promotion.path = 'content/cvv'
133
- @payload[:path] = 'content/cvv'
134
- expect(@action1).to receive(:perform).with(@payload)
135
- expect(@action2).to receive(:perform).with(@payload)
136
- promotion.activate(@payload)
137
- end
138
-
139
- it "does not perform actions against an order in a finalized state" do
140
- expect(@action1).not_to receive(:perform).with(@payload)
141
-
142
- @order.state = 'complete'
143
- promotion.activate(@payload)
144
-
145
- @order.state = 'awaiting_return'
146
- promotion.activate(@payload)
147
-
148
- @order.state = 'returned'
149
- promotion.activate(@payload)
150
- end
151
-
152
- it "does activate if newer then order" do
153
- expect(@action1).to receive(:perform).with(@payload)
154
- promotion.created_at = DateTime.current + 2
155
- expect(promotion.activate(@payload)).to be true
156
- end
157
-
158
- context "keeps track of the orders" do
159
- context "when activated" do
160
- it "assigns the order" do
161
- expect(promotion.orders).to be_empty
162
- expect(promotion.activate(@payload)).to be true
163
- expect(promotion.orders.first).to eql @order
164
- end
165
- end
166
- context "when not activated" do
167
- it "will not assign the order" do
168
- @order.state = 'complete'
169
- expect(promotion.orders).to be_empty
170
- expect(promotion.activate(@payload)).to be_falsey
171
- expect(promotion.orders).to be_empty
172
- end
173
- end
174
-
175
- end
176
-
177
- end
178
-
179
- context "#usage_limit_exceeded" do
180
- let(:promotable) { double('Promotable') }
181
- it "should not have its usage limit exceeded with no usage limit" do
182
- promotion.usage_limit = 0
183
- expect(promotion.usage_limit_exceeded?(promotable)).to be false
184
- end
185
-
186
- it "should have its usage limit exceeded" do
187
- promotion.usage_limit = 2
188
- allow(promotion).to receive_messages(:adjusted_credits_count => 2)
189
- expect(promotion.usage_limit_exceeded?(promotable)).to be true
190
-
191
- allow(promotion).to receive_messages(:adjusted_credits_count => 3)
192
- expect(promotion.usage_limit_exceeded?(promotable)).to be true
193
- end
194
- end
195
-
196
- context "#expired" do
197
- it "should not be exipired" do
198
- expect(promotion).not_to be_expired
199
- end
200
-
201
- it "should be expired if it hasn't started yet" do
202
- promotion.starts_at = Time.current + 1.day
203
- expect(promotion).to be_expired
204
- end
205
-
206
- it "should be expired if it has already ended" do
207
- promotion.expires_at = Time.current - 1.day
208
- expect(promotion).to be_expired
209
- end
210
-
211
- it "should not be expired if it has started already" do
212
- promotion.starts_at = Time.current - 1.day
213
- expect(promotion).not_to be_expired
214
- end
215
-
216
- it "should not be expired if it has not ended yet" do
217
- promotion.expires_at = Time.current + 1.day
218
- expect(promotion).not_to be_expired
219
- end
220
-
221
- it "should not be expired if current time is within starts_at and expires_at range" do
222
- promotion.starts_at = Time.current - 1.day
223
- promotion.expires_at = Time.current + 1.day
224
- expect(promotion).not_to be_expired
225
- end
226
-
227
- it "should not be expired if usage limit is not exceeded" do
228
- promotion.usage_limit = 2
229
- allow(promotion).to receive_messages(:credits_count => 1)
230
- expect(promotion).not_to be_expired
231
- end
232
- end
233
-
234
- context "#credits_count" do
235
- let!(:promotion) do
236
- promotion = Spree::Promotion.new
237
- promotion.name = "Foo"
238
- promotion.code = "XXX"
239
- calculator = Spree::Calculator::FlatRate.new
240
- promotion.tap(&:save)
241
- end
242
-
243
- let!(:action) do
244
- calculator = Spree::Calculator::FlatRate.new
245
- action_params = { :promotion => promotion, :calculator => calculator }
246
- action = Spree::Promotion::Actions::CreateAdjustment.create(action_params)
247
- promotion.actions << action
248
- action
249
- end
250
-
251
- let!(:adjustment) do
252
- order = create(:order)
253
- Spree::Adjustment.create!(
254
- order: order,
255
- adjustable: order,
256
- source: action,
257
- amount: 10,
258
- label: 'Promotional adjustment'
259
- )
260
- end
261
-
262
- it "counts eligible adjustments" do
263
- adjustment.update_column(:eligible, true)
264
- expect(promotion.credits_count).to eq(1)
265
- end
266
-
267
- # Regression test for #4112
268
- it "does not count ineligible adjustments" do
269
- adjustment.update_column(:eligible, false)
270
- expect(promotion.credits_count).to eq(0)
271
- end
272
- end
273
-
274
- context "#adjusted_credits_count" do
275
- let(:order) { create :order }
276
- let(:line_item) { create :line_item, order: order }
277
- let(:promotion) { Spree::Promotion.create name: "promo", :code => "10off" }
278
- let(:order_action) {
279
- action = Spree::Promotion::Actions::CreateAdjustment.create(calculator: Spree::Calculator::FlatPercentItemTotal.new)
280
- promotion.actions << action
281
- action
282
- }
283
- let(:item_action) {
284
- action = Spree::Promotion::Actions::CreateItemAdjustments.create(calculator: Spree::Calculator::FlatPercentItemTotal.new)
285
- promotion.actions << action
286
- action
287
- }
288
- let(:order_adjustment) do
289
- Spree::Adjustment.create!(
290
- :source => order_action,
291
- :amount => 10,
292
- :adjustable => order,
293
- :order => order,
294
- :label => "Promotional adjustment"
295
- )
296
- end
297
- let(:item_adjustment) do
298
- Spree::Adjustment.create!(
299
- :source => item_action,
300
- :amount => 10,
301
- :adjustable => line_item,
302
- :order => order,
303
- :label => "Promotional adjustment"
304
- )
305
- end
306
-
307
- it "counts order level adjustments" do
308
- expect(order_adjustment.adjustable).to eq(order)
309
- expect(promotion.credits_count).to eq(1)
310
- expect(promotion.adjusted_credits_count(order)).to eq(0)
311
- end
312
-
313
- it "counts item level adjustments" do
314
- expect(item_adjustment.adjustable).to eq(line_item)
315
- expect(promotion.credits_count).to eq(1)
316
- expect(promotion.adjusted_credits_count(order)).to eq(0)
317
- end
318
- end
319
-
320
- context "#products" do
321
- let(:product) { create(:product) }
322
- let(:promotion) { create(:promotion) }
323
-
324
- context "when it has product rules with products associated" do
325
- let(:promotion_rule) { create(:promotion_rule, promotion: promotion, type: 'Spree::Promotion::Rules::Product') }
326
-
327
- before do
328
- promotion.promotion_rules << promotion_rule
329
- product.product_promotion_rules.create(promotion_rule_id: promotion_rule.id)
330
- end
331
-
332
- it "should have products" do
333
- expect(promotion.reload.products.size).to eq(1)
334
- end
335
- end
336
-
337
- context "when there's no product rule associated" do
338
- it "should not have products but still return an empty array" do
339
- expect(promotion.products).to be_blank
340
- end
341
- end
342
- end
343
-
344
- context "#eligible?" do
345
- let(:promotable) { create :order }
346
- subject { promotion.eligible?(promotable) }
347
- context "when promotion is expired" do
348
- before { promotion.expires_at = Time.current - 10.days }
349
- it { is_expected.to be false }
350
- end
351
- context "when promotable is a Spree::LineItem" do
352
- let(:promotable) { create :line_item }
353
- let(:product) { promotable.product }
354
- before do
355
- product.promotionable = promotionable
356
- end
357
- context "and product is promotionable" do
358
- let(:promotionable) { true }
359
- it { is_expected.to be true }
360
- end
361
- context "and product is not promotionable" do
362
- let(:promotionable) { false }
363
- it { is_expected.to be false }
364
- end
365
- end
366
- context "when promotable is a Spree::Order" do
367
- let(:promotable) { create :order }
368
- context "and it is empty" do
369
- it { is_expected.to be true }
370
- end
371
- context "and it contains items" do
372
- let!(:line_item) { create(:line_item, order: promotable) }
373
- context "and the items are all non-promotionable" do
374
- before do
375
- line_item.product.update_column(:promotionable, false)
376
- end
377
- it { is_expected.to be false }
378
- end
379
- context "and at least one item is promotionable" do
380
- it { is_expected.to be true }
381
- end
382
- end
383
- end
384
- end
385
-
386
- context "#eligible_rules" do
387
- let(:promotable) { double('Promotable') }
388
- it "true if there are no rules" do
389
- expect(promotion.eligible_rules(promotable)).to eq []
390
- end
391
-
392
- it "true if there are no applicable rules" do
393
- promotion.promotion_rules = [stub_model(Spree::PromotionRule, :eligible? => true, :applicable? => false)]
394
- allow(promotion.promotion_rules).to receive(:for).and_return([])
395
- expect(promotion.eligible_rules(promotable)).to eq []
396
- end
397
-
398
- context "with 'all' match policy" do
399
- let(:promo1) { Spree::PromotionRule.create! }
400
- let(:promo2) { Spree::PromotionRule.create! }
401
-
402
- before { promotion.match_policy = 'all' }
403
-
404
- context "when all rules are eligible" do
405
- before do
406
- allow(promo1).to receive_messages(eligible?: true, applicable?: true)
407
- allow(promo2).to receive_messages(eligible?: true, applicable?: true)
408
-
409
- promotion.promotion_rules = [promo1, promo2]
410
- allow(promotion.promotion_rules).to receive(:for).and_return(promotion.promotion_rules)
411
- end
412
- it "returns the eligible rules" do
413
- expect(promotion.eligible_rules(promotable)).to eq [promo1, promo2]
414
- end
415
- it "does set anything to eligiblity errors" do
416
- promotion.eligible_rules(promotable)
417
- expect(promotion.eligibility_errors).to be_nil
418
- end
419
- end
420
-
421
- context "when any of the rules is not eligible" do
422
- let(:errors) { double ActiveModel::Errors, empty?: false }
423
- before do
424
- allow(promo1).to receive_messages(eligible?: true, applicable?: true, eligibility_errors: nil)
425
- allow(promo2).to receive_messages(eligible?: false, applicable?: true, eligibility_errors: errors)
426
-
427
- promotion.promotion_rules = [promo1, promo2]
428
- allow(promotion.promotion_rules).to receive(:for).and_return(promotion.promotion_rules)
429
- end
430
- it "returns nil" do
431
- expect(promotion.eligible_rules(promotable)).to be_nil
432
- end
433
- it "sets eligibility errors to the first non-nil one" do
434
- promotion.eligible_rules(promotable)
435
- expect(promotion.eligibility_errors).to eq errors
436
- end
437
- end
438
- end
439
-
440
- context "with 'any' match policy" do
441
- let(:promotion) { Spree::Promotion.create(:name => "Promo", :match_policy => 'any') }
442
- let(:promotable) { double('Promotable') }
443
-
444
- it "should have eligible rules if any of the rules are eligible" do
445
- allow_any_instance_of(Spree::PromotionRule).to receive_messages(:applicable? => true)
446
- true_rule = Spree::PromotionRule.create(:promotion => promotion)
447
- allow(true_rule).to receive_messages(:eligible? => true)
448
- allow(promotion).to receive_messages(:rules => [true_rule])
449
- allow(promotion).to receive_message_chain(:rules, :for).and_return([true_rule])
450
- expect(promotion.eligible_rules(promotable)).to eq [true_rule]
451
- end
452
-
453
- context "when none of the rules are eligible" do
454
- let(:promo) { Spree::PromotionRule.create! }
455
- let(:errors) { double ActiveModel::Errors, empty?: false }
456
- before do
457
- allow(promo).to receive_messages(eligible?: false, applicable?: true, eligibility_errors: errors)
458
-
459
- promotion.promotion_rules = [promo]
460
- allow(promotion.promotion_rules).to receive(:for).and_return(promotion.promotion_rules)
461
- end
462
- it "returns nil" do
463
- expect(promotion.eligible_rules(promotable)).to be_nil
464
- end
465
- it "sets eligibility errors to the first non-nil one" do
466
- promotion.eligible_rules(promotable)
467
- expect(promotion.eligibility_errors).to eq errors
468
- end
469
- end
470
- end
471
- end
472
-
473
- describe '#line_item_actionable?' do
474
- let(:order) { double Spree::Order }
475
- let(:line_item) { double Spree::LineItem}
476
- let(:true_rule) { double Spree::PromotionRule, eligible?: true, applicable?: true, actionable?: true }
477
- let(:false_rule) { double Spree::PromotionRule, eligible?: true, applicable?: true, actionable?: false }
478
- let(:rules) { [] }
479
-
480
- before do
481
- allow(promotion).to receive(:rules) { rules }
482
- allow(rules).to receive(:for) { rules }
483
- end
484
-
485
- subject { promotion.line_item_actionable? order, line_item }
486
-
487
- context 'when the order is eligible for promotion' do
488
- context 'when there are no rules' do
489
- it { is_expected.to be }
490
- end
491
-
492
- context 'when there are rules' do
493
- context 'when the match policy is all' do
494
- before { promotion.match_policy = 'all' }
495
-
496
- context 'when all rules allow action on the line item' do
497
- let(:rules) { [true_rule] }
498
- it { is_expected.to be}
499
- end
500
-
501
- context 'when at least one rule does not allow action on the line item' do
502
- let(:rules) { [true_rule, false_rule] }
503
- it { is_expected.not_to be}
504
- end
505
- end
506
-
507
- context 'when the match policy is any' do
508
- before { promotion.match_policy = 'any' }
509
-
510
- context 'when at least one rule allows action on the line item' do
511
- let(:rules) { [true_rule, false_rule] }
512
- it { is_expected.to be }
513
- end
514
-
515
- context 'when no rules allow action on the line item' do
516
- let(:rules) { [false_rule] }
517
- it { is_expected.not_to be}
518
- end
519
- end
520
- end
521
- end
522
-
523
- context 'when the order is not eligible for the promotion' do
524
- before { promotion.starts_at = Time.current + 2.days }
525
- it { is_expected.not_to be }
526
- end
527
- end
528
-
529
- # regression for #4059
530
- # admin form posts the code and path as empty string
531
- describe "normalize blank values for code & path" do
532
- it "will save blank value as nil value instead" do
533
- promotion = Spree::Promotion.create(:name => "A promotion", :code => "", :path => "")
534
- expect(promotion.code).to be_nil
535
- expect(promotion.path).to be_nil
536
- end
537
- end
538
-
539
- # Regression test for #4081
540
- describe "#with_coupon_code" do
541
- context "and code stored in uppercase" do
542
- let!(:promotion) { create(:promotion, :with_order_adjustment, code: "MY-COUPON-123") }
543
- it "finds the code with lowercase" do
544
- expect(Spree::Promotion.with_coupon_code("my-coupon-123")).to eql promotion
545
- end
546
- end
547
-
548
- context "and there are multiple coupons with the same code" do
549
- context "and only one has any actions" do
550
- let!(:promotion_without_actions) { create(:promotion, code: "MY-COUPON-123").actions.clear }
551
- let!(:promotion) { create(:promotion, :with_order_adjustment, code: "MY-COUPON-123") }
552
-
553
- it "then returns the one with an action" do
554
- expect(Spree::Promotion.with_coupon_code("MY-COUPON-123").actions.exists?).to be
555
- end
556
- end
557
-
558
- context "and all of them has actions" do
559
- let!(:first_promotion) { create(:promotion, :with_order_adjustment, code: "MY-COUPON-123") }
560
- let!(:second_promotion) { create(:promotion, :with_order_adjustment, code: "MY-COUPON-123") }
561
-
562
- it "then returns the first one with an action" do
563
- expect(Spree::Promotion.with_coupon_code("MY-COUPON-123").id).to eq(first_promotion.id)
564
- end
565
- end
566
- end
567
- end
568
-
569
- describe '#used_by?' do
570
- subject { promotion.used_by? user, [excluded_order] }
571
-
572
- let(:promotion) { create :promotion, :with_order_adjustment }
573
- let(:user) { create :user }
574
- let(:order) { create :order_with_line_items, user: user }
575
- let(:excluded_order) { create :order_with_line_items, user: user }
576
-
577
- before do
578
- order.user_id = user.id
579
- order.save!
580
- end
581
-
582
- context 'when the user has used this promo' do
583
- before do
584
- promotion.activate(order: order)
585
- order.update_with_updater!
586
- order.completed_at = Time.current
587
- order.save!
588
- end
589
-
590
- context 'when the order is complete' do
591
- it { is_expected.to be true }
592
-
593
- context 'when the promotion was not eligible' do
594
- let(:adjustment) { order.adjustments.first }
595
-
596
- before do
597
- adjustment.eligible = false
598
- adjustment.save!
599
- end
600
-
601
- it { is_expected.to be false }
602
- end
603
-
604
- context 'when the only matching order is the excluded order' do
605
- let(:excluded_order) { order }
606
- it { is_expected.to be false }
607
- end
608
- end
609
-
610
- context 'when the order is not complete' do
611
- let(:order) { create :order, user: user }
612
- it { is_expected.to be false }
613
- end
614
- end
615
-
616
- context 'when the user has not used this promo' do
617
- it { is_expected.to be false }
618
- end
619
- end
620
-
621
- describe "adding items to the cart" do
622
- let(:order) { create :order }
623
- let(:line_item) { create :line_item, order: order }
624
- let(:promo) { create :promotion_with_item_adjustment, adjustment_rate: 5, code: 'promo' }
625
- let(:variant) { create :variant }
626
-
627
- it "updates the promotions for new line items" do
628
- expect(line_item.adjustments).to be_empty
629
- expect(order.adjustment_total).to eq 0
630
-
631
- promo.activate order: order
632
- order.update_with_updater!
633
-
634
- expect(line_item.adjustments.size).to eq(1)
635
- expect(order.adjustment_total).to eq -5
636
-
637
- other_line_item = order.contents.add(variant, 1, currency: order.currency)
638
-
639
- expect(other_line_item).not_to eq line_item
640
- expect(other_line_item.adjustments.size).to eq(1)
641
- expect(order.adjustment_total).to eq -10
642
- end
643
- end
644
- end