Skip to content

Commit 7a53392

Browse files
committed
ENG-5491: Prefill owner group by user preferences
1 parent 98e5cb0 commit 7a53392

File tree

5 files changed

+170
-7
lines changed

5 files changed

+170
-7
lines changed

Diff for: cms-plugin/src/main/java/com/agiletec/plugins/jacms/apsadmin/content/ContentAction.java

+2
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public void validate() {
8282
* @return Il codice del risultato dell'azione.
8383
*/
8484
public String edit() {
85+
this.getRequest().getSession().removeAttribute(ContentActionConstants.SESSION_PARAM_NAME_CURRENT_CONTENT_GROUP);
8586
try {
8687
Content content = this.getContentManager().loadContent(this.getContentId(), false);
8788
if (null == content) {
@@ -107,6 +108,7 @@ public String edit() {
107108
* @return Il codice del risultato dell'azione.
108109
*/
109110
public String copyPaste() {
111+
this.getRequest().getSession().removeAttribute("contentGroupOnSession");
110112
try {
111113
Content content = this.getContentManager().loadContent(this.getContentId(), this.isCopyPublicVersion());
112114
if (null == content) {

Diff for: cms-plugin/src/main/java/com/agiletec/plugins/jacms/apsadmin/content/IntroNewContentAction.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.agiletec.apsadmin.system.ApsAdminSystemConstants;
2121
import com.agiletec.plugins.jacms.aps.system.services.content.model.Content;
2222
import java.util.Arrays;
23+
import javax.servlet.http.HttpSession;
2324
import org.apache.commons.lang.StringUtils;
2425
import org.entando.entando.aps.system.services.userpreferences.IUserPreferencesManager;
2526
import org.entando.entando.aps.system.services.userpreferences.UserPreferences;
@@ -80,6 +81,7 @@ public String createNew() {
8081
_logger.debug("Invalid content type");
8182
return INPUT;
8283
}
84+
this.getRequest().getSession().removeAttribute(ContentActionConstants.SESSION_PARAM_NAME_CURRENT_CONTENT_GROUP);
8385
prototype.setFirstEditor(username);
8486
UserPreferences pref = this.getUserPreferencesManager().getUserPreferences(username);
8587
if (pref != null) {
@@ -94,7 +96,7 @@ public String createNew() {
9496
});
9597
}
9698
if (StringUtils.isNotBlank(defaultContentOwnerGroup) && null != this.getGroup(defaultContentOwnerGroup)) {
97-
prototype.setMainGroup(defaultContentOwnerGroup);
99+
this.getRequest().getSession().setAttribute(ContentActionConstants.SESSION_PARAM_NAME_CURRENT_CONTENT_GROUP, defaultContentOwnerGroup);
98100
_logger.info("setting ownerGroup to {}", defaultContentOwnerGroup);
99101
}
100102
}
@@ -107,12 +109,14 @@ public String createNew() {
107109
}
108110

109111
protected void fillSessionAttribute(Content prototype) {
110-
if (this.getAuthorizationManager().isAuthOnGroup(this.getCurrentUser(), Group.FREE_GROUP_NAME)) {
111-
this.getRequest().getSession().setAttribute(ContentActionConstants.SESSION_PARAM_NAME_CURRENT_CONTENT_GROUP, Group.FREE_GROUP_NAME);
112+
HttpSession session = this.getRequest().getSession();
113+
if (null == session.getAttribute(ContentActionConstants.SESSION_PARAM_NAME_CURRENT_CONTENT_GROUP)
114+
&& this.getAuthorizationManager().isAuthOnGroup(this.getCurrentUser(), Group.FREE_GROUP_NAME)) {
115+
session.setAttribute(ContentActionConstants.SESSION_PARAM_NAME_CURRENT_CONTENT_GROUP, Group.FREE_GROUP_NAME);
112116
}
113117
String marker = buildContentOnSessionMarker(prototype, ApsAdminSystemConstants.ADD);
114118
super.setContentOnSessionMarker(marker);
115-
this.getRequest().getSession().setAttribute(ContentActionConstants.SESSION_PARAM_NAME_CURRENT_CONTENT_PREXIX + marker, prototype);
119+
session.setAttribute(ContentActionConstants.SESSION_PARAM_NAME_CURRENT_CONTENT_PREXIX + marker, prototype);
116120
_logger.debug("Created ed inserted on session content prototype of type {}", prototype.getTypeCode());
117121
}
118122

Diff for: cms-plugin/src/main/webapp/WEB-INF/plugins/jacms/apsadmin/jsp/content/entryContent.jsp

+2-1
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,9 @@
100100
</s:if>
101101
<s:else>
102102
<div class="input-group">
103+
<s:set var="contentGroupOnSessionVar">${session.contentGroupOnSession}</s:set>
103104
<wpsf:select name="mainGroup" id="contentMainGroup" list="allowedGroups"
104-
value="#session.contentGroupOnSession"
105+
value="#contentGroupOnSessionVar"
105106
listKey="name" listValue="descr" cssClass="form-control" />
106107
<span class="input-group-btn">
107108
<wpsf:submit action="configureMainGroup"

Diff for: cms-plugin/src/test/java/com/agiletec/plugins/jacms/apsadmin/content/TestIntroNewContentAction.java renamed to cms-plugin/src/test/java/com/agiletec/plugins/jacms/apsadmin/content/IntroNewContentActionIntegrationTest.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
/**
3434
* @author E.Santoboni
3535
*/
36-
class TestIntroNewContentAction extends AbstractBaseTestContentAction {
36+
class IntroNewContentActionIntegrationTest extends AbstractBaseTestContentAction {
3737

3838
@Test
3939
void testOpenNew() throws Throwable {
@@ -150,5 +150,7 @@ void testCreateNewVoid_5() throws Throwable {
150150
result = this.executeAction();
151151
assertEquals(Action.SUCCESS, result);
152152
}
153-
153+
154+
155+
154156
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*
2+
* Copyright 2024-Present Entando Inc. (http://www.entando.com) All rights reserved.
3+
*
4+
* This library is free software; you can redistribute it and/or modify it under
5+
* the terms of the GNU Lesser General Public License as published by the Free
6+
* Software Foundation; either version 2.1 of the License, or (at your option)
7+
* any later version.
8+
*
9+
* This library is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11+
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12+
* details.
13+
*/
14+
package com.agiletec.plugins.jacms.apsadmin.content;
15+
16+
import com.agiletec.aps.system.SystemConstants;
17+
import com.agiletec.aps.system.services.authorization.IAuthorizationManager;
18+
import com.agiletec.aps.system.services.group.Group;
19+
import com.agiletec.aps.system.services.group.IGroupManager;
20+
import com.agiletec.aps.system.services.lang.ILangManager;
21+
import com.agiletec.aps.system.services.user.UserDetails;
22+
import com.agiletec.apsadmin.system.BaseAction;
23+
import com.agiletec.plugins.jacms.aps.system.services.content.IContentManager;
24+
import com.agiletec.plugins.jacms.aps.system.services.content.model.Content;
25+
import com.agiletec.plugins.jacms.apsadmin.content.helper.IContentActionHelper;
26+
import com.opensymphony.xwork2.ActionSupport;
27+
import javax.servlet.http.HttpServletRequest;
28+
import javax.servlet.http.HttpSession;
29+
import org.entando.entando.aps.system.services.userpreferences.IUserPreferencesManager;
30+
import org.entando.entando.aps.system.services.userpreferences.UserPreferences;
31+
import org.entando.entando.ent.exception.EntException;
32+
import org.junit.jupiter.api.Assertions;
33+
import org.junit.jupiter.api.BeforeEach;
34+
import org.junit.jupiter.api.Test;
35+
import org.junit.jupiter.api.extension.ExtendWith;
36+
import org.mockito.InjectMocks;
37+
import org.mockito.Mock;
38+
import org.mockito.Mockito;
39+
import org.mockito.MockitoAnnotations;
40+
import org.mockito.Spy;
41+
import org.mockito.junit.jupiter.MockitoExtension;
42+
43+
@ExtendWith(MockitoExtension.class)
44+
class IntroNewContentActionTest {
45+
46+
@Mock
47+
private UserDetails currentUser;
48+
@Mock
49+
private HttpServletRequest request;
50+
@Mock
51+
private HttpSession session;
52+
@Mock
53+
private ILangManager langManager;
54+
@Mock
55+
private IGroupManager groupManager;
56+
@Mock
57+
private IUserPreferencesManager userPreferencesManager;
58+
@Mock
59+
private IContentManager contentManager;
60+
@Mock
61+
private IContentActionHelper contentActionHelper;
62+
@Mock
63+
private IAuthorizationManager authorizationManager;
64+
65+
@Spy
66+
@InjectMocks
67+
private IntroNewContentAction action;
68+
69+
@BeforeEach
70+
void initMocks() {
71+
MockitoAnnotations.initMocks(this);
72+
action.setServletRequest(this.request);
73+
action.setGroupManager(this.groupManager);
74+
action.setLangManager(this.langManager);
75+
action.setContentManager(this.contentManager);
76+
action.setContentActionHelper(this.contentActionHelper);
77+
action.setLangManager(this.langManager);
78+
action.setUserPreferencesManager(this.userPreferencesManager);
79+
action.setAuthorizationManager(this.authorizationManager);
80+
Mockito.when(this.request.getSession()).thenReturn(this.session);
81+
Mockito.when(this.request.getSession().getAttribute(SystemConstants.SESSIONPARAM_CURRENT_USER)).thenReturn(this.currentUser);
82+
Mockito.when(this.currentUser.getUsername()).thenReturn("username");
83+
}
84+
85+
@Test
86+
void showldReturnErrorWithMissingContentType() {
87+
Mockito.doReturn("invalid content type").when(action).getText(Mockito.anyString());
88+
action.setContentTypeCode("XXX");
89+
Mockito.when(this.contentManager.createContentType("XXX")).thenReturn(null);
90+
String result = this.action.createNew();
91+
Assertions.assertEquals(ActionSupport.INPUT, result);
92+
Assertions.assertEquals(1, action.getFieldErrors().size());
93+
Assertions.assertEquals(1, action.getFieldErrors().get("contentTypeCode").size());
94+
Assertions.assertEquals("invalid content type", action.getFieldErrors().get("contentTypeCode").get(0));
95+
}
96+
97+
@Test
98+
void showldExecuteCreateNewContentWithoutPreferences() {
99+
action.setContentTypeCode("XXX");
100+
Content prototype = new Content();
101+
Mockito.when(this.contentManager.createContentType("XXX")).thenReturn(prototype);
102+
String result = this.action.createNew();
103+
Assertions.assertEquals(ActionSupport.SUCCESS, result);
104+
Assertions.assertEquals(0, action.getFieldErrors().size());
105+
Assertions.assertNull(prototype.getMainGroup());
106+
}
107+
108+
@Test
109+
void showldExecuteCreateNewContentWithPreferences() throws Exception {
110+
action.setContentTypeCode("XXX");
111+
Content prototype = new Content();
112+
Mockito.when(this.contentManager.createContentType("XXX")).thenReturn(prototype);
113+
UserPreferences userPreferences = new UserPreferences();
114+
userPreferences.setDefaultContentOwnerGroup("main_group");
115+
userPreferences.setDefaultContentJoinGroups("join1;join2");
116+
Mockito.when(this.userPreferencesManager.getUserPreferences(Mockito.anyString())).thenReturn(userPreferences);
117+
Mockito.when(this.groupManager.getGroup(Mockito.anyString())).thenReturn(Mockito.mock(Group.class));
118+
String result = this.action.createNew();
119+
Assertions.assertEquals(ActionSupport.SUCCESS, result);
120+
Assertions.assertEquals(0, action.getFieldErrors().size());
121+
Mockito.verify(this.session, Mockito.times(1))
122+
.setAttribute(ContentActionConstants.SESSION_PARAM_NAME_CURRENT_CONTENT_GROUP, "main_group");
123+
Assertions.assertEquals(2, prototype.getGroups().size());
124+
Assertions.assertTrue(prototype.getGroups().contains("join1"));
125+
Assertions.assertTrue(prototype.getGroups().contains("join2"));
126+
}
127+
128+
@Test
129+
void showldExecuteCreateNewContentWithEmptyPreferences() throws Exception {
130+
action.setContentTypeCode("XXX");
131+
Content prototype = new Content();
132+
Mockito.when(this.contentManager.createContentType("XXX")).thenReturn(prototype);
133+
Mockito.when(this.userPreferencesManager.getUserPreferences(Mockito.anyString())).thenReturn(new UserPreferences());
134+
String result = this.action.createNew();
135+
Assertions.assertEquals(ActionSupport.SUCCESS, result);
136+
Assertions.assertEquals(0, action.getFieldErrors().size());
137+
Assertions.assertNull(prototype.getMainGroup());
138+
Assertions.assertEquals(0, prototype.getGroups().size());
139+
}
140+
141+
@Test
142+
void showldExecuteCreateNewContentWithErrorOnPreferences() throws Exception {
143+
action.setContentTypeCode("XXX");
144+
Content prototype = new Content();
145+
Mockito.when(this.contentManager.createContentType("XXX")).thenReturn(prototype);
146+
Mockito.when(this.userPreferencesManager.getUserPreferences(Mockito.anyString())).thenThrow(new EntException("Error"));
147+
String result = this.action.createNew();
148+
Assertions.assertEquals(BaseAction.FAILURE, result);
149+
Assertions.assertEquals(0, action.getFieldErrors().size());
150+
Assertions.assertNull(prototype.getMainGroup());
151+
Assertions.assertEquals(0, prototype.getGroups().size());
152+
}
153+
154+
}

0 commit comments

Comments
 (0)