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 * Integration test to catch regressions like T74870. Taken and modified
52 * from SemanticMediaWiki
54 * @covers Title::moveTo
56 public function testTitleMoveCompleteIntegrationTest() {
57 $oldTitle = Title
::newFromText( 'Help:Some title' );
58 WikiPage
::factory( $oldTitle )->doEditContent( new WikitextContent( 'foo' ), 'bar' );
59 $newTitle = Title
::newFromText( 'Help:Some other title' );
61 WikiPage
::factory( $newTitle )->getRevision()
64 $this->assertTrue( $oldTitle->moveTo( $newTitle, false, 'test1', true ) );
66 WikiPage
::factory( $oldTitle )->getRevision()
69 WikiPage
::factory( $newTitle )->getRevision()
74 * Test for the move operation being aborted via the TitleMove hook
75 * @covers MovePage::move
77 public function testMoveAbortedByTitleMoveHook() {
78 $error = 'Preventing move operation with TitleMove hook.';
79 $this->setTemporaryHook( 'TitleMove',
80 function ( $old, $new, $user, $reason, $status ) use ( $error ) {
81 $status->fatal( $error );
85 $oldTitle = Title
::newFromText( 'Some old title' );
86 WikiPage
::factory( $oldTitle )->doEditContent( new WikitextContent( 'foo' ), 'bar' );
87 $newTitle = Title
::newFromText( 'A brand new title' );
88 $mp = new MovePage( $oldTitle, $newTitle );
89 $user = User
::newFromName( 'TitleMove tester' );
90 $status = $mp->move( $user, 'Reason', true );
91 $this->assertTrue( $status->hasMessage( $error ) );
95 * Test moving subpages from one page to another
96 * @covers MovePage::moveSubpages
98 public function testMoveSubpages() {
99 $name = ucfirst( __FUNCTION__
);
101 $subPages = [ "Talk:$name/1", "Talk:$name/2" ];
109 foreach ( array_merge( $pages, $subPages ) as $page ) {
110 $ids[$page] = $this->createPage( $page );
113 $oldTitle = Title
::newFromText( "Talk:$name" );
114 $newTitle = Title
::newFromText( "Talk:$name 2" );
115 $mp = new MovePage( $oldTitle, $newTitle );
116 $status = $mp->moveSubpages( $this->getTestUser()->getUser(), 'Reason', true );
118 $this->assertTrue( $status->isGood(),
119 "Moving subpages from Talk:{$name} to Talk:{$name} 2 was not completely successful." );
120 foreach ( $subPages as $page ) {
121 $this->assertMoved( $page, str_replace( $name, "$name 2", $page ), $ids[$page] );
126 * Test moving subpages from one page to another
127 * @covers MovePage::moveSubpagesIfAllowed
129 public function testMoveSubpagesIfAllowed() {
130 $name = ucfirst( __FUNCTION__
);
132 $subPages = [ "Talk:$name/1", "Talk:$name/2" ];
140 foreach ( array_merge( $pages, $subPages ) as $page ) {
141 $ids[$page] = $this->createPage( $page );
144 $oldTitle = Title
::newFromText( "Talk:$name" );
145 $newTitle = Title
::newFromText( "Talk:$name 2" );
146 $mp = new MovePage( $oldTitle, $newTitle );
147 $status = $mp->moveSubpagesIfAllowed( $this->getTestUser()->getUser(), 'Reason', true );
149 $this->assertTrue( $status->isGood(),
150 "Moving subpages from Talk:{$name} to Talk:{$name} 2 was not completely successful." );
151 foreach ( $subPages as $page ) {
152 $this->assertMoved( $page, str_replace( $name, "$name 2", $page ), $ids[$page] );
157 * Shortcut function to create a page and return its id.
159 * @param string $name Page to create
160 * @return int ID of created page
162 protected function createPage( $name ) {
163 return $this->editPage( $name, 'Content' )->value
['revision']->getPage();
167 * @param string $from Prefixed name of source
168 * @param string $to Prefixed name of destination
169 * @param string $id Page id of the page to move
170 * @param array|string|null $opts Options: 'noredirect' to expect no redirect
172 protected function assertMoved( $from, $to, $id, $opts = null ) {
173 $opts = (array)$opts;
175 Title
::clearCaches();
176 $fromTitle = Title
::newFromText( $from );
177 $toTitle = Title
::newFromText( $to );
179 $this->assertTrue( $toTitle->exists(),
180 "Destination {$toTitle->getPrefixedText()} does not exist" );
182 if ( in_array( 'noredirect', $opts ) ) {
183 $this->assertFalse( $fromTitle->exists(),
184 "Source {$fromTitle->getPrefixedText()} exists" );
186 $this->assertTrue( $fromTitle->exists(),
187 "Source {$fromTitle->getPrefixedText()} does not exist" );
188 $this->assertTrue( $fromTitle->isRedirect(),
189 "Source {$fromTitle->getPrefixedText()} is not a redirect" );
191 $target = Revision
::newFromTitle( $fromTitle )->getContent()->getRedirectTarget();
192 $this->assertSame( $toTitle->getPrefixedText(), $target->getPrefixedText() );
195 $this->assertSame( $id, $toTitle->getArticleID() );