Skip to content

Commit

Permalink
MAGETWO-56978: [GITHUB] Product prices not scoped to Website - scopin…
Browse files Browse the repository at this point in the history
…g seems to be at store view level #5133 #7251
  • Loading branch information
Yaroslav Onischenko committed Jan 18, 2017
2 parents 64f2769 + 91aa307 commit 2a573cc
Show file tree
Hide file tree
Showing 18 changed files with 525 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,55 @@
*/
namespace Magento\CatalogUrlRewrite\Model\Category\Plugin\Category;

use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider;
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
use Magento\UrlRewrite\Model\UrlPersistInterface;
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;

class Remove
{
/** @var UrlPersistInterface */
/** @var \Magento\UrlRewrite\Model\UrlPersistInterface */
protected $urlPersist;

/** @var ProductUrlRewriteGenerator */
/** @var \Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator */
protected $productUrlRewriteGenerator;

/** @var ChildrenCategoriesProvider */
/** @var \Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider */
protected $childrenCategoriesProvider;

/** @var \Magento\Framework\Serialize\Serializer\Json */
private $serializer;

/**
* @param UrlPersistInterface $urlPersist
* @param ProductUrlRewriteGenerator $productUrlRewriteGenerator
* @param ChildrenCategoriesProvider $childrenCategoriesProvider
* @param \Magento\UrlRewrite\Model\UrlPersistInterface $urlPersist
* @param \Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator $productUrlRewriteGenerator
* @param \Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider $childrenCategoriesProvider
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
*/
public function __construct(
UrlPersistInterface $urlPersist,
ProductUrlRewriteGenerator $productUrlRewriteGenerator,
ChildrenCategoriesProvider $childrenCategoriesProvider
\Magento\UrlRewrite\Model\UrlPersistInterface $urlPersist,
\Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator $productUrlRewriteGenerator,
\Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider $childrenCategoriesProvider,
\Magento\Framework\Serialize\Serializer\Json $serializer = null
) {
$this->urlPersist = $urlPersist;
$this->productUrlRewriteGenerator = $productUrlRewriteGenerator;
$this->childrenCategoriesProvider = $childrenCategoriesProvider;
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()->get(
\Magento\Framework\Serialize\Serializer\Json::class
);
}

/**
* Remove product urls from storage
*
* @param \Magento\Catalog\Model\ResourceModel\Category $subject
* @param callable $proceed
* @param CategoryInterface $category
* @param \Closure $proceed
* @param \Magento\Catalog\Api\Data\CategoryInterface $category
* @return mixed
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function aroundDelete(
\Magento\Catalog\Model\ResourceModel\Category $subject,
\Closure $proceed,
CategoryInterface $category
\Magento\Catalog\Api\Data\CategoryInterface $category
) {
$categoryIds = $this->childrenCategoriesProvider->getChildrenIds($category, true);
$categoryIds[] = $category->getId();
Expand All @@ -72,13 +75,13 @@ protected function deleteRewritesForCategory($categoryId)
$this->urlPersist->deleteByData(
[
UrlRewrite::ENTITY_ID => $categoryId,
UrlRewrite::ENTITY_TYPE => CategoryUrlRewriteGenerator::ENTITY_TYPE,
UrlRewrite::ENTITY_TYPE => \Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator::ENTITY_TYPE,
]
);
$this->urlPersist->deleteByData(
[
UrlRewrite::METADATA => serialize(['category_id' => $categoryId]),
UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE,
UrlRewrite::METADATA => $this->serializer->serialize(['category_id' => $categoryId]),
UrlRewrite::ENTITY_TYPE => \Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator::ENTITY_TYPE,
]
);
}
Expand Down
81 changes: 43 additions & 38 deletions app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,18 @@
*/
namespace Magento\CatalogUrlRewrite\Observer;

use Magento\Catalog\Model\Category;
use Magento\CatalogUrlRewrite\Model\CategoryBasedProductRewriteGenerator;
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\UrlRewrite\Model\UrlPersistInterface;
use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
use Magento\UrlRewrite\Model\MergeDataProviderFactory;

class UrlRewriteHandler
{
/** @var \Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider */
protected $childrenCategoriesProvider;

/** @var CategoryUrlRewriteGenerator */
/** @var \Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator */
protected $categoryUrlRewriteGenerator;

/** @var ProductUrlRewriteGenerator */
/** @var \Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator */
protected $productUrlRewriteGenerator;

/** @var UrlPersistInterface */
/** @var \Magento\UrlRewrite\Model\UrlPersistInterface */
protected $urlPersist;

/** @var array */
Expand All @@ -35,48 +25,59 @@ class UrlRewriteHandler
/** @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory */
protected $productCollectionFactory;

/**
* @var CategoryBasedProductRewriteGenerator
*/
/** @var \Magento\CatalogUrlRewrite\Model\CategoryBasedProductRewriteGenerator */
private $categoryBasedProductRewriteGenerator;

/** @var \Magento\UrlRewrite\Model\MergeDataProvider */
private $mergeDataProviderPrototype;

/** @var \Magento\Framework\Serialize\Serializer\Json */
private $serializer;

/**
* @param \Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider $childrenCategoriesProvider
* @param CategoryUrlRewriteGenerator $categoryUrlRewriteGenerator
* @param ProductUrlRewriteGenerator $productUrlRewriteGenerator
* @param UrlPersistInterface $urlPersist
* @param \Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator $categoryUrlRewriteGenerator
* @param \Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator $productUrlRewriteGenerator
* @param \Magento\UrlRewrite\Model\UrlPersistInterface $urlPersist
* @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
* @param \Magento\UrlRewrite\Model\MergeDataProviderFactory|null $mergeDataProviderFactory
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
*/
public function __construct(
\Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider $childrenCategoriesProvider,
CategoryUrlRewriteGenerator $categoryUrlRewriteGenerator,
ProductUrlRewriteGenerator $productUrlRewriteGenerator,
UrlPersistInterface $urlPersist,
\Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator $categoryUrlRewriteGenerator,
\Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator $productUrlRewriteGenerator,
\Magento\UrlRewrite\Model\UrlPersistInterface $urlPersist,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
MergeDataProviderFactory $mergeDataProviderFactory = null
\Magento\UrlRewrite\Model\MergeDataProviderFactory $mergeDataProviderFactory = null,
\Magento\Framework\Serialize\Serializer\Json $serializer = null
) {
$this->childrenCategoriesProvider = $childrenCategoriesProvider;
$this->categoryUrlRewriteGenerator = $categoryUrlRewriteGenerator;
$this->productUrlRewriteGenerator = $productUrlRewriteGenerator;
$this->urlPersist = $urlPersist;
$this->productCollectionFactory = $productCollectionFactory;

if (!isset($mergeDataProviderFactory)) {
$mergeDataProviderFactory = ObjectManager::getInstance()->get(MergeDataProviderFactory::class);
$mergeDataProviderFactory = \Magento\Framework\App\ObjectManager::getInstance()->get(
\Magento\UrlRewrite\Model\MergeDataProviderFactory::class
);
}

$this->mergeDataProviderPrototype = $mergeDataProviderFactory->create();

$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()->get(
\Magento\Framework\Serialize\Serializer\Json::class
);
}

/**
* Generate url rewrites for products assigned to category
*
* @param Category $category
* @param \Magento\Catalog\Model\Category $category
* @return array
*/
public function generateProductUrlRewrites(Category $category)
public function generateProductUrlRewrites(\Magento\Catalog\Model\Category $category)
{
$mergeDataProvider = clone $this->mergeDataProviderPrototype;
$this->isSkippedProduct = [];
Expand Down Expand Up @@ -123,14 +124,14 @@ public function generateProductUrlRewrites(Category $category)
}

/**
* @param Category $category
* @param \Magento\Catalog\Model\Category $category
* @param int $storeId
* @param bool $saveRewriteHistory
* @param int|null $rootCategoryId
* @return UrlRewrite[]
* @return array
*/
public function getCategoryProductsUrlRewrites(
Category $category,
\Magento\Catalog\Model\Category $category,
$storeId,
$saveRewriteHistory,
$rootCategoryId = null
Expand Down Expand Up @@ -161,37 +162,41 @@ public function getCategoryProductsUrlRewrites(
* Retrieve generator, which use single category for different products
*
* @deprecated
* @return CategoryBasedProductRewriteGenerator|mixed
* @return \Magento\CatalogUrlRewrite\Model\CategoryBasedProductRewriteGenerator|mixed
*/
private function getCategoryBasedProductRewriteGenerator()
{
if (!$this->categoryBasedProductRewriteGenerator) {
$this->categoryBasedProductRewriteGenerator = ObjectManager::getInstance()
->get(CategoryBasedProductRewriteGenerator::class);
$this->categoryBasedProductRewriteGenerator = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\CatalogUrlRewrite\Model\CategoryBasedProductRewriteGenerator::class);
}

return $this->categoryBasedProductRewriteGenerator;
}

/**
* @param Category $category
* @param \Magento\Catalog\Model\Category $category
* @return void
*/
public function deleteCategoryRewritesForChildren(Category $category)
public function deleteCategoryRewritesForChildren(\Magento\Catalog\Model\Category $category)
{
$categoryIds = $this->childrenCategoriesProvider->getChildrenIds($category, true);
$categoryIds[] = $category->getId();
foreach ($categoryIds as $categoryId) {
$this->urlPersist->deleteByData(
[
UrlRewrite::ENTITY_ID => $categoryId,
UrlRewrite::ENTITY_TYPE => CategoryUrlRewriteGenerator::ENTITY_TYPE,
\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::ENTITY_ID =>
$categoryId,
\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::ENTITY_TYPE =>
\Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator::ENTITY_TYPE,
]
);
$this->urlPersist->deleteByData(
[
UrlRewrite::METADATA => serialize(['category_id' => $categoryId]),
UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE,
\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::METADATA =>
$this->serializer->serialize(['category_id' => $categoryId]),
\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::ENTITY_TYPE =>
\Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator::ENTITY_TYPE,
]
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Magento\CatalogUrlRewrite\Test\Unit\Model\Category;

use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Framework\Serialize\Serializer\Json;

class ChildrenUrlRewriteGeneratorTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -27,8 +28,14 @@ class ChildrenUrlRewriteGeneratorTest extends \PHPUnit_Framework_TestCase
/** @var \PHPUnit_Framework_MockObject_MockObject */
private $mergeDataProvider;

/** @var \PHPUnit_Framework_MockObject_MockObject */
private $serializerMock;

protected function setUp()
{
$this->serializerMock = $this->getMockBuilder(Json::class)
->disableOriginalConstructor()
->getMock();
$this->childrenCategoriesProvider = $this->getMockBuilder(
\Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider::class
)->disableOriginalConstructor()->getMock();
Expand Down Expand Up @@ -84,13 +91,13 @@ public function testGenerate()
->will($this->returnValue($saveRewritesHistory));
$this->categoryUrlRewriteGeneratorFactory->expects($this->once())->method('create')
->will($this->returnValue($this->categoryUrlRewriteGenerator));
$url1 = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite();
$url1 = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite([], $this->serializerMock);
$url1->setRequestPath('category-1')
->setStoreId(1);
$url2 = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite();
$url2 = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite([], $this->serializerMock);
$url2->setRequestPath('category-2')
->setStoreId(2);
$url3 = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite();
$url3 = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite([], $this->serializerMock);
$url3->setRequestPath('category-1')
->setStoreId(1);
$this->categoryUrlRewriteGenerator->expects($this->once())->method('generate')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class RemoveTest extends \PHPUnit_Framework_TestCase
*/
private $objectMock;

/** @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject */
private $serializerMock;

protected function setUp()
{
$this->objectManager = new ObjectManager($this);
Expand All @@ -52,6 +55,7 @@ protected function setUp()
$this->objectMock = $this->getMockBuilder(Category::class)
->disableOriginalConstructor()
->getMock();
$this->serializerMock = $this->getMock(\Magento\Framework\Serialize\Serializer\Json::class, [], [], '', false);
}

public function testAroundDelete()
Expand All @@ -64,7 +68,8 @@ public function testAroundDelete()
CategoryRemovePlugin::class,
[
'urlPersist' => $this->urlPersistMock,
'childrenCategoriesProvider' => $this->childrenCategoriesProviderMock
'childrenCategoriesProvider' => $this->childrenCategoriesProviderMock,
'serializer' => $this->serializerMock
]
);
$this->childrenCategoriesProviderMock->expects($this->once())
Expand All @@ -76,6 +81,9 @@ public function testAroundDelete()
->willReturn(1);
$this->urlPersistMock->expects($this->exactly(2))
->method('deleteByData');
$this->serializerMock->expects($this->once())
->method('serialize')
->with(['category_id' => 1]);
$this->assertSame(
$this->subjectMock,
$plugin->aroundDelete($this->subjectMock, $proceed, $this->objectMock)
Expand Down
Loading

0 comments on commit 2a573cc

Please sign in to comment.