From 94aa2c78bd01feaca3893fbcf8068bdde186bda2 Mon Sep 17 00:00:00 2001 From: Peter Burnett Date: Mon, 6 Jun 2022 18:43:36 +1000 Subject: [PATCH] MDL-74435 user: Trim whitespace from user search fields --- user/filters/text.php | 6 +++++ user/tests/behat/filter_trim.feature | 37 ++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 user/tests/behat/filter_trim.feature diff --git a/user/filters/text.php b/user/filters/text.php index 0fc4a684f3e..e8d71acb523 100644 --- a/user/filters/text.php +++ b/user/filters/text.php @@ -105,6 +105,12 @@ class user_filter_text extends user_filter_type { $fieldvalue = null; if (isset($formdata->$field)) { $fieldvalue = $formdata->$field; + + // If we aren't doing a whitespace comparison, an exact match, trim will give us a better result set. + $trimmed = trim($fieldvalue); + if ($trimmed !== '' && $formdata->$operator != 2) { + $fieldvalue = $trimmed; + } } return array('operator' => (int)$formdata->$operator, 'value' => $fieldvalue); } diff --git a/user/tests/behat/filter_trim.feature b/user/tests/behat/filter_trim.feature new file mode 100644 index 00000000000..df22dc75543 --- /dev/null +++ b/user/tests/behat/filter_trim.feature @@ -0,0 +1,37 @@ +@core @core_user +Feature: Trim entered user filters + As a system administrator + I need to be able to filter users ignoring whitespace + So that I can find users even when entered data has surrounding whitespace. + + Background: + Given the following "users" exist: + | username | firstname | lastname | email | idnumber | + | teacher1 | Teacher | 1 | teacher@example.com | 0000002 | + | student1 | Student1 | 1 | student1@example.com | 0000003 | + And I log in as "admin" + And I navigate to "Users > Accounts > Browse list of users" in site administration + + @javascript + Scenario: Filtering username - with case "contains" + When I set the field "id_realname_op" to "contains" + And I set the field "id_realname" to " Teacher " + And I press "Add filter" + # We should see the teacher user, with the trimmmed string present. + Then I should see "User full name contains \"Teacher\"" + And I should see "Teacher" in the "users" "table" + And I should not see "Student1" in the "users" "table" + + @javascript + Scenario: Filtering username - with case "contains" and a whitespace string + When I set the field "id_realname_op" to "contains" + And I set the field "id_realname" to " " + And I press "Add filter" + Then I should see "User full name contains \" \"" + + @javascript + Scenario: Filtering username - with case "is equal to" + When I set the field "id_realname_op" to "is equal to" + And I set the field "id_realname" to " Teacher" + And I press "Add filter" + Then I should see "User full name is equal to \" Teacher\""