File: FixDoubleRedirectsTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (117 lines) | stat: -rw-r--r-- 4,563 bytes parent folder | download
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
<?php

namespace MediaWiki\Tests\Maintenance;

use FixDoubleRedirects;
use MediaWiki\Title\Title;

/**
 * @covers \FixDoubleRedirects
 * @covers \DoubleRedirectJob
 * @group Database
 * @author Dreamy Jazz
 */
class FixDoubleRedirectsTest extends MaintenanceBaseTestCase {

	protected function getMaintenanceClass() {
		return FixDoubleRedirects::class;
	}

	/** @dataProvider provideExecuteWhenNoDoubleRedirects */
	public function testExecuteWhenNoDoubleRedirects( $setTitle ) {
		$testRedirect = $this->getExistingTestPage();
		$this->editPage( $testRedirect, '#REDIRECT [[Test]]' );
		if ( $setTitle ) {
			$this->maintenance->setOption( 'title', $testRedirect->getTitle()->getPrefixedText() );
		}
		$this->maintenance->execute();
		$this->expectOutputRegex( '/No double redirects found/' );
	}

	public static function provideExecuteWhenNoDoubleRedirects() {
		return [
			'Title option set to a redirect' => [ true ],
			'Title option not set' => [ false ],
		];
	}

	/** @dataProvider provideExecute */
	public function testExecute( $options, $expectedProcessedCount, $shouldActuallyFixDoubleRedirect ) {
		// Create one double redirect
		$this->editPage( Title::newFromText( 'DoubleRedirect1' ), '#REDIRECT [[Testing]]' );
		$this->editPage( Title::newFromText( 'Testing' ), '#REDIRECT [[Abc]]' );
		// Create a non-double redirect
		$this->editPage( Title::newFromText( 'Test1234' ), '#REDIRECT [[Testabc]]' );
		// Run the maintenance script
		foreach ( $options as $name => $value ) {
			$this->maintenance->setOption( $name, $value );
		}
		$this->maintenance->execute();
		// Assert on the output and whether the page content has been updated
		$expectedOutputRegex = "/$expectedProcessedCount double redirects processed";
		if ( $expectedProcessedCount ) {
			$expectedOutputRegex .= '[\s\S]*\* \[\[DoubleRedirect1\]\]';
		}
		$expectedOutputRegex .= '/';
		$this->expectOutputRegex( $expectedOutputRegex );
		$doubleRedirectWikiPage = $this->getServiceContainer()->getWikiPageFactory()
			->newFromTitle( Title::newFromText( 'DoubleRedirect1' ) );
		$doubleRedirectPageContent = $doubleRedirectWikiPage->getContent()->getWikitextForTransclusion();
		if ( $shouldActuallyFixDoubleRedirect ) {
			$this->assertSame( '#REDIRECT [[Abc]]', $doubleRedirectPageContent );
			$lastRevision = $doubleRedirectWikiPage->getRevisionRecord();
			$this->assertSame(
				wfMessage( 'double-redirect-fixer' )->inContentLanguage()->text(),
				$lastRevision->getUser()->getName()
			);
			$this->assertTrue( $lastRevision->isMinor() );
			// Check that the edit made to fix the double redirect was not sent to recentchanges
			$this->newSelectQueryBuilder()
				->select( 'COUNT(*)' )
				->from( 'recentchanges' )
				->where( [ 'rc_this_oldid' => $lastRevision->getId() ] )
				->assertFieldValue( 0 );
		} else {
			$this->assertSame( '#REDIRECT [[Testing]]', $doubleRedirectPageContent );
		}
	}

	public static function provideExecute() {
		return [
			'Dry-run' => [ [ 'dry-run' => 1 ], 1, false ],
			'Dry-run with title set' => [ [ 'dry-run' => 1, 'title' => 'Testing' ], 1, false ],
			'No options' => [ [], 1, true ],
			'Title set' => [ [ 'title' => 'Testing' ], 1, true ],
		];
	}

	public function testExecuteWhenInAsyncMode() {
		// Create one double redirect
		$this->editPage( Title::newFromText( 'DoubleRedirect1' ), '#REDIRECT [[Testing]]' );
		$this->editPage( Title::newFromText( 'Testing' ), '#REDIRECT [[Abc]]' );
		// Create a non-double redirect
		$this->editPage( Title::newFromText( 'Test1234' ), '#REDIRECT [[Testabc]]' );
		// Run the maintenance script
		$this->maintenance->setOption( 'async', 1 );
		$this->maintenance->execute();
		// Run all queued jobs, which were added because the script was run with the
		// async mode.
		$this->runJobs();
		// Assert on the output and whether the page content has been updated
		$this->expectOutputRegex(
			'/Queuing batch of 1 double redirects[\s\S]*' .
			'1 double redirects processed[\s\S]*\* \[\[DoubleRedirect1\]\]/'
		);
		$doubleRedirectWikiPage = $this->getServiceContainer()->getWikiPageFactory()
			->newFromTitle( Title::newFromText( 'DoubleRedirect1' ) );
		$doubleRedirectPageContent = $doubleRedirectWikiPage->getContent()->getWikitextForTransclusion();
		$this->assertSame( '#REDIRECT [[Abc]]', $doubleRedirectPageContent );
	}

	public function testExecuteWhenTitleIsNotARedirect() {
		$this->expectCallToFatalError();
		$this->expectOutputRegex( '/Testabc is not a redirect/' );
		$this->maintenance->setOption( 'title', 'Testabc' );
		$this->maintenance->execute();
	}
}