File: CheckBadRedirectsTest.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 (84 lines) | stat: -rw-r--r-- 2,527 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
<?php

namespace MediaWiki\Tests\Maintenance;

use CheckBadRedirects;
use MediaWiki\Title\Title;

/**
 * @covers \CheckBadRedirects
 * @group Database
 * @author Dreamy Jazz
 */
class CheckBadRedirectsTest extends MaintenanceBaseTestCase {

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

	/**
	 * @param int $count
	 * @return Title[] The titles which are bad redirects
	 */
	private function getPagesIncorrectlyMarkedAsRedirects( int $count ): array {
		$returnArray = [];
		for ( $i = 0; $i < $count; $i++ ) {
			$existingTestPage = $this->getExistingTestPage();
			$returnArray[] = $existingTestPage->getTitle();
		}
		// Set the page_is_redirect as 1 for the test on all pages in $returnArray
		$pageIds = array_map( static function ( $page ) {
			return $page->getId();
		}, $returnArray );
		if ( count( $pageIds ) ) {
			$this->getDb()->newUpdateQueryBuilder()
				->update( 'page' )
				->set( [ 'page_is_redirect' => 1 ] )
				->where( [ 'page_id' => $pageIds ] )
				->execute();
		}
		return $returnArray;
	}

	/**
	 * @param int $count
	 * @return Title[] The titles which are good redirects
	 */
	private function getRedirectPages( int $count ): array {
		$returnArray = [];
		for ( $i = 0; $i < $count; $i++ ) {
			$existingTestPage = $this->getExistingTestPage();
			$this->editPage(
				$existingTestPage,
				$this->getServiceContainer()->getContentHandlerFactory()
					->getContentHandler( CONTENT_MODEL_WIKITEXT )
					->makeRedirectContent( $this->getExistingTestPage()->getTitle() )
			);
			$returnArray[] = $existingTestPage->getTitle();
		}
		return $returnArray;
	}

	/** @dataProvider provideExecute */
	public function testExecute( $brokenCount, $validCount, $expectedRedirectsCount ) {
		$redirectsAreValid = $this->getRedirectPages( $validCount );
		$pagesThatNeedFixing = $this->getPagesIncorrectlyMarkedAsRedirects( $brokenCount );
		// Run the maintenance script
		$this->maintenance->execute();
		// Verify that the count of redirects is expected.
		$expectedOutputRegex = "/[\S\s]*Found $expectedRedirectsCount redirects[\S\s]*";
		// Verify that the pages with an invalid page_is_redirect field are listed.
		foreach ( $pagesThatNeedFixing as $title ) {
			$expectedOutputRegex .= $title->getPrefixedText() . "\n";
		}
		$expectedOutputRegex .= "\nDone.\n/";
		$this->expectOutputRegex( $expectedOutputRegex );
	}

	public static function provideExecute() {
		return [
			'No broken pages' => [ 0, 3, 3 ],
			'Five broken pages' => [ 5, 2, 7 ],
		];
	}
}