'ResourceLoaderTestModule' => "$testDir/phpunit/ResourceLoaderTestCase.php",
'ResourceLoaderFileModuleTestModule' => "$testDir/phpunit/ResourceLoaderTestCase.php",
'TestUser' => "$testDir/phpunit/includes/TestUser.php",
+ 'TestUserRegistry' => "$testDir/phpunit/includes/TestUserRegistry.php",
'LessFileCompilationTest' => "$testDir/phpunit/LessFileCompilationTest.php",
# tests/phpunit/includes
self::prepareServices( new GlobalVarConfig() );
}
+ /**
+ * Convenience method for getting an immutable test user
+ *
+ * @since 1.28
+ *
+ * @param string[] $groups Groups the test user should be in.
+ * @return TestUser
+ */
+ public static function getTestUser( $groups = [] ) {
+ return TestUserRegistry::getImmutableTestUser( $groups );
+ }
+
+ /**
+ * Convenience method for getting a mutable test user
+ *
+ * @since 1.28
+ *
+ * @param string[] $groups Groups the test user should be added in.
+ * @return TestUser
+ */
+ public static function getMutableTestUser( $groups = [] ) {
+ return TestUserRegistry::getMutableTestUser( __CLASS__, $groups );
+ }
+
+ /**
+ * Convenience method for getting an immutable admin test user
+ *
+ * @since 1.28
+ *
+ * @param string[] $groups Groups the test user should be added to.
+ * @return TestUser
+ */
+ public static function getTestSysop() {
+ return self::getTestUser( [ 'sysop', 'bureaucrat' ] );
+ }
+
/**
* Prepare service configuration for unit testing.
*
$needsResetDB = false;
- if ( $this->needsDB() ) {
+ if ( !self::$dbSetup || $this->needsDB() ) {
// set up a DB connection for this test to use
self::$useTemporaryTables = !$this->getCliArg( 'use-normal-tables' );
protected function insertPage( $pageName, $text = 'Sample page for unit test.' ) {
$title = Title::newFromText( $pageName, 0 );
- $user = User::newFromName( 'UTSysop' );
+ $user = static::getTestSysop()->getUser();
$comment = __METHOD__ . ': Sample page for unit test.';
// Avoid memory leak...?
# Insert 0 user to prevent FK violations
# Anonymous user
- $this->db->insert( 'user', [
- 'user_id' => 0,
- 'user_name' => 'Anonymous' ], __METHOD__, [ 'IGNORE' ] );
+ if ( !$this->db->selectField( 'user', '1', [ 'user_id' => 0 ] ) ) {
+ $this->db->insert( 'user', [
+ 'user_id' => 0,
+ 'user_name' => 'Anonymous' ], __METHOD__, [ 'IGNORE' ] );
+ }
# Insert 0 page to prevent FK violations
# Blank page
- $this->db->insert( 'page', [
- 'page_id' => 0,
- 'page_namespace' => 0,
- 'page_title' => ' ',
- 'page_restrictions' => null,
- 'page_is_redirect' => 0,
- 'page_is_new' => 0,
- 'page_random' => 0,
- 'page_touched' => $this->db->timestamp(),
- 'page_latest' => 0,
- 'page_len' => 0 ], __METHOD__, [ 'IGNORE' ] );
+ if ( !$this->db->selectField( 'page', '1', [ 'page_id' => 0 ] ) ) {
+ $this->db->insert( 'page', [
+ 'page_id' => 0,
+ 'page_namespace' => 0,
+ 'page_title' => ' ',
+ 'page_restrictions' => null,
+ 'page_is_redirect' => 0,
+ 'page_is_new' => 0,
+ 'page_random' => 0,
+ 'page_touched' => $this->db->timestamp(),
+ 'page_latest' => 0,
+ 'page_len' => 0 ], __METHOD__, [ 'IGNORE' ] );
+ }
}
User::resetIdByNameCache();
// Make sysop user
- $user = User::newFromName( 'UTSysop' );
-
- if ( $user->idForName() == 0 ) {
- $user->addToDatabase();
- TestUser::setPasswordForUser( $user, 'UTSysopPassword' );
- $user->addGroup( 'sysop' );
- $user->addGroup( 'bureaucrat' );
- }
+ $user = static::getTestSysop()->getUser();
// Make 1 page with 1 revision
$page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
TestingAccessWrapper::newFromClass( 'User' )
->getInProcessCache()
->clear();
+
+ TestUserRegistry::clear();
}
$truncate = in_array( $db->getType(), [ 'oracle', 'mysql' ] );
);
// Sysop with mergehistory permission
- $sysop = User::newFromName( 'UTSysop' );
+ $sysop = static::getTestSysop()->getUser();
$status = $mh->checkPermissions( $sysop, '' );
$this->assertTrue( $status->isOK() );
// Normal user
- $notSysop = User::newFromName( 'UTNotSysop' );
- $notSysop->addToDatabase();
+ $notSysop = static::getTestUser()->getUser();
$status = $mh->checkPermissions( $notSysop, '' );
$this->assertTrue( $status->hasMessage( 'mergehistory-fail-permission' ) );
}
Title::newFromText( 'Merge2' )
);
- $mh->merge( User::newFromName( 'UTSysop' ) );
+ $sysop = static::getTestSysop()->getUser();
+ $mh->merge( $sysop );
$this->assertEquals( $mh->getMergedRevisionCount(), 1 );
}
}
--- /dev/null
+<?php
+
+/**
+ * @since 1.28
+ */
+class TestUserRegistry {
+
+ /** @var TestUser[] (group key => TestUser) */
+ private static $testUsers = [];
+
+ /** @var int Count of users that have been generated */
+ private static $counter = 0;
+
+ /** @var int Random int, included in IDs */
+ private static $randInt;
+
+ public static function getNextId() {
+ if ( !self::$randInt ) {
+ self::$randInt = mt_rand( 1, 0xFFFFFF );
+ }
+ return sprintf( '%06x.%03x', self::$randInt, ++self::$counter );
+ }
+
+ /**
+ * Get a TestUser object that the caller may modify.
+ *
+ * @since 1.28
+ *
+ * @param string $testName Caller's __CLASS__. Used to generate the
+ * user's username.
+ * @param string[] $groups Groups the test user should be added to.
+ * @return TestUser
+ */
+ public static function getMutableTestUser( $testName, $groups = [] ) {
+ $id = self::getNextId();
+ $password = wfRandomString( 20 );
+ $testUser = new TestUser(
+ "TestUser $testName $id", // username
+ "Name $id", // real name
+ "$id@mediawiki.test", // e-mail
+ $groups, // groups
+ $password // password
+ );
+ $testUser->getUser()->clearInstanceCache();
+ return $testUser;
+ }
+
+ /**
+ * Get a TestUser object that the caller may not modify.
+ *
+ * Whenever possible, unit tests should use immutable users, because
+ * immutable users can be reused in multiple tests, which helps keep
+ * the unit tests fast.
+ *
+ * @since 1.28
+ *
+ * @param string[] $groups Groups the test user should be added to.
+ * @return TestUser
+ */
+ public static function getImmutableTestUser( $groups = [] ) {
+ $groups = array_unique( $groups );
+ sort( $groups );
+ $key = implode( ',', $groups );
+
+ $testUser = isset( self::$testUsers[$key] )
+ ? self::$testUsers[$key]
+ : false;
+
+ if ( !$testUser || !$testUser->getUser()->isLoggedIn() ) {
+ $id = self::getNextId();
+ // Hack! If this is the primary sysop account, make the username
+ // be 'UTSysop', for back-compat, and for the sake of PHPUnit data
+ // provider methods, which are executed before the test database
+ // is set up. See T136348.
+ if ( $groups === [ 'bureaucrat', 'sysop' ] ) {
+ $username = 'UTSysop';
+ $password = 'UTSysopPassword';
+ } else {
+ $username = "TestUser $id";
+ $password = wfRandomString( 20 );
+ }
+ self::$testUsers[$key] = $testUser = new TestUser(
+ $username, // username
+ "Name $id", // real name
+ "$id@mediawiki.test", // e-mail
+ $groups, // groups
+ $password // password
+ );
+ }
+
+ $testUser->getUser()->clearInstanceCache();
+ return self::$testUsers[$key];
+ }
+
+ /**
+ * Clear the registry.
+ *
+ * TestUsers created by this class will not be deleted, but any handles
+ * to existing immutable TestUsers will be deleted, ensuring these users
+ * are not reused. We don't reset the counter or random string by design.
+ *
+ * @since 1.28
+ *
+ * @param string[] $groups Groups the test user should be added to.
+ * @return TestUser
+ */
+ public static function clear() {
+ self::$testUsers = [];
+ }
+}
protected function setUp() {
parent::setUp();
- self::$users['ApiQueryWatchlistIntegrationTestUser']
- = new TestUser( 'ApiQueryWatchlistIntegrationTestUser' );
- self::$users['ApiQueryWatchlistIntegrationTestUser2']
- = new TestUser( 'ApiQueryWatchlistIntegrationTestUser2' );
+ self::$users['ApiQueryWatchlistIntegrationTestUser'] = $this->getMutableTestUser();
+ self::$users['ApiQueryWatchlistIntegrationTestUser2'] = $this->getMutableTestUser();
$this->doLogin( 'ApiQueryWatchlistIntegrationTestUser' );
}
- private function getTestUser() {
+ private function getLoggedInTestUser() {
return self::$users['ApiQueryWatchlistIntegrationTestUser']->getUser();
}
return self::$users['ApiQueryWatchlistIntegrationTestUser2']->getUser();
}
- private function getSysopTestUser() {
- return self::$users['sysop']->getUser();
- }
-
private function doPageEdit( User $user, LinkTarget $target, $content, $summary ) {
$title = Title::newFromLinkTarget( $target );
$page = WikiPage::factory( $title );
}
private function cleanTestUsersWatchlist() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$store = $this->getWatchedItemStore();
$items = $store->getWatchedItemsForUser( $user );
foreach ( $items as $item ) {
// the user with the same user ID as user used here as the test user
$this->cleanTestUsersWatchlist();
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
$this->doPageEdit(
$user,
}
public function testIdsPropParameter() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
$this->doPageEdit(
$user,
}
public function testTitlePropParameter() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
$talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
$this->doPageEdits(
}
public function testFlagsPropParameter() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$normalEditTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
$minorEditTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPageM' );
$botEditTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPageB' );
}
public function testUserPropParameter() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$userEditTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
$anonEditTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPageA' );
$this->doPageEdit(
}
public function testUserIdPropParameter() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$userEditTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
$anonEditTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPageA' );
$this->doPageEdit(
}
public function testCommentPropParameter() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
$this->doPageEdit(
$user,
}
public function testParsedCommentPropParameter() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
$this->doPageEdit(
$user,
}
public function testTimestampPropParameter() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
$this->doPageEdit(
$user,
}
public function testSizesPropParameter() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
$this->doPageEdit(
$user,
'Create the page'
);
$store = $this->getWatchedItemStore();
- $store->addWatch( $this->getTestUser(), $target );
+ $store->addWatch( $this->getLoggedInTestUser(), $target );
$store->updateNotificationTimestamp(
$otherUser,
$target,
}
public function testPatrolPropParameter() {
- $user = $this->getSysopTestUser();
+ $testUser = static::getTestSysop();
+ $user = $testUser->getUser();
$this->setupPatrolledSpecificFixtures( $user );
$result = $this->doListWatchlistRequest( [ 'wlprop' => 'patrol', ], $user );
private function createPageAndDeleteIt( LinkTarget $target ) {
$this->doPageEdit(
- $this->getTestUser(),
+ $this->getLoggedInTestUser(),
$target,
'Some Content',
'Create the page that will be deleted'
$target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
$this->createPageAndDeleteIt( $target );
- $this->watchPages( $this->getTestUser(), [ $target ] );
+ $this->watchPages( $this->getLoggedInTestUser(), [ $target ] );
$result = $this->doListWatchlistRequest( [ 'wlprop' => 'loginfo', ] );
}
public function testEmptyPropParameter() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
$this->doPageEdit(
$user,
}
public function testNamespaceParam() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
$talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
$this->doPageEdits(
}
public function testUserParam() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$otherUser = $this->getNonLoggedInTestUser();
$subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
$talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
}
public function testExcludeUserParam() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$otherUser = $this->getNonLoggedInTestUser();
$subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
$talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
}
public function testShowMinorParams() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
$this->doPageEdits(
$user,
}
public function testShowBotParams() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
$this->doBotPageEdit(
$user,
}
public function testShowAnonParams() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
$this->doAnonPageEdit(
$target,
}
public function testShowUnreadParams() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$otherUser = $this->getNonLoggedInTestUser();
$subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
$talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
}
public function testShowPatrolledParams() {
- $user = $this->getSysopTestUser();
+ $user = static::getTestSysop()->getUser();
$this->setupPatrolledSpecificFixtures( $user );
$resultPatrolled = $this->doListWatchlistRequest( [
}
public function testNewAndEditTypeParameters() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
$talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
$this->doPageEdits(
}
public function testLogTypeParameters() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
$talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
$this->createPageAndDeleteIt( $subjectTarget );
}
public function testExternalTypeParameters() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
$talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
$this->doPageEdit(
}
public function testCategorizeTypeParameter() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
$categoryTarget = new TitleValue( NS_CATEGORY, 'ApiQueryWatchlistIntegrationTestCategory' );
$this->doPageEdits(
}
public function testLimitParam() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$target1 = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
$target2 = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
$target3 = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage2' );
}
public function testAllRevParam() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
$this->doPageEdits(
$user,
}
public function testDirParams() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$subjectTarget = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
$talkTarget = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
$this->doPageEdits(
}
public function testStartEndParams() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
$this->doPageEdit(
$user,
}
public function testContinueParam() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$target1 = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
$target2 = new TitleValue( 1, 'ApiQueryWatchlistIntegrationTestPage' );
$target3 = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage2' );
public function testOwnerAndTokenParams() {
$target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
$this->doPageEdit(
- $this->getTestUser(),
+ $this->getLoggedInTestUser(),
$target,
'Some Content',
'Create the page'
}
public function testGeneratorWatchlistPropInfo_returnsWatchedPages() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
$this->doPageEdit(
$user,
}
public function testGeneratorWatchlistPropRevisions_returnsWatchedItemsRevisions() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$target = new TitleValue( 0, 'ApiQueryWatchlistIntegrationTestPage' );
$this->doPageEdits(
$user,
*/
protected $apiContext;
- /**
- * @var array
- */
- protected $tablesUsed = [ 'user', 'user_groups', 'user_properties' ];
-
protected function setUp() {
global $wgServer, $wgDisableAuthManager;
ApiQueryInfo::resetTokenCache(); // tokens are invalid because we cleared the session
self::$users = [
- 'sysop' => new TestUser(
- 'Apitestsysop',
- 'Api Test Sysop',
- 'api_test_sysop@example.com',
- [ 'sysop' ]
- ),
- 'uploader' => new TestUser(
- 'Apitestuser',
- 'Api Test User',
- 'api_test_user@example.com',
- []
- )
+ 'sysop' => static::getTestSysop(),
+ 'uploader' => static::getTestUser(),
];
$this->setMwGlobals( [
}
}
- protected function doLogin( $user = 'sysop' ) {
- if ( !array_key_exists( $user, self::$users ) ) {
- throw new MWException( "Can not log in to undefined user $user" );
+ protected function doLogin( $testUser = 'sysop' ) {
+ if ( $testUser === null ) {
+ $testUser = static::getTestSysop();
+ } elseif ( is_string( $testUser ) && array_key_exists( $testUser, self::$users ) ) {
+ $testUser = self::$users[ $testUser ];
+ } elseif ( !$testUser instanceof TestUser ) {
+ throw new MWException( "Can not log in to undefined user $testUser" );
}
$data = $this->doApiRequest( [
'action' => 'login',
- 'lgname' => self::$users[$user]->username,
- 'lgpassword' => self::$users[$user]->password ] );
+ 'lgname' => $testUser->username,
+ 'lgpassword' => $testUser->password ] );
$token = $data[0]['login']['token'];
[
'action' => 'login',
'lgtoken' => $token,
- 'lgname' => self::$users[$user]->username,
- 'lgpassword' => self::$users[$user]->password,
+ 'lgname' => $testUser->username,
+ 'lgpassword' => $testUser->password,
],
$data[2]
);
if ( $data[0]['login']['result'] === 'Success' ) {
// DWIM
global $wgUser;
- $wgUser = self::$users[$user]->getUser();
+ $wgUser = $testUser->getUser();
RequestContext::getMain()->setUser( $wgUser );
}
}
public function testBasics() {
+ $user = $this->getMutableTestUser()->getUser();
+ $userName = $user->getName();
+ $lowerInitialUserName = mb_strtolower( $userName[0] ) . substr( $userName, 1 );
+
$provider = new LocalPasswordPrimaryAuthenticationProvider();
$this->assertSame(
$provider->accountCreationType()
);
- $this->assertTrue( $provider->testUserExists( 'UTSysop' ) );
- $this->assertTrue( $provider->testUserExists( 'uTSysop' ) );
+ $this->assertTrue( $provider->testUserExists( $userName ) );
+ $this->assertTrue( $provider->testUserExists( $lowerInitialUserName ) );
$this->assertFalse( $provider->testUserExists( 'DoesNotExist' ) );
$this->assertFalse( $provider->testUserExists( '<invalid>' ) );
$provider->accountCreationType()
);
- $this->assertTrue( $provider->testUserExists( 'UTSysop' ) );
+ $this->assertTrue( $provider->testUserExists( $userName ) );
$this->assertFalse( $provider->testUserExists( 'DoesNotExist' ) );
$req = new PasswordAuthenticationRequest;
}
public function testTestUserCanAuthenticate() {
+ $user = $this->getMutableTestUser()->getUser();
+ $userName = $user->getName();
$dbw = wfGetDB( DB_MASTER );
- $oldHash = $dbw->selectField( 'user', 'user_password', [ 'user_name' => 'UTSysop' ] );
- $cb = new \ScopedCallback( function () use ( $dbw, $oldHash ) {
- $dbw->update( 'user', [ 'user_password' => $oldHash ], [ 'user_name' => 'UTSysop' ] );
- } );
- $id = \User::idFromName( 'UTSysop' );
$provider = $this->getProvider();
$this->assertFalse( $provider->testUserCanAuthenticate( 'DoesNotExist' ) );
- $this->assertTrue( $provider->testUserCanAuthenticate( 'UTSysop' ) );
- $this->assertTrue( $provider->testUserCanAuthenticate( 'uTSysop' ) );
+ $this->assertTrue( $provider->testUserCanAuthenticate( $userName ) );
+ $lowerInitialUserName = mb_strtolower( $userName[0] ) . substr( $userName, 1 );
+ $this->assertTrue( $provider->testUserCanAuthenticate( $lowerInitialUserName ) );
$dbw->update(
'user',
[ 'user_password' => \PasswordFactory::newInvalidPassword()->toString() ],
- [ 'user_name' => 'UTSysop' ]
+ [ 'user_name' => $userName ]
);
- $this->assertFalse( $provider->testUserCanAuthenticate( 'UTSysop' ) );
+ $this->assertFalse( $provider->testUserCanAuthenticate( $userName ) );
// Really old format
$dbw->update(
'user',
[ 'user_password' => '0123456789abcdef0123456789abcdef' ],
- [ 'user_name' => 'UTSysop' ]
+ [ 'user_name' => $userName ]
);
- $this->assertTrue( $provider->testUserCanAuthenticate( 'UTSysop' ) );
+ $this->assertTrue( $provider->testUserCanAuthenticate( $userName ) );
}
public function testSetPasswordResetFlag() {
$provider->setManager( $this->manager );
$providerPriv = \TestingAccessWrapper::newFromObject( $provider );
+ $user = $this->getMutableTestUser()->getUser();
+ $userName = $user->getName();
$dbw = wfGetDB( DB_MASTER );
$row = $dbw->selectRow(
'user',
'*',
- [ 'user_name' => 'UTSysop' ],
+ [ 'user_name' => $userName ],
__METHOD__
);
$this->manager->removeAuthenticationSessionData( null );
$row->user_password_expires = wfTimestamp( TS_MW, time() + 200 );
- $providerPriv->setPasswordResetFlag( 'UTSysop', \Status::newGood(), $row );
+ $providerPriv->setPasswordResetFlag( $userName, \Status::newGood(), $row );
$this->assertNull( $this->manager->getAuthenticationSessionData( 'reset-pass' ) );
$this->manager->removeAuthenticationSessionData( null );
$row->user_password_expires = wfTimestamp( TS_MW, time() - 200 );
- $providerPriv->setPasswordResetFlag( 'UTSysop', \Status::newGood(), $row );
+ $providerPriv->setPasswordResetFlag( $userName, \Status::newGood(), $row );
$ret = $this->manager->getAuthenticationSessionData( 'reset-pass' );
$this->assertNotNull( $ret );
$this->assertSame( 'resetpass-expired', $ret->msg->getKey() );
$this->manager->removeAuthenticationSessionData( null );
$row->user_password_expires = wfTimestamp( TS_MW, time() - 1 );
- $providerPriv->setPasswordResetFlag( 'UTSysop', \Status::newGood(), $row );
+ $providerPriv->setPasswordResetFlag( $userName, \Status::newGood(), $row );
$ret = $this->manager->getAuthenticationSessionData( 'reset-pass' );
$this->assertNotNull( $ret );
$this->assertSame( 'resetpass-expired-soft', $ret->msg->getKey() );
$row->user_password_expires = null;
$status = \Status::newGood();
$status->error( 'testing' );
- $providerPriv->setPasswordResetFlag( 'UTSysop', $status, $row );
+ $providerPriv->setPasswordResetFlag( $userName, $status, $row );
$ret = $this->manager->getAuthenticationSessionData( 'reset-pass' );
$this->assertNotNull( $ret );
$this->assertSame( 'resetpass-validity-soft', $ret->msg->getKey() );
}
public function testAuthentication() {
+ $testUser = $this->getMutableTestUser();
+ $userName = $testUser->getUser()->getName();
+
$dbw = wfGetDB( DB_MASTER );
- $oldHash = $dbw->selectField( 'user', 'user_password', [ 'user_name' => 'UTSysop' ] );
- $cb = new \ScopedCallback( function () use ( $dbw, $oldHash ) {
- $dbw->update( 'user', [ 'user_password' => $oldHash ], [ 'user_name' => 'UTSysop' ] );
- } );
- $id = \User::idFromName( 'UTSysop' );
+ $id = \User::idFromName( $userName );
$req = new PasswordAuthenticationRequest();
$req->action = AuthManager::ACTION_LOGIN;
);
// Validation failure
- $req->username = 'UTSysop';
- $req->password = 'UTSysopPassword';
+ $req->username = $userName;
+ $req->password = $testUser->getPassword();
$this->validity = \Status::newFatal( 'arbitrary-failure' );
$ret = $provider->beginPrimaryAuthentication( $reqs );
$this->assertEquals(
$this->manager->removeAuthenticationSessionData( null );
$this->validity = \Status::newGood();
$this->assertEquals(
- AuthenticationResponse::newPass( 'UTSysop' ),
+ AuthenticationResponse::newPass( $userName ),
$provider->beginPrimaryAuthentication( $reqs )
);
$this->assertNull( $this->manager->getAuthenticationSessionData( 'reset-pass' ) );
// Successful auth after normalizing name
$this->manager->removeAuthenticationSessionData( null );
$this->validity = \Status::newGood();
- $req->username = 'uTSysop';
+ $req->username = mb_strtolower( $userName[0] ) . substr( $userName, 1 );
$this->assertEquals(
- AuthenticationResponse::newPass( 'UTSysop' ),
+ AuthenticationResponse::newPass( $userName ),
$provider->beginPrimaryAuthentication( $reqs )
);
$this->assertNull( $this->manager->getAuthenticationSessionData( 'reset-pass' ) );
- $req->username = 'UTSysop';
+ $req->username = $userName;
// Successful auth with reset
$this->manager->removeAuthenticationSessionData( null );
$this->validity->error( 'arbitrary-warning' );
$this->assertEquals(
- AuthenticationResponse::newPass( 'UTSysop' ),
+ AuthenticationResponse::newPass( $userName ),
$provider->beginPrimaryAuthentication( $reqs )
);
$this->assertNotNull( $this->manager->getAuthenticationSessionData( 'reset-pass' ) );
// Correct handling of legacy encodings
$password = ':B:salt:' . md5( 'salt-' . md5( "\xe1\xe9\xed\xf3\xfa" ) );
- $dbw->update( 'user', [ 'user_password' => $password ], [ 'user_name' => 'UTSysop' ] );
+ $dbw->update( 'user', [ 'user_password' => $password ], [ 'user_name' => $userName ] );
$req->password = 'áéÃóú';
$ret = $provider->beginPrimaryAuthentication( $reqs );
$this->assertEquals(
$this->config->set( 'LegacyEncoding', true );
$this->assertEquals(
- AuthenticationResponse::newPass( 'UTSysop' ),
+ AuthenticationResponse::newPass( $userName ),
$provider->beginPrimaryAuthentication( $reqs )
);
// Correct handling of really old password hashes
$this->config->set( 'PasswordSalt', false );
$password = md5( 'FooBar' );
- $dbw->update( 'user', [ 'user_password' => $password ], [ 'user_name' => 'UTSysop' ] );
+ $dbw->update( 'user', [ 'user_password' => $password ], [ 'user_name' => $userName ] );
$req->password = 'FooBar';
$this->assertEquals(
- AuthenticationResponse::newPass( 'UTSysop' ),
+ AuthenticationResponse::newPass( $userName ),
$provider->beginPrimaryAuthentication( $reqs )
);
$this->config->set( 'PasswordSalt', true );
$password = md5( "$id-" . md5( 'FooBar' ) );
- $dbw->update( 'user', [ 'user_password' => $password ], [ 'user_name' => 'UTSysop' ] );
+ $dbw->update( 'user', [ 'user_password' => $password ], [ 'user_name' => $userName ] );
$req->password = 'FooBar';
$this->assertEquals(
- AuthenticationResponse::newPass( 'UTSysop' ),
+ AuthenticationResponse::newPass( $userName ),
$provider->beginPrimaryAuthentication( $reqs )
);
/**
* @dataProvider provideProviderChangeAuthenticationData
- * @param string $user
+ * @param callable|bool $usernameTransform
* @param string $type
* @param bool $loginOnly
* @param bool $changed
*/
- public function testProviderChangeAuthenticationData( $user, $type, $loginOnly, $changed ) {
+ public function testProviderChangeAuthenticationData(
+ $usernameTransform, $type, $loginOnly, $changed ) {
+ $testUser = $this->getMutableTestUser();
+ $user = $testUser->getUser()->getName();
+ if ( is_callable( $usernameTransform ) ) {
+ $user = call_user_func( $usernameTransform, $user );
+ }
$cuser = ucfirst( $user );
- $oldpass = 'UTSysopPassword';
+ $oldpass = $testUser->getPassword();
$newpass = 'NewPassword';
$dbw = wfGetDB( DB_MASTER );
- $oldHash = $dbw->selectField( 'user', 'user_password', [ 'user_name' => $cuser ] );
$oldExpiry = $dbw->selectField( 'user', 'user_password_expires', [ 'user_name' => $cuser ] );
- $cb = new \ScopedCallback( function () use ( $dbw, $cuser, $oldHash, $oldExpiry ) {
- $dbw->update(
- 'user',
- [
- 'user_password' => $oldHash,
- 'user_password_expires' => $oldExpiry,
- ],
- [ 'user_name' => $cuser ]
- );
- } );
$this->mergeMwGlobalArrayValue( 'wgHooks', [
'ResetPasswordExpiration' => [ function ( $user, &$expires ) {
public static function provideProviderChangeAuthenticationData() {
return [
- [ 'UTSysop', AuthenticationRequest::class, false, false ],
- [ 'UTSysop', PasswordAuthenticationRequest::class, false, true ],
- [ 'UTSysop', AuthenticationRequest::class, true, false ],
- [ 'UTSysop', PasswordAuthenticationRequest::class, true, true ],
- [ 'uTSysop', PasswordAuthenticationRequest::class, false, true ],
- [ 'uTSysop', PasswordAuthenticationRequest::class, true, true ],
+ [ false, AuthenticationRequest::class, false, false ],
+ [ false, PasswordAuthenticationRequest::class, false, true ],
+ [ false, AuthenticationRequest::class, true, false ],
+ [ false, PasswordAuthenticationRequest::class, true, true ],
+ [ 'ucfirst', PasswordAuthenticationRequest::class, false, true ],
+ [ 'ucfirst', PasswordAuthenticationRequest::class, true, true ],
];
}
// We have to cheat a bit to avoid having to add a new user to
// the database to test the actual setting of the password works right
$dbw = wfGetDB( DB_MASTER );
- $oldHash = $dbw->selectField( 'user', 'user_password', [ 'user_name' => $user ] );
- $cb = new \ScopedCallback( function () use ( $dbw, $user, $oldHash ) {
- $dbw->update( 'user', [ 'user_password' => $oldHash ], [ 'user_name' => $user ] );
- } );
$user = \User::newFromName( 'UTSysop' );
$req->username = $user->getName();
*/
class GenderCacheTest extends MediaWikiLangTestCase {
+ /** @var string[] User key => username */
+ private static $nameMap;
+
function addDBDataOnce() {
// ensure the correct default gender
$this->mergeMwGlobalArrayValue( 'wgDefaultUserOptions', [ 'gender' => 'unknown' ] );
- $user = User::newFromName( 'UTMale' );
- if ( $user->getId() == 0 ) {
- $user->addToDatabase();
- TestUser::setPasswordForUser( $user, 'UTMalePassword' );
- }
- // ensure the right gender
- $user->setOption( 'gender', 'male' );
- $user->saveSettings();
+ $male = $this->getMutableTestUser()->getUser();
+ $male->setOption( 'gender', 'male' );
+ $male->saveSettings();
+
+ $female = $this->getMutableTestUser()->getUser();
+ $female->setOption( 'gender', 'female' );
+ $female->saveSettings();
- $user = User::newFromName( 'UTFemale' );
- if ( $user->getId() == 0 ) {
- $user->addToDatabase();
- TestUser::setPasswordForUser( $user, 'UTFemalePassword' );
- }
- // ensure the right gender
- $user->setOption( 'gender', 'female' );
- $user->saveSettings();
+ $default = $this->getMutableTestUser()->getUser();
+ $default->setOption( 'gender', null );
+ $default->saveSettings();
- $user = User::newFromName( 'UTDefaultGender' );
- if ( $user->getId() == 0 ) {
- $user->addToDatabase();
- TestUser::setPasswordForUser( $user, 'UTDefaultGenderPassword' );
- }
- // ensure the default gender
- $user->setOption( 'gender', null );
- $user->saveSettings();
+ self::$nameMap = [
+ 'UTMale' => $male->getName(),
+ 'UTFemale' => $female->getName(),
+ 'UTDefaultGender' => $default->getName()
+ ];
}
/**
* @dataProvider provideUserGenders
* @covers GenderCache::getGenderOf
*/
- public function testUserName( $username, $expectedGender ) {
+ public function testUserName( $userKey, $expectedGender ) {
$genderCache = GenderCache::singleton();
+ $username = isset( self::$nameMap[$userKey] ) ? self::$nameMap[$userKey] : $userKey;
$gender = $genderCache->getGenderOf( $username );
$this->assertEquals( $gender, $expectedGender, "GenderCache normal" );
}
* @dataProvider provideUserGenders
* @covers GenderCache::getGenderOf
*/
- public function testUserObjects( $username, $expectedGender ) {
+ public function testUserObjects( $userKey, $expectedGender ) {
+ $username = isset( self::$nameMap[$userKey] ) ? self::$nameMap[$userKey] : $userKey;
$genderCache = GenderCache::singleton();
- $user = User::newFromName( $username );
- $gender = $genderCache->getGenderOf( $user );
+ $gender = $genderCache->getGenderOf( $username );
$this->assertEquals( $gender, $expectedGender, "GenderCache normal" );
}
* test strip of subpages to avoid unnecessary queries
* against the never existing username
*
- * @dataProvider provideStripSubpages
+ * @dataProvider provideUserGenders
* @covers GenderCache::getGenderOf
*/
- public function testStripSubpages( $pageWithSubpage, $expectedGender ) {
+ public function testStripSubpages( $userKey, $expectedGender ) {
+ $username = isset( self::$nameMap[$userKey] ) ? self::$nameMap[$userKey] : $userKey;
$genderCache = GenderCache::singleton();
- $gender = $genderCache->getGenderOf( $pageWithSubpage );
+ $gender = $genderCache->getGenderOf( "$username/subpage" );
$this->assertEquals( $gender, $expectedGender, "GenderCache must strip of subpages" );
}
-
- public static function provideStripSubpages() {
- return [
- [ 'UTMale/subpage', 'male' ],
- [ 'UTFemale/subpage', 'female' ],
- [ 'UTDefaultGender/subpage', 'unknown' ],
- [ 'UTNotExist/subpage', 'unknown' ],
- [ '127.0.0.1/subpage', 'unknown' ],
- ];
- }
}
*/
private static $pageRev = null;
+ /**
+ * @var User
+ */
+ private static $revUser = null;
+
/**
* @var string
*/
$page = WikiPage::factory( $title );
self::$pageRev = $page->getRevision();
+ self::$revUser = User::newFromId( self::$pageRev->getUser( Revision::RAW ) );
}
private function newChange( Revision $revision = null ) {
$this->assertTrue( strlen( self::$lastNotifyArgs[0] ) === 14 );
$this->assertEquals( 'Category:CategoryName', self::$lastNotifyArgs[1]->getPrefixedText() );
- $this->assertEquals( 'UTSysop', self::$lastNotifyArgs[2]->getName() );
+ $this->assertEquals( self::$revUser->getName(), self::$lastNotifyArgs[2]->getName() );
$this->assertEquals( '(recentchanges-page-added-to-category: ' . self::$pageName . ')',
self::$lastNotifyArgs[3] );
$this->assertEquals( self::$pageName, self::$lastNotifyArgs[4]->getPrefixedText() );
$this->assertTrue( strlen( self::$lastNotifyArgs[0] ) === 14 );
$this->assertEquals( 'Category:CategoryName', self::$lastNotifyArgs[1]->getPrefixedText() );
- $this->assertEquals( 'UTSysop', self::$lastNotifyArgs[2]->getName() );
+ $this->assertEquals( self::$revUser->getName(), self::$lastNotifyArgs[2]->getName() );
$this->assertEquals( '(recentchanges-page-removed-from-category: ' . self::$pageName . ')',
self::$lastNotifyArgs[3] );
$this->assertEquals( self::$pageName, self::$lastNotifyArgs[4]->getPrefixedText() );
* @return RecentChange
*/
private function getEditChange( $timestamp ) {
- $user = $this->getTestUser();
+ $user = $this->getMutableTestUser()->getUser();
$recentChange = $this->testRecentChangesHelper->makeEditRecentChange(
$user, 'Cat', $timestamp, 5, 191, 190, 0, 0
);
$wikiPage = new WikiPage( Title::newFromText( 'Category:Foo' ) );
$wikiPage->doEditContent( new WikitextContent( 'Some random text' ), 'category page created' );
- $user = $this->getTestUser();
+ $user = $this->getMutableTestUser()->getUser();
$recentChange = $this->testRecentChangesHelper->makeCategorizationRecentChange(
$user, 'Category:Foo', $wikiPage->getId(), $thisId, $lastId, $timestamp
);
return $recentChange;
}
- /**
- * @return User
- */
- private function getTestUser() {
- $user = User::newFromName( 'TestRecentChangesUser' );
-
- if ( !$user->getId() ) {
- $user->addToDatabase();
- }
-
- return $user;
- }
-
private function createCategorizationLine( $recentChange ) {
$enhancedChangesList = $this->newEnhancedChangesList();
$cacheEntry = $this->testRecentChangesHelper->getCacheEntry( $recentChange );
}
private function getNewBotEditChange() {
- $user = $this->getTestUser();
+ $user = $this->getMutableTestUser()->getUser();
$recentChange = $this->testRecentChangesHelper->makeNewBotEditRecentChange(
$user, 'Abc', '20131103212153', 5, 191, 190, 0, 0
}
private function getLogChange( $logType, $logAction ) {
- $user = $this->getTestUser();
+ $user = $this->getMutableTestUser()->getUser();
$recentChange = $this->testRecentChangesHelper->makeLogRecentChange(
$logType, $logAction, $user, 'Abc', '20131103212153', 0, 0
}
private function getEditChange() {
- $user = $this->getTestUser();
+ $user = $this->getMutableTestUser()->getUser();
$recentChange = $this->testRecentChangesHelper->makeEditRecentChange(
$user, 'Cat', '20131103212153', 5, 191, 190, 0, 0
);
return new OldChangesList( $context );
}
- private function getTestUser() {
- $user = User::newFromName( 'TestRecentChangesUser' );
-
- if ( !$user->getId() ) {
- $user->addToDatabase();
- }
-
- return $user;
- }
-
private function getContext() {
- $user = $this->getTestUser();
+ $user = $this->getMutableTestUser()->getUser();
$context = $this->testRecentChangesHelper->getTestContext( $user );
$context->setLanguage( 'qqx' );
] );
}
- /**
- * @dataProvider editChangeProvider
- */
- public function testNewFromRecentChange( $expected, $context, $messages,
- $recentChange, $watched
- ) {
- $cacheEntryFactory = new RCCacheEntryFactory( $context, $messages );
- $cacheEntry = $cacheEntryFactory->newFromRecentChange( $recentChange, $watched );
+ public function testNewFromRecentChange() {
+ $user = $this->getMutableTestUser()->getUser();
+ $recentChange = $this->testRecentChangesHelper->makeEditRecentChange(
+ $user,
+ 'Xyz',
+ 5, // curid
+ 191, // thisid
+ 190, // lastid
+ '20131103212153',
+ 0, // counter
+ 0 // number of watching users
+ );
+ $cacheEntryFactory = new RCCacheEntryFactory( $this->getContext(), $this->getMessages() );
+ $cacheEntry = $cacheEntryFactory->newFromRecentChange( $recentChange, false );
$this->assertInstanceOf( 'RCCacheEntry', $cacheEntry );
- $this->assertEquals( $watched, $cacheEntry->watched, 'watched' );
- $this->assertEquals( $expected['timestamp'], $cacheEntry->timestamp, 'timestamp' );
- $this->assertEquals(
- $expected['numberofWatchingusers'], $cacheEntry->numberofWatchingusers,
- 'watching users'
- );
- $this->assertEquals( $expected['unpatrolled'], $cacheEntry->unpatrolled, 'unpatrolled' );
+ $this->assertEquals( false, $cacheEntry->watched, 'watched' );
+ $this->assertEquals( '21:21', $cacheEntry->timestamp, 'timestamp' );
+ $this->assertEquals( 0, $cacheEntry->numberofWatchingusers, 'watching users' );
+ $this->assertEquals( false, $cacheEntry->unpatrolled, 'unpatrolled' );
- $this->assertUserLinks( 'TestRecentChangesUser', $cacheEntry );
+ $this->assertUserLinks( $user->getName(), $cacheEntry );
$this->assertTitleLink( 'Xyz', $cacheEntry );
- $this->assertQueryLink( 'cur', $expected['cur'], $cacheEntry->curlink, 'cur link' );
- $this->assertQueryLink( 'prev', $expected['diff'], $cacheEntry->lastlink, 'prev link' );
- $this->assertQueryLink( 'diff', $expected['diff'], $cacheEntry->difflink, 'diff link' );
+ $diff = [ 'curid' => 5, 'diff' => 191, 'oldid' => 190 ];
+ $cur = [ 'curid' => 5, 'diff' => 0, 'oldid' => 191 ];
+ $this->assertQueryLink( 'cur', $cur, $cacheEntry->curlink, 'cur link' );
+ $this->assertQueryLink( 'prev', $diff, $cacheEntry->lastlink, 'prev link' );
+ $this->assertQueryLink( 'diff', $diff, $cacheEntry->difflink, 'diff link' );
}
- public function editChangeProvider() {
- return [
- [
- [
- 'title' => 'Xyz',
- 'user' => 'TestRecentChangesUser',
- 'diff' => [ 'curid' => 5, 'diff' => 191, 'oldid' => 190 ],
- 'cur' => [ 'curid' => 5, 'diff' => 0, 'oldid' => 191 ],
- 'timestamp' => '21:21',
- 'numberofWatchingusers' => 0,
- 'unpatrolled' => false
- ],
- $this->getContext(),
- $this->getMessages(),
- $this->testRecentChangesHelper->makeEditRecentChange(
- $this->getTestUser(),
- 'Xyz',
- 5, // curid
- 191, // thisid
- 190, // lastid
- '20131103212153',
- 0, // counter
- 0 // number of watching users
- ),
- false
- ]
+ public function testNewForDeleteChange() {
+ $expected = [
+ 'title' => 'Abc',
+ 'user' => 'TestRecentChangesUser',
+ 'timestamp' => '21:21',
+ 'numberofWatchingusers' => 0,
+ 'unpatrolled' => false
];
- }
-
- /**
- * @dataProvider deleteChangeProvider
- */
- public function testNewForDeleteChange( $expected, $context, $messages, $recentChange, $watched ) {
- $cacheEntryFactory = new RCCacheEntryFactory( $context, $messages );
- $cacheEntry = $cacheEntryFactory->newFromRecentChange( $recentChange, $watched );
+ $user = $this->getMutableTestUser()->getUser();
+ $recentChange = $this->testRecentChangesHelper->makeLogRecentChange(
+ 'delete',
+ 'delete',
+ $user,
+ 'Abc',
+ '20131103212153',
+ 0, // counter
+ 0 // number of watching users
+ );
+ $cacheEntryFactory = new RCCacheEntryFactory( $this->getContext(), $this->getMessages() );
+ $cacheEntry = $cacheEntryFactory->newFromRecentChange( $recentChange, false );
$this->assertInstanceOf( 'RCCacheEntry', $cacheEntry );
- $this->assertEquals( $watched, $cacheEntry->watched, 'watched' );
- $this->assertEquals( $expected['timestamp'], $cacheEntry->timestamp, 'timestamp' );
- $this->assertEquals(
- $expected['numberofWatchingusers'],
- $cacheEntry->numberofWatchingusers, 'watching users'
- );
- $this->assertEquals( $expected['unpatrolled'], $cacheEntry->unpatrolled, 'unpatrolled' );
+ $this->assertEquals( false, $cacheEntry->watched, 'watched' );
+ $this->assertEquals( '21:21', $cacheEntry->timestamp, 'timestamp' );
+ $this->assertEquals( 0, $cacheEntry->numberofWatchingusers, 'watching users' );
+ $this->assertEquals( false, $cacheEntry->unpatrolled, 'unpatrolled' );
$this->assertDeleteLogLink( $cacheEntry );
- $this->assertUserLinks( 'TestRecentChangesUser', $cacheEntry );
+ $this->assertUserLinks( $user->getName(), $cacheEntry );
$this->assertEquals( 'cur', $cacheEntry->curlink, 'cur link for delete log or rev' );
$this->assertEquals( 'diff', $cacheEntry->difflink, 'diff link for delete log or rev' );
$this->assertEquals( 'prev', $cacheEntry->lastlink, 'pref link for delete log or rev' );
}
- public function deleteChangeProvider() {
- return [
- [
- [
- 'title' => 'Abc',
- 'user' => 'TestRecentChangesUser',
- 'timestamp' => '21:21',
- 'numberofWatchingusers' => 0,
- 'unpatrolled' => false
- ],
- $this->getContext(),
- $this->getMessages(),
- $this->testRecentChangesHelper->makeLogRecentChange(
- 'delete',
- 'delete',
- $this->getTestUser(),
- 'Abc',
- '20131103212153',
- 0, // counter
- 0 // number of watching users
- ),
- false
- ]
- ];
- }
-
- /**
- * @dataProvider revUserDeleteProvider
- */
- public function testNewForRevUserDeleteChange( $expected, $context, $messages,
- $recentChange, $watched
- ) {
- $cacheEntryFactory = new RCCacheEntryFactory( $context, $messages );
- $cacheEntry = $cacheEntryFactory->newFromRecentChange( $recentChange, $watched );
+ public function testNewForRevUserDeleteChange() {
+ $user = $this->getMutableTestUser()->getUser();
+ $recentChange = $this->testRecentChangesHelper->makeDeletedEditRecentChange(
+ $user,
+ 'Zzz',
+ '20131103212153',
+ 191, // thisid
+ 190, // lastid
+ '20131103212153',
+ 0, // counter
+ 0 // number of watching users
+ );
+ $cacheEntryFactory = new RCCacheEntryFactory( $this->getContext(), $this->getMessages() );
+ $cacheEntry = $cacheEntryFactory->newFromRecentChange( $recentChange, false );
$this->assertInstanceOf( 'RCCacheEntry', $cacheEntry );
- $this->assertEquals( $watched, $cacheEntry->watched, 'watched' );
- $this->assertEquals( $expected['timestamp'], $cacheEntry->timestamp, 'timestamp' );
- $this->assertEquals(
- $expected['numberofWatchingusers'],
- $cacheEntry->numberofWatchingusers, 'watching users'
- );
- $this->assertEquals( $expected['unpatrolled'], $cacheEntry->unpatrolled, 'unpatrolled' );
+ $this->assertEquals( false, $cacheEntry->watched, 'watched' );
+ $this->assertEquals( '21:21', $cacheEntry->timestamp, 'timestamp' );
+ $this->assertEquals( 0, $cacheEntry->numberofWatchingusers, 'watching users' );
+ $this->assertEquals( false, $cacheEntry->unpatrolled, 'unpatrolled' );
$this->assertRevDel( $cacheEntry );
$this->assertTitleLink( 'Zzz', $cacheEntry );
$this->assertEquals( 'prev', $cacheEntry->lastlink, 'pref link for delete log or rev' );
}
- public function revUserDeleteProvider() {
- return [
- [
- [
- 'title' => 'Zzz',
- 'user' => 'TestRecentChangesUser',
- 'diff' => '',
- 'cur' => '',
- 'timestamp' => '21:21',
- 'numberofWatchingusers' => 0,
- 'unpatrolled' => false
- ],
- $this->getContext(),
- $this->getMessages(),
- $this->testRecentChangesHelper->makeDeletedEditRecentChange(
- $this->getTestUser(),
- 'Zzz',
- '20131103212153',
- 191, // thisid
- 190, // lastid
- '20131103212153',
- 0, // counter
- 0 // number of watching users
- ),
- false
- ]
- ];
- }
-
private function assertUserLinks( $user, $cacheEntry ) {
$this->assertTag(
[
];
}
- private function getTestUser() {
- $user = User::newFromName( 'TestRecentChangesUser' );
-
- if ( !$user->getId() ) {
- $user->addToDatabase();
- }
-
- return $user;
- }
-
private function getContext() {
- $user = $this->getTestUser();
+ $user = $this->getMutableTestUser()->getUser();
$context = $this->testRecentChangesHelper->getTestContext( $user );
$title = Title::newFromText( 'RecentChanges', NS_SPECIAL );
$passwordFactory->init( \RequestContext::getMain()->getConfig() );
$passwordHash = $passwordFactory->newFromPlaintext( 'foobaz' );
- $userId = \CentralIdLookup::factory( 'local' )->centralIdFromName( 'UTSysop' );
+ $sysop = static::getTestSysop()->getUser();
+ $userId = \CentralIdLookup::factory( 'local' )->centralIdFromName( $sysop->getName() );
$dbw = wfGetDB( DB_MASTER );
$dbw->delete(
public function testNewSessionInfoForRequest() {
$provider = $this->getProvider();
- $user = \User::newFromName( 'UTSysop' );
+ $user = static::getTestSysop()->getUser();
$request = $this->getMock( 'FauxRequest', [ 'getIP' ] );
$request->expects( $this->any() )->method( 'getIP' )
->will( $this->returnValue( '127.0.0.1' ) );
$provider = $this->getProvider();
$provider->setLogger( $logger );
- $user = \User::newFromName( 'UTSysop' );
+ $user = static::getTestSysop()->getUser();
$request = $this->getMock( 'FauxRequest', [ 'getIP' ] );
$request->expects( $this->any() )->method( 'getIP' )
->will( $this->returnValue( '127.0.0.1' ) );
$provider->setConfig( $this->getConfig() );
$provider->setManager( new SessionManager() );
- $user = User::newFromName( 'UTSysop' );
+ $user = static::getTestSysop()->getUser();
$id = $user->getId();
$name = $user->getName();
$token = $user->getToken( true );
$sessionId = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
$store = new TestBagOStuff();
- $user = User::newFromName( 'UTSysop' );
+ $user = static::getTestSysop()->getUser();
$anon = new User;
$backend = new SessionBackend(
$provider->setManager( SessionManager::singleton() );
$sessionId = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
- $user = User::newFromName( 'UTSysop' );
+ $user = static::getTestSysop()->getUser();
$this->assertFalse( $user->requiresHTTPS(), 'sanity check' );
$backend = new SessionBackend(
$sessionId = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
$store = new TestBagOStuff();
- $user = User::newFromName( 'UTSysop' );
+ $user = static::getTestSysop()->getUser();
$anon = new User;
$backend = new SessionBackend(
}
public function testSetUser() {
- $user = User::newFromName( 'UTSysop' );
+ $user = static::getTestSysop()->getUser();
$this->provider = $this->getMock( 'DummySessionProvider', [ 'canChangeUser' ] );
$this->provider->expects( $this->any() )->method( 'canChangeUser' )
}
public function testSave() {
- $user = User::newFromName( 'UTSysop' );
+ $user = static::getTestSysop()->getUser();
$this->store = new TestBagOStuff();
$testData = [ 'foo' => 'foo!', 'bar', [ 'baz', null ] ];
}
public function testRenew() {
- $user = User::newFromName( 'UTSysop' );
+ $user = static::getTestSysop()->getUser();
$this->store = new TestBagOStuff();
$testData = [ 'foo' => 'foo!', 'bar', [ 'baz', null ] ];
$handler->enable = true;
}
- $backend = $this->getBackend( User::newFromName( 'UTSysop' ) );
+ $backend = $this->getBackend( static::getTestSysop()->getUser() );
\TestingAccessWrapper::newFromObject( $backend )->usePhpSessionHandling = true;
$resetSingleton = TestUtils::setSessionManagerSingleton( $this->manager );
* @group Database
*/
class BotPasswordTest extends MediaWikiTestCase {
+
+ /** @var TestUser */
+ private $testUser;
+
+ /** @var string */
+ private $testUserName;
+
protected function setUp() {
parent::setUp();
'wgUserrightsInterwikiDelimiter' => '@',
] );
+ $this->testUser = $this->getMutableTestUser();
+ $this->testUserName = $this->testUser->getUser()->getName();
+
$mock1 = $this->getMockForAbstractClass( 'CentralIdLookup' );
$mock1->expects( $this->any() )->method( 'isAttached' )
->will( $this->returnValue( true ) );
$mock1->expects( $this->any() )->method( 'lookupUserNames' )
- ->will( $this->returnValue( [ 'UTSysop' => 42, 'UTDummy' => 43, 'UTInvalid' => 0 ] ) );
+ ->will( $this->returnValue( [ $this->testUserName => 42, 'UTDummy' => 43, 'UTInvalid' => 0 ] ) );
$mock1->expects( $this->never() )->method( 'lookupCentralIds' );
$mock2 = $this->getMockForAbstractClass( 'CentralIdLookup' );
}
public function testBasics() {
- $user = User::newFromName( 'UTSysop' );
+ $user = $this->testUser->getUser();
$bp = BotPassword::newFromUser( $user, 'BotPassword' );
$this->assertInstanceOf( 'BotPassword', $bp );
$this->assertTrue( $bp->isSaved() );
}
public function testUnsaved() {
- $user = User::newFromName( 'UTSysop' );
+ $user = $this->testUser->getUser();
$bp = BotPassword::newUnsaved( [
'user' => $user,
'appId' => 'DoesNotExist'
$this->assertEquals( '{"IPAddresses":["127.0.0.0/8"]}', $bp->getRestrictions()->toJson() );
$this->assertSame( [ 'test' ], $bp->getGrants() );
- $user = User::newFromName( 'UTSysop' );
+ $user = $this->testUser->getUser();
$bp = BotPassword::newUnsaved( [
'centralId' => 45,
'appId' => 'DoesNotExist'
$this->assertSame( 45, $bp->getUserCentralId() );
$this->assertSame( 'DoesNotExist', $bp->getAppId() );
- $user = User::newFromName( 'UTSysop' );
+ $user = $this->testUser->getUser();
$bp = BotPassword::newUnsaved( [
'user' => $user,
'appId' => 'BotPassword'
'appId' => str_repeat( 'X', BotPassword::APPID_MAXLENGTH + 1 ),
] ) );
$this->assertNull( BotPassword::newUnsaved( [
- 'user' => 'UTSysop',
+ 'user' => $this->testUserName,
'appId' => 'Ok',
] ) );
$this->assertNull( BotPassword::newUnsaved( [
$this->assertNotInstanceOf( 'InvalidPassword', $bp1->getPassword(), 'sanity check' );
$this->assertNotInstanceOf( 'InvalidPassword', $bp2->getPassword(), 'sanity check' );
- BotPassword::invalidateAllPasswordsForUser( 'UTSysop' );
+ BotPassword::invalidateAllPasswordsForUser( $this->testUserName );
$this->assertInstanceOf( 'InvalidPassword', $bp1->getPassword() );
$this->assertNotInstanceOf( 'InvalidPassword', $bp2->getPassword() );
$this->assertNotNull( BotPassword::newFromCentralId( 42, 'BotPassword' ), 'sanity check' );
$this->assertNotNull( BotPassword::newFromCentralId( 43, 'BotPassword' ), 'sanity check' );
- BotPassword::removeAllPasswordsForUser( 'UTSysop' );
+ BotPassword::removeAllPasswordsForUser( $this->testUserName );
$this->assertNull( BotPassword::newFromCentralId( 42, 'BotPassword' ) );
$this->assertNotNull( BotPassword::newFromCentralId( 43, 'BotPassword' ) );
public function testLogin() {
// Test failure when bot passwords aren't enabled
$this->setMwGlobals( 'wgEnableBotPasswords', false );
- $status = BotPassword::login( 'UTSysop@BotPassword', 'foobaz', new FauxRequest );
+ $status = BotPassword::login( "{$this->testUserName}@BotPassword", 'foobaz', new FauxRequest );
$this->assertEquals( Status::newFatal( 'botpasswords-disabled' ), $status );
$this->setMwGlobals( 'wgEnableBotPasswords', true );
$manager->getProvider( MediaWiki\Session\BotPasswordSessionProvider::class ),
'sanity check'
);
- $status = BotPassword::login( 'UTSysop@BotPassword', 'foobaz', new FauxRequest );
+ $status = BotPassword::login( "{$this->testUserName}@BotPassword", 'foobaz', new FauxRequest );
$this->assertEquals( Status::newFatal( 'botpasswords-no-provider' ), $status );
ScopedCallback::consume( $reset );
$reset = MediaWiki\Session\TestUtils::setSessionManagerSingleton( $manager );
// No "@"-thing in the username
- $status = BotPassword::login( 'UTSysop', 'foobaz', new FauxRequest );
+ $status = BotPassword::login( $this->testUserName, 'foobaz', new FauxRequest );
$this->assertEquals( Status::newFatal( 'botpasswords-invalid-name', '@' ), $status );
// No base user
$this->assertEquals( Status::newFatal( 'nosuchuser', 'UTDummy' ), $status );
// No bot password
- $status = BotPassword::login( 'UTSysop@DoesNotExist', 'foobaz', new FauxRequest );
+ $status = BotPassword::login( "{$this->testUserName}@DoesNotExist", 'foobaz', new FauxRequest );
$this->assertEquals(
- Status::newFatal( 'botpasswords-not-exist', 'UTSysop', 'DoesNotExist' ),
+ Status::newFatal( 'botpasswords-not-exist', $this->testUserName, 'DoesNotExist' ),
$status
);
$request = $this->getMock( 'FauxRequest', [ 'getIP' ] );
$request->expects( $this->any() )->method( 'getIP' )
->will( $this->returnValue( '10.0.0.1' ) );
- $status = BotPassword::login( 'UTSysop@BotPassword', 'foobaz', $request );
+ $status = BotPassword::login( "{$this->testUserName}@BotPassword", 'foobaz', $request );
$this->assertEquals( Status::newFatal( 'botpasswords-restriction-failed' ), $status );
// Wrong password
- $status = BotPassword::login( 'UTSysop@BotPassword', 'UTSysopPassword', new FauxRequest );
+ $status = BotPassword::login(
+ "{$this->testUserName}@BotPassword", $this->testUser->password, new FauxRequest );
$this->assertEquals( Status::newFatal( 'wrongpassword' ), $status );
// Success!
$request->getSession()->getProvider(),
'sanity check'
);
- $status = BotPassword::login( 'UTSysop@BotPassword', 'foobaz', $request );
+ $status = BotPassword::login( "{$this->testUserName}@BotPassword", 'foobaz', $request );
$this->assertInstanceOf( 'Status', $status );
$this->assertTrue( $status->isGood() );
$session = $status->getValue();
$this->getMockForAbstractClass( 'CentralIdLookup' )
);
- $user = User::newFromName( 'UTSysop' );
+ $user = static::getTestSysop()->getUser();
$this->assertSame( $user, $mock->checkAudience( $user ) );
$user = $mock->checkAudience( CentralIdLookup::AUDIENCE_PUBLIC );
public function addDBData() {
for ( $i = 1; $i <= 4; $i++ ) {
- $user = User::newFromName( "UTLocalIdLookup$i" );
- if ( $user->getId() == 0 ) {
- $user->addToDatabase();
- }
- $this->localUsers["UTLocalIdLookup$i"] = $user->getId();
+ $this->localUsers[] = $this->getMutableTestUser()->getUser();
}
- User::newFromName( 'UTLocalIdLookup1' )->addGroup( 'local-id-lookup-test' );
+ $sysop = static::getTestSysop()->getUser();
$block = new Block( [
- 'address' => 'UTLocalIdLookup3',
- 'by' => User::idFromName( 'UTSysop' ),
+ 'address' => $this->localUsers[2]->getName(),
+ 'by' => $sysop->getId(),
'reason' => __METHOD__,
'expiry' => '1 day',
'hideName' => false,
$block->insert();
$block = new Block( [
- 'address' => 'UTLocalIdLookup4',
- 'by' => User::idFromName( 'UTSysop' ),
+ 'address' => $this->localUsers[3]->getName(),
+ 'by' => $sysop->getId(),
'reason' => __METHOD__,
'expiry' => '1 day',
'hideName' => true,
$block->insert();
}
+ public function getLookupUser() {
+ return static::getTestUser( [ 'local-id-lookup-test' ] )->getUser();
+ }
+
public function testLookupCentralIds() {
$lookup = new LocalIdLookup();
- $user1 = User::newFromName( 'UTLocalIdLookup1' );
+
+ $user1 = $this->getLookupUser();
$user2 = User::newFromName( 'UTLocalIdLookup2' );
$this->assertTrue( $user1->isAllowed( 'hideuser' ), 'sanity check' );
$this->assertSame( [], $lookup->lookupCentralIds( [] ) );
- $expect = array_flip( $this->localUsers );
- $expect[123] = 'X';
+ $expect = [];
+ foreach ( $this->localUsers as $localUser ) {
+ $expect[$localUser->getId()] = $localUser->getName();
+ }
+ $expect[12345] = 'X';
ksort( $expect );
$expect2 = $expect;
- $expect2[$this->localUsers['UTLocalIdLookup4']] = '';
+ $expect2[$this->localUsers[3]->getId()] = '';
$arg = array_fill_keys( array_keys( $expect ), 'X' );
public function testLookupUserNames() {
$lookup = new LocalIdLookup();
- $user1 = User::newFromName( 'UTLocalIdLookup1' );
+ $user1 = $this->getLookupUser();
$user2 = User::newFromName( 'UTLocalIdLookup2' );
$this->assertTrue( $user1->isAllowed( 'hideuser' ), 'sanity check' );
$this->assertSame( [], $lookup->lookupUserNames( [] ) );
- $expect = $this->localUsers;
+ $expect = [];
+ foreach ( $this->localUsers as $localUser ) {
+ $expect[$localUser->getName()] = $localUser->getId();
+ }
$expect['UTDoesNotExist'] = 'X';
ksort( $expect );
$expect2 = $expect;
- $expect2['UTLocalIdLookup4'] = 'X';
+ $expect2[$this->localUsers[3]->getName()] = 'X';
$arg = array_fill_keys( array_keys( $expect ), 'X' );
public function testIsAttached() {
$lookup = new LocalIdLookup();
- $user1 = User::newFromName( 'UTLocalIdLookup1' );
+ $user1 = $this->getLookupUser();
$user2 = User::newFromName( 'DoesNotExist' );
$this->assertTrue( $lookup->isAttached( $user1 ) );
$lookup = new LocalIdLookup();
$this->assertSame(
$sharedDB && $sharedTable && $localDBSet,
- $lookup->isAttached( User::newFromName( 'UTLocalIdLookup1' ), 'shared' )
+ $lookup->isAttached( $this->getLookupUser(), 'shared' )
);
}
* @covers User::getEditCount
*/
public function testEditCount() {
- $user = User::newFromName( 'UnitTestUser' );
-
- if ( !$user->getId() ) {
- $user->addToDatabase();
- }
+ $user = $this->getMutableTestUser()->getUser();
// let the user have a few (3) edits
$page = WikiPage::factory( Title::newFromText( 'Help:UserTest_EditCount' ) );
* @covers User::getOption
*/
public function testOptions() {
- $user = User::newFromName( 'UnitTestUser' );
-
- if ( !$user->getId() ) {
- $user->addToDatabase();
- }
+ $user = $this->getMutableTestUser()->getUser();
$user->setOption( 'userjs-someoption', 'test' );
$user->setOption( 'cols', 200 );
$user->saveSettings();
- $user = User::newFromName( 'UnitTestUser' );
+ $user = User::newFromName( $user->getName() );
$this->assertEquals( 'test', $user->getOption( 'userjs-someoption' ) );
$this->assertEquals( 200, $user->getOption( 'cols' ) );
}
'MinimalPasswordLength' => 6,
'PasswordCannotMatchUsername' => true,
'PasswordCannotMatchBlacklist' => true,
- 'MaximalPasswordLength' => 30,
+ 'MaximalPasswordLength' => 40,
],
],
'checks' => [
],
] );
- $user = User::newFromName( 'Useruser' );
+ $user = static::getTestUser()->getUser();
+
// Sanity
$this->assertTrue( $user->isValidPassword( 'Password1234' ) );
$this->assertEquals( 'passwordtooshort', $user->getPasswordValidity( 'a' ) );
// Maximum length
- $longPass = str_repeat( 'a', 31 );
+ $longPass = str_repeat( 'a', 41 );
$this->assertFalse( $user->isValidPassword( $longPass ) );
$this->assertFalse( $user->checkPasswordValidity( $longPass )->isGood() );
$this->assertFalse( $user->checkPasswordValidity( $longPass )->isOK() );
$this->assertEquals( 'passwordtoolong', $user->getPasswordValidity( $longPass ) );
// Matches username
- $this->assertFalse( $user->checkPasswordValidity( 'Useruser' )->isGood() );
- $this->assertTrue( $user->checkPasswordValidity( 'Useruser' )->isOK() );
- $this->assertEquals( 'password-name-match', $user->getPasswordValidity( 'Useruser' ) );
+ $this->assertFalse( $user->checkPasswordValidity( $user->getName() )->isGood() );
+ $this->assertTrue( $user->checkPasswordValidity( $user->getName() )->isOK() );
+ $this->assertEquals( 'password-name-match', $user->getPasswordValidity( $user->getName() ) );
// On the forbidden list
+ $user = User::newFromName( 'Useruser' );
$this->assertFalse( $user->checkPasswordValidity( 'Passpass' )->isGood() );
$this->assertEquals( 'password-login-forbidden', $user->getPasswordValidity( 'Passpass' ) );
}
* @covers User::equals
*/
public function testEquals() {
- $first = User::newFromName( 'EqualUser' );
- $second = User::newFromName( 'EqualUser' );
+ $first = $this->getMutableTestUser()->getUser();
+ $second = User::newFromName( $first->getName() );
$this->assertTrue( $first->equals( $first ) );
$this->assertTrue( $first->equals( $second ) );
$this->assertTrue( $second->equals( $first ) );
- $third = User::newFromName( '0' );
- $fourth = User::newFromName( '000' );
+ $third = $this->getMutableTestUser()->getUser();
+ $fourth = $this->getMutableTestUser()->getUser();
$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' );
+ $user = $this->getMutableTestUser()->getUser();
+ $fifth = User::newFromId( $user->getId() );
+ $sixth = User::newFromName( $user->getName() );
$this->assertTrue( $fifth->equals( $sixth ) );
}
* @covers User::getId
*/
public function testGetId() {
- $user = User::newFromName( 'UTSysop' );
+ $user = static::getTestUser()->getUser();
$this->assertTrue( $user->getId() > 0 );
}
* @covers User::isAnon
*/
public function testLoggedIn() {
- $user = User::newFromName( 'UTSysop' );
+ $user = $this->getMutableTestUser()->getUser();
$this->assertTrue( $user->isLoggedIn() );
$this->assertFalse( $user->isAnon() );
* @covers User::checkAndSetTouched
*/
public function testCheckAndSetTouched() {
- $user = TestingAccessWrapper::newFromObject( User::newFromName( 'UTSysop' ) );
+ $user = $this->getMutableTestUser()->getUser();
+ $user = TestingAccessWrapper::newFromObject( $user );
$this->assertTrue( $user->isLoggedIn() );
$touched = $user->getDBTouched();