Clone links when cloning product variants

Only links within the same product are supported so far.
This commit is contained in:
David Cook
2026-03-31 15:59:38 +11:00
parent 5beed6f028
commit b11ce2cdfe
2 changed files with 41 additions and 1 deletions

View File

@@ -36,6 +36,7 @@ RSpec.describe Spree::Core::ProductDuplicator do
it "can duplicate a product" do
duplicator = Spree::Core::ProductDuplicator.new(product)
allow(duplicator).to receive(:duplicate_variant_links) # tested elsewhere
expect(new_product).to receive(:name=).with("COPY OF foo")
expect(new_product).to receive(:sku=).with("")
expect(new_product).to receive(:product_properties=).with([new_property])
@@ -65,6 +66,29 @@ RSpec.describe Spree::Core::ProductDuplicator do
end
end
describe "duplicating" do
subject { described_class.new(product).duplicate }
context "with variant links" do
let!(:product) { create(:product) }
let!(:source_variant) { product.variants.first }
let!(:linked_variant1) { source_variant.create_linked_variant(source_variant.supplier.owner) }
let!(:linked_variant2) { source_variant.create_linked_variant(source_variant.supplier.owner) }
it "duplicates variant links" do
expect(subject).to be_a Spree::Product
expect(subject.variants.count).to eq 3
# assuming they are cloned in the same order
new_source_variant = subject.variants[0]
new_linked_variant1 = subject.variants[1]
new_linked_variant2 = subject.variants[2]
expect(new_source_variant.target_variants).to eq [new_linked_variant1, new_linked_variant2]
expect(new_linked_variant1.source_variants).to eq [new_source_variant]
expect(new_linked_variant2.source_variants).to eq [new_source_variant]
end
end
end
describe "errors" do
context "with invalid product" do
# Name has a max length of 255 char, when cloning a product the cloned product has a name