Hoo man has uploaded a new change for review.

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

Change subject: Fix EditEntity::isNew for new entities with an Id set
......................................................................

Fix EditEntity::isNew for new entities with an Id set

In some places (eg. the EditEntity api) we set an id on new
entities before calling out to EditEntity (because several
things require that).

This broke watching new entities that were created via the api,
as we always assumed that they already existed.

Bug: T85454
Change-Id: Ib915e928ee65a9db027326fb8e4aec62ea88b362
---
M repo/includes/EditEntity.php
M repo/tests/phpunit/includes/EditEntityTest.php
2 files changed, 25 insertions(+), 6 deletions(-)


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

diff --git a/repo/includes/EditEntity.php b/repo/includes/EditEntity.php
index eb65d3f..1b12f50 100644
--- a/repo/includes/EditEntity.php
+++ b/repo/includes/EditEntity.php
@@ -283,19 +283,21 @@
        /**
         * Returns the latest revision ID.
         *
-        * @return int
+        * @return int 0 if the entity doesn't exist
         */
        public function getLatestRevisionId() {
-               if ( $this->isNew() ) {
+               if ( $this->newEntity->getId() === null ) {
                        return 0;
                }
 
                wfProfileIn( __METHOD__ );
-               if ( $this->latestRevId === null ) {
+               // Don't do negative caching: We call this to see whether the 
entity yet exists
+               // before creating.
+               if ( $this->latestRevId === null || $this->latestRevId === 0 ) {
                        if ( $this->latestRev !== null ) {
                                $this->latestRevId = 
$this->latestRev->getRevisionId();
                        } else {
-                               $this->latestRevId = 
$this->entityRevisionLookup->getLatestRevisionId(
+                               $this->latestRevId = 
(int)$this->entityRevisionLookup->getLatestRevisionId(
                                        $this->getEntityId(),
                                        EntityRevisionLookup::LATEST_FROM_MASTER
                                );
@@ -316,10 +318,10 @@
        }
 
        /**
-        * Returns whether the new content is new, that is, does not have an ID 
yet and thus no title, page or revisions.
+        * Returns whether the new content is new: No title, page or revisions 
(but maybe an Id).
         */
        public function isNew() {
-               return $this->newEntity->getId() === null;
+               return $this->newEntity->getId() === null || 
$this->getLatestRevisionId() === 0;
        }
 
        /**
diff --git a/repo/tests/phpunit/includes/EditEntityTest.php 
b/repo/tests/phpunit/includes/EditEntityTest.php
index 02e7e69..8b69d86 100644
--- a/repo/tests/phpunit/includes/EditEntityTest.php
+++ b/repo/tests/phpunit/includes/EditEntityTest.php
@@ -10,6 +10,7 @@
 use Title;
 use User;
 use Wikibase\DataModel\Entity\Entity;
+use Wikibase\DataModel\Entity\EntityDocument;
 use Wikibase\DataModel\Entity\EntityId;
 use Wikibase\DataModel\Entity\Item;
 use Wikibase\DataModel\Entity\ItemId;
@@ -760,4 +761,20 @@
                $this->assertEquals( $expected, $repo->isWatching( $user, 
$item->getId() ), "watched" );
        }
 
+       public function testIsNew() {
+               $repo = $this->getMockRepository();
+               $titleLookup = $this->getEntityTitleLookup();
+               $item = new Item();
+
+               $edit = $this->makeEditEntity( $repo, $item, $titleLookup );
+               $this->assertTrue( $edit->isNew(), 'New entity: No id' );
+
+               $repo->assignFreshId( $item );
+               $edit = $this->makeEditEntity( $repo, $item, $titleLookup );
+               $this->assertTrue( $edit->isNew(), "New entity: Has an id, but 
doesn't exist, yet" );
+
+               $repo->saveEntity( $item, 'testIsNew', $this->getUser( 
'EditEntityTestUser1' ) );
+               $edit = $this->makeEditEntity( $repo, $item, $titleLookup );
+               $this->assertFalse( $edit->isNew(), "Entity exists" );
+       }
 }

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

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

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

Reply via email to