Addshore has submitted this change and it was merged. Change subject: Revert "General further tidy up of the test base" ......................................................................
Revert "General further tidy up of the test base" This reverts commit f5c2173bd9f4bb43a07838d7c8d3585d2568baa7. Change-Id: I14566d0c07aef72d41c089e7478af8fd09fb9d8f --- M repo/tests/phpunit/includes/api/BotEditTest.php M repo/tests/phpunit/includes/api/GetEntitiesTest.php M repo/tests/phpunit/includes/api/LinkTitlesTest.php M repo/tests/phpunit/includes/api/ModifyEntityTestBase.php 4 files changed, 84 insertions(+), 42 deletions(-) Approvals: Addshore: Looks good to me, approved jenkins-bot: Verified diff --git a/repo/tests/phpunit/includes/api/BotEditTest.php b/repo/tests/phpunit/includes/api/BotEditTest.php index 5d0108a..2162568 100644 --- a/repo/tests/phpunit/includes/api/BotEditTest.php +++ b/repo/tests/phpunit/includes/api/BotEditTest.php @@ -61,18 +61,16 @@ protected static $baseOfItemIds = 1; public function setUp() { + global $wgUser; parent::setUp(); - self::$users['wbbot'] = new TestUser( + ApiTestCase::$users['wbbot'] = new TestUser( 'Apitestbot', 'Api Test Bot', 'api_test_...@example.com', array( 'bot' ) ); - - $this->setMwGlobals( array( - 'wgUser' => self::$users['wbbot']->user, - ) ); + $wgUser = self::$users['wbbot']->user; $this->login( 'wbbot' ); } diff --git a/repo/tests/phpunit/includes/api/GetEntitiesTest.php b/repo/tests/phpunit/includes/api/GetEntitiesTest.php index 908c8a8..a2fb930 100644 --- a/repo/tests/phpunit/includes/api/GetEntitiesTest.php +++ b/repo/tests/phpunit/includes/api/GetEntitiesTest.php @@ -131,16 +131,6 @@ $this->assertEquals( 1, count( $res['entities'] ) ); } - function provideEntityHandles() { - $handles = array(); - - foreach ( self::getEntityInput() as $handle => $entity ) { - $handles[] = array( $handle ); - } - - return $handles; - } - public static function provideGetItemByTitle() { $calls = array(); $handles = static::getEntityHandles(); diff --git a/repo/tests/phpunit/includes/api/LinkTitlesTest.php b/repo/tests/phpunit/includes/api/LinkTitlesTest.php index d20d31e..92e153a 100644 --- a/repo/tests/phpunit/includes/api/LinkTitlesTest.php +++ b/repo/tests/phpunit/includes/api/LinkTitlesTest.php @@ -175,6 +175,16 @@ $this->assertEquals( $totitle, $links[$tosite], 'wrong link target' ); } + if ( $cleanUp ) { + // This sucks really bad, but it's the only way we can do this + // for several reasons: + // This tests assume the entities to not being reseted during + // run time (so we can't use the setUp/ tearDown functions for + // this) but it changes them so they have to be cleared out at + // some point. + $this->resetEntities(); + } + $this->assertTrue( true ); } diff --git a/repo/tests/phpunit/includes/api/ModifyEntityTestBase.php b/repo/tests/phpunit/includes/api/ModifyEntityTestBase.php index 78d5971..cd5622c 100644 --- a/repo/tests/phpunit/includes/api/ModifyEntityTestBase.php +++ b/repo/tests/phpunit/includes/api/ModifyEntityTestBase.php @@ -57,7 +57,9 @@ protected static $entityInput = null; // entities in input format, using handles as keys protected static $entityOutput = array(); // entities in output format, using handles as keys + protected static $loginSession = null; protected static $loginUser = null; + protected static $token = null; protected $setUpComplete = false; @@ -66,26 +68,34 @@ } public function setUp() { + global $wgUser; parent::setUp(); - \TestSites::insertIntoDb(); + static $hasSites = false; + + if ( !$hasSites ) { + \TestSites::insertIntoDb(); + $hasSites = true; + } self::$usepost = Settings::get( 'apiInDebug' ) ? Settings::get( 'apiDebugWithPost' ) : true; self::$usetoken = Settings::get( 'apiInDebug' ) ? Settings::get( 'apiDebugWithTokens' ) : true; self::$userights = Settings::get( 'apiInDebug' ) ? Settings::get( 'apiDebugWithRights' ) : true; - self::$users['wbeditor'] = new TestUser( + ApiTestCase::$users['wbeditor'] = new TestUser( 'Apitesteditor', 'Api Test Editor', 'api_test_edi...@example.com', array( 'wbeditor' ) ); - $this->setMwGlobals( array( - 'wgUser' => self::$users['wbeditor']->user, - ) ); + $wgUser = self::$users['wbeditor']->user; $this->login(); + + //TODO: preserve session and token between calls?! + self::$loginSession = false; + self::$token = false; self::initEntities(); $this->setUpComplete = true; @@ -101,8 +111,9 @@ } self::$entityInput = array(); + $data = self::makeEntityData(); - foreach ( self::makeEntityData() as $entity ) { + foreach ( $data as $entity ) { self::$entityInput[ $entity['handle'] ] = $entity; } } @@ -112,7 +123,7 @@ * This data is used in particular by createEntities(). * Note that test Entities are identified by "handles". */ - protected static function makeEntityData() { + static function makeEntityData() { return array( array( "handle" => "Empty", @@ -258,9 +269,9 @@ * Performs a login, if necessary, and returns the resulting session. */ function login( $user = 'wbeditor' ) { - $data = self::doLogin( $user ); + self::doLogin( $user ); self::$loginUser = self::$users[ $user ]; - return $data; + return self::$loginSession; } /** @@ -280,24 +291,42 @@ return; } - foreach ( self::getEntityInput() as $entity ) { + self::initEntities(); + $token = $this->getToken(); + + foreach ( self::$entityInput as $entity ) { $handle = $entity['handle']; - $createdEntity = $this->setEntity( $entity ); + $createdEntity = $this->setEntity( $entity, $token ); self::$entityOutput[ $handle ] = $createdEntity; } } /** + * Restores all well known entities test in the database to their original state. + */ + function resetEntities() { + $this->createEntities(); + $token = $this->getToken(); + + foreach ( self::$entityInput as $handle => $entity ) { + $entity['id'] = $this->getEntityId( $handle ); + + $data = $this->setEntity( $entity, $token ); + + self::$entityOutput[ $handle ] = $data; + } + } + + /** * Restores the entity with the given handle to its original state - * @param $handle of entity to reset - * @return mixed */ function resetEntity( $handle ) { $entity = $this->getEntityInput( $handle ); $entity['id'] = $this->getEntityId( $handle ); - $data = $this->setEntity( $entity ); + $token = $this->getToken(); + $data = $this->setEntity( $entity, $token ); self::$entityOutput[ $handle ] = $data; return $data; @@ -305,15 +334,13 @@ /** * Creates or updates a single entity in the database - * @param $data array|string data to set as array of json string - * @return mixed - * @throws \MWException */ - function setEntity( $data ) { + function setEntity( $data, $token ) { $params = array( 'action' => 'wbeditentity', 'clear' => true, 'format' => 'json', // make sure IDs are used as keys. + 'token' => $token, ); if ( !is_string($data) ) { @@ -333,7 +360,12 @@ $params['data'] = $data; - list( $res,, ) = $this->doApiRequestWithToken( $params ); + list( $res,, ) = $this->doApiRequest( + $params, + null, + false, + self::$users['wbeditor']->user + ); if ( !isset( $res['success'] ) || !isset( $res['entity'] ) ) { throw new \MWException( "failed to create entity" ); @@ -345,17 +377,12 @@ /** * Returns the entity for the given handle, in input format. */ - static function getEntityInput( $handle = null ) { - self::initEntities(); - - if( $handle === null ){ - return self::$entityInput; - } - + static function getEntityInput( $handle ) { if ( !is_string( $handle ) ) { trigger_error( "bad handle: $handle", E_USER_ERROR ); } + self::initEntities(); return self::$entityInput[ $handle ]; } @@ -378,10 +405,27 @@ } /** + * data provider for passing each entity handle to the test function. + */ + function provideEntityHandles() { + self::initEntities(); + + $handles = array(); + + foreach ( self::$entityInput as $handle => $entity ) { + $handles[] = array( $handle ); + } + + return $handles; + } + + /** * returns the list handles for the well known test entities. */ static function getEntityHandles() { - return array_keys( self::getEntityInput() ); + self::initEntities(); + + return array_keys( self::$entityInput ); } /** -- To view, visit https://gerrit.wikimedia.org/r/78048 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I14566d0c07aef72d41c089e7478af8fd09fb9d8f Gerrit-PatchSet: 3 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Addshore <addshorew...@gmail.com> Gerrit-Reviewer: Addshore <addshorew...@gmail.com> Gerrit-Reviewer: jenkins-bot _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits