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

1532 Do not allow words that are digits #1583

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
a433132
1532 Do not allow words that are digits
tomaszsmy Jul 9, 2022
fed3ea0
Merge branch 'main' into 1532_Do_not_allow_words_that_are_digits
nya-elimu Jul 11, 2022
1490342
Merge branch 'main' into 1532_Do_not_allow_words_that_are_digits
nya-elimu Jul 11, 2022
f1d8a6a
Merge branch 'main' into 1532_Do_not_allow_words_that_are_digits
nya-elimu Jul 11, 2022
e425202
Merge branch 'main' into 1532_Do_not_allow_words_that_are_digits
nya-elimu Jul 11, 2022
c76e817
Merge branch 'main' into 1532_Do_not_allow_words_that_are_digits
nya-elimu Jul 12, 2022
3754339
Merge branch 'main' into 1532_Do_not_allow_words_that_are_digits
nya-elimu Jul 20, 2022
0644d26
Merge branch 'main' into 1532_Do_not_allow_words_that_are_digits
nya-elimu Jul 20, 2022
e744533
Merge branch 'main' into 1532_Do_not_allow_words_that_are_digits
tomaszsmy Jul 21, 2022
fcd512d
1532 Do not allow words that are digits
tomaszsmy Jul 21, 2022
297e9f2
Merge branch 'main' into 1532_Do_not_allow_words_that_are_digits
nya-elimu Jul 22, 2022
f221ee0
Merge branch 'main' into 1532_Do_not_allow_words_that_are_digits
nya-elimu Jul 22, 2022
9418962
1532 Do not allow words that are digits
tomaszsmy Jul 22, 2022
7600e9b
Merge branch 'main' into 1532_Do_not_allow_words_that_are_digits
tomaszsmy Jul 24, 2022
8357c9d
1532 Do not allow words that are digits
tomaszsmy Jul 24, 2022
9e81b60
1532 Do not allow words that are digits
tomaszsmy Jul 25, 2022
b97b2ec
Merge branch 'main' into 1532_Do_not_allow_words_that_are_digits
nya-elimu Aug 5, 2022
8d97ff3
Merge branch 'main' into 1532_Do_not_allow_words_that_are_digits
tomaszsmy Aug 26, 2022
e5c5774
1532 Do not allow words that are digits
tomaszsmy Aug 26, 2022
bb1e164
Merge remote-tracking branch 'origin/main' into 1532_Do_not_allow_wor…
tomaszsmy Jan 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,7 @@ public String handleSubmit(
Model model) {
logger.info("handleSubmit");

Word existingWord = wordDao.readByText(word.getText());
if (existingWord != null) {
result.rejectValue("text", "NonUnique");
}

if (StringUtils.containsAny(word.getText(), " ")) {
result.rejectValue("text", "WordSpace");
}
validateWord(word, result);

if (result.hasErrors()) {
model.addAttribute("word", word);
Expand Down Expand Up @@ -272,4 +265,20 @@ private void autoSelectLetterSoundCorrespondences(Word word) {

word.setLetterSoundCorrespondences(letterSoundCorrespondences);
}

private void validateWord(Word word, BindingResult result) {
tomaszsmy marked this conversation as resolved.
Show resolved Hide resolved
Word existingWord = wordDao.readByText(word.getText());

if (existingWord != null) {
result.rejectValue("text", "NonUnique");
}

if (StringUtils.containsAny(word.getText(), " ")) {
result.rejectValue("text", "WordSpace");
}

if (word.getText() != null && word.getText().matches(".*[0-9].*")) {
result.rejectValue("text", "WordNumbers");
}
}
}
1 change: 1 addition & 0 deletions src/main/webapp/WEB-INF/i18n/errors_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ formatHint.Integer=Number (e.g. "5000")
image.too.small=The image width must be at least 640px
emoji.unicode.version=Only emojis up to Unicode version 9
WordSpace=Spaces are not allowed
WordNumbers=A word cannot consist of numbers
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package ai.elimu.web.content.word;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -15,6 +13,12 @@
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import java.util.List;

import static org.junit.Assert.*;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={
Expand Down Expand Up @@ -54,4 +58,45 @@ public void testHandleSubmit_emptyText() throws Exception {
// assertEquals(HttpStatus.BAD_REQUEST.value(), mvcResult.getResponse().getStatus());
// assertEquals("content/word/create", mvcResult.getModelAndView().getViewName());
}

@Test
public void testValidateDigitsInWord() throws Exception {
assertTrue(containGivenErrorCode(getBindingResult(getRequestWithSpecificText("10")).getAllErrors(), "WordNumbers"));
tomaszsmy marked this conversation as resolved.
Show resolved Hide resolved
assertTrue(containGivenErrorCode(getBindingResult(getRequestWithSpecificText("'10'")).getAllErrors(), "WordNumbers"));
assertTrue(containGivenErrorCode(getBindingResult(getRequestWithSpecificText("\\\"10\\\"")).getAllErrors(), "WordNumbers"));
assertTrue(containGivenErrorCode(getBindingResult(getRequestWithSpecificText("Test10Test")).getAllErrors(), "WordNumbers"));
tomaszsmy marked this conversation as resolved.
Show resolved Hide resolved
assertTrue(containGivenErrorCode(getBindingResult(getRequestWithSpecificText("Test 10 Test")).getAllErrors(), "WordNumbers"));
}

@Test
public void testValidateSpacesInWord() throws Exception {
assertTrue(containGivenErrorCode(getBindingResult(getRequestWithSpecificText("Test Test")).getAllErrors(), "WordSpace"));
assertTrue(containGivenErrorCode(getBindingResult(getRequestWithSpecificText(" Test")).getAllErrors(), "WordSpace"));
assertTrue(containGivenErrorCode(getBindingResult(getRequestWithSpecificText("Test ")).getAllErrors(), "WordSpace"));
}

private RequestBuilder getRequestWithSpecificText(String text) {
return MockMvcRequestBuilders
.post("/content/word/create")
.param("timeStart", String.valueOf(System.currentTimeMillis()))
.param("text", text)
.contentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE);
}

private BindingResult getBindingResult(RequestBuilder requestBuilder) throws Exception {
MvcResult mvcResult = mockMvc.perform(requestBuilder).andReturn();
return (BindingResult) mvcResult.getModelAndView().getModel().get("org.springframework.validation.BindingResult.word");
nya-elimu marked this conversation as resolved.
Show resolved Hide resolved
}

private boolean containGivenErrorCode(List<ObjectError> objectErrorList, String error) {
for (ObjectError objectError : objectErrorList) {
for (String errorCode : objectError.getCodes()) {
if (errorCode.equals(error)) {
return true;
}
}
}
return false;
}

}