Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CIF-1549 - Catalog option 'Show landing page' / commerce tab does not show up under page properties #355

Merged
merged 7 commits into from
Aug 4, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 },
Expand All @@ -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) {
Expand All @@ -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;
Expand All @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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());
}
}
8 changes: 8 additions & 0 deletions bundles/core/src/test/resources/context/jcr-page-filter.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
Original file line number Diff line number Diff line change
@@ -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<NameValuePair> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
<granite:rendercondition
jcr:primaryType="nt:unstructured"
sling:resourceType="granite/ui/components/coral/foundation/renderconditions/or">
<catalogPageCondition jcr:primaryType="nt:unstructured"
sling:resourceType="core/cif/components/renderconditions/pagetype"
pageType="catalog"/>
<categoryPageCondition jcr:primaryType="nt:unstructured"
sling:resourceType="core/cif/components/renderconditions/pagetype"
pageType="category"/>
Expand Down