From a3b66e79c0a6ddad90e60d5016d1e1d56a14db1a Mon Sep 17 00:00:00 2001 From: "daniel.solis" Date: Thu, 31 Oct 2024 17:41:06 -0600 Subject: [PATCH] fix(containers) : change search functionality to case insensitive for file containers (#30457) --- .../business/ContainerFactoryImpl.java | 2 +- .../containers/business/ContainerAPITest.java | 74 ++++++++++++++++++- 2 files changed, 71 insertions(+), 5 deletions(-) diff --git a/dotCMS/src/main/java/com/dotmarketing/portlets/containers/business/ContainerFactoryImpl.java b/dotCMS/src/main/java/com/dotmarketing/portlets/containers/business/ContainerFactoryImpl.java index e2da48308b4b..f81c414d8e0b 100644 --- a/dotCMS/src/main/java/com/dotmarketing/portlets/containers/business/ContainerFactoryImpl.java +++ b/dotCMS/src/main/java/com/dotmarketing/portlets/containers/business/ContainerFactoryImpl.java @@ -657,7 +657,7 @@ private Collection findFolderAssetContainers(final Use for (final Object name : searchParams.filteringCriteria().values()) { - if (container.getName().toLowerCase().contains(name.toString())) { + if (container.getName().toLowerCase().contains(name.toString().toLowerCase())) { return true; } } diff --git a/dotcms-integration/src/test/java/com/dotmarketing/portlets/containers/business/ContainerAPITest.java b/dotcms-integration/src/test/java/com/dotmarketing/portlets/containers/business/ContainerAPITest.java index 1289a80c4ddf..118695a352a2 100644 --- a/dotcms-integration/src/test/java/com/dotmarketing/portlets/containers/business/ContainerAPITest.java +++ b/dotcms-integration/src/test/java/com/dotmarketing/portlets/containers/business/ContainerAPITest.java @@ -1,6 +1,8 @@ package com.dotmarketing.portlets.containers.business; +import com.dotcms.JUnit4WeldRunner; import com.dotcms.contenttype.model.type.ContentType; +import com.dotcms.datagen.ContainerAsFileDataGen; import com.dotcms.datagen.ContainerDataGen; import com.dotcms.datagen.ContentTypeDataGen; import com.dotcms.datagen.SiteDataGen; @@ -20,16 +22,21 @@ import com.dotmarketing.util.UUIDGenerator; import com.dotmarketing.util.UtilMethods; import com.liferay.portal.model.User; -import com.liferay.portal.model.User; -import java.util.ArrayList; -import java.util.List; import org.apache.commons.beanutils.BeanUtils; import org.junit.Test; +import org.junit.runner.RunWith; +import javax.enterprise.context.Dependent; import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.Random; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; /** * This class will test operations related with interacting with Containers. @@ -37,6 +44,8 @@ * @author Jorge Urdaneta * @since Aug 31st, 2012 */ +@Dependent +@RunWith(JUnit4WeldRunner.class) public class ContainerAPITest extends ContentletBaseTest { @Test @@ -425,4 +434,61 @@ public void findContainerUsedBySpecificContentType() throws DotDataException, Do } } + /** + * Method to test: {@link ContainerAPI#findContainers(User, ContainerAPI.SearchParams)} + * Given Scenario: Searching for Containers with a given name. + * Expected Result: At least one Container must be returned. + */ + @Test + public void findFileContainerByName() throws DotDataException, DotSecurityException { + Host testSite = null; + try { + // Test data generation + testSite = new SiteDataGen().nextPersisted(); + final String containerName = generateRandomName(10); + final String metadataCode = "$dotJSON.put(\"title\", \"Test " + + containerName + " File Container\")\n" + + "$dotJSON.put(\"max_contentlets\", 25)"; + new ContainerAsFileDataGen() + .host(testSite) + .folderName("Test" + containerName + " File Container") + .metadataCode(metadataCode) + .nextPersisted(); + + // Find by name + final ContainerAPI.SearchParams searchParams = ContainerAPI.SearchParams.newBuilder() + .includeArchived(false) + .includeSystemContainer(false) + .siteId(testSite.getIdentifier()) + .filteringCriterion(Map.of("title", containerName.toLowerCase())).build(); + final List allContainers = containerAPI.findContainers(user, searchParams); + + // Assertions + assertFalse("There must be at least one Container with the name 'File Container'", allContainers.isEmpty()); + } finally { + // Clean up + if (null != testSite) { + APILocator.getHostAPI().archive(testSite, APILocator.systemUser(), false); + APILocator.getHostAPI().delete(testSite, APILocator.systemUser(), false); + } + } + } + + private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + private static final Random RANDOM = new Random(); + + /** + * Generates a random name with the given length. + * + * @param length the length of the name to generate + * @return the generated name + */ + private static String generateRandomName(int length) { + StringBuilder name = new StringBuilder(length); + for (int i = 0; i < length; i++) { + name.append(ALPHABET.charAt(RANDOM.nextInt(ALPHABET.length()))); + } + return name.toString(); + } + }