From c5303b92bfe6733b76e095a6e687095f61ed931c Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Wed, 5 Jun 2024 09:41:55 +0200 Subject: [PATCH 1/2] App configuration: Use SimpleOrderContents by default With the null promotion configuration in place, we can't use the legacy Spree::OrderContents class, as that makes references to the legacy promotion system. See https://github.com/solidusio/solidus/pull/5755 --- api/spec/requests/spree/api/orders_spec.rb | 14 ------ core/lib/spree/app_configuration.rb | 2 +- core/spec/lib/spree/app_configuration_spec.rb | 4 ++ core/spec/models/spree/promotion_spec.rb | 25 ---------- .../spree/promotion_integration_spec.rb | 25 ++++++++++ .../spec/requests/spree/api/orders_spec.rb | 50 +++++++++++++++++++ 6 files changed, 80 insertions(+), 40 deletions(-) create mode 100644 legacy_promotions/spec/requests/spree/api/orders_spec.rb diff --git a/api/spec/requests/spree/api/orders_spec.rb b/api/spec/requests/spree/api/orders_spec.rb index 4ca347a2abb..4d3a5410220 100644 --- a/api/spec/requests/spree/api/orders_spec.rb +++ b/api/spec/requests/spree/api/orders_spec.rb @@ -77,20 +77,6 @@ module Spree::Api end end end - - context "with existing promotion" do - let(:discount) { 2 } - before do - create(:promotion, :with_line_item_adjustment, apply_automatically: true, adjustment_rate: discount ) - end - - it "activates the promotion" do - post spree.api_orders_path, params: { order: { line_items: { "0" => { variant_id: variant.to_param, quantity: 1 } } } } - order = Spree::Order.last - line_item = order.line_items.first - expect(order.total).to eq(line_item.price - discount) - end - end end context "when the current user can administrate the order" do diff --git a/core/lib/spree/app_configuration.rb b/core/lib/spree/app_configuration.rb index b10ddde992a..37d48e16e2e 100644 --- a/core/lib/spree/app_configuration.rb +++ b/core/lib/spree/app_configuration.rb @@ -394,7 +394,7 @@ def default_pricing_options # @!attribute [rw] order_contents_class # @return [Class] a class with the same public interfaces as # Spree::OrderContents. - class_name_attribute :order_contents_class, default: 'Spree::OrderContents' + class_name_attribute :order_contents_class, default: 'Spree::SimpleOrderContents' # Allows providing your own class for shipping an order. # diff --git a/core/spec/lib/spree/app_configuration_spec.rb b/core/spec/lib/spree/app_configuration_spec.rb index 7ffc0e84d69..6770b9c5567 100644 --- a/core/spec/lib/spree/app_configuration_spec.rb +++ b/core/spec/lib/spree/app_configuration_spec.rb @@ -36,6 +36,10 @@ expect(prefs.variant_search_class).to eq Spree::Core::Search::Variant end + it "uses simple order contents class by default" do + expect(prefs.order_contents_class).to eq Spree::SimpleOrderContents + end + it "uses variant price selector class by default" do expect(prefs.variant_price_selector_class).to eq Spree::Variant::PriceSelector end diff --git a/core/spec/models/spree/promotion_spec.rb b/core/spec/models/spree/promotion_spec.rb index 449e06730b6..c4b696192fa 100644 --- a/core/spec/models/spree/promotion_spec.rb +++ b/core/spec/models/spree/promotion_spec.rb @@ -915,31 +915,6 @@ end end - describe "adding items to the cart" do - let(:order) { create :order } - let(:line_item) { create :line_item, order: order } - let(:promo) { create :promotion_with_item_adjustment, adjustment_rate: 5, code: 'promo' } - let(:promotion_code) { promo.codes.first } - let(:variant) { create :variant } - - it "updates the promotions for new line items" do - expect(line_item.adjustments).to be_empty - expect(order.adjustment_total).to eq 0 - - promo.activate order: order, promotion_code: promotion_code - order.recalculate - - expect(line_item.adjustments.size).to eq(1) - expect(order.adjustment_total).to eq(-5) - - other_line_item = order.contents.add(variant, 1, currency: order.currency) - - expect(other_line_item).not_to eq line_item - expect(other_line_item.adjustments.size).to eq(1) - expect(order.adjustment_total).to eq(-10) - end - end - describe "promotion deletion" do subject { promotion.destroy! } diff --git a/legacy_promotions/spec/models/spree/promotion_integration_spec.rb b/legacy_promotions/spec/models/spree/promotion_integration_spec.rb index 90ad8f194da..6ed19aba4cb 100644 --- a/legacy_promotions/spec/models/spree/promotion_integration_spec.rb +++ b/legacy_promotions/spec/models/spree/promotion_integration_spec.rb @@ -181,4 +181,29 @@ }.to change{ order.reload.state }.from("confirm").to("address") end end + + describe "adding items to the cart" do + let(:order) { create :order } + let(:line_item) { create :line_item, order: order } + let(:promo) { create :promotion_with_item_adjustment, adjustment_rate: 5, code: 'promo' } + let(:promotion_code) { promo.codes.first } + let(:variant) { create :variant } + + it "updates the promotions for new line items" do + expect(line_item.adjustments).to be_empty + expect(order.adjustment_total).to eq 0 + + promo.activate order: order, promotion_code: promotion_code + order.recalculate + + expect(line_item.adjustments.size).to eq(1) + expect(order.adjustment_total).to eq(-5) + + other_line_item = order.contents.add(variant, 1, currency: order.currency) + + expect(other_line_item).not_to eq line_item + expect(other_line_item.adjustments.size).to eq(1) + expect(order.adjustment_total).to eq(-10) + end + end end diff --git a/legacy_promotions/spec/requests/spree/api/orders_spec.rb b/legacy_promotions/spec/requests/spree/api/orders_spec.rb new file mode 100644 index 00000000000..3fc4f700f45 --- /dev/null +++ b/legacy_promotions/spec/requests/spree/api/orders_spec.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'API Orders', type: :request do + let!(:order) { create(:order) } + let(:variant) { create(:variant) } + let(:line_item) { create(:line_item) } + let(:address_params) { { country_id: Country.first.id, state_id: State.first.id } } + + let(:current_api_user) do + user = Spree.user_class.new(email: "solidus@example.com") + user.generate_spree_api_key! + user + end + + before do + stub_authentication! + end + + describe "POST create" do + let(:target_user) { create :user } + let(:attributes) { { user_id: target_user.id, email: target_user.email } } + + subject do + post spree.api_orders_path, params: { order: attributes } + response + end + + context "when the current user cannot administrate the order" do + custom_authorization! do |_| + can :create, Spree::Order + end + + context "with existing promotion" do + let(:discount) { 2 } + before do + create(:promotion, :with_line_item_adjustment, apply_automatically: true, adjustment_rate: discount ) + end + + it "activates the promotion" do + post spree.api_orders_path, params: { order: { line_items: { "0" => { variant_id: variant.to_param, quantity: 1 } } } } + order = Spree::Order.last + line_item = order.line_items.first + expect(order.total).to eq(line_item.price - discount) + end + end + end + end +end From 44387447e7625a8df480f8db3519ce7278bc474e Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Thu, 6 Jun 2024 16:53:18 +0200 Subject: [PATCH 2/2] Move Spree::OrderContents to LegacyPromotions gem This class directly references Spree::PromotionHandler::Cart, and thus needs to live in the legacy_promotions gem. If you inherit from this class, and it happens to not be available after upgrading, please add ``` gem "solidus_legacy_promotions" ``` to your Gemfile. --- backend/spec/controllers/spree/admin/orders_controller_spec.rb | 2 +- {core => legacy_promotions}/app/models/spree/order_contents.rb | 0 .../spec/models/spree/order_contents_spec.rb | 0 3 files changed, 1 insertion(+), 1 deletion(-) rename {core => legacy_promotions}/app/models/spree/order_contents.rb (100%) rename {core => legacy_promotions}/spec/models/spree/order_contents_spec.rb (100%) diff --git a/backend/spec/controllers/spree/admin/orders_controller_spec.rb b/backend/spec/controllers/spree/admin/orders_controller_spec.rb index 491c2b9d523..a9b4028198b 100644 --- a/backend/spec/controllers/spree/admin/orders_controller_spec.rb +++ b/backend/spec/controllers/spree/admin/orders_controller_spec.rb @@ -27,7 +27,7 @@ before do allow(Spree::Order).to receive_message_chain(:includes, find_by!: order) - allow(order).to receive_messages(contents: Spree::OrderContents.new(order)) + allow(order).to receive_messages(contents: Spree::Config.order_contents_class.new(order)) end context "#approve" do diff --git a/core/app/models/spree/order_contents.rb b/legacy_promotions/app/models/spree/order_contents.rb similarity index 100% rename from core/app/models/spree/order_contents.rb rename to legacy_promotions/app/models/spree/order_contents.rb diff --git a/core/spec/models/spree/order_contents_spec.rb b/legacy_promotions/spec/models/spree/order_contents_spec.rb similarity index 100% rename from core/spec/models/spree/order_contents_spec.rb rename to legacy_promotions/spec/models/spree/order_contents_spec.rb