X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2Fpage%2FWikiPageDbTestBase.php;h=b7fe47e53d02029c84287d3669edf63e4531b4fb;hb=4eff5204d9ec6eb09df70d2fe017a2146cfa4238;hp=b8480dd014d8d3bc0d57be044615f7690cace491;hpb=bc6ed2ba24a8691ad6d6cc321a43720bcfb770e2;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/includes/page/WikiPageDbTestBase.php b/tests/phpunit/includes/page/WikiPageDbTestBase.php index b8480dd014..b7fe47e53d 100644 --- a/tests/phpunit/includes/page/WikiPageDbTestBase.php +++ b/tests/phpunit/includes/page/WikiPageDbTestBase.php @@ -341,7 +341,7 @@ abstract class WikiPageDbTestBase extends MediaWikiLangTestCase { # sanity check, because this test seems to fail for no reason for some people. $c = $page->getContent(); - $this->assertEquals( 'WikitextContent', get_class( $c ) ); + $this->assertEquals( WikitextContent::class, get_class( $c ) ); # now, test the actual redirect $t = $page->getRedirectTarget(); @@ -1065,15 +1065,15 @@ more stuff public function testWikiPageFactory() { $title = Title::makeTitle( NS_FILE, 'Someimage.png' ); $page = WikiPage::factory( $title ); - $this->assertEquals( 'WikiFilePage', get_class( $page ) ); + $this->assertEquals( WikiFilePage::class, get_class( $page ) ); $title = Title::makeTitle( NS_CATEGORY, 'SomeCategory' ); $page = WikiPage::factory( $title ); - $this->assertEquals( 'WikiCategoryPage', get_class( $page ) ); + $this->assertEquals( WikiCategoryPage::class, get_class( $page ) ); $title = Title::makeTitle( NS_MAIN, 'SomePage' ); $page = WikiPage::factory( $title ); - $this->assertEquals( 'WikiPage', get_class( $page ) ); + $this->assertEquals( WikiPage::class, get_class( $page ) ); } /** @@ -1423,7 +1423,7 @@ more stuff } public function provideTestInsertProtectNullRevision() { - // @codingStandardsIgnoreStart Generic.Files.LineLength + // phpcs:disable Generic.Files.LineLength yield [ 'goat-message-key', [ 'edit' => 'sysop' ], @@ -1442,7 +1442,7 @@ more stuff true, '(goat-key: WikiPageDbTestBase::testInsertProtectNullRevision, UTSysop)(colon-separator)Goat Goat(word-separator)(parentheses: (protect-summary-desc: (restriction-edit), (protect-level-sysop), (protect-expiring: 04:04, 1 (january) 2020, 1 (january) 2020, 04:04))(word-separator)(protect-summary-desc: (restriction-move), (protect-level-something), (protect-expiring: 05:05, 1 (january) 2021, 1 (january) 2021, 05:05)))' ]; - // @codingStandardsIgnoreEnd Generic.Files.LineLength + // phpcs:enable } /** @@ -1678,7 +1678,7 @@ more stuff public function testInsertOn_idSpecified() { $title = Title::newFromText( __METHOD__ ); $page = new WikiPage( $title ); - $id = 3478952189; + $id = 1478952189; $result = $page->insertOn( $this->db, $id ); @@ -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' ] ] + ); + } + }