|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\CatalogUrlRewrite\Model; |
| 9 | + |
| 10 | +use Magento\Catalog\Model\Category; |
| 11 | +use Magento\Catalog\Model\CategoryFactory; |
| 12 | +use Magento\Catalog\Model\CategoryRepository; |
| 13 | +use Magento\CatalogUrlRewrite\Model\Map\DataCategoryUrlRewriteDatabaseMap; |
| 14 | +use Magento\Framework\ObjectManagerInterface; |
| 15 | +use Magento\TestFramework\Helper\Bootstrap; |
| 16 | +use Magento\UrlRewrite\Model\ResourceModel\UrlRewriteCollectionFactory; |
| 17 | +use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | + |
| 20 | +/** |
| 21 | + * Class for category url rewrites tests |
| 22 | + * |
| 23 | + * @magentoAppArea adminhtml |
| 24 | + * @magentoDbIsolation enabled |
| 25 | + */ |
| 26 | +class CategoryUrlRewriteTest extends TestCase |
| 27 | +{ |
| 28 | + /** @var ObjectManagerInterface */ |
| 29 | + private $objectManager; |
| 30 | + |
| 31 | + /** @var CategoryFactory */ |
| 32 | + private $categoryFactory; |
| 33 | + |
| 34 | + /** @var UrlRewriteCollectionFactory */ |
| 35 | + private $urlRewriteCollectionFactory; |
| 36 | + |
| 37 | + /** @var CategoryRepository */ |
| 38 | + private $categoryRepository; |
| 39 | + |
| 40 | + /** |
| 41 | + * @inheritDoc |
| 42 | + */ |
| 43 | + protected function setUp() |
| 44 | + { |
| 45 | + parent::setUp(); |
| 46 | + |
| 47 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 48 | + $this->categoryFactory = $this->objectManager->get(CategoryFactory::class); |
| 49 | + $this->urlRewriteCollectionFactory = $this->objectManager->get(UrlRewriteCollectionFactory::class); |
| 50 | + $this->categoryRepository = $this->objectManager->get(CategoryRepository::class); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @magentoConfigFixture default/catalog/seo/generate_category_product_rewrites 1 |
| 55 | + * @magentoDataFixture Magento/Catalog/_files/category_with_position.php |
| 56 | + * @dataProvider categoryProvider |
| 57 | + * @param array $data |
| 58 | + * @return void |
| 59 | + */ |
| 60 | + public function testUrlRewriteOnCategorySave(array $data): void |
| 61 | + { |
| 62 | + $categoryModel = $this->categoryFactory->create(); |
| 63 | + $categoryModel->isObjectNew(true); |
| 64 | + $categoryModel->setData($data['data']); |
| 65 | + $category = $categoryModel->save($categoryModel); |
| 66 | + $this->assertNotNull($category->getId(), 'The category was not created'); |
| 67 | + $urlRewriteCollection = $this->urlRewriteCollectionFactory->create(); |
| 68 | + $urlRewriteCollection->addFieldToFilter(UrlRewrite::ENTITY_ID, ['eq' => $category->getId()]) |
| 69 | + ->addFieldToFilter(UrlRewrite::ENTITY_TYPE, ['eq' => DataCategoryUrlRewriteDatabaseMap::ENTITY_TYPE]); |
| 70 | + |
| 71 | + foreach ($urlRewriteCollection as $item) { |
| 72 | + foreach ($data['expected_data'] as $field => $expectedItem) { |
| 73 | + $this->assertEquals( |
| 74 | + sprintf($expectedItem, $category->getId()), |
| 75 | + $item[$field], |
| 76 | + 'The expected data does not match actual value' |
| 77 | + ); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @return array |
| 84 | + */ |
| 85 | + public function categoryProvider(): array |
| 86 | + { |
| 87 | + return [ |
| 88 | + 'without_url_key' => [ |
| 89 | + [ |
| 90 | + 'data' => [ |
| 91 | + 'name' => 'Test Category', |
| 92 | + 'attribute_set_id' => '3', |
| 93 | + 'parent_id' => 2, |
| 94 | + 'path' => '1/2', |
| 95 | + 'is_active' => true, |
| 96 | + ], |
| 97 | + 'expected_data' => [ |
| 98 | + 'request_path' => 'test-category.html', |
| 99 | + 'target_path' => 'catalog/category/view/id/%s', |
| 100 | + ], |
| 101 | + ], |
| 102 | + ], |
| 103 | + 'subcategory_without_url_key' => [ |
| 104 | + [ |
| 105 | + 'data' => [ |
| 106 | + 'name' => 'Test Sub Category', |
| 107 | + 'attribute_set_id' => '3', |
| 108 | + 'parent_id' => 444, |
| 109 | + 'path' => '1/2/444', |
| 110 | + 'is_active' => true, |
| 111 | + ], |
| 112 | + 'expected_data' => [ |
| 113 | + 'request_path' => 'category-1/test-sub-category.html', |
| 114 | + 'target_path' => 'catalog/category/view/id/%s', |
| 115 | + ], |
| 116 | + ], |
| 117 | + ], |
| 118 | + ]; |
| 119 | + } |
| 120 | +} |
0 commit comments