Skip to content
Snippets Groups Projects
Commit 9a99b766 authored by gnaegi's avatar gnaegi
Browse files

OO-1969 allow more phone number formats, write testcases

parent defaaeda
No related branches found
No related tags found
No related merge requests found
......@@ -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;
}
......
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment