diff --git a/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Category/Remove.php b/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Category/Remove.php index e44aa9e0fab09..2617f3290ee0e 100644 --- a/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Category/Remove.php +++ b/app/code/Magento/CatalogUrlRewrite/Model/Category/Plugin/Category/Remove.php @@ -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(); @@ -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, ] ); } diff --git a/app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php b/app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php index 73b7c2b8167bb..9d53150675518 100644 --- a/app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php +++ b/app/code/Magento/CatalogUrlRewrite/Observer/UrlRewriteHandler.php @@ -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 */ @@ -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 = []; @@ -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 @@ -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, ] ); } diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/ChildrenUrlRewriteGeneratorTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/ChildrenUrlRewriteGeneratorTest.php index f41d35f7688f3..6ff97830c8e8f 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/ChildrenUrlRewriteGeneratorTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/ChildrenUrlRewriteGeneratorTest.php @@ -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 { @@ -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(); @@ -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') diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/Category/RemoveTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/Category/RemoveTest.php index 75b0509e0245d..a2cd880e85acc 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/Category/RemoveTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Category/Plugin/Category/RemoveTest.php @@ -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); @@ -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() @@ -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()) @@ -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) diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/CategoryUrlRewriteGeneratorTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/CategoryUrlRewriteGeneratorTest.php index c3a2ebb47b156..cfc6243525f42 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/CategoryUrlRewriteGeneratorTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/CategoryUrlRewriteGeneratorTest.php @@ -8,9 +8,13 @@ namespace Magento\CatalogUrlRewrite\Test\Unit\Model; -use Magento\Catalog\Model\Category; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; +/** + * Class CategoryUrlRewriteGeneratorTest + * + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + */ class CategoryUrlRewriteGeneratorTest extends \PHPUnit_Framework_TestCase { /** @var \PHPUnit_Framework_MockObject_MockObject */ @@ -37,11 +41,30 @@ class CategoryUrlRewriteGeneratorTest extends \PHPUnit_Framework_TestCase /** @var \PHPUnit_Framework_MockObject_MockObject */ private $mergeDataProvider; + /** @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject */ + protected $serializer; + /** * Test method */ protected function setUp() { + $this->serializer = $this->getMock(\Magento\Framework\Serialize\Serializer\Json::class, [], [], '', false); + $this->serializer->expects($this->any()) + ->method('serialize') + ->willReturnCallback( + function ($value) { + return json_encode($value); + } + ); + $this->serializer->expects($this->any()) + ->method('unserialize') + ->willReturnCallback( + function ($value) { + return json_decode($value, true); + } + ); + $this->currentUrlRewritesRegenerator = $this->getMockBuilder( \Magento\CatalogUrlRewrite\Model\Category\CurrentUrlRewritesRegenerator::class )->disableOriginalConstructor()->getMock(); @@ -88,21 +111,21 @@ public function testGenerationForGlobalScope() $this->category->expects($this->any())->method('getStoreIds')->will($this->returnValue([1])); $this->storeViewService->expects($this->once())->method('doesEntityHaveOverriddenUrlKeyForStore') ->will($this->returnValue(false)); - $canonical = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite(); + $canonical = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite([], $this->serializer); $canonical->setRequestPath('category-1') ->setStoreId(1); $this->canonicalUrlRewriteGenerator->expects($this->any())->method('generate') ->will($this->returnValue(['category-1' => $canonical])); - $children1 = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite(); + $children1 = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite([], $this->serializer); $children1->setRequestPath('category-2') ->setStoreId(2); - $children2 = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite(); + $children2 = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite([], $this->serializer); $children2->setRequestPath('category-22') ->setStoreId(2); $this->childrenUrlRewriteGenerator->expects($this->any())->method('generate') ->with(1, $this->category, $categoryId) ->will($this->returnValue(['category-2' => $children1, 'category-1' => $children2])); - $current = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite(); + $current = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite([], $this->serializer); $current->setRequestPath('category-3') ->setStoreId(3); $this->currentUrlRewritesRegenerator->expects($this->any())->method('generate') @@ -135,7 +158,7 @@ public function testGenerationForSpecificStore() { $this->category->expects($this->any())->method('getStoreId')->will($this->returnValue(1)); $this->category->expects($this->never())->method('getStoreIds'); - $canonical = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite(); + $canonical = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite([], $this->serializer); $canonical->setRequestPath('category-1') ->setStoreId(1); $this->canonicalUrlRewriteGenerator->expects($this->any())->method('generate') diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/UrlRewriteFinderTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/UrlRewriteFinderTest.php index b0c4500dab3e0..ca5581f366352 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/UrlRewriteFinderTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/Map/UrlRewriteFinderTest.php @@ -13,9 +13,12 @@ use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; use Magento\CatalogUrlRewrite\Model\Map\DataCategoryUrlRewriteDatabaseMap; use Magento\CatalogUrlRewrite\Model\Map\DataProductUrlRewriteDatabaseMap; +use Magento\Framework\Serialize\Serializer\Json; /** * Class UrlRewriteFinderTest + * + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class UrlRewriteFinderTest extends \PHPUnit_Framework_TestCase { @@ -34,12 +37,16 @@ class UrlRewriteFinderTest extends \PHPUnit_Framework_TestCase /** @var UrlRewriteFinder|\PHPUnit_Framework_MockObject_MockObject */ private $model; + /** @var \PHPUnit_Framework_MockObject_MockObject */ + private $serializerMock; + protected function setUp() { + $this->serializerMock = $this->getMock(Json::class, [], [], '', false); $this->databaseMapPoolMock = $this->getMock(DatabaseMapPool::class, [], [], '', false); $this->urlFinderMock = $this->getMock(UrlFinderInterface::class); $this->urlRewriteFactoryMock = $this->getMock(UrlRewriteFactory::class, ['create'], [], '', false); - $this->urlRewritePrototypeMock = new UrlRewrite(); + $this->urlRewritePrototypeMock = new UrlRewrite([], $this->serializerMock); $this->urlRewriteFactoryMock->expects($this->any()) ->method('create') diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/ProductScopeRewriteGeneratorTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/ProductScopeRewriteGeneratorTest.php index 15d93a5026f32..7bc0c96cd6e5c 100644 --- a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/ProductScopeRewriteGeneratorTest.php +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Model/ProductScopeRewriteGeneratorTest.php @@ -44,8 +44,27 @@ class ProductScopeRewriteGeneratorTest extends \PHPUnit_Framework_TestCase /** @var \PHPUnit_Framework_MockObject_MockObject */ private $mergeDataProvider; + /** @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject */ + private $serializer; + public function setUp() { + $this->serializer = $this->getMock(\Magento\Framework\Serialize\Serializer\Json::class, [], [], '', false); + $this->serializer->expects($this->any()) + ->method('serialize') + ->willReturnCallback( + function ($value) { + return json_encode($value); + } + ); + $this->serializer->expects($this->any()) + ->method('unserialize') + ->willReturnCallback( + function ($value) { + return json_decode($value, true); + } + ); + $this->currentUrlRewritesRegenerator = $this->getMockBuilder( \Magento\CatalogUrlRewrite\Model\Product\CurrentUrlRewritesRegenerator::class )->disableOriginalConstructor()->getMock(); @@ -103,22 +122,22 @@ public function testGenerationForGlobalScope() ->method('getParentId') ->willReturn(1); $this->initObjectRegistryFactory([]); - $canonical = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite(); + $canonical = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite([], $this->serializer); $canonical->setRequestPath('category-1') ->setStoreId(1); $this->canonicalUrlRewriteGenerator->expects($this->any())->method('generate') ->will($this->returnValue([$canonical])); - $categories = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite(); + $categories = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite([], $this->serializer); $categories->setRequestPath('category-2') ->setStoreId(2); $this->categoriesUrlRewriteGenerator->expects($this->any())->method('generate') ->will($this->returnValue([$categories])); - $current = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite(); + $current = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite([], $this->serializer); $current->setRequestPath('category-3') ->setStoreId(3); $this->currentUrlRewritesRegenerator->expects($this->any())->method('generate') ->will($this->returnValue([$current])); - $anchorCategories = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite(); + $anchorCategories = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite([], $this->serializer); $anchorCategories->setRequestPath('category-4') ->setStoreId(4); $this->anchorUrlRewriteGenerator->expects($this->any())->method('generate') @@ -150,7 +169,7 @@ public function testGenerationForSpecificStore() $store->expects($this->any())->method('getRootCategoryId')->will($this->returnValue($storeRootCategoryId)); $this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($store)); $this->initObjectRegistryFactory([$category]); - $canonical = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite(); + $canonical = new \Magento\UrlRewrite\Service\V1\Data\UrlRewrite([], $this->serializer); $canonical->setRequestPath('category-1') ->setStoreId(1); $this->canonicalUrlRewriteGenerator->expects($this->any())->method('generate') diff --git a/app/code/Magento/CatalogUrlRewrite/Test/Unit/Observer/UrlRewriteHandlerTest.php b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Observer/UrlRewriteHandlerTest.php new file mode 100644 index 0000000000000..3c91e485e77e1 --- /dev/null +++ b/app/code/Magento/CatalogUrlRewrite/Test/Unit/Observer/UrlRewriteHandlerTest.php @@ -0,0 +1,123 @@ +childrenCategoriesProviderMock = $this->getMockBuilder(ChildrenCategoriesProvider::class) + ->getMock(); + $this->categoryUrlRewriteGeneratorMock = $this->getMockBuilder(CategoryUrlRewriteGenerator::class) + ->disableOriginalConstructor() + ->getMock(); + $this->productUrlRewriteGeneratorMock = $this->getMockBuilder(ProductUrlRewriteGenerator::class) + ->disableOriginalConstructor() + ->getMock(); + $this->urlPersistMock = $this->getMockBuilder(UrlPersistInterface::class) + ->getMock(); + $this->collectionFactoryMock = $this->getMockBuilder(CollectionFactory::class) + ->disableOriginalConstructor() + ->getMock(); + $this->mergeDataProviderFactoryMock = $this->getMockBuilder(MergeDataProviderFactory::class) + ->setMethods(['create']) + ->disableOriginalConstructor() + ->getMock(); + $mergeDataProviderMock = $this->getMockBuilder(MergeDataProvider::class) + ->disableOriginalConstructor() + ->getMock(); + $this->mergeDataProviderFactoryMock->expects($this->any()) + ->method('create') + ->willReturn($mergeDataProviderMock); + + $this->serializerMock = $this->getMockBuilder(Json::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->urlRewriteHandler = new UrlRewriteHandler( + $this->childrenCategoriesProviderMock, + $this->categoryUrlRewriteGeneratorMock, + $this->productUrlRewriteGeneratorMock, + $this->urlPersistMock, + $this->collectionFactoryMock, + $this->mergeDataProviderFactoryMock, + $this->serializerMock + ); + } + + public function testDeleteCategoryRewritesForChildren() + { + $category = $this->getMockBuilder(\Magento\Catalog\Model\Category::class) + ->disableOriginalConstructor() + ->getMock(); + $category->expects($this->once()) + ->method('getId') + ->willReturn(2); + + $this->childrenCategoriesProviderMock->expects($this->once()) + ->method('getChildrenIds') + ->with($category, true) + ->willReturn([3, 4]); + + $this->serializerMock->expects($this->exactly(3)) + ->method('serialize'); + + $this->urlRewriteHandler->deleteCategoryRewritesForChildren($category); + } +} diff --git a/app/code/Magento/SampleData/etc/module.xml b/app/code/Magento/SampleData/etc/module.xml index 24be4417e689f..e511dfd4bbb76 100644 --- a/app/code/Magento/SampleData/etc/module.xml +++ b/app/code/Magento/SampleData/etc/module.xml @@ -7,5 +7,8 @@ --> + + + diff --git a/app/code/Magento/UrlRewrite/Controller/Adminhtml/Url/Rewrite/Save.php b/app/code/Magento/UrlRewrite/Controller/Adminhtml/Url/Rewrite/Save.php index 5b2698749019e..8181f3c6d80a9 100644 --- a/app/code/Magento/UrlRewrite/Controller/Adminhtml/Url/Rewrite/Save.php +++ b/app/code/Magento/UrlRewrite/Controller/Adminhtml/Url/Rewrite/Save.php @@ -6,8 +6,6 @@ */ namespace Magento\UrlRewrite\Controller\Adminhtml\Url\Rewrite; -use Magento\Catalog\Model\Category; -use Magento\Catalog\Model\Product; use Magento\Framework\Exception\LocalizedException; use Magento\UrlRewrite\Model\UrlFinderInterface; use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; @@ -63,7 +61,7 @@ protected function _handleCatalogUrlRewrite($model) $model->setEntityType($productId ? self::ENTITY_TYPE_PRODUCT : self::ENTITY_TYPE_CATEGORY) ->setEntityId($productId ?: $categoryId); if ($productId && $categoryId) { - $model->setMetadata(serialize(['category_id' => $categoryId])); + $model->setMetadata(['category_id' => $categoryId]); } } $model->setTargetPath($this->getTargetPath($model)); diff --git a/app/code/Magento/UrlRewrite/Model/UrlRewrite.php b/app/code/Magento/UrlRewrite/Model/UrlRewrite.php index b8f97ace72936..e2eaae257b291 100644 --- a/app/code/Magento/UrlRewrite/Model/UrlRewrite.php +++ b/app/code/Magento/UrlRewrite/Model/UrlRewrite.php @@ -5,6 +5,9 @@ */ namespace Magento\UrlRewrite\Model; +use Magento\Framework\App\ObjectManager; +use Magento\Framework\Serialize\Serializer\Json; + /** * @method int getEntityId() * @method string getEntityType() @@ -14,7 +17,6 @@ * @method string getTargetPath() * @method UrlRewrite setEntityId(int $value) * @method UrlRewrite setEntityType(string $value) - * @method UrlRewrite setMetadata($value) * @method UrlRewrite setRequestPath($value) * @method UrlRewrite setTargetPath($value) * @method UrlRewrite setRedirectType($value) @@ -23,6 +25,32 @@ */ class UrlRewrite extends \Magento\Framework\Model\AbstractModel { + /** + * @var Json + */ + private $serializer; + + /** + * UrlRewrite constructor. + * @param \Magento\Framework\Model\Context $context + * @param \Magento\Framework\Registry $registry + * @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource + * @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection + * @param array $data + * @param Json $serializer + */ + public function __construct( + \Magento\Framework\Model\Context $context, + \Magento\Framework\Registry $registry, + \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null, + \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null, + array $data = [], + Json $serializer = null + ) { + $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class); + parent::__construct($context, $registry, $resource, $resourceCollection, $data); + } + /** * Initialize corresponding resource model * @@ -41,6 +69,21 @@ protected function _construct() public function getMetadata() { $metadata = $this->getData(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::METADATA); - return !empty($metadata) ? unserialize($metadata) : []; + return !empty($metadata) ? $this->serializer->unserialize($metadata) : []; + } + + /** + * Overwrite Metadata in the object. + * + * @param array|string $metadata + * + * @return $this + */ + public function setMetadata($metadata) + { + if (is_array($metadata)) { + $metadata = $this->serializer->serialize($metadata); + } + return $this->setData(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::METADATA, $metadata); } } diff --git a/app/code/Magento/UrlRewrite/Service/V1/Data/UrlRewrite.php b/app/code/Magento/UrlRewrite/Service/V1/Data/UrlRewrite.php index 11315279ff219..8ed0479a07637 100644 --- a/app/code/Magento/UrlRewrite/Service/V1/Data/UrlRewrite.php +++ b/app/code/Magento/UrlRewrite/Service/V1/Data/UrlRewrite.php @@ -6,6 +6,8 @@ namespace Magento\UrlRewrite\Service\V1\Data; use Magento\Framework\Api\AbstractSimpleObject; +use Magento\Framework\App\ObjectManager; +use Magento\Framework\Serialize\Serializer\Json; /** * Data abstract class for url storage @@ -37,6 +39,25 @@ class UrlRewrite extends AbstractSimpleObject self::DESCRIPTION => null, ]; + /** + * @var Json + */ + private $serializer; + + /** + * UrlRewrite constructor. + * + * @param array $data + * @param Json $serializer + */ + public function __construct( + $data = [], + Json $serializer = null + ) { + $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class); + parent::__construct($data); + } + /** * Get data by key * @@ -216,7 +237,7 @@ public function setDescription($description) public function getMetadata() { $metadata = $this->_get(self::METADATA); - return !empty($metadata) ? unserialize($metadata) : []; + return !empty($metadata) ? $this->serializer->unserialize($metadata) : []; } /** @@ -227,7 +248,7 @@ public function getMetadata() public function setMetadata($metadata) { if (is_array($metadata)) { - $metadata = serialize($metadata); + $metadata = $this->serializer->serialize($metadata); } return $this->setData(UrlRewrite::METADATA, $metadata); } diff --git a/app/code/Magento/UrlRewrite/Setup/UpgradeData.php b/app/code/Magento/UrlRewrite/Setup/UpgradeData.php new file mode 100644 index 0000000000000..ad71639398e1b --- /dev/null +++ b/app/code/Magento/UrlRewrite/Setup/UpgradeData.php @@ -0,0 +1,63 @@ +fieldDataConverterFactory = $fieldDataConverterFactory; + } + + /** + * @inheritdoc + */ + public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) + { + $setup->startSetup(); + + if (version_compare($context->getVersion(), '2.0.1', '<')) { + $this->convertSerializedDataToJson($setup); + } + + $setup->endSetup(); + } + + /** + * Convert metadata from serialized to JSON format: + * + * @param ModuleDataSetupInterface $setup + * + * @return void + */ + public function convertSerializedDataToJson($setup) + { + $fieldDataConverter = $this->fieldDataConverterFactory->create(SerializedToJson::class); + $fieldDataConverter->convert( + $setup->getConnection(), + $setup->getTable('url_rewrite'), + 'url_rewrite_id', + 'metadata' + ); + } +} diff --git a/app/code/Magento/UrlRewrite/Test/Unit/Block/Plugin/Store/Switcher/SetRedirectUrlTest.php b/app/code/Magento/UrlRewrite/Test/Unit/Block/Plugin/Store/Switcher/SetRedirectUrlTest.php index 9cf18a76cd045..5118d12fad6c6 100644 --- a/app/code/Magento/UrlRewrite/Test/Unit/Block/Plugin/Store/Switcher/SetRedirectUrlTest.php +++ b/app/code/Magento/UrlRewrite/Test/Unit/Block/Plugin/Store/Switcher/SetRedirectUrlTest.php @@ -81,7 +81,7 @@ public function testTrimPathInfoForGetTargetStorePostData() public function testGetTargetStorePostData() { - $urlRewrite = $this->getMock(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class); + $urlRewrite = $this->getMock(\Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class, [], [], '', false); $urlRewrite->expects($this->once())->method('getRequestPath')->willReturn('path'); $this->request->expects($this->once())->method('getPathInfo')->willReturn('path'); diff --git a/app/code/Magento/UrlRewrite/Test/Unit/Model/UrlRewriteTest.php b/app/code/Magento/UrlRewrite/Test/Unit/Model/UrlRewriteTest.php new file mode 100644 index 0000000000000..db536c5aa38b3 --- /dev/null +++ b/app/code/Magento/UrlRewrite/Test/Unit/Model/UrlRewriteTest.php @@ -0,0 +1,66 @@ +getMock(\Magento\Framework\Model\Context::class, [], [], '', false); + $registry = $this->getMock(\Magento\Framework\Registry::class, [], [], '', false); + $resource = $this->getMock( + \Magento\Framework\Model\ResourceModel\AbstractResource::class, + ['getIdFieldName', '_construct', 'getConnection'], + [], + '', + false + ); + $resourceCollection = $this->getMock(\Magento\Framework\Data\Collection\AbstractDb::class, [], [], '', false); + $serializer = $this->getMock(\Magento\Framework\Serialize\Serializer\Json::class, [], [], '', false); + $serializer->expects($this->any()) + ->method('serialize') + ->willReturnCallback( + function ($value) { + return json_encode($value); + } + ); + $serializer->expects($this->any()) + ->method('unserialize') + ->willReturnCallback( + function ($value) { + return json_decode($value, true); + } + ); + + $this->model = $objectManager->getObject( + \Magento\UrlRewrite\Model\UrlRewrite::class, + [ + 'context' => $context, + 'registry' => $registry, + 'resource' => $resource, + 'resourceCollection' => $resourceCollection, + 'data' => [], + 'serializer' => $serializer, + ] + ); + } + + public function testSetAndGetMetadata() + { + $testData = [1, 2, 3]; + + $this->model->setMetadata($testData); + + $this->assertEquals($testData, $this->model->getMetadata()); + } +} diff --git a/app/code/Magento/UrlRewrite/Test/Unit/Service/V1/Data/UrlRewriteTest.php b/app/code/Magento/UrlRewrite/Test/Unit/Service/V1/Data/UrlRewriteTest.php new file mode 100644 index 0000000000000..bce4505b2eda2 --- /dev/null +++ b/app/code/Magento/UrlRewrite/Test/Unit/Service/V1/Data/UrlRewriteTest.php @@ -0,0 +1,52 @@ +getMock(\Magento\Framework\Serialize\Serializer\Json::class, [], [], '', false); + $serializer->expects($this->any()) + ->method('serialize') + ->willReturnCallback( + function ($value) { + return json_encode($value); + } + ); + $serializer->expects($this->any()) + ->method('unserialize') + ->willReturnCallback( + function ($value) { + return json_decode($value, true); + } + ); + + $this->model = $objectManager->getObject( + \Magento\UrlRewrite\Service\V1\Data\UrlRewrite::class, + [ + 'data' => [], + 'serializer' => $serializer, + ] + ); + } + + public function testSetAndGetMetadata() + { + $testData = [1, 2, 3]; + + $this->model->setMetadata($testData); + + $this->assertEquals($testData, $this->model->getMetadata()); + } +} diff --git a/app/code/Magento/UrlRewrite/etc/module.xml b/app/code/Magento/UrlRewrite/etc/module.xml index 284c67950ac53..499a5166a8606 100644 --- a/app/code/Magento/UrlRewrite/etc/module.xml +++ b/app/code/Magento/UrlRewrite/etc/module.xml @@ -6,6 +6,6 @@ */ --> - + diff --git a/setup/performance-toolkit/benchmark.jmx b/setup/performance-toolkit/benchmark.jmx index 6ba614e16665c..918d5bf4622ac 100644 --- a/setup/performance-toolkit/benchmark.jmx +++ b/setup/performance-toolkit/benchmark.jmx @@ -3853,7 +3853,7 @@ vars.put("loadType", "Guest"); - + true