Introduce hasSlot in RevisionRecord and RevisionSlots.
authordaniel <daniel.kinzler@wikimedia.de>
Tue, 6 Feb 2018 21:42:07 +0000 (22:42 +0100)
committerDaniel Kinzler <daniel.kinzler@wikimedia.de>
Mon, 26 Feb 2018 17:34:48 +0000 (17:34 +0000)
Change-Id: Ie7ee2afe26d37ac726f4682c96166cb89134227c

includes/Storage/RevisionRecord.php
includes/Storage/RevisionSlots.php
tests/common/TestsAutoLoader.php
tests/phpunit/includes/Storage/MutableRevisionRecordTest.php
tests/phpunit/includes/Storage/MutableRevisionSlotsTest.php
tests/phpunit/includes/Storage/RevisionSlotsTest.php
tests/phpunit/includes/Storage/RevisionStoreRecordTest.php

index 8734f48..6d83e1c 100644 (file)
@@ -196,6 +196,17 @@ abstract class RevisionRecord {
                return $slot;
        }
 
+       /**
+        * Returns whether the given slot is defined in this revision.
+        *
+        * @param string $role The role name of the desired slot
+        *
+        * @return bool
+        */
+       public function hasSlot( $role ) {
+               return $this->mSlots->hasSlot( $role );
+       }
+
        /**
         * Returns the slot names (roles) of all slots present in this revision.
         * getContent() will succeed only for the names returned by this method.
index 8d3d7e3..7fa5431 100644 (file)
@@ -110,6 +110,19 @@ class RevisionSlots {
                }
        }
 
+       /**
+        * Returns whether the given slot is set.
+        *
+        * @param string $role The role name of the desired slot
+        *
+        * @return bool
+        */
+       public function hasSlot( $role ) {
+               $slots = $this->getSlots();
+
+               return isset( $slots[$role] );
+       }
+
        /**
         * Returns the slot names (roles) of all slots present in this revision.
         * getContent() will succeed only for the names returned by this method.
index 82149b3..a777153 100644 (file)
@@ -143,6 +143,9 @@ $wgAutoloadClasses += [
        'SpecialPageTestBase' => "$testDir/phpunit/includes/specials/SpecialPageTestBase.php",
        'SpecialPageExecutor' => "$testDir/phpunit/includes/specials/SpecialPageExecutor.php",
 
+       # tests/phpunit/includes/Storage
+       'MediaWiki\Tests\Storage\RevisionSlotsTest' => "$testDir/phpunit/includes/Storage/RevisionSlotsTest.php",
+
        # tests/phpunit/languages
        'LanguageClassesTestCase' => "$testDir/phpunit/languages/LanguageClassesTestCase.php",
 
index 79cac5e..807099f 100644 (file)
@@ -60,6 +60,8 @@ class MutableRevisionRecordTest extends MediaWikiTestCase {
 
        public function testSimpleGetSlotWhenEmpty() {
                $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
+               $this->assertFalse( $record->hasSlot( 'main' ) );
+
                $this->setExpectedException( RevisionAccessException::class );
                $record->getSlot( 'main' );
        }
@@ -71,6 +73,7 @@ class MutableRevisionRecordTest extends MediaWikiTestCase {
                        new WikitextContent( 'x' )
                );
                $record->setSlot( $slot );
+               $this->assertTrue( $record->hasSlot( 'main' ) );
                $this->assertSame( $slot, $record->getSlot( 'main' ) );
        }
 
index c2a275f..0416bcf 100644 (file)
@@ -5,13 +5,12 @@ namespace MediaWiki\Tests\Storage;
 use MediaWiki\Storage\MutableRevisionSlots;
 use MediaWiki\Storage\RevisionAccessException;
 use MediaWiki\Storage\SlotRecord;
-use MediaWikiTestCase;
 use WikitextContent;
 
 /**
  * @covers \MediaWiki\Storage\MutableRevisionSlots
  */
