Skip to content

Commit 64d2fb9

Browse files
committed
MC-39600: Create automated test for: "Change category/product url rewrite suffix in configurations"
1 parent 661c15c commit 64d2fb9

File tree

1 file changed

+287
-0
lines changed
  • dev/tests/integration/testsuite/Magento/Catalog/Model/System/Config/Backend/Catalog/Url/Rewrite

1 file changed

+287
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,287 @@
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\Model\System\Config\Backend\Catalog\Url\Rewrite;
9+
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator;
12+
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
13+
use Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator;
14+
use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
15+
use Magento\Framework\App\Cache\Type\Block;
16+
use Magento\Framework\App\Cache\Type\Collection;
17+
use Magento\Framework\App\Cache\TypeListInterface;
18+
use Magento\Framework\App\Config\ScopeConfigInterface;
19+
use Magento\Framework\Exception\LocalizedException;
20+
use Magento\Framework\ObjectManagerInterface;
21+
use Magento\Store\Model\ScopeInterface;
22+
use Magento\Store\Model\StoreManagerInterface;
23+
use Magento\TestFramework\Helper\Bootstrap;
24+
use Magento\UrlRewrite\Model\Storage\DbStorage;
25+
use PHPUnit\Framework\TestCase;
26+
27+
/**
28+
* Class checks url suffix config save behaviour
29+
*
30+
* @see \Magento\Catalog\Model\System\Config\Backend\Catalog\Url\Rewrite\Suffix
31+
*
32+
* @magentoAppArea adminhtml
33+
* @magentoDbIsolation enabled
34+
* @magentoAppIsolation enabled
35+
*/
36+
class SuffixTest extends TestCase
37+
{
38+
/** @var ObjectManagerInterface */
39+
private $objectManager;
40+
41+
/** @var Suffix */
42+
private $model;
43+
44+
/** @var DbStorage */
45+
private $urlFinder;
46+
47+
/** @var StoreManagerInterface */
48+
private $storeManager;
49+
50+
/** @var TypeListInterface */
51+
private $typeList;
52+
53+
/** @var ScopeConfigInterface */
54+
private $scopeConfig;
55+
56+
/** @var ProductRepositoryInterface */
57+
private $productRepository;
58+
59+
/** @var int */
60+
private $defaultStoreId;
61+
62+
/**
63+
* @inheritdoc
64+
*/
65+
protected function setUp(): void
66+
{
67+
parent::setUp();
68+
69+
$this->objectManager = Bootstrap::getObjectManager();
70+
$this->model = $this->objectManager->get(Suffix::class);
71+
$this->urlFinder = $this->objectManager->get(DbStorage::class);
72+
$this->storeManager = $this->objectManager->get(StoreManagerInterface::class);
73+
$this->typeList = $this->objectManager->get(TypeListInterface::class);
74+
$this->scopeConfig = $this->objectManager->get(ScopeConfigInterface::class);
75+
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
76+
$this->productRepository->cleanCache();
77+
$this->defaultStoreId = (int)$this->storeManager->getStore('default')->getId();
78+
}
79+
80+
/**
81+
* @return void
82+
*/
83+
public function testSaveWithError(): void
84+
{
85+
$this->expectException(LocalizedException::class);
86+
$this->expectErrorMessage((string)__('Anchor symbol (#) is not supported in url rewrite suffix.'));
87+
$this->model->setValue('.html#');
88+
$this->model->beforeSave();
89+
}
90+
91+
/**
92+
* @dataProvider wrongValuesProvider
93+
*
94+
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
95+
*
96+
* @param array $data
97+
* @return void
98+
*/
99+
public function testSaveWithWrongData(array $data): void
100+
{
101+
$productId = (int)$this->productRepository->get('simple2')->getId();
102+
$this->model->addData($data);
103+
$this->model->afterSave();
104+
$this->assertRewrite(
105+
$this->scopeConfig->getValue(ProductUrlPathGenerator::XML_PATH_PRODUCT_URL_SUFFIX),
106+
[
107+
'entity_type' => ProductUrlRewriteGenerator::ENTITY_TYPE,
108+
'entity_id' => $productId,
109+
'store_id' => $this->defaultStoreId,
110+
]
111+
);
112+
}
113+
114+
/**
115+
* @return array
116+
*/
117+
public function wrongValuesProvider(): array
118+
{
119+
return [
120+
'with_wrong_path' => [
121+
['path' => 'wrong_path', 'value' => 'some_test_value'],
122+
],
123+
'with_null_value' => [
124+
['path' => ProductUrlPathGenerator::XML_PATH_PRODUCT_URL_SUFFIX, 'value' => null],
125+
],
126+
];
127+
}
128+
129+
/**
130+
* @magentoDbIsolation disabled
131+
*
132+
* @magentoDataFixture Magento/Catalog/_files/product_multistore_different_short_description.php
133+
*
134+
* @return void
135+
*/
136+
public function testSaveInStoreScope(): void
137+
{
138+
$productId = $this->productRepository->get('simple-different-short-description')->getId();
139+
$newSuffix = 'some_test_value_for_store';
140+
$storeId = $this->storeManager->getStore('fixturestore')->getId();
141+
$this->model->addData([
142+
'path' => ProductUrlPathGenerator::XML_PATH_PRODUCT_URL_SUFFIX,
143+
'value' => $newSuffix,
144+
'scope' => ScopeInterface::SCOPE_STORES,
145+
'scope_id' => $storeId,
146+
]);
147+
$this->model->afterSave();
148+
$this->assertRewrite(
149+
$this->scopeConfig->getValue(ProductUrlPathGenerator::XML_PATH_PRODUCT_URL_SUFFIX),
150+
[
151+
'entity_type' => ProductUrlRewriteGenerator::ENTITY_TYPE,
152+
'entity_id' => $productId,
153+
'store_id' => $this->defaultStoreId,
154+
]
155+
);
156+
$this->assertRewrite(
157+
$newSuffix,
158+
[
159+
'entity_type' => ProductUrlRewriteGenerator::ENTITY_TYPE,
160+
'entity_id' => $productId,
161+
'store_id' => $storeId,
162+
]
163+
);
164+
}
165+
166+
/**
167+
* @magentoDbIsolation disabled
168+
*
169+
* @magentoDataFixture Magento/Catalog/_files/product_two_websites.php
170+
*
171+
* @return void
172+
*/
173+
public function testSaveInWebsiteScope(): void
174+
{
175+
$productId = (int)$this->productRepository->get('simple-on-two-websites')->getId();
176+
$newSuffix = 'some_test_value_for_website';
177+
$website = $this->storeManager->getWebsite('test');
178+
$this->model->addData([
179+
'path' => ProductUrlPathGenerator::XML_PATH_PRODUCT_URL_SUFFIX,
180+
'value' => $newSuffix,
181+
'scope' => ScopeInterface::SCOPE_WEBSITES,
182+
'scope_id' => $website->getId(),
183+
]);
184+
$this->model->afterSave();
185+
$this->assertRewrite(
186+
$this->scopeConfig->getValue(ProductUrlPathGenerator::XML_PATH_PRODUCT_URL_SUFFIX),
187+
[
188+
'entity_type' => ProductUrlRewriteGenerator::ENTITY_TYPE,
189+
'entity_id' => $productId,
190+
'store_id' => $this->defaultStoreId,
191+
]
192+
);
193+
$this->assertRewrite(
194+
$newSuffix,
195+
[
196+
'entity_type' => ProductUrlRewriteGenerator::ENTITY_TYPE,
197+
'entity_id' => $productId,
198+
'store_id' => $website->getStoreIds(),
199+
]
200+
);
201+
}
202+
203+
/**
204+
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
205+
*
206+
* @magentoConfigFixture default_store catalog/seo/product_url_suffix .html_default
207+
*
208+
* @return void
209+
*/
210+
public function testSaveDefaultScopeWithOverrideStoreScope(): void
211+
{
212+
$productId = (int)$this->productRepository->get('simple2')->getId();
213+
$newSuffix = 'some_test_value';
214+
$this->model->addData([
215+
'path' => ProductUrlPathGenerator::XML_PATH_PRODUCT_URL_SUFFIX,
216+
'value' => $newSuffix,
217+
]);
218+
$this->model->afterSave();
219+
$this->assertRewrite(
220+
'.html_default',
221+
[
222+
'entity_type' => ProductUrlRewriteGenerator::ENTITY_TYPE,
223+
'entity_id' => $productId,
224+
'store_id' => $this->defaultStoreId,
225+
]
226+
);
227+
}
228+
229+
/**
230+
* @magentoDataFixture Magento/Catalog/_files/category.php
231+
*
232+
* @return void
233+
*/
234+
public function testSaveCategorySuffix(): void
235+
{
236+
$this->model->addData(['path' => CategoryUrlPathGenerator::XML_PATH_CATEGORY_URL_SUFFIX, 'value' => null]);
237+
$this->model->afterSave();
238+
$this->assertRewrite('.html', ['entity_type' => CategoryUrlRewriteGenerator::ENTITY_TYPE]);
239+
$this->checkIsCacheInvalidated();
240+
}
241+
242+
/**
243+
* @return void
244+
*/
245+
public function testDeleteCategorySuffix(): void
246+
{
247+
$this->model->addData(
248+
['path' => CategoryUrlPathGenerator::XML_PATH_CATEGORY_URL_SUFFIX, 'value' => 'test_value']
249+
);
250+
$this->model->afterDeleteCommit();
251+
$this->checkIsCacheInvalidated();
252+
}
253+
254+
/**
255+
* Check that provided cache types are invalidated
256+
*
257+
* @param array $cacheTypes
258+
* @return void
259+
*/
260+
private function checkIsCacheInvalidated(
261+
array $cacheTypes = [Block::TYPE_IDENTIFIER, Collection::TYPE_IDENTIFIER]
262+
): void {
263+
$types = $this->typeList->getTypes();
264+
265+
foreach ($cacheTypes as $type) {
266+
$this->assertNotNull($types[$type]);
267+
$this->assertEquals(0, $types[$type]->getStatus());
268+
}
269+
}
270+
271+
/**
272+
* Assert url rewrite rewrite
273+
*
274+
* @param string $expectedSuffix
275+
* @param array $data
276+
* @return void
277+
*/
278+
private function assertRewrite(string $expectedSuffix, array $data)
279+
{
280+
$rewrite = $this->urlFinder->findOneByData($data);
281+
$this->assertNotNull($rewrite);
282+
$this->assertTrue(
283+
substr($rewrite->getRequestPath(), -strlen($expectedSuffix)) === $expectedSuffix,
284+
'The url rewrite suffix does not match expected value'
285+
);
286+
}
287+
}

0 commit comments

Comments
 (0)