Skip to content

Commit 03e0a0b

Browse files
Adrien Louis-Rossignolrostyslav-hymon
authored andcommitted
Add integration tests for product urls rewrite generation
1 parent 8e17718 commit 03e0a0b

File tree

3 files changed

+274
-0
lines changed

3 files changed

+274
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
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+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
use Magento\Catalog\Api\ProductRepositoryInterface;
4+
use Magento\Catalog\Setup\CategorySetup;
5+
use Magento\Store\Model\StoreManagerInterface;
6+
use Magento\TestFramework\Helper\Bootstrap;
7+
8+
\Magento\TestFramework\Helper\Bootstrap::getInstance()->loadArea(\Magento\Backend\App\Area\FrontNameResolver::AREA_CODE);
9+
10+
require __DIR__ . '/../../Store/_files/store.php';
11+
12+
/** @var $installer CategorySetup */
13+
$objectManager = Bootstrap::getObjectManager();
14+
$installer = $objectManager->create(CategorySetup::class);
15+
$storeManager = $objectManager->get(StoreManagerInterface::class);
16+
$storeManager->setCurrentStore(0);
17+
18+
/** @var $product \Magento\Catalog\Model\Product */
19+
$product = $objectManager->create(\Magento\Catalog\Model\Product::class);
20+
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
21+
->setAttributeSetId($installer->getAttributeSetId('catalog_product', 'Default'))
22+
->setStoreId(0)
23+
->setWebsiteIds([1])
24+
->setName('Product1')
25+
->setSku('product1')
26+
->setPrice(10)
27+
->setWeight(18)
28+
->setStockData(['use_config_manage_stock' => 0])
29+
->setUrlKey('product-1')
30+
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
31+
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
32+
33+
/** @var ProductRepositoryInterface $productRepository */
34+
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
35+
$productRepository->save($product);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
use Magento\Catalog\Api\ProductRepositoryInterface;
4+
use Magento\TestFramework\Helper\Bootstrap;
5+
6+
$objectManager = Bootstrap::getObjectManager();
7+
8+
require __DIR__ . '/../../Store/_files/store_rollback.php';
9+
require __DIR__ . '/../../Store/_files/second_store_rollback.php';

0 commit comments

Comments
 (0)