Skip to content

Commit

Permalink
SITES-14361 - List component added to template structure shows wrong …
Browse files Browse the repository at this point in the history
…images

 * fixed featured image display for pages in a list
 * updated unit tests
  • Loading branch information
LSantha committed Jul 12, 2023
1 parent 312e3d6 commit 360968a
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ public static Resource getComponentCandidate(@NotNull String suffix, Resource co
.map(Page::getTemplate)
.map(template -> ResourceUtil.normalize(template.getPath() + suffixPath))
.map(resourceResolver::getResource).orElse(null);
} else {
}

if (componentCandidate == null) {
// image coming from external resource
Resource externalImageResource = resourceResolver.getResource(suffixPath);
if (externalImageResource != null && externalImageResource.isResourceType(IMAGE_RESOURCE_TYPE)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ protected void initModel() {
templateRelativePath = resource.getPath().substring(template.getPath().length());
} else {
baseResourcePath = resource.getPath();
if (resource.getResourceResolver().getResource(resource.getPath()) == null) {
// synthetic merged resource, use the current page path as base path
baseResourcePath = currentPage.getPath();
}
}
baseResourcePath = resource.getResourceResolver().map(request, baseResourcePath);
if (smartSizesSupported()) {
Expand Down Expand Up @@ -293,9 +297,9 @@ protected void initModel() {
} else {
src += extension;
}
src += (inTemplate ? Text.escapePath(templateRelativePath) : hasExternalImageResource ? externalImageResourcePath : "") +
(lastModifiedDate > 0 ? ("/" + lastModifiedDate + (StringUtils.isNotBlank(imageName) ? ("/" + imageName) : "")) : "") +
(inTemplate || hasExternalImageResource || lastModifiedDate > 0 ? DOT + extension : "");
src += inTemplate ? Text.escapePath(templateRelativePath) : hasExternalImageResource ? externalImageResourcePath : "";
src += lastModifiedDate > 0 ? "/" + lastModifiedDate + (StringUtils.isNotBlank(imageName) ? "/" + imageName : "") : "";
src += inTemplate || hasExternalImageResource || lastModifiedDate > 0 ? DOT + extension : "";
}

if (!isDecorative) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

import javax.annotation.PostConstruct;

import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.SyntheticResource;
import org.apache.sling.models.annotations.Exporter;
import org.apache.sling.models.annotations.Model;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -50,7 +53,20 @@ public class ListImpl extends com.adobe.cq.wcm.core.components.internal.models.v

@Override
protected ListItem newPageListItem(@NotNull LinkManager linkManager, @NotNull Page page, String parentId, Component component) {
return new PageListItemImpl(linkManager, page, parentId, component, showDescription, linkItems || displayItemAsTeaser, resource);
Resource listResource = getListResource();
return new PageListItemImpl(linkManager, page, parentId, component, showDescription, linkItems || displayItemAsTeaser, listResource);
}

protected Resource getListResource() {
Resource listResource = resource;
String template = currentPage.getProperties().get("cq:template", String.class);
if (StringUtils.isNotBlank(template) && listResource.getPath().startsWith(template)) {
String containingPagePath = currentPage.getPageManager().getContainingPage(listResource).getPath();
String mergedResourcePath = listResource.getPath().replace(containingPagePath, currentPage.getPath());
// if the resource comes from the template we need to merge it with the current page
listResource = new SyntheticResource(request.getResourceResolver(), mergedResourcePath, listResource.getResourceType());
}
return listResource;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.apache.sling.models.annotations.Model;
import org.jetbrains.annotations.NotNull;

import javax.annotation.PostConstruct;
import java.text.Collator;
import java.util.Collection;
import java.util.Comparator;
Expand All @@ -51,7 +50,8 @@ public class ListImpl extends com.adobe.cq.wcm.core.components.internal.models.v

@Override
protected ListItem newPageListItem(@NotNull LinkManager linkManager, @NotNull Page page, String parentId, Component component) {
return new PageListItemImpl(linkManager.get(page).build(), page, parentId, component, showDescription, linkItems || displayItemAsTeaser, resource);
Resource listResource = getListResource();
return new PageListItemImpl(linkManager.get(page).build(), page, parentId, component, showDescription, linkItems || displayItemAsTeaser, listResource);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import java.util.Collection;

import com.adobe.cq.wcm.core.components.models.Image;
import com.adobe.cq.wcm.core.components.models.Teaser;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -36,13 +38,19 @@
public class ListImplTest extends com.adobe.cq.wcm.core.components.internal.models.v1.ListImplTest {

private static final String TEST_BASE = "/list/v3";
protected static final String CONF_ROOT = "/conf";
protected static final String TEST_CONF_ROOT = "/apps/core/wcm/components";

protected static final String LIST_V3_1 = "/content/list/pages/page_7";
protected static final String LIST_V3_1_TEMPLATE = "/conf/coretest/settings/wcm/templates/template-1/structure/jcr:content/root/list";

@BeforeEach
@Override
public void setUp() {
testBase = TEST_BASE;
internalSetup();
context.load().json(testBase + CoreComponentTestContext.TEST_APPS_JSON, TEST_APPS_ROOT);
context.load().json(testBase + CoreComponentTestContext.TEST_CONF_JSON, CONF_ROOT);
}

@Test
Expand Down Expand Up @@ -85,4 +93,48 @@ protected void testListRenderedAsTeaserItems() {

Utils.testJSONExport(list, Utils.getTestExporterJSONPath(testBase, LIST_20));
}

@Test
protected void testChildPageListInTemplateWithFeaturedImageRenderedAsTeaser() {
Utils.enableDataLayer(context, true);
Resource resource = context.resourceResolver().getResource(LIST_V3_1_TEMPLATE);
if (resource == null) {
throw new IllegalStateException("Did you forget to define test resource " + LIST_V3_1_TEMPLATE + "?");
}
context.currentPage(context.pageManager().getPage(LIST_V3_1));
context.currentResource(resource);

List list = context.request().adaptTo(List.class);

assertEquals(2, list.getListItems().size());
Collection<ListItem> items = list.getListItems();

// The featured image of the page exists: the featured image node of the page is used to render the teaser item
ListItem item0 = (ListItem) items.toArray()[0];
Resource teaserResource0 = item0.getTeaserResource();
ValueMap teaserProperties = teaserResource0.getValueMap();
String linkURL = teaserProperties.get("linkURL", String.class);
String fileReference = teaserProperties.get("fileReference", String.class);
String title = teaserProperties.get("jcr:title", String.class);
assertEquals("/content/list/pages/page_7/jcr:content/root/list", teaserResource0.getPath(), "image resource: path");
assertEquals("core/wcm/components/teaser/v2/teaser", teaserResource0.getResourceType(), "image resource: resource type");
assertEquals("/content/list/pages/page_7/page_7_1", linkURL, "image resource: linkURL");
assertEquals("/content/dam/core/images/Adobe_Systems_logo_and_wordmark.png", fileReference, "image resource: fileReference");
assertEquals("Page 7.1", title, "image resource: title");

// The featured image of the page does not exist: the content node of the page is used to render the teaser item
ListItem item1 = (ListItem) items.toArray()[1];
Resource teaserResource2 = item1.getTeaserResource();
assertEquals("/content/list/pages/page_7/jcr:content/root/list", teaserResource2.getPath(), "image resource: path");

Utils.testJSONExport(list, Utils.getTestExporterJSONPath(testBase, LIST_V3_1));

context.currentResource(teaserResource0);
Teaser teaser = context.request().adaptTo(Teaser.class);
Resource imageResource = teaser.getImageResource();

context.currentResource(imageResource);
Image image = context.request().adaptTo(Image.class);
assertEquals("/content/list/pages/page_7.coreimg.png/content/list/pages/page_7/page_7_1/jcr:content/cq:featuredimage.png", image.getSrc());
}
}
50 changes: 50 additions & 0 deletions bundles/core/src/test/resources/list/v3/exporter-page_7.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"id": "list-dc1f6a2ea5",
"dateFormatString": "yyyy-MM-dd",
"items": [
{
"id": "list-dc1f6a2ea5-item-b1f4d722d9",
"path": "/content/list/pages/page_7/page_7_1",
"link": {
"valid": true,
"url": "/content/list/pages/page_7/page_7_1.html"
},
"title": "Page 7.1",
"dataLayer": {
"list-dc1f6a2ea5-item-b1f4d722d9": {
"@type": "core/wcm/components/list/v3/list/item",
"dc:title": "Page 7.1",
"xdm:linkURL": "/content/list/pages/page_7/page_7_1.html"
}
},
":type": "cq:PageContent"
},
{
"id": "list-dc1f6a2ea5-item-e85a27c407",
"path": "/content/list/pages/page_7/page_7_2",
"link": {
"valid": true,
"url": "/content/list/pages/page_7/page_7_2.html"
},
"title": "Page 7.2",
"dataLayer": {
"list-dc1f6a2ea5-item-e85a27c407": {
"@type": "core/wcm/components/list/v3/list/item",
"dc:title": "Page 7.2",
"xdm:linkURL": "/content/list/pages/page_7/page_7_2.html"
}
},
":type": "cq:PageContent"
}
],
"showDescription": false,
"showModificationDate": false,
"linkItems": false,
"displayItemAsTeaser": true,
"dataLayer": {
"list-dc1f6a2ea5": {
"@type": "core/wcm/components/list/v3/list"
}
},
":type": "core/wcm/components/list/v3/list"
}
29 changes: 29 additions & 0 deletions bundles/core/src/test/resources/list/v3/test-apps.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
{
"image": {
"jcr:primaryType": "sling:Folder",
"v2" : {
"jcr:primaryType": "nt:unstructured",
"image" : {
"jcr:primaryType" : "cq:Component",
"jcr:title" : "Image (v2)",
"sling:resourceSuperType": "core/wcm/components/image",
"jcr:description" : "Smart Adaptive Image",
"cq:icon" : "image",
"componentGroup" : ".core-wcm"
}
}
},
"list": {
"jcr:primaryType": "sling:Folder",
"v3" : {
Expand All @@ -13,5 +27,20 @@
"componentGroup" : ".core-wcm"
}
}
},
"teaser": {
"jcr:primaryType": "sling:Folder",
"v2" : {
"jcr:primaryType": "nt:unstructured",
"teaser" : {
"jcr:primaryType" : "cq:Component",
"jcr:title" : "Teaser (v2)",
"sling:resourceSuperType": "core/wcm/components/teaser",
"jcr:description" : "Teaser Component",
"cq:icon" : "teaser",
"imageDelegate": "core/wcm/components/image/v3/image",
"componentGroup" : ".core-wcm"
}
}
}
}
36 changes: 36 additions & 0 deletions bundles/core/src/test/resources/list/v3/test-conf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"jcr:primaryType": "sling:Folder",
"coretest" : {
"jcr:primaryType": "sling:OrderedFolder",
"jcr:title" : "Core",
"settings" : {
"jcr:primaryType": "sling:Folder",
"wcm" : {
"jcr:primaryType": "cq:Page",
"templates" : {
"jcr:primaryType": "cq:Page",
"template-1" : {
"structure": {
"jcr:primaryType": "cq:Page",
"jcr:content" : {
"jcr:primaryType" : "cq:PageContent",
"jcr:createdBy" : "admin",
"cq:template" : "/conf/coretest/settings/wcm/templates/template-1",
"root" : {
"jcr:primaryType" : "nt:unstructured",
"sling:resourceType": "wcm/foundation/components/responsivegrid",
"list": {
"jcr:primaryType": "nt:unstructured",
"sling:resourceType": "core/wcm/components/list/v3/list",
"listFrom": "children",
"displayItemAsTeaser": "true"
}
}
}
}
}
}
}
}
}
}
47 changes: 47 additions & 0 deletions bundles/core/src/test/resources/list/v3/test-content.json
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,53 @@
"jcr:title": " ",
"cq:lastModified": "2016-09-21T16:12:45.000-07:00"
}
},
"page_7": {
"jcr:primaryType": "cq:Page",
"jcr:content": {
"jcr:primaryType": "cq:PageContent",
"jcr:title": " ",
"cq:template": "/conf/coretest/settings/wcm/templates/template-1"
},
"page_7_1": {
"jcr:primaryType": "cq:Page",
"jcr:content": {
"jcr:primaryType": "cq:PageContent",
"jcr:title": "Page 7.1",
"cq:featuredimage" : {
"jcr:primaryType" : "nt:unstructured",
"jcr:createdBy" : "admin",
"fileReference" : "/content/dam/core/images/Adobe_Systems_logo_and_wordmark.png"
}
}
},
"page_7_2": {
"jcr:primaryType": "cq:Page",
"jcr:content": {
"jcr:primaryType": "cq:PageContent",
"jcr:title": "Page 7.2"
}
}
}
}
},
"dam": {
"jcr:primaryType": "sling:Folder",
"core": {
"jcr:primaryType": "sling:Folder",
"images": {
"jcr:primaryType": "sling:Folder",
"Adobe_Systems_logo_and_wordmark.png": {
"jcr:primaryType": "dam:Asset",
"jcr:content": {
"jcr:primaryType": "dam:AssetContent",
"jcr:title": "Adobe Systems logo and wordmark",
"metadata": {
"jcr:primaryType": "nt:unstructured",
"dc:format": "image/png"
}
}
}
}
}
}
Expand Down

0 comments on commit 360968a

Please sign in to comment.