Skip to content

Commit

Permalink
#15640 merge master done
Browse files Browse the repository at this point in the history
  • Loading branch information
jdotcms committed Feb 26, 2019
2 parents b6072cd + 34b338e commit 5bc272c
Show file tree
Hide file tree
Showing 54 changed files with 2,027 additions and 489 deletions.

Large diffs are not rendered by default.

@@ -0,0 +1,101 @@
package com.dotmarketing.cms.urlmap;

import static com.dotmarketing.business.PermissionAPI.PERMISSION_READ;
import static org.jgroups.util.Util.assertEquals;
import static org.jgroups.util.Util.assertFalse;
import static org.mockito.Mockito.mock;

import com.dotcms.util.IntegrationTestInitService;
import com.dotmarketing.beans.Host;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.business.PermissionLevel;
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.exception.DotSecurityException;
import com.dotmarketing.util.PageMode;
import com.liferay.portal.model.User;

import java.util.List;
import java.util.Optional;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class URLMapAPIImplTest {
private static User systemUser;
private static Host host;

private URLMapAPIImpl urlMapAPI;

@BeforeClass
public static void prepare() throws Exception {
IntegrationTestInitService.getInstance().init();

systemUser = APILocator.getUserAPI().getSystemUser();
host = APILocator.getHostAPI().findByName("demo.dotcms.com", systemUser, false);
}

@Before
public void prepareTest() {
urlMapAPI = new URLMapAPIImpl();
}

@Test
public void shouldReturnContentletWhenTheContentExists()
throws DotDataException, DotSecurityException {

final UrlMapContext context = getUrlMapContext(systemUser, host,
"/news/u-s-labor-department-moves-forward-on-retirement-advice-proposal");

final Optional<URLMapInfo> urlMapInfoOptional = urlMapAPI.processURLMap(context);

final URLMapInfo urlMapInfo = urlMapInfoOptional.get();
assertEquals("U.S. Labor Department moves forward on retirement advice proposal", urlMapInfo.getContentlet().getName());
assertEquals("/news-events/news/news-detail", urlMapInfo.getIdentifier().getURI());
}

@Test
public void shouldReturnNullWhenTheContentNotExists()
throws DotDataException, DotSecurityException {

final UrlMapContext context = getUrlMapContext(systemUser, host,
"/news/u-s-labor-department-moves-forward-on-retirement-advice-proposal-esp");

final Optional<URLMapInfo> urlMapInfoOptional = urlMapAPI.processURLMap(context);

assertFalse(urlMapInfoOptional.isPresent());
}

@Test
public void shouldReturnNullWhenThePageUriIsNotDetailPage()
throws DotDataException, DotSecurityException {

final UrlMapContext context = getUrlMapContext(systemUser, host, "/about-us/index");

final Optional<URLMapInfo> urlMapInfoOptional = urlMapAPI.processURLMap(context);

assertFalse(urlMapInfoOptional.isPresent());
}

@Test(expected = DotSecurityException.class)
public void shouldThrowDotSecurityExceptionWhenUserDontHavePermission()
throws DotDataException, DotSecurityException {

final List<User> users = APILocator.getUserAPI().getUsersByNameOrEmail("chris@dotcms.com", 1, 1);
final UrlMapContext context = getUrlMapContext(users.get(0), host,
"/news/u-s-labor-department-moves-forward-on-retirement-advice-proposal");

urlMapAPI.processURLMap(context);
}


private UrlMapContext getUrlMapContext(final User systemUser, final Host host, final String uri) {
return UrlMapContextBuilder.builder()
.setHost(host)
.setLanguageId(1l)
.setMode(PageMode.PREVIEW_MODE)
.setUri(uri)
.setUser(systemUser)
.build();
}
}
Expand Up @@ -14,9 +14,13 @@
import com.dotcms.util.CollectionsUtils;
import com.dotcms.util.IntegrationTestInitService;
import com.dotmarketing.beans.Host;
import com.dotmarketing.beans.Permission;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.business.DotStateException;
import com.dotmarketing.business.PermissionAPI;
import com.dotmarketing.business.RelationshipAPI;
import com.dotmarketing.business.Role;
import com.dotmarketing.business.RoleAPI;
import com.dotmarketing.business.UserAPI;
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.exception.DotSecurityException;
Expand Down Expand Up @@ -45,6 +49,7 @@ public class ContentletIntegrationTest {
private static Language defaultLanguage;
private static LanguageAPI languageAPI;
private static RelationshipAPI relationshipAPI;
private static RoleAPI roleAPI;
private static UserAPI userAPI;
private static User user;
private static Host defaultHost;
Expand All @@ -63,6 +68,7 @@ public static void prepare () throws Exception {
languageAPI = APILocator.getLanguageAPI();

userAPI = APILocator.getUserAPI();
roleAPI = APILocator.getRoleAPI();
user = userAPI.getSystemUser();

contentletAPI = APILocator.getContentletAPI();
Expand Down Expand Up @@ -156,13 +162,13 @@ public void testGetRelatedForOneSidedRelationship()
false);

//No cached value
List<Contentlet> result = parentContentlet.getRelated(field.variable());
List<Contentlet> result = parentContentlet.getRelated(field.variable(), user, false);

assertEquals(1, result.size());
assertEquals(childContentlet.getIdentifier(), result.get(0).getIdentifier());

//Cached value
result = parentContentlet.getRelated(field.variable());
result = parentContentlet.getRelated(field.variable(), user, false);

assertEquals(1, result.size());
assertEquals(childContentlet.getIdentifier(), result.get(0).getIdentifier());
Expand All @@ -179,6 +185,91 @@ public void testGetRelatedForOneSidedRelationship()
}
}

