Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LPS-52280 Tests #23193

Closed
wants to merge 15 commits into from
Closed
Expand Up @@ -95,19 +95,22 @@ public String updateContent(
blogsEntryAttachmentFileEntryReference :
blogsEntryAttachmentFileEntryReferences) {

StringBundler sb = new StringBundler(5);
StringBundler sb = new StringBundler(8);

sb.append("<img.*");
sb.append("<\\s*?img");
sb.append(_ATTRIBUTE_LIST_REGEXP);
sb.append(EditorConstants.ATTRIBUTE_DATA_IMAGE_ID);
sb.append("=\\s?\"");
sb.append("\\s*?=\\s*?\"");
sb.append(
blogsEntryAttachmentFileEntryReference.
getTempBlogsEntryAttachmentFileEntryId());
sb.append("\".*src=\\s?\"(.*)\".*/>");
sb.append("\"");
sb.append(_ATTRIBUTE_LIST_REGEXP);
sb.append("/>");

content = content.replaceAll(
sb.toString(),
getBlogsEntryAttachmentFileEntryLink(
getBlogsEntryAttachmentFileEntryImgTag(
blogsEntryAttachmentFileEntryReference.
getBlogsEntryAttachmentFileEntry()));
}
Expand All @@ -129,7 +132,7 @@ protected FileEntry addBlogsEntryAttachmentFileEntry(
true);
}

