|
| 1 | +<?php |
| 2 | +/************************************************************************ |
| 3 | + * |
| 4 | + * Copyright 2023 Adobe |
| 5 | + * All Rights Reserved. |
| 6 | + * |
| 7 | + * NOTICE: All information contained herein is, and remains |
| 8 | + * the property of Adobe and its suppliers, if any. The intellectual |
| 9 | + * and technical concepts contained herein are proprietary to Adobe |
| 10 | + * and its suppliers and are protected by all applicable intellectual |
| 11 | + * property laws, including trade secret and copyright laws. |
| 12 | + * Dissemination of this information or reproduction of this material |
| 13 | + * is strictly forbidden unless prior written permission is obtained |
| 14 | + * from Adobe. |
| 15 | + * ************************************************************************ |
| 16 | + */ |
| 17 | +declare(strict_types=1); |
| 18 | + |
| 19 | +namespace Magento\CatalogUrlRewrite\Observer; |
| 20 | + |
| 21 | +use Magento\Catalog\Api\CategoryRepositoryInterface; |
| 22 | +use Magento\Catalog\Model\CategoryFactory; |
| 23 | +use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory; |
| 24 | +use Magento\Catalog\Test\Fixture\Category as CategoryFixture; |
| 25 | +use Magento\Framework\App\Response\Http; |
| 26 | +use Magento\Framework\ObjectManagerInterface; |
| 27 | +use Magento\Store\Model\Store as StoreModel; |
| 28 | +use Magento\Store\Model\StoreManagerInterface; |
| 29 | +use Magento\Store\Test\Fixture\Group; |
| 30 | +use Magento\Store\Test\Fixture\Store; |
| 31 | +use Magento\Store\Test\Fixture\Website; |
| 32 | +use Magento\TestFramework\Fixture\AppIsolation; |
| 33 | +use Magento\TestFramework\Fixture\DataFixture; |
| 34 | +use Magento\TestFramework\Fixture\DataFixtureBeforeTransaction; |
| 35 | +use Magento\TestFramework\Fixture\DataFixtureStorage; |
| 36 | +use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
| 37 | +use Magento\TestFramework\Fixture\DbIsolation; |
| 38 | +use Magento\TestFramework\Helper\Bootstrap; |
| 39 | +use Magento\TestFramework\TestCase\AbstractController; |
| 40 | + |
| 41 | +class CategoryUrlPathAutogeneratorObserverTest extends AbstractController |
| 42 | +{ |
| 43 | + /** |
| 44 | + * @var ObjectManagerInterface |
| 45 | + */ |
| 46 | + private $objectManager; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var DataFixtureStorage |
| 50 | + */ |
| 51 | + private $fixtures; |
| 52 | + |
| 53 | + /** @var StoreManagerInterface */ |
| 54 | + private $storeManager; |
| 55 | + |
| 56 | + /** @var CategoryRepositoryInterface */ |
| 57 | + private $categoryRepository; |
| 58 | + |
| 59 | + /** |
| 60 | + * @var CategoryFactory |
| 61 | + */ |
| 62 | + private $categoryFactory; |
| 63 | + |
| 64 | + /** |
| 65 | + * @var CollectionFactory |
| 66 | + */ |
| 67 | + private $categoryCollectionFactory; |
| 68 | + protected function setUp(): void |
| 69 | + { |
| 70 | + parent::setUp(); |
| 71 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 72 | + $this->storeManager = $this->objectManager->get(StoreManagerInterface::class); |
| 73 | + $this->categoryRepository = $this->objectManager->get(CategoryRepositoryInterface::class); |
| 74 | + $this->categoryFactory = $this->objectManager->get(CategoryFactory::class); |
| 75 | + $this->categoryCollectionFactory = $this->objectManager->get(CollectionFactory::class); |
| 76 | + $this->fixtures = DataFixtureStorageManager::getStorage(); |
| 77 | + } |
| 78 | + |
| 79 | + #[ |
| 80 | + DbIsolation(true), |
| 81 | + AppIsolation(true), |
| 82 | + DataFixtureBeforeTransaction(Website::class, as: 'website2'), |
| 83 | + DataFixtureBeforeTransaction(Group::class, ['website_id' => '$website2.id$'], as:'group2'), |
| 84 | + DataFixtureBeforeTransaction( |
| 85 | + Store::class, |
| 86 | + ['website_id' => '$website2.id$', 'group_id' => '$group2.id$'], |
| 87 | + as:'store2' |
| 88 | + ), |
| 89 | + DataFixture(CategoryFixture::class, ['url_key' => 'default-store-category1'], as:'category1') |
| 90 | + ] |
| 91 | + public function testChildrenUrlPathContainsParentCustomScopeUrlKey() |
| 92 | + { |
| 93 | + $category1 = $this->fixtures->get('category1'); |
| 94 | + $secondStore = $this->fixtures->get('store2'); |
| 95 | + |
| 96 | + $this->storeManager->setCurrentStore($secondStore); |
| 97 | + |
| 98 | + $secondStoreCategory1 = $this->categoryRepository->get($category1->getId(), $secondStore->getId()); |
| 99 | + $secondStoreCategory1->setUrlKey('second-store-category-'.$category1->getId()); |
| 100 | + $this->categoryRepository->save($secondStoreCategory1); |
| 101 | + |
| 102 | + $this->storeManager->setCurrentStore(StoreModel::DEFAULT_STORE_ID); |
| 103 | + |
| 104 | + $categoryData2 = $this->categoryFactory->create()->setData( |
| 105 | + [ |
| 106 | + 'parent_id' => $category1->getId(), |
| 107 | + 'name' => 'Category 2', |
| 108 | + 'url_key' => null, |
| 109 | + 'is_active' => true |
| 110 | + ] |
| 111 | + ); |
| 112 | + $category2 = $this->categoryRepository->save($categoryData2); |
| 113 | + |
| 114 | + $this->storeManager->setCurrentStore($secondStore); |
| 115 | + |
| 116 | + $category2 = $this->categoryRepository->get($category2->getId()); |
| 117 | + $category2->setUrlKey(null); |
| 118 | + $this->categoryRepository->save($category2); |
| 119 | + |
| 120 | + $this->storeManager->setCurrentStore(StoreModel::DEFAULT_STORE_ID); |
| 121 | + |
| 122 | + $categoryData3 = $this->categoryFactory->create()->setData( |
| 123 | + [ |
| 124 | + 'parent_id' => $category2->getId(), |
| 125 | + 'name' => 'Category 3', |
| 126 | + 'url_key' => 'default-store-category3', |
| 127 | + 'is_active' => true |
| 128 | + ] |
| 129 | + ); |
| 130 | + $category3 = $this->categoryRepository->save($categoryData3); |
| 131 | + |
| 132 | + $this->storeManager->setCurrentStore($secondStore); |
| 133 | + |
| 134 | + $categories = $this->categoryCollectionFactory->create() |
| 135 | + ->addAttributeToSelect('*') |
| 136 | + ->setStoreId($secondStore->getId()) |
| 137 | + ->addFieldToFilter( |
| 138 | + 'entity_id', |
| 139 | + [ |
| 140 | + 'in' => |
| 141 | + [ |
| 142 | + $category1->getId(), |
| 143 | + $category2->getId(), |
| 144 | + $category3->getId() |
| 145 | + ] |
| 146 | + ] |
| 147 | + ); |
| 148 | + |
| 149 | + $fullPath = []; |
| 150 | + foreach ($categories as $category) { |
| 151 | + $fullPath[] = $category->getUrlKey(); |
| 152 | + } |
| 153 | + |
| 154 | + $fullPath = implode('/', $fullPath) . '.html'; |
| 155 | + $this->dispatch($fullPath); |
| 156 | + $response = $this->getResponse(); |
| 157 | + |
| 158 | + $this->assertStringContainsString($fullPath, $response->getBody()); |
| 159 | + $this->assertEquals( |
| 160 | + Http::STATUS_CODE_200, |
| 161 | + $response->getHttpResponseCode(), |
| 162 | + 'Response code does not match expected value' |
| 163 | + ); |
| 164 | + } |
| 165 | +} |
0 commit comments