MDL-45242 Admin: User list supports custom profile fields

This commit is contained in:
sam marshall
2021-03-10 10:57:11 +00:00
parent 558cc1b85e
commit dbc09f74e9
4 changed files with 144 additions and 22 deletions
+96
View File
@@ -746,4 +746,100 @@ class core_datalib_testcase extends advanced_testcase {
"Please also make sure \$CFG->maxcoursesincategory * MAX_COURSE_CATEGORIES less than max integer. " .
"See tracker issues: MDL-25669 and MDL-69573");
}
/**
* Tests the get_users_listing function.
*/
public function test_get_users_listing(): void {
global $DB;
$this->resetAfterTest();
$generator = $this->getDataGenerator();
// Set up profile field.
$generator->create_custom_profile_field(['datatype' => 'text',
'shortname' => 'specialid', 'name' => 'Special user id']);
// Set up the show user identity option.
set_config('showuseridentity', 'department,profile_field_specialid');
// Get all the existing user ids (we're going to remove these from test results).
$existingids = array_fill_keys($DB->get_fieldset_select('user', 'id', '1 = 1'), true);
// Create some test user accounts.
$userids = [];
foreach (['a', 'b', 'c', 'd'] as $key) {
$record = [
'username' => 'user_' . $key,
'firstname' => $key . '_first',
'lastname' => 'last_' . $key,
'department' => 'department_' . $key,
'profile_field_specialid' => 'special_' . $key,
'lastaccess' => ord($key)
];
$user = $generator->create_user($record);
$userids[] = $user->id;
}
// Check default result with no parameters.
$results = get_users_listing();
$results = array_diff_key($results, $existingids);
// It should return all the results in order.
$this->assertEquals($userids, array_keys($results));
// Results should have some general fields and name fields, check some samples.
$this->assertEquals('user_a', $results[$userids[0]]->username);
$this->assertEquals('[email protected]', $results[$userids[0]]->email);
$this->assertEquals(1, $results[$userids[0]]->confirmed);
$this->assertEquals('a_first', $results[$userids[0]]->firstname);
$this->assertObjectHasAttribute('firstnamephonetic', $results[$userids[0]]);
// Should not have the custom field or department because no context specified.
$this->assertObjectNotHasAttribute('department', $results[$userids[0]]);
$this->assertObjectNotHasAttribute('profile_field_specialid', $results[$userids[0]]);
// Check sorting.
$results = get_users_listing('username', 'DESC');
$results = array_diff_key($results, $existingids);
$this->assertEquals([$userids[3], $userids[2], $userids[1], $userids[0]], array_keys($results));
// Add the options to showuseridentity and check it returns those fields but only if you
// specify a context AND have permissions.
$results = get_users_listing('lastaccess', 'asc', 0, 0, '', '', '', '', null,
\context_system::instance());
$this->assertObjectNotHasAttribute('department', $results[$userids[0]]);
$this->assertObjectNotHasAttribute('profile_field_specialid', $results[$userids[0]]);
$this->setAdminUser();
$results = get_users_listing('lastaccess', 'asc', 0, 0, '', '', '', '', null,
\context_system::instance());
$this->assertEquals('department_a', $results[$userids[0]]->department);
$this->assertEquals('special_a', $results[$userids[0]]->profile_field_specialid);
// Check search (full name, email, username).
$results = get_users_listing('lastaccess', 'asc', 0, 0, 'b_first last_b');
$this->assertEquals([$userids[1]], array_keys($results));
$results = get_users_listing('lastaccess', 'asc', 0, 0, 'c@example');
$this->assertEquals([$userids[2]], array_keys($results));
$results = get_users_listing('lastaccess', 'asc', 0, 0, 'user_d');
$this->assertEquals([$userids[3]], array_keys($results));
// Check first and last initial restriction (all the test ones have same last initial).
$results = get_users_listing('lastaccess', 'asc', 0, 0, '', 'C');
$this->assertEquals([$userids[2]], array_keys($results));
$results = get_users_listing('lastaccess', 'asc', 0, 0, '', '', 'L');
$results = array_diff_key($results, $existingids);
$this->assertEquals($userids, array_keys($results));
// Check the extra where clause, either with the 'u.' prefix or not.
$results = get_users_listing('lastaccess', 'asc', 0, 0, '', '', '', 'id IN (:x,:y)',
['x' => $userids[1], 'y' => $userids[3]]);
$results = array_diff_key($results, $existingids);
$this->assertEquals([$userids[1], $userids[3]], array_keys($results));
$results = get_users_listing('lastaccess', 'asc', 0, 0, '', '', '', 'u.id IN (:x,:y)',
['x' => $userids[1], 'y' => $userids[3]]);
$results = array_diff_key($results, $existingids);
$this->assertEquals([$userids[1], $userids[3]], array_keys($results));
}
}