3 namespace MediaWiki\Tests\Maintenance
;
5 use FixDoubleRedirects
;
6 use MediaWiki\Title\Title
;
9 * @covers \FixDoubleRedirects
10 * @covers \DoubleRedirectJob
14 class FixDoubleRedirectsTest
extends MaintenanceBaseTestCase
{
16 protected function getMaintenanceClass() {
17 return FixDoubleRedirects
::class;
20 /** @dataProvider provideExecuteWhenNoDoubleRedirects */
21 public function testExecuteWhenNoDoubleRedirects( $setTitle ) {
22 $testRedirect = $this->getExistingTestPage();
23 $this->editPage( $testRedirect, '#REDIRECT [[Test]]' );
25 $this->maintenance
->setOption( 'title', $testRedirect->getTitle()->getPrefixedText() );
27 $this->maintenance
->execute();
28 $this->expectOutputRegex( '/No double redirects found/' );
31 public static function provideExecuteWhenNoDoubleRedirects() {
33 'Title option set to a redirect' => [ true ],
34 'Title option not set' => [ false ],
38 /** @dataProvider provideExecute */
39 public function testExecute( $options, $expectedProcessedCount, $shouldActuallyFixDoubleRedirect ) {
40 // Create one double redirect
41 $this->editPage( Title
::newFromText( 'DoubleRedirect1' ), '#REDIRECT [[Testing]]' );
42 $this->editPage( Title
::newFromText( 'Testing' ), '#REDIRECT [[Abc]]' );
43 // Create a non-double redirect
44 $this->editPage( Title
::newFromText( 'Test1234' ), '#REDIRECT [[Testabc]]' );
45 // Run the maintenance script
46 foreach ( $options as $name => $value ) {
47 $this->maintenance
->setOption( $name, $value );
49 $this->maintenance
->execute();
50 // Assert on the output and whether the page content has been updated
51 $expectedOutputRegex = "/$expectedProcessedCount double redirects processed";
52 if ( $expectedProcessedCount ) {
53 $expectedOutputRegex .= '[\s\S]*\* \[\[DoubleRedirect1\]\]';
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();
64 wfMessage( 'double-redirect-fixer' )->inContentLanguage()->text(),
65 $lastRevision->getUser()->getName()
67 $this->assertTrue( $lastRevision->isMinor() );
68 // Check that the edit made to fix the double redirect was not sent to recentchanges
69 $this->newSelectQueryBuilder()
70 ->select( 'COUNT(*)' )
71 ->from( 'recentchanges' )
72 ->where( [ 'rc_this_oldid' => $lastRevision->getId() ] )
73 ->assertFieldValue( 0 );
75 $this->assertSame( '#REDIRECT [[Testing]]', $doubleRedirectPageContent );
79 public static function provideExecute() {
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 ],
88 public function testExecuteWhenInAsyncMode() {
89 // Create one double redirect
90 $this->editPage( Title
::newFromText( 'DoubleRedirect1' ), '#REDIRECT [[Testing]]' );
91 $this->editPage( Title
::newFromText( 'Testing' ), '#REDIRECT [[Abc]]' );
92 // Create a non-double redirect
93 $this->editPage( Title
::newFromText( 'Test1234' ), '#REDIRECT [[Testabc]]' );
94 // Run the maintenance script
95 $this->maintenance
->setOption( 'async', 1 );
96 $this->maintenance
->execute();
97 // Run all queued jobs, which were added because the script was run with the
100 // Assert on the output and whether the page content has been updated
101 $this->expectOutputRegex(
102 '/Queuing batch of 1 double redirects[\s\S]*' .
103 '1 double redirects processed[\s\S]*\* \[\[DoubleRedirect1\]\]/'
105 $doubleRedirectWikiPage = $this->getServiceContainer()->getWikiPageFactory()
106 ->newFromTitle( Title
::newFromText( 'DoubleRedirect1' ) );
107 $doubleRedirectPageContent = $doubleRedirectWikiPage->getContent()->getWikitextForTransclusion();
108 $this->assertSame( '#REDIRECT [[Abc]]', $doubleRedirectPageContent );
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();