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 global $wgMultiContentRevisionSchemaMigrationStage;
22 if ( $wgMultiContentRevisionSchemaMigrationStage === SCHEMA_COMPAT_OLD
) {
23 // We can only set this to false with the old schema
24 $this->setMwGlobals( 'wgContentHandlerUseDB', false );
27 Title
::newFromText( $old ),
28 Title
::newFromText( $new )
30 $status = $mp->isValidMove();
31 if ( $error === true ) {
32 $this->assertTrue( $status->isGood() );
34 $this->assertTrue( $status->hasMessage( $error ) );
39 * This should be kept in sync with TitleTest::provideTestIsValidMoveOperation
41 public static function provideIsValidMove() {
42 global $wgMultiContentRevisionSchemaMigrationStage;
44 // for MovePage::isValidMove
45 [ 'Test', 'Test', 'selfmove' ],
46 [ 'Special:FooBar', 'Test', 'immobile-source-namespace' ],
47 [ 'Test', 'Special:FooBar', 'immobile-target-namespace' ],
48 [ 'Page', 'File:Test.jpg', 'nonfile-cannot-move-to-file' ],
49 // for MovePage::isValidFileMove
50 [ 'File:Test.jpg', 'Page', 'imagenocrossnamespace' ],
52 if ( $wgMultiContentRevisionSchemaMigrationStage === SCHEMA_COMPAT_OLD
) {
53 // The error can only occur if $wgContentHandlerUseDB is false, which doesn't work with
54 // the new schema, so omit the test in that case
56 [ 'MediaWiki:Common.js', 'Help:Some wikitext page', 'bad-target-model' ] );
62 * Test for the move operation being aborted via the TitleMove hook
63 * @covers MovePage::move
65 public function testMoveAbortedByTitleMoveHook() {
66 $error = 'Preventing move operation with TitleMove hook.';
67 $this->setTemporaryHook( 'TitleMove',
68 function ( $old, $new, $user, $reason, $status ) use ( $error ) {
69 $status->fatal( $error );
73 $oldTitle = Title
::newFromText( 'Some old title' );
74 WikiPage
::factory( $oldTitle )->doEditContent( new WikitextContent( 'foo' ), 'bar' );
75 $newTitle = Title
::newFromText( 'A brand new title' );
76 $mp = new MovePage( $oldTitle, $newTitle );
77 $user = User
::newFromName( 'TitleMove tester' );
78 $status = $mp->move( $user, 'Reason', true );
79 $this->assertTrue( $status->hasMessage( $error ) );
83 * Test moving subpages from one page to another
84 * @covers MovePage::moveSubpages
86 public function testMoveSubpages() {
87 $name = ucfirst( __FUNCTION__
);
89 $subPages = [ "Talk:$name/1", "Talk:$name/2" ];
97 foreach ( array_merge( $pages, $subPages ) as $page ) {
98 $ids[$page] = $this->createPage( $page );
101 $oldTitle = Title
::newFromText( "Talk:$name" );
102 $newTitle = Title
::newFromText( "Talk:$name 2" );
103 $mp = new MovePage( $oldTitle, $newTitle );
104 $status = $mp->moveSubpages( $this->getTestUser()->getUser(), 'Reason', true );
106 $this->assertTrue( $status->isGood(),
107 "Moving subpages from Talk:{$name} to Talk:{$name} 2 was not completely successful." );
108 foreach ( $subPages as $page ) {
109 $this->assertMoved( $page, str_replace( $name, "$name 2", $page ), $ids[$page] );
114 * Test moving subpages from one page to another
115 * @covers MovePage::moveSubpagesIfAllowed
117 public function testMoveSubpagesIfAllowed() {
118 $name = ucfirst( __FUNCTION__
);
120 $subPages = [ "Talk:$name/1", "Talk:$name/2" ];
128 foreach ( array_merge( $pages, $subPages ) as $page ) {
129 $ids[$page] = $this->createPage( $page );
132 $oldTitle = Title
::newFromText( "Talk:$name" );
133 $newTitle = Title
::newFromText( "Talk:$name 2" );
134 $mp = new MovePage( $oldTitle, $newTitle );
135 $status = $mp->moveSubpagesIfAllowed( $this->getTestUser()->getUser(), 'Reason', true );
137 $this->assertTrue( $status->isGood(),
138 "Moving subpages from Talk:{$name} to Talk:{$name} 2 was not completely successful." );
139 foreach ( $subPages as $page ) {
140 $this->assertMoved( $page, str_replace( $name, "$name 2", $page ), $ids[$page] );
145 * Shortcut function to create a page and return its id.
147 * @param string $name Page to create
148 * @return int ID of created page
150 protected function createPage( $name ) {
151 return $this->editPage( $name, 'Content' )->value
['revision']->getPage();
155 * @param string $from Prefixed name of source
156 * @param string $to Prefixed name of destination
157 * @param string $id Page id of the page to move
158 * @param array|string|null $opts Options: 'noredirect' to expect no redirect
160 protected function assertMoved( $from, $to, $id, $opts = null ) {
161 $opts = (array)$opts;
163 Title
::clearCaches();
164 $fromTitle = Title
::newFromText( $from );
165 $toTitle = Title
::newFromText( $to );
167 $this->assertTrue( $toTitle->exists(),
168 "Destination {$toTitle->getPrefixedText()} does not exist" );
170 if ( in_array( 'noredirect', $opts ) ) {
171 $this->assertFalse( $fromTitle->exists(),
172 "Source {$fromTitle->getPrefixedText()} exists" );
174 $this->assertTrue( $fromTitle->exists(),
175 "Source {$fromTitle->getPrefixedText()} does not exist" );
176 $this->assertTrue( $fromTitle->isRedirect(),
177 "Source {$fromTitle->getPrefixedText()} is not a redirect" );
179 $target = Revision
::newFromTitle( $fromTitle )->getContent()->getRedirectTarget();
180 $this->assertSame( $toTitle->getPrefixedText(), $target->getPrefixedText() );
183 $this->assertSame( $id, $toTitle->getArticleID() );