mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-23 05:28:53 +00:00
Merge branch 'master' into product-amount-units
Conflicts: app/assets/javascripts/admin/bulk_product_update.js.coffee spec/spec_helper.rb
This commit is contained in:
@@ -7,59 +7,20 @@ module Spree
|
||||
let(:params) { double(:params) }
|
||||
let(:distributor) { double(:distributor) }
|
||||
let(:order_cycle) { double(:order_cycle) }
|
||||
let(:orig_distributor) { double(:distributor) }
|
||||
let(:orig_order_cycle) { double(:order_cycle) }
|
||||
let(:op) { OrderPopulator.new(order, currency) }
|
||||
|
||||
describe "populate" do
|
||||
|
||||
it "checks that distribution can supply all products in the cart" do
|
||||
op.should_receive(:load_distributor_and_order_cycle).with(params).
|
||||
op.should_receive(:distributor_and_order_cycle).
|
||||
and_return([distributor, order_cycle])
|
||||
op.should_receive(:distribution_can_supply_products_in_cart).
|
||||
with(distributor, order_cycle).and_return(false)
|
||||
op.stub(:orig_distributor_and_order_cycle).and_return([orig_distributor,
|
||||
orig_order_cycle])
|
||||
op.should_receive(:populate_without_distribution_validation).never
|
||||
op.should_receive(:set_cart_distributor_and_order_cycle).never
|
||||
|
||||
op.populate(params).should be_false
|
||||
op.errors.to_a.should == ["That distributor or order cycle can't supply all the products in your cart. Please choose another."]
|
||||
end
|
||||
|
||||
it "resets cart distributor and order cycle if populate fails" do
|
||||
op.should_receive(:load_distributor_and_order_cycle).with(params).
|
||||
and_return([distributor, order_cycle])
|
||||
op.should_receive(:distribution_can_supply_products_in_cart).
|
||||
with(distributor, order_cycle).and_return(true)
|
||||
op.stub(:orig_distributor_and_order_cycle).and_return([orig_distributor,
|
||||
orig_order_cycle])
|
||||
|
||||
op.class_eval do
|
||||
def populate_without_distribution_validation(from_hash)
|
||||
errors.add(:base, "Something went wrong.")
|
||||
end
|
||||
end
|
||||
|
||||
op.should_receive(:set_cart_distributor_and_order_cycle).with(distributor, order_cycle)
|
||||
op.should_receive(:set_cart_distributor_and_order_cycle).with(orig_distributor, orig_order_cycle)
|
||||
|
||||
op.populate(params).should be_false
|
||||
op.errors.to_a.should == ["Something went wrong."]
|
||||
end
|
||||
|
||||
it "sets cart distributor and order cycle when populate succeeds" do
|
||||
op.should_receive(:load_distributor_and_order_cycle).with(params).
|
||||
and_return([distributor, order_cycle])
|
||||
op.should_receive(:distribution_can_supply_products_in_cart).
|
||||
with(distributor, order_cycle).and_return(true)
|
||||
op.stub(:orig_distributor_and_order_cycle).and_return([orig_distributor,
|
||||
orig_order_cycle])
|
||||
op.should_receive(:populate_without_distribution_validation).with(params)
|
||||
op.should_receive(:set_cart_distributor_and_order_cycle).with(distributor, order_cycle)
|
||||
|
||||
op.populate(params).should be_true
|
||||
end
|
||||
end
|
||||
|
||||
describe "attempt_cart_add" do
|
||||
@@ -69,7 +30,7 @@ module Spree
|
||||
Spree::Variant.stub(:find).and_return(variant)
|
||||
|
||||
op.should_receive(:check_stock_levels).with(variant, quantity).and_return(true)
|
||||
op.should_receive(:check_distribution_provided_for).with(variant).and_return(true)
|
||||
op.should_receive(:check_order_cycle_provided_for).with(variant).and_return(true)
|
||||
op.should_receive(:check_variant_available_under_distribution).with(variant).
|
||||
and_return(true)
|
||||
order.should_receive(:add_variant).with(variant, quantity, currency)
|
||||
@@ -89,30 +50,22 @@ module Spree
|
||||
end
|
||||
end
|
||||
|
||||
describe "checking distribution is provided for a variant" do
|
||||
describe "checking order cycle is provided for a variant, OR is not needed" do
|
||||
let(:variant) { double(:variant) }
|
||||
|
||||
it "returns false and errors when distribution is not provided and order cycle is required" do
|
||||
op.should_receive(:distribution_provided_for).with(variant).and_return(false)
|
||||
op.should_receive(:order_cycle_required_for).with(variant).and_return(true)
|
||||
|
||||
op.send(:check_distribution_provided_for, variant).should be_false
|
||||
op.errors.to_a.should == ["Please choose a distributor and order cycle for this order."]
|
||||
it "returns false and errors when order cycle is not provided and is required" do
|
||||
op.stub(:order_cycle_required_for).and_return true
|
||||
op.send(:check_order_cycle_provided_for, variant).should be_false
|
||||
op.errors.to_a.should == ["Please choose an order cycle for this order."]
|
||||
end
|
||||
|
||||
it "returns false and errors when distribution is not provided and order cycle is not required" do
|
||||
op.should_receive(:distribution_provided_for).with(variant).and_return(false)
|
||||
op.should_receive(:order_cycle_required_for).with(variant).and_return(false)
|
||||
|
||||
op.send(:check_distribution_provided_for, variant).should be_false
|
||||
op.errors.to_a.should == ["Please choose a distributor for this order."]
|
||||
it "returns true when order cycle is provided" do
|
||||
op.stub(:order_cycle_required_for).and_return true
|
||||
op.instance_variable_set :@order_cycle, double(:order_cycle)
|
||||
op.send(:check_order_cycle_provided_for, variant).should be_true
|
||||
end
|
||||
|
||||
it "returns true and does not error otherwise" do
|
||||
op.should_receive(:distribution_provided_for).with(variant).and_return(true)
|
||||
|
||||
op.send(:check_distribution_provided_for, variant).should be_true
|
||||
op.errors.should be_empty
|
||||
it "returns true when order cycle is not required" do
|
||||
op.stub(:order_cycle_required_for).and_return false
|
||||
op.send(:check_order_cycle_provided_for, variant).should be_true
|
||||
end
|
||||
end
|
||||
|
||||
@@ -142,60 +95,6 @@ module Spree
|
||||
|
||||
|
||||
describe "support" do
|
||||
describe "loading distributor and order cycle from hash" do
|
||||
it "loads distributor and order cycle when present" do
|
||||
params = {distributor_id: 1, order_cycle_id: 2}
|
||||
distributor = double(:distributor)
|
||||
order_cycle = double(:order_cycle)
|
||||
|
||||
enterprise_scope = double(:enterprise_scope)
|
||||
enterprise_scope.should_receive(:find).with(1).and_return(distributor)
|
||||
Enterprise.should_receive(:is_distributor).and_return(enterprise_scope)
|
||||
OrderCycle.should_receive(:find).with(2).and_return(order_cycle)
|
||||
|
||||
op.send(:load_distributor_and_order_cycle, params).should ==
|
||||
[distributor, order_cycle]
|
||||
end
|
||||
|
||||
it "returns nil when not present" do
|
||||
op.send(:load_distributor_and_order_cycle, {}).should == [nil, nil]
|
||||
end
|
||||
end
|
||||
|
||||
it "sets cart distributor and order cycle" do
|
||||
Spree::Order.should_receive(:find).with(order.id).and_return(order)
|
||||
order.should_receive(:set_distribution!).with(distributor, order_cycle)
|
||||
|
||||
op.send(:set_cart_distributor_and_order_cycle, distributor, order_cycle)
|
||||
end
|
||||
|
||||
describe "checking if distribution is provided for a variant" do
|
||||
let(:variant) { double(:variant) }
|
||||
|
||||
it "returns false when distributor is nil" do
|
||||
op.instance_eval { @distributor = nil }
|
||||
op.send(:distribution_provided_for, variant).should be_false
|
||||
end
|
||||
|
||||
it "returns false when order cycle is nil when it's required" do
|
||||
op.instance_eval { @distributor = 1; @order_cycle = nil }
|
||||
op.should_receive(:order_cycle_required_for).with(variant).and_return(true)
|
||||
op.send(:distribution_provided_for, variant).should be_false
|
||||
end
|
||||
|
||||
it "returns true when distributor is present and order cycle is not required" do
|
||||
op.instance_eval { @distributor = 1; @order_cycle = nil }
|
||||
op.should_receive(:order_cycle_required_for).with(variant).and_return(false)
|
||||
op.send(:distribution_provided_for, variant).should be_true
|
||||
end
|
||||
|
||||
it "returns true when distributor is present and order cycle is required and present" do
|
||||
op.instance_eval { @distributor = 1; @order_cycle = 1 }
|
||||
op.should_receive(:order_cycle_required_for).with(variant).and_return(true)
|
||||
op.send(:distribution_provided_for, variant).should be_true
|
||||
end
|
||||
end
|
||||
|
||||
describe "checking if order cycle is required for a variant" do
|
||||
it "requires an order cycle when the product has no product distributions" do
|
||||
product = double(:product, product_distributions: [])
|
||||
@@ -210,11 +109,11 @@ module Spree
|
||||
end
|
||||
end
|
||||
|
||||
it "provides the original distributor and order cycle for the order" do
|
||||
order.should_receive(:distributor).and_return(orig_distributor)
|
||||
order.should_receive(:order_cycle).and_return(orig_order_cycle)
|
||||
op.send(:orig_distributor_and_order_cycle).should == [orig_distributor,
|
||||
orig_order_cycle]
|
||||
it "provides the distributor and order cycle for the order" do
|
||||
order.should_receive(:distributor).and_return(distributor)
|
||||
order.should_receive(:order_cycle).and_return(order_cycle)
|
||||
op.send(:distributor_and_order_cycle).should == [distributor,
|
||||
order_cycle]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -15,7 +15,7 @@ module Spree
|
||||
sm.distributors << d1
|
||||
sm.distributors << d2
|
||||
|
||||
sm.reload.distributors.should == [d1, d2]
|
||||
sm.reload.distributors.sort.should == [d1, d2].sort
|
||||
end
|
||||
|
||||
it "finds shipping methods for a particular distributor" do
|
||||
|
||||
Reference in New Issue
Block a user