MDL-71836 core_enrol: Cannot search for users by username

This commit is contained in:
sam marshall 2021-06-02 14:51:38 +01:00
parent 30b8ad51f4
commit 171313781d
2 changed files with 13 additions and 3 deletions

View File

@ -392,9 +392,13 @@ class course_enrolment_manager {
['selects' => $fieldselects, 'joins' => $fieldjoins, 'params' => $params, 'mappings' => $mappings] =
(array)$userfields->get_sql('u', true, '', '', false);
// Searchable fields are only the identity and name ones (not userpic).
$searchable = array_fill_keys($userfields->get_required_fields(
[fields::PURPOSE_IDENTITY, fields::PURPOSE_NAME]), true);
// Searchable fields are only the identity and name ones (not userpic, and without exclusions).
$searchablefields = fields::for_identity($this->context)->with_name();
$searchable = array_fill_keys($searchablefields->get_required_fields(), true);
if (array_key_exists('username', $searchable)) {
// Add the username into the mappings list from the other query, because it was excluded.
$mappings['username'] = 'u.username';
}
// Add some additional sensible conditions
$tests = array("u.id <> :guestid", 'u.deleted = 0', 'u.confirmed = 1');

View File

@ -461,6 +461,12 @@ class core_course_enrolment_manager_testcase extends advanced_testcase {
$this->assertEquals([], array_keys($users[0]));
$users = array_values($manager->get_potential_users($enrolid, 'Frogs'));
$this->assertEquals([], array_keys($users[0]));
// Search for username field (there is special handling for this one field).
set_config('showuseridentity', 'username');
$this->setAdminUser();
$users = array_values($manager->get_potential_users($enrolid, 'newuse'));
$this->assertEquals([$newuser->id], array_keys($users[0]));
}
/**