6 class MovePageTest
extends MediaWikiTestCase
{
8 public function setUp() {
10 $this->tablesUsed
[] = 'page';
11 $this->tablesUsed
[] = 'revision';
12 $this->tablesUsed
[] = 'comment';
16 * @dataProvider provideIsValidMove
17 * @covers MovePage::isValidMove
18 * @covers MovePage::isValidFileMove
20 public function testIsValidMove( $old, $new, $error ) {
21 $this->setMwGlobals( 'wgContentHandlerUseDB', false );
23 Title
::newFromText( $old ),
24 Title
::newFromText( $new )
26 $status = $mp->isValidMove();
27 if ( $error === true ) {
28 $this->assertTrue( $status->isGood() );
30 $this->assertTrue( $status->hasMessage( $error ) );
35 * This should be kept in sync with TitleTest::provideTestIsValidMoveOperation
37 public static function provideIsValidMove() {
39 // for MovePage::isValidMove
40 [ 'Test', 'Test', 'selfmove' ],
41 [ 'Special:FooBar', 'Test', 'immobile-source-namespace' ],
42 [ 'Test', 'Special:FooBar', 'immobile-target-namespace' ],
43 [ 'MediaWiki:Common.js', 'Help:Some wikitext page', 'bad-target-model' ],
44 [ 'Page', 'File:Test.jpg', 'nonfile-cannot-move-to-file' ],
45 // for MovePage::isValidFileMove
46 [ 'File:Test.jpg', 'Page', 'imagenocrossnamespace' ],
51 * Test for the move operation being aborted via the TitleMove hook
52 * @covers MovePage::move
54 public function testMoveAbortedByTitleMoveHook() {
55 $error = 'Preventing move operation with TitleMove hook.';
56 $this->setTemporaryHook( 'TitleMove',
57 function ( $old, $new, $user, $reason, $status ) use ( $error ) {
58 $status->fatal( $error );
62 $oldTitle = Title
::newFromText( 'Some old title' );
63 WikiPage
::factory( $oldTitle )->doEditContent( new WikitextContent( 'foo' ), 'bar' );
64 $newTitle = Title
::newFromText( 'A brand new title' );
65 $mp = new MovePage( $oldTitle, $newTitle );
66 $user = User
::newFromName( 'TitleMove tester' );
67 $status = $mp->move( $user, 'Reason', true );
68 $this->assertTrue( $status->hasMessage( $error ) );
72 * Test moving subpages from one page to another
73 * @covers MovePage::moveSubpages
75 public function testMoveSubpages() {
76 $name = ucfirst( __FUNCTION__
);
78 $subPages = [ "Talk:$name/1", "Talk:$name/2" ];
86 foreach ( array_merge( $pages, $subPages ) as $page ) {
87 $ids[$page] = $this->createPage( $page );
90 $oldTitle = Title
::newFromText( "Talk:$name" );
91 $newTitle = Title
::newFromText( "Talk:$name 2" );
92 $mp = new MovePage( $oldTitle, $newTitle );
93 $status = $mp->moveSubpages( $this->getTestUser()->getUser(), 'Reason', true );
95 $this->assertTrue( $status->isGood(),
96 "Moving subpages from Talk:{$name} to Talk:{$name} 2 was not completely successful." );
97 foreach ( $subPages as $page ) {
98 $this->assertMoved( $page, str_replace( $name, "$name 2", $page ), $ids[$page] );
103 * Test moving subpages from one page to another
104 * @covers MovePage::moveSubpagesIfAllowed
106 public function testMoveSubpagesIfAllowed() {
107 $name = ucfirst( __FUNCTION__
);
109 $subPages = [ "Talk:$name/1", "Talk:$name/2" ];
117 foreach ( array_merge( $pages, $subPages ) as $page ) {
118 $ids[$page] = $this->createPage( $page );
121 $oldTitle = Title
::newFromText( "Talk:$name" );
122 $newTitle = Title
::newFromText( "Talk:$name 2" );
123 $mp = new MovePage( $oldTitle, $newTitle );
124 $status = $mp->moveSubpagesIfAllowed( $this->getTestUser()->getUser(), 'Reason', true );
126 $this->assertTrue( $status->isGood(),
127 "Moving subpages from Talk:{$name} to Talk:{$name} 2 was not completely successful." );
128 foreach ( $subPages as $page ) {
129 $this->assertMoved( $page, str_replace( $name, "$name 2", $page ), $ids[$page] );
134 * Shortcut function to create a page and return its id.
136 * @param string $name Page to create
137 * @return int ID of created page
139 protected function createPage( $name ) {
140 return $this->editPage( $name, 'Content' )->value
['revision']->getPage();
144 * @param string $from Prefixed name of source
145 * @param string $to Prefixed name of destination
146 * @param string $id Page id of the page to move
147 * @param array|string|null $opts Options: 'noredirect' to expect no redirect
149 protected function assertMoved( $from, $to, $id, $opts = null ) {
150 $opts = (array)$opts;
152 Title
::clearCaches();
153 $fromTitle = Title
::newFromText( $from );
154 $toTitle = Title
::newFromText( $to );
156 $this->assertTrue( $toTitle->exists(),
157 "Destination {$toTitle->getPrefixedText()} does not exist" );
159 if ( in_array( 'noredirect', $opts ) ) {
160 $this->assertFalse( $fromTitle->exists(),
161 "Source {$fromTitle->getPrefixedText()} exists" );
163 $this->assertTrue( $fromTitle->exists(),
164 "Source {$fromTitle->getPrefixedText()} does not exist" );
165 $this->assertTrue( $fromTitle->isRedirect(),
166 "Source {$fromTitle->getPrefixedText()} is not a redirect" );
168 $target = Revision
::newFromTitle( $fromTitle )->getContent()->getRedirectTarget();
169 $this->assertSame( $toTitle->getPrefixedText(), $target->getPrefixedText() );
172 $this->assertSame( $id, $toTitle->getArticleID() );