diff --git a/app/models/spree/product.rb b/app/models/spree/product.rb index 4e603ab48c..e08012beea 100755 --- a/app/models/spree/product.rb +++ b/app/models/spree/product.rb @@ -114,6 +114,7 @@ module Spree presence: { if: ->(p) { %w(weight volume).include? p.variant_unit } } validates :variant_unit_name, presence: { if: ->(p) { p.variant_unit == 'items' } } + validate :validate_image_for_master attr_accessor :option_values_hash @@ -474,5 +475,11 @@ module Spree requested = permalink.presence || permalink_was.presence || name.presence || 'product' self.permalink = create_unique_permalink(requested.parameterize) end + + def validate_image_for_master + return if master.images.all?(&:valid?) + + errors.add(:base, I18n.t('spree.admin.products.image_not_processable')) + end end end diff --git a/config/locales/en.yml b/config/locales/en.yml index e0aab6e77e..c5079f8ebd 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -4140,6 +4140,7 @@ See the %{link} to find out more about %{sitename}'s features and to start using no_payment_via_admin_backend: Paypal payments cannot be captured in the Backoffice products: image_upload_error: "Please upload the image in JPG, PNG, GIF, SVG or WEBP format." + image_not_processable: "Image attachment is not a valid image." new: title: "New Product" new_product: "New Product" diff --git a/spec/controllers/api/v0/product_images_controller_spec.rb b/spec/controllers/api/v0/product_images_controller_spec.rb index c40785ba2c..75c4a53f20 100644 --- a/spec/controllers/api/v0/product_images_controller_spec.rb +++ b/spec/controllers/api/v0/product_images_controller_spec.rb @@ -25,7 +25,7 @@ describe Api::V0::ProductImagesController, type: :controller do } expect(response.status).to eq 201 - expect(product_without_image.images.first.id).to eq json_response['id'] + expect(product_without_image.reload.images.first.id).to eq json_response['id'] end it "updates an existing product image" do @@ -34,7 +34,7 @@ describe Api::V0::ProductImagesController, type: :controller do } expect(response.status).to eq 200 - expect(product_with_image.images.first.id).to eq json_response['id'] + expect(product_with_image.reload.images.first.id).to eq json_response['id'] end it "reports errors when saving fails" do diff --git a/spec/models/spree/product_spec.rb b/spec/models/spree/product_spec.rb index 096fbd5db7..63f8e1b9a0 100644 --- a/spec/models/spree/product_spec.rb +++ b/spec/models/spree/product_spec.rb @@ -460,6 +460,25 @@ module Spree expect(product).not_to be_valid end end + + describe "#validate_image_for_master" do + let(:product) { build_stubbed(:simple_product) } + + context "when the image attached to the master variant is invalid" do + before { product.master.images.new.errors.add(:image_not_processable, "invalid") } + + it "adds an error message to the base object" do + expect(product).not_to be_valid + expect(product.errors[:base]).to include('Image attachment is not a valid image.') + end + end + + context "when master variant is valid" do + it "returns true" do + expect(product).to be_valid + end + end + end end describe "callbacks" do diff --git a/spec/services/image_importer_spec.rb b/spec/services/image_importer_spec.rb index 9ec390d25f..2c0ed2f7ad 100644 --- a/spec/services/image_importer_spec.rb +++ b/spec/services/image_importer_spec.rb @@ -15,7 +15,7 @@ describe ImageImporter do }.by(1) expect(product.images.count).to eq 1 - expect(product.images.first.attachment_blob.byte_size).to eq 6274 + expect(product.reload.images.first.attachment_blob.byte_size).to eq 6274 end end end