Don't use new MCR schema without using DB
[lhc/web/wiklou.git] / tests / phpunit / includes / MovePageTest.php
1 <?php
2
3 /**
4 * @group Database
5 */
6 class MovePageTest extends MediaWikiTestCase {
7
8 public function setUp() {
9 parent::setUp();
10 $this->tablesUsed[] = 'page';
11 $this->tablesUsed[] = 'revision';
12 $this->tablesUsed[] = 'comment';
13 }
14
15 /**
16 * @dataProvider provideIsValidMove
17 * @covers MovePage::isValidMove
18 * @covers MovePage::isValidFileMove
19 */
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 );
25 }
26 $mp = new MovePage(
27 Title::newFromText( $old ),
28 Title::newFromText( $new )
29 );
30 $status = $mp->isValidMove();
31 if ( $error === true ) {
32 $this->assertTrue( $status->isGood() );
33 } else {
34 $this->assertTrue( $status->hasMessage( $error ) );
35 }
36 }
37
38 /**
39 * This should be kept in sync with TitleTest::provideTestIsValidMoveOperation
40 */
41 public static function provideIsValidMove() {
42 global $wgMultiContentRevisionSchemaMigrationStage;
43 $ret = [
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' ],
51 ];
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
55 array_push( $ret,
56 [ 'MediaWiki:Common.js', 'Help:Some wikitext page', 'bad-target-model' ] );
57 }
58 return $ret;
59 }
60
61 /**
62 * Test for the move operation being aborted via the TitleMove hook
63 * @covers MovePage::move
64 */
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 );
70 }
71 );
72
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 ) );
80 }
81
82 /**
83 * Test moving subpages from one page to another
84 * @covers MovePage::moveSubpages
85 */
86 public function testMoveSubpages() {
87 $name = ucfirst( __FUNCTION__ );
88
89 $subPages = [ "Talk:$name/1", "Talk:$name/2" ];
90 $ids = [];
91 $pages = [
92 $name,
93 "Talk:$name",
94 "$name 2",
95 "Talk:$name 2",
96 ];
97 foreach ( array_merge( $pages, $subPages ) as $page ) {
98 $ids[$page] = $this->createPage( $page );
99 }
100
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 );
105
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] );
110 }
111 }
112
113 /**
114 * Test moving subpages from one page to another
115 * @covers MovePage::moveSubpagesIfAllowed
116 */
117 public function testMoveSubpagesIfAllowed() {
118 $name = ucfirst( __FUNCTION__ );
119
120 $subPages = [ "Talk:$name/1", "Talk:$name/2" ];
121 $ids = [];
122 $pages = [
123 $name,
124 "Talk:$name",
125 "$name 2",
126 "Talk:$name 2",
127 ];
128 foreach ( array_merge( $pages, $subPages ) as $page ) {
129 $ids[$page] = $this->createPage( $page );
130 }
131
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 );
136
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] );
141 }
142 }
143
144 /**
145 * Shortcut function to create a page and return its id.
146 *
147 * @param string $name Page to create
148 * @return int ID of created page
149 */
150 protected function createPage( $name ) {
151 return $this->editPage( $name, 'Content' )->value['revision']->getPage();
152 }
153
154 /**
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
159 */
160 protected function assertMoved( $from, $to, $id, $opts = null ) {
161 $opts = (array)$opts;
162
163 Title::clearCaches();
164 $fromTitle = Title::newFromText( $from );
165 $toTitle = Title::newFromText( $to );
166
167 $this->assertTrue( $toTitle->exists(),
168 "Destination {$toTitle->getPrefixedText()} does not exist" );
169
170 if ( in_array( 'noredirect', $opts ) ) {
171 $this->assertFalse( $fromTitle->exists(),
172 "Source {$fromTitle->getPrefixedText()} exists" );
173 } else {
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" );
178
179 $target = Revision::newFromTitle( $fromTitle )->getContent()->getRedirectTarget();
180 $this->assertSame( $toTitle->getPrefixedText(), $target->getPrefixedText() );
181 }
182
183 $this->assertSame( $id, $toTitle->getArticleID() );
184 }
185 }