9
9
10
10
use Magento \Catalog \Api \CategoryListInterface ;
11
11
use Magento \Catalog \Api \Data \CategoryInterface ;
12
- use Magento \Catalog \Api \Data \ProductInterface ;
13
12
use Magento \Catalog \Api \ProductRepositoryInterface ;
14
13
use Magento \Framework \Api \SearchCriteriaBuilder ;
15
14
use Magento \TestFramework \Helper \Bootstrap ;
19
18
20
19
/**
21
20
* @magentoAppArea adminhtml
21
+ * @magentoDbIsolation disabled
22
22
*/
23
23
class UrlRewriteHandlerTest extends TestCase
24
24
{
@@ -27,6 +27,11 @@ class UrlRewriteHandlerTest extends TestCase
27
27
*/
28
28
private $ handler ;
29
29
30
+ /**
31
+ * @var ProductRepositoryInterface
32
+ */
33
+ private $ productRepository ;
34
+
30
35
/**
31
36
* @var ObjectManager
32
37
*/
@@ -38,21 +43,21 @@ class UrlRewriteHandlerTest extends TestCase
38
43
protected function setUp ()
39
44
{
40
45
$ this ->objectManager = Bootstrap::getObjectManager ();
41
- $ this ->handler = $ this ->objectManager ->get (UrlRewriteHandler::class);
46
+ $ this ->productRepository = $ this ->objectManager ->get (ProductRepositoryInterface::class);
47
+ $ this ->handler = $ this ->objectManager ->create (UrlRewriteHandler::class);
42
48
}
43
49
44
50
/**
45
51
* Checks category URLs rewrites generation with enabled `Use Categories Path for Product URLs` option and
46
52
* store's specific product URL key.
47
53
*
48
- * @magentoDbIsolation disabled
49
54
* @magentoDataFixture Magento/CatalogUrlRewrite/Fixtures/product_custom_url_key.php
50
55
* @magentoConfigFixture admin_store catalog/seo/product_use_categories 1
51
56
* @magentoConfigFixture default/catalog/seo/generate_category_product_rewrites 1
52
57
*/
53
58
public function testGenerateProductUrlRewrites ()
54
59
{
55
- $ product = $ this ->getProduct ('p002 ' );
60
+ $ product = $ this ->productRepository -> get ('p002 ' );
56
61
$ category = $ this ->getCategory ('category 1 ' );
57
62
// change the category scope to the global
58
63
$ category ->setStoreId (0 )
@@ -81,6 +86,31 @@ function (UrlRewrite $urlRewrite) {
81
86
self ::assertEquals ($ expected , $ actual , 'Generated URLs rewrites do not match. ' );
82
87
}
83
88
89
+ /**
90
+ * @magentoDataFixture Magento/CatalogUrlRewrite/_files/category_with_products.php
91
+ */
92
+ public function testGenerateProductUrlRewrites2 ()
93
+ {
94
+ $ product1 = $ this ->productRepository ->get ('simple ' );
95
+ $ product2 = $ this ->productRepository ->get ('12345 ' );
96
+ $ category = $ this ->getCategory ('Category 1 ' );
97
+
98
+ $ category ->setChangedProductIds ([$ product1 ->getId ()]);
99
+ $ category ->setAffectedProductIds ([$ product1 ->getId (), $ product2 ->getId ()]);
100
+ $ category ->setAnchorsAbove (false );
101
+ $ generatedUrls = $ this ->handler ->generateProductUrlRewrites ($ category );
102
+ $ actual = array_values (array_map (function (UrlRewrite $ urlRewrite ) {
103
+ return $ urlRewrite ->getRequestPath ();
104
+ }, $ generatedUrls ));
105
+
106
+ $ expected = [
107
+ 'simple-product.html ' ,
108
+ 'category-1/simple-product.html ' ,
109
+ '/simple-product.html ' ,
110
+ ];
111
+ $ this ->assertEquals ($ expected , $ actual , 'Generated URLs rewrites do not match. ' );
112
+ }
113
+
84
114
/**
85
115
* Gets category by name.
86
116
*
@@ -100,18 +130,4 @@ private function getCategory(string $name): CategoryInterface
100
130
101
131
return array_pop ($ items );
102
132
}
103
-
104
- /**
105
- * Gets product by SKU.
106
- *
107
- * @param string $sku
108
- * @return ProductInterface
109
- * @throws \Magento\Framework\Exception\NoSuchEntityException
110
- */
111
- private function getProduct (string $ sku ): ProductInterface
112
- {
113
- /** @var ProductRepositoryInterface $productRepository */
114
- $ productRepository = $ this ->objectManager ->get (ProductRepositoryInterface::class);
115
- return $ productRepository ->get ($ sku );
116
- }
117
133
}
0 commit comments