-class MutableRevisionSlotsTest extends MediaWikiTestCase {
+class MutableRevisionSlotsTest extends RevisionSlotsTest {
 
        public function testSetMultipleSlots() {
                $slots = new MutableRevisionSlots();
@@ -20,11 +19,13 @@ class MutableRevisionSlotsTest extends MediaWikiTestCase {
 
                $slotA = SlotRecord::newUnsaved( 'some', new WikitextContent( 'A' ) );
                $slots->setSlot( $slotA );
+               $this->assertTrue( $slots->hasSlot( 'some' ) );
                $this->assertSame( $slotA, $slots->getSlot( 'some' ) );
                $this->assertSame( [ 'some' => $slotA ], $slots->getSlots() );
 
                $slotB = SlotRecord::newUnsaved( 'other', new WikitextContent( 'B' ) );
                $slots->setSlot( $slotB );
+               $this->assertTrue( $slots->hasSlot( 'other' ) );
                $this->assertSame( $slotB, $slots->getSlot( 'other' ) );
                $this->assertSame( [ 'some' => $slotA, 'other' => $slotB ], $slots->getSlots() );
        }
index 4dfae4b..b9f833c 100644 (file)
@@ -10,13 +10,21 @@ use WikitextContent;
 
 class RevisionSlotsTest extends MediaWikiTestCase {
 
+       /**
+        * @param SlotRecord[] $slots
+        * @return RevisionSlots
+        */
+       protected function newRevisionSlots( $slots = [] ) {
+               return new RevisionSlots( $slots );
+       }
+
        /**
         * @covers \MediaWiki\Storage\RevisionSlots::getSlot
         */
        public function testGetSlot() {
                $mainSlot = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) );
                $auxSlot = SlotRecord::newUnsaved( 'aux', new WikitextContent( 'B' ) );
-               $slots = new RevisionSlots( [ $mainSlot, $auxSlot ] );
+               $slots = $this->newRevisionSlots( [ $mainSlot, $auxSlot ] );
 
                $this->assertSame( $mainSlot, $slots->getSlot( 'main' ) );
                $this->assertSame( $auxSlot, $slots->getSlot( 'aux' ) );
@@ -24,6 +32,20 @@ class RevisionSlotsTest extends MediaWikiTestCase {
                $slots->getSlot( 'nothere' );
        }
 
+       /**
+        * @covers \MediaWiki\Storage\RevisionSlots::hasSlot
+        */
+       public function testHasSlot() {
+               $mainSlot = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) );
+               $auxSlot = SlotRecord::newUnsaved( 'aux', new WikitextContent( 'B' ) );
+               $slots = $this->newRevisionSlots( [ $mainSlot, $auxSlot ] );
+
+               $this->assertTrue( $slots->hasSlot( 'main' ) );
+               $this->assertTrue( $slots->hasSlot( 'aux' ) );
+               $this->assertFalse( $slots->hasSlot( 'AUX' ) );
+               $this->assertFalse( $slots->hasSlot( 'xyz' ) );
+       }
+
        /**
         * @covers \MediaWiki\Storage\RevisionSlots::getContent
         */
@@ -32,7 +54,7 @@ class RevisionSlotsTest extends MediaWikiTestCase {
                $auxContent = new WikitextContent( 'B' );
                $mainSlot = SlotRecord::newUnsaved( 'main', $mainContent );
                $auxSlot = SlotRecord::newUnsaved( 'aux', $auxContent );
-               $slots = new RevisionSlots( [ $mainSlot, $auxSlot ] );
+               $slots = $this->newRevisionSlots( [ $mainSlot, $auxSlot ] );
 
                $this->assertSame( $mainContent, $slots->getContent( 'main' ) );
                $this->assertSame( $auxContent, $slots->getContent( 'aux' ) );
@@ -46,7 +68,7 @@ class RevisionSlotsTest extends MediaWikiTestCase {
        public function testGetSlotRoles_someSlots() {
                $mainSlot = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) );
                $auxSlot = SlotRecord::newUnsaved( 'aux', new WikitextContent( 'B' ) );
-               $slots = new RevisionSlots( [ $mainSlot, $auxSlot ] );
+               $slots = $this->newRevisionSlots( [ $mainSlot, $auxSlot ] );
 
                $this->assertSame( [ 'main', 'aux' ], $slots->getSlotRoles() );
        }
@@ -55,7 +77,7 @@ class RevisionSlotsTest extends MediaWikiTestCase {
         * @covers \MediaWiki\Storage\RevisionSlots::getSlotRoles
         */
        public function testGetSlotRoles_noSlots() {
-               $slots = new RevisionSlots( [] );
+               $slots = $this->newRevisionSlots( [] );
 
                $this->assertSame( [], $slots->getSlotRoles() );
        }
@@ -67,7 +89,7 @@ class RevisionSlotsTest extends MediaWikiTestCase {
                $mainSlot = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) );
                $auxSlot = SlotRecord::newUnsaved( 'aux', new WikitextContent( 'B' ) );
                $slotsArray = [ $mainSlot, $auxSlot ];
-               $slots = new RevisionSlots( $slotsArray );
+               $slots = $this->newRevisionSlots( $slotsArray );
 
                $this->assertEquals( [ 'main' => $mainSlot, 'aux' => $auxSlot ], $slots->getSlots() );
        }
