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 all 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 @@ -121,7 +121,8 @@
if (StringUtils.containsAny(word.getText(), " ")) {
result.rejectValue("text", "WordSpace");
}

validateWord(word, result);

if (result.hasErrors()) {
model.addAttribute("word", word);
model.addAttribute("timeStart", request.getParameter("timeStart"));
Expand Down Expand Up @@ -276,4 +277,20 @@

word.setLetterSoundCorrespondences(letterSoundCorrespondences);
}

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

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

Check warning on line 285 in src/main/java/ai/elimu/web/content/word/WordCreateController.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/ai/elimu/web/content/word/WordCreateController.java#L285

Added line #L285 was not covered by tests
}

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

if (word.getText() != null && word.getText().matches("[0-9\\W_]*")) {
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
Expand Up @@ -19,6 +19,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 All @@ -34,7 +40,7 @@ public class WordCreateControllerTest {

@Autowired
private WordDao wordDao;

@Before
public void setup() {
assertNotNull(wordCreateController);
Expand All @@ -61,29 +67,82 @@ public void testHandleSubmit_emptyText() throws Exception {
// assertEquals(HttpStatus.BAD_REQUEST.value(), mvcResult.getResponse().getStatus());
// assertEquals("content/word/create", mvcResult.getModelAndView().getViewName());
}

@Test
public void testHandleSubmit_success() throws Exception {
Word wordHello = wordDao.readByText("hello");
assertThat(wordHello, is(nullValue()));

int numberOfWordsBefore = wordDao.readAll().size();

RequestBuilder requestBuilder = MockMvcRequestBuilders
.post("/content/word/create")
.param("timeStart", String.valueOf(System.currentTimeMillis()))
.param("text", "hello")
.contentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE);
MvcResult mvcResult = mockMvc.perform(requestBuilder).andReturn();

wordHello = wordDao.readByText("hello");
assertThat(wordHello, not(nullValue()));
assertThat(wordHello.getText(), is("hello"));

int numberOfWordsAfter = wordDao.readAll().size();
assertThat(numberOfWordsAfter, is(numberOfWordsBefore + 1));

assertEquals(HttpStatus.MOVED_TEMPORARILY.value(), mvcResult.getResponse().getStatus());
assertEquals("redirect:/content/word/list#" + wordHello.getId(), 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("[10]")).getAllErrors(), "WordNumbers"));
assertTrue(containGivenErrorCode(getBindingResult(getRequestWithSpecificText("1'0")).getAllErrors(), "WordNumbers"));
assertTrue(containGivenErrorCode(getBindingResult(getRequestWithSpecificText("1-0")).getAllErrors(), "WordNumbers"));
assertTrue(containGivenErrorCode(getBindingResult(getRequestWithSpecificText("1*0")).getAllErrors(), "WordNumbers"));
assertTrue(containGivenErrorCode(getBindingResult(getRequestWithSpecificText("1/0")).getAllErrors(), "WordNumbers"));
assertTrue(containGivenErrorCode(getBindingResult(getRequestWithSpecificText("1!0")).getAllErrors(), "WordNumbers"));
assertTrue(containGivenErrorCode(getBindingResult(getRequestWithSpecificText("1#0")).getAllErrors(), "WordNumbers"));
assertTrue(containGivenErrorCode(getBindingResult(getRequestWithSpecificText("1%0")).getAllErrors(), "WordNumbers"));
assertTrue(containGivenErrorCode(getBindingResult(getRequestWithSpecificText("1()0")).getAllErrors(), "WordNumbers"));
assertTrue(containGivenErrorCode(getBindingResult(getRequestWithSpecificText("1_0")).getAllErrors(), "WordNumbers"));
assertTrue(containGivenErrorCode(getBindingResult(getRequestWithSpecificText("1+0")).getAllErrors(), "WordNumbers"));
assertTrue(containGivenErrorCode(getBindingResult(getRequestWithSpecificText("1{}0")).getAllErrors(), "WordNumbers"));
assertTrue(containGivenErrorCode(getBindingResult(getRequestWithSpecificText("1|?><.,0")).getAllErrors(), "WordNumbers"));
assertFalse(containGivenErrorCode(getBindingResult(getRequestWithSpecificText("10th")).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");
}

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;
}

}