X-Git-Url: https://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/banques/?a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2Fpage%2FWikiPageDbTestBase.php;h=be8b444f70b05b97b3491f9379d00eb909dbe523;hb=2af18246aaeda5a61bd298ebc6edd572e73f418a;hp=7ef279e174038ec015b1d7d1ebc44ee81d142d07;hpb=7b86ada2f4f0918ea976cda0f1a0fcea465386cb;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/includes/page/WikiPageDbTestBase.php b/tests/phpunit/includes/page/WikiPageDbTestBase.php index 7ef279e174..be8b444f70 100644 --- a/tests/phpunit/includes/page/WikiPageDbTestBase.php +++ b/tests/phpunit/includes/page/WikiPageDbTestBase.php @@ -11,7 +11,9 @@ abstract class WikiPageDbTestBase extends MediaWikiLangTestCase { $this->tablesUsed, [ 'page', 'revision', + 'redirect', 'archive', + 'category', 'ip_changes', 'text', @@ -1154,4 +1156,334 @@ more stuff ]; } + /** + * @covers WikiPage::updateCategoryCounts + */ + public function testUpdateCategoryCounts() { + $page = new WikiPage( Title::newFromText( __METHOD__ ) ); + + // Add an initial category + $page->updateCategoryCounts( [ 'A' ], [], 0 ); + + $this->assertEquals( 1, Category::newFromName( 'A' )->getPageCount() ); + $this->assertEquals( 0, Category::newFromName( 'B' )->getPageCount() ); + $this->assertEquals( 0, Category::newFromName( 'C' )->getPageCount() ); + + // Add a new category + $page->updateCategoryCounts( [ 'B' ], [], 0 ); + + $this->assertEquals( 1, Category::newFromName( 'A' )->getPageCount() ); + $this->assertEquals( 1, Category::newFromName( 'B' )->getPageCount() ); + $this->assertEquals( 0, Category::newFromName( 'C' )->getPageCount() ); + + // Add and remove a category + $page->updateCategoryCounts( [ 'C' ], [ 'A' ], 0 ); + + $this->assertEquals( 0, Category::newFromName( 'A' )->getPageCount() ); + $this->assertEquals( 1, Category::newFromName( 'B' )->getPageCount() ); + $this->assertEquals( 1, Category::newFromName( 'C' )->getPageCount() ); + } + + public function provideUpdateRedirectOn() { + yield [ '#REDIRECT [[Foo]]', true, null, true, true, 0 ]; + yield [ '#REDIRECT [[Foo]]', true, 'Foo', true, false, 1 ]; + yield [ 'SomeText', false, null, false, true, 0 ]; + yield [ 'SomeText', false, 'Foo', false, false, 1 ]; + } + + /** + * @dataProvider provideUpdateRedirectOn + * @covers WikiPage::updateRedirectOn + * + * @param string $initialText + * @param bool $initialRedirectState + * @param string|null $redirectTitle + * @param bool|null $lastRevIsRedirect + * @param bool $expectedSuccess + * @param int $expectedRowCount + */ + public function testUpdateRedirectOn( + $initialText, + $initialRedirectState, + $redirectTitle, + $lastRevIsRedirect, + $expectedSuccess, + $expectedRowCount + ) { + static $pageCounter = 0; + $pageCounter++; + + $page = $this->createPage( Title::newFromText( __METHOD__ . $pageCounter ), $initialText ); + $this->assertSame( $initialRedirectState, $page->isRedirect() ); + + $redirectTitle = is_string( $redirectTitle ) + ? Title::newFromText( $redirectTitle ) + : $redirectTitle; + + $success = $page->updateRedirectOn( $this->db, $redirectTitle, $lastRevIsRedirect ); + $this->assertSame( $expectedSuccess, $success, 'Success assertion' ); + /** + * updateRedirectOn explicitly updates the redirect table (and not the page table). + * Most of core checks the page table for redirect status, so we have to be ugly and + * assert a select from the table here. + */ + $this->assertRedirectTableCountForPageId( $page->getId(), $expectedRowCount ); + } + + private function assertRedirectTableCountForPageId( $pageId, $expected ) { + $this->assertSelect( + 'redirect', + 'COUNT(*)', + [ 'rd_from' => $pageId ], + [ [ strval( $expected ) ] ] + ); + } + + /** + * @covers WikiPage::insertRedirectEntry + */ + public function testInsertRedirectEntry_insertsRedirectEntry() { + $page = $this->createPage( Title::newFromText( __METHOD__ ), 'A' ); + $this->assertRedirectTableCountForPageId( $page->getId(), 0 ); + + $targetTitle = Title::newFromText( 'SomeTarget#Frag' ); + $targetTitle->mInterwiki = 'eninter'; + $page->insertRedirectEntry( $targetTitle, null ); + + $this->assertSelect( + 'redirect', + [ 'rd_from', 'rd_namespace', 'rd_title', 'rd_fragment', 'rd_interwiki' ], + [ 'rd_from' => $page->getId() ], + [ [ + strval( $page->getId() ), + strval( $targetTitle->getNamespace() ), + strval( $targetTitle->getDBkey() ), + strval( $targetTitle->getFragment() ), + strval( $targetTitle->getInterwiki() ), + ] ] + ); + } + + /** + * @covers WikiPage::insertRedirectEntry + */ + public function testInsertRedirectEntry_insertsRedirectEntryWithPageLatest() { + $page = $this->createPage( Title::newFromText( __METHOD__ ), 'A' ); + $this->assertRedirectTableCountForPageId( $page->getId(), 0 ); + + $targetTitle = Title::newFromText( 'SomeTarget#Frag' ); + $targetTitle->mInterwiki = 'eninter'; + $page->insertRedirectEntry( $targetTitle, $page->getLatest() ); + + $this->assertSelect( + 'redirect', + [ 'rd_from', 'rd_namespace', 'rd_title', 'rd_fragment', 'rd_interwiki' ], + [ 'rd_from' => $page->getId() ], + [ [ + strval( $page->getId() ), + strval( $targetTitle->getNamespace() ), + strval( $targetTitle->getDBkey() ), + strval( $targetTitle->getFragment() ), + strval( $targetTitle->getInterwiki() ), + ] ] + ); + } + + /** + * @covers WikiPage::insertRedirectEntry + */ + public function testInsertRedirectEntry_doesNotInsertIfPageLatestIncorrect() { + $page = $this->createPage( Title::newFromText( __METHOD__ ), 'A' ); + $this->assertRedirectTableCountForPageId( $page->getId(), 0 ); + + $targetTitle = Title::newFromText( 'SomeTarget#Frag' ); + $targetTitle->mInterwiki = 'eninter'; + $page->insertRedirectEntry( $targetTitle, 215251 ); + + $this->assertRedirectTableCountForPageId( $page->getId(), 0 ); + } + + private function getRow( array $overrides = [] ) { + $row = [ + 'page_id' => '44', + 'page_len' => '76', + 'page_is_redirect' => '1', + 'page_latest' => '99', + 'page_namespace' => '3', + 'page_title' => 'JaJaTitle', + 'page_restrictions' => 'edit=autoconfirmed,sysop:move=sysop', + 'page_touched' => '20120101020202', + 'page_links_updated' => '20140101020202', + ]; + foreach ( $overrides as $key => $value ) { + $row[$key] = $value; + } + return (object)$row; + } + + public function provideNewFromRowSuccess() { + yield 'basic row' => [ + $this->getRow(), + function ( WikiPage $wikiPage, self $test ) { + $test->assertSame( 44, $wikiPage->getId() ); + $test->assertSame( 76, $wikiPage->getTitle()->getLength() ); + $test->assertTrue( $wikiPage->isRedirect() ); + $test->assertSame( 99, $wikiPage->getLatest() ); + $test->assertSame( 3, $wikiPage->getTitle()->getNamespace() ); + $test->assertSame( 'JaJaTitle', $wikiPage->getTitle()->getDBkey() ); + $test->assertSame( + [ + 'edit' => [ 'autoconfirmed', 'sysop' ], + 'move' => [ 'sysop' ], + ], + $wikiPage->getTitle()->getAllRestrictions() + ); + $test->assertSame( '20120101020202', $wikiPage->getTouched() ); + $test->assertSame( '20140101020202', $wikiPage->getLinksTimestamp() ); + } + ]; + yield 'different timestamp formats' => [ + $this->getRow( [ + 'page_touched' => '2012-01-01 02:02:02', + 'page_links_updated' => '2014-01-01 02:02:02', + ] ), + function ( WikiPage $wikiPage, self $test ) { + $test->assertSame( '20120101020202', $wikiPage->getTouched() ); + $test->assertSame( '20140101020202', $wikiPage->getLinksTimestamp() ); + } + ]; + yield 'no restrictions' => [ + $this->getRow( [ + 'page_restrictions' => '', + ] ), + function ( WikiPage $wikiPage, self $test ) { + $test->assertSame( + [ + 'edit' => [], + 'move' => [], + ], + $wikiPage->getTitle()->getAllRestrictions() + ); + } + ]; + yield 'not redirect' => [ + $this->getRow( [ + 'page_is_redirect' => '0', + ] ), + function ( WikiPage $wikiPage, self $test ) { + $test->assertFalse( $wikiPage->isRedirect() ); + } + ]; + } + + /** + * @covers WikiPage::newFromRow + * @covers WikiPage::loadFromRow + * @dataProvider provideNewFromRowSuccess + * + * @param object $row + * @param callable $assertions + */ + public function testNewFromRow( $row, $assertions ) { + $page = WikiPage::newFromRow( $row, 'fromdb' ); + $assertions( $page, $this ); + } + + public function provideTestNewFromId_returnsNullOnBadPageId() { + yield[ 0 ]; + yield[ -11 ]; + } + + /** + * @covers WikiPage::newFromID + * @dataProvider provideTestNewFromId_returnsNullOnBadPageId + */ + public function testNewFromId_returnsNullOnBadPageId( $pageId ) { + $this->assertNull( WikiPage::newFromID( $pageId ) ); + } + + /** + * @covers WikiPage::newFromID + */ + public function testNewFromId_appearsToFetchCorrectRow() { + $createdPage = $this->createPage( __METHOD__, 'Xsfaij09' ); + $fetchedPage = WikiPage::newFromID( $createdPage->getId() ); + $this->assertSame( $createdPage->getId(), $fetchedPage->getId() ); + $this->assertEquals( + $createdPage->getContent()->getNativeData(), + $fetchedPage->getContent()->getNativeData() + ); + } + + /** + * @covers WikiPage::newFromID + */ + public function testNewFromId_returnsNullOnNonExistingId() { + $this->assertNull( WikiPage::newFromID( 73574757437437743743 ) ); + } + + public function provideTestInsertProtectNullRevision() { + // @codingStandardsIgnoreStart Generic.Files.LineLength + yield [ + 'goat-message-key', + [ 'edit' => 'sysop' ], + [ 'edit' => '20200101040404' ], + false, + 'Goat Reason', + true, + '(goat-message-key: WikiPageDbTestBase::testInsertProtectNullRevision, UTSysop)(colon-separator)Goat Reason(word-separator)(parentheses: (protect-summary-desc: (restriction-edit), (protect-level-sysop), (protect-expiring: 04:04, 1 (january) 2020, 1 (january) 2020, 04:04)))' + ]; + yield [ + 'goat-key', + [ 'edit' => 'sysop', 'move' => 'something' ], + [ 'edit' => '20200101040404', 'move' => '20210101050505' ], + false, + 'Goat Goat', + 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 + } + + /** + * @dataProvider provideTestInsertProtectNullRevision + * @covers WikiPge::insertProtectNullRevision + * @covers WikiPge::protectDescription + * + * @param string $revCommentMsg + * @param array $limit + * @param array $expiry + * @param bool $cascade + * @param string $reason + * @param bool|null $user true if the test sysop should be used, or null + * @param string $expectedComment + */ + public function testInsertProtectNullRevision( + $revCommentMsg, + array $limit, + array $expiry, + $cascade, + $reason, + $user, + $expectedComment + ) { + $this->setContentLang( 'qqx' ); + + $page = $this->createPage( __METHOD__, 'Goat' ); + + $user = $user === null ? $user : $this->getTestSysop()->getUser(); + + $result = $page->insertProtectNullRevision( + $revCommentMsg, + $limit, + $expiry, + $cascade, + $reason, + $user + ); + + $this->assertTrue( $result instanceof Revision ); + $this->assertSame( $expectedComment, $result->getComment( Revision::RAW ) ); + } + }