From a4fa5b005a551431e736bf634fd94cb5b8481429 Mon Sep 17 00:00:00 2001 From: "Mark J. Becker" Date: Wed, 13 Jul 2022 15:22:34 +0200 Subject: [PATCH 1/2] CIF-2673 - Add navRoot checkbox to page properties --- .../ShowNavRootRenderConditionServlet.java | 78 ++++++++++++++++ ...ShowNavRootRenderConditionServletTest.java | 92 +++++++++++++++++++ .../page/v1/page/_cq_dialog/.content.xml | 13 +++ .../page/v2/page/_cq_dialog/.content.xml | 13 +++ .../page/v3/page/_cq_dialog/.content.xml | 16 +++- 5 files changed, 210 insertions(+), 2 deletions(-) create mode 100644 bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/servlets/ShowNavRootRenderConditionServlet.java create mode 100644 bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/servlets/ShowNavRootRenderConditionServletTest.java diff --git a/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/servlets/ShowNavRootRenderConditionServlet.java b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/servlets/ShowNavRootRenderConditionServlet.java new file mode 100644 index 0000000000..852a7b66c8 --- /dev/null +++ b/bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/servlets/ShowNavRootRenderConditionServlet.java @@ -0,0 +1,78 @@ +/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + ~ Copyright 2022 Adobe + ~ + ~ Licensed 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 CONDITIONS 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.core.components.internal.servlets; + +import java.io.IOException; + +import javax.servlet.Servlet; +import javax.servlet.ServletException; + +import org.apache.sling.api.SlingHttpServletRequest; +import org.apache.sling.api.SlingHttpServletResponse; +import org.apache.sling.api.servlets.SlingSafeMethodsServlet; +import org.jetbrains.annotations.NotNull; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Reference; + +import com.adobe.cq.commerce.core.components.internal.services.site.SiteStructureFactory; +import com.adobe.cq.commerce.core.components.models.common.SiteStructure; +import com.adobe.granite.ui.components.rendercondition.RenderCondition; +import com.adobe.granite.ui.components.rendercondition.SimpleRenderCondition; +import com.day.cq.wcm.api.Page; +import com.day.cq.wcm.api.PageManager; + +/** + * {@code ShowNavRootRenderConditionServlet} implements a {@code granite:rendercondition} used to determine if the navRoot option should be + * displayed on the properties page of a page. + */ +@Component( + service = { Servlet.class }, + property = { + "sling.servlet.resourceTypes=" + ShowNavRootRenderConditionServlet.RESOURCE_TYPE, + "sling.servlet.methods=GET", + "sling.servlet.extensions=html" + }) +public class ShowNavRootRenderConditionServlet extends SlingSafeMethodsServlet { + public final static String RESOURCE_TYPE = "core/cif/components/renderconditions/showNavRoot"; + + @Reference + private SiteStructureFactory siteStructureFactory; + + @Override + protected void doGet(@NotNull SlingHttpServletRequest request, @NotNull SlingHttpServletResponse response) + throws ServletException, IOException { + request.setAttribute(RenderCondition.class.getName(), new SimpleRenderCondition(showNavRoot(request))); + } + + private boolean showNavRoot(SlingHttpServletRequest request) { + PageManager pageManager = request.getResourceResolver().adaptTo(PageManager.class); + if (pageManager == null) { + return false; + } + + String pagePath = request.getParameter("item"); + Page page = pageManager.getPage(pagePath); + if (page == null) { + return false; + } + + SiteStructure siteStructure = siteStructureFactory.getSiteStructure(page); + Page landingPage = siteStructure.getLandingPage(); + + // Show navRoot option if there is no landing page or if the current page is the landing page. + return landingPage == null || landingPage.equals(page); + } +} diff --git a/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/servlets/ShowNavRootRenderConditionServletTest.java b/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/servlets/ShowNavRootRenderConditionServletTest.java new file mode 100644 index 0000000000..cfacdaba89 --- /dev/null +++ b/bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/servlets/ShowNavRootRenderConditionServletTest.java @@ -0,0 +1,92 @@ +/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + ~ Copyright 2022 Adobe + ~ + ~ Licensed 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 CONDITIONS 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.core.components.internal.servlets; + +import java.io.IOException; + +import javax.servlet.ServletException; + +import org.apache.sling.api.resource.Resource; +import org.apache.sling.servlethelpers.MockSlingHttpServletRequest; +import org.apache.sling.servlethelpers.MockSlingHttpServletResponse; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +import com.adobe.granite.ui.components.rendercondition.RenderCondition; +import com.adobe.granite.ui.components.rendercondition.SimpleRenderCondition; +import io.wcm.testing.mock.aem.junit.AemContext; + +import static com.adobe.cq.commerce.core.testing.TestContext.newAemContext; +import static org.mockito.Mockito.mock; + +public class ShowNavRootRenderConditionServletTest { + + @Rule + public final AemContext context = newAemContext("/context/SiteStructureImplTest/jcr-content.json"); + + private MockSlingHttpServletRequest request; + private MockSlingHttpServletResponse response; + private ShowNavRootRenderConditionServlet servlet; + + @Before + public void setUp() { + response = new MockSlingHttpServletResponse(); + request = new MockSlingHttpServletRequest(context.resourceResolver()); + Resource resource = mock(Resource.class); + request.setResource(resource); + request.setPathInfo("/mnt/overlay/wcm/core/content/sites/properties.html"); + + servlet = context.registerInjectActivateService(new ShowNavRootRenderConditionServlet()); + } + + @Test + public void testReturnsTrueForLandingPageEqualsPage() throws ServletException, IOException { + request.setQueryString("item=/content/nav-root"); + servlet.doGet(request, response); + + SimpleRenderCondition condition = (SimpleRenderCondition) request.getAttribute(RenderCondition.class.getName()); + Assert.assertTrue(condition.check()); + } + + @Test + public void testReturnsTrueForNoLandingPage() throws ServletException, IOException { + request.setQueryString("item=/content/no-nav-root"); + servlet.doGet(request, response); + + SimpleRenderCondition condition = (SimpleRenderCondition) request.getAttribute(RenderCondition.class.getName()); + Assert.assertTrue(condition.check()); + } + + @Test + public void testReturnsFalseForDefinedLandingPage() throws ServletException, IOException { + request.setQueryString("item=/content/nav-root/content-page"); + servlet.doGet(request, response); + + SimpleRenderCondition condition = (SimpleRenderCondition) request.getAttribute(RenderCondition.class.getName()); + Assert.assertFalse(condition.check()); + } + + @Test + public void testReturnsFalseForInvalidPage() throws ServletException, IOException { + request.setQueryString("item=/content/does-not-exist"); + servlet.doGet(request, response); + + SimpleRenderCondition condition = (SimpleRenderCondition) request.getAttribute(RenderCondition.class.getName()); + Assert.assertFalse(condition.check()); + } +} 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 7866f5a183..6926d2080c 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 @@ -130,8 +130,21 @@ jcr:primaryType="nt:unstructured" sling:resourceType="core/cif/components/renderconditions/pagetype" pageType="product"/> + + + + + + + + - + + + + Date: Thu, 14 Jul 2022 11:47:16 +0200 Subject: [PATCH 2/2] hide associated content section when no landing page is known --- .../components/structure/page/v1/page/_cq_dialog/.content.xml | 2 ++ .../components/structure/page/v2/page/_cq_dialog/.content.xml | 2 ++ .../components/structure/page/v3/page/_cq_dialog/.content.xml | 2 ++ 3 files changed, 6 insertions(+) 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 6926d2080c..9a30bd34e5 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 @@ -47,6 +47,8 @@ + + +