X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2FMovePageTest.php;h=607f4f7a56f892e636fb1966c6cbc915727327cd;hb=dfec83932fd38a9086eb5a2e212889ad00f35b0e;hp=583b75184db992274076c2d259ecd6b58384e0c8;hpb=e83db4f33702a440dc6e16afc93d16cabf70e55e;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/includes/MovePageTest.php b/tests/phpunit/includes/MovePageTest.php index 583b75184d..9166666543 100644 --- a/tests/phpunit/includes/MovePageTest.php +++ b/tests/phpunit/includes/MovePageTest.php @@ -5,6 +5,13 @@ */ class MovePageTest extends MediaWikiTestCase { + public function setUp() { + parent::setUp(); + $this->tablesUsed[] = 'page'; + $this->tablesUsed[] = 'revision'; + $this->tablesUsed[] = 'comment'; + } + /** * @dataProvider provideIsValidMove * @covers MovePage::isValidMove @@ -41,25 +48,127 @@ class MovePageTest extends MediaWikiTestCase { } /** - * Integration test to catch regressions like T74870. Taken and modified - * from SemanticMediaWiki - * - * @covers Title::moveTo + * Test for the move operation being aborted via the TitleMove hook + * @covers MovePage::move */ - public function testTitleMoveCompleteIntegrationTest() { - $oldTitle = Title::newFromText( 'Help:Some title' ); - WikiPage::factory( $oldTitle )->doEditContent( new WikitextContent( 'foo' ), 'bar' ); - $newTitle = Title::newFromText( 'Help:Some other title' ); - $this->assertNull( - WikiPage::factory( $newTitle )->getRevision() + public function testMoveAbortedByTitleMoveHook() { + $error = 'Preventing move operation with TitleMove hook.'; + $this->setTemporaryHook( 'TitleMove', + function ( $old, $new, $user, $reason, $status ) use ( $error ) { + $status->fatal( $error ); + } ); - $this->assertTrue( $oldTitle->moveTo( $newTitle, false, 'test1', true ) ); - $this->assertNotNull( - WikiPage::factory( $oldTitle )->getRevision() - ); - $this->assertNotNull( - WikiPage::factory( $newTitle )->getRevision() - ); + $oldTitle = Title::newFromText( 'Some old title' ); + WikiPage::factory( $oldTitle )->doEditContent( new WikitextContent( 'foo' ), 'bar' ); + $newTitle = Title::newFromText( 'A brand new title' ); + $mp = new MovePage( $oldTitle, $newTitle ); + $user = User::newFromName( 'TitleMove tester' ); + $status = $mp->move( $user, 'Reason', true ); + $this->assertTrue( $status->hasMessage( $error ) ); + } + + /** + * Test moving subpages from one page to another + * @covers MovePage::moveSubpages + */ + public function testMoveSubpages() { + $name = ucfirst( __FUNCTION__ ); + + $subPages = [ "Talk:$name/1", "Talk:$name/2" ]; + $ids = []; + $pages = [ + $name, + "Talk:$name", + "$name 2", + "Talk:$name 2", + ]; + foreach ( array_merge( $pages, $subPages ) as $page ) { + $ids[$page] = $this->createPage( $page ); + } + + $oldTitle = Title::newFromText( "Talk:$name" ); + $newTitle = Title::newFromText( "Talk:$name 2" ); + $mp = new MovePage( $oldTitle, $newTitle ); + $status = $mp->moveSubpages( $this->getTestUser()->getUser(), 'Reason', true ); + + $this->assertTrue( $status->isGood(), + "Moving subpages from Talk:{$name} to Talk:{$name} 2 was not completely successful." ); + foreach ( $subPages as $page ) { + $this->assertMoved( $page, str_replace( $name, "$name 2", $page ), $ids[$page] ); + } + } + + /** + * Test moving subpages from one page to another + * @covers MovePage::moveSubpagesIfAllowed + */ + public function testMoveSubpagesIfAllowed() { + $name = ucfirst( __FUNCTION__ ); + + $subPages = [ "Talk:$name/1", "Talk:$name/2" ]; + $ids = []; + $pages = [ + $name, + "Talk:$name", + "$name 2", + "Talk:$name 2", + ]; + foreach ( array_merge( $pages, $subPages ) as $page ) { + $ids[$page] = $this->createPage( $page ); + } + + $oldTitle = Title::newFromText( "Talk:$name" ); + $newTitle = Title::newFromText( "Talk:$name 2" ); + $mp = new MovePage( $oldTitle, $newTitle ); + $status = $mp->moveSubpagesIfAllowed( $this->getTestUser()->getUser(), 'Reason', true ); + + $this->assertTrue( $status->isGood(), + "Moving subpages from Talk:{$name} to Talk:{$name} 2 was not completely successful." ); + foreach ( $subPages as $page ) { + $this->assertMoved( $page, str_replace( $name, "$name 2", $page ), $ids[$page] ); + } + } + + /** + * Shortcut function to create a page and return its id. + * + * @param string $name Page to create + * @return int ID of created page + */ + protected function createPage( $name ) { + return $this->editPage( $name, 'Content' )->value['revision']->getPage(); + } + + /** + * @param string $from Prefixed name of source + * @param string $to Prefixed name of destination + * @param string $id Page id of the page to move + * @param array|string|null $opts Options: 'noredirect' to expect no redirect + */ + protected function assertMoved( $from, $to, $id, $opts = null ) { + $opts = (array)$opts; + + Title::clearCaches(); + $fromTitle = Title::newFromText( $from ); + $toTitle = Title::newFromText( $to ); + + $this->assertTrue( $toTitle->exists(), + "Destination {$toTitle->getPrefixedText()} does not exist" ); + + if ( in_array( 'noredirect', $opts ) ) { + $this->assertFalse( $fromTitle->exists(), + "Source {$fromTitle->getPrefixedText()} exists" ); + } else { + $this->assertTrue( $fromTitle->exists(), + "Source {$fromTitle->getPrefixedText()} does not exist" ); + $this->assertTrue( $fromTitle->isRedirect(), + "Source {$fromTitle->getPrefixedText()} is not a redirect" ); + + $target = Revision::newFromTitle( $fromTitle )->getContent()->getRedirectTarget(); + $this->assertSame( $toTitle->getPrefixedText(), $target->getPrefixedText() ); + } + + $this->assertSame( $id, $toTitle->getArticleID() ); } }