diff --git a/src/main/java/org/olat/user/propertyhandlers/PhonePropertyHandler.java b/src/main/java/org/olat/user/propertyhandlers/PhonePropertyHandler.java index 6892343c0b8c810c3b51a1620f55779ad791bb76..7fe769b3e14d3bbdc7569b7367b3bee3fa7d11f8 100644 --- a/src/main/java/org/olat/user/propertyhandlers/PhonePropertyHandler.java +++ b/src/main/java/org/olat/user/propertyhandlers/PhonePropertyHandler.java @@ -40,7 +40,7 @@ import org.olat.core.util.StringHelper; public class PhonePropertyHandler extends Generic127CharTextPropertyHandler { // Regexp to define valid phone numbers - private static final Pattern VALID_PHONE_PATTERN_IP = Pattern.compile( "[0-9/\\-+' ]+" ); + private static final Pattern VALID_PHONE_PATTERN_IP = Pattern.compile( "[0-9/\\-+'\\(\\)\\. e(ext\\.*)(extension)]+" ); /* (non-Javadoc) * @see org.olat.user.propertyhandlers.Generic127CharTextPropertyHandler#addFormItem(java.util.Locale, org.olat.core.id.User, java.lang.String, boolean, org.olat.core.gui.components.form.flexible.FormItemContainer) @@ -85,7 +85,7 @@ public class PhonePropertyHandler extends Generic127CharTextPropertyHandler { if (StringHelper.containsNonWhitespace(value)) { // check phone address syntax - if (!VALID_PHONE_PATTERN_IP.matcher(value).matches()) { + if (!VALID_PHONE_PATTERN_IP.matcher(value.toLowerCase()).matches()) { formItem.setErrorKey(i18nFormElementLabelKey() + ".error.valid", null); return false; } @@ -103,7 +103,7 @@ public class PhonePropertyHandler extends Generic127CharTextPropertyHandler { if (StringHelper.containsNonWhitespace(value)) { // check phone address syntax - if ( ! VALID_PHONE_PATTERN_IP.matcher(value).matches()) { + if ( ! VALID_PHONE_PATTERN_IP.matcher(value.toLowerCase()).matches()) { validationError.setErrorKey(i18nFormElementLabelKey()+ ".error.valid"); return false; } diff --git a/src/test/java/org/olat/user/UserPropertiesTest.java b/src/test/java/org/olat/user/UserPropertiesTest.java index 671390d835d0ab61d6fea1cf0b2e9833a87eee1f..464be7d17b49744e877929a3e7e413c4f9c5559e 100644 --- a/src/test/java/org/olat/user/UserPropertiesTest.java +++ b/src/test/java/org/olat/user/UserPropertiesTest.java @@ -19,11 +19,12 @@ */ package org.olat.user; -import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import org.junit.Test; import org.olat.core.gui.components.form.ValidationError; +import org.olat.user.propertyhandlers.PhonePropertyHandler; import org.olat.user.propertyhandlers.URLPropertyHandler; /** @@ -44,19 +45,37 @@ public class UserPropertiesTest { URLPropertyHandler urlHandler = new URLPropertyHandler(); ValidationError error = new ValidationError(); - boolean valid1 = urlHandler.isValidValue(null, "http://www.openolat.org", error, null); - assertTrue(valid1); - - boolean valid2 = urlHandler.isValidValue(null, "http://test.ch", error, null); - assertTrue(valid2); - - boolean valid3 = urlHandler.isValidValue(null, "http://localhost", error, null); - assertTrue(valid3); - - boolean invalid1 = urlHandler.isValidValue(null, "http:www.openolat.org", error, null); - assertFalse(invalid1); + // test for valid URL's + assertTrue(urlHandler.isValidValue(null, "http://www.openolat.org", error, null)); + assertTrue(urlHandler.isValidValue(null, "https://www.openolat.org", error, null)); + assertTrue(urlHandler.isValidValue(null, "http://test.ch", error, null)); + assertTrue(urlHandler.isValidValue(null, "http://localhost", error, null)); + // test for invalid URL's + assertFalse(urlHandler.isValidValue(null, "http:www.openolat.org", error, null)); + assertFalse(urlHandler.isValidValue(null, "www.openolat.org", error, null)); } - + @Test + public void testPhonePropertyHandlerValidation() { + + PhonePropertyHandler phoneHandler = new PhonePropertyHandler(); + ValidationError error = new ValidationError(); + // test for valid phone number formats + assertTrue(phoneHandler.isValidValue(null, "043 544 90 00", error, null)); + assertTrue(phoneHandler.isValidValue(null, "043/544'90'00", error, null)); + assertTrue(phoneHandler.isValidValue(null, "043/544'90'00", error, null)); + assertTrue(phoneHandler.isValidValue(null, "043-544-90-00", error, null)); + assertTrue(phoneHandler.isValidValue(null, "043.544.90.00", error, null)); + assertTrue(phoneHandler.isValidValue(null, "+41 43 544 90 00", error, null)); + assertTrue(phoneHandler.isValidValue(null, "+41 (0)43 544 90 00", error, null)); + assertTrue(phoneHandler.isValidValue(null, "+41 43 544 90 00 x0", error, null)); + assertTrue(phoneHandler.isValidValue(null, "+41 43 544 90 00 ext. 0", error, null)); + assertTrue(phoneHandler.isValidValue(null, "+41 43 544 90 00 ext0", error, null)); + assertTrue(phoneHandler.isValidValue(null, "+41 43 544 90 00 ext 0", error, null)); + assertTrue(phoneHandler.isValidValue(null, "+41 43 544 90 00 extension 0", error, null)); + assertTrue(phoneHandler.isValidValue(null, "+41 43 544 90 00 Extension 0", error, null)); + // test for invalid phone number formats + assertFalse(phoneHandler.isValidValue(null, "+41 43 frentix GmbH", error, null)); + } } \ No newline at end of file