Skip to content

Commit c2e3508

Browse files
committed
Merge remote-tracking branch 'goinc/MAGETWO-49618' into pr-539
2 parents ae40e98 + c2a3ef6 commit c2e3508

File tree

8 files changed

+92
-19
lines changed

8 files changed

+92
-19
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Product/Save.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ private function getCategoryLinkManagement()
251251

252252
/**
253253
* @return StoreManagerInterface
254+
* @deprecated
254255
*/
255256
private function getStoreManager()
256257
{
@@ -265,6 +266,7 @@ private function getStoreManager()
265266
* Retrieve data persistor
266267
*
267268
* @return DataPersistorInterface|mixed
269+
* @deprecated
268270
*/
269271
protected function getDataPersistor()
270272
{

app/code/Magento/Catalog/Controller/Adminhtml/Product/Validate.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Backend\App\Action;
1010
use Magento\Catalog\Controller\Adminhtml\Product;
1111
use Magento\Framework\App\ObjectManager;
12+
use Magento\Store\Model\StoreManagerInterface;
1213

1314
/**
1415
* Product validate
@@ -47,6 +48,11 @@ class Validate extends \Magento\Catalog\Controller\Adminhtml\Product
4748
*/
4849
protected $initializationHelper;
4950

51+
/**
52+
* @var StoreManagerInterface
53+
*/
54+
private $storeManager;
55+
5056
/**
5157
* @param Action\Context $context
5258
* @param Builder $productBuilder
@@ -91,10 +97,12 @@ public function execute()
9197
if ($productData && !isset($productData['stock_data']['use_config_manage_stock'])) {
9298
$productData['stock_data']['use_config_manage_stock'] = 0;
9399
}
100+
$storeId = $this->getRequest()->getParam('store', 0);
101+
$store = $this->getStoreManager()->getStore($storeId);
102+
$this->getStoreManager()->setCurrentStore($store->getCode());
94103
/* @var $product \Magento\Catalog\Model\Product */
95104
$product = $this->productFactory->create();
96105
$product->setData('_edit_mode', true);
97-
$storeId = $this->getRequest()->getParam('store');
98106
if ($storeId) {
99107
$product->setStoreId($storeId);
100108
}
@@ -137,6 +145,19 @@ public function execute()
137145
return $this->resultJsonFactory->create()->setData($response);
138146
}
139147

148+
/**
149+
* @return StoreManagerInterface
150+
* @deprecated
151+
*/
152+
private function getStoreManager()
153+
{
154+
if (null === $this->storeManager) {
155+
$this->storeManager = \Magento\Framework\App\ObjectManager::getInstance()
156+
->get('Magento\Store\Model\StoreManagerInterface');
157+
}
158+
return $this->storeManager;
159+
}
160+
140161
/**
141162
* @return Initialization\Helper
142163
* @deprecated

app/code/Magento/Catalog/Model/ProductRepository.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -522,14 +522,11 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO
522522
$product->setCanSaveCustomOptions(true);
523523
}
524524

525-
$useValidation = \Magento\Store\Model\Store::ADMIN_CODE === $this->storeManager->getStore()->getCode();
526-
if ($useValidation) {
527-
$validationResult = $this->resourceModel->validate($product);
528-
if (true !== $validationResult) {
529-
throw new \Magento\Framework\Exception\CouldNotSaveException(
530-
__('Invalid product data: %1', implode(',', $validationResult))
531-
);
532-
}
525+
$validationResult = $this->resourceModel->validate($product);
526+
if (true !== $validationResult) {
527+
throw new \Magento\Framework\Exception\CouldNotSaveException(
528+
__('Invalid product data: %1', implode(',', $validationResult))
529+
);
533530
}
534531

535532
try {

app/code/Magento/Catalog/Model/ResourceModel/Product.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ public function duplicate($oldId, $newId)
523523
$this->getLinkField() . ' = ?',
524524
$oldId
525525
)->where(
526-
'store_id > ?',
526+
'store_id >= ?',
527527
0
528528
);
529529

@@ -541,7 +541,7 @@ public function duplicate($oldId, $newId)
541541
$statusAttribute = $this->getAttribute('status');
542542
$statusAttributeId = $statusAttribute->getAttributeId();
543543
$statusAttributeTable = $statusAttribute->getBackend()->getTable();
544-
$updateCond[] = 'store_id > 0';
544+
$updateCond[] = 'store_id >= 0';
545545
$updateCond[] = $connection->quoteInto($this->getLinkField() . ' = ?', $newId);
546546
$updateCond[] = $connection->quoteInto('attribute_id = ?', $statusAttributeId);
547547
$connection->update(

app/code/Magento/Catalog/Plugin/Model/Attribute/Backend/AttributeValidation.php

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,48 @@
55
*/
66
namespace Magento\Catalog\Plugin\Model\Attribute\Backend;
77

8+
use Magento\Store\Model\Store;
9+
810
class AttributeValidation
911
{
12+
/** @var \Magento\Store\Model\StoreManagerInterface */
13+
private $storeManager;
14+
15+
/**
16+
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
17+
* @param array $allowedEntityTypes
18+
*/
19+
public function __construct(
20+
\Magento\Store\Model\StoreManagerInterface $storeManager,
21+
$allowedEntityTypes = []
22+
) {
23+
$this->storeManager = $storeManager;
24+
$this->allowedEntityTypes = $allowedEntityTypes;
25+
}
26+
1027
/**
1128
* @param \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend $subject
1229
* @param \Closure $proceed
13-
* @param \Magento\Framework\DataObject $attribute
30+
* @param \Magento\Framework\DataObject $entity
1431
* @return bool
1532
*/
1633
public function aroundValidate(
1734
\Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend $subject,
1835
\Closure $proceed,
19-
\Magento\Framework\DataObject $attribute
36+
\Magento\Framework\DataObject $entity
2037
) {
21-
$useDefault = $attribute->getUseDefault();
22-
$attrCode = $subject->getAttribute()->getAttributeCode();
23-
if ($useDefault && isset($useDefault[$attrCode])) {
24-
return true;
38+
$isAllowedType = !empty(array_filter(array_map(function ($allowedEntity) use ($entity) {
39+
return $entity instanceof $allowedEntity;
40+
}, $this->allowedEntityTypes)));
41+
42+
if ($isAllowedType && $this->storeManager->getStore()->getId() !== Store::DEFAULT_STORE_ID) {
43+
$attrCode = $subject->getAttribute()->getAttributeCode();
44+
// Null is meaning "no value" which should be overridden by value from default scope
45+
if (array_key_exists($attrCode, $entity->getData()) && $entity->getData($attrCode) === null) {
46+
return true;
47+
}
2548
}
26-
return $proceed($attribute);
49+
50+
return $proceed($entity);
2751
}
2852
}

app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/ValidateTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ class ValidateTest extends \Magento\Catalog\Test\Unit\Controller\Adminhtml\Produ
4343
/** @var \Magento\Framework\Controller\Result\JsonFactory|\PHPUnit_Framework_MockObject_MockObject */
4444
protected $resultJsonFactory;
4545

46+
/**
47+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
48+
* @return void
49+
*/
4650
protected function setUp()
4751
{
4852
$this->productBuilder = $this->getMock(
@@ -121,6 +125,20 @@ protected function setUp()
121125
->getMock();
122126
$this->resultJsonFactory->expects($this->any())->method('create')->willReturn($this->resultJson);
123127

128+
$storeManagerInterfaceMock = $this->getMockForAbstractClass(
129+
'Magento\Store\Model\StoreManagerInterface',
130+
[],
131+
'',
132+
false,
133+
true,
134+
true,
135+
['getStore', 'getCode']
136+
);
137+
138+
$storeManagerInterfaceMock->expects($this->any())
139+
->method('getStore')
140+
->will($this->returnSelf());
141+
124142
$additionalParams = ['resultRedirectFactory' => $this->resultRedirectFactory];
125143
$this->action = (new ObjectManagerHelper($this))->getObject(
126144
'Magento\Catalog\Controller\Adminhtml\Product\Validate',
@@ -132,6 +150,7 @@ protected function setUp()
132150
'initializationHelper' => $this->initializationHelper,
133151
'resultJsonFactory' => $this->resultJsonFactory,
134152
'productFactory' => $this->productFactory,
153+
'storeManager' => $storeManagerInterfaceMock,
135154
]
136155
);
137156
}

app/code/Magento/Catalog/etc/di.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,14 @@
473473
<type name="Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend">
474474
<plugin name="attributeValidation" type="Magento\Catalog\Plugin\Model\Attribute\Backend\AttributeValidation"/>
475475
</type>
476+
<type name="Magento\Catalog\Plugin\Model\Attribute\Backend\AttributeValidation">
477+
<arguments>
478+
<argument name="allowedEntityTypes" xsi:type="array">
479+
<item name="product" xsi:type="string">Magento\Catalog\Api\Data\ProductInterface</item>
480+
<item name="category" xsi:type="string">Magento\Catalog\Api\Data\CategoryInterface</item>
481+
</argument>
482+
</arguments>
483+
</type>
476484
<type name="Magento\Catalog\Model\CategoryRepository">
477485
<arguments>
478486
<argument name="categoryResource" xsi:type="object">Magento\Catalog\Model\ResourceModel\Category\Proxy</argument>

app/code/Magento/Eav/Model/ResourceModel/UpdateHandler.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ protected function getAttributes($entityType)
9999
* @throws \Magento\Framework\Exception\ConfigurationMismatchException
100100
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
101101
* @SuppressWarnings(PHPMD.NPathComplexity)
102+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
102103
*/
103104
public function execute($entityType, $entityData, $arguments = [])
104105
{
@@ -121,7 +122,8 @@ public function execute($entityType, $entityData, $arguments = [])
121122
/**
122123
* Only scalar values can be stored in generic tables
123124
*/
124-
if (isset($data[$attribute->getAttributeCode()]) && !is_scalar($data[$attribute->getAttributeCode()])) {
125+
if (isset($entityData[$attribute->getAttributeCode()])
126+
&& !is_scalar($entityData[$attribute->getAttributeCode()])) {
125127
continue;
126128
}
127129
if (isset($snapshot[$attribute->getAttributeCode()])

0 commit comments

Comments
 (0)