| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Maintenance; |
| 4 |
|
| 5 |
use FixDoubleRedirects; |
| 6 |
use MediaWiki\Title\Title; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class FixDoubleRedirectsTest extends MaintenanceBaseTestCase { |
| 15 |
|
| 16 |
protected function getMaintenanceClass() { |
| 17 |
return FixDoubleRedirects::class; |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
public function testExecuteWhenNoDoubleRedirects( $setTitle ) { |
| 22 |
$testRedirect = $this->getExistingTestPage(); |
| 23 |
$this->editPage( $testRedirect, '#REDIRECT [[Test]]' ); |
| 24 |
if ( $setTitle ) { |
| 25 |
$this->maintenance->setOption( 'title', $testRedirect->getTitle()->getPrefixedText() ); |
| 26 |
} |
| 27 |
$this->maintenance->execute(); |
| 28 |
$this->expectOutputRegex( '/No double redirects found/' ); |
| 29 |
} |
| 30 |
|
| 31 |
public static function provideExecuteWhenNoDoubleRedirects() { |
| 32 |
return [ |
| 33 |
'Title option set to a redirect' => [ true ], |
| 34 |
'Title option not set' => [ false ], |
| 35 |
]; |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
public function testExecute( $options, $expectedProcessedCount, $shouldActuallyFixDoubleRedirect ) { |
| 40 |
|
| 41 |
$this->editPage( Title::newFromText( 'DoubleRedirect1' ), '#REDIRECT [[Testing]]' ); |
| 42 |
$this->editPage( Title::newFromText( 'Testing' ), '#REDIRECT [[Abc]]' ); |
| 43 |
|
| 44 |
$this->editPage( Title::newFromText( 'Test1234' ), '#REDIRECT [[Testabc]]' ); |
| 45 |
|
| 46 |
foreach ( $options as $name => $value ) { |
| 47 |
$this->maintenance->setOption( $name, $value ); |
| 48 |
} |
| 49 |
$this->maintenance->execute(); |
| 50 |
|
| 51 |
$expectedOutputRegex = "/$expectedProcessedCount double redirects processed"; |
| 52 |
if ( $expectedProcessedCount ) { |
| 53 |
$expectedOutputRegex .= '[\s\S]*\* \[\[DoubleRedirect1\]\]'; |
| 54 |
} |
| 55 |
$expectedOutputRegex .= '/'; |
| 56 |
$this->expectOutputRegex( $expectedOutputRegex ); |
| 57 |
$doubleRedirectWikiPage = $this->getServiceContainer()->getWikiPageFactory() |
| 58 |
->newFromTitle( Title::newFromText( 'DoubleRedirect1' ) ); |
| 59 |
$doubleRedirectPageContent = $doubleRedirectWikiPage->getContent()->getWikitextForTransclusion(); |
| 60 |
if ( $shouldActuallyFixDoubleRedirect ) { |
| 61 |
$this->assertSame( '#REDIRECT [[Abc]]', $doubleRedirectPageContent ); |
| 62 |
$lastRevision = $doubleRedirectWikiPage->getRevisionRecord(); |
| 63 |
$this->assertSame( |
| 64 |
wfMessage( 'double-redirect-fixer' )->inContentLanguage()->text(), |
| 65 |
$lastRevision->getUser()->getName() |
| 66 |
); |
| 67 |
$this->assertTrue( $lastRevision->isMinor() ); |
| 68 |
|
| 69 |
$this->newSelectQueryBuilder() |
| 70 |
->select( 'COUNT(*)' ) |
| 71 |
->from( 'recentchanges' ) |
| 72 |
->where( [ 'rc_this_oldid' => $lastRevision->getId() ] ) |
| 73 |
->assertFieldValue( 0 ); |
| 74 |
} else { |
| 75 |
$this->assertSame( '#REDIRECT [[Testing]]', $doubleRedirectPageContent ); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
public static function provideExecute() { |
| 80 |
return [ |
| 81 |
'Dry-run' => [ [ 'dry-run' => 1 ], 1, false ], |
| 82 |
'Dry-run with title set' => [ [ 'dry-run' => 1, 'title' => 'Testing' ], 1, false ], |
| 83 |
'No options' => [ [], 1, true ], |
| 84 |
'Title set' => [ [ 'title' => 'Testing' ], 1, true ], |
| 85 |
]; |
| 86 |
} |
| 87 |
|
| 88 |
public function testExecuteWhenInAsyncMode() { |
| 89 |
|
| 90 |
$this->editPage( Title::newFromText( 'DoubleRedirect1' ), '#REDIRECT [[Testing]]' ); |
| 91 |
$this->editPage( Title::newFromText( 'Testing' ), '#REDIRECT [[Abc]]' ); |
| 92 |
|
| 93 |
$this->editPage( Title::newFromText( 'Test1234' ), '#REDIRECT [[Testabc]]' ); |
| 94 |
|
| 95 |
$this->maintenance->setOption( 'async', 1 ); |
| 96 |
$this->maintenance->execute(); |
| 97 |
|
| 98 |
|
| 99 |
$this->runJobs(); |
| 100 |
|
| 101 |
$this->expectOutputRegex( |
| 102 |
'/Queuing batch of 1 double redirects[\s\S]*' . |
| 103 |
'1 double redirects processed[\s\S]*\* \[\[DoubleRedirect1\]\]/' |
| 104 |
); |
| 105 |
$doubleRedirectWikiPage = $this->getServiceContainer()->getWikiPageFactory() |
| 106 |
->newFromTitle( Title::newFromText( 'DoubleRedirect1' ) ); |
| 107 |
$doubleRedirectPageContent = $doubleRedirectWikiPage->getContent()->getWikitextForTransclusion(); |
| 108 |
$this->assertSame( '#REDIRECT [[Abc]]', $doubleRedirectPageContent ); |
| 109 |
} |
| 110 |
|
| 111 |
public function testExecuteWhenTitleIsNotARedirect() { |
| 112 |
$this->expectCallToFatalError(); |
| 113 |
$this->expectOutputRegex( '/Testabc is not a redirect/' ); |
| 114 |
$this->maintenance->setOption( 'title', 'Testabc' ); |
| 115 |
$this->maintenance->execute(); |
| 116 |
} |
| 117 |
} |