Skip to content

Commit

Permalink
feat(TimeMachine) include FTM content as rendered html Refs: #30937 (#…
Browse files Browse the repository at this point in the history
…30952)

### Proposed Changes
This update resolves an issue where changes related to TimeMachineDate
were not reflected in the Rendered field, which generates the HTML
output. Previously, the Contentlets field responded to these changes,
but the same functionality was not applied to the Rendered field.

The update introduces two key improvements:

The TimeMachineDate is now passed to the same function used to process
Contentlets, ensuring that changes also affect the Rendered field.

This function handles validation of contentlets, checking if they are
scheduled for future publication or if their validity has expired. This
ensures consistent behavior across fields.

The VTL layer contained logic for handling TimeMachine, which is now
obsolete. All necessary validation is centralized within the updated
function.
  • Loading branch information
fabrizzio-dotCMS authored Dec 18, 2024
1 parent 9bd0ce7 commit 5340ecd
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,7 @@ private InputStream buildVelocity(final Container container, final String uuid,
velocityCodeBuilder.append("#if(!$UtilMethods.isSet($user)) ")
.append("#set($user = $cmsuser.getLoggedInUser($request)) ")
.append("#end");

//end of time-machine block
//end of time-machine block
velocityCodeBuilder.append("#end");

velocityCodeBuilder.append("#set($CONTENT_INODE = '')");
Expand Down Expand Up @@ -408,8 +407,9 @@ private InputStream buildVelocity(final Container container, final String uuid,
}



return writeOutVelocity(filePath, velocityCodeBuilder.toString());
final String containerCode = velocityCodeBuilder.toString();
Logger.debug(this, "DotResourceLoader:\tWriting out container code = " + containerCode);
return writeOutVelocity(filePath, containerCode);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.dotcms.rest.api.v1.DotObjectMapperProvider;
import com.dotcms.util.ConversionUtils;
import com.dotcms.util.TimeMachineUtil;
import com.dotcms.variant.VariantAPI;
import com.dotmarketing.beans.Identifier;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.business.web.WebAPILocator;
Expand Down Expand Up @@ -36,6 +37,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -80,7 +82,8 @@ public ContentUtils getInstance()
* @return The requested {@link Contentlet} object.
*/
public static Contentlet find(final String inodeOrIdentifier, final User user, final boolean EDIT_OR_PREVIEW_MODE, final long sessionLang){
return find(inodeOrIdentifier,user,null,EDIT_OR_PREVIEW_MODE, sessionLang);
final Optional<String> timeMachineDate = TimeMachineUtil.getTimeMachineDate();
return find(inodeOrIdentifier,user,timeMachineDate.orElse(null),EDIT_OR_PREVIEW_MODE, sessionLang);
}

private static Contentlet fixRecurringDates(Contentlet contentlet, String[] recDates) {
Expand Down Expand Up @@ -120,25 +123,19 @@ public static Contentlet find(final String inodeOrIdentifierIn, final User user,
return fixRecurringDates(contentlet, recDates);
}

// timemachine
// time-machine
if (tmDate != null) {
// timemachine future dates
// This should take care of the rendering bits for the time machine
final Date ffdate = new Date(Long.parseLong(tmDate));
final Identifier ident = APILocator.getIdentifierAPI().find(inodeOrIdentifier);
if (ident == null || !UtilMethods.isSet(ident.getId())) {
return null;
}

// timemachine content has expired. return nothing
if (UtilMethods.isSet(ident.getSysExpireDate()) && ffdate.after(ident.getSysExpireDate())) {
return null;
}

// timemachine content to be published in the future, return the working version
if (UtilMethods.isSet(ident.getSysPublishDate()) && ffdate.after(ident.getSysPublishDate())) {
return conAPI.findContentletByIdentifierOrFallback(inodeOrIdentifier, false, sessionLang, user, true)
.orElse(null);
}
final PageMode pageMode = PageMode.get();
final Optional<Contentlet> futureContent = conAPI.findContentletByIdentifierOrFallback(
inodeOrIdentifier, sessionLang, VariantAPI.DEFAULT_VARIANT.name(),
ffdate, user, pageMode.respectAnonPerms);
if (futureContent.isPresent()) {
return futureContent.get();
}
// If the content is not found or has expired
// No need to return null we continue to the next step to try to find the content in the live or working version
}

final ContentletVersionInfo contentletVersionInfoByFallback = WebAPILocator.getVariantWebAPI()
Expand Down
2 changes: 1 addition & 1 deletion test-karate/src/test/java/graphql/ftm/newContainer.feature
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Feature: Create a Container
"containerStructures":[
{
"structureId":"#(contentTypeId)",
"code":"$!{dotContentMap.title}"
"code":"<div class=\"contentlet-title\">$!{dotContentMap.title}</div>"
}
]
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Feature: Test Time Machine functionality

Background:

* def CONTENTLET_ONE_V1 = 'test 1'
* def CONTENTLET_ONE_V2 = 'test 1 v2 (This ver will be publshed in the future)'
* def CONTENTLET_TWO_V2 = 'test 2 v2'

* callonce read('classpath:graphql/ftm/setup.feature')

@smoke @positive
Expand All @@ -10,15 +15,11 @@ Feature: Test Time Machine functionality
When method GET
Then status 200
* def pageContents = extractContentlets (response)

* def contentPieceOne = getContentletByUUID(contentPieceOne, contentPieceOneId)
* def contentPieceTwo = getContentletByUUID(contentPieceTwo, contentPieceTwoId)

* def titles = pageContents.map(x => x.title)
# This is the first version of the content, test 1 v2 as the title says it will be published in the future
* match titles contains 'test 1'
* match titles contains CONTENTLET_ONE_V1
# This is the second version of the content, Thisone is already published therefore it should be displayed
* match titles contains 'test 2 v2'
* match titles contains CONTENTLET_TWO_V2

@positive
Scenario: Test Time Machine functionality when a publish date is provided expect the future content to be displayed
Expand All @@ -27,13 +28,11 @@ Feature: Test Time Machine functionality
And headers commonHeaders
When method GET
Then status 200
* def rendered = response.entity.page.rendered
* match rendered contains CONTENTLET_ONE_V2
* def pageContents = extractContentlets (response)

* def contentPieceOne = getContentletByUUID(contentPieceOne, contentPieceOneId)
* def contentPieceTwo = getContentletByUUID(contentPieceTwo, contentPieceTwoId)

* def titles = pageContents.map(x => x.title)
* match titles contains 'test 1 v2 (This ver will be publshed in the future)'
* match titles contains CONTENTLET_TWO_V2

@smoke @positive
Scenario: Send GraphQL query to fetch page details no publish date is sent
Expand All @@ -46,8 +45,8 @@ Feature: Test Time Machine functionality
Then status 200
* def contentlets = contentletsFromGraphQlResponse(response)
* karate.log('contentlets:', contentlets)
* match contentlets contains 'test 1'
* match contentlets contains 'test 2 v2'
* match contentlets contains CONTENTLET_ONE_V1
* match contentlets contains CONTENTLET_TWO_V2

@smoke @positive
Scenario: Send GraphQL query to fetch page details, publish date is sent expect the future content to be displayed
Expand All @@ -60,4 +59,4 @@ Feature: Test Time Machine functionality
Then status 200
* def contentlets = contentletsFromGraphQlResponse(response)
* karate.log('contentlets:', contentlets)
* match contentlets contains 'test 1 v2 (This ver will be publshed in the future)'
* match contentlets contains CONTENTLET_ONE_V2

0 comments on commit 5340ecd

Please sign in to comment.