Skip to content

Commit 149a936

Browse files
committed
MAGETWO-39663: Product save through API service is not visible on frontend(direct url, category page)
- fixed product saving process in productRepository for reindexing and applying url rewrite - moved to global area event listeners from catalogUrlRewrite - moved to global area event listeners from CatalogSearch - fixed api-functional tests to use url rewrite via api - fixed integration tests to use url rewrite - fixed unit tests
1 parent 9c1711b commit 149a936

File tree

43 files changed

+425
-138
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+425
-138
lines changed

app/code/Magento/Catalog/Helper/Product.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public function __construct(
132132
* Retrieve data for price indexer update
133133
*
134134
* @param \Magento\Catalog\Model\Product|array $data
135-
* @return boolean
135+
* @return bool
136136
*/
137137
public function isDataForPriceIndexerWasChanged($data)
138138
{
@@ -161,7 +161,7 @@ public function isDataForPriceIndexerWasChanged($data)
161161
* Retrieve data for product category indexer update
162162
*
163163
* @param \Magento\Catalog\Model\Product $data
164-
* @return boolean
164+
* @return bool
165165
*/
166166
public function isDataForProductCategoryIndexerWasChanged(\Magento\Catalog\Model\Product $data)
167167
{
@@ -177,7 +177,7 @@ public function isDataForProductCategoryIndexerWasChanged(\Magento\Catalog\Model
177177
* Retrieve product view page url
178178
*
179179
* @param int|ModelProduct $product
180-
* @return string|false
180+
* @return string|bool
181181
*/
182182
public function getProductUrl($product)
183183
{
@@ -290,7 +290,7 @@ public function getStatuses()
290290
*
291291
* @param ModelProduct|int $product
292292
* @param string $where
293-
* @return boolean
293+
* @return bool
294294
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
295295
*/
296296
public function canShow($product, $where = 'catalog')
@@ -392,7 +392,7 @@ public function getAttributeSourceModelByInputType($inputType)
392392
* @param \Magento\Framework\App\Action\Action $controller
393393
* @param \Magento\Framework\DataObject $params
394394
*
395-
* @return false|ModelProduct
395+
* @return bool|ModelProduct
396396
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
397397
* @SuppressWarnings(PHPMD.NPathComplexity)
398398
*/
@@ -547,7 +547,7 @@ public function setSkipSaleableCheck($skipSaleableCheck = false)
547547
/**
548548
* Get flag that shows if Magento has to check product to be saleable (enabled and/or inStock)
549549
*
550-
* @return boolean
550+
* @return bool
551551
* @SuppressWarnings(PHPMD.BooleanGetMethodName)
552552
*/
553553
public function getSkipSaleableCheck()

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -968,11 +968,26 @@ public function priceReindexCallback()
968968
*/
969969
public function eavReindexCallback()
970970
{
971-
if ($this->isObjectNew() || $this->hasDataChanges()) {
971+
if ($this->isObjectNew() || $this->isDataChanged($this)) {
972972
$this->_productEavIndexerProcessor->reindexRow($this->getEntityId());
973973
}
974974
}
975975

976+
/**
977+
* Check if data was changed
978+
*
979+
* @return bool
980+
*/
981+
public function isDataChanged()
982+
{
983+
foreach (array_keys($this->getData()) as $field) {
984+
if ($this->dataHasChangedFor($field)) {
985+
return true;
986+
}
987+
}
988+
return false;
989+
}
990+
976991
/**
977992
* Init indexing process after product save
978993
*

app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class ProductRepositoryTest extends \PHPUnit_Framework_TestCase
134134
/**
135135
* @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
136136
*/
137-
protected $storeManager;
137+
protected $storeManagerMock;
138138

139139
/**
140140
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
@@ -155,6 +155,8 @@ protected function setUp()
155155
'getMediaAttributes',
156156
'getProductLinks',
157157
'setProductLinks',
158+
'validate',
159+
'save'
158160
],
159161
[],
160162
'',
@@ -222,9 +224,16 @@ protected function setUp()
222224
['getLinkTypes'], [], '', false);
223225
$this->imageProcessorMock = $this->getMock('Magento\Framework\Api\ImageProcessorInterface', [], [], '', false);
224226

225-
$this->storeManager = $this->getMockBuilder('Magento\Store\Model\StoreManagerInterface')
227+
$this->storeManagerMock = $this->getMockBuilder('Magento\Store\Model\StoreManagerInterface')
226228
->disableOriginalConstructor()
229+
->setMethods([])
227230
->getMockForAbstractClass();
231+
$storeMock = $this->getMockBuilder('Magento\Store\Api\Data\StoreInterface')
232+
->disableOriginalConstructor()
233+
->setMethods([])
234+
->getMockForAbstractClass();
235+
$storeMock->expects($this->any())->method('getWebsiteId')->willReturn('1');
236+
$this->storeManagerMock->expects($this->any())->method('getStore')->willReturn($storeMock);
228237

229238
$this->model = $this->objectManager->getObject(
230239
'Magento\Catalog\Model\ProductRepository',
@@ -245,7 +254,7 @@ protected function setUp()
245254
'mimeTypeExtensionMap' => $this->mimeTypeExtensionMapMock,
246255
'linkTypeProvider' => $this->linkTypeProviderMock,
247256
'imageProcessor' => $this->imageProcessorMock,
248-
'storeManager' => $this->storeManager,
257+
'storeManager' => $this->storeManagerMock,
249258
]
250259
);
251260
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@
475475
<virtualType name="Magento\Catalog\Model\ResourceModel\Attribute\Collection" type="Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection">
476476
</virtualType>
477477
<type name="Magento\Catalog\Api\ProductRepositoryInterface">
478-
<plugin name="transactionWrapper" type="\Magento\Catalog\Model\Plugin\ProductRepository\TransactionWrapper" sortOrder="-1"/>
478+
<plugin name="transactionWrapper" type="Magento\Catalog\Model\Plugin\ProductRepository\TransactionWrapper" sortOrder="-1"/>
479479
</type>
480480
<type name="Magento\Catalog\Model\CategoryRepository">
481481
<arguments>

app/code/Magento/CatalogRule/Plugin/Indexer/Product/Save/ApplyRules.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66
namespace Magento\CatalogRule\Plugin\Indexer\Product\Save;
77

8-
use Magento\Catalog\Model\Product;
98
use Magento\CatalogRule\Model\Indexer\Product\ProductRuleProcessor;
109

1110
class ApplyRules
@@ -24,21 +23,24 @@ public function __construct(ProductRuleProcessor $productRuleProcessor)
2423
}
2524

2625
/**
27-
* Apply catalog rules after product save
26+
* Apply catalog rules after product resource model save
2827
*
29-
* @param Product $subject
30-
* @param Product $result
31-
* @return Product
28+
* @param \Magento\Catalog\Model\ResourceModel\Product $subject
29+
* @param callable $proceed
30+
* @param \Magento\Framework\Model\AbstractModel $product
31+
* @return \Magento\Catalog\Model\ResourceModel\Product
3232
*
3333
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
3434
*/
35-
public function afterSave(
36-
Product $subject,
37-
Product $result
35+
public function aroundSave(
36+
\Magento\Catalog\Model\ResourceModel\Product $subject,
37+
callable $proceed,
38+
\Magento\Framework\Model\AbstractModel $product
3839
) {
39-
if (!$result->getIsMassupdate()) {
40-
$this->productRuleProcessor->reindexRow($result->getId());
40+
$productResource = $proceed($product);
41+
if (!$product->getIsMassupdate()) {
42+
$this->productRuleProcessor->reindexRow($product->getId());
4143
}
42-
return $result;
44+
return $productResource;
4345
}
4446
}

app/code/Magento/CatalogRule/etc/adminhtml/di.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</argument>
1515
</arguments>
1616
</type>
17-
<type name="Magento\Catalog\Model\Product">
17+
<type name="Magento\Catalog\Model\ResourceModel\Product">
1818
<plugin name="apply_catalog_rules_after_product_save" type="Magento\CatalogRule\Plugin\Indexer\Product\Save\ApplyRules"/>
1919
</type>
2020
<type name="Magento\Catalog\Model\Category">

app/code/Magento/CatalogUrlRewrite/Model/CategoryUrlPathGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function getUrlPath($category)
8787
*/
8888
protected function isNeedToGenerateUrlPathForParent($category)
8989
{
90-
return $category->isObjectNew() || $category->getLevel() >= self::MINIMAL_CATEGORY_LEVEL_FOR_PROCESSING;
90+
return $category->getLevel() >= self::MINIMAL_CATEGORY_LEVEL_FOR_PROCESSING;
9191
}
9292

9393
/**

app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/CategoryUrlPathGeneratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public function testGetUrlPathWithParent(
152152
$parentCategory->expects($this->any())->method('dataHasChangedFor')
153153
->will($this->returnValueMap([['url_key', false], ['path_ids', false]]));
154154

155-
$this->categoryRepository->expects($this->once())->method('get')->with('parent_id')
155+
$this->categoryRepository->expects($this->any())->method('get')->with('parent_id')
156156
->will($this->returnValue($parentCategory));
157157

158158
$this->assertEquals($result, $this->categoryUrlPathGenerator->getUrlPath($this->category));

app/code/Magento/CatalogUrlRewrite/etc/adminhtml/events.xml

Lines changed: 0 additions & 33 deletions
This file was deleted.

app/code/Magento/CatalogUrlRewrite/etc/events.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,19 @@
1818
<event name="catalog_product_import_bunch_delete_after">
1919
<observer name="catalog_product_import_rewrites_delete" instance="Magento\CatalogUrlRewrite\Observer\ClearProductUrlsObserver"/>
2020
</event>
21+
<event name="catalog_product_delete_before">
22+
<observer name="process_url_rewrite_removing" instance="Magento\CatalogUrlRewrite\Observer\ProductProcessUrlRewriteRemovingObserver"/>
23+
</event>
24+
<event name="catalog_product_save_before">
25+
<observer name="product_url_key_autogeneration" instance="Magento\CatalogUrlRewrite\Observer\ProductUrlKeyAutogeneratorObserver"/>
26+
</event>
27+
<event name="catalog_product_save_after">
28+
<observer name="process_url_rewrite_saving" instance="Magento\CatalogUrlRewrite\Observer\ProductProcessUrlRewriteSavingObserver"/>
29+
</event>
30+
<event name="catalog_category_save_before">
31+
<observer name="category_url_path_autogeneration" instance="Magento\CatalogUrlRewrite\Observer\CategoryUrlPathAutogeneratorObserver"/>
32+
</event>
33+
<event name="catalog_category_move_after">
34+
<observer name="process_url_rewrite_moving" instance="Magento\CatalogUrlRewrite\Observer\CategoryProcessUrlRewriteMovingObserver"/>
35+
</event>
2136
</config>

0 commit comments

Comments
 (0)