Hoo man has uploaded a new change for review. https://gerrit.wikimedia.org/r/74930
Change subject: Revert "Revert "Revert "Revert "Automatically register repo tests."""" ...................................................................... Revert "Revert "Revert "Revert "Automatically register repo tests."""" This reverts commit d0b951beeff2e4dc2693177be2acd6b0cd14f8a2. Change-Id: Idc11440312fa229a2c53aee092756cbfc9ea8381 --- M repo/Wikibase.hooks.php M repo/tests/phpunit/includes/EditEntityTest.php M repo/tests/phpunit/includes/api/ModifyEntityTestBase.php M repo/tests/phpunit/includes/api/PermissionsTest.php 4 files changed, 73 insertions(+), 160 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/30/74930/1 diff --git a/repo/Wikibase.hooks.php b/repo/Wikibase.hooks.php index fe1e9a0..695e855 100644 --- a/repo/Wikibase.hooks.php +++ b/repo/Wikibase.hooks.php @@ -1,10 +1,13 @@ <?php namespace Wikibase; - +use RecursiveDirectoryIterator; +use RecursiveIteratorIterator; use RequestContext; +use SplFileInfo; use Title, Language, User, Revision, WikiPage, EditPage, ContentHandler, Html, MWException; use Wikibase\Repo\WikibaseRepo; + /** * File defining the hook handlers for the Wikibase extension. @@ -150,102 +153,19 @@ */ public static function registerUnitTests( array &$files ) { // @codeCoverageIgnoreStart - $testFiles = array( - 'Autocomment', - 'ClaimSummaryBuilder', - 'EditEntity', - 'EntityView', - 'ItemMove', - 'ItemContentDiffView', - 'ItemMove', - 'ItemView', - 'LabelDescriptionDuplicateDetector', - 'MultiLangConstraintDetector', - 'NamespaceUtils', - 'Summary', - 'WikibaseRepo', + $directoryIterator = new RecursiveDirectoryIterator( __DIR__ . '/tests/phpunit/' ); - 'actions/EditEntityAction', - 'actions/ViewEntityAction', - - 'api/BotEdit', - 'api/EditPage', - 'api/GetEntities', - 'api/SetLabel', - 'api/SetDescription', - 'api/LinkTitles', - 'api/Permissions', - 'api/SetAliases', - 'api/EditEntity', - 'api/SetSiteLink', - 'api/CreateClaim', - 'api/GetClaims', - 'api/RemoveClaims', - 'api/SetClaimValue', - 'api/SetReference', - 'api/RemoveReferences', - 'api/SetClaim', - 'api/RemoveQualifiers', - 'api/SetQualifier', - 'api/SnakValidationHelper', - 'api/ItemByTitleHelper', - - 'changeop/ChangeOps', - 'changeop/ChangeOpLabel', - 'changeop/ChangeOpDescription', - 'changeop/ChangeOpAliases', - 'changeop/ChangeOpSiteLink', - - 'content/EntityContentFactory', - 'content/EntityHandler', - 'content/ItemContent', - 'content/ItemHandler', - 'content/PropertyContent', - 'content/PropertyHandler', - - 'LinkedData/EntityDataSerializationService', - 'LinkedData/EntityDataRequestHandler', - 'LinkedData/EntityDataUriManager', - - 'rdf/RdfBuilder', - 'rdf/RdfSerializer', - - 'specials/SpecialEntityData', - 'specials/SpecialMyLanguageFallbackChain', - 'specials/SpecialNewItem', - 'specials/SpecialNewProperty', - 'specials/SpecialItemDisambiguation', - 'specials/SpecialItemByTitle', - 'specials/SpecialSetDescription', - 'specials/SpecialSetLabel', - 'specials/SpecialSetAliases', - - 'store/IdGenerator', - 'store/StoreFactory', - 'store/Store', - - 'store/sql/DispatchStats', - 'store/sql/EntityPerPageBuilder', - 'store/sql/SqlIdGenerator', - 'store/sql/TermSqlIndex', - 'store/sql/TermSearchKeyBuilder', - - 'updates/ItemDeletionUpdate', - 'updates/ItemModificationUpdate', - - 'Validators/SnakValidator', - ); - - foreach ( $testFiles as $file ) { - $file = __DIR__ . '/tests/phpunit/includes/' . $file . 'Test.php'; - - if ( !file_exists( $file ) ) { - throw new MWException( "Test file not found: $file" ); + /** + * @var SplFileInfo $fileInfo + */ + $ourFiles = array(); + foreach ( new RecursiveIteratorIterator( $directoryIterator ) as $fileInfo ) { + if ( substr( $fileInfo->getFilename(), -8 ) === 'Test.php' ) { + $ourFiles[] = $fileInfo->getPathname(); } - - $files[] = $file; } + $files = array_merge( $files, $ourFiles ); return true; // @codeCoverageIgnoreEnd } diff --git a/repo/tests/phpunit/includes/EditEntityTest.php b/repo/tests/phpunit/includes/EditEntityTest.php index 9ec051e..2213cae 100644 --- a/repo/tests/phpunit/includes/EditEntityTest.php +++ b/repo/tests/phpunit/includes/EditEntityTest.php @@ -65,14 +65,14 @@ } protected static function getTestRevisions() { - global $wgUser; + $user = self::getUser( "EditEntityTestUser2" ); if ( self::$testRevisions === null ) { $otherUser = self::getUser( "EditEntityTestUser2" ); $itemContent = ItemContent::newEmpty(); $itemContent->getEntity()->setLabel('en', "foo"); - $itemContent->save( "rev 0", $wgUser, EDIT_NEW ); + $itemContent->save( "rev 0", $user, EDIT_NEW ); self::$testRevisions[] = $itemContent->getWikiPage()->getRevision(); $itemContent = $itemContent->copy(); @@ -82,13 +82,13 @@ $itemContent = $itemContent->copy(); $itemContent->getEntity()->setLabel('de', "bar"); - $itemContent->save( "rev 2", $wgUser, EDIT_UPDATE ); + $itemContent->save( "rev 2", $user, EDIT_UPDATE ); self::$testRevisions[] = $itemContent->getWikiPage()->getRevision(); $itemContent = $itemContent->copy(); $itemContent->getEntity()->setLabel('en', "test"); $itemContent->getEntity()->setDescription('en', "more testing"); - $itemContent->save( "rev 3", $wgUser, EDIT_UPDATE ); + $itemContent->save( "rev 3", $user, EDIT_UPDATE ); self::$testRevisions[] = $itemContent->getWikiPage()->getRevision(); } @@ -99,13 +99,13 @@ protected $userGroups; function setUp() { - global $wgGroupPermissions, $wgUser; + global $wgGroupPermissions; global $wgOut, $wgTitle; parent::setUp(); $this->permissions = $wgGroupPermissions; - $this->userGroups = $wgUser->getGroups(); + $this->userGroups = array( 'user' ); if ( $wgTitle === null ) { $wgTitle = \Title::newFromText( "Test" ); @@ -122,21 +122,9 @@ } function tearDown() { - global $wgGroupPermissions, $wgUser; + global $wgGroupPermissions; $wgGroupPermissions = $this->permissions; - - $userGroups = $wgUser->getGroups(); - - foreach ( array_diff( $this->userGroups, $userGroups ) as $group ) { - $wgUser->addGroup( $group ); - } - - foreach ( array_diff( $userGroups, $this->userGroups ) as $group ) { - $wgUser->removeGroup( $group ); - } - - $wgUser->getEffectiveGroups( true ); // recache parent::tearDown(); } @@ -211,17 +199,10 @@ * @dataProvider provideHasEditConflict */ public function testHasEditConflict( $inputData, $baseRevisionIdx, $expectedConflict, $expectedFix, array $expectedData = null ) { - global $wgUser; - /* @var $content \Wikibase\EntityContent */ /* @var $revision \Revision */ - static $user = null; - if ( !$user ) { - $user = \User::newFromId( 0 ); - $user->setName( '127.0.0.1' ); - } - $this->setMwGlobals( 'wgUser', $user ); + $user = self::getUser( 'EntityEditEntityTestHasEditConflictUser' ); $revisions = self::getTestRevisions(); @@ -254,7 +235,7 @@ } // save entity ---------------------------------- - $editEntity = new EditEntity( $content, $wgUser, $baseRevisionId ); + $editEntity = new EditEntity( $content, $user, $baseRevisionId ); $conflict = $editEntity->hasEditConflict(); $this->assertEquals( $expectedConflict, $conflict, 'hasEditConflict()' ); @@ -294,44 +275,54 @@ * @dataProvider provideAttemptSaveWithLateConflict */ public function testAttemptSaveWithLateConflict( $baseRevId, $expectedConflict ) { - global $wgUser; + $user = self::getUser( "EditEntityTestUser" ); // create item $content = ItemContent::newEmpty(); $content->getEntity()->setLabel( 'en', 'Test' ); - $content->save( "rev 0", $wgUser, EDIT_NEW ); - + $content->save( "rev 0", $user, EDIT_NEW ); // begin editing the entity $content->getEntity()->setLabel( 'en', 'Trust' ); - $editEntity = new EditEntity( $content, $wgUser, $baseRevId ); + $editEntity = new EditEntity( $content, $user, $baseRevId ); $editEntity->getCurrentRevision(); // make sure EditEntity has page and revision $this->assertEquals( $baseRevId, $editEntity->doesCheckForEditConflicts(), 'doesCheckForEditConflicts()' ); // create independent EntityContent instance for the same entity, and modify and save it $page = \WikiPage::factory( $content->getTitle() ); + + $user2 = self::getUser( "EditEntityTestUser2" ); + + /* @var EntityContent $content2 */ $content2 = $page->getContent(); $content2->getEntity()->setLabel( 'en', 'Toast' ); - $content2->save( 'Trolololo!', null, EDIT_UPDATE ); + $content2->save( 'Trolololo!', $user2, EDIT_UPDATE ); // now try to save the original edit. The conflict should still be detected - $token = $wgUser->getEditToken(); + $token = $user->getEditToken(); $status = $editEntity->attemptSave( "Testing", EDIT_UPDATE, $token ); + $id = $content->getEntity()->getId()->__toString(); + + if ( $status->isOK() ) { + $statusMessage = "Status ($id): OK"; + } else { + $statusMessage = "Status ($id): " . $status->getWikiText(); + } + $this->assertNotEquals( $expectedConflict, $status->isOK(), - 'saving should have failed late if and only if a base rev was provided' ); + "Saving should have failed late if and only if a base rev was provided.\n$statusMessage" ); $this->assertEquals( $expectedConflict, $editEntity->hasError(), - 'saving should have failed late if and only if a base rev was provided' ); + "Saving should have failed late if and only if a base rev was provided.\n$statusMessage" ); $this->assertEquals( $expectedConflict, $status->hasMessage( 'edit-conflict' ), - 'saving should have failed late if and only if a base rev was provided' ); + "Saving should have failed late if and only if a base rev was provided.\n$statusMessage" ); $this->assertEquals( $expectedConflict, $editEntity->showErrorPage(), - 'if and only if there was an error, an error page should be show' ); - + "If and only if there was an error, an error page should be shown.\n$statusMessage" ); } public function testUserWasLastToEdit() { @@ -425,7 +416,7 @@ } protected function prepareItemForPermissionCheck( $group, $permissions, $create ) { - global $wgUser; + $user = self::getUser( "EditEntityTestUser" ); $content = ItemContent::newEmpty(); @@ -434,8 +425,8 @@ $content->save( "testing", null, EDIT_NEW ); } - if ( !in_array( $group, $wgUser->getEffectiveGroups() ) ) { - $wgUser->addGroup( $group ); + if ( !in_array( $group, $user->getEffectiveGroups() ) ) { + $user->addGroup( $group ); } if ( $permissions !== null ) { @@ -456,7 +447,8 @@ $content = $this->prepareItemForPermissionCheck( $group, $permissions, $create ); $content->getItem()->setLabel( 'xx', 'Foo' ); - $edit = new EditEntity( $content ); + $user = self::getUser( "EditEntityTestUser" ); + $edit = new EditEntity( $content, $user ); $edit->checkEditPermissions(); @@ -468,14 +460,13 @@ * @dataProvider dataCheckEditPermissions */ public function testAttemptSavePermissions( $group, $permissions, $create, $expectedOK ) { - global $wgUser; + $user = self::getUser( "EditEntityTestUser" ); $content = $this->prepareItemForPermissionCheck( $group, $permissions, $create ); $content->getItem()->setLabel( 'xx', 'Foo' ); - $token = $wgUser->getEditToken(); - - $edit = new EditEntity( $content ); + $token = $user->getEditToken(); + $edit = new EditEntity( $content, $user ); $edit->attemptSave( "testing", ( $content->isNew() ? EDIT_NEW : 0 ), $token ); @@ -676,14 +667,14 @@ * @dataProvider provideIsTokenOk */ public function testIsTokenOk( $token, $shouldWork ) { - global $wgUser; + $user = self::getUser( "EditEntityTestUser" ); $content = ItemContent::newEmpty(); - $edit = new EditEntity( $content ); + $edit = new EditEntity( $content, $user ); // check valid token -------------------- if ( $token === true ) { - $token = $wgUser->getEditToken(); + $token = $user->getEditToken(); } $this->assertEquals( $shouldWork, $edit->isTokenOK( $token ) ); diff --git a/repo/tests/phpunit/includes/api/ModifyEntityTestBase.php b/repo/tests/phpunit/includes/api/ModifyEntityTestBase.php index 7b7c503..eee89b0 100644 --- a/repo/tests/phpunit/includes/api/ModifyEntityTestBase.php +++ b/repo/tests/phpunit/includes/api/ModifyEntityTestBase.php @@ -392,7 +392,10 @@ $entity['id'] = $this->getEntityId( $handle ); $token = $this->getEditToken(); - return $this->setEntity( $entity, $token ); + $data = $this->setEntity( $entity, $token ); + + self::$entityOutput[ $handle ] = $data; + return $data; } /** @@ -599,13 +602,13 @@ */ public function assertEntityEquals( $expected, $actual ) { if ( isset( $expected['id'] ) ) { - $this->assertEquals( $expected['id'], $actual['id'] ); + $this->assertEquals( $expected['id'], $actual['id'], 'id' ); } if ( isset( $expected['lastrevid'] ) ) { - $this->assertEquals( $expected['lastrevid'], $actual['lastrevid'] ); + $this->assertEquals( $expected['lastrevid'], $actual['lastrevid'], 'lastrevid' ); } if ( isset( $expected['type'] ) ) { - $this->assertEquals( $expected['type'], $actual['type'] ); + $this->assertEquals( $expected['type'], $actual['type'], 'type' ); } if ( isset( $expected['labels'] ) ) { diff --git a/repo/tests/phpunit/includes/api/PermissionsTest.php b/repo/tests/phpunit/includes/api/PermissionsTest.php index b57cc52..9724b60 100644 --- a/repo/tests/phpunit/includes/api/PermissionsTest.php +++ b/repo/tests/phpunit/includes/api/PermissionsTest.php @@ -88,7 +88,7 @@ return $re[0]; } - function doPermissionsTest( $action, $params, $permissions, $expectedError ) { + function doPermissionsTest( $action, $params, $permissions = array(), $expectedError = null, array $restore = array() ) { global $wgUser; self::applyPermissions( $permissions ); @@ -100,6 +100,12 @@ $params[ 'action' ] = $action; list( $re, , ) = $this->doApiRequest( $params, null, false, $wgUser ); + + // Restore any items we may have modified. + // This should always be done, regardless of validation. + foreach ( $restore as $restoreHandle ) { + $this->resetEntity( $restoreHandle ); + } if ( $expectedError == null ) { $this->assertArrayHasKey( 'success', $re, 'API call must report success.' ); @@ -173,7 +179,7 @@ 'ids' => $this->getEntityId( "Oslo" ), ); - $this->doPermissionsTest( 'wbgetentities', $params, $permissions, $expectedError ); + $this->doPermissionsTest( 'wbgetentities', $params, $permissions, $expectedError, array() ); } function provideAddItemPermissions() { @@ -212,7 +218,7 @@ 'new' => 'item', ); - $this->doPermissionsTest( 'wbeditentity', $params, $permissions, $expectedError ); + $this->doPermissionsTest( 'wbeditentity', $params, $permissions, $expectedError, array() ); } function provideSetSiteLinkPermissions() { @@ -233,20 +239,13 @@ * @dataProvider provideSetSiteLinkPermissions */ function testSetSiteLink( $permissions, $expectedError ) { - #XXX: hack: clear tables first. This may create database inconsistencies. - #TODO: Use $this->tables_used *everywhere*, so each test cleans up after itself. - - // TODO: use store - $dbw = wfGetDB( DB_MASTER ); - $dbw->delete( 'wb_items_per_site', '*', __METHOD__ ); - $params = array( 'id' => $this->getEntityId( "Oslo" ), 'linksite' => 'enwiki', 'linktitle' => 'Oslo', ); - $this->doPermissionsTest( 'wbsetsitelink', $params, $permissions, $expectedError ); + $this->doPermissionsTest( 'wbsetsitelink', $params, $permissions, $expectedError, array( "Oslo" ) ); } function provideSetLabelPermissions() { @@ -273,7 +272,7 @@ 'value' => 'Oslo', ); - $this->doPermissionsTest( 'wbsetlabel', $params, $permissions, $expectedError ); + $this->doPermissionsTest( 'wbsetlabel', $params, $permissions, $expectedError, array( "Oslo" ) ); } function provideSetDescriptionPermissions() { @@ -300,7 +299,7 @@ 'value' => 'Capitol of Norway', ); - $this->doPermissionsTest( 'wbsetdescription', $params, $permissions, $expectedError ); + $this->doPermissionsTest( 'wbsetdescription', $params, $permissions, $expectedError, array( "Oslo" ) ); } } -- To view, visit https://gerrit.wikimedia.org/r/74930 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Idc11440312fa229a2c53aee092756cbfc9ea8381 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Hoo man <h...@online.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits