You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by dk...@apache.org on 2020/07/16 14:11:36 UTC

[sling-org-apache-sling-app-cms] branch master updated: Improving test coverage and test cases for the EditIncludeFilter

This is an automated email from the ASF dual-hosted git repository.

dklco pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-app-cms.git


The following commit(s) were added to refs/heads/master by this push:
     new e6b8ab3  Improving test coverage and test cases for the EditIncludeFilter
e6b8ab3 is described below

commit e6b8ab3973b60e1b25f5b2f26355982846f07994
Author: Dan Klco <dk...@apache.org>
AuthorDate: Thu Jul 16 10:10:37 2020 -0400

    Improving test coverage and test cases for the EditIncludeFilter
---
 .../core/internal/filters/EditIncludeFilter.java   |  6 +++-
 .../internal/filters/EditIncludeFilterTest.java    | 39 +++++++++++++++++++---
 core/src/test/resources/content.json               |  1 +
 3 files changed, 40 insertions(+), 6 deletions(-)

diff --git a/core/src/main/java/org/apache/sling/cms/core/internal/filters/EditIncludeFilter.java b/core/src/main/java/org/apache/sling/cms/core/internal/filters/EditIncludeFilter.java
index b6239ff..fbca8c4 100644
--- a/core/src/main/java/org/apache/sling/cms/core/internal/filters/EditIncludeFilter.java
+++ b/core/src/main/java/org/apache/sling/cms/core/internal/filters/EditIncludeFilter.java
@@ -76,7 +76,8 @@ public class EditIncludeFilter implements Filter {
             String en = entries.nextElement();
             log.info("Loaded template: {}", en);
             try (InputStream is = bundle.getEntry(en).openStream()) {
-                templates.put(en.replace(ENTRY_BASE, ""), IOUtils.toString(is, StandardCharsets.UTF_8));
+                templates.put(en.replace(ENTRY_BASE, ""),
+                        StringUtils.substringAfter(IOUtils.toString(is, StandardCharsets.UTF_8), "-->"));
             }
         }
     }
@@ -222,6 +223,9 @@ public class EditIncludeFilter implements Filter {
         if (component != null && !component.isType(CMSConstants.COMPONENT_TYPE_PAGE)) {
             editPath = component.getEditPath();
         }
+        if (editPath == null) {
+            editPath = "";
+        }
 
         if (StringUtils.isNotEmpty(editPath)) {
             includeEnd = true;
diff --git a/core/src/test/java/org/apache/sling/cms/core/internal/filters/EditIncludeFilterTest.java b/core/src/test/java/org/apache/sling/cms/core/internal/filters/EditIncludeFilterTest.java
index 52f6293..cd8b559 100644
--- a/core/src/test/java/org/apache/sling/cms/core/internal/filters/EditIncludeFilterTest.java
+++ b/core/src/test/java/org/apache/sling/cms/core/internal/filters/EditIncludeFilterTest.java
@@ -16,8 +16,7 @@
  */
 package org.apache.sling.cms.core.internal.filters;
 
-import static org.junit.Assert.assertNotEquals;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertEquals;
 
 import java.io.IOException;
 import java.util.Arrays;
@@ -29,7 +28,6 @@ import javax.jcr.UnsupportedRepositoryOperationException;
 import javax.servlet.FilterChain;
 import javax.servlet.ServletException;
 
-import org.apache.commons.lang3.StringUtils;
 import org.apache.sling.cms.core.helpers.SlingCMSTestHelper;
 import org.apache.sling.testing.mock.sling.junit.SlingContext;
 import org.junit.Before;
@@ -83,7 +81,20 @@ public class EditIncludeFilterTest {
 
         includeFilter.doFilter(context.request(), context.response(), Mockito.mock(FilterChain.class));
 
-        assertTrue(StringUtils.isBlank(context.response().getOutputAsString()));
+        assertEquals("", context.response().getOutputAsString());
+    }
+
+    @Test
+    public void testDisabled() throws IOException, ServletException {
+
+        context.currentResource("/content/apache/sling-apache-org/index/jcr:content/menu/richtext");
+        context.requestPathInfo().setExtension("html");
+
+        context.request().setAttribute(EditIncludeFilter.ENABLED_ATTR_NAME, "false");
+
+        includeFilter.doFilter(context.request(), context.response(), Mockito.mock(FilterChain.class));
+
+        assertEquals("", context.response().getOutputAsString());
     }
 
     @Test
@@ -96,7 +107,25 @@ public class EditIncludeFilterTest {
 
         includeFilter.doFilter(context.request(), context.response(), Mockito.mock(FilterChain.class));
 
-        assertNotEquals("", context.response().getOutputAsString());
+        assertEquals(
+                "<div class=\"sling-cms-component\" data-reload=\"false\" data-component=\"/libs/sling-cms/components/general/richtext\" data-sling-cms-title=\"Rich Text Editor\" data-sling-cms-resource-path=\"/content/apache/sling-apache-org/index/jcr:content/menu/richtext\" data-sling-cms-resource-type=\"sling-cms/components/general/richtext\" data-sling-cms-edit=\"/libs/sling-cms/components/general/richtext/edit\" data-sling-cms-resource-name=\"richtext\">\n    <div class=\"sling-cms- [...]
+                context.response().getOutputAsString());
+    }
+
+    @Test
+    public void testContainer() throws IOException, ServletException {
+
+        context.currentResource("/content/apache/sling-apache-org/index/jcr:content/container");
+        context.requestPathInfo().setExtension("html");
+
+        context.request().setAttribute(EditIncludeFilter.ENABLED_ATTR_NAME, "true");
+        context.request().setAttribute(EditIncludeFilter.WRITE_DROP_TARGET_ATTR_NAME, Boolean.TRUE);
+
+        includeFilter.doFilter(context.request(), context.response(), Mockito.mock(FilterChain.class));
+
+        assertEquals(
+                "<div class=\"sling-cms-droptarget\" data-path=\"/content/apache/sling-apache-org/index/jcr:content\" data-order=\"before container\"></div><div class=\"sling-cms-component\" data-reload=\"false\" data-component=\"/libs/sling-cms/components/general/container\" data-sling-cms-title=\"Container\" data-sling-cms-resource-path=\"/content/apache/sling-apache-org/index/jcr:content/container\" data-sling-cms-resource-type=\"sling-cms/components/general/container\" data-sling-cms [...]
+                context.response().getOutputAsString());
     }
 
 }
\ No newline at end of file
diff --git a/core/src/test/resources/content.json b/core/src/test/resources/content.json
index 82e8ffc..9325468 100644
--- a/core/src/test/resources/content.json
+++ b/core/src/test/resources/content.json
@@ -79,6 +79,7 @@
                     "hideInSitemap": false,
                     "container": {
                         "jcr:primaryType": "nt:unstructured",
+                        "sling:resourceType": "sling-cms/components/general/container",
                         "richtext": {
                             "jcr:primaryType": "nt:unstructured",
                             "text": "<p>Apache Sling(TM) is a framework for RESTful web-applications based on an extensible content tree.</p>\r\n<p>In a nutshell, Sling maps HTTP request URLs to content resources based on the request's path, extension and selectors. Using convention over configuration, requests are processed by scripts and servlets, dynamically selected based on the current resource. This fosters meaningful URLs and resource driven request processing, while the modular n [...]