@Test
public void testGetRelatedForOneSidedRelationshipWhenLimitedUserShouldReturnEmptyList()
throws DotDataException, DotSecurityException {

ContentType parentContentType = null;
ContentType childContentType = null;
Role newRole = null;

try {

//Create Content Types
parentContentType = getNewContentType();
childContentType = getNewContentType();

//Create Content
Contentlet parentContentlet = new ContentletDataGen(parentContentType.id())
.languageId(defaultLanguage.getId()).next();

final Contentlet childContentlet = new ContentletDataGen(childContentType.id())
.languageId(defaultLanguage.getId()).nextPersisted();

//Create Relationship
final Field field = createAndSaveRelationshipField("myChild", parentContentType.id(),
childContentType.variable(), CARDINALITY);

final Relationship relationship = relationshipAPI.getRelationshipFromField(field, user);

//Save related content
parentContentlet = contentletAPI.checkin(parentContentlet,
CollectionsUtils.map(relationship, CollectionsUtils.list(childContentlet)),
user,
false);

newRole = createRole();

//set individual permissions to the child
APILocator.getPermissionAPI()
.save(new Permission(PermissionAPI.INDIVIDUAL_PERMISSION_TYPE,
childContentlet.getPermissionId(), newRole.getId(),
PermissionAPI.PERMISSION_READ, true), childContentlet, user, false);

//Get related content with system user
List<Contentlet> result = parentContentlet.getRelated(field.variable(), user, false);

assertEquals(1, result.size());
assertEquals(childContentlet.getIdentifier(), result.get(0).getIdentifier());

//Get related content with anonymous user
result = parentContentlet.getRelated(field.variable(), null, false);

assertEquals(0, result.size());

} finally {

if (parentContentType != null && parentContentType.id() != null) {
contentTypeAPI.delete(parentContentType);
}

if (childContentType != null && childContentType.id() != null) {
contentTypeAPI.delete(childContentType);
}

if (newRole != null){
roleAPI.delete(newRole);
}
}
}

private Role createRole() throws DotDataException {
final long millis = System.currentTimeMillis();

// Create Role.
Role newRole = new Role();
newRole.setName("Role" + millis);
newRole.setEditUsers(true);
newRole.setEditPermissions(true);
newRole.setSystem(false);
newRole.setEditLayouts(true);
newRole.setParent(newRole.getId());
newRole = roleAPI.save(newRole);

return newRole;
}


