Skip to content

Commit

Permalink
Merge pull request #539 from magento-goinc/pr-539
Browse files Browse the repository at this point in the history
[SWAT] Bug fixes
  • Loading branch information
Bomko, Alex(abomko) committed Apr 16, 2016
2 parents e04ef1b + 59cda3c commit 6a377bb
Show file tree
Hide file tree
Showing 14 changed files with 120 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ private function getCategoryLinkManagement()

/**
* @return StoreManagerInterface
* @deprecated
*/
private function getStoreManager()
{
Expand All @@ -265,6 +266,7 @@ private function getStoreManager()
* Retrieve data persistor
*
* @return DataPersistorInterface|mixed
* @deprecated
*/
protected function getDataPersistor()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Magento\Backend\App\Action;
use Magento\Catalog\Controller\Adminhtml\Product;
use Magento\Framework\App\ObjectManager;
use Magento\Store\Model\StoreManagerInterface;

/**
* Product validate
Expand Down Expand Up @@ -47,6 +48,11 @@ class Validate extends \Magento\Catalog\Controller\Adminhtml\Product
*/
protected $initializationHelper;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* @param Action\Context $context
* @param Builder $productBuilder
Expand Down Expand Up @@ -91,10 +97,12 @@ public function execute()
if ($productData && !isset($productData['stock_data']['use_config_manage_stock'])) {
$productData['stock_data']['use_config_manage_stock'] = 0;
}
$storeId = $this->getRequest()->getParam('store', 0);
$store = $this->getStoreManager()->getStore($storeId);
$this->getStoreManager()->setCurrentStore($store->getCode());
/* @var $product \Magento\Catalog\Model\Product */
$product = $this->productFactory->create();
$product->setData('_edit_mode', true);
$storeId = $this->getRequest()->getParam('store');
if ($storeId) {
$product->setStoreId($storeId);
}
Expand Down Expand Up @@ -137,6 +145,19 @@ public function execute()
return $this->resultJsonFactory->create()->setData($response);
}

/**
* @return StoreManagerInterface
* @deprecated
*/
private function getStoreManager()
{
if (null === $this->storeManager) {
$this->storeManager = \Magento\Framework\App\ObjectManager::getInstance()
->get('Magento\Store\Model\StoreManagerInterface');
}
return $this->storeManager;
}

/**
* @return Initialization\Helper
* @deprecated
Expand Down
13 changes: 5 additions & 8 deletions app/code/Magento/Catalog/Model/ProductRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,14 +522,11 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO
$product->setCanSaveCustomOptions(true);
}

$useValidation = \Magento\Store\Model\Store::ADMIN_CODE === $this->storeManager->getStore()->getCode();
if ($useValidation) {
$validationResult = $this->resourceModel->validate($product);
if (true !== $validationResult) {
throw new \Magento\Framework\Exception\CouldNotSaveException(
__('Invalid product data: %1', implode(',', $validationResult))
);
}
$validationResult = $this->resourceModel->validate($product);
if (true !== $validationResult) {
throw new \Magento\Framework\Exception\CouldNotSaveException(
__('Invalid product data: %1', implode(',', $validationResult))
);
}

try {
Expand Down
4 changes: 2 additions & 2 deletions app/code/Magento/Catalog/Model/ResourceModel/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ public function duplicate($oldId, $newId)
$this->getLinkField() . ' = ?',
$oldId
)->where(
'store_id > ?',
'store_id >= ?',
0
);

Expand All @@ -541,7 +541,7 @@ public function duplicate($oldId, $newId)
$statusAttribute = $this->getAttribute('status');
$statusAttributeId = $statusAttribute->getAttributeId();
$statusAttributeTable = $statusAttribute->getBackend()->getTable();
$updateCond[] = 'store_id > 0';
$updateCond[] = 'store_id >= 0';
$updateCond[] = $connection->quoteInto($this->getLinkField() . ' = ?', $newId);
$updateCond[] = $connection->quoteInto('attribute_id = ?', $statusAttributeId);
$connection->update(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,48 @@
*/
namespace Magento\Catalog\Plugin\Model\Attribute\Backend;

use Magento\Store\Model\Store;

class AttributeValidation
{
/** @var \Magento\Store\Model\StoreManagerInterface */
private $storeManager;

/**
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param array $allowedEntityTypes
*/
public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager,
$allowedEntityTypes = []
) {
$this->storeManager = $storeManager;
$this->allowedEntityTypes = $allowedEntityTypes;
}

