UserIdentityValue: Introduce convenience static factory methods

Bug: T281972
Change-Id: I3e65690695313380c798b62edfda726b6e374f89
This commit is contained in:
Petr Pchelko 2021-05-05 11:13:13 -07:00
parent f413a6210b
commit bbc75d4048
2 changed files with 82 additions and 1 deletions

View file

@ -83,6 +83,46 @@ class UserIdentityValue implements UserIdentity {
$this->wikiId = $wikiId;
}
/**
* Create UserIdentity for an anonymous user.
*
* @since 1.37
* @param string $name
* @param string|false $wikiId wiki ID or self::LOCAL for the local wiki
* @return UserIdentityValue
*/
public static function newAnonymous( string $name, $wikiId = self::LOCAL ): self {
return new self( 0, $name, $wikiId );
}
/**
* Create UserIdentity for a registered user.
*
* @since 1.37
* @param int $userId
* @param string $name
* @param string|false $wikiId wiki ID or self::LOCAL for the local wiki
* @return UserIdentityValue
*/
public static function newRegistered( int $userId, string $name, $wikiId = self::LOCAL ): self {
Assert::parameter( $userId > 0, '$userId', 'must be greater than zero (user must exist)' );
return new self( $userId, $name, $wikiId );
}
/**
* Create UserIdentity for an external user with $prefix and $name
*
* @since 1.37
* @param string $prefix
* @param string $name
* @param string|false $wikiId wiki ID or self::LOCAL for the local wiki
* @return UserIdentityValue
*/
public static function newExternal( string $prefix, string $name, $wikiId = self::LOCAL ): self {
// > is a standard separator for external users in the database, see ExternalUserNames
return new self( 0, "$prefix>$name", $wikiId );
}
/**
* Get the ID of the wiki this UserIdentity belongs to.
*
@ -145,7 +185,7 @@ class UserIdentityValue implements UserIdentity {
* getId() != 0 and is provided for code readability.
*/
public function isRegistered() : bool {
return $this->getId() != 0;
return $this->getId( $this->wikiId ) != 0;
}
public function __toString(): string {

View file

@ -105,4 +105,45 @@ class UserIdentityValueTest extends MediaWikiUnitTestCase {
$this->expectException( PreconditionException::class );
$user->assertWiki( UserIdentityValue::LOCAL );
}
/**
* @covers \MediaWiki\User\UserIdentityValue::newAnonymous
*/
public function testNewAnonymous() {
$user = UserIdentityValue::newAnonymous( 'TEST', 'acmewiki' );
$this->assertFalse( $user->isRegistered() );
$this->assertSame( 'TEST', $user->getName() );
$this->assertSame( 0, $user->getId( 'acmewiki' ) );
$this->assertSame( 'acmewiki', $user->getWikiId() );
}
/**
* @covers \MediaWiki\User\UserIdentityValue::newRegistered
*/
public function testNewRegistered() {
$user = UserIdentityValue::newRegistered( 1, 'TEST', 'acmewiki' );
$this->assertTrue( $user->isRegistered() );
$this->assertSame( 'TEST', $user->getName() );
$this->assertSame( 1, $user->getId( 'acmewiki' ) );
$this->assertSame( 'acmewiki', $user->getWikiId() );
}
/**
* @covers \MediaWiki\User\UserIdentityValue::newRegistered
*/
public function testNewRegistered_invalid() {
$this->expectException( InvalidArgumentException::class );
UserIdentityValue::newRegistered( 0, 'TEST', 'acmewiki' );
}
/**
* @covers \MediaWiki\User\UserIdentityValue::newExternal
*/
public function testNewExternal() {
$user = UserIdentityValue::newExternal( 'imported', 'TEST', 'acmewiki' );
$this->assertFalse( $user->isRegistered() );
$this->assertSame( 'imported>TEST', $user->getName() );
$this->assertSame( 0, $user->getId( 'acmewiki' ) );
$this->assertSame( 'acmewiki', $user->getWikiId() );
}
}