MDL-52831 users: Automatically strip whitespace from name fields

This change prevents from registering and/or saving the profile of a
user with the whitespace instead of the required name.

Additionally, there is accessibility improvement for better error labels
in case of missing values (copied over from signup_form).
This commit is contained in:
David Mudrák
2016-01-19 21:32:42 +01:00
parent 6a01418aa1
commit 73f2ca5411
5 changed files with 73 additions and 1 deletions
+5
View File
@@ -112,6 +112,11 @@ class login_signup_form extends moodleform {
function definition_after_data(){
$mform = $this->_form;
$mform->applyFilter('username', 'trim');
// Trim required name fields.
foreach (useredit_get_required_name_fields() as $field) {
$mform->applyFilter($field, 'trim');
}
}
function validation($data, $files) {
+5
View File
@@ -99,6 +99,11 @@ class user_edit_form extends moodleform {
$mform = $this->_form;
$userid = $mform->getElementValue('id');
// Trim required name fields.
foreach (useredit_get_required_name_fields() as $field) {
$mform->applyFilter($field, 'trim');
}
if ($user = $DB->get_record('user', array('id' => $userid))) {
// Remove description.
+6
View File
@@ -140,6 +140,12 @@ class user_editadvanced_form extends moodleform {
global $USER, $CFG, $DB, $OUTPUT;
$mform = $this->_form;
// Trim required name fields.
foreach (useredit_get_required_name_fields() as $field) {
$mform->applyFilter($field, 'trim');
}
if ($userid = $mform->getElementValue('id')) {
$user = $DB->get_record('user', array('id' => $userid));
} else {
+7 -1
View File
@@ -285,11 +285,17 @@ function useredit_shared_definition(&$mform, $editoroptions, $filemanageroptions
}
$strrequired = get_string('required');
$stringman = get_string_manager();
// Add the necessary names.
foreach (useredit_get_required_name_fields() as $fullname) {
$mform->addElement('text', $fullname, get_string($fullname), 'maxlength="100" size="30"');
$mform->addRule($fullname, $strrequired, 'required', null, 'client');
if ($stringman->string_exists('missing'.$fullname, 'core')) {
$strmissingfield = get_string('missing'.$fullname, 'core');
} else {
$strmissingfield = $strrequired;
}
$mform->addRule($fullname, $strmissingfield, 'required', null, 'client');
$mform->setType($fullname, PARAM_NOTAGS);
}
+50
View File
@@ -0,0 +1,50 @@
@core @core_user
Feature: Both first name and surname are always available for every user
In order to easily identify and display users on Moodle pages
As any user
I need to rely on both first name and surname are always available
Scenario: Attempting to self-register as a new user with empty names
Given the following config values are set as admin:
| registerauth | email |
| passwordpolicy | 0 |
And I am on site homepage
And I follow "Log in"
And I follow "New Account"
When I set the following fields to these values:
| Username | mrwhitespace |
| Password | Gue$$m3ifY0uC&n |
| Email address | mrwhitespace@nas.ty |
| Email (again) | mrwhitespace@nas.ty |
And I set the field "First name" to " "
And I set the field "Surname" to " "
And I click on "Create my new account" "button"
Then I should see "Missing given name"
And I should see "Missing surname"
Scenario: Attempting to change own names to whitespace
Given the following "users" exist:
| username | firstname | lastname | email |
| foobar | Foo | Bar | foo@bar.com |
And I log in as "foobar"
And I follow "Profile" in the user menu
And I follow "Edit profile"
When I set the field "First name" to " "
And I set the field "Surname" to " "
And I click on "Update profile" "button"
Then I should see "Missing given name"
And I should see "Missing surname"
Scenario: Attempting to change someone else's names to whitespace
Given the following "users" exist:
| username | firstname | lastname | email |
| foobar | Foo | Bar | foo@bar.com |
And I log in as "admin"
And I navigate to "Browse list of users" node in "Site administration > Users > Accounts"
And I follow "Foo Bar"
And I follow "Edit profile"
When I set the field "First name" to " "
And I set the field "Surname" to " "
And I click on "Update profile" "button"
Then I should see "Missing given name"
And I should see "Missing surname"