From b0433bd8f53ff67ba90993f19e18b1727f15e46b Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Tue, 27 Aug 2024 10:41:33 +1000 Subject: [PATCH] Re add test for invalid cloned products There is no easy way to make the original product invalid, but we can make sure the cloned product will be invalid. The cloned product add "COPe OF " in front of the product's name, so by starting with a name that's long enough, the cloned product will have a name longer that 255 char and will then be invalid. --- spec/lib/spree/core/product_duplicator_spec.rb | 15 +++++++++++++++ spec/models/spree/product_spec.rb | 7 +++++++ spec/system/admin/products_v3/actions_spec.rb | 15 +++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/spec/lib/spree/core/product_duplicator_spec.rb b/spec/lib/spree/core/product_duplicator_spec.rb index bab00af468..e8163e961d 100644 --- a/spec/lib/spree/core/product_duplicator_spec.rb +++ b/spec/lib/spree/core/product_duplicator_spec.rb @@ -68,6 +68,21 @@ RSpec.describe Spree::Core::ProductDuplicator do 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 + # starting with "COPY OF ". So we set a name with 254 char to make sure the + # cloned product will be invalid + let(:product) { + create(:product).tap{ |v| v.update_columns(name: "l" * 254) } + } + + subject { Spree::Core::ProductDuplicator.new(product).duplicate } + + it "raises RecordInvalid error" do + expect{ subject }.to raise_error(ActiveRecord::ActiveRecordError) + end + end + context "invalid variant" do let(:variant) { # tax_category is required when products_require_tax_category diff --git a/spec/models/spree/product_spec.rb b/spec/models/spree/product_spec.rb index e5919a4463..df80c8f052 100644 --- a/spec/models/spree/product_spec.rb +++ b/spec/models/spree/product_spec.rb @@ -17,6 +17,13 @@ module Spree expect(clone.sku).to eq "" expect(clone.image).to eq product.image end + + it 'fails to duplicate invalid product' do + # cloned product will be invalid + product.update_columns(name: "l" * 254) + + expect{ product.duplicate }.to raise_error(ActiveRecord::ActiveRecordError) + end end context "product has variants" do diff --git a/spec/system/admin/products_v3/actions_spec.rb b/spec/system/admin/products_v3/actions_spec.rb index 9abb75cd56..872b19ae7d 100644 --- a/spec/system/admin/products_v3/actions_spec.rb +++ b/spec/system/admin/products_v3/actions_spec.rb @@ -294,6 +294,21 @@ RSpec.describe 'As an enterprise user, I can manage my products' do end end end + + it "shows error message when cloning invalid record" do + # The cloned product will be invalid + product_a.update_columns(name: "L" * 254) + + # The page has not been reloaded so the product's name is still "Apples" + click_product_clone "Apples" + + expect(page).to have_content "Unable to clone the product" + + within "table.products" do + # Products does not include the cloned product. + expect(all_input_values).not_to match /COPY OF #{('L' * 254)}/ + end + end end describe "delete" do