Skip to content

Commit 3acbf4a

Browse files
authored
CIF-2866 - Alternative category page takes over the whole navigation (#900)
* fix navigation rendering with multiple catalog pages
1 parent 1706fa6 commit 3acbf4a

File tree

2 files changed

+56
-5
lines changed
  • bundles/core/src

2 files changed

+56
-5
lines changed

bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/models/v1/navigation/NavigationImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ private boolean isCatalogRoot(Page page) {
180180
}
181181

182182
private void expandCatalogRoot(Page catalogPage, List<NavigationItem> pages) {
183-
Page categoryPage = SiteNavigation.getCategoryPage(currentPage);
183+
Page categoryPage = SiteNavigation.getCategoryPage(catalogPage);
184184
if (categoryPage == null) {
185185
return;
186186
}

bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/models/v1/navigation/NavigationImplTest.java

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@
5959

6060
public class NavigationImplTest {
6161
private static final String CATALOG_PAGE_PATH = "catalog_page_path";
62+
private static final String CATALOG_PAGE_PATH_2 = "catalog_page_path_2";
6263
private static final String CATEGORY_PAGE_PATH = "category_page_path";
64+
private static final String CATEGORY_PAGE_PATH_2 = "category_page_path_2";
6365
NavigationImpl navigation;
6466
com.adobe.cq.wcm.core.components.internal.models.v1.NavigationImpl wcmNavigation;
6567
GraphQLCategoryProvider categoryProvider;
@@ -81,20 +83,25 @@ public void init() {
8183
when(currentPage.getPath()).thenReturn("/content/currentPage");
8284
Resource currentPageContent = mock(Resource.class);
8385
Resource contentResource = mock(Resource.class);
84-
Map<String, Object> currentPageProperties = new HashMap<>();
85-
currentPageProperties.put("cq:cifCategoryPage", CATEGORY_PAGE_PATH);
8686
Page categoryPage = mock(Page.class);
8787
when(categoryPage.getPath()).thenReturn(CATEGORY_PAGE_PATH);
8888
when(currentPageContent.getPath()).thenReturn(CATEGORY_PAGE_PATH + "/jcr:content");
8989
when(pageManager.getPage(CATEGORY_PAGE_PATH)).thenReturn(categoryPage);
90-
when(currentPageContent.getValueMap()).thenReturn(new ValueMapDecorator(currentPageProperties));
9190
when(currentPage.getContentResource()).thenReturn(currentPageContent);
9291
Resource categoryPageResource = new SyntheticResource(null, CATEGORY_PAGE_PATH, null);
9392
when(categoryPage.adaptTo(Resource.class)).thenReturn(categoryPageResource);
9493
when(categoryPage.getContentResource()).thenReturn(categoryPageResource);
9594
when(contentResource.adaptTo(ComponentsConfiguration.class)).thenReturn(null);
9695
Whitebox.setInternalState(navigation, "currentPage", currentPage);
9796

97+
// category page 2
98+
categoryPage = mock(Page.class);
99+
when(categoryPage.getPath()).thenReturn(CATEGORY_PAGE_PATH_2);
100+
when(pageManager.getPage(CATEGORY_PAGE_PATH_2)).thenReturn(categoryPage);
101+
categoryPageResource = new SyntheticResource(null, CATEGORY_PAGE_PATH_2, null);
102+
when(categoryPage.adaptTo(Resource.class)).thenReturn(categoryPageResource);
103+
when(categoryPage.getContentResource()).thenReturn(categoryPageResource);
104+
98105
// WCM navigation model
99106
wcmNavigation = mock(com.adobe.cq.wcm.core.components.internal.models.v1.NavigationImpl.class);
100107
Whitebox.setInternalState(navigation, "wcmNavigation", wcmNavigation);
@@ -522,6 +529,43 @@ public void testNavigationCategoryBeforePage() {
522529
testNavigationItemOrdering(false);
523530
}
524531

532+
@Test
533+
public void testNavigationCategoriesForTwoCatalogPages() {
534+
535+
String categoryId = "uid-0";
536+
String categoryUrlPath = "category-1";
537+
String categoryName = "Category 1";
538+
539+
initCatalogPage(true, true, false);
540+
initCatalogPage(true, true, false, CATALOG_PAGE_PATH_2, CATEGORY_PAGE_PATH_2);
541+
542+
NavigationItem item = mock(NavigationItem.class);
543+
when(item.getPath()).thenReturn(CATALOG_PAGE_PATH);
544+
navigationItems.add(item);
545+
546+
NavigationItem item2 = mock(NavigationItem.class);
547+
when(item2.getPath()).thenReturn(CATALOG_PAGE_PATH_2);
548+
navigationItems.add(item2);
549+
550+
CategoryTree category = mock(CategoryTree.class);
551+
when(category.getUid()).thenReturn(new ID(categoryId));
552+
when(category.getUrlPath()).thenReturn(categoryUrlPath);
553+
when(category.getName()).thenReturn(categoryName);
554+
categoryList.add(category);
555+
556+
List<com.adobe.cq.commerce.core.components.models.navigation.NavigationItem> items = navigation.getItems();
557+
Assert.assertEquals(2, items.size());
558+
559+
// the two items are rendered with two different category pages
560+
com.adobe.cq.commerce.core.components.models.navigation.NavigationItem navigationItem = items.get(0);
561+
Assert.assertEquals(categoryName, navigationItem.getTitle());
562+
Assert.assertEquals(CATEGORY_PAGE_PATH + ".html/" + categoryUrlPath + ".html", navigationItem.getURL());
563+
564+
navigationItem = items.get(1);
565+
Assert.assertEquals(categoryName, navigationItem.getTitle());
566+
Assert.assertEquals(CATEGORY_PAGE_PATH_2 + ".html/" + categoryUrlPath + ".html", navigationItem.getURL());
567+
}
568+
525569
private void testNavigationItemOrdering(boolean pageBeforeCategory) {
526570
// checks that the navigation items are ordered according to the underlying page nodes
527571

@@ -567,11 +611,18 @@ private void testNavigationItemOrdering(boolean pageBeforeCategory) {
567611
}
568612

569613
private void initCatalogPage(boolean catalogRoot, boolean showMainCategories, boolean useCaConfig) {
614+
initCatalogPage(catalogRoot, showMainCategories, useCaConfig, CATALOG_PAGE_PATH, CATEGORY_PAGE_PATH);
615+
}
616+
617+
private void initCatalogPage(boolean catalogRoot, boolean showMainCategories, boolean useCaConfig, String catalogPagePath,
618+
String categoryPagePath) {
570619
Page catalogPage = mock(Page.class);
571620
Resource catalogPageContent = mock(Resource.class);
572621
when(catalogPageContent.isResourceType(RT_CATALOG_PAGE)).thenReturn(catalogRoot);
622+
when(catalogPage.getPageManager()).thenReturn(pageManager);
573623
Map<String, Object> catalogPageProperties = new HashMap<>();
574624
catalogPageProperties.put(PN_SHOW_MAIN_CATEGORIES, showMainCategories);
625+
catalogPageProperties.put("cq:cifCategoryPage", categoryPagePath);
575626

576627
if (!useCaConfig) {
577628
catalogPageProperties.put(PN_MAGENTO_ROOT_CATEGORY_IDENTIFIER, 4);
@@ -585,7 +636,7 @@ private void initCatalogPage(boolean catalogRoot, boolean showMainCategories, bo
585636
ResourceResolver mockResourceResolver = mock(ResourceResolver.class);
586637
when(mockResourceResolver.getResource(any(String.class))).thenReturn(null);
587638
when(catalogPageContent.getResourceResolver()).thenReturn(mockResourceResolver);
588-
when(pageManager.getPage(CATALOG_PAGE_PATH)).thenReturn(catalogPage);
639+
when(pageManager.getPage(catalogPagePath)).thenReturn(catalogPage);
589640

590641
ValueMap configProperties = new ValueMapDecorator(ImmutableMap.of(PN_MAGENTO_ROOT_CATEGORY_IDENTIFIER, 4));
591642

0 commit comments

Comments
 (0)