Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Cover collection-rule apply fallbacks and collection PLP default sort

Łukasz Adamczykcdae1d6d76dc
diff --git a/spree/api/spec/controllers/spree/api/v3/store/collections/products_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/collections/products_controller_spec.rb
index 9878bf65b6..4412c34c47 100644
--- a/spree/api/spec/controllers/spree/api/v3/store/collections/products_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/store/collections/products_controller_spec.rb
@@ -37,6 +37,21 @@ RSpec.describe Spree::Api::V3::Store::Collections::ProductsController, type: :co
expect(response).to have_http_status(:not_found)
end
+ # When `sort` is omitted, the collection's sort_order drives the default,
+ # rendered from the internal space format ('price asc') to the API sort ('price').
+ it "defaults to the collection's sort_order when the request omits sort" do
+ cheap = create(:product, status: 'active', price: 5)
+ pricey = create(:product, status: 'active', price: 50)
+ sorted = create(:collection, store: store, sort_order: 'price asc')
+ Spree::ProductCollection.create!(collection: sorted, product: pricey, position: 1)
+ Spree::ProductCollection.create!(collection: sorted, product: cheap, position: 2)
+
+ get :index, params: { collection_id: sorted.prefixed_id }
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['data'].pluck('id')).to eq([cheap.prefixed_id, pricey.prefixed_id])
+ end
+
# Mirrors Store::CollectionsController#find_resource: a permalink that only
# exists in the default locale must still resolve the PLP on a non-default
# locale, so the detail page and its product listing stay in sync.
diff --git a/spree/core/spec/models/spree/collection_rules/available_on_spec.rb b/spree/core/spec/models/spree/collection_rules/available_on_spec.rb
index 31291712a1..f02af856e3 100644
--- a/spree/core/spec/models/spree/collection_rules/available_on_spec.rb
+++ b/spree/core/spec/models/spree/collection_rules/available_on_spec.rb
@@ -18,5 +18,13 @@ RSpec.describe Spree::CollectionRules::AvailableOn, type: :model do
expect(result).to include(recent_product)
expect(result).not_to include(old_product)
end
+
+ it 'returns the scope unchanged for an unhandled match policy' do
+ rule = build(:available_on_collection_rule, collection: collection, value: 30, match_policy: 'is_not_equal_to')
+
+ result = rule.apply(store.products.not_archived)
+
+ expect(result).to include(recent_product, old_product)
+ end
end
end
diff --git a/spree/core/spec/models/spree/collection_rules/sale_spec.rb b/spree/core/spec/models/spree/collection_rules/sale_spec.rb
new file mode 100644
index 0000000000..6bf743ca0f
--- /dev/null
+++ b/spree/core/spec/models/spree/collection_rules/sale_spec.rb
@@ -0,0 +1,21 @@
+require 'spec_helper'
+
+RSpec.describe Spree::CollectionRules::Sale, type: :model do
+ let(:store) { @default_store }
+ let(:collection) { create(:automatic_collection, store: store) }
+
+ # The four match policies are exercised via Collection#products_matching_rules;
+ # here we cover the else (unknown-policy) fallback directly. build, not create,
+ # bypasses the regeneration callback and the match_policy inclusion validation.
+ describe '#apply' do
+ let!(:on_sale) { create(:product, price: 10, compare_at_price: 12) }
+ let!(:regular) { create(:product, price: 10) }
+ let(:scope) { store.products.where(id: [on_sale.id, regular.id]) }
+
+ it 'returns the scope unchanged for an unknown match policy' do
+ rule = build(:sale_collection_rule, collection: collection, value: true, match_policy: 'bogus')
+
+ expect(rule.apply(scope)).to contain_exactly(on_sale, regular)
+ end
+ end
+end