From: Niklas Laxström Date: Thu, 11 Dec 2014 08:59:28 +0000 (+0100) Subject: Add User::equals X-Git-Tag: 1.31.0-rc.0~12700^2 X-Git-Url: http://git.cyclocoop.org/%22%20.%20generer_url_ecrire%28%22articles%22%2C%22id_article=%24ze_article%22%29%20.%20%22?a=commitdiff_plain;h=80ca508ed117bbf54778068f24590c80dfe39128;p=lhc%2Fweb%2Fwiklou.git 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 --- 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 ) ); + } }