MDL-60518 core_user: A user with ID 0 is never a real user
This commit is contained in:
committed by
Amaia Anabitarte
parent
c8b4ee45a4
commit
7dcc1e721c
@@ -517,18 +517,17 @@ class core_user {
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true is user id is greater than self::NOREPLY_USER and
|
||||
* alternatively check db.
|
||||
* Return true if user id is greater than 0 and alternatively check db.
|
||||
*
|
||||
* @param int $userid user id.
|
||||
* @param bool $checkdb if true userid will be checked in db. By default it's false, and
|
||||
* userid is compared with NOREPLY_USER for performance.
|
||||
* userid is compared with 0 for performance.
|
||||
* @return bool true is real user else false.
|
||||
*/
|
||||
public static function is_real_user($userid, $checkdb = false) {
|
||||
global $DB;
|
||||
|
||||
if ($userid < 0) {
|
||||
if ($userid <= 0) {
|
||||
return false;
|
||||
}
|
||||
if ($checkdb) {
|
||||
|
||||
@@ -732,4 +732,44 @@ class core_user_testcase extends advanced_testcase {
|
||||
$this->assertNotEquals($enuser, $xxuser);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test is_real_user method.
|
||||
*/
|
||||
public function test_is_real_user() {
|
||||
global $CFG, $USER;
|
||||
|
||||
// Real users are real users.
|
||||
$auser = $this->getDataGenerator()->create_user();
|
||||
$guest = guest_user();
|
||||
$this->assertTrue(\core_user::is_real_user($auser->id));
|
||||
$this->assertTrue(\core_user::is_real_user($auser->id, true));
|
||||
$this->assertTrue(\core_user::is_real_user($guest->id));
|
||||
$this->assertTrue(\core_user::is_real_user($guest->id, true));
|
||||
|
||||
// Non-logged in users are not real users.
|
||||
$this->assertSame(0, $USER->id, 'The non-logged in user should have an ID of 0.');
|
||||
$this->assertFalse(\core_user::is_real_user($USER->id));
|
||||
$this->assertFalse(\core_user::is_real_user($USER->id, true));
|
||||
|
||||
// Other types of logged in users are real users.
|
||||
$this->setAdminUser();
|
||||
$this->assertTrue(\core_user::is_real_user($USER->id));
|
||||
$this->assertTrue(\core_user::is_real_user($USER->id, true));
|
||||
$this->setGuestUser();
|
||||
$this->assertTrue(\core_user::is_real_user($USER->id));
|
||||
$this->assertTrue(\core_user::is_real_user($USER->id, true));
|
||||
$this->setUser($auser);
|
||||
$this->assertTrue(\core_user::is_real_user($USER->id));
|
||||
$this->assertTrue(\core_user::is_real_user($USER->id, true));
|
||||
|
||||
// Fake accounts are not real users.
|
||||
$CFG->noreplyuserid = null;
|
||||
$this->assertFalse(\core_user::is_real_user(core_user::get_noreply_user()->id));
|
||||
$this->assertFalse(\core_user::is_real_user(core_user::get_noreply_user()->id, true));
|
||||
$CFG->supportuserid = null;
|
||||
$CFG->supportemail = '[email protected]';
|
||||
$this->assertFalse(\core_user::is_real_user(core_user::get_support_user()->id));
|
||||
$this->assertFalse(\core_user::is_real_user(core_user::get_support_user()->id, true));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ information provided here is intended especially for developers.
|
||||
* New optional parameter $context for the groups_get_members_join() function and ability to filter users that are not members of
|
||||
any group. Besides, groups_get_members_ids_sql, get_enrolled_sql and get_enrolled_users now accepts -1 (USERSWITHOUTGROUP) for
|
||||
the groupid field.
|
||||
* The method core_user::is_real_user() now returns false for userid = 0 parameter
|
||||
|
||||
=== 3.5.2 ===
|
||||
|
||||
|
||||
Reference in New Issue
Block a user