Merge "Add checkDependencies.php"
[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 * Test for the move operation being aborted via the TitleMove hook
52 * @covers MovePage::move
53 */
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 );
59 }
60 );
61
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 ) );
69 }
70
71 /**
72 * Test moving subpages from one page to another
73 * @covers MovePage::moveSubpages
74 */
75 public function testMoveSubpages() {
76 $name = ucfirst( __FUNCTION__ );
77
78 $subPages = [ "Talk:$name/1", "Talk:$name/2" ];
79 $ids = [];
80 $pages = [
81 $name,
82 "Talk:$name",
83 "$name 2",
84 "Talk:$name 2",
85 ];
86 foreach ( array_merge( $pages, $subPages ) as $page ) {
87 $ids[$page] = $this->createPage( $page );
88 }
89
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 );
94
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] );
99 }
100 }
101
102 /**
103 * Test moving subpages from one page to another
104 * @covers MovePage::moveSubpagesIfAllowed
105 */
106 public function testMoveSubpagesIfAllowed() {
107 $name = ucfirst( __FUNCTION__ );
108
109 $subPages = [ "Talk:$name/1", "Talk:$name/2" ];
110 $ids = [];
111 $pages = [
112 $name,
113 "Talk:$name",
114 "$name 2",
115 "Talk:$name 2",
116 ];
117 foreach ( array_merge( $pages, $subPages ) as $page ) {
118 $ids[$page] = $this->createPage( $page );
119 }
120
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 );
125
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] );
130 }
131 }
132
133 /**
134 * Shortcut function to create a page and return its id.
135 *
136 * @param string $name Page to create
137 * @return int ID of created page
138 */
139 protected function createPage( $name ) {
140 return $this->editPage( $name, 'Content' )->value['revision']->getPage();
141 }
142
143 /**
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
148 */
149 protected function assertMoved( $from, $to, $id, $opts = null ) {
150 $opts = (array)$opts;
151
152 Title::clearCaches();
153 $fromTitle = Title::newFromText( $from );
154 $toTitle = Title::newFromText( $to );
155
156 $this->assertTrue( $toTitle->exists(),
157 "Destination {$toTitle->getPrefixedText()} does not exist" );
158
159 if ( in_array( 'noredirect', $opts ) ) {
160 $this->assertFalse( $fromTitle->exists(),
161 "Source {$fromTitle->getPrefixedText()} exists" );
162 } else {
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" );
167
168 $target = Revision::newFromTitle( $fromTitle )->getContent()->getRedirectTarget();
169 $this->assertSame( $toTitle->getPrefixedText(), $target->getPrefixedText() );
170 }
171
172 $this->assertSame( $id, $toTitle->getArticleID() );
173 }
174 }