88namespace Magento \Catalog \Controller ;
99
1010use Magento \Catalog \Api \CategoryRepositoryInterface ;
11+ use Magento \Catalog \Api \Data \CategoryInterface ;
1112use Magento \Catalog \Model \Category ;
1213use Magento \Catalog \Model \Category \Attribute \LayoutUpdateManager ;
1314use Magento \Catalog \Model \Product \ProductList \Toolbar as ToolbarModel ;
15+ use Magento \Catalog \Model \ResourceModel \Category \Collection ;
16+ use Magento \Catalog \Model \ResourceModel \Category \CollectionFactory ;
1417use Magento \Catalog \Model \Session ;
1518use Magento \Framework \App \Http \Context ;
1619use Magento \Framework \ObjectManagerInterface ;
1720use Magento \Framework \Registry ;
1821use Magento \Framework \View \LayoutInterface ;
22+ use Magento \Store \Model \Store ;
1923use Magento \TestFramework \Catalog \Model \CategoryLayoutUpdateManager ;
2024use Magento \TestFramework \Helper \Bootstrap ;
2125use Magento \TestFramework \TestCase \AbstractController ;
@@ -53,6 +57,11 @@ class CategoryTest extends AbstractController
5357 */
5458 private $ httpContext ;
5559
60+ /**
61+ * @var CollectionFactory
62+ */
63+ private $ categoryCollectionFactory ;
64+
5665 /**
5766 * @inheritdoc
5867 */
@@ -64,6 +73,8 @@ protected function setUp(): void
6473 $ this ->objectManager ->configure ([
6574 'preferences ' => [LayoutUpdateManager::class => CategoryLayoutUpdateManager::class]
6675 ]);
76+
77+ $ this ->categoryCollectionFactory = $ this ->objectManager ->create (CollectionFactory::class);
6778 $ this ->registry = $ this ->objectManager ->get (Registry::class);
6879 $ this ->layout = $ this ->objectManager ->get (LayoutInterface::class);
6980 $ this ->session = $ this ->objectManager ->get (Session::class);
@@ -233,4 +244,50 @@ public function testViewWithRememberPaginationAndPreviousValue(): void
233244 $ this ->assertEquals ($ newPaginationValue , $ this ->session ->getData (ToolbarModel::LIMIT_PARAM_NAME ));
234245 $ this ->assertEquals ($ newPaginationValue , $ this ->httpContext ->getValue (ToolbarModel::LIMIT_PARAM_NAME ));
235246 }
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+ }
236293}
0 commit comments