Skip to content

Commit

Permalink
Make template optional (#38)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexandru Marian Stancioiu <[email protected]>
Co-authored-by: Stefan Seifert <[email protected]>
  • Loading branch information
3 people authored Apr 23, 2024
1 parent 1ff3c16 commit edc0934
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
3 changes: 3 additions & 0 deletions changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
<action type="update" dev="koenkicken" issue="36">
MockComponent: Implement isContainer method.
</action>
<action type="update" dev="alexandru-stancioiu" issue="38">
MockPageManager: Allow to create page without template.
</action>
<action type="update" dev="sseifert" issue="33">
Remove outdated dependency to org.apache.sling.commons.json.
</action>
Expand Down
30 changes: 19 additions & 11 deletions core/src/main/java/io/wcm/testing/mock/aem/MockPageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,14 @@ else if (!JcrUtil.isValidName(childResourceName)) {
props = new HashMap<>();
props.put(JCR_PRIMARYTYPE, "cq:PageContent");
props.put(JCR_TITLE, title);
props.put(PN_TEMPLATE, template);
if (template != null) {
props.put(PN_TEMPLATE, template);
}
Resource contentResource = this.resourceResolver.create(pageResource, JCR_CONTENT, props);

// create initial content from template
Resource templateResource = resourceResolver.getResource(template);
if (templateResource != null) {
Template templateInstance = templateResource.adaptTo(Template.class);
if (templateInstance != null) {
String initialContentPath = templateInstance.getInitialContentPath();
Resource initialContentResource = resourceResolver.getResource(initialContentPath);
if (initialContentResource != null) {
copyContent(initialContentResource, contentResource, true);
}
}
if (template != null) {
createInitialContentFromTemplate(contentResource, template);
}

if (autoSave) {
Expand All @@ -146,6 +140,20 @@ else if (!JcrUtil.isValidName(childResourceName)) {
return pageResource.adaptTo(Page.class);
}

private void createInitialContentFromTemplate(@NotNull Resource contentResource, @NotNull String template) throws PersistenceException {
Resource templateResource = resourceResolver.getResource(template);
if (templateResource != null) {
Template templateInstance = templateResource.adaptTo(Template.class);
if (templateInstance != null) {
String initialContentPath = templateInstance.getInitialContentPath();
Resource initialContentResource = resourceResolver.getResource(initialContentPath);
if (initialContentResource != null) {
copyContent(initialContentResource, contentResource, true);
}
}
}
}

@SuppressFBWarnings("STYLE")
private void copyContent(Resource source, Resource target, boolean skipPrimaryType) throws PersistenceException {
ValueMap sourceProps = source.adaptTo(ValueMap.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ public void testCreatePage() throws WCMException {
assertTrue(this.resourceResolver.hasChanges());
}

@Test
public void testCreatePageWithNullTemplate() throws WCMException {
testCreatePageInternal(false, null);
assertTrue(this.resourceResolver.hasChanges());
}

@Test
public void testCreatePageWithAutoSave() throws WCMException {
testCreatePageInternal(true, "/apps/sample/templates/homepage");
Expand Down

0 comments on commit edc0934

Please sign in to comment.