|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\CatalogUrlRewrite\Observer; |
| 7 | + |
| 8 | +use Magento\Store\Model\StoreManagerInterface; |
| 9 | +use Magento\UrlRewrite\Service\V1\Data\UrlRewrite; |
| 10 | +use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator; |
| 11 | + |
| 12 | +/** |
| 13 | + * @magentoAppArea adminhtml |
| 14 | + */ |
| 15 | +class ProductProcessUrlRewriteSavingObserverTest extends \PHPUnit\Framework\TestCase |
| 16 | +{ |
| 17 | + /** @var \Magento\Framework\ObjectManagerInterface */ |
| 18 | + protected $objectManager; |
| 19 | + |
| 20 | + protected function setUp() |
| 21 | + { |
| 22 | + $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * @param array $filter |
| 27 | + * @return array |
| 28 | + */ |
| 29 | + private function getActualResults(array $filter) |
| 30 | + { |
| 31 | + /** @var \Magento\UrlRewrite\Model\UrlFinderInterface $urlFinder */ |
| 32 | + $urlFinder = $this->objectManager->get(\Magento\UrlRewrite\Model\UrlFinderInterface::class); |
| 33 | + $actualResults = []; |
| 34 | + foreach ($urlFinder->findAllByData($filter) as $url) { |
| 35 | + $actualResults[] = [ |
| 36 | + 'request_path' => $url->getRequestPath(), |
| 37 | + 'target_path' => $url->getTargetPath(), |
| 38 | + 'is_auto_generated' => (int)$url->getIsAutogenerated(), |
| 39 | + 'redirect_type' => $url->getRedirectType(), |
| 40 | + 'store_id' => $url->getStoreId() |
| 41 | + ]; |
| 42 | + } |
| 43 | + return $actualResults; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @magentoDataFixture Magento/CatalogUrlRewrite/_files/product_rewrite_multistore.php |
| 48 | + * @magentoAppIsolation enabled |
| 49 | + */ |
| 50 | + public function testUrlKeyHasChangedInGlobalContext() |
| 51 | + { |
| 52 | + /** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository*/ |
| 53 | + $productRepository = $this->objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class); |
| 54 | + /** @var \Magento\Catalog\Model\Product $product*/ |
| 55 | + $product = $productRepository->get('product1'); |
| 56 | + |
| 57 | + /** @var StoreManagerInterface $storeManager */ |
| 58 | + $storeManager = $this->objectManager->get(StoreManagerInterface::class); |
| 59 | + $storeManager->setCurrentStore(0); |
| 60 | + |
| 61 | + $testStore = $storeManager->getStore('test'); |
| 62 | + $productFilter = [ |
| 63 | + UrlRewrite::ENTITY_TYPE => 'product', |
| 64 | + ]; |
| 65 | + |
| 66 | + $expected = [ |
| 67 | + [ |
| 68 | + 'request_path' => "product-1.html", |
| 69 | + 'target_path' => "catalog/product/view/id/" . $product->getId(), |
| 70 | + 'is_auto_generated' => 1, |
| 71 | + 'redirect_type' => 0, |
| 72 | + 'store_id' => 1, |
| 73 | + ], |
| 74 | + [ |
| 75 | + 'request_path' => "product-1.html", |
| 76 | + 'target_path' => "catalog/product/view/id/" . $product->getId(), |
| 77 | + 'is_auto_generated' => 1, |
| 78 | + 'redirect_type' => 0, |
| 79 | + 'store_id' => $testStore->getId(), |
| 80 | + ], |
| 81 | + ]; |
| 82 | + $actual = $this->getActualResults($productFilter); |
| 83 | + foreach ($expected as $row) { |
| 84 | + $this->assertContains($row, $actual); |
| 85 | + } |
| 86 | + |
| 87 | + $product->setData('save_rewrites_history', true); |
| 88 | + $product->setUrlKey('new-url'); |
| 89 | + $product->save(); |
| 90 | + |
| 91 | + $expected = [ |
| 92 | + [ |
| 93 | + 'request_path' => "new-url.html", |
| 94 | + 'target_path' => "catalog/product/view/id/" . $product->getId(), |
| 95 | + 'is_auto_generated' => 1, |
| 96 | + 'redirect_type' => 0, |
| 97 | + 'store_id' => 1, |
| 98 | + ], |
| 99 | + [ |
| 100 | + 'request_path' => "new-url.html", |
| 101 | + 'target_path' => "catalog/product/view/id/" . $product->getId(), |
| 102 | + 'is_auto_generated' => 1, |
| 103 | + 'redirect_type' => 0, |
| 104 | + 'store_id' => $testStore->getId(), |
| 105 | + ], |
| 106 | + [ |
| 107 | + 'request_path' => "product-1.html", |
| 108 | + 'target_path' => "new-url.html", |
| 109 | + 'is_auto_generated' => 0, |
| 110 | + 'redirect_type' => 301, |
| 111 | + 'store_id' => 1, |
| 112 | + ], |
| 113 | + [ |
| 114 | + 'request_path' => "product-1.html", |
| 115 | + 'target_path' => "new-url.html", |
| 116 | + 'is_auto_generated' => 0, |
| 117 | + 'redirect_type' => 301, |
| 118 | + 'store_id' => $testStore->getId(), |
| 119 | + ], |
| 120 | + ]; |
| 121 | + |
| 122 | + $actual = $this->getActualResults($productFilter); |
| 123 | + foreach ($expected as $row) { |
| 124 | + $this->assertContains($row, $actual); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * @magentoDataFixture Magento/CatalogUrlRewrite/_files/product_rewrite_multistore.php |
| 130 | + * @magentoAppIsolation enabled |
| 131 | + */ |
| 132 | + public function testUrlKeyHasChangedInStoreviewContextWithPermanentRedirection() |
| 133 | + { |
| 134 | + /** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository*/ |
| 135 | + $productRepository = $this->objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class); |
| 136 | + /** @var \Magento\Catalog\Model\Product $product*/ |
| 137 | + $product = $productRepository->get('product1'); |
| 138 | + |
| 139 | + /** @var StoreManagerInterface $storeManager */ |
| 140 | + $storeManager = $this->objectManager->get(StoreManagerInterface::class); |
| 141 | + $storeManager->setCurrentStore(1); |
| 142 | + |
| 143 | + $testStore = $storeManager->getStore('test'); |
| 144 | + |
| 145 | + $productFilter = [ |
| 146 | + UrlRewrite::ENTITY_TYPE => 'product', |
| 147 | + ]; |
| 148 | + |
| 149 | + $product->setData('save_rewrites_history', true); |
| 150 | + $product->setUrlKey('new-url'); |
| 151 | + $product->save(); |
| 152 | + |
| 153 | + $expected = [ |
| 154 | + [ |
| 155 | + 'request_path' => "new-url.html", |
| 156 | + 'target_path' => "catalog/product/view/id/" . $product->getId(), |
| 157 | + 'is_auto_generated' => 1, |
| 158 | + 'redirect_type' => 0, |
| 159 | + 'store_id' => 1, |
| 160 | + ], |
| 161 | + [ |
| 162 | + 'request_path' => "product-1.html", |
| 163 | + 'target_path' => "catalog/product/view/id/" . $product->getId(), |
| 164 | + 'is_auto_generated' => 1, |
| 165 | + 'redirect_type' => 0, |
| 166 | + 'store_id' => $testStore->getId(), |
| 167 | + ], |
| 168 | + [ |
| 169 | + 'request_path' => "product-1.html", |
| 170 | + 'target_path' => "new-url.html", |
| 171 | + 'is_auto_generated' => 0, |
| 172 | + 'redirect_type' => 301, |
| 173 | + 'store_id' => 1, |
| 174 | + ], |
| 175 | + ]; |
| 176 | + |
| 177 | + $actual = $this->getActualResults($productFilter); |
| 178 | + foreach ($expected as $row) { |
| 179 | + $this->assertContains($row, $actual); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * @magentoDataFixture Magento/CatalogUrlRewrite/_files/product_rewrite_multistore.php |
| 185 | + * @magentoAppIsolation enabled |
| 186 | + */ |
| 187 | + public function testUrlKeyHasChangedInStoreviewContextWithoutPermanentRedirection() |
| 188 | + { |
| 189 | + /** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository*/ |
| 190 | + $productRepository = $this->objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class); |
| 191 | + /** @var \Magento\Catalog\Model\Product $product*/ |
| 192 | + $product = $productRepository->get('product1'); |
| 193 | + |
| 194 | + /** @var StoreManagerInterface $storeManager */ |
| 195 | + $storeManager = $this->objectManager->get(StoreManagerInterface::class); |
| 196 | + $storeManager->setCurrentStore(1); |
| 197 | + |
| 198 | + $testStore = $storeManager->getStore('test'); |
| 199 | + |
| 200 | + $productFilter = [ |
| 201 | + UrlRewrite::ENTITY_TYPE => 'product', |
| 202 | + ]; |
| 203 | + |
| 204 | + $product->setData('save_rewrites_history', false); |
| 205 | + $product->setUrlKey('new-url'); |
| 206 | + $product->save(); |
| 207 | + |
| 208 | + $expected = [ |
| 209 | + [ |
| 210 | + 'request_path' => "new-url.html", |
| 211 | + 'target_path' => "catalog/product/view/id/" . $product->getId(), |
| 212 | + 'is_auto_generated' => 1, |
| 213 | + 'redirect_type' => 0, |
| 214 | + 'store_id' => 1, |
| 215 | + ], |
| 216 | + [ |
| 217 | + 'request_path' => "product-1.html", |
| 218 | + 'target_path' => "catalog/product/view/id/" . $product->getId(), |
| 219 | + 'is_auto_generated' => 1, |
| 220 | + 'redirect_type' => 0, |
| 221 | + 'store_id' => $testStore->getId(), |
| 222 | + ], |
| 223 | + ]; |
| 224 | + |
| 225 | + $actual = $this->getActualResults($productFilter); |
| 226 | + foreach ($expected as $row) { |
| 227 | + $this->assertContains($row, $actual); |
| 228 | + } |
| 229 | + } |
| 230 | +} |
0 commit comments