Skip to content

Commit

Permalink
Merge pull request #934 from entando/ENG-1184
Browse files Browse the repository at this point in the history
ENG-1184: Validation improvement for category and widget titles (v5.3.1)
  • Loading branch information
eugeniosant authored Sep 4, 2020
2 parents f068576 + e239678 commit aa27834
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,14 @@ private void checkTitles(boolean checkErrors) {
this.getTitles().put(lang.getCode(), title.trim());
}
if (checkErrors) {
if (null == title || title.trim().length() == 0) {
int maxLength = 70;
if (StringUtils.isBlank(title)) {
String[] args = {lang.getDescr()};
this.addFieldError(titleKey, this.getText("error.category.insertTitle", args));
}
} else if (null != title && title.trim().length() > maxLength) {
String[] args = {lang.getDescr(), String.valueOf(maxLength)};
this.addFieldError(titleKey, this.getText("error.category.title.wrongTitleLength", args));
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ error.category.deleteWithChildren.notAllowed=Sorry. You can''t delete a category
#VALIDATOR'S LABELS
categoryCode=Category code
error.category.insertTitle=Please provide a category title in ''{0}''
error.category.title.wrongTitleLength=The field (''{0}'') Title is longer than ''{1}'' characters
error.category.duplicateCode=A category with the code ''{0}'' already exists
error.category.noParentSelected=No parent category selected!
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ error.category.deleteWithChildren.notAllowed=Non è consentita l''eliminaz
#VALIDATOR'S LABELS
categoryCode=Codice Categoria
error.category.insertTitle=Titolo per lingua ''{0}'' è richiesto
error.category.title.wrongTitleLength=Il campo Titolo (''{0}'') ha più di ''{1}'' caratteri
error.category.duplicateCode=Codice categoria ''{0}'' già presente
error.category.noParentSelected=Nessuna categoria padre selezionata!
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,22 @@
<field-validator type="requiredstring">
<message key="requiredstring" />
</field-validator>
<field-validator type="stringlength" short-circuit="true">
<param name="maxLength">100</param>
<param name="trim">true</param>
<message key="wrongMaxLength" />
</field-validator>
</field>

<field name="italianTitle">
<field-validator type="requiredstring">
<message key="requiredstring" />
</field-validator>
<field-validator type="stringlength" short-circuit="true">
<param name="maxLength">100</param>
<param name="trim">true</param>
<message key="wrongMaxLength" />
</field-validator>
</field>

</validators>
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,7 @@ public class WidgetTypeAction extends AbstractPortalAction {
public void validate() {
super.validate();
try {
if (this.getStrutsAction() == ApsAdminSystemConstants.ADD) {
WidgetType type = this.getWidgetType(this.getWidgetTypeCode());
if (null != type) {
this.addFieldError("widgetTypeCode", this.getText("error.widgetType.guiRequired"));
}
} else if (this.getStrutsAction() == ApsAdminSystemConstants.PASTE) {
if (this.getStrutsAction() == ApsAdminSystemConstants.PASTE) {
this.checkWidgetToCopy();
} else if (this.getStrutsAction() == NEW_USER_WIDGET) {
this.checkNewUserWidget();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void testViewTree() throws Throwable {
}

public void testValidateAddCategory_1() throws Throwable {
Map<String, String> params = new HashMap<String, String>();
Map<String, String> params = new HashMap<>();
params.put("parentCategoryCode", this._categoryManager.getRoot().getCode());
params.put("strutsAction", "1");
params.put("categoryCode", "");
Expand All @@ -108,7 +108,7 @@ public void testValidateAddCategory_1() throws Throwable {
public void testValidateAddCategory_2() throws Throwable {
String categoryCode = "veryLongCategoryCode_veryLongCategoryCode";
assertNull(this._categoryManager.getCategory(categoryCode));
Map<String, String> params = new HashMap<String, String>();
Map<String, String> params = new HashMap<>();
params.put("parentCategoryCode", this._categoryManager.getRoot().getCode());
params.put("strutsAction", "1");
params.put("categoryCode", categoryCode);//long category code
Expand All @@ -123,7 +123,7 @@ public void testValidateAddCategory_2() throws Throwable {

public void testValidateAddCategory_3() throws Throwable {
assertNotNull(this._categoryManager.getCategory("evento"));
Map<String, String> params = new HashMap<String, String>();
Map<String, String> params = new HashMap<>();
params.put("parentCategoryCode", this._categoryManager.getRoot().getCode());
params.put("strutsAction", "1");
params.put("categoryCode", "evento");//duplicate Code
Expand All @@ -140,7 +140,7 @@ public void testValidateAddCategory_4() throws Throwable {
String categoryCode = "cat_temp2";
assertNull(this._categoryManager.getCategory(categoryCode));
try {
Map<String, String> params = new HashMap<String, String>();
Map<String, String> params = new HashMap<>();
params.put("parentCategoryCode", this._categoryManager.getRoot().getCode());
params.put("strutsAction", "1");
params.put("categoryCode", categoryCode);
Expand All @@ -151,6 +151,15 @@ public void testValidateAddCategory_4() throws Throwable {
Map<String, List<String>> fieldErrors = this.getAction().getFieldErrors();
assertEquals(1, fieldErrors.size());
assertEquals(1, fieldErrors.get("langen").size());

String longCategoryTitle = "Titolo di lunghezza maggiore di 70 caratteri - abcdefghi lmnopq rstuvz 1234567890";
params.put("langen", "Category title");
params.put("langit", longCategoryTitle);
result = this.executeSaveCategory("admin", params);
assertEquals(Action.INPUT, result);
fieldErrors = this.getAction().getFieldErrors();
assertEquals(1, fieldErrors.size());
assertEquals(1, fieldErrors.get("langit").size());
} catch (Throwable t) {
this._categoryManager.deleteCategory(categoryCode);
assertNotNull(this._categoryManager.getCategory(categoryCode));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Arrays;
import java.util.Collections;
import org.entando.entando.aps.system.services.guifragment.GuiFragment;

import org.entando.entando.aps.system.services.guifragment.IGuiFragmentManager;
Expand All @@ -50,6 +51,63 @@ protected void setUp() throws Exception {
this.init();
}

public void testFailureAdd_1() throws Throwable {
String[] invalidCodes = new String[]{null, "", "Code_longher_than_40_characters_1234567890",
"Code_With_Special%$_characters", "Code with spaces", "entando_apis" /* already exists */};
for (int i = 0; i < invalidCodes.length; i++) {
String invalidCode = invalidCodes[i];
try {
String result = this.execute(invalidCode, "italian title",
"english title", "admin", null, "*GUI*", ApsAdminSystemConstants.ADD);
assertEquals(Action.INPUT, result);
assertEquals(1, this.getAction().getFieldErrors().size());
assertEquals(1, this.getAction().getFieldErrors().get("widgetTypeCode").size());
} catch (Throwable t) {
if (!invalidCode.equals("entando_apis")) {
this.cleanDatabase(invalidCode);
}
throw t;
}
}
}

public void testFailureAdd_2() throws Throwable {
String widgetTypeCode = "test_widgetType_add_1";
assertNull(this._widgetTypeManager.getWidgetType(widgetTypeCode));
List<String> fragmentCodes = this._guiFragmentManager.getGuiFragmentCodesByWidgetType(widgetTypeCode);
assertEquals(0, fragmentCodes.size());
String[] invalidTitles = new String[]{null, "", String.join("", Collections.nCopies(30, "test "))};
for (int i = 0; i < invalidTitles.length; i++) {
String invalidTitle = invalidTitles[i];
try {
String result = this.execute(widgetTypeCode, "italian title",
invalidTitle, "admin", null, "*GUI*", ApsAdminSystemConstants.ADD);
assertEquals(Action.INPUT, result);
assertEquals(1, this.getAction().getFieldErrors().size());
assertEquals(1, this.getAction().getFieldErrors().get("englishTitle").size());
} catch (Throwable t) {
this.cleanDatabase(widgetTypeCode);
throw t;
}
}
}

public void testFailureAdd_3() throws Throwable {
String widgetTypeCode = "test_widgetType_add_2";
assertNull(this._widgetTypeManager.getWidgetType(widgetTypeCode));
List<String> fragmentCodes = this._guiFragmentManager.getGuiFragmentCodesByWidgetType(widgetTypeCode);
assertEquals(0, fragmentCodes.size());
try {
String result = this.execute(widgetTypeCode, "italian title",
"english title", "admin", null, "", ApsAdminSystemConstants.ADD);
assertEquals(Action.INPUT, result);
assertEquals(1, this.getAction().getFieldErrors().size());
assertEquals(1, this.getAction().getFieldErrors().get("gui").size());
} catch (Throwable t) {
this.cleanDatabase(widgetTypeCode);
throw t;
}
}
public void testFailureUpdateTitles() throws Throwable {
String result = this.executeUpdate("content_viewer", "italian title", "english title", "editorCustomers", "*GUI*");
assertEquals("userNotAllowed", result);
Expand Down Expand Up @@ -160,7 +218,7 @@ public void testUpdate_3() throws Throwable {
this.cleanDatabase(widgetTypeCode);
}
}

private void cleanDatabase(String widgetTypeCode) throws Throwable {
List<String> fragmentCodes = this._guiFragmentManager.getGuiFragmentCodesByWidgetType(widgetTypeCode);
if (null != fragmentCodes) {
Expand All @@ -174,7 +232,7 @@ private void cleanDatabase(String widgetTypeCode) throws Throwable {
}
assertNull(this._widgetTypeManager.getWidgetType(widgetTypeCode));
}

public void testFailureTrashType_1() throws Throwable {
String result = this.executeTrash("content_viewer", "editorCustomers");
assertEquals("userNotAllowed", result);
Expand Down Expand Up @@ -263,7 +321,7 @@ public void testTrashType() throws Throwable {
assertNull(this._widgetTypeManager.getWidgetType(widgetTypeCode));
}
}

private String executeJoinWidget(String pageCode, int frame, String widgetTypeCode, String username) throws Throwable {
this.setUserOnSession(username);
this.initAction("/do/Page", "joinWidget");
Expand All @@ -287,13 +345,18 @@ private String executeUpdate(String widgetTypeCode, String italianTitle, String

private String executeUpdate(String widgetTypeCode, String italianTitle,
String englishTitle, String username, ApsProperties properties, String gui) throws Throwable {
return this.execute(widgetTypeCode, italianTitle, englishTitle, username, properties, gui, ApsAdminSystemConstants.EDIT);
}

private String execute(String widgetTypeCode, String italianTitle, String englishTitle,
String username, ApsProperties properties, String gui, int strutsAction) throws Throwable {
this.setUserOnSession(username);
this.initAction("/do/Portal/WidgetType", "save");
this.addParameter("widgetTypeCode", widgetTypeCode);
this.addParameter("italianTitle", italianTitle);
this.addParameter("englishTitle", englishTitle);
this.addParameter("gui", gui);
this.addParameter("strutsAction", ApsAdminSystemConstants.EDIT);
this.addParameter("strutsAction", String.valueOf(strutsAction));
if (null != properties) {
this.addParameters(properties);
}
Expand Down Expand Up @@ -334,7 +397,7 @@ private WidgetType createNewLogicWidgetType(String code) {
type.setConfig(config);
return type;
}

public void testCopyWidgetType() throws Throwable {
String result = this.executeCopyWidgetType("editorCustomers", "customers_page", "2");
assertEquals("userNotAllowed", result);
Expand All @@ -351,7 +414,7 @@ public void testCopyWidgetType() throws Throwable {
result = this.executeCopyWidgetType("admin", "pagina_2", "2");
assertEquals(Action.SUCCESS, result);
}

private String executeCopyWidgetType(String username, String pageCode, String framePos) throws Throwable {
this.setUserOnSession(username);
this.initAction("/do/Portal/WidgetType", "copy");
Expand All @@ -360,7 +423,7 @@ private String executeCopyWidgetType(String username, String pageCode, String fr
this.addParameter("strutsAction", ApsAdminSystemConstants.PASTE);
return this.executeAction();
}

public void testNewWidgetType() throws Throwable {
String result = this.executeNewUserWidgetType("editorCustomers", "content_viewer_list");
assertEquals("userNotAllowed", result);
Expand Down Expand Up @@ -610,7 +673,7 @@ public void testAddNewWidgetType() throws Throwable {
this._widgetTypeManager.deleteWidgetType(widgetTypeCode);
}
}

//
private String executePasteUserWidgetType(String username, String code,
String englishTitle, String italianTitle, String pageCode, String framePos) throws Throwable {
Expand Down

0 comments on commit aa27834

Please sign in to comment.