Fix typos in MessageCache
[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 $this->setMwGlobals( 'wgContentHandlerUseDB', false );
22 $mp = new MovePage(
23 Title::newFromText( $old ),
24 Title::newFromText( $new )
25 );
26 $status = $mp->isValidMove();
27 if ( $error === true ) {
28 $this->assertTrue( $status->isGood() );
29 } else {
30 $this->assertTrue( $status->hasMessage( $error ) );
31 }
32 }
33
34 /**
35 * This should be kept in sync with TitleTest::provideTestIsValidMoveOperation
36 */
37 public static function provideIsValidMove() {
38 return [
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' ],
47 ];
48 }
49
50 /**
51 * Integration test to catch regressions like T74870. Taken and modified
52 * from SemanticMediaWiki
53 *
54 * @covers Title::moveTo
55 */
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' );
60 $this->assertNull(
61 WikiPage::factory( $newTitle )->getRevision()
62 );
63
64 $this->assertTrue( $oldTitle->moveTo( $newTitle, false, 'test1', true ) );
65 $this->assertNotNull(
66 WikiPage::factory( $oldTitle )->getRevision()
67 );
68 $this->assertNotNull(
69 WikiPage::factory( $newTitle )->getRevision()
70 );
71 }
72
73 /**
74 * Test for the move operation being aborted via the TitleMove hook
75 * @covers MovePage::move
76 */
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 );
82 }
83 );
84
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 ) );
92 }
93
94 /**
95 * Test moving subpages from one page to another
96 * @covers MovePage::moveSubpages
97 */
98 public function testMoveSubpages() {
99 $name = ucfirst( __FUNCTION__ );
100
101 $subPages = [ "Talk:$name/1", "Talk:$name/2" ];
102 $ids = [];
103 $pages = [
104 $name,
105 "Talk:$name",
106 "$name 2",
107 "Talk:$name 2",
108 ];
109 foreach ( array_merge( $pages, $subPages ) as $page ) {
110 $ids[$page] = $this->createPage( $page );
111 }
112
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 );
117
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] );
122 }
123 }
124
125 /**
126 * Test moving subpages from one page to another
127 * @covers MovePage::moveSubpagesIfAllowed
128 */
129 public function testMoveSubpagesIfAllowed() {
130 $name = ucfirst( __FUNCTION__ );
131
132 $subPages = [ "Talk:$name/1", "Talk:$name/2" ];
133 $ids = [];
134 $pages = [
135 $name,
136 "Talk:$name",
137 "$name 2",
138 "Talk:$name 2",
139 ];
140 foreach ( array_merge( $pages, $subPages ) as $page ) {
141 $ids[$page] = $this->createPage( $page );
142 }
143
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 );
148
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] );
153 }
154 }
155
156 /**
157 * Shortcut function to create a page and return its id.
158 *
159 * @param string $name Page to create
160 * @return int ID of created page
161 */
162 protected function createPage( $name ) {
163 return $this->editPage( $name, 'Content' )->value['revision']->getPage();
164 }
165
166 /**
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
171 */
172 protected function assertMoved( $from, $to, $id, $opts = null ) {
173 $opts = (array)$opts;
174
175 Title::clearCaches();
176 $fromTitle = Title::newFromText( $from );
177 $toTitle = Title::newFromText( $to );
178
179 $this->assertTrue( $toTitle->exists(),
180 "Destination {$toTitle->getPrefixedText()} does not exist" );
181
182 if ( in_array( 'noredirect', $opts ) ) {
183 $this->assertFalse( $fromTitle->exists(),
184 "Source {$fromTitle->getPrefixedText()} exists" );
185 } else {
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" );
190
191 $target = Revision::newFromTitle( $fromTitle )->getContent()->getRedirectTarget();
192 $this->assertSame( $toTitle->getPrefixedText(), $target->getPrefixedText() );
193 }
194
195 $this->assertSame( $id, $toTitle->getArticleID() );
196 }
197 }