8
8
namespace Magento \Catalog \Controller ;
9
9
10
10
use Magento \Catalog \Api \CategoryRepositoryInterface ;
11
+ use Magento \Catalog \Api \Data \CategoryInterface ;
11
12
use Magento \Catalog \Model \Category ;
12
13
use Magento \Catalog \Model \Category \Attribute \LayoutUpdateManager ;
13
14
use Magento \Catalog \Model \Product \ProductList \Toolbar as ToolbarModel ;
15
+ use Magento \Catalog \Model \ResourceModel \Category \Collection ;
16
+ use Magento \Catalog \Model \ResourceModel \Category \CollectionFactory ;
14
17
use Magento \Catalog \Model \Session ;
15
18
use Magento \Framework \App \Http \Context ;
16
19
use Magento \Framework \ObjectManagerInterface ;
17
20
use Magento \Framework \Registry ;
18
21
use Magento \Framework \View \LayoutInterface ;
22
+ use Magento \Store \Model \Store ;
19
23
use Magento \TestFramework \Catalog \Model \CategoryLayoutUpdateManager ;
20
24
use Magento \TestFramework \Helper \Bootstrap ;
21
25
use Magento \TestFramework \TestCase \AbstractController ;
@@ -53,6 +57,11 @@ class CategoryTest extends AbstractController
53
57
*/
54
58
private $ httpContext ;
55
59
60
+ /**
61
+ * @var CollectionFactory
62
+ */
63
+ private $ categoryCollectionFactory ;
64
+
56
65
/**
57
66
* @inheritdoc
58
67
*/
@@ -64,6 +73,8 @@ protected function setUp(): void
64
73
$ this ->objectManager ->configure ([
65
74
'preferences ' => [LayoutUpdateManager::class => CategoryLayoutUpdateManager::class]
66
75
]);
76
+
77
+ $ this ->categoryCollectionFactory = $ this ->objectManager ->create (CollectionFactory::class);
67
78
$ this ->registry = $ this ->objectManager ->get (Registry::class);
68
79
$ this ->layout = $ this ->objectManager ->get (LayoutInterface::class);
69
80
$ this ->session = $ this ->objectManager ->get (Session::class);
@@ -233,4 +244,50 @@ public function testViewWithRememberPaginationAndPreviousValue(): void
233
244
$ this ->assertEquals ($ newPaginationValue , $ this ->session ->getData (ToolbarModel::LIMIT_PARAM_NAME ));
234
245
$ this ->assertEquals ($ newPaginationValue , $ this ->httpContext ->getValue (ToolbarModel::LIMIT_PARAM_NAME ));
235
246
}
247
+
248
+ /**
249
+ * Test to generate category page without duplicate html element ids
250
+ *
251
+ * @magentoDataFixture Magento/Catalog/_files/category_with_three_products.php
252
+ * @magentoDataFixture Magento/Catalog/_files/catalog_category_product_reindex_all.php
253
+ * @magentoDataFixture Magento/Catalog/_files/catalog_product_category_reindex_all.php
254
+ * @magentoDbIsolation disabled
255
+ */
256
+ public function testViewWithoutDuplicateHmlElementIds (): void
257
+ {
258
+ $ category = $ this ->loadCategory ('Category 999 ' , Store::DEFAULT_STORE_ID );
259
+ $ this ->dispatch ('catalog/category/view/id/ ' . $ category ->getId ());
260
+
261
+ $ responseHtml = $ this ->getResponse ()->getBody ();
262
+ $ htmlElementIds = ['modes-label ' , 'mode-list ' , 'toolbar-amount ' , 'sorter ' , 'limiter ' ];
263
+ foreach ($ htmlElementIds as $ elementId ) {
264
+ $ matches = [];
265
+ $ idAttribute = "id= \"$ elementId \"" ;
266
+ preg_match_all ("/ $ idAttribute/mx " , $ responseHtml , $ matches );
267
+ $ this ->assertCount (1 , $ matches [0 ]);
268
+ $ this ->assertEquals ($ idAttribute , $ matches [0 ][0 ]);
269
+ }
270
+ }
271
+
272
+ /**
273
+ * Loads category by id
274
+ *
275
+ * @param string $categoryName
276
+ * @param int $storeId
277
+ * @return CategoryInterface
278
+ */
279
+ private function loadCategory (string $ categoryName , int $ storeId ): CategoryInterface
280
+ {
281
+ /** @var Collection $categoryCollection */
282
+ $ categoryCollection = $ this ->categoryCollectionFactory ->create ();
283
+ /** @var CategoryInterface $category */
284
+ $ category = $ categoryCollection ->setStoreId ($ storeId )
285
+ ->addAttributeToSelect ('display_mode ' , 'left ' )
286
+ ->addAttributeToFilter (CategoryInterface::KEY_NAME , $ categoryName )
287
+ ->setPageSize (1 )
288
+ ->getFirstItem ();
289
+ $ category ->setStoreId ($ storeId );
290
+
291
+ return $ category ;
292
+ }
236
293
}
0 commit comments