1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
|
<?php
use MediaWiki\Config\ServiceOptions;
use MediaWiki\Interwiki\InterwikiLookup;
use MediaWiki\MediaWikiServices;
use MediaWiki\Page\PageCommandFactory;
use MediaWiki\Permissions\PermissionManager;
use MediaWiki\Revision\SlotRecord;
use Wikimedia\Rdbms\IDatabase;
use Wikimedia\Rdbms\LoadBalancer;
/**
* @group Database
*/
class MovePageTest extends MediaWikiTestCase {
/**
* The only files that exist are 'File:Existent.jpg', 'File:Existent2.jpg', and
* 'File:Existent-file-no-page.jpg'. Calling unexpected methods causes a test failure.
*
* @return RepoGroup
*/
private function getMockRepoGroup() : RepoGroup {
$mockExistentFile = $this->createMock( LocalFile::class );
$mockExistentFile->method( 'exists' )->willReturn( true );
$mockExistentFile->method( 'getMimeType' )->willReturn( 'image/jpeg' );
$mockExistentFile->expects( $this->never() )
->method( $this->anythingBut( 'exists', 'load', 'getMimeType', '__destruct' ) );
$mockNonexistentFile = $this->createMock( LocalFile::class );
$mockNonexistentFile->method( 'exists' )->willReturn( false );
$mockNonexistentFile->expects( $this->never() )
->method( $this->anythingBut( 'exists', 'load', '__destruct' ) );
$mockLocalRepo = $this->createMock( LocalRepo::class );
$mockLocalRepo->method( 'newFile' )->will( $this->returnCallback(
function ( Title $title ) use ( $mockExistentFile, $mockNonexistentFile ) {
if ( in_array( $title->getPrefixedText(),
[ 'File:Existent.jpg', 'File:Existent2.jpg', 'File:Existent-file-no-page.jpg' ]
) ) {
return $mockExistentFile;
}
return $mockNonexistentFile;
}
) );
$mockLocalRepo->expects( $this->never() )
->method( $this->anythingBut( 'newFile', '__destruct' ) );
$mockRepoGroup = $this->createMock( RepoGroup::class );
$mockRepoGroup->method( 'getLocalRepo' )->willReturn( $mockLocalRepo );
$mockRepoGroup->expects( $this->never() )
->method( $this->anythingBut( 'getLocalRepo', '__destruct' ) );
return $mockRepoGroup;
}
/**
* @param LinkTarget $old
* @param LinkTarget $new
* @param array $params Valid keys are: db, options, nsInfo, wiStore, repoGroup.
* options is an indexed array that will overwrite our defaults, not a ServiceOptions, so it
* need not contain all keys.
* @return MovePage
*/
private function newMovePage( $old, $new, array $params = [] ) : MovePage {
$mockLB = $this->createMock( LoadBalancer::class );
$mockLB->method( 'getConnection' )
->willReturn( $params['db'] ?? $this->createNoOpMock( IDatabase::class ) );
$mockLB->expects( $this->never() )
->method( $this->anythingBut( 'getConnection', '__destruct' ) );
$mockNsInfo = $this->createMock( NamespaceInfo::class );
$mockNsInfo->method( 'isMovable' )->will( $this->returnCallback(
function ( $ns ) {
return $ns >= 0;
}
) );
$mockNsInfo->expects( $this->never() )
->method( $this->anythingBut( 'isMovable', '__destruct' ) );
return new MovePage(
$old,
$new,
new ServiceOptions(
PageCommandFactory::CONSTRUCTOR_OPTIONS,
$params['options'] ?? [],
[
'CategoryCollation' => 'uppercase',
]
),
$mockLB,
$params['nsInfo'] ?? $mockNsInfo,
$params['wiStore'] ?? $this->createNoOpMock( WatchedItemStore::class ),
$params['permMgr'] ?? $this->createNoOpMock( PermissionManager::class ),
$params['repoGroup'] ?? $this->getMockRepoGroup(),
$params['contentHandlerFactory']
?? MediaWikiServices::getInstance()->getContentHandlerFactory()
);
}
public function setUp() : void {
parent::setUp();
// Ensure we have some pages that are guaranteed to exist or not
$this->getExistingTestPage( 'Existent' );
$this->getExistingTestPage( 'Existent2' );
$this->getExistingTestPage( 'File:Existent.jpg' );
$this->getExistingTestPage( 'File:Existent2.jpg' );
$this->getExistingTestPage( 'File:Non-file.jpg' );
$this->getExistingTestPage( 'MediaWiki:Existent.js' );
$this->getExistingTestPage( 'Hooked in place' );
$this->getNonExistingTestPage( 'Nonexistent' );
$this->getNonExistingTestPage( 'Nonexistent2' );
$this->getNonExistingTestPage( 'File:Nonexistent.jpg' );
$this->getNonExistingTestPage( 'File:Nonexistent.png' );
$this->getNonExistingTestPage( 'File:Existent-file-no-page.jpg' );
$this->getNonExistingTestPage( 'MediaWiki:Nonexistent' );
$this->getNonExistingTestPage( 'No content allowed' );
// Set a couple of hooks for specific pages
$this->setTemporaryHook( 'ContentModelCanBeUsedOn',
function ( $modelId, Title $title, &$ok ) {
if ( $title->getPrefixedText() === 'No content allowed' ) {
$ok = false;
}
}
);
$this->setTemporaryHook( 'TitleIsMovable',
function ( Title $title, &$result ) {
if ( strtolower( $title->getPrefixedText() ) === 'hooked in place' ) {
$result = false;
}
}
);
$this->tablesUsed[] = 'page';
$this->tablesUsed[] = 'revision';
$this->tablesUsed[] = 'comment';
}
/**
* @covers MovePage::__construct
*/
public function testConstructorDefaults() {
$services = MediaWikiServices::getInstance();
$obj1 = new MovePage( Title::newFromText( 'A' ), Title::newFromText( 'B' ) );
$obj2 = new MovePage(
Title::newFromText( 'A' ),
Title::newFromText( 'B' ),
new ServiceOptions( MovePage::CONSTRUCTOR_OPTIONS, $services->getMainConfig() ),
$services->getDBLoadBalancer(),
$services->getNamespaceInfo(),
$services->getWatchedItemStore(),
$services->getPermissionManager(),
$services->getRepoGroup(),
$services->getContentHandlerFactory()
);
$this->assertEquals( $obj2, $obj1 );
}
/**
* @dataProvider provideIsValidMove
* @covers MovePage::isValidMove
* @covers MovePage::isValidMoveTarget
* @covers MovePage::isValidFileMove
* @covers MovePage::__construct
*
* @param string|Title $old
* @param string|Title $new
* @param array $expectedErrors
* @param array $extraOptions
*/
public function testIsValidMove(
$old, $new, array $expectedErrors, array $extraOptions = []
) {
$iwLookup = $this->createMock( InterwikiLookup::class );
$iwLookup->method( 'isValidInterwiki' )
->willReturn( true );
$this->setService(
'InterwikiLookup',
$iwLookup
);
if ( is_string( $old ) ) {
$old = Title::newFromText( $old );
}
if ( is_string( $new ) ) {
$new = Title::newFromText( $new );
}
$mp = $this->newMovePage( $old, $new, [ 'options' => $extraOptions ] );
$this->assertSame( $expectedErrors, $mp->isValidMove()->getErrorsArray() );
}
public static function provideIsValidMove() {
$ret = [
'Self move' => [
'Existent',
'Existent',
[ [ 'selfmove' ] ],
],
'Move from empty name' => [
Title::makeTitle( NS_MAIN, '' ),
'Nonexistent',
// @todo More specific error message, or make the move valid if the page actually
// exists somehow in the database
[ [ 'badarticleerror' ] ],
],
'Move to empty name' => [
'Existent',
Title::makeTitle( NS_MAIN, '' ),
[ [ 'movepage-invalid-target-title' ] ],
],
'Move to invalid name' => [
'Existent',
Title::makeTitle( NS_MAIN, '<' ),
[ [ 'movepage-invalid-target-title' ] ],
],
'Move between invalid names' => [
Title::makeTitle( NS_MAIN, '<' ),
Title::makeTitle( NS_MAIN, '>' ),
// @todo First error message should be more specific, or maybe we should make moving
// such pages valid if they actually exist somehow in the database
[ [ 'movepage-source-doesnt-exist' ], [ 'movepage-invalid-target-title' ] ],
],
'Move nonexistent' => [
'Nonexistent',
'Nonexistent2',
[ [ 'movepage-source-doesnt-exist' ] ],
],
'Move over existing' => [
'Existent',
'Existent2',
[ [ 'articleexists', 'Existent2' ] ],
],
'Move from another wiki' => [
Title::makeTitle( NS_MAIN, 'Test', '', 'otherwiki' ),
'Nonexistent',
[ [ 'immobile-source-namespace-iw' ] ],
],
'Move special page' => [
'Special:FooBar',
'Nonexistent',
[ [ 'immobile-source-namespace', 'Special' ] ],
],
'Move to another wiki' => [
'Existent',
Title::makeTitle( NS_MAIN, 'Test', '', 'otherwiki' ),
[ [ 'immobile-target-namespace-iw' ] ],
],
'Move to special page' =>
[ 'Existent', 'Special:FooBar', [ [ 'immobile-target-namespace', 'Special' ] ] ],
'Move to allowed content model' => [
'MediaWiki:Existent.js',
'MediaWiki:Nonexistent',
[],
],
'Move to prohibited content model' => [
'Existent',
'No content allowed',
[ [ 'content-not-allowed-here', 'wikitext', 'No content allowed', 'main' ] ],
],
'Aborted by hook' => [
'Hooked in place',
'Nonexistent',
// @todo Error is wrong
[ [ 'immobile-source-namespace', '' ] ],
],
'Doubly aborted by hook' => [
'Hooked in place',
'Hooked In Place',
// @todo Both errors are wrong
[ [ 'immobile-source-namespace', '' ], [ 'immobile-target-namespace', '' ] ],
],
'Non-file to file' =>
[ 'Existent', 'File:Nonexistent.jpg', [ [ 'nonfile-cannot-move-to-file' ] ] ],
'File to non-file' => [
'File:Existent.jpg',
'Nonexistent',
[ [ 'imagenocrossnamespace' ] ],
],
'Existing file to non-existing file' => [
'File:Existent.jpg',
'File:Nonexistent.jpg',
[],
],
'Existing file to existing file' => [
'File:Existent.jpg',
'File:Existent2.jpg',
[ [ 'articleexists', 'File:Existent2.jpg' ] ],
],
'Existing file to existing file with no page' => [
'File:Existent.jpg',
'File:Existent-file-no-page.jpg',
// @todo Is this correct? Moving over an existing file with no page should succeed?
[],
],
'Existing file to name with slash' => [
'File:Existent.jpg',
'File:Existent/slashed.jpg',
[ [ 'imageinvalidfilename' ] ],
],
'Mismatched file extension' => [
'File:Existent.jpg',
'File:Nonexistent.png',
[ [ 'imagetypemismatch' ] ],
],
'Non-file page in the File namespace' => [
'File:Non-file.jpg',
'File:Non-file-new.png',
[],
],
];
return $ret;
}
/**
* @dataProvider provideMove
* @covers MovePage::move
*
* @param string $old Old name
* @param string $new New name
* @param array $expectedErrors
* @param array $extraOptions
*/
public function testMove( $old, $new, array $expectedErrors, array $extraOptions = [] ) {
$iwLookup = $this->createMock( InterwikiLookup::class );
$iwLookup->method( 'isValidInterwiki' )
->willReturn( true );
$this->setService(
'InterwikiLookup',
$iwLookup
);
if ( is_string( $old ) ) {
$old = Title::newFromText( $old );
}
if ( is_string( $new ) ) {
$new = Title::newFromText( $new );
}
$params = [ 'options' => $extraOptions ];
if ( $expectedErrors === [] ) {
$this->markTestIncomplete( 'Checking actual moves has not yet been implemented' );
}
$obj = $this->newMovePage( $old, $new, $params );
$status = $obj->move( $this->getTestUser()->getUser() );
$this->assertSame( $expectedErrors, $status->getErrorsArray() );
}
public static function provideMove() {
$ret = [];
foreach ( self::provideIsValidMove() as $name => $arr ) {
list( $old, $new, $expectedErrors, $extraOptions ) = array_pad( $arr, 4, [] );
if ( !$new ) {
// Not supported by testMove
continue;
}
$ret[$name] = $arr;
}
return $ret;
}
/**
* Test for the move operation being aborted via the TitleMove hook
* @covers MovePage::move
*/
public function testMoveAbortedByTitleMoveHook() {
$error = 'Preventing move operation with TitleMove hook.';
$this->setTemporaryHook( 'TitleMove',
function ( $old, $new, $user, $reason, $status ) use ( $error ) {
$status->fatal( $error );
}
);
$oldTitle = Title::newFromText( 'Some old title' );
WikiPage::factory( $oldTitle )->doEditContent( new WikitextContent( 'foo' ), 'bar' );
$newTitle = Title::newFromText( 'A brand new title' );
$mp = $this->newMovePage( $oldTitle, $newTitle );
$user = User::newFromName( 'TitleMove tester' );
$status = $mp->move( $user, 'Reason', true );
$this->assertTrue( $status->hasMessage( $error ) );
}
/**
* Test moving subpages from one page to another
* @covers MovePage::moveSubpages
*/
public function testMoveSubpages() {
$name = ucfirst( __FUNCTION__ );
$subPages = [ "Talk:$name/1", "Talk:$name/2" ];
$ids = [];
$pages = [
$name,
"Talk:$name",
"$name 2",
"Talk:$name 2",
];
foreach ( array_merge( $pages, $subPages ) as $page ) {
$ids[$page] = $this->createPage( $page );
}
$oldTitle = Title::newFromText( "Talk:$name" );
$newTitle = Title::newFromText( "Talk:$name 2" );
$mp = new MovePage( $oldTitle, $newTitle );
$status = $mp->moveSubpages( $this->getTestUser()->getUser(), 'Reason', true );
$this->assertTrue( $status->isGood(),
"Moving subpages from Talk:{$name} to Talk:{$name} 2 was not completely successful." );
foreach ( $subPages as $page ) {
$this->assertMoved( $page, str_replace( $name, "$name 2", $page ), $ids[$page] );
}
}
/**
* Test moving subpages from one page to another
* @covers MovePage::moveSubpagesIfAllowed
*/
public function testMoveSubpagesIfAllowed() {
$name = ucfirst( __FUNCTION__ );
$subPages = [ "Talk:$name/1", "Talk:$name/2" ];
$ids = [];
$pages = [
$name,
"Talk:$name",
"$name 2",
"Talk:$name 2",
];
foreach ( array_merge( $pages, $subPages ) as $page ) {
$ids[$page] = $this->createPage( $page );
}
$oldTitle = Title::newFromText( "Talk:$name" );
$newTitle = Title::newFromText( "Talk:$name 2" );
$mp = new MovePage( $oldTitle, $newTitle );
$status = $mp->moveSubpagesIfAllowed( $this->getTestUser()->getUser(), 'Reason', true );
$this->assertTrue( $status->isGood(),
"Moving subpages from Talk:{$name} to Talk:{$name} 2 was not completely successful." );
foreach ( $subPages as $page ) {
$this->assertMoved( $page, str_replace( $name, "$name 2", $page ), $ids[$page] );
}
}
/**
* Shortcut function to create a page and return its id.
*
* @param string $name Page to create
* @return int ID of created page
*/
protected function createPage( $name ) {
return $this->editPage( $name, 'Content' )->value['revision']->getPage();
}
/**
* @param string $from Prefixed name of source
* @param string $to Prefixed name of destination
* @param string $id Page id of the page to move
* @param array|string|null $opts Options: 'noredirect' to expect no redirect
*/
protected function assertMoved( $from, $to, $id, $opts = null ) {
$opts = (array)$opts;
Title::clearCaches();
$fromTitle = Title::newFromText( $from );
$toTitle = Title::newFromText( $to );
$this->assertTrue( $toTitle->exists(),
"Destination {$toTitle->getPrefixedText()} does not exist" );
if ( in_array( 'noredirect', $opts ) ) {
$this->assertFalse( $fromTitle->exists(),
"Source {$fromTitle->getPrefixedText()} exists" );
} else {
$this->assertTrue( $fromTitle->exists(),
"Source {$fromTitle->getPrefixedText()} does not exist" );
$this->assertTrue( $fromTitle->isRedirect(),
"Source {$fromTitle->getPrefixedText()} is not a redirect" );
$target = MediaWikiServices::getInstance()
->getRevisionLookup()
->getRevisionByTitle( $fromTitle )
->getContent( SlotRecord::MAIN )
->getRedirectTarget();
$this->assertSame( $toTitle->getPrefixedText(), $target->getPrefixedText() );
}
$this->assertSame( $id, $toTitle->getArticleID() );
}
}
|