From 80ca508ed117bbf54778068f24590c80dfe39128 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Niklas=20Laxstr=C3=B6m?= Date: Thu, 11 Dec 2014 09:59:28 +0100 Subject: [PATCH] Add User::equals Seems stupid omission. Title has one. Why do I need to think how to determine how to users objects point to the same user. Allows more expressive code. Also fixes a bug in multiple places where users "0" and "00" were considered equal. Change-Id: I682392e564b332b77ab489f2ad394fa2d28098a5 --- includes/User.php | 13 +++++++++++- includes/changes/RecentChange.php | 2 +- includes/diff/DifferenceEngine.php | 2 +- includes/page/Article.php | 2 +- includes/specials/SpecialUserrights.php | 4 ++-- tests/phpunit/includes/UserTest.php | 27 +++++++++++++++++++++++++ 6 files changed, 44 insertions(+), 6 deletions(-) diff --git a/includes/User.php b/includes/User.php index 7ca7d807cd..ad4ce60a45 100644 --- a/includes/User.php +++ b/includes/User.php @@ -4739,7 +4739,7 @@ class User implements IDBAccessObject { if ( $action === true ) { $action = 'byemail'; } elseif ( $action === false ) { - if ( $this->getName() == $wgUser->getName() ) { + if ( $this->equals( $wgUser ) ) { $action = 'create'; } else { $action = 'create2'; @@ -5024,4 +5024,15 @@ class User implements IDBAccessObject { return Status::newFatal( 'badaccess-group0' ); } } + + /** + * Checks if two user objects point to the same user. + * + * @since 1.25 + * @param User $user + * @return bool + */ + public function equals( User $user ) { + return $this->getName() === $user->getName(); + } } diff --git a/includes/changes/RecentChange.php b/includes/changes/RecentChange.php index c719d8da58..86cd1d766e 100644 --- a/includes/changes/RecentChange.php +++ b/includes/changes/RecentChange.php @@ -450,7 +450,7 @@ class RecentChange { } // Users without the 'autopatrol' right can't patrol their // own revisions - if ( $user->getName() == $this->getAttribute( 'rc_user_text' ) + if ( $user->getName() === $this->getAttribute( 'rc_user_text' ) && !$user->isAllowed( 'autopatrol' ) ) { $errors[] = array( 'markedaspatrollederror-noautopatrol' ); diff --git a/includes/diff/DifferenceEngine.php b/includes/diff/DifferenceEngine.php index 90a278572d..47967e489c 100644 --- a/includes/diff/DifferenceEngine.php +++ b/includes/diff/DifferenceEngine.php @@ -487,7 +487,7 @@ class DifferenceEngine extends ContextSource { array( 'USE INDEX' => 'rc_timestamp' ) ); - if ( $change && $change->getPerformer()->getName() !== $user->getName() ) { + if ( $change && !$change->getPerformer()->equals( $user ) ) { $rcid = $change->getAttribute( 'rc_id' ); } else { // None found or the page has been created by the current user. diff --git a/includes/page/Article.php b/includes/page/Article.php index 438a17cbf9..9ce3854e99 100644 --- a/includes/page/Article.php +++ b/includes/page/Article.php @@ -1129,7 +1129,7 @@ class Article implements Page { return false; } - if ( $rc->getPerformer()->getName() == $user->getName() ) { + if ( $rc->getPerformer()->equals( $user ) ) { // Don't show a patrol link for own creations. If the user could // patrol them, they already would be patrolled return false; diff --git a/includes/specials/SpecialUserrights.php b/includes/specials/SpecialUserrights.php index 892ff5bc95..36a31bd8a7 100644 --- a/includes/specials/SpecialUserrights.php +++ b/includes/specials/SpecialUserrights.php @@ -106,7 +106,7 @@ class UserrightsPage extends SpecialPage { } } - if ( User::getCanonicalName( $this->mTarget ) == $user->getName() ) { + if ( User::getCanonicalName( $this->mTarget ) === $user->getName() ) { $this->isself = true; } @@ -228,7 +228,7 @@ class UserrightsPage extends SpecialPage { global $wgAuth; // Validate input set... - $isself = ( $user->getName() == $this->getUser()->getName() ); + $isself = $user->equals( $this->getUser() ); $groups = $user->getGroups(); $changeable = $this->changeableGroups(); $addable = array_merge( $changeable['add'], $isself ? $changeable['add-self'] : array() ); diff --git a/tests/phpunit/includes/UserTest.php b/tests/phpunit/includes/UserTest.php index 8cc2a8e29e..f5cd1fcd94 100644 --- a/tests/phpunit/includes/UserTest.php +++ b/tests/phpunit/includes/UserTest.php @@ -355,4 +355,31 @@ class UserTest extends MediaWikiTestCase { 'false' => 'With / slash' ), 'With slash' ), ); } + + public function testEquals() { + $first = User::newFromName( 'EqualUser' ); + $second = User::newFromName( 'EqualUser' ); + + $this->assertTrue( $first->equals( $first ) ); + $this->assertTrue( $first->equals( $second ) ); + $this->assertTrue( $second->equals( $first ) ); + + $third = User::newFromName( '0' ); + $fourth = User::newFromName( '000' ); + + $this->assertFalse( $third->equals( $fourth ) ); + $this->assertFalse( $fourth->equals( $third ) ); + + // Test users loaded from db with id + $user = User::newFromName( 'EqualUnitTestUser' ); + if ( !$user->getId() ) { + $user->addToDatabase(); + } + + $id = $user->getId(); + + $fifth = User::newFromId( $id ); + $sixth = User::newFromName( 'EqualUnitTestUser' ); + $this->assertTrue( $fifth->equals( $sixth ) ); + } } -- 2.20.1