Merge branch 'MDL-42816-master' of https://github.com/StudiUM/moodle

This commit is contained in:
Eloy Lafuente (stronk7) 2014-04-02 01:52:41 +02:00
commit 57c92af7ad
7 changed files with 309 additions and 6 deletions

View file

@ -79,6 +79,31 @@ class core_user {
}
}
/**
* Return user object from db based on their username.
*
* @param string $username The username of the user searched.
* @param string $fields A comma separated list of user fields to be returned, support and noreply user.
* @param int $mnethostid The id of the remote host.
* @param int $strictness IGNORE_MISSING means compatible mode, false returned if user not found, debug message if more found;
* IGNORE_MULTIPLE means return first user, ignore multiple user records found(not recommended);
* MUST_EXIST means throw an exception if no user record or multiple records found.
* @return stdClass|bool user record if found, else false.
* @throws dml_exception if user record not found and respective $strictness is set.
*/
public static function get_user_by_username($username, $fields = '*', $mnethostid = null, $strictness = IGNORE_MISSING) {
global $DB, $CFG;
// Because we use the username as the search criteria, we must also restrict our search based on mnet host.
if (empty($mnethostid)) {
// If empty, we restrict to local users.
$mnethostid = $CFG->mnet_localhost_id;
}
return $DB->get_record('user', array('username' => $username, 'mnethostid' => $mnethostid), $fields, $strictness);
}
/**
* Helper function to return dummy noreply user record.
*

View file

@ -226,7 +226,10 @@ EOD;
$record['deleted'] = 0;
}
$record['timecreated'] = time();
if (!isset($record['timecreated'])) {
$record['timecreated'] = time();
}
$record['timemodified'] = $record['timecreated'];
$record['lastip'] = '0.0.0.0';

View file

@ -31,10 +31,16 @@
*/
class core_user_testcase extends advanced_testcase {
/**
* Setup test data.
*/
protected function setUp() {
$this->resetAfterTest(true);
}
public function test_get_user() {
global $CFG;
$this->resetAfterTest(true);
// Create user and try fetach it with api.
$user = $this->getDataGenerator()->create_user();
@ -78,4 +84,36 @@ class core_user_testcase extends advanced_testcase {
$this->assertEquals($user, $supportuser);
$this->assertTrue(core_user::is_real_user($supportuser->id));
}
/**
* Test get_user_by_username method.
*/
public function test_get_user_by_username() {
$record = array();
$record['username'] = 'johndoe';
$record['email'] = 'johndoe@example.com';
$record['timecreated'] = time();
// Create a default user for the test.
$userexpected = $this->getDataGenerator()->create_user($record);
// Assert that the returned user is the espected one.
$this->assertEquals($userexpected, core_user::get_user_by_username('johndoe'));
// Assert that a subset of fields is correctly returned.
$this->assertEquals((object) $record, core_user::get_user_by_username('johndoe', 'username,email,timecreated'));
// Assert that a user with a different mnethostid will no be returned.
$this->assertFalse(core_user::get_user_by_username('johndoe', 'username,email,timecreated', 2));
// Create a new user from a different host.
$record['mnethostid'] = 2;
$userexpected2 = $this->getDataGenerator()->create_user($record);
// Assert that the new user is returned when specified the correct mnethostid.
$this->assertEquals($userexpected2, core_user::get_user_by_username('johndoe', '*', 2));
// Assert that a user not in the db return false.
$this->assertFalse(core_user::get_user_by_username('janedoe'));
}
}