/**
* @param \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend $subject
* @param \Closure $proceed
* @param \Magento\Framework\DataObject $attribute
* @param \Magento\Framework\DataObject $entity
* @return bool
*/
public function aroundValidate(
\Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend $subject,
\Closure $proceed,
\Magento\Framework\DataObject $attribute
\Magento\Framework\DataObject $entity
) {
$useDefault = $attribute->getUseDefault();
$attrCode = $subject->getAttribute()->getAttributeCode();
if ($useDefault && isset($useDefault[$attrCode])) {
return true;
$isAllowedType = !empty(array_filter(array_map(function ($allowedEntity) use ($entity) {
return $entity instanceof $allowedEntity;
}, $this->allowedEntityTypes)));

if ($isAllowedType && $this->storeManager->getStore()->getId() !== Store::DEFAULT_STORE_ID) {
$attrCode = $subject->getAttribute()->getAttributeCode();
// Null is meaning "no value" which should be overridden by value from default scope
if (array_key_exists($attrCode, $entity->getData()) && $entity->getData($attrCode) === null) {
return true;
}
}
return $proceed($attribute);

return $proceed($entity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ class ValidateTest extends \Magento\Catalog\Test\Unit\Controller\Adminhtml\Produ
/** @var \Magento\Framework\Controller\Result\JsonFactory|\PHPUnit_Framework_MockObject_MockObject */
protected $resultJsonFactory;

/**
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @return void
*/
protected function setUp()
{
$this->productBuilder = $this->getMock(
Expand Down Expand Up @@ -121,6 +125,20 @@ protected function setUp()
->getMock();
$this->resultJsonFactory->expects($this->any())->method('create')->willReturn($this->resultJson);

$storeManagerInterfaceMock = $this->getMockForAbstractClass(
'Magento\Store\Model\StoreManagerInterface',
[],
'',
false,
true,
true,
['getStore', 'getCode']
);

$storeManagerInterfaceMock->expects($this->any())
->method('getStore')
->will($this->returnSelf());

$additionalParams = ['resultRedirectFactory' => $this->resultRedirectFactory];
$this->action = (new ObjectManagerHelper($this))->getObject(
'Magento\Catalog\Controller\Adminhtml\Product\Validate',
Expand All @@ -132,6 +150,7 @@ protected function setUp()
'initializationHelper' => $this->initializationHelper,
'resultJsonFactory' => $this->resultJsonFactory,
'productFactory' => $this->productFactory,
'storeManager' => $storeManagerInterfaceMock,
]
);
}
Expand Down
8 changes: 8 additions & 0 deletions app/code/Magento/Catalog/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,14 @@
<type name="Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend">
<plugin name="attributeValidation" type="Magento\Catalog\Plugin\Model\Attribute\Backend\AttributeValidation"/>
</type>
<type name="Magento\Catalog\Plugin\Model\Attribute\Backend\AttributeValidation">
<arguments>
<argument name="allowedEntityTypes" xsi:type="array">
<item name="product" xsi:type="string">Magento\Catalog\Api\Data\ProductInterface</item>
<item name="category" xsi:type="string">Magento\Catalog\Api\Data\CategoryInterface</item>
</argument>
</arguments>
</type>
<type name="Magento\Catalog\Model\CategoryRepository">
<arguments>
<argument name="categoryResource" xsi:type="object">Magento\Catalog\Model\ResourceModel\Category\Proxy</argument>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,8 @@ protected function getButtonSet()
'title' => __('Add Products Manually'),
'sortOrder' => 10,
'imports' => [
'visible' => '!ns = ${ $.ns }, index = '
. ConfigurablePanel::CONFIGURABLE_MATRIX . ':isEmpty',
'disabled' => 'ns = ${ $.ns }, index = '
. ConfigurablePanel::CONFIGURABLE_MATRIX . ':isEmpty',
'visible' => 'ns = ${ $.ns }, index = '
. ConfigurablePanel::CONFIGURABLE_MATRIX . ':isShowAddProductButton',
],
],
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ $currencySymbol = $block->getCurrencySymbol();
}
</script>
<script>
require(['jquery'], function ($) {
require(['jquery', 'mage/apply/main'], function ($, main) {
main.apply();
$('.<?= /* @noEscape */ $block->getData('config/dataScope') ?>[data-role=step-wizard-dialog]').applyBindings();
$('.<?= /* @noEscape */ $block->getData('config/dataScope') ?>[data-role=product-variations-matrix]').applyBindings();
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ define([
insertDataFromWizard: [],
map: null,
isEmpty: true,
isShowAddProductButton: false,
cacheGridData: [],
unionInsertData: [],
deleteProperty: false,
dataLength: 0,
identificationProperty: 'id',
'attribute_set_id': '',
attributesTmp: [],
listens: {
'insertDataFromGrid': 'processingInsertDataFromGrid',
'insertDataFromWizard': 'processingInsertDataFromWizard',
Expand Down Expand Up @@ -118,7 +120,9 @@ define([
tmpArray.splice(index, 1);

if (!tmpArray.length) {
this.attributesTmp = this.source.get('data.attributes');
this.source.set('data.attributes', []);
this.cacheGridData = [];
}

if (parseInt(this.currentPage(), 10) === this.pages()) {
Expand Down Expand Up @@ -166,7 +170,7 @@ define([
initObservable: function () {
this._super()
.observe([
'insertDataFromGrid', 'unionInsertData', 'isEmpty', 'actionsListOpened'
'insertDataFromGrid', 'unionInsertData', 'isEmpty', 'isShowAddProductButton', 'actionsListOpened'
]);

return this;
Expand Down Expand Up @@ -197,9 +201,13 @@ define([
var dataCount,
elemsCount,
tmpData,
path;
path,
attributeCodes = this.source.get('data.attribute_codes');

this.isEmpty(data.length === 0);
this.isShowAddProductButton(
(!attributeCodes || data.length > 0 ? data.length : attributeCodes.length) > 0
);

tmpData = data.slice(this.pageSize * (this.currentPage() - 1),
this.pageSize * (this.currentPage() - 1) + this.pageSize);
Expand Down Expand Up @@ -260,6 +268,10 @@ define([
tmpArray.push(mappedData);
}, this);

// Attributes cannot be changed before regeneration thought wizard
if (!this.source.get('data.attributes').length) {
this.source.set('data.attributes', this.attributesTmp);
}
this.unionInsertData(tmpArray);
},

Expand Down
4 changes: 3 additions & 1 deletion app/code/Magento/Eav/Model/ResourceModel/UpdateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ protected function getAttributes($entityType)
* @throws \Magento\Framework\Exception\ConfigurationMismatchException
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function execute($entityType, $entityData, $arguments = [])
{
Expand All @@ -121,7 +122,8 @@ public function execute($entityType, $entityData, $arguments = [])
/**
* Only scalar values can be stored in generic tables
*/
if (isset($data[$attribute->getAttributeCode()]) && !is_scalar($data[$attribute->getAttributeCode()])) {
if (isset($entityData[$attribute->getAttributeCode()])
&& !is_scalar($entityData[$attribute->getAttributeCode()])) {
continue;
}
if (isset($snapshot[$attribute->getAttributeCode()])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ define([
function applyComponents(el, ctx) {
ko.utils.arrayForEach(el.childNodes, ko.cleanNode);
ko.applyBindingsToDescendants(ctx, el);
mage.apply();
mage.apply(el);
}

ko.bindingHandlers.bindHtml = {
Expand Down
8 changes: 5 additions & 3 deletions lib/web/mage/apply/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@ define([
/**
* Initializes components assigned to HTML elements via [data-mage-init].
*
* @param {HTMLElement} context - Element of context to search
* @example Sample 'data-mage-init' declaration.
* data-mage-init='{"path/to/component": {"foo": "bar"}}'
*/
apply: function () {
var virtuals = processScripts(),
nodes = document.querySelectorAll(nodeSelector);
apply: function (context) {
var el = _.isElement(context) ? context : document,
virtuals = processScripts(el),
nodes = el.querySelectorAll(nodeSelector);

_.toArray(nodes)
.map(getData)
Expand Down
6 changes: 4 additions & 2 deletions lib/web/mage/apply/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ define([
* to a 'data-mage-init' attribute of an elemennt found by provided selector.
* Note: All found script nodes will be removed from DOM.
*
* @param {HTMLElement} context - Element of context to search
* @returns {Array} An array of components not assigned to the specific element.
*
* @example Sample declaration.
Expand All @@ -109,8 +110,9 @@ define([
* }
* }
*/
return function () {
var nodes = document.querySelectorAll(scriptSelector);
return function (context) {
var el = _.isElement(context) ? context : document,
nodes = el.querySelectorAll(scriptSelector);

_.toArray(nodes)
.map(getNodeData)
Expand Down

0 comments on commit 6a377bb

Please sign in to comment.