[MCR] Tests for WikiPage::doUpdateRestrictions
authoraddshore <addshorewiki@gmail.com>
Mon, 4 Dec 2017 18:23:17 +0000 (19:23 +0100)
committeraddshore <addshorewiki@gmail.com>
Thu, 4 Jan 2018 11:28:01 +0000 (11:28 +0000)
Bug: T180989
Change-Id: I75518d5ba34119e88e3e382523583fa8b65bc730

tests/phpunit/includes/page/WikiPageDbTestBase.php

index 8e5b183..9a70e61 100644 (file)
@@ -1695,4 +1695,235 @@ more stuff
                );
        }
 
+       public function provideTestDoUpdateRestrictions_setBasicRestrictions() {
+               // Note: Once the current dates passes the date in these tests they will fail.
+               yield 'move something' => [
+                       true,
+                       [ 'move' => 'something' ],
+                       [],
+                       [ 'edit' => [], 'move' => [ 'something' ] ],
+                       [],
+               ];
+               yield 'move something, edit blank' => [
+                       true,
+                       [ 'move' => 'something', 'edit' => '' ],
+                       [],
+                       [ 'edit' => [], 'move' => [ 'something' ] ],
+                       [],
+               ];
+               yield 'edit sysop, with expiry' => [
+                       true,
+                       [ 'edit' => 'sysop' ],
+                       [ 'edit' => '21330101020202' ],
+                       [ 'edit' => [ 'sysop' ], 'move' => [] ],
+                       [ 'edit' => '21330101020202' ],
+               ];
+               yield 'move and edit, move with expiry' => [
+                       true,
+                       [ 'move' => 'something', 'edit' => 'another' ],
+                       [ 'move' => '22220202010101' ],
+                       [ 'edit' => [ 'another' ], 'move' => [ 'something' ] ],
+                       [ 'move' => '22220202010101' ],
+               ];
+               yield 'move and edit, edit with infinity expiry' => [
+                       true,
+                       [ 'move' => 'something', 'edit' => 'another' ],
+                       [ 'edit' => 'infinity' ],
+                       [ 'edit' => [ 'another' ], 'move' => [ 'something' ] ],
+                       [ 'edit' => 'infinity' ],
+               ];
+               yield 'non existing, create something' => [
+                       false,
+                       [ 'create' => 'something' ],
+                       [],
+                       [ 'create' => [ 'something' ] ],
+                       [],
+               ];
+               yield 'non existing, create something with expiry' => [
+                       false,
+                       [ 'create' => 'something' ],
+                       [ 'create' => '23451212112233' ],
+                       [ 'create' => [ 'something' ] ],
+                       [ 'create' => '23451212112233' ],
+               ];
+       }
+
+       /**
+        * @dataProvider provideTestDoUpdateRestrictions_setBasicRestrictions
+        * @covers WikiPage::doUpdateRestrictions
+        */
+       public function testDoUpdateRestrictions_setBasicRestrictions(
+               $pageExists,
+               array $limit,
+               array $expiry,
+               array $expectedRestrictions,
+               array $expectedRestrictionExpiries
+       ) {
+               if ( $pageExists ) {
+                       $page = $this->createPage( __METHOD__, 'ABC' );
+               } else {
+                       $page = new WikiPage( Title::newFromText( __METHOD__ . '-nonexist' ) );
+               }
+               $user = $this->getTestSysop()->getUser();
+               $cascade = false;
+
+               $status = $page->doUpdateRestrictions( $limit, $expiry, $cascade, 'aReason', $user, [] );
+
+               $logId = $status->getValue();
+               $allRestrictions = $page->getTitle()->getAllRestrictions();
+
+               $this->assertTrue( $status->isGood() );
+               $this->assertInternalType( 'int', $logId );
+               $this->assertSame( $expectedRestrictions, $allRestrictions );
+               foreach ( $expectedRestrictionExpiries as $key => $value ) {
+                       $this->assertSame( $value, $page->getTitle()->getRestrictionExpiry( $key ) );
+               }
+
+               // Make sure the log entry looks good
+               // log_params is not checked here
+               $this->assertSelect(
+                       'logging',
+                       [
+                               'log_comment',
+                               'log_user',
+                               'log_user_text',
+                               'log_namespace',
+                               'log_title',
+                       ],
+                       [ 'log_id' => $logId ],
+                       [ [
+                               'aReason',
+                               (string)$user->getId(),
+                               $user->getName(),
+                               (string)$page->getTitle()->getNamespace(),
+                               $page->getTitle()->getDBkey(),
+                       ] ]
+               );
+       }
+
+       /**
+        * @covers WikiPage::doUpdateRestrictions
+        */
+       public function testDoUpdateRestrictions_failsOnReadOnly() {
+               $page = $this->createPage( __METHOD__, 'ABC' );
+               $user = $this->getTestSysop()->getUser();
+               $cascade = false;
+
+               // Set read only
+               $readOnly = $this->getMockBuilder( ReadOnlyMode::class )
+                       ->disableOriginalConstructor()
+                       ->setMethods( [ 'isReadOnly', 'getReason' ] )
+                       ->getMock();
+               $readOnly->expects( $this->once() )
+                       ->method( 'isReadOnly' )
+                       ->will( $this->returnValue( true ) );
+               $readOnly->expects( $this->once() )
+                       ->method( 'getReason' )
+                       ->will( $this->returnValue( 'Some Read Only Reason' ) );
+               $this->setService( 'ReadOnlyMode', $readOnly );
+
+               $status = $page->doUpdateRestrictions( [], [], $cascade, 'aReason', $user, [] );
+               $this->assertFalse( $status->isOK() );
+               $this->assertSame( 'readonlytext', $status->getMessage()->getKey() );
+       }
+
+       /**
+        * @covers WikiPage::doUpdateRestrictions
+        */
+       public function testDoUpdateRestrictions_returnsGoodIfNothingChanged() {
+               $page = $this->createPage( __METHOD__, 'ABC' );
+               $user = $this->getTestSysop()->getUser();
+               $cascade = false;
+               $limit = [ 'edit' => 'sysop' ];
+
+               $status = $page->doUpdateRestrictions(
+                       $limit,
+                       [],
+                       $cascade,
+                       'aReason',
+                       $user,
+                       []
+               );
+
+               // The first entry should have a logId as it did something
+               $this->assertTrue( $status->isGood() );
+               $this->assertInternalType( 'int', $status->getValue() );
+
+               $status = $page->doUpdateRestrictions(
+                       $limit,
+                       [],
+                       $cascade,
+                       'aReason',
+                       $user,
+                       []
+               );
+
+               // The second entry should not have a logId as nothing changed
+               $this->assertTrue( $status->isGood() );
+               $this->assertNull( $status->getValue() );
+       }
+
+       /**
+        * @covers WikiPage::doUpdateRestrictions
+        */
+       public function testDoUpdateRestrictions_logEntryTypeAndAction() {
+               $page = $this->createPage( __METHOD__, 'ABC' );
+               $user = $this->getTestSysop()->getUser();
+               $cascade = false;
+
+               // Protect the page
+               $status = $page->doUpdateRestrictions(
+                       [ 'edit' => 'sysop' ],
+                       [],
+                       $cascade,
+                       'aReason',
+                       $user,
+                       []
+               );
+               $this->assertTrue( $status->isGood() );
+               $this->assertInternalType( 'int', $status->getValue() );
+               $this->assertSelect(
+                       'logging',
+                       [ 'log_type', 'log_action' ],
+                       [ 'log_id' => $status->getValue() ],
+                       [ [ 'protect', 'protect' ] ]
+               );
+
+               // Modify the protection
+               $status = $page->doUpdateRestrictions(
+                       [ 'edit' => 'somethingElse' ],
+                       [],
+                       $cascade,
+                       'aReason',
+                       $user,
+                       []
+               );
+               $this->assertTrue( $status->isGood() );
+               $this->assertInternalType( 'int', $status->getValue() );
+               $this->assertSelect(
+                       'logging',
+                       [ 'log_type', 'log_action' ],
+                       [ 'log_id' => $status->getValue() ],
+                       [ [ 'protect', 'modify' ] ]
+               );
+
+               // Remove the protection
+               $status = $page->doUpdateRestrictions(
+                       [],
+                       [],
+                       $cascade,
+                       'aReason',
+                       $user,
+                       []
+               );
+               $this->assertTrue( $status->isGood() );
+               $this->assertInternalType( 'int', $status->getValue() );
+               $this->assertSelect(
+                       'logging',
+                       [ 'log_type', 'log_action' ],
+                       [ 'log_id' => $status->getValue() ],
+                       [ [ 'protect', 'unprotect' ] ]
+               );
+       }
+
 }