protected String getBlogsEntryAttachmentFileEntryLink(
protected String getBlogsEntryAttachmentFileEntryImgTag(
FileEntry blogsEntryAttachmentEntryFileEntry) {

String fileEntryURL = PortletFileRepositoryUtil.getPortletFileEntryURL(
Expand All @@ -138,4 +141,7 @@ protected String getBlogsEntryAttachmentFileEntryLink(
return "<img src=\"" + fileEntryURL + "\" />";
}

private static final String _ATTRIBUTE_LIST_REGEXP =
"(\\s*?\\w+\\s*?=\\s*?\"[^\"]*\")*?\\s*?";

}
@@ -0,0 +1,237 @@
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/

package com.liferay.portlet.blogs;

import com.liferay.portal.kernel.editor.EditorConstants;
import com.liferay.portal.kernel.repository.model.FileEntry;
import com.liferay.portal.kernel.test.AggregateTestRule;
import com.liferay.portal.kernel.util.ContentTypes;
import com.liferay.portal.kernel.util.DigesterUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.TempFileEntryUtil;
import com.liferay.portal.model.Group;
import com.liferay.portal.model.User;
import com.liferay.portal.portletfilerepository.PortletFileRepositoryUtil;
import com.liferay.portal.test.DeleteAfterTestRun;
import com.liferay.portal.test.LiferayIntegrationTestRule;
import com.liferay.portal.test.MainServletTestRule;
import com.liferay.portal.util.test.GroupTestUtil;
import com.liferay.portal.util.test.RandomTestUtil;
import com.liferay.portal.util.test.UserTestUtil;
import com.liferay.portlet.blogs.model.BlogsEntry;
import com.liferay.portlet.blogs.util.test.BlogsTestUtil;

import java.io.InputStream;

import java.util.ArrayList;
import java.util.List;

import org.junit.Assert;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;

/**
* @author Roberto Díaz
* @author Sergio González
*/
public class BlogsEntryAttachmentFileEntryHelperTest {

@ClassRule
@Rule
public static final AggregateTestRule aggregateTestRule =
new AggregateTestRule(
new LiferayIntegrationTestRule(), MainServletTestRule.INSTANCE);

@Before
public void setUp() throws Exception {
_group = GroupTestUtil.addGroup();
_user = UserTestUtil.addGroupAdminUser(_group);
}

@Test
public void testAddBlogsEntryAttachmentFileEntries() throws Exception {
FileEntry tempFileEntry = TempFileEntryUtil.addTempFileEntry(
_group.getGroupId(), _user.getUserId(), _TEMP_FOLDER_NAME,
"image.jpg", getInputStream(), ContentTypes.IMAGE_JPEG);

List<BlogsEntryAttachmentFileEntryReference>
blogsEntryAttachmentFileEntryReferences =
getBlogsEntryAttachmentFileEntryReferences(tempFileEntry);

Assert.assertEquals(1, blogsEntryAttachmentFileEntryReferences.size());

BlogsEntryAttachmentFileEntryReference
blogsEntryAttachmentFileEntryReference =
blogsEntryAttachmentFileEntryReferences.get(0);

Assert.assertEquals(
tempFileEntry.getFileEntryId(),
blogsEntryAttachmentFileEntryReference.
getTempBlogsEntryAttachmentFileEntryId());

FileEntry fileEntry =
blogsEntryAttachmentFileEntryReference.
getBlogsEntryAttachmentFileEntry();

Assert.assertEquals(tempFileEntry.getTitle(), fileEntry.getTitle());
Assert.assertEquals(
tempFileEntry.getMimeType(), fileEntry.getMimeType());
Assert.assertEquals(
DigesterUtil.digestBase64(tempFileEntry.getContentStream()),
DigesterUtil.digestBase64(fileEntry.getContentStream()));
}

@Test
public void testGetBlogsEntryAttachmentFileEntryImgTag() throws Exception {
FileEntry tempFileEntry = TempFileEntryUtil.addTempFileEntry(
_group.getGroupId(), _user.getUserId(), _TEMP_FOLDER_NAME,
"image.jpg", getInputStream(), ContentTypes.IMAGE_JPEG);

String fileEntryURL = PortletFileRepositoryUtil.getPortletFileEntryURL(
null, tempFileEntry, StringPool.BLANK);

Assert.assertEquals(
"<img src=\"" + fileEntryURL + "\" />",
_blogsEntryAttachmentFileEntryHelper.
getBlogsEntryAttachmentFileEntryImgTag(tempFileEntry));
}

@Test
public void testGetTempBlogsEntryAttachmentFileEntries() throws Exception {
FileEntry tempFileEntry = TempFileEntryUtil.addTempFileEntry(
_group.getGroupId(), _user.getUserId(), _TEMP_FOLDER_NAME,
"image.jpg", getInputStream(), ContentTypes.IMAGE_JPEG);

String tempFileEntryImgTag =
BlogsTestUtil.getTempBlogsEntryAttachmentFileEntryImgTag(
tempFileEntry.getFileEntryId(),
PortletFileRepositoryUtil.getPortletFileEntryURL(
null, tempFileEntry, StringPool.BLANK));

List<FileEntry> tempBlogsEntryAttachments =
_blogsEntryAttachmentFileEntryHelper.
getTempBlogsEntryAttachmentFileEntries(
getContent(tempFileEntryImgTag));

Assert.assertEquals(1, tempBlogsEntryAttachments.size());

for (FileEntry tempBlogsEntryAttachment : tempBlogsEntryAttachments) {
Assert.assertEquals(
tempFileEntry.getFileEntryId(),
tempBlogsEntryAttachment.getFileEntryId());
}
}

@Test
public void testGetTempBlogsEntryAttachmentFileEntriesWithModifiedImgTag()
throws Exception {

FileEntry tempFileEntry = TempFileEntryUtil.addTempFileEntry(
_group.getGroupId(), _user.getUserId(), _TEMP_FOLDER_NAME,
"image.jpg", getInputStream(), ContentTypes.IMAGE_JPEG);

String tempFileEntryImgTag = getModifiedTempFileEntryImgTag(
tempFileEntry);

List<FileEntry> tempBlogsEntryAttachments =
_blogsEntryAttachmentFileEntryHelper.
getTempBlogsEntryAttachmentFileEntries(
getContent(tempFileEntryImgTag));

Assert.assertEquals(1, tempBlogsEntryAttachments.size());

for (FileEntry tempBlogsEntryAttachment : tempBlogsEntryAttachments) {
Assert.assertEquals(
tempFileEntry.getFileEntryId(),
tempBlogsEntryAttachment.getFileEntryId());
}
}

protected List<BlogsEntryAttachmentFileEntryReference>
getBlogsEntryAttachmentFileEntryReferences(
FileEntry tempFileEntry)
throws Exception {

BlogsEntry entry = BlogsTestUtil.addEntry(_group, true);

List<FileEntry> tempFileEntries = new ArrayList<>();

tempFileEntries.add(tempFileEntry);

return
_blogsEntryAttachmentFileEntryHelper.
addBlogsEntryAttachmentFileEntries(
_group.getGroupId(), _user.getUserId(), entry.getEntryId(),
tempFileEntries);
}

protected String getContent(String tempFileEntryImgTag) {
StringBundler sb = new StringBundler(10);

sb.append("<p>");
sb.append(RandomTestUtil.randomStrings(50));
sb.append("</p>");
sb.append("<a href=\"www.liferay.com\"><span>");
sb.append(RandomTestUtil.randomStrings(50));
sb.append("<img src=\"www.liferay.com/logo.png\" /><span>");
sb.append(RandomTestUtil.randomStrings(50));
sb.append("</span>");
sb.append(tempFileEntryImgTag);
sb.append("<span></a>");

return sb.toString();
}

protected InputStream getInputStream() {
Class<?> clazz = BlogsEntryAttachmentFileEntryHelperTest.class;

ClassLoader classLoader = clazz.getClassLoader();

return classLoader.getResourceAsStream(
"com/liferay/portal/util/dependencies/test.jpg");
}

protected String getModifiedTempFileEntryImgTag(FileEntry tempFileEntry) {
StringBundler sb = new StringBundler(7);

sb.append("<img ");
sb.append(EditorConstants.ATTRIBUTE_DATA_IMAGE_ID);
sb.append("=\"");
sb.append(tempFileEntry.getFileEntryId());
sb.append("\" class=\"test-class\" id=\"test-id\" src=\"");
sb.append(
PortletFileRepositoryUtil.getPortletFileEntryURL(
null, tempFileEntry, StringPool.BLANK));
sb.append("\" title=\"test-title\" />");

return sb.toString();
}

private static final String _TEMP_FOLDER_NAME = BlogsEntry.class.getName();

private final BlogsEntryAttachmentFileEntryHelper
_blogsEntryAttachmentFileEntryHelper =
new BlogsEntryAttachmentFileEntryHelper();

@DeleteAfterTestRun
private Group _group;

@DeleteAfterTestRun
private User _user;

}