Skip to content

Commit

Permalink
Issue 19972 fixing bug when default path is set with a wrong value (#…
Browse files Browse the repository at this point in the history
…21388)

* #19972 Fixing bug when the defaultPath is set with a wrong path

* #19972 Testing
  • Loading branch information
freddyDOTCMS authored and victoralfaro-dotcms committed Dec 13, 2021
1 parent 83e5c85 commit 3f412f0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
Expand Up @@ -120,7 +120,45 @@ public void defaultPathEqualsToFieldVariable() throws DotDataException, DotSecur

final Field field = (Field) map.get(FIELD);

addDefaultPathFieldVariable(host, field, folder.getPath());
final Folder defaultPathFolderValue = new FolderDataGen().nextPersisted();
addDefaultPathFieldVariable(host, field, defaultPathFolderValue.getPath());

final Optional<Folder> defaultPathFolder = BrowserUtil.getDefaultPathFolder(
(Contentlet) map.get(CONTENTLET), field, APILocator.systemUser());

assertEquals(defaultPathFolderValue.getIdentifier(), defaultPathFolder.get().getIdentifier());
}

/**
* Method to test: {@link BrowserUtil#getDefaultPathFolder(Contentlet, Field, User)}
* When: The {@link Field} has a wrong defaultPath's Field Variable
* Should: Return the folder set in the defaultPath
*
* @throws DotDataException
* @throws IOException
* @throws DotSecurityException
*/
@Test
public void defaultPathEqualsToWrongFieldVariable() throws DotDataException, DotSecurityException {

final Map<String, Object> map = createContentletWithImageFieldWithoutValue();
addFolderHostField((ContentType) map.get(CONTENT_TYPE), (Contentlet) map.get(CONTENTLET));

final Host host = (Host) map.get(HOST);
final Folder folder = (Folder) map.get(FOLDER);

final HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletRequestThreadLocal.INSTANCE.setRequest(request);

final HttpSession httpSession = mock(HttpSession.class);
when(request.getSession(false)).thenReturn(httpSession);
when(request.getAttribute(Host.HOST_VELOCITY_VAR_NAME)).thenReturn(host.getIdentifier());

final Folder selectAsLastFolder = selectAsLastFolder(httpSession);

final Field field = (Field) map.get(FIELD);

addDefaultPathFieldVariable(host, field, "wrong_path");

final Optional<Folder> defaultPathFolder = BrowserUtil.getDefaultPathFolder(
(Contentlet) map.get(CONTENTLET), field, APILocator.systemUser());
Expand Down
Expand Up @@ -111,13 +111,12 @@ private static Optional<Folder> resolveWithFieldVariable(final Field field, fina
List<FieldVariable> fieldVariables = APILocator.getFieldAPI()
.getFieldVariablesForField(field.id(), user, true);

final List<FieldVariable> defaulPathVariable = fieldVariables.stream()
final Optional<FieldVariable> defaulPathVariable = fieldVariables.stream()
.filter(fieldVariable -> "defaultPath".equals(fieldVariable.getKey()))
.limit(1)
.collect(Collectors.toList());
.findFirst();

if (UtilMethods.isSet(defaulPathVariable)) {
final FieldVariable defaultPathVariable = defaulPathVariable.get(0);
if (defaulPathVariable.isPresent()) {
final FieldVariable defaultPathVariable = defaulPathVariable.get();
final String fieldVariableValue = defaultPathVariable.getValue();

if (fieldVariableValue.startsWith(HOST_INDICATOR)) {
Expand All @@ -141,11 +140,13 @@ private static Optional<Folder> resolveWithFieldVariable(final Field field, fina
} else {
final String currentHost = WebAPILocator.getHostWebAPI().getCurrentHost()
.getIdentifier();
return Optional.of(
APILocator.getFolderAPI()

final Folder folderByPath = APILocator.getFolderAPI()
.findFolderByPath(fieldVariableValue, currentHost,
APILocator.systemUser(), false)
);
APILocator.systemUser(), false);

return UtilMethods.isSet(folderByPath) && UtilMethods.isSet(folderByPath.getIdentifier())
? Optional.of(folderByPath) : Optional.empty();
}
} else {
Logger.warn(BrowserUtil.class, () -> "defaultPath variable not exists for field " + field.name());
Expand Down

0 comments on commit 3f412f0

Please sign in to comment.