diff --git a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/servlets/PageTypeRenderConditionServlet.java b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/servlets/PageTypeRenderConditionServlet.java index 9e30f9a74c..a6d8eb4cdf 100644 --- a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/servlets/PageTypeRenderConditionServlet.java +++ b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/servlets/PageTypeRenderConditionServlet.java @@ -14,6 +14,8 @@ package com.adobe.cq.commerce.core.components.internal.servlets; +import java.util.Arrays; + import javax.annotation.Nonnull; import javax.servlet.Servlet; @@ -32,8 +34,9 @@ import com.day.cq.wcm.api.PageManager; /** - * {@code PageTypeRenderConditionServlet} implements a {@code granite:rendercondition} used to decide if the product picker - * and category picker should be displayed in the page properties dialog of custom product pages and custom category pages. + * {@code PageTypeRenderConditionServlet} implements a {@code granite:rendercondition} used which evaluates to true for catalog pages, + * custom category pages and custom product pages. + * It requires the {@code pageType} parameter which should have one of the values: {@code catalog}, {@code category}, {@code product}. */ @Component( service = { Servlet.class }, @@ -49,8 +52,10 @@ public class PageTypeRenderConditionServlet extends SlingSafeMethodsServlet { private static final Logger LOGGER = LoggerFactory.getLogger(PageTypeRenderConditionServlet.class); private static final String PAGE_PROPERTIES = "wcm/core/content/sites/properties"; private static final String PAGE_TYPE_PROPERTY = "pageType"; - private static final String PAGE_TYPE_PRODUCT = "product"; - private static final String PAGE_TYPE_CATEGORY = "category"; + private static final String PRODUCT_PAGE_TYPE = "product"; + private static final String CATEGORY_PAGE_TYPE = "category"; + private static final String CATALOG_PAGE_TYPE = "catalog"; + private static final String CATALOG_PAGE_RESOURCE_TYPE = "core/cif/components/structure/catalogpage/v1/catalogpage"; @Override protected void doGet(@Nonnull SlingHttpServletRequest request, @Nonnull SlingHttpServletResponse response) { @@ -64,6 +69,11 @@ private boolean checkPageType(SlingHttpServletRequest slingRequest, String pageT return false; } + if (!Arrays.asList(new String[] { CATALOG_PAGE_TYPE, CATEGORY_PAGE_TYPE, PRODUCT_PAGE_TYPE }).contains(pageType)) { + LOGGER.error("{} property has invalid value at {}: {}", PAGE_TYPE_PROPERTY, slingRequest.getResource().getPath(), pageType); + return false; + } + // the caller is a page properties dialog if (!StringUtils.contains(slingRequest.getPathInfo(), PAGE_PROPERTIES)) { return false; @@ -77,16 +87,20 @@ private boolean checkPageType(SlingHttpServletRequest slingRequest, String pageT return false; } + if (CATALOG_PAGE_TYPE.equals(pageType)) { + return page.hasContent() && page.getContentResource().isResourceType(CATALOG_PAGE_RESOURCE_TYPE); + } + Page parentPage = page.getParent(); if (parentPage == null) { return false; } // perform the appropriate checks according to the pageType property - if (PAGE_TYPE_PRODUCT.equals(pageType)) { + if (PRODUCT_PAGE_TYPE.equals(pageType)) { Page productPage = SiteNavigation.getProductPage(page); return productPage != null && productPage.getPath().equals(parentPage.getPath()); - } else if (PAGE_TYPE_CATEGORY.equals(pageType)) { + } else if (CATEGORY_PAGE_TYPE.equals(pageType)) { Page categoryPage = SiteNavigation.getCategoryPage(page); return categoryPage != null && categoryPage.getPath().equals(parentPage.getPath()); } diff --git a/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/servlets/PageTypeRenderConditionServletTest.java b/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/servlets/PageTypeRenderConditionServletTest.java index 4582c7be2a..7ff55acb7c 100644 --- a/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/servlets/PageTypeRenderConditionServletTest.java +++ b/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/servlets/PageTypeRenderConditionServletTest.java @@ -81,6 +81,30 @@ public void testCategoryPage() { Assert.assertTrue(condition.check()); } + @Test + public void testCatalogPage() { + request.getResource().getValueMap().put("pageType", "catalog"); + request.setQueryString("item=/content/catalog-page"); + request.setPathInfo("/mnt/overlay/wcm/core/content/sites/properties.html"); + + servlet.doGet(request, null); + + SimpleRenderCondition condition = (SimpleRenderCondition) request.getAttribute(RenderCondition.class.getName()); + Assert.assertTrue(condition.check()); + } + + @Test + public void testNotCatalogPage() { + request.getResource().getValueMap().put("pageType", "catalog"); + request.setQueryString("item=/content/category-page/sub-page"); + request.setPathInfo("/mnt/overlay/wcm/core/content/sites/properties.html"); + + servlet.doGet(request, null); + + SimpleRenderCondition condition = (SimpleRenderCondition) request.getAttribute(RenderCondition.class.getName()); + Assert.assertFalse(condition.check()); + } + @Test public void testNotProductPage() { request.getResource().getValueMap().put("pageType", "product"); @@ -127,4 +151,16 @@ public void testNoPageType() { SimpleRenderCondition condition = (SimpleRenderCondition) request.getAttribute(RenderCondition.class.getName()); Assert.assertFalse(condition.check()); } + + @Test + public void testInvalidPageType() { + request.getResource().getValueMap().put("pageType", "something"); + request.setQueryString("item=/content/product-page/sub-page"); + request.setPathInfo("/mnt/overlay/wcm/core/content/sites/properties.html"); + + servlet.doGet(request, null); + + SimpleRenderCondition condition = (SimpleRenderCondition) request.getAttribute(RenderCondition.class.getName()); + Assert.assertFalse(condition.check()); + } } diff --git a/bundles/core/src/test/resources/context/jcr-page-filter.json b/bundles/core/src/test/resources/context/jcr-page-filter.json index 5bcdaaa264..bd3b4ea1e6 100644 --- a/bundles/core/src/test/resources/context/jcr-page-filter.json +++ b/bundles/core/src/test/resources/context/jcr-page-filter.json @@ -33,5 +33,13 @@ "cq:template": "/conf/venia/settings/wcm/templates/anything-else" } } + }, + + "catalog-page": { + "jcr:primaryType": "cq:Page", + "jcr:content": { + "jcr:primaryType":"cq:PageContent", + "sling:resourceType": "core/cif/components/structure/catalogpage/v1/catalogpage" + } } } \ No newline at end of file diff --git a/it/http/src/test/java/com/adobe/cq/commerce/it/http/CatalogPagePropertiesIT.java b/it/http/src/test/java/com/adobe/cq/commerce/it/http/CatalogPagePropertiesIT.java new file mode 100644 index 0000000000..8f4f169420 --- /dev/null +++ b/it/http/src/test/java/com/adobe/cq/commerce/it/http/CatalogPagePropertiesIT.java @@ -0,0 +1,87 @@ +/******************************************************************************* + * + * Copyright 2020 Adobe. All rights reserved. + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. You may obtain a copy + * of the License at http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS + * OF ANY KIND, either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + * + ******************************************************************************/ + +package com.adobe.cq.commerce.it.http; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.http.NameValuePair; +import org.apache.http.message.BasicNameValuePair; +import org.apache.sling.testing.clients.SlingHttpResponse; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.select.Elements; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; + +import com.adobe.cq.testing.client.CQClient; +import com.adobe.cq.testing.client.CommerceClient; +import com.adobe.cq.testing.junit.rules.CQAuthorClassRule; + +import static org.apache.http.HttpStatus.SC_OK; + +public class CatalogPagePropertiesIT { + private static final String PAGE_PROPERTIES_URL = "/mnt/overlay/wcm/core/content/sites/properties.html"; + private static final String CATALOG_PAGE_RESOURCE_TYPE = "core/cif/components/structure/catalogpage/v1/catalogpage"; + private final String CATALOG_PAGE_PATH = "/content" + "/test-catalog-page"; + + @ClassRule + public static final CQAuthorClassRule cqBaseClassRule = new CQAuthorClassRule(); + private static CQClient adminAuthor; + + @BeforeClass + public static void init() { + adminAuthor = cqBaseClassRule.authorRule.getAdminClient(CommerceClient.class); + } + + @Before + public void setUp() throws Exception { + // Create catalog page + adminAuthor.createNode(CATALOG_PAGE_PATH, "cq:Page"); + adminAuthor.createNode(CATALOG_PAGE_PATH + "/jcr:content", "cq:PageContent"); + List props = new ArrayList<>(); + props.add(new BasicNameValuePair("sling:resourceType", CATALOG_PAGE_RESOURCE_TYPE)); + props.add(new BasicNameValuePair("jcr:title", "Test Catalog Page")); + adminAuthor.setPageProperties(CATALOG_PAGE_PATH, props, SC_OK); + } + + @Test + public void testCatalogPageProperties() throws Exception { + SlingHttpResponse response = adminAuthor.doGet(PAGE_PROPERTIES_URL + "?item=" + CATALOG_PAGE_PATH, SC_OK); + Document doc = Jsoup.parse(response.getContent()); + + // Check commerce tab exits + Elements elements = doc.select("coral-tab:contains(Commerce)"); + Assert.assertEquals(1, elements.size()); + + // Check label + elements = doc.select("coral-panel h3:contains(Catalog Page)"); + Assert.assertEquals(1, elements.size()); + + // Check checkbox for show catalog page + elements = doc.select("coral-panel coral-checkbox coral-checkbox-label:contains(Show catalog page)"); + Assert.assertEquals(1, elements.size()); + } + + @After + public void tearDown() throws Exception { + // delete catalog page + adminAuthor.deletePath(CATALOG_PAGE_PATH, SC_OK); + } +} diff --git a/ui.apps/src/main/content/jcr_root/apps/core/cif/components/structure/page/v1/page/_cq_dialog/.content.xml b/ui.apps/src/main/content/jcr_root/apps/core/cif/components/structure/page/v1/page/_cq_dialog/.content.xml index 52a2519fd6..57cda81852 100644 --- a/ui.apps/src/main/content/jcr_root/apps/core/cif/components/structure/page/v1/page/_cq_dialog/.content.xml +++ b/ui.apps/src/main/content/jcr_root/apps/core/cif/components/structure/page/v1/page/_cq_dialog/.content.xml @@ -27,6 +27,9 @@ +