@Test
public void testGetRelatedWhenNoRelatedContentShouldReturnEmptyList()
throws DotDataException, DotSecurityException {
Expand All @@ -199,7 +290,7 @@ public void testGetRelatedWhenNoRelatedContentShouldReturnEmptyList()
final Field field = createAndSaveRelationshipField("myChild", parentContentType.id(),
childContentType.variable(), CARDINALITY);

final List<Contentlet> result = contentlet.getRelated(field.variable());
final List<Contentlet> result = contentlet.getRelated(field.variable(), user, false);

assertTrue(result.isEmpty());

Expand Down Expand Up @@ -227,7 +318,7 @@ public void testGetRelatedWhenInvalidVarFieldShouldThrowAnException()
final Contentlet contentlet = new ContentletDataGen(contentType.id())
.languageId(defaultLanguage.getId()).nextPersisted();

contentlet.getRelated("AnyField");
contentlet.getRelated("AnyField", user, false);

}finally{
contentTypeAPI.delete(contentType);
Expand Down
@@ -1,9 +1,5 @@
package com.dotmarketing.portlets.htmlpages.business;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import com.dotcms.IntegrationTestBase;
import com.dotcms.datagen.ContentletDataGen;
import com.dotcms.datagen.FolderDataGen;
Expand Down Expand Up @@ -51,20 +47,25 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

@RunWith(DataProviderRunner.class)
public class HTMLPageAPITest extends IntegrationTestBase {

private static User sysuser;

@BeforeClass
public static void prepare () throws Exception {

//Setting web app environment
IntegrationTestInitService.getInstance().init();

sysuser=APILocator.getUserAPI().getSystemUser();
}

@Test
public void saveHTMLPage() throws Exception {

User sysuser=APILocator.getUserAPI().getSystemUser();

Host host=APILocator.getHostAPI().findDefaultHost(sysuser, false);
String ext="."+Config.getStringProperty("VELOCITY_PAGE_EXTENSION");

Expand Down Expand Up @@ -122,7 +123,6 @@ public void saveHTMLPage() throws Exception {

@Test
public void delete() throws Exception {
User sysuser=APILocator.getUserAPI().getSystemUser();
Host host=APILocator.getHostAPI().findDefaultHost(sysuser, false);
String ext="."+Config.getStringProperty("VELOCITY_PAGE_EXTENSION");

Expand Down Expand Up @@ -191,7 +191,6 @@ public void delete() throws Exception {

@Test
public void move() throws Exception {
User sysuser=null;
HTMLPageAsset page=null;
Template template=null;
Container container=null;
Expand All @@ -200,7 +199,6 @@ public void move() throws Exception {
Folder folder=null;

try {
sysuser=APILocator.getUserAPI().getSystemUser();
Host host=APILocator.getHostAPI().findDefaultHost(sysuser, false);
String ext="."+Config.getStringProperty("VELOCITY_PAGE_EXTENSION");

Expand Down Expand Up @@ -292,6 +290,37 @@ public void move() throws Exception {

}

@Test
public void testCopy_customTemplateCreatesNewTemplate() throws Exception{
Template template = null;
Folder folder = null;
HTMLPageAsset pageAsset= null;
HTMLPageAsset copiedHTMLPageAsset = null;
try{
//Create Template with Anonymous prefix, so it's a custom layout
template = new TemplateDataGen().title(Template.ANONYMOUS_PREFIX + System.currentTimeMillis()).nextPersisted();

//Create page with that template
folder = new FolderDataGen().nextPersisted();
pageAsset = new HTMLPageDataGen(folder, template).nextPersisted();

//Copy Page
final Contentlet contentlet_copiedHTMLPageAsset = APILocator.getContentletAPI().copyContentlet(pageAsset,sysuser,false);
copiedHTMLPageAsset = APILocator.getHTMLPageAssetAPI().fromContentlet(contentlet_copiedHTMLPageAsset);


//Assert that the template id of the copied page is different than the original page
assertNotEquals("Template ID is the same for both pages",pageAsset.getTemplateId(),copiedHTMLPageAsset.getTemplateId());
}finally {
if(template!=null){
TemplateDataGen.remove(template);
}
if(folder!=null){
FolderDataGen.remove(folder);
}
}
}

/**
* Tests the deletion of a multi-lang page in only one language.
* <p>
Expand Down
Expand Up @@ -94,7 +94,7 @@ public CardinalityTestCase(final int cardinality) {
public static Object[] testCases() {
return new TestCase[]{
//invalid indexed
new TestCase(false, true, 4, 2),
new TestCase(false, true, 4, 1),
new TestCase(false, false, 3, 0),
//invalid listed
new TestCase(true, true, 3, 1),
Expand Down
2 changes: 1 addition & 1 deletion dotCMS/src/main/enterprise

0 comments on commit 5bc272c

Please sign in to comment.