From 3e53601130cd8ad29af8a93e05330d6150717660 Mon Sep 17 00:00:00 2001 From: Alex Akimov Date: Mon, 18 Apr 2016 13:23:00 +0300 Subject: [PATCH 1/8] MAGETWO-51943: Required varchar and text EAV Attributes Can be Saved as Empty String --- .../Entity/Attribute/AbstractAttribute.php | 16 +++--- .../Attribute/AbstractAttributeTest.php | 55 +++++++++++++++++++ 2 files changed, 62 insertions(+), 9 deletions(-) diff --git a/app/code/Magento/Eav/Model/Entity/Attribute/AbstractAttribute.php b/app/code/Magento/Eav/Model/Entity/Attribute/AbstractAttribute.php index a3296e738491c..d21ed2142312d 100644 --- a/app/code/Magento/Eav/Model/Entity/Attribute/AbstractAttribute.php +++ b/app/code/Magento/Eav/Model/Entity/Attribute/AbstractAttribute.php @@ -593,15 +593,13 @@ protected function _getDefaultSourceModel() */ public function isValueEmpty($value) { - $attrType = $this->getBackend()->getType(); - $isEmpty = (is_array($value) && count($value) == 0) || - $value === null || - $value === false && $attrType != 'int' || - $value === '' && ($attrType == 'int' || - $attrType == 'decimal' || - $attrType == 'datetime'); - - return $isEmpty; + /** @var array $emptyStringTypes list of attribute types that treat empty string as a possible value */ + $emptyStringTypes = ['int', 'decimal', 'datetime', 'varchar', 'text']; + $attributeType = $this->getBackend()->getType(); + return (is_array($value) && count($value) == 0) + || $value === null + || ($value === false && $attributeType != 'int') + || ($value === '' && in_array($attributeType, $emptyStringTypes)); } /** diff --git a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/AbstractAttributeTest.php b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/AbstractAttributeTest.php index 873b4680a0dff..1d0364fb7638f 100644 --- a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/AbstractAttributeTest.php +++ b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/AbstractAttributeTest.php @@ -175,4 +175,59 @@ public function testGetValidationRulesWhenRuleIsEmpty() $this->assertEquals([], $model->getValidationRules()); } + + /** + * @param bool $isEmpty + * @param mixed $value + * @param string $attributeType + * @dataProvider attributeValueDataProvider + */ + public function testIsValueEmpty($isEmpty, $value, $attributeType) + { + /** @var \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $model */ + $model = $this->getMockForAbstractClass( + '\Magento\Eav\Model\Entity\Attribute\AbstractAttribute', + [], + '', + false, + false, + true, + [ + 'getBackend' + ] + ); + $backendModelMock = $this->getMockForAbstractClass( + '\Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend', + [], + '', + false, + false, + true, + [ + 'getType' + ] + ); + $backendModelMock->expects($this->any())->method('getType')->willReturn($attributeType); + $model->expects($this->once())->method('getBackend')->willReturn($backendModelMock); + $this->assertEquals($isEmpty, $model->isValueEmpty($value)); + } + + /** + * @return array + */ + public function attributeValueDataProvider() + { + return [ + [true, '', 'int'], + [true, '', 'decimal'], + [true, '', 'datetime'], + [true, '', 'varchar'], + [true, '', 'text'], + [true, null, 'varchar'], + [true, [], 'varchar'], + [true, false, 'varchar'], + [false, 'not empty value', 'varchar'], + [false, false, 'int'], + ]; + } } From bde57fa677a5c01789c4c415af092b78d14915e1 Mon Sep 17 00:00:00 2001 From: Iryna Lagno Date: Wed, 20 Apr 2016 16:19:25 +0300 Subject: [PATCH 2/8] MAGETWO-52011: Second error message is not displayed when adding product with two invalid file fields to the cart --- .../Model/Product/Type/AbstractType.php | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/app/code/Magento/Catalog/Model/Product/Type/AbstractType.php b/app/code/Magento/Catalog/Model/Product/Type/AbstractType.php index 73153aa5a64e5..88b039473f38f 100644 --- a/app/code/Magento/Catalog/Model/Product/Type/AbstractType.php +++ b/app/code/Magento/Catalog/Model/Product/Type/AbstractType.php @@ -7,7 +7,7 @@ use Magento\Catalog\Api\ProductRepositoryInterface; use Magento\Framework\App\Filesystem\DirectoryList; - +use Magento\Framework\Exception\LocalizedException; /** * @api * Abstract model for product type implementation @@ -561,6 +561,7 @@ public function getSpecifyOptionMessage() * @param \Magento\Catalog\Model\Product $product * @param string $processMode * @return array + * @throws LocalizedException */ protected function _prepareOptions(\Magento\Framework\DataObject $buyRequest, $product, $processMode) { @@ -571,20 +572,29 @@ protected function _prepareOptions(\Magento\Framework\DataObject $buyRequest, $p $options = $product->getOptions(); } if ($options !== null) { + $result = []; foreach ($options as $option) { /* @var $option \Magento\Catalog\Model\Product\Option */ - $group = $option->groupFactory($option->getType()) - ->setOption($option) - ->setProduct($product) - ->setRequest($buyRequest) - ->setProcessMode($processMode) - ->validateUserValue($buyRequest->getOptions()); + try { + $group = $option->groupFactory($option->getType()) + ->setOption($option) + ->setProduct($product) + ->setRequest($buyRequest) + ->setProcessMode($processMode) + ->validateUserValue($buyRequest->getOptions()); + } catch (\Exception $e) { + $result[] = $e->getMessage(); + continue; + } $preparedValue = $group->prepareForCart(); if ($preparedValue !== null) { $transport->options[$option->getId()] = $preparedValue; } } + if (count($result) > 0) { + throw new LocalizedException(__(implode("\n", $result))); + } } $eventName = sprintf('catalog_product_type_prepare_%s_options', $processMode); From 628687b491d2d95e164388bb2f4b97be41a1e211 Mon Sep 17 00:00:00 2001 From: Alex Akimov Date: Wed, 20 Apr 2016 17:26:10 +0300 Subject: [PATCH 3/8] MAGETWO-52013: DblClick on "Add to cart" Button --- .../view/frontend/web/js/catalog-add-to-cart.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Catalog/view/frontend/web/js/catalog-add-to-cart.js b/app/code/Magento/Catalog/view/frontend/web/js/catalog-add-to-cart.js index ec9b0a87ff671..0da521c07d8ba 100644 --- a/app/code/Magento/Catalog/view/frontend/web/js/catalog-add-to-cart.js +++ b/app/code/Magento/Catalog/view/frontend/web/js/catalog-add-to-cart.js @@ -44,10 +44,20 @@ define([ return this.options.processStart && this.options.processStop; }, - submitForm: function(form) { - var self = this; + /** + * Handler for the form 'submit' event + * + * @param {Object} form + */ + submitForm: function (form) { + var addToCartButton, self = this; + if (form.has('input[type="file"]').length && form.find('input[type="file"]').val() !== '') { self.element.off('submit'); + // disable 'Add to Cart' button + addToCartButton = $(form).find(this.options.addToCartButtonSelector); + addToCartButton.prop('disabled', true); + addToCartButton.addClass(this.options.addToCartButtonDisabledClass); form.submit(); } else { self.ajaxSubmit(form); From 0bdb94ea63619ce1dfd221750d5311ff6a8b4fc8 Mon Sep 17 00:00:00 2001 From: Iryna Lagno Date: Wed, 20 Apr 2016 18:11:57 +0300 Subject: [PATCH 4/8] MAGETWO-52011: Second error message is not displayed when adding product with two invalid file fields to the cart --- app/code/Magento/Catalog/Model/Product/Type/AbstractType.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Catalog/Model/Product/Type/AbstractType.php b/app/code/Magento/Catalog/Model/Product/Type/AbstractType.php index 88b039473f38f..03b2418a12c7a 100644 --- a/app/code/Magento/Catalog/Model/Product/Type/AbstractType.php +++ b/app/code/Magento/Catalog/Model/Product/Type/AbstractType.php @@ -582,7 +582,7 @@ protected function _prepareOptions(\Magento\Framework\DataObject $buyRequest, $p ->setRequest($buyRequest) ->setProcessMode($processMode) ->validateUserValue($buyRequest->getOptions()); - } catch (\Exception $e) { + } catch (LocalizedException $e) { $result[] = $e->getMessage(); continue; } From ba908723d0a90f1d0fd0a0e45af2acbd7a9fb313 Mon Sep 17 00:00:00 2001 From: Iryna Lagno Date: Thu, 21 Apr 2016 09:07:42 +0300 Subject: [PATCH 5/8] MAGETWO-52011: Second error message is not displayed when adding product with two invalid file fields to the cart --- .../Magento/Catalog/Model/Product/Type/AbstractType.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/Catalog/Model/Product/Type/AbstractType.php b/app/code/Magento/Catalog/Model/Product/Type/AbstractType.php index 03b2418a12c7a..8e16f5891a752 100644 --- a/app/code/Magento/Catalog/Model/Product/Type/AbstractType.php +++ b/app/code/Magento/Catalog/Model/Product/Type/AbstractType.php @@ -572,7 +572,7 @@ protected function _prepareOptions(\Magento\Framework\DataObject $buyRequest, $p $options = $product->getOptions(); } if ($options !== null) { - $result = []; + $results = []; foreach ($options as $option) { /* @var $option \Magento\Catalog\Model\Product\Option */ try { @@ -583,7 +583,7 @@ protected function _prepareOptions(\Magento\Framework\DataObject $buyRequest, $p ->setProcessMode($processMode) ->validateUserValue($buyRequest->getOptions()); } catch (LocalizedException $e) { - $result[] = $e->getMessage(); + $results[] = $e->getMessage(); continue; } @@ -592,8 +592,8 @@ protected function _prepareOptions(\Magento\Framework\DataObject $buyRequest, $p $transport->options[$option->getId()] = $preparedValue; } } - if (count($result) > 0) { - throw new LocalizedException(__(implode("\n", $result))); + if (count($results) > 0) { + throw new LocalizedException(__(implode("\n", $results))); } } From 8cfcdc0a941b07b56d0e358629ddc7b0ba0b8856 Mon Sep 17 00:00:00 2001 From: Iryna Lagno Date: Mon, 25 Apr 2016 11:04:02 +0300 Subject: [PATCH 6/8] MAGETWO-51943: Required varchar and text eav attributes can be saved as empty string --- app/code/Magento/Catalog/Model/Product/Type/AbstractType.php | 1 + .../Test/Unit/Model/Entity/Attribute/AbstractAttributeTest.php | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Catalog/Model/Product/Type/AbstractType.php b/app/code/Magento/Catalog/Model/Product/Type/AbstractType.php index 8e16f5891a752..6de6ea6c6c6bc 100644 --- a/app/code/Magento/Catalog/Model/Product/Type/AbstractType.php +++ b/app/code/Magento/Catalog/Model/Product/Type/AbstractType.php @@ -8,6 +8,7 @@ use Magento\Catalog\Api\ProductRepositoryInterface; use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\Exception\LocalizedException; + /** * @api * Abstract model for product type implementation diff --git a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/AbstractAttributeTest.php b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/AbstractAttributeTest.php index 1d0364fb7638f..f083839929170 100644 --- a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/AbstractAttributeTest.php +++ b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/AbstractAttributeTest.php @@ -33,7 +33,6 @@ public function testGetOptionWhenOptionsAreSet() ->with(['options']) ->willReturn('expected value'); - $this->assertEquals('expected value', $model->getOptions()); } From 0c21e19e9f216e009f6e2f3fd08a2d8be1f430f6 Mon Sep 17 00:00:00 2001 From: Iryna Lagno Date: Mon, 25 Apr 2016 12:55:09 +0300 Subject: [PATCH 7/8] MAGETWO-51943: Required varchar and text eav attributes can be saved as empty string --- .../Magento/Catalog/Controller/Adminhtml/CategoryTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/CategoryTest.php b/dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/CategoryTest.php index 534a39ad58c9f..f660eda22d845 100644 --- a/dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/CategoryTest.php +++ b/dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/CategoryTest.php @@ -233,7 +233,7 @@ public function saveActionDataProvider() 'display_mode' => true, 'meta_title' => true, 'custom_design' => true, - 'page_layout' => true, + 'page_layout' => false, 'is_active' => true, 'include_in_menu' => true, 'landing_page' => true, @@ -242,7 +242,7 @@ public function saveActionDataProvider() 'description' => true, 'meta_keywords' => true, 'meta_description' => true, - 'custom_layout_update' => true, + 'custom_layout_update' => false, 'custom_design_from' => true, 'custom_design_to' => true, 'filter_price_range' => false From 79b37e7180cccc71a40e0a29990ac77d4e6d9e6a Mon Sep 17 00:00:00 2001 From: Iryna Lagno Date: Mon, 25 Apr 2016 12:55:36 +0300 Subject: [PATCH 8/8] MAGETWO-52011: Second error message is not displayed when adding product with two invalid file fields to the cart --- .../Magento/Catalog/Model/Product/Type/AbstractTypeTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dev/tests/integration/testsuite/Magento/Catalog/Model/Product/Type/AbstractTypeTest.php b/dev/tests/integration/testsuite/Magento/Catalog/Model/Product/Type/AbstractTypeTest.php index fd074a3eb17ae..7dd3177f30bfc 100644 --- a/dev/tests/integration/testsuite/Magento/Catalog/Model/Product/Type/AbstractTypeTest.php +++ b/dev/tests/integration/testsuite/Magento/Catalog/Model/Product/Type/AbstractTypeTest.php @@ -190,7 +190,8 @@ public function testPrepareForCartOptionsException() ); $product = $repository->get('simple'); // fixture - $this->assertEquals( + + $this->assertContains( 'Please specify product\'s required option(s).', $this->_model->prepareForCart(new \Magento\Framework\DataObject(), $product) );