|
| 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\Catalog\Controller\Product; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\CategoryRepositoryInterface; |
| 11 | +use Magento\Catalog\Api\Data\ProductInterface; |
| 12 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 13 | +use Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator; |
| 14 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 15 | +use Magento\Framework\Registry; |
| 16 | +use Magento\Store\Model\ScopeInterface; |
| 17 | +use Magento\Store\Model\StoreManagerInterface; |
| 18 | +use Magento\TestFramework\Request; |
| 19 | +use Magento\TestFramework\Response; |
| 20 | +use Magento\TestFramework\TestCase\AbstractController; |
| 21 | + |
| 22 | +/** |
| 23 | + * Checks product availability on storefront by url rewrite |
| 24 | + * |
| 25 | + * @magentoConfigFixture default/catalog/seo/generate_category_product_rewrites 1 |
| 26 | + * @magentoDbIsolation enabled |
| 27 | + */ |
| 28 | +class ProductUrlRewriteTest extends AbstractController |
| 29 | +{ |
| 30 | + /** @var ScopeConfigInterface */ |
| 31 | + private $config; |
| 32 | + |
| 33 | + /** @var ProductRepositoryInterface */ |
| 34 | + private $productRepository; |
| 35 | + |
| 36 | + /** @var Registry */ |
| 37 | + private $registry; |
| 38 | + |
| 39 | + /** @var CategoryRepositoryInterface */ |
| 40 | + private $categoryRepository; |
| 41 | + |
| 42 | + /** @var StoreManagerInterface */ |
| 43 | + private $storeManager; |
| 44 | + |
| 45 | + /** @var string */ |
| 46 | + private $urlSuffix; |
| 47 | + |
| 48 | + /** |
| 49 | + * @inheritdoc |
| 50 | + */ |
| 51 | + protected function setUp() |
| 52 | + { |
| 53 | + parent::setUp(); |
| 54 | + |
| 55 | + $this->config = $this->_objectManager->get(ScopeConfigInterface::class); |
| 56 | + $this->productRepository = $this->_objectManager->create(ProductRepositoryInterface::class); |
| 57 | + $this->registry = $this->_objectManager->get(Registry::class); |
| 58 | + $this->categoryRepository = $this->_objectManager->create(CategoryRepositoryInterface::class); |
| 59 | + $this->storeManager = $this->_objectManager->get(StoreManagerInterface::class); |
| 60 | + $this->urlSuffix = $this->config->getValue( |
| 61 | + ProductUrlPathGenerator::XML_PATH_PRODUCT_URL_SUFFIX, |
| 62 | + ScopeInterface::SCOPE_STORE |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @magentoDataFixture Magento/Catalog/_files/second_product_simple.php |
| 68 | + * @return void |
| 69 | + */ |
| 70 | + public function testProductUrlRewrite(): void |
| 71 | + { |
| 72 | + $product = $this->productRepository->get('simple2'); |
| 73 | + $url = $this->prepareUrl($product->getUrlKey()); |
| 74 | + $this->dispatch($url); |
| 75 | + |
| 76 | + $this->assertProductIsVisible($product); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @magentoDataFixture Magento/Catalog/_files/category_product.php |
| 81 | + * @return void |
| 82 | + */ |
| 83 | + public function testCategoryProductUrlRewrite(): void |
| 84 | + { |
| 85 | + $category = $this->categoryRepository->get(333); |
| 86 | + $product = $this->productRepository->get('simple333'); |
| 87 | + $url = $this->prepareUrl($category->getUrlKey(), false) . $this->prepareUrl($product->getUrlKey()); |
| 88 | + $this->dispatch($url); |
| 89 | + |
| 90 | + $this->assertProductIsVisible($product); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @magentoDataFixture Magento/Catalog/_files/second_product_simple.php |
| 95 | + * @return void |
| 96 | + */ |
| 97 | + public function testProductRedirect(): void |
| 98 | + { |
| 99 | + $product = $this->productRepository->get('simple2'); |
| 100 | + $oldUrl = $this->prepareUrl($product->getUrlKey()); |
| 101 | + $data = [ |
| 102 | + 'url_key' => 'new-url-key', |
| 103 | + 'url_key_create_redirect' => $product->getUrlKey(), |
| 104 | + 'save_rewrites_history' => true, |
| 105 | + ]; |
| 106 | + $this->updateProduct($product, $data); |
| 107 | + $this->dispatch($oldUrl); |
| 108 | + |
| 109 | + $this->assertRedirect($this->stringContains($this->prepareUrl('new-url-key'))); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @magentoDbIsolation disabled |
| 114 | + * @magentoDataFixture Magento/Store/_files/core_fixturestore.php |
| 115 | + * @magentoDataFixture Magento/Catalog/_files/second_product_simple.php |
| 116 | + * @return void |
| 117 | + */ |
| 118 | + public function testMultistoreProductUrlRewrite(): void |
| 119 | + { |
| 120 | + $currentStore = $this->storeManager->getStore(); |
| 121 | + $product = $this->productRepository->get('simple2'); |
| 122 | + $firstStoreUrl = $this->prepareUrl($product->getUrlKey()); |
| 123 | + $secondStoreId = $this->storeManager->getStore('fixturestore')->getId(); |
| 124 | + $this->storeManager->setCurrentStore($secondStoreId); |
| 125 | + |
| 126 | + try { |
| 127 | + $product = $this->updateProduct($product, ['url_key' => 'second-store-url-key']); |
| 128 | + $this->assertEquals('second-store-url-key', $product->getUrlKey()); |
| 129 | + $secondStoreUrl = $this->prepareUrl($product->getUrlKey()); |
| 130 | + |
| 131 | + $this->dispatch($secondStoreUrl); |
| 132 | + $this->assertProductIsVisible($product); |
| 133 | + $this->cleanUpCachedObjects(); |
| 134 | + } finally { |
| 135 | + $this->storeManager->setCurrentStore($currentStore); |
| 136 | + } |
| 137 | + |
| 138 | + $this->dispatch($firstStoreUrl); |
| 139 | + $this->assertProductIsVisible($product); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Update product |
| 144 | + * |
| 145 | + * @param ProductInterface $product |
| 146 | + * @param array $data |
| 147 | + * @return ProductInterface |
| 148 | + */ |
| 149 | + private function updateProduct(ProductInterface $product, array $data): ProductInterface |
| 150 | + { |
| 151 | + $product->addData($data); |
| 152 | + |
| 153 | + return $this->productRepository->save($product); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Clean up cached objects |
| 158 | + * |
| 159 | + * @return void |
| 160 | + */ |
| 161 | + private function cleanUpCachedObjects(): void |
| 162 | + { |
| 163 | + $this->registry->unregister('current_product'); |
| 164 | + $this->registry->unregister('product'); |
| 165 | + $this->_objectManager->removeSharedInstance(Request::class); |
| 166 | + $this->_objectManager->removeSharedInstance(Response::class); |
| 167 | + $this->_response = null; |
| 168 | + $this->_request = null; |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Prepare url to dispatch |
| 173 | + * |
| 174 | + * @param string $urlKey |
| 175 | + * @param bool $addSuffix |
| 176 | + * @return string |
| 177 | + */ |
| 178 | + private function prepareUrl(string $urlKey, bool $addSuffix = true): string |
| 179 | + { |
| 180 | + $url = $addSuffix ? '/' . $urlKey . $this->urlSuffix : '/' . $urlKey; |
| 181 | + |
| 182 | + return $url; |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Assert that product is available in storefront |
| 187 | + * |
| 188 | + * @param ProductInterface $product |
| 189 | + * @return void |
| 190 | + */ |
| 191 | + private function assertProductIsVisible(ProductInterface $product): void |
| 192 | + { |
| 193 | + $this->assertEquals( |
| 194 | + Response::STATUS_CODE_200, |
| 195 | + $this->getResponse()->getHttpResponseCode(), |
| 196 | + 'Wrong response code is returned' |
| 197 | + ); |
| 198 | + $currentProduct = $this->registry->registry('current_product'); |
| 199 | + $this->assertNotNull($currentProduct); |
| 200 | + $this->assertEquals( |
| 201 | + $product->getSku(), |
| 202 | + $currentProduct->getSku(), |
| 203 | + 'Wrong product is registered' |
| 204 | + ); |
| 205 | + } |
| 206 | +} |
0 commit comments