@@ -87,7 +109,7 @@ class RevisionSlotsTest extends MediaWikiTestCase {
                foreach ( $contentStrings as $key => $contentString ) {
                        $slotsArray[] = SlotRecord::newUnsaved( strval( $key ), new WikitextContent( $contentString ) );
                }
-               $slots = new RevisionSlots( $slotsArray );
+               $slots = $this->newRevisionSlots( $slotsArray );
 
                $this->assertSame( $expected, $slots->computeSize() );
        }
@@ -109,7 +131,7 @@ class RevisionSlotsTest extends MediaWikiTestCase {
                foreach ( $contentStrings as $key => $contentString ) {
                        $slotsArray[] = SlotRecord::newUnsaved( strval( $key ), new WikitextContent( $contentString ) );
                }
-               $slots = new RevisionSlots( $slotsArray );
+               $slots = $this->newRevisionSlots( $slotsArray );
 
                $this->assertSame( $expected, $slots->computeSha1() );
        }
index 43784b6..3976995 100644 (file)
@@ -458,8 +458,9 @@ class RevisionStoreRecordTest extends MediaWikiTestCase {
                $rev = $this->newRevision( [ 'rev_deleted' => $visibility ] );
 
                // NOTE: slot meta-data is never suppressed, just the content is!
-               $this->assertNotNull( $rev->getSlot( 'main', RevisionRecord::RAW ), 'raw can' );
-               $this->assertNotNull( $rev->getSlot( 'main', RevisionRecord::FOR_PUBLIC ), 'public can' );
+               $this->assertTrue( $rev->hasSlot( 'main' ), 'hasSlot is never suppressed' );
+               $this->assertNotNull( $rev->getSlot( 'main', RevisionRecord::RAW ), 'raw meta' );
+               $this->assertNotNull( $rev->getSlot( 'main', RevisionRecord::FOR_PUBLIC ), 'public meta' );
 
                $this->assertNotNull(
                        $rev->getSlot( 'main', RevisionRecord::FOR_THIS_USER, $user ),
@@ -562,6 +563,13 @@ class RevisionStoreRecordTest extends MediaWikiTestCase {
                $this->assertSame( 'main', $slot->getRole(), 'getRole()' );
        }
 
+       public function testHasSlot() {
+               $rev = $this->newRevision();
+
+               $this->assertTrue( $rev->hasSlot( 'main' ) );
+               $this->assertFalse( $rev->hasSlot( 'xyz' ) );
+       }
+
        public function testGetContent() {
                $rev = $this->newRevision();