From 7659e6c0d7ef6b4878184dd7860312cd730f4429 Mon Sep 17 00:00:00 2001 From: freddyDOTCMS Date: Tue, 17 Dec 2024 12:23:31 -0600 Subject: [PATCH] Fixing error when try to publish a Template and the Unqiue feilds database validation is enabled --- .../business/VersionableAPIImpl.java | 4 +-- .../templates/business/TemplateAPITest.java | 35 +++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/dotCMS/src/main/java/com/dotmarketing/business/VersionableAPIImpl.java b/dotCMS/src/main/java/com/dotmarketing/business/VersionableAPIImpl.java index 243dffc331c8..199b0ab8c542 100644 --- a/dotCMS/src/main/java/com/dotmarketing/business/VersionableAPIImpl.java +++ b/dotCMS/src/main/java/com/dotmarketing/business/VersionableAPIImpl.java @@ -524,6 +524,8 @@ public void setLive ( final Versionable versionable ) throws DotDataException, D newInfo.setLiveInode(versionable.getInode()); newInfo.setPublishDate(new Date()); versionableFactory.saveContentletVersionInfo( newInfo, true ); + + uniqueFieldValidationStrategyResolver.get().afterPublish(versionable.getInode()); } else { final VersionInfo info = versionableFactory.getVersionInfo( versionable.getVersionId() ); @@ -534,8 +536,6 @@ public void setLive ( final Versionable versionable ) throws DotDataException, D info.setLiveInode( versionable.getInode() ); this.versionableFactory.saveVersionInfo( info, true ); } - - uniqueFieldValidationStrategyResolver.get().afterPublish(versionable.getInode()); } /** diff --git a/dotcms-integration/src/test/java/com/dotmarketing/portlets/templates/business/TemplateAPITest.java b/dotcms-integration/src/test/java/com/dotmarketing/portlets/templates/business/TemplateAPITest.java index 1c87d8c96076..09bdaf5689e0 100644 --- a/dotcms-integration/src/test/java/com/dotmarketing/portlets/templates/business/TemplateAPITest.java +++ b/dotcms-integration/src/test/java/com/dotmarketing/portlets/templates/business/TemplateAPITest.java @@ -12,6 +12,7 @@ import com.dotcms.IntegrationTestBase; import com.dotcms.api.web.HttpServletRequestThreadLocal; import com.dotcms.content.elasticsearch.ESQueryCache; +import com.dotcms.content.elasticsearch.business.ESContentletAPIImpl; import com.dotcms.contenttype.model.type.ContentType; import com.dotcms.datagen.*; import com.dotcms.rendering.velocity.viewtools.DotTemplateTool; @@ -63,6 +64,7 @@ import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; +import org.quartz.JobExecutionException; import javax.servlet.http.HttpServletRequest; @@ -317,6 +319,39 @@ public void publishTemplate_expects_live_true() throws Exception { assertTrue(templateSaved.isLive()); } + + /** + * Method to test: {@link TemplateAPIImpl#publishTemplate(Template, User, boolean)} + * When: Publish a Template with the UniqueField Database Validation set to true + * should: Template should be live true + */ + @Test + public void publishTemplateWithUniqueFieldDatbaseValidationEnabled() throws Exception { + final boolean oldEnabledDataBaseValidation = ESContentletAPIImpl.getFeatureFlagDbUniqueFieldValidation(); + + try { + ESContentletAPIImpl.setFeatureFlagDbUniqueFieldValidation(true); + final Host host = hostAPI.findDefaultHost(user, false); + final String body = " I'm mostly empty "; + final String title = "empty test template " + UUIDGenerator.generateUuid(); + final Template template = new Template(); + template.setTitle(title); + template.setBody(body); + final Template templateSaved = templateAPI.saveTemplate(template, host, user, false); + assertTrue(UtilMethods.isSet(templateSaved.getInode())); + assertTrue(UtilMethods.isSet(templateSaved.getIdentifier())); + assertEquals(templateSaved.getBody(), body); + assertEquals(templateSaved.getTitle(), title); + assertFalse(templateSaved.isLive()); + + templateAPI.publishTemplate(templateSaved, user, false); + assertTrue(templateSaved.isLive()); + } finally { + + ESContentletAPIImpl.setFeatureFlagDbUniqueFieldValidation(oldEnabledDataBaseValidation); + } + } + /** * Method to test: unpublishTemplate * Given Scenario: Create a template, publish and unpublish it