Skip to content

Commit

Permalink
Merge pull request #563 from magento-folks/bugs
Browse files Browse the repository at this point in the history
[Folks] Bugfixes
  • Loading branch information
slavvka committed Apr 25, 2016
2 parents e60afa2 + 79b37e7 commit 8bc4691
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 21 deletions.
23 changes: 17 additions & 6 deletions app/code/Magento/Catalog/Model/Product/Type/AbstractType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\LocalizedException;

/**
* @api
Expand Down Expand Up @@ -561,6 +562,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)
{
Expand All @@ -571,20 +573,29 @@ protected function _prepareOptions(\Magento\Framework\DataObject $buyRequest, $p
$options = $product->getOptions();
}
if ($options !== null) {
$results = [];
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 (LocalizedException $e) {
$results[] = $e->getMessage();
continue;
}

$preparedValue = $group->prepareForCart();
if ($preparedValue !== null) {
$transport->options[$option->getId()] = $preparedValue;
}
}
if (count($results) > 0) {
throw new LocalizedException(__(implode("\n", $results)));
}
}

$eventName = sprintf('catalog_product_type_prepare_%s_options', $processMode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public function testGetOptionWhenOptionsAreSet()
->with(['options'])
->willReturn('expected value');


$this->assertEquals('expected value', $model->getOptions());
}

Expand Down Expand Up @@ -175,4 +174,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'],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
Expand Down

0 comments on commit 8bc4691

Please sign in to comment.