Skip to content

Commit

Permalink
MAGETWO-39663: Product save through API service is not visible on fro…
Browse files Browse the repository at this point in the history
…ntend(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
  • Loading branch information
arkadiych committed Nov 4, 2015
1 parent 9c1711b commit 149a936
Show file tree
Hide file tree
Showing 43 changed files with 425 additions and 138 deletions.
12 changes: 6 additions & 6 deletions app/code/Magento/Catalog/Helper/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public function __construct(
* Retrieve data for price indexer update
*
* @param \Magento\Catalog\Model\Product|array $data
* @return boolean
* @return bool
*/
public function isDataForPriceIndexerWasChanged($data)
{
Expand Down Expand Up @@ -161,7 +161,7 @@ public function isDataForPriceIndexerWasChanged($data)
* Retrieve data for product category indexer update
*
* @param \Magento\Catalog\Model\Product $data
* @return boolean
* @return bool
*/
public function isDataForProductCategoryIndexerWasChanged(\Magento\Catalog\Model\Product $data)
{
Expand All @@ -177,7 +177,7 @@ public function isDataForProductCategoryIndexerWasChanged(\Magento\Catalog\Model
* Retrieve product view page url
*
* @param int|ModelProduct $product
* @return string|false
* @return string|bool
*/
public function getProductUrl($product)
{
Expand Down Expand Up @@ -290,7 +290,7 @@ public function getStatuses()
*
* @param ModelProduct|int $product
* @param string $where
* @return boolean
* @return bool
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function canShow($product, $where = 'catalog')
Expand Down Expand Up @@ -392,7 +392,7 @@ public function getAttributeSourceModelByInputType($inputType)
* @param \Magento\Framework\App\Action\Action $controller
* @param \Magento\Framework\DataObject $params
*
* @return false|ModelProduct
* @return bool|ModelProduct
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
Expand Down Expand Up @@ -547,7 +547,7 @@ public function setSkipSaleableCheck($skipSaleableCheck = false)
/**
* Get flag that shows if Magento has to check product to be saleable (enabled and/or inStock)
*
* @return boolean
* @return bool
* @SuppressWarnings(PHPMD.BooleanGetMethodName)
*/
public function getSkipSaleableCheck()
Expand Down
17 changes: 16 additions & 1 deletion app/code/Magento/Catalog/Model/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -968,11 +968,26 @@ public function priceReindexCallback()
*/
public function eavReindexCallback()
{
if ($this->isObjectNew() || $this->hasDataChanges()) {
if ($this->isObjectNew() || $this->isDataChanged($this)) {
$this->_productEavIndexerProcessor->reindexRow($this->getEntityId());
}
}

/**
* Check if data was changed
*
* @return bool
*/
public function isDataChanged()
{
foreach (array_keys($this->getData()) as $field) {
if ($this->dataHasChangedFor($field)) {
return true;
}
}
return false;
}

/**
* Init indexing process after product save
*
Expand Down
15 changes: 12 additions & 3 deletions app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class ProductRepositoryTest extends \PHPUnit_Framework_TestCase
/**
* @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $storeManager;
protected $storeManagerMock;

/**
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
Expand All @@ -155,6 +155,8 @@ protected function setUp()
'getMediaAttributes',
'getProductLinks',
'setProductLinks',
'validate',
'save'
],
[],
'',
Expand Down Expand Up @@ -222,9 +224,16 @@ protected function setUp()
['getLinkTypes'], [], '', false);
$this->imageProcessorMock = $this->getMock('Magento\Framework\Api\ImageProcessorInterface', [], [], '', false);

$this->storeManager = $this->getMockBuilder('Magento\Store\Model\StoreManagerInterface')
$this->storeManagerMock = $this->getMockBuilder('Magento\Store\Model\StoreManagerInterface')
->disableOriginalConstructor()
->setMethods([])
->getMockForAbstractClass();
$storeMock = $this->getMockBuilder('Magento\Store\Api\Data\StoreInterface')
->disableOriginalConstructor()
->setMethods([])
->getMockForAbstractClass();
$storeMock->expects($this->any())->method('getWebsiteId')->willReturn('1');
$this->storeManagerMock->expects($this->any())->method('getStore')->willReturn($storeMock);

$this->model = $this->objectManager->getObject(
'Magento\Catalog\Model\ProductRepository',
Expand All @@ -245,7 +254,7 @@ protected function setUp()
'mimeTypeExtensionMap' => $this->mimeTypeExtensionMapMock,
'linkTypeProvider' => $this->linkTypeProviderMock,
'imageProcessor' => $this->imageProcessorMock,
'storeManager' => $this->storeManager,
'storeManager' => $this->storeManagerMock,
]
);
}
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Catalog/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@
<virtualType name="Magento\Catalog\Model\ResourceModel\Attribute\Collection" type="Magento\Eav\Model\ResourceModel\Entity\Attribute\Collection">
</virtualType>
<type name="Magento\Catalog\Api\ProductRepositoryInterface">
<plugin name="transactionWrapper" type="\Magento\Catalog\Model\Plugin\ProductRepository\TransactionWrapper" sortOrder="-1"/>
<plugin name="transactionWrapper" type="Magento\Catalog\Model\Plugin\ProductRepository\TransactionWrapper" sortOrder="-1"/>
</type>
<type name="Magento\Catalog\Model\CategoryRepository">
<arguments>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/
namespace Magento\CatalogRule\Plugin\Indexer\Product\Save;

use Magento\Catalog\Model\Product;
use Magento\CatalogRule\Model\Indexer\Product\ProductRuleProcessor;

class ApplyRules
Expand All @@ -24,21 +23,24 @@ public function __construct(ProductRuleProcessor $productRuleProcessor)
}

/**
* Apply catalog rules after product save
* Apply catalog rules after product resource model save
*
* @param Product $subject
* @param Product $result
* @return Product
* @param \Magento\Catalog\Model\ResourceModel\Product $subject
* @param callable $proceed
* @param \Magento\Framework\Model\AbstractModel $product
* @return \Magento\Catalog\Model\ResourceModel\Product
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function afterSave(
Product $subject,
Product $result
public function aroundSave(
\Magento\Catalog\Model\ResourceModel\Product $subject,
callable $proceed,
\Magento\Framework\Model\AbstractModel $product
) {
if (!$result->getIsMassupdate()) {
$this->productRuleProcessor->reindexRow($result->getId());
$productResource = $proceed($product);
if (!$product->getIsMassupdate()) {
$this->productRuleProcessor->reindexRow($product->getId());
}
return $result;
return $productResource;
}
}
2 changes: 1 addition & 1 deletion app/code/Magento/CatalogRule/etc/adminhtml/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</argument>
</arguments>
</type>
<type name="Magento\Catalog\Model\Product">
<type name="Magento\Catalog\Model\ResourceModel\Product">
<plugin name="apply_catalog_rules_after_product_save" type="Magento\CatalogRule\Plugin\Indexer\Product\Save\ApplyRules"/>
</type>
<type name="Magento\Catalog\Model\Category">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function getUrlPath($category)
*/
protected function isNeedToGenerateUrlPathForParent($category)
{
return $category->isObjectNew() || $category->getLevel() >= self::MINIMAL_CATEGORY_LEVEL_FOR_PROCESSING;
return $category->getLevel() >= self::MINIMAL_CATEGORY_LEVEL_FOR_PROCESSING;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public function testGetUrlPathWithParent(
$parentCategory->expects($this->any())->method('dataHasChangedFor')
->will($this->returnValueMap([['url_key', false], ['path_ids', false]]));

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

$this->assertEquals($result, $this->categoryUrlPathGenerator->getUrlPath($this->category));
Expand Down
33 changes: 0 additions & 33 deletions app/code/Magento/CatalogUrlRewrite/etc/adminhtml/events.xml

This file was deleted.

15 changes: 15 additions & 0 deletions app/code/Magento/CatalogUrlRewrite/etc/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,19 @@
<event name="catalog_product_import_bunch_delete_after">
<observer name="catalog_product_import_rewrites_delete" instance="Magento\CatalogUrlRewrite\Observer\ClearProductUrlsObserver"/>
</event>
<event name="catalog_product_delete_before">
<observer name="process_url_rewrite_removing" instance="Magento\CatalogUrlRewrite\Observer\ProductProcessUrlRewriteRemovingObserver"/>
</event>
<event name="catalog_product_save_before">
<observer name="product_url_key_autogeneration" instance="Magento\CatalogUrlRewrite\Observer\ProductUrlKeyAutogeneratorObserver"/>
</event>
<event name="catalog_product_save_after">
<observer name="process_url_rewrite_saving" instance="Magento\CatalogUrlRewrite\Observer\ProductProcessUrlRewriteSavingObserver"/>
</event>
<event name="catalog_category_save_before">
<observer name="category_url_path_autogeneration" instance="Magento\CatalogUrlRewrite\Observer\CategoryUrlPathAutogeneratorObserver"/>
</event>
<event name="catalog_category_move_after">
<observer name="process_url_rewrite_moving" instance="Magento\CatalogUrlRewrite\Observer\CategoryProcessUrlRewriteMovingObserver"/>
</event>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ class StockItemTest extends WebapiAbstract
*/
const RESOURCE_PUT_PATH = '/V1/products/:productSku/stockItems/:itemId';

/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection */
protected $productCollection;

/** @var \Magento\Framework\ObjectManagerInterface */
protected $objectManager;

Expand All @@ -48,25 +45,6 @@ class StockItemTest extends WebapiAbstract
public function setUp()
{
$this->objectManager = Bootstrap::getObjectManager();
$this->productCollection = $this->objectManager->get('Magento\Catalog\Model\ResourceModel\Product\Collection');
}

/**
* Execute per test cleanup
*/
public function tearDown()
{
/** @var \Magento\Framework\Registry $registry */
$registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\Framework\Registry');

$registry->unregister('isSecureArea');
$registry->register('isSecureArea', true);

$this->productCollection->addFieldToFilter('entity_id', ['in' => [10, 11, 12]])->delete();
unset($this->productCollection);

$registry->unregister('isSecureArea');
$registry->register('isSecureArea', false);
}

/**
Expand Down Expand Up @@ -121,10 +99,12 @@ public function testStockItemPUTWithWrongInput($newData, $expectedResult, $fixtu
],
];

$stockItemDetailsDo = $this->objectManager->get(
'Magento\CatalogInventory\Api\Data\StockItemInterfaceFactory'
)->create();
$dataObjectHelper = $this->objectManager->get('\Magento\Framework\Api\DataObjectHelper');
/** @var \Magento\CatalogInventory\Api\Data\StockItemInterfaceFactory $stockItemDetailsDo */
$stockItemDetailsDo = $this->objectManager
->get('Magento\CatalogInventory\Api\Data\StockItemInterfaceFactory')
->create();
/** @var \Magento\Framework\Api\DataObjectHelper $dataObjectHelper */
$dataObjectHelper = $this->objectManager->get('Magento\Framework\Api\DataObjectHelper');
$dataObjectHelper->populateWithArray(
$stockItemDetailsDo,
$newData,
Expand All @@ -135,8 +115,10 @@ public function testStockItemPUTWithWrongInput($newData, $expectedResult, $fixtu
$arguments = ['productSku' => $productSku, 'stockItem' => $data];
$this->assertEquals($stockItemOld['item_id'], $this->_webApiCall($serviceInfo, $arguments));

/** @var \Magento\CatalogInventory\Api\Data\StockItemInterfaceFactory $stockItemFactory */
$stockItemFactory = $this->objectManager->get('Magento\CatalogInventory\Api\Data\StockItemInterfaceFactory');
$stockItem = $stockItemFactory->create();
/** @var \Magento\CatalogInventory\Model\ResourceModel\Stock\Item $stockItemResource */
$stockItemResource = $this->objectManager->get('Magento\CatalogInventory\Model\ResourceModel\Stock\Item');
$stockItemResource->loadByProductId($stockItem, $stockItemOld['product_id'], $stockItemOld['website_id']);
$expectedResult['item_id'] = $stockItem->getItemId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*
* @see \Magento\Catalog\Model\CategoryTreeTest
* @magentoDataFixture Magento/Catalog/_files/categories.php
* @magentoAppIsolation enabled
*/
class CategoryTest extends \PHPUnit_Framework_TestCase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,19 +81,22 @@ public function testReindexAll()

/**
* @magentoAppArea adminhtml
* @magentoDataFixture Magento/CatalogUrlRewrite/_files/categories.php
* @magentoDataFixture Magento/Catalog/_files/indexer_catalog_category.php
* @magentoAppIsolation enabled
* @magentoDbIsolation enabled
*
*/
public function testCategoryMove()
{
$this->testReindexAll();
$categories = $this->getCategories(4);
$products = $this->getProducts(2);

/** @var Category $categoryFourth */
$categoryFourth = end($categories);
foreach ($products as $product) {
/** @var \Magento\Catalog\Model\Product $product */
$product->setCategoryIds([$categoryFourth->getId()]);
$product->save();
}

/** @var Category $categorySecond */
$categorySecond = $categories[1];
Expand All @@ -102,6 +105,11 @@ public function testCategoryMove()

/** @var Category $categoryThird */
$categoryThird = $categories[2];
$categoryThird->setIsAnchor(true);
$categoryThird->save();

$this->clearIndex();
$this->indexer->reindexAll();

/**
* Move category from $categoryThird to $categorySecond
Expand Down Expand Up @@ -149,9 +157,6 @@ public function testCategoryDelete()
}
}

/**
*
*/
public function testCategoryCreate()
{
$this->testReindexAll();
Expand Down Expand Up @@ -226,7 +231,9 @@ protected function getProducts($count)
'Magento\Catalog\Model\Product'
);

$result = $product->getCollection()->getItems();
$result[] = $product->load(1);
$result[] = $product->load(2);
$result[] = $product->load(3);

return array_slice($result, 0, $count);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function testCRUD()
)->setAttributeSetId(
4
)->setName(
'Simple Product'
'Simple Product 1'
)->setSku(
uniqid()
)->setPrice(
Expand Down
Loading

0 comments on commit 149a936

Please sign in to comment.