Jakob has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/314013

Change subject: Make WikiPageEntityStore fail for foreign entities.
......................................................................

Make WikiPageEntityStore fail for foreign entities.

WikiPageEntityStore should fail for entities that are not stored on
the local wiki.

Bug: T146992
Change-Id: I1a4c524dc67ba2f26209d28eeeb116ffd1739dc9
---
M repo/includes/Store/Sql/WikiPageEntityStore.php
M repo/tests/phpunit/includes/Store/Sql/WikiPageEntityStoreTest.php
2 files changed, 105 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/13/314013/1

diff --git a/repo/includes/Store/Sql/WikiPageEntityStore.php 
b/repo/includes/Store/Sql/WikiPageEntityStore.php
index 99b5b81..0b1d41b 100644
--- a/repo/includes/Store/Sql/WikiPageEntityStore.php
+++ b/repo/includes/Store/Sql/WikiPageEntityStore.php
@@ -2,6 +2,7 @@
 
 namespace Wikibase\Repo\Store;
 
+use InvalidArgumentException;
 use MWException;
 use Revision;
 use Status;
@@ -63,6 +64,17 @@
        }
 
        /**
+        * @param EntityId $id
+        *
+        * @throws InvalidArgumentException
+        */
+       private function assertLocalEntityId( EntityId $id ) {
+               if ( $id->isForeign() ) {
+                       throw new InvalidArgumentException( 'The entity must 
not be foreign' );
+               }
+       }
+
+       /**
         * @see EntityStore::assignFreshId()
         *
         * @param EntityDocument $entity
@@ -98,6 +110,7 @@
         * @return bool
         */
        public function canCreateWithCustomId( EntityId $id ) {
+               $this->assertLocalEntityId( $id );
                $type = $id->getEntityType();
                $handler = $this->contentFactory->getContentHandlerForType( 
$type );
 
@@ -121,12 +134,14 @@
         *
         * @param EntityId $entityId
         *
+        * @throws InvalidArgumentException
         * @throws StorageException
         * @return WikiPage
         */
        public function getWikiPageForEntity( EntityId $entityId ) {
-               $title = $this->getTitleForEntity( $entityId );
+               $this->assertLocalEntityId( $entityId );
 
+               $title = $this->getTitleForEntity( $entityId );
                if ( !$title ) {
                        throw new StorageException( 'Entity could not be mapped 
to a page title!' );
                }
@@ -196,8 +211,10 @@
                $flags = 0,
                $baseRevId = false
        ) {
-               $content = $this->contentFactory->newFromRedirect( $redirect );
+               $this->assertLocalEntityId( $redirect->getEntityId() );
+               $this->assertLocalEntityId( $redirect->getTargetId() );
 
+               $content = $this->contentFactory->newFromRedirect( $redirect );
                if ( !$content ) {
                        throw new StorageException( 'Failed to create redirect' 
.
                                ' from ' . 
$redirect->getEntityId()->getSerialization() .
@@ -237,6 +254,7 @@
                $flags = 0,
                $baseRevId = false
        ) {
+               $this->assertLocalEntityId( $entityContent->getEntityId() );
                $page = $this->getWikiPageForEntity( 
$entityContent->getEntityId() );
 
                if ( ( $flags & EDIT_NEW ) === EDIT_NEW ) {
@@ -304,6 +322,7 @@
         * @throws StorageException
         */
        public function deleteEntity( EntityId $entityId, $reason, User $user ) 
{
+               $this->assertLocalEntityId( $entityId );
                $page = $this->getWikiPageForEntity( $entityId );
                $ok = $page->doDeleteArticle( $reason, false, 0, true, $error, 
$user );
 
@@ -329,6 +348,7 @@
         * @return bool
         */
        public function userWasLastToEdit( User $user, EntityId $id, $lastRevId 
) {
+               $this->assertLocalEntityId( $id );
                $revision = Revision::newFromId( $lastRevId );
                if ( !$revision ) {
                        return false;
@@ -364,6 +384,8 @@
         * @note keep in sync with logic in EditPage
         */
        public function updateWatchlist( User $user, EntityId $id, $watch ) {
+               $this->assertLocalEntityId( $id );
+
                $title = $this->getTitleForEntity( $id );
 
                if ( $user->isLoggedIn() && $title && ( $watch != 
$user->isWatched( $title ) ) ) {
@@ -394,6 +416,8 @@
         * @return bool
         */
        public function isWatching( User $user, EntityId $id ) {
+               $this->assertLocalEntityId( $id );
+
                $title = $this->getTitleForEntity( $id );
                return ( $title && $user->isWatched( $title ) );
        }
diff --git a/repo/tests/phpunit/includes/Store/Sql/WikiPageEntityStoreTest.php 
b/repo/tests/phpunit/includes/Store/Sql/WikiPageEntityStoreTest.php
index 07c3261..902bd41 100644
--- a/repo/tests/phpunit/includes/Store/Sql/WikiPageEntityStoreTest.php
+++ b/repo/tests/phpunit/includes/Store/Sql/WikiPageEntityStoreTest.php
@@ -2,6 +2,7 @@
 
 namespace Wikibase\Repo\Tests\Store\Sql;
 
+use InvalidArgumentException;
 use MediaWikiTestCase;
 use RawMessage;
 use Revision;
@@ -655,4 +656,82 @@
                $this->assertSame( $expected, $store->canCreateWithCustomId( 
$id ), $id->getSerialization() );
        }
 
+       public function testCanCreateWithCustomIdFails_GivenForeignEntityId() {
+               /* @var WikiPageEntityStore $store */
+               list( $store, ) = $this->createStoreAndLookup();
+               $this->setExpectedException( InvalidArgumentException::class );
+
+               $store->canCreateWithCustomId( new ItemId( 'foo:Q42' ) );
+       }
+
+       public function testGetWikiPageForEntityFails_GivenForeignEntityId() {
+               /* @var WikiPageEntityStore $store */
+               list( $store, ) = $this->createStoreAndLookup();
+               $this->setExpectedException( InvalidArgumentException::class );
+
+               $store->getWikiPageForEntity( new ItemId( 'foo:Q42' ) );
+       }
+
+       public function testSaveEntityFails_GivenForeignEntityId() {
+               /* @var WikiPageEntityStore $store */
+               list( $store, ) = $this->createStoreAndLookup();
+               $this->setExpectedException( InvalidArgumentException::class );
+
+               $store->saveEntity( new Item( new ItemId( 'foo:Q123' ) ), 
'testing', $GLOBALS['wgUser'], EDIT_NEW );
+       }
+
+       public function testDeleteEntityFails_GivenForeignEntityId() {
+               /* @var WikiPageEntityStore $store */
+               list( $store, ) = $this->createStoreAndLookup();
+               $this->setExpectedException( InvalidArgumentException::class );
+
+               $store->deleteEntity( new ItemId( 'foo:Q123' ), 'testing', 
$GLOBALS['wgUser'] );
+       }
+
+       public function testUserWasLastToEditFails_GivenForeignEntityId() {
+               /* @var WikiPageEntityStore $store */
+               list( $store, ) = $this->createStoreAndLookup();
+               $this->setExpectedException( InvalidArgumentException::class );
+
+               $store->userWasLastToEdit( $GLOBALS['wgUser'], new ItemId( 
'foo:Q123' ), false );
+       }
+
+       /**
+        * @dataProvider foreignRedirectServiceProvider
+        */
+       public function testSaveRedirectFails_GivenForeignEntityId( EntityId 
$source, EntityId $target ) {
+               /* @var WikiPageEntityStore $store */
+               list( $store, ) = $this->createStoreAndLookup();
+               $this->setExpectedException( InvalidArgumentException::class );
+
+               $store->saveRedirect(
+                       new EntityRedirect( $source, $target ),
+                       'testing',
+                       $GLOBALS['wgUser']
+               );
+       }
+
+       public function foreignRedirectServiceProvider() {
+               return [
+                       [ new ItemId( 'foo:Q123' ), new ItemId( 'Q42' ) ],
+                       [ new ItemId( 'Q42' ), new ItemId( 'foo:Q123' ) ],
+               ];
+       }
+
+       public function testUpdateWatchListFails_GivenForeignEntityId() {
+               /* @var WikiPageEntityStore $store */
+               list( $store, ) = $this->createStoreAndLookup();
+               $this->setExpectedException( InvalidArgumentException::class );
+
+               $store->updateWatchlist( $GLOBALS['wgUser'], new ItemId( 
'foo:Q123' ), false );
+       }
+
+       public function testIsWatchingFails_GivenForeignEntityId() {
+               /* @var WikiPageEntityStore $store */
+               list( $store, ) = $this->createStoreAndLookup();
+               $this->setExpectedException( InvalidArgumentException::class );
+
+               $store->isWatching( $GLOBALS['wgUser'], new ItemId( 'foo:Q123' 
) );
+       }
+
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/314013
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I1a4c524dc67ba2f26209d28eeeb116ffd1739dc9
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Jakob <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to