Hello Krinkle, Hashar,
I'd like you to do a code review. Please visit
https://gerrit.wikimedia.org/r/289369
to review the following change.
Change subject: Whenever possible, reuse User objects in unit tests
......................................................................
Whenever possible, reuse User objects in unit tests
The unit tests spend nearly half of their run time resetting the user table for
each test. But the majority of tests do not depend on the user table having the
exact value that the setup code resets it to, and do not need to modify the
user objects they require to run.
Fix that by providing an API for tests to get User objects, and to indicate
whether the User object will be subject to destructive modification or not.
This allows User objects to be reused across multiple unit tests.
Change-Id: I17ef1f519759c5e7796c259282afe730ef722e96
---
M tests/TestsAutoLoader.php
M tests/phpunit/MediaWikiTestCase.php
M tests/phpunit/includes/MergeHistoryTest.php
M tests/phpunit/includes/PagePropsTest.php
A tests/phpunit/includes/TestUserRegistry.php
M tests/phpunit/includes/api/ApiCreateAccountTest.php
M tests/phpunit/includes/api/ApiQueryWatchlistIntegrationTest.php
M tests/phpunit/includes/api/ApiTestCase.php
M tests/phpunit/includes/cache/GenderCacheTest.php
M tests/phpunit/includes/changes/CategoryMembershipChangeTest.php
M tests/phpunit/includes/changes/EnhancedChangesListTest.php
M tests/phpunit/includes/changes/OldChangesListTest.php
M tests/phpunit/includes/changes/RCCacheEntryFactoryTest.php
M tests/phpunit/includes/debug/MWDebugTest.php
M tests/phpunit/includes/logging/LogFormatterTestCase.php
M tests/phpunit/includes/logging/NewUsersLogFormatterTest.php
M tests/phpunit/includes/session/BotPasswordSessionProviderTest.php
M tests/phpunit/includes/session/CookieSessionProviderTest.php
M tests/phpunit/includes/session/SessionBackendTest.php
M tests/phpunit/includes/session/SessionManagerTest.php
M tests/phpunit/includes/session/UserInfoTest.php
M tests/phpunit/includes/user/BotPasswordTest.php
M tests/phpunit/includes/user/CentralIdLookupTest.php
M tests/phpunit/includes/user/LocalIdLookupTest.php
M tests/phpunit/includes/user/UserTest.php
25 files changed, 432 insertions(+), 444 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core
refs/changes/69/289369/1
diff --git a/tests/TestsAutoLoader.php b/tests/TestsAutoLoader.php
index 26085b8..5b4b9e7 100644
--- a/tests/TestsAutoLoader.php
+++ b/tests/TestsAutoLoader.php
@@ -46,6 +46,7 @@
'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
diff --git a/tests/phpunit/MediaWikiTestCase.php
b/tests/phpunit/MediaWikiTestCase.php
index dedd087..b2fbb7e 100644
--- a/tests/phpunit/MediaWikiTestCase.php
+++ b/tests/phpunit/MediaWikiTestCase.php
@@ -127,10 +127,14 @@
self::prepareServices( new GlobalVarConfig() );
}
- public static function getTestSysop( $mutable = false ) {
- return self::getTestUser( $mutable, [ 'sysop', 'bureaucrat' ] );
- }
-
+ /**
+ * Convenience method for getting a test user
+ *
+ * @param bool $mutable If true, get a mutable test user. If false,
+ * immutable.
+ * @param string[] $groups Groups the test user should be added to.
+ * @return TestUser
+ */
public static function getTestUser( $mutable = false, $groups = [] ) {
return $mutable
? TestUserRegistry::getMutableTestUser( __CLASS__,
$groups )
@@ -138,6 +142,18 @@
}
/**
+ * Convenience method for getting an admin test user
+ *
+ * @param bool $mutable If true, get a mutable test user. If false,
+ * immutable.
+ * @param string[] $groups Groups the test user should be added to.
+ * @return TestUser
+ */
+ public static function getTestSysop( $mutable = false ) {
+ return self::getTestUser( $mutable, [ 'sysop', 'bureaucrat' ] );
+ }
+
+ /**
* Prepare service configuration for unit testing.
*
* This calls MediaWikiServices::resetGlobalInstance() to allow some
critical services
diff --git a/tests/phpunit/includes/MergeHistoryTest.php
b/tests/phpunit/includes/MergeHistoryTest.php
index 22f6fa6..ea2759f 100644
--- a/tests/phpunit/includes/MergeHistoryTest.php
+++ b/tests/phpunit/includes/MergeHistoryTest.php
@@ -97,13 +97,12 @@
);
// Sysop with mergehistory permission
- $sysop = User::newFromName( 'UTSysop' );
+ $sysop = $this->getTestSysop()->getUser();
$status = $mh->checkPermissions( $sysop, '' );
$this->assertTrue( $status->isOK() );
// Normal user
- $notSysop = User::newFromName( 'UTNotSysop' );
- $notSysop->addToDatabase();
+ $notSysop = $this->getTestUser()->getUser();
$status = $mh->checkPermissions( $notSysop, '' );
$this->assertTrue( $status->hasMessage(
'mergehistory-fail-permission' ) );
}
@@ -118,7 +117,8 @@
Title::newFromText( 'Merge2' )
);
- $mh->merge( User::newFromName( 'UTSysop' ) );
+ $sysop = $this->getTestSysop()->getUser();
+ $mh->merge( $sysop );
$this->assertEquals( $mh->getMergedRevisionCount(), 1 );
}
}
diff --git a/tests/phpunit/includes/PagePropsTest.php
b/tests/phpunit/includes/PagePropsTest.php
index cc1708a..3705b51 100644
--- a/tests/phpunit/includes/PagePropsTest.php
+++ b/tests/phpunit/includes/PagePropsTest.php
@@ -12,22 +12,28 @@
/**
* @var Title $title1
*/
- private $title1;
+ private static $title1;
/**
* @var Title $title2
*/
- private $title2;
+ private static $title2;
/**
* @var array $the_properties
*/
- private $the_properties;
+ private static $the_properties = [
+ "property1" => "value1",
+ "property2" => "value2",
+ "property3" => "value3",
+ "property4" => "value4"
+ ];
protected function setUp() {
global $wgExtraNamespaces, $wgNamespaceContentModels,
$wgContentHandlers, $wgContLang;
parent::setUp();
+
$wgExtraNamespaces[12312] = 'Dummy';
$wgExtraNamespaces[12313] = 'Dummy_talk';
@@ -38,36 +44,29 @@
MWNamespace::getCanonicalNamespaces( true ); # reset namespace
cache
$wgContLang->resetNamespaces(); # reset namespace cache
- if ( !$this->the_properties ) {
- $this->the_properties = [
- "property1" => "value1",
- "property2" => "value2",
- "property3" => "value3",
- "property4" => "value4"
- ];
- }
-
- if ( !$this->title1 ) {
+ if ( !self::$title1 ) {
$page = $this->createPage(
'PagePropsTest_page_1',
"just a dummy page",
CONTENT_MODEL_WIKITEXT
);
- $this->title1 = $page->getTitle();
- $page1ID = $this->title1->getArticleID();
- $this->setProperties( $page1ID, $this->the_properties );
+ self::$title1 = $page->getTitle();
+ $page1ID = self::$title1->getArticleID();
+ $this->setProperties( $page1ID, self::$the_properties );
}
- if ( !$this->title2 ) {
+ if ( !self::$title2 ) {
$page = $this->createPage(
'PagePropsTest_page_2',
"just a dummy page",
CONTENT_MODEL_WIKITEXT
);
- $this->title2 = $page->getTitle();
- $page2ID = $this->title2->getArticleID();
- $this->setProperties( $page2ID, $this->the_properties );
+ self::$title2 = $page->getTitle();
+ $page2ID = self::$title2->getArticleID();
+ $this->setProperties( $page2ID, self::$the_properties );
}
+
+ $this->scopedOverride = PageProps::overrideInstance();
}
protected function tearDown() {
@@ -83,6 +82,7 @@
MWNamespace::getCanonicalNamespaces( true ); # reset namespace
cache
$wgContLang->resetNamespaces(); # reset namespace cache
+ ScopedCallback::consume( $this->scopedOverride );
}
/**
@@ -91,8 +91,8 @@
*/
public function testGetSingleProperty() {
$pageProps = PageProps::getInstance();
- $page1ID = $this->title1->getArticleID();
- $result = $pageProps->getProperties( $this->title1, "property1"
);
+ $page1ID = self::$title1->getArticleID();
+ $result = $pageProps->getProperties( self::$title1, "property1"
);
$this->assertArrayHasKey( $page1ID, $result, "Found property" );
$this->assertEquals( $result[$page1ID], "value1", "Get
property" );
}
@@ -103,11 +103,11 @@
*/
public function testGetSinglePropertyMultiplePages() {
$pageProps = PageProps::getInstance();
- $page1ID = $this->title1->getArticleID();
- $page2ID = $this->title2->getArticleID();
+ $page1ID = self::$title1->getArticleID();
+ $page2ID = self::$title2->getArticleID();
$titles = [
- $this->title1,
- $this->title2
+ self::$title1,
+ self::$title2
];
$result = $pageProps->getProperties( $titles, "property1" );
$this->assertArrayHasKey( $page1ID, $result, "Found page 1
property" );
@@ -122,11 +122,11 @@
*/
public function testGetMultiplePropertiesMultiplePages() {
$pageProps = PageProps::getInstance();
- $page1ID = $this->title1->getArticleID();
- $page2ID = $this->title2->getArticleID();
+ $page1ID = self::$title1->getArticleID();
+ $page2ID = self::$title2->getArticleID();
$titles = [
- $this->title1,
- $this->title2
+ self::$title1,
+ self::$title2
];
$properties = [
"property1",
@@ -158,11 +158,11 @@
*/
public function testGetAllProperties() {
$pageProps = PageProps::getInstance();
- $page1ID = $this->title1->getArticleID();
- $result = $pageProps->getAllProperties( $this->title1 );
+ $page1ID = self::$title1->getArticleID();
+ $result = $pageProps->getAllProperties( self::$title1 );
$this->assertArrayHasKey( $page1ID, $result, "Found properties"
);
$properties = $result[$page1ID];
- $patched = array_replace_recursive( $properties,
$this->the_properties );
+ $patched = array_replace_recursive( $properties,
self::$the_properties );
$this->assertEquals( $patched, $properties, "Get all
properties" );
}
@@ -172,20 +172,20 @@
*/
public function testGetAllPropertiesMultiplePages() {
$pageProps = PageProps::getInstance();
- $page1ID = $this->title1->getArticleID();
- $page2ID = $this->title2->getArticleID();
+ $page1ID = self::$title1->getArticleID();
+ $page2ID = self::$title2->getArticleID();
$titles = [
- $this->title1,
- $this->title2
+ self::$title1,
+ self::$title2
];
$result = $pageProps->getAllProperties( $titles );
$this->assertArrayHasKey( $page1ID, $result, "Found page 1
properties" );
$this->assertArrayHasKey( $page2ID, $result, "Found page 2
properties" );
$properties1 = $result[$page1ID];
- $patched = array_replace_recursive( $properties1,
$this->the_properties );
+ $patched = array_replace_recursive( $properties1,
self::$the_properties );
$this->assertEquals( $patched, $properties1, "Get all
properties page 1" );
$properties2 = $result[$page2ID];
- $patched = array_replace_recursive( $properties2,
$this->the_properties );
+ $patched = array_replace_recursive( $properties2,
self::$the_properties );
$this->assertEquals( $patched, $properties2, "Get all
properties page 2" );
}
@@ -197,10 +197,10 @@
*/
public function testSingleCache() {
$pageProps = PageProps::getInstance();
- $page1ID = $this->title1->getArticleID();
- $value1 = $pageProps->getProperties( $this->title1, "property1"
);
+ $page1ID = self::$title1->getArticleID();
+ $value1 = $pageProps->getProperties( self::$title1, "property1"
);
$this->setProperty( $page1ID, "property1", "another value" );
- $value2 = $pageProps->getProperties( $this->title1, "property1"
);
+ $value2 = $pageProps->getProperties( self::$title1, "property1"
);
$this->assertEquals( $value1, $value2, "Single cache" );
}
@@ -212,10 +212,10 @@
*/
public function testMultiCache() {
$pageProps = PageProps::getInstance();
- $page1ID = $this->title1->getArticleID();
- $properties1 = $pageProps->getAllProperties( $this->title1 );
+ $page1ID = self::$title1->getArticleID();
+ $properties1 = $pageProps->getAllProperties( self::$title1 );
$this->setProperty( $page1ID, "property1", "another value" );
- $properties2 = $pageProps->getAllProperties( $this->title1 );
+ $properties2 = $pageProps->getAllProperties( self::$title1 );
$this->assertEquals( $properties1, $properties2, "Multi Cache"
);
}
@@ -229,12 +229,12 @@
*/
public function testClearCache() {
$pageProps = PageProps::getInstance();
- $page1ID = $this->title1->getArticleID();
- $pageProps->getProperties( $this->title1, "property1" );
+ $page1ID = self::$title1->getArticleID();
+ $pageProps->getProperties( self::$title1, "property1" );
$new_value = "another value";
$this->setProperty( $page1ID, "property1", $new_value );
- $pageProps->getAllProperties( $this->title1 );
- $result = $pageProps->getProperties( $this->title1, "property1"
);
+ $pageProps->getAllProperties( self::$title1 );
+ $result = $pageProps->getProperties( self::$title1, "property1"
);
$this->assertArrayHasKey( $page1ID, $result, "Found property" );
$this->assertEquals( $result[$page1ID], "another value", "Clear
cache" );
}
diff --git a/tests/phpunit/includes/TestUserRegistry.php
b/tests/phpunit/includes/TestUserRegistry.php
new file mode 100644
index 0000000..8785429
--- /dev/null
+++ b/tests/phpunit/includes/TestUserRegistry.php
@@ -0,0 +1,80 @@
+<?php
+
+class TestUserRegistry {
+
+ /** @var TestUser[] (group key => TestUser) */
+ private static $testUsers = [];
+
+ /**
+ * Get a TestUser object that the caller may modify.
+ *
+ * @param string $testSuiteName Caller's __METHOD__. Used to generate
+ * the test user's username.
+ * @param string[] $groups Groups the test user should be added to.
+ * @return TestUser
+ */
+ public static function getMutableTestUser( $testSuiteName, $groups = []
) {
+ $id = $testSuiteName . ' ' . wfRandomString( 6 );
+ $password = PasswordFactory::generateRandomPasswordString( 20 );
+ $testUser = new TestUser(
+ "TestUser $id", // username
+ "Name $id", // real name
+ "[email protected]", // e-mail
+ $groups, // groups
+ $password // password
+ );
+ $testUser->getUser()->saveSettings();
+ 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.
+ *
+ * @param string[] $groups Groups the test user should be added to.
+ * @return TestUser
+ */
+ public static function getImmutableTestUser( $groups = [] ) {
+ $groups = array_unique( $groups );
+ $key = implode( ',', $groups );
+
+ if ( !isset( self::$testUsers[$key] ) ) {
+ $id = wfRandomString( 6 );
+ // Hack! If this is the primary sysop account, make the
username be
+ // 'UTSysop', for back-compat. This will be removed
once extensions
+ // are updated not to hard-code test user names.
+ $username = $groups === [ 'bureaucrat', 'sysop' ]
+ ? 'UTSysop'
+ : "TestUser $id";
+ $username = "TestUser $id";
+ $password =
PasswordFactory::generateRandomPasswordString( 20 );
+ self::$testUsers[$key] = $testUser = new TestUser(
+ "TestUser $id", // username
+ "Name $id", // real name
+ "[email protected]", // e-mail
+ $groups, // groups
+ $password // password
+ );
+ $testUser->getUser()->saveSettings();
+ }
+
+ 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.
+ *
+ * @param string[] $groups Groups the test user should be added to.
+ * @return TestUser
+ */
+ public static function clear() {
+ self::$testUsers = [];
+ }
+}
diff --git a/tests/phpunit/includes/api/ApiCreateAccountTest.php
b/tests/phpunit/includes/api/ApiCreateAccountTest.php
index 9a83e61..4f1c7db 100644
--- a/tests/phpunit/includes/api/ApiCreateAccountTest.php
+++ b/tests/phpunit/includes/api/ApiCreateAccountTest.php
@@ -137,7 +137,7 @@
public function testExistingUser() {
$this->doApiRequest( [
'action' => 'createaccount',
- 'name' => 'Apitestsysop',
+ 'name' => self::$users['sysop']->getUser()->getName(),
'token' =>
LoginForm::getCreateaccountToken()->toString(),
'password' => 'password',
'email' => '[email protected]',
diff --git a/tests/phpunit/includes/api/ApiQueryWatchlistIntegrationTest.php
b/tests/phpunit/includes/api/ApiQueryWatchlistIntegrationTest.php
index f1f9295..e059fdc 100644
--- a/tests/phpunit/includes/api/ApiQueryWatchlistIntegrationTest.php
+++ b/tests/phpunit/includes/api/ApiQueryWatchlistIntegrationTest.php
@@ -21,23 +21,17 @@
protected function setUp() {
parent::setUp();
- self::$users['ApiQueryWatchlistIntegrationTestUser']
- = new TestUser( 'ApiQueryWatchlistIntegrationTestUser'
);
- self::$users['ApiQueryWatchlistIntegrationTestUser2']
- = new TestUser( 'ApiQueryWatchlistIntegrationTestUser2'
);
+ self::$users['ApiQueryWatchlistIntegrationTestUser'] =
$this->getTestUser( true );
+ self::$users['ApiQueryWatchlistIntegrationTestUser2'] =
$this->getTestUser( true );
$this->doLogin( 'ApiQueryWatchlistIntegrationTestUser' );
}
- private function getTestUser() {
+ private function getLoggedInTestUser() {
return
self::$users['ApiQueryWatchlistIntegrationTestUser']->getUser();
}
private function getNonLoggedInTestUser() {
return
self::$users['ApiQueryWatchlistIntegrationTestUser2']->getUser();
- }
-
- private function getSysopTestUser() {
- return self::$users['sysop']->getUser();
}
private function doPageEdit( User $user, LinkTarget $target, $content,
$summary ) {
@@ -244,7 +238,7 @@
}
private function cleanTestUsersWatchlist() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$store = $this->getWatchedItemStore();
$items = $store->getWatchedItemsForUser( $user );
foreach ( $items as $item ) {
@@ -257,7 +251,7 @@
// 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,
@@ -290,7 +284,7 @@
}
public function testIdsPropParameter() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$target = new TitleValue( 0,
'ApiQueryWatchlistIntegrationTestPage' );
$this->doPageEdit(
$user,
@@ -311,7 +305,7 @@
}
public function testTitlePropParameter() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$subjectTarget = new TitleValue( 0,
'ApiQueryWatchlistIntegrationTestPage' );
$talkTarget = new TitleValue( 1,
'ApiQueryWatchlistIntegrationTestPage' );
$this->doPageEdits(
@@ -351,7 +345,7 @@
}
public function testFlagsPropParameter() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$normalEditTarget = new TitleValue( 0,
'ApiQueryWatchlistIntegrationTestPage' );
$minorEditTarget = new TitleValue( 0,
'ApiQueryWatchlistIntegrationTestPageM' );
$botEditTarget = new TitleValue( 0,
'ApiQueryWatchlistIntegrationTestPageB' );
@@ -412,7 +406,7 @@
}
public function testUserPropParameter() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$userEditTarget = new TitleValue( 0,
'ApiQueryWatchlistIntegrationTestPage' );
$anonEditTarget = new TitleValue( 0,
'ApiQueryWatchlistIntegrationTestPageA' );
$this->doPageEdit(
@@ -447,7 +441,7 @@
}
public function testUserIdPropParameter() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$userEditTarget = new TitleValue( 0,
'ApiQueryWatchlistIntegrationTestPage' );
$anonEditTarget = new TitleValue( 0,
'ApiQueryWatchlistIntegrationTestPageA' );
$this->doPageEdit(
@@ -484,7 +478,7 @@
}
public function testCommentPropParameter() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$target = new TitleValue( 0,
'ApiQueryWatchlistIntegrationTestPage' );
$this->doPageEdit(
$user,
@@ -508,7 +502,7 @@
}
public function testParsedCommentPropParameter() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$target = new TitleValue( 0,
'ApiQueryWatchlistIntegrationTestPage' );
$this->doPageEdit(
$user,
@@ -532,7 +526,7 @@
}
public function testTimestampPropParameter() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$target = new TitleValue( 0,
'ApiQueryWatchlistIntegrationTestPage' );
$this->doPageEdit(
$user,
@@ -551,7 +545,7 @@
}
public function testSizesPropParameter() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$target = new TitleValue( 0,
'ApiQueryWatchlistIntegrationTestPage' );
$this->doPageEdit(
$user,
@@ -585,7 +579,7 @@
'Create the page'
);
$store = $this->getWatchedItemStore();
- $store->addWatch( $this->getTestUser(), $target );
+ $store->addWatch( $this->getLoggedInTestUser(), $target );
$store->updateNotificationTimestamp(
$otherUser,
$target,
@@ -620,7 +614,7 @@
}
public function testPatrolPropParameter() {
- $user = $this->getSysopTestUser();
+ $user = $this->getTestSysop()->getUser();
$this->setupPatrolledSpecificFixtures( $user );
$result = $this->doListWatchlistRequest( [ 'wlprop' =>
'patrol', ], $user );
@@ -639,7 +633,7 @@
private function createPageAndDeleteIt( LinkTarget $target ) {
$this->doPageEdit(
- $this->getTestUser(),
+ $this->getLoggedInTestUser(),
$target,
'Some Content',
'Create the page that will be deleted'
@@ -651,7 +645,7 @@
$target = new TitleValue( 0,
'ApiQueryWatchlistIntegrationTestPage' );
$this->createPageAndDeleteIt( $target );
- $this->watchPages( $this->getTestUser(), [ $target ] );
+ $this->watchPages( $this->getLoggedInTestUser(), [ $target ] );
$result = $this->doListWatchlistRequest( [ 'wlprop' =>
'loginfo', ] );
@@ -671,7 +665,7 @@
}
public function testEmptyPropParameter() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$target = new TitleValue( 0,
'ApiQueryWatchlistIntegrationTestPage' );
$this->doPageEdit(
$user,
@@ -694,7 +688,7 @@
}
public function testNamespaceParam() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$subjectTarget = new TitleValue( 0,
'ApiQueryWatchlistIntegrationTestPage' );
$talkTarget = new TitleValue( 1,
'ApiQueryWatchlistIntegrationTestPage' );
$this->doPageEdits(
@@ -729,7 +723,7 @@
}
public function testUserParam() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$otherUser = $this->getNonLoggedInTestUser();
$subjectTarget = new TitleValue( 0,
'ApiQueryWatchlistIntegrationTestPage' );
$talkTarget = new TitleValue( 1,
'ApiQueryWatchlistIntegrationTestPage' );
@@ -766,7 +760,7 @@
}
public function testExcludeUserParam() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$otherUser = $this->getNonLoggedInTestUser();
$subjectTarget = new TitleValue( 0,
'ApiQueryWatchlistIntegrationTestPage' );
$talkTarget = new TitleValue( 1,
'ApiQueryWatchlistIntegrationTestPage' );
@@ -803,7 +797,7 @@
}
public function testShowMinorParams() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$target = new TitleValue( 0,
'ApiQueryWatchlistIntegrationTestPage' );
$this->doPageEdits(
$user,
@@ -837,7 +831,7 @@
}
public function testShowBotParams() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$target = new TitleValue( 0,
'ApiQueryWatchlistIntegrationTestPage' );
$this->doBotPageEdit(
$user,
@@ -861,7 +855,7 @@
}
public function testShowAnonParams() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$target = new TitleValue( 0,
'ApiQueryWatchlistIntegrationTestPage' );
$this->doAnonPageEdit(
$target,
@@ -890,7 +884,7 @@
}
public function testShowUnreadParams() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$otherUser = $this->getNonLoggedInTestUser();
$subjectTarget = new TitleValue( 0,
'ApiQueryWatchlistIntegrationTestPage' );
$talkTarget = new TitleValue( 1,
'ApiQueryWatchlistIntegrationTestPage' );
@@ -948,7 +942,7 @@
}
public function testShowPatrolledParams() {
- $user = $this->getSysopTestUser();
+ $user = $this->getTestSysop()->getUser();
$this->setupPatrolledSpecificFixtures( $user );
$resultPatrolled = $this->doListWatchlistRequest( [
@@ -974,7 +968,7 @@
}
public function testNewAndEditTypeParameters() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$subjectTarget = new TitleValue( 0,
'ApiQueryWatchlistIntegrationTestPage' );
$talkTarget = new TitleValue( 1,
'ApiQueryWatchlistIntegrationTestPage' );
$this->doPageEdits(
@@ -1025,7 +1019,7 @@
}
public function testLogTypeParameters() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$subjectTarget = new TitleValue( 0,
'ApiQueryWatchlistIntegrationTestPage' );
$talkTarget = new TitleValue( 1,
'ApiQueryWatchlistIntegrationTestPage' );
$this->createPageAndDeleteIt( $subjectTarget );
@@ -1093,7 +1087,7 @@
}
public function testExternalTypeParameters() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$subjectTarget = new TitleValue( 0,
'ApiQueryWatchlistIntegrationTestPage' );
$talkTarget = new TitleValue( 1,
'ApiQueryWatchlistIntegrationTestPage' );
$this->doPageEdit(
@@ -1129,7 +1123,7 @@
}
public function testCategorizeTypeParameter() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$subjectTarget = new TitleValue( 0,
'ApiQueryWatchlistIntegrationTestPage' );
$categoryTarget = new TitleValue( NS_CATEGORY,
'ApiQueryWatchlistIntegrationTestCategory' );
$this->doPageEdits(
@@ -1180,7 +1174,7 @@
}
public function testLimitParam() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$target1 = new TitleValue( 0,
'ApiQueryWatchlistIntegrationTestPage' );
$target2 = new TitleValue( 1,
'ApiQueryWatchlistIntegrationTestPage' );
$target3 = new TitleValue( 0,
'ApiQueryWatchlistIntegrationTestPage2' );
@@ -1249,7 +1243,7 @@
}
public function testAllRevParam() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$target = new TitleValue( 0,
'ApiQueryWatchlistIntegrationTestPage' );
$this->doPageEdits(
$user,
@@ -1299,7 +1293,7 @@
}
public function testDirParams() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$subjectTarget = new TitleValue( 0,
'ApiQueryWatchlistIntegrationTestPage' );
$talkTarget = new TitleValue( 1,
'ApiQueryWatchlistIntegrationTestPage' );
$this->doPageEdits(
@@ -1355,7 +1349,7 @@
}
public function testStartEndParams() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$target = new TitleValue( 0,
'ApiQueryWatchlistIntegrationTestPage' );
$this->doPageEdit(
$user,
@@ -1390,7 +1384,7 @@
}
public function testContinueParam() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$target1 = new TitleValue( 0,
'ApiQueryWatchlistIntegrationTestPage' );
$target2 = new TitleValue( 1,
'ApiQueryWatchlistIntegrationTestPage' );
$target3 = new TitleValue( 0,
'ApiQueryWatchlistIntegrationTestPage2' );
@@ -1456,7 +1450,7 @@
public function testOwnerAndTokenParams() {
$target = new TitleValue( 0,
'ApiQueryWatchlistIntegrationTestPage' );
$this->doPageEdit(
- $this->getTestUser(),
+ $this->getLoggedInTestUser(),
$target,
'Some Content',
'Create the page'
@@ -1509,7 +1503,7 @@
}
public function testGeneratorWatchlistPropInfo_returnsWatchedPages() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$target = new TitleValue( 0,
'ApiQueryWatchlistIntegrationTestPage' );
$this->doPageEdit(
$user,
@@ -1541,7 +1535,7 @@
}
public function
testGeneratorWatchlistPropRevisions_returnsWatchedItemsRevisions() {
- $user = $this->getTestUser();
+ $user = $this->getLoggedInTestUser();
$target = new TitleValue( 0,
'ApiQueryWatchlistIntegrationTestPage' );
$this->doPageEdits(
$user,
diff --git a/tests/phpunit/includes/api/ApiTestCase.php
b/tests/phpunit/includes/api/ApiTestCase.php
index 246ea3d..6218a03 100644
--- a/tests/phpunit/includes/api/ApiTestCase.php
+++ b/tests/phpunit/includes/api/ApiTestCase.php
@@ -8,11 +8,6 @@
*/
protected $apiContext;
- /**
- * @var array
- */
- protected $tablesUsed = [ 'user', 'user_groups', 'user_properties' ];
-
protected function setUp() {
global $wgServer;
@@ -22,18 +17,8 @@
ApiQueryInfo::resetTokenCache(); // tokens are invalid because
we cleared the session
self::$users = [
- 'sysop' => new TestUser(
- 'Apitestsysop',
- 'Api Test Sysop',
- '[email protected]',
- [ 'sysop' ]
- ),
- 'uploader' => new TestUser(
- 'Apitestuser',
- 'Api Test User',
- '[email protected]',
- []
- )
+ 'sysop' => $this->getTestSysop( true ),
+ 'uploader' => $this->getTestUser( true ),
];
$this->setMwGlobals( [
@@ -162,6 +147,9 @@
}
protected function doLogin( $user = 'sysop' ) {
+ if ( $user === null ) {
+ $user = $this->getTestSysop();
+ }
if ( !array_key_exists( $user, self::$users ) ) {
throw new MWException( "Can not log in to undefined
user $user" );
}
diff --git a/tests/phpunit/includes/cache/GenderCacheTest.php
b/tests/phpunit/includes/cache/GenderCacheTest.php
index e329f8d..f9963d4 100644
--- a/tests/phpunit/includes/cache/GenderCacheTest.php
+++ b/tests/phpunit/includes/cache/GenderCacheTest.php
@@ -6,36 +6,29 @@
*/
class GenderCacheTest extends MediaWikiLangTestCase {
+ 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->getTestUser( true )->getUser();
+ $male->setOption( 'gender', 'male' );
+ $male->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();
+ $female = $this->getTestUser( true )->getUser();
+ $female->setOption( 'gender', 'female' );
+ $female->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();
+ $default = $this->getTestUser( true )->getUser();
+ $default->setOption( 'gender', null );
+ $default->saveSettings();
+
+ self::$nameMap = [
+ 'UTMale' => $male->getName(),
+ 'UTFemale' => $female->getName(),
+ 'UTDefaultGender' => $default->getName()
+ ];
}
/**
@@ -44,8 +37,9 @@
* @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" );
}
@@ -56,10 +50,10 @@
* @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" );
}
@@ -79,22 +73,13 @@
* 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' ],
- ];
}
}
diff --git a/tests/phpunit/includes/changes/CategoryMembershipChangeTest.php
b/tests/phpunit/includes/changes/CategoryMembershipChangeTest.php
index 1d86fb4..e44de09 100644
--- a/tests/phpunit/includes/changes/CategoryMembershipChangeTest.php
+++ b/tests/phpunit/includes/changes/CategoryMembershipChangeTest.php
@@ -30,6 +30,11 @@
private static $pageRev = null;
/**
+ * @var User
+ */
+ private static $revUser = null;
+
+ /**
* @var string
*/
private static $pageName = 'CategoryMembershipChangeTestPage';
@@ -54,6 +59,7 @@
$page = WikiPage::factory( $title );
self::$pageRev = $page->getRevision();
+ self::$revUser = User::newFromId( self::$pageRev->getUser(
Revision::RAW ) );
}
private function newChange( Revision $revision = null ) {
@@ -114,7 +120,7 @@
$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() );
@@ -135,7 +141,7 @@
$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() );
diff --git a/tests/phpunit/includes/changes/EnhancedChangesListTest.php
b/tests/phpunit/includes/changes/EnhancedChangesListTest.php
index b8be8d4..5836961 100644
--- a/tests/phpunit/includes/changes/EnhancedChangesListTest.php
+++ b/tests/phpunit/includes/changes/EnhancedChangesListTest.php
@@ -121,7 +121,7 @@
* @return RecentChange
*/
private function getEditChange( $timestamp ) {
- $user = $this->getTestUser();
+ $user = $this->getTestUser( true )->getUser();
$recentChange =
$this->testRecentChangesHelper->makeEditRecentChange(
$user, 'Cat', $timestamp, 5, 191, 190, 0, 0
);
@@ -139,25 +139,12 @@
$wikiPage = new WikiPage( Title::newFromText( 'Category:Foo' )
);
$wikiPage->doEditContent( new WikitextContent( 'Some random
text' ), 'category page created' );
- $user = $this->getTestUser();
+ $user = $this->getTestUser( true )->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 ) {
diff --git a/tests/phpunit/includes/changes/OldChangesListTest.php
b/tests/phpunit/includes/changes/OldChangesListTest.php
index 5746a61..fc3d6f6 100644
--- a/tests/phpunit/includes/changes/OldChangesListTest.php
+++ b/tests/phpunit/includes/changes/OldChangesListTest.php
@@ -151,7 +151,7 @@
}
private function getNewBotEditChange() {
- $user = $this->getTestUser();
+ $user = $this->getTestUser( true )->getUser();
$recentChange =
$this->testRecentChangesHelper->makeNewBotEditRecentChange(
$user, 'Abc', '20131103212153', 5, 191, 190, 0, 0
@@ -161,7 +161,7 @@
}
private function getLogChange( $logType, $logAction ) {
- $user = $this->getTestUser();
+ $user = $this->getTestUser( true )->getUser();
$recentChange =
$this->testRecentChangesHelper->makeLogRecentChange(
$logType, $logAction, $user, 'Abc', '20131103212153',
0, 0
@@ -171,7 +171,7 @@
}
private function getEditChange() {
- $user = $this->getTestUser();
+ $user = $this->getTestUser( true )->getUser();
$recentChange =
$this->testRecentChangesHelper->makeEditRecentChange(
$user, 'Cat', '20131103212153', 5, 191, 190, 0, 0
);
@@ -184,18 +184,8 @@
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->getTestUser( true )->getUser();
$context = $this->testRecentChangesHelper->getTestContext(
$user );
$context->setLanguage( 'qqx' );
diff --git a/tests/phpunit/includes/changes/RCCacheEntryFactoryTest.php
b/tests/phpunit/includes/changes/RCCacheEntryFactoryTest.php
index 602340b..8adcff0 100644
--- a/tests/phpunit/includes/changes/RCCacheEntryFactoryTest.php
+++ b/tests/phpunit/includes/changes/RCCacheEntryFactoryTest.php
@@ -28,131 +28,95 @@
] );
}
- /**
- * @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->getTestUser( true )->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->getTestUser( true )->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->getTestUser( true )->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 );
@@ -160,35 +124,6 @@
$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 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 ) {
@@ -308,18 +243,8 @@
];
}
- private function getTestUser() {
- $user = User::newFromName( 'TestRecentChangesUser' );
-
- if ( !$user->getId() ) {
- $user->addToDatabase();
- }
-
- return $user;
- }
-
private function getContext() {
- $user = $this->getTestUser();
+ $user = $this->getTestUser( true )->getUser();
$context = $this->testRecentChangesHelper->getTestContext(
$user );
$title = Title::newFromText( 'RecentChanges', NS_SPECIAL );
diff --git a/tests/phpunit/includes/debug/MWDebugTest.php
b/tests/phpunit/includes/debug/MWDebugTest.php
index e659af4..c1449ea 100644
--- a/tests/phpunit/includes/debug/MWDebugTest.php
+++ b/tests/phpunit/includes/debug/MWDebugTest.php
@@ -4,21 +4,18 @@
protected function setUp() {
parent::setUp();
- // Make sure MWDebug class is enabled
- static $MWDebugEnabled = false;
- if ( !$MWDebugEnabled ) {
- MWDebug::init();
- $MWDebugEnabled = true;
- }
/** Clear log before each test */
MWDebug::clearLog();
+ }
+
+ public static function setUpBeforeClass() {
+ MWDebug::init();
MediaWiki\suppressWarnings();
}
- protected function tearDown() {
- MediaWiki\restoreWarnings();
+ public static function tearDownAfterClass() {
MWDebug::deinit();
- parent::tearDown();
+ MediaWiki\restoreWarnings();
}
/**
diff --git a/tests/phpunit/includes/logging/LogFormatterTestCase.php
b/tests/phpunit/includes/logging/LogFormatterTestCase.php
index b09e5b1..61455c0 100644
--- a/tests/phpunit/includes/logging/LogFormatterTestCase.php
+++ b/tests/phpunit/includes/logging/LogFormatterTestCase.php
@@ -10,12 +10,19 @@
$row = $this->expandDatabaseRow( $row, $this->isLegacy( $extra
) );
$formatter = LogFormatter::newFromRow( $row );
-
- $this->assertEquals(
- $extra['text'],
- self::removeSomeHtml( $formatter->getActionText() ),
- 'Action text is equal to expected text'
- );
+ if ( preg_match( '/^\/.*\/$/', $extra['text'] ) ) {
+ $this->assertRegexp(
+ $extra['text'],
+ self::removeSomeHtml(
$formatter->getActionText() ),
+ 'Action text is equal to expected text'
+ );
+ } else {
+ $this->assertEquals(
+ $extra['text'],
+ self::removeSomeHtml(
$formatter->getActionText() ),
+ 'Action text is equal to expected text'
+ );
+ }
$this->assertSame( // ensure types and array key order
$extra['api'],
diff --git a/tests/phpunit/includes/logging/NewUsersLogFormatterTest.php
b/tests/phpunit/includes/logging/NewUsersLogFormatterTest.php
index c4b52f0..94a8659 100644
--- a/tests/phpunit/includes/logging/NewUsersLogFormatterTest.php
+++ b/tests/phpunit/includes/logging/NewUsersLogFormatterTest.php
@@ -113,7 +113,7 @@
],
],
[
- 'text' => 'User account UTSysop was
created by User',
+ 'text' => '/User account .* was created
by User/',
'api' => [
'userid' => 1,
],
@@ -151,7 +151,7 @@
],
],
[
- 'text' => 'User account UTSysop was
created by Sysop and password was sent by email',
+ 'text' => '/User account .* was created
by Sysop and password was sent by email/',
'api' => [
'userid' => 1,
],
diff --git a/tests/phpunit/includes/session/BotPasswordSessionProviderTest.php
b/tests/phpunit/includes/session/BotPasswordSessionProviderTest.php
index d4b1587..f2d3ea0 100644
--- a/tests/phpunit/includes/session/BotPasswordSessionProviderTest.php
+++ b/tests/phpunit/includes/session/BotPasswordSessionProviderTest.php
@@ -67,7 +67,8 @@
$passwordFactory->init( \RequestContext::getMain()->getConfig()
);
$passwordHash = $passwordFactory->newFromPlaintext( 'foobaz' );
- $userId = \CentralIdLookup::factory( 'local'
)->centralIdFromName( 'UTSysop' );
+ $sysop = $this->getTestSysop()->getUser();
+ $userId = \CentralIdLookup::factory( 'local'
)->centralIdFromName( $sysop->getName() );
$dbw = wfGetDB( DB_MASTER );
$dbw->delete(
@@ -182,7 +183,7 @@
public function testNewSessionInfoForRequest() {
$provider = $this->getProvider();
- $user = \User::newFromName( 'UTSysop' );
+ $user = $this->getTestSysop()->getUser();
$request = $this->getMock( 'FauxRequest', [ 'getIP' ] );
$request->expects( $this->any() )->method( 'getIP' )
->will( $this->returnValue( '127.0.0.1' ) );
@@ -209,7 +210,7 @@
$provider = $this->getProvider();
$provider->setLogger( $logger );
- $user = \User::newFromName( 'UTSysop' );
+ $user = $this->getTestSysop()->getUser();
$request = $this->getMock( 'FauxRequest', [ 'getIP' ] );
$request->expects( $this->any() )->method( 'getIP' )
->will( $this->returnValue( '127.0.0.1' ) );
diff --git a/tests/phpunit/includes/session/CookieSessionProviderTest.php
b/tests/phpunit/includes/session/CookieSessionProviderTest.php
index 70e89d4..a88f4e8 100644
--- a/tests/phpunit/includes/session/CookieSessionProviderTest.php
+++ b/tests/phpunit/includes/session/CookieSessionProviderTest.php
@@ -165,7 +165,7 @@
$provider->setConfig( $this->getConfig() );
$provider->setManager( new SessionManager() );
- $user = User::newFromName( 'UTSysop' );
+ $user = $this->getTestSysop()->getUser();
$id = $user->getId();
$name = $user->getName();
$token = $user->getToken( true );
@@ -392,7 +392,7 @@
$sessionId = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
$store = new TestBagOStuff();
- $user = User::newFromName( 'UTSysop' );
+ $user = $this->getTestSysop()->getUser();
$anon = new User;
$backend = new SessionBackend(
@@ -478,7 +478,7 @@
$provider->setManager( SessionManager::singleton() );
$sessionId = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
- $user = User::newFromName( 'UTSysop' );
+ $user = $this->getTestSysop()->getUser();
$this->assertFalse( $user->requiresHTTPS(), 'sanity check' );
$backend = new SessionBackend(
@@ -580,7 +580,7 @@
$sessionId = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
$store = new TestBagOStuff();
- $user = User::newFromName( 'UTSysop' );
+ $user = $this->getTestSysop()->getUser();
$anon = new User;
$backend = new SessionBackend(
diff --git a/tests/phpunit/includes/session/SessionBackendTest.php
b/tests/phpunit/includes/session/SessionBackendTest.php
index 0b5f4c2..8edd45e 100644
--- a/tests/phpunit/includes/session/SessionBackendTest.php
+++ b/tests/phpunit/includes/session/SessionBackendTest.php
@@ -360,7 +360,7 @@
}
public function testSetUser() {
- $user = User::newFromName( 'UTSysop' );
+ $user = $this->getTestSysop()->getUser();
$this->provider = $this->getMock( 'DummySessionProvider', [
'canChangeUser' ] );
$this->provider->expects( $this->any() )->method(
'canChangeUser' )
@@ -484,7 +484,7 @@
}
public function testSave() {
- $user = User::newFromName( 'UTSysop' );
+ $user = $this->getTestSysop()->getUser();
$this->store = new TestBagOStuff();
$testData = [ 'foo' => 'foo!', 'bar', [ 'baz', null ] ];
@@ -733,7 +733,7 @@
}
public function testRenew() {
- $user = User::newFromName( 'UTSysop' );
+ $user = $this->getTestSysop()->getUser();
$this->store = new TestBagOStuff();
$testData = [ 'foo' => 'foo!', 'bar', [ 'baz', null ] ];
@@ -829,7 +829,7 @@
$handler->enable = true;
}
- $backend = $this->getBackend( User::newFromName( 'UTSysop' ) );
+ $backend = $this->getBackend( $this->getTestSysop()->getUser()
);
\TestingAccessWrapper::newFromObject( $backend
)->usePhpSessionHandling = true;
$resetSingleton = TestUtils::setSessionManagerSingleton(
$this->manager );
diff --git a/tests/phpunit/includes/session/SessionManagerTest.php
b/tests/phpunit/includes/session/SessionManagerTest.php
index 5f387ea..dadfbe9 100644
--- a/tests/phpunit/includes/session/SessionManagerTest.php
+++ b/tests/phpunit/includes/session/SessionManagerTest.php
@@ -283,13 +283,15 @@
$this->assertFalse( $request->unpersist1 );
$this->assertFalse( $request->unpersist2 );
+ $userName = $this->getTestSysop()->getUser()->getName();
+
// Unusable session info
$this->logger->setCollect( true );
$request->info1 = new SessionInfo( SessionInfo::MAX_PRIORITY, [
'provider' => $provider1,
'id' => ( $id1 = $manager->generateSessionId() ),
'persisted' => true,
- 'userInfo' => UserInfo::newFromName( 'UTSysop', false ),
+ 'userInfo' => UserInfo::newFromName( $userName, false ),
'idIsSafe' => true,
] );
$request->info2 = new SessionInfo( SessionInfo::MIN_PRIORITY, [
@@ -317,7 +319,7 @@
'provider' => $provider2,
'id' => ( $id2 = $manager->generateSessionId() ),
'persisted' => true,
- 'userInfo' => UserInfo::newFromName( 'UTSysop', false ),
+ 'userInfo' => UserInfo::newFromName( $userName, false ),
'idIsSafe' => true,
] );
$session = $manager->getSessionForRequest( $request );
@@ -333,7 +335,7 @@
'provider' => $provider1,
'id' => ( $id1 = $manager->generateSessionId() ),
'persisted' => false,
- 'userInfo' => UserInfo::newFromName( 'UTSysop', true ),
+ 'userInfo' => UserInfo::newFromName( $userName, true ),
'idIsSafe' => true,
] );
$request->info2 = null;
@@ -368,7 +370,7 @@
$this->logger->setCollect( true );
$id = $manager->generateSessionId();
$this->store->setSession( $id, [ 'metadata' => [
- 'userId' => User::idFromName( 'UTSysop' ),
+ 'userId' => $this->getTestSysop()->getUser()->getId(),
'userToken' => 'bad',
] ] );
@@ -643,7 +645,7 @@
}
public function testInvalidateSessionsForUser() {
- $user = User::newFromName( 'UTSysop' );
+ $user = $this->getTestSysop()->getUser();
$manager = $this->getManager();
$providerBuilder = $this->getMockBuilder(
'DummySessionProvider' )
@@ -897,11 +899,12 @@
$session = SessionManager::getGlobalSession();
// Can't create an already-existing user
- $user = User::newFromName( 'UTSysop' );
+ $user = User::newFromName(
$this->getTestSysop()->getUser()->getName() );
$id = $user->getId();
+ $name = $user->getName();
$this->assertFalse( $manager->autoCreateUser( $user ) );
$this->assertSame( $id, $user->getId() );
- $this->assertSame( 'UTSysop', $user->getName() );
+ $this->assertSame( $name, $user->getName() );
$this->assertSame( [], $logger->getBuffer() );
$logger->clearBuffer();
@@ -1195,8 +1198,9 @@
return $rMethod->invokeArgs( $manager, [ &$info,
$request ] );
};
- $userInfo = UserInfo::newFromName( 'UTSysop', true );
- $unverifiedUserInfo = UserInfo::newFromName( 'UTSysop', false );
+ $userName = $this->getTestSysop()->getUser()->getName();
+ $userInfo = UserInfo::newFromName( $userName, true );
+ $unverifiedUserInfo = UserInfo::newFromName( $userName, false );
$id = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
$metadata = [
@@ -1540,7 +1544,7 @@
// Lookup user by name
$this->store->setSessionMeta(
- $id, [ 'userId' => 0, 'userName' => 'UTSysop',
'userToken' => null ] + $metadata
+ $id, [ 'userId' => 0, 'userName' => $userName,
'userToken' => null ] + $metadata
);
$info = new SessionInfo( SessionInfo::MIN_PRIORITY, [
'provider' => $provider,
diff --git a/tests/phpunit/includes/session/UserInfoTest.php
b/tests/phpunit/includes/session/UserInfoTest.php
index c38edd6..35a34b1 100644
--- a/tests/phpunit/includes/session/UserInfoTest.php
+++ b/tests/phpunit/includes/session/UserInfoTest.php
@@ -34,7 +34,7 @@
$this->assertSame( 'Invalid ID', $ex->getMessage() );
}
- $user = User::newFromName( 'UTSysop' );
+ $user = $this->getTestUser()->getUser();
$userinfo = UserInfo::newFromId( $user->getId() );
$this->assertFalse( $userinfo->isAnon() );
$this->assertFalse( $userinfo->isVerified() );
@@ -69,7 +69,7 @@
}
// User name that exists
- $user = User::newFromName( 'UTSysop' );
+ $user = $this->getTestSysop()->getUser();
$userinfo = UserInfo::newFromName( $user->getName() );
$this->assertFalse( $userinfo->isAnon() );
$this->assertFalse( $userinfo->isVerified() );
@@ -124,7 +124,7 @@
public function testNewFromUser() {
// User that exists
- $user = User::newFromName( 'UTSysop' );
+ $user = $this->getTestSysop()->getUser();
$userinfo = UserInfo::newFromUser( $user );
$this->assertFalse( $userinfo->isAnon() );
$this->assertFalse( $userinfo->isVerified() );
diff --git a/tests/phpunit/includes/user/BotPasswordTest.php
b/tests/phpunit/includes/user/BotPasswordTest.php
index 629c6e5..3a6a156 100644
--- a/tests/phpunit/includes/user/BotPasswordTest.php
+++ b/tests/phpunit/includes/user/BotPasswordTest.php
@@ -7,6 +7,13 @@
* @group Database
*/
class BotPasswordTest extends MediaWikiTestCase {
+
+ /** @var TestUser */
+ private $testUser;
+
+ /** @var string */
+ private $testUserName;
+
protected function setUp() {
parent::setUp();
@@ -20,11 +27,14 @@
'wgUserrightsInterwikiDelimiter' => '@',
] );
+ $this->testUser = $this->getTestUser( true );
+ $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' );
@@ -82,7 +92,7 @@
}
public function testBasics() {
- $user = User::newFromName( 'UTSysop' );
+ $user = $this->testUser->getUser();
$bp = BotPassword::newFromUser( $user, 'BotPassword' );
$this->assertInstanceOf( 'BotPassword', $bp );
$this->assertTrue( $bp->isSaved() );
@@ -107,7 +117,7 @@
}
public function testUnsaved() {
- $user = User::newFromName( 'UTSysop' );
+ $user = $this->testUser->getUser();
$bp = BotPassword::newUnsaved( [
'user' => $user,
'appId' => 'DoesNotExist'
@@ -132,7 +142,7 @@
$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'
@@ -142,7 +152,7 @@
$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'
@@ -159,7 +169,7 @@
'appId' => str_repeat( 'X',
BotPassword::APPID_MAXLENGTH + 1 ),
] ) );
$this->assertNull( BotPassword::newUnsaved( [
- 'user' => 'UTSysop',
+ 'user' => $this->testUserName,
'appId' => 'Ok',
] ) );
$this->assertNull( BotPassword::newUnsaved( [
@@ -200,7 +210,7 @@
$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() );
@@ -212,7 +222,7 @@
$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' ) );
@@ -221,7 +231,7 @@
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 );
@@ -235,7 +245,7 @@
$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 );
@@ -257,7 +267,7 @@
$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
@@ -265,9 +275,9 @@
$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
);
@@ -275,11 +285,11 @@
$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", 'UTSysopPassword', new FauxRequest );
$this->assertEquals( Status::newFatal( 'wrongpassword' ),
$status );
// Success!
@@ -289,7 +299,7 @@
$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();
diff --git a/tests/phpunit/includes/user/CentralIdLookupTest.php
b/tests/phpunit/includes/user/CentralIdLookupTest.php
index 1786261..8d0f424 100644
--- a/tests/phpunit/includes/user/CentralIdLookupTest.php
+++ b/tests/phpunit/includes/user/CentralIdLookupTest.php
@@ -45,7 +45,7 @@
$this->getMockForAbstractClass( 'CentralIdLookup' )
);
- $user = User::newFromName( 'UTSysop' );
+ $user = $this->getTestSysop()->getUser();
$this->assertSame( $user, $mock->checkAudience( $user ) );
$user = $mock->checkAudience( CentralIdLookup::AUDIENCE_PUBLIC
);
diff --git a/tests/phpunit/includes/user/LocalIdLookupTest.php
b/tests/phpunit/includes/user/LocalIdLookupTest.php
index c86fb6c..7f2ba89 100644
--- a/tests/phpunit/includes/user/LocalIdLookupTest.php
+++ b/tests/phpunit/includes/user/LocalIdLookupTest.php
@@ -18,18 +18,14 @@
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->getTestUser( true
)->getUser();
}
- User::newFromName( 'UTLocalIdLookup1' )->addGroup(
'local-id-lookup-test' );
+ $sysop = $this->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,
@@ -37,8 +33,8 @@
$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,
@@ -46,9 +42,14 @@
$block->insert();
}
+ public function getLookupUser() {
+ return $this->getTestUser( false, [ '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' );
@@ -56,12 +57,15 @@
$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' );
@@ -73,7 +77,7 @@
public function testLookupUserNames() {
$lookup = new LocalIdLookup();
- $user1 = User::newFromName( 'UTLocalIdLookup1' );
+ $user1 = $this->getLookupUser();
$user2 = User::newFromName( 'UTLocalIdLookup2' );
$this->assertTrue( $user1->isAllowed( 'hideuser' ), 'sanity
check' );
@@ -81,12 +85,15 @@
$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' );
@@ -98,7 +105,7 @@
public function testIsAttached() {
$lookup = new LocalIdLookup();
- $user1 = User::newFromName( 'UTLocalIdLookup1' );
+ $user1 = $this->getLookupUser();
$user2 = User::newFromName( 'DoesNotExist' );
$this->assertTrue( $lookup->isAttached( $user1 ) );
@@ -130,7 +137,7 @@
$lookup = new LocalIdLookup();
$this->assertSame(
$sharedDB && $sharedTable && $localDBSet,
- $lookup->isAttached( User::newFromName(
'UTLocalIdLookup1' ), 'shared' )
+ $lookup->isAttached( $this->getLookupUser(), 'shared' )
);
}
diff --git a/tests/phpunit/includes/user/UserTest.php
b/tests/phpunit/includes/user/UserTest.php
index c9b6929..5b0ba7f 100644
--- a/tests/phpunit/includes/user/UserTest.php
+++ b/tests/phpunit/includes/user/UserTest.php
@@ -213,11 +213,7 @@
* @covers User::getEditCount
*/
public function testEditCount() {
- $user = User::newFromName( 'UnitTestUser' );
-
- if ( !$user->getId() ) {
- $user->addToDatabase();
- }
+ $user = $this->getTestUser( true )->getUser();
// let the user have a few (3) edits
$page = WikiPage::factory( Title::newFromText(
'Help:UserTest_EditCount' ) );
@@ -249,17 +245,13 @@
* @covers User::getOption
*/
public function testOptions() {
- $user = User::newFromName( 'UnitTestUser' );
-
- if ( !$user->getId() ) {
- $user->addToDatabase();
- }
+ $user = $this->getTestUser( true )->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' ) );
}
@@ -298,7 +290,7 @@
'MinimalPasswordLength' => 6,
'PasswordCannotMatchUsername'
=> true,
'PasswordCannotMatchBlacklist'
=> true,
- 'MaximalPasswordLength' => 30,
+ 'MaximalPasswordLength' => 40,
],
],
'checks' => [
@@ -311,7 +303,9 @@
],
] );
- $user = User::newFromName( 'Useruser' );
+
+ $user = $this->getTestUser()->getUser();
+
// Sanity
$this->assertTrue( $user->isValidPassword( 'Password1234' ) );
@@ -322,18 +316,19 @@
$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' ) );
}
@@ -389,29 +384,23 @@
* @covers User::equals
*/
public function testEquals() {
- $first = User::newFromName( 'EqualUser' );
- $second = User::newFromName( 'EqualUser' );
+ $first = $this->getTestUser( true )->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->getTestUser( true )->getUser();
+ $fourth = $this->getTestUser( true )->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->getTestUser( true )->getUser();
+ $fifth = User::newFromId( $user->getId() );
+ $sixth = User::newFromName( $user->getName() );
$this->assertTrue( $fifth->equals( $sixth ) );
}
@@ -419,7 +408,7 @@
* @covers User::getId
*/
public function testGetId() {
- $user = User::newFromName( 'UTSysop' );
+ $user = $this->getTestUser()->getUser();
$this->assertTrue( $user->getId() > 0 );
}
@@ -429,7 +418,7 @@
* @covers User::isAnon
*/
public function testLoggedIn() {
- $user = User::newFromName( 'UTSysop' );
+ $user = $this->getTestUser( true )->getUser();
$this->assertTrue( $user->isLoggedIn() );
$this->assertFalse( $user->isAnon() );
@@ -447,7 +436,8 @@
* @covers User::checkAndSetTouched
*/
public function testCheckAndSetTouched() {
- $user = TestingAccessWrapper::newFromObject( User::newFromName(
'UTSysop' ) );
+ $user = $this->getTestUser( true )->getUser();
+ $user = TestingAccessWrapper::newFromObject( $user );
$this->assertTrue( $user->isLoggedIn() );
$touched = $user->getDBTouched();
--
To view, visit https://gerrit.wikimedia.org/r/289369
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I17ef1f519759c5e7796c259282afe730ef722e96
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Ori.livneh <[email protected]>
Gerrit-Reviewer: Hashar <[email protected]>
Gerrit-Reviewer: Krinkle <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits