Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / maintenance / CheckBadRedirectsTest.php
blob9145654a65a26c05159a2bade9e2971d7c4fbf3e
1 <?php
3 namespace MediaWiki\Tests\Maintenance;
5 use CheckBadRedirects;
6 use MediaWiki\Title\Title;
8 /**
9 * @covers \CheckBadRedirects
10 * @group Database
11 * @author Dreamy Jazz
13 class CheckBadRedirectsTest extends MaintenanceBaseTestCase {
15 protected function getMaintenanceClass() {
16 return CheckBadRedirects::class;
19 /**
20 * @param int $count
21 * @return Title[] The titles which are bad redirects
23 private function getPagesIncorrectlyMarkedAsRedirects( int $count ): array {
24 $returnArray = [];
25 for ( $i = 0; $i < $count; $i++ ) {
26 $existingTestPage = $this->getExistingTestPage();
27 $returnArray[] = $existingTestPage->getTitle();
29 // Set the page_is_redirect as 1 for the test on all pages in $returnArray
30 $pageIds = array_map( static function ( $page ) {
31 return $page->getId();
32 }, $returnArray );
33 if ( count( $pageIds ) ) {
34 $this->getDb()->newUpdateQueryBuilder()
35 ->update( 'page' )
36 ->set( [ 'page_is_redirect' => 1 ] )
37 ->where( [ 'page_id' => $pageIds ] )
38 ->execute();
40 return $returnArray;
43 /**
44 * @param int $count
45 * @return Title[] The titles which are good redirects
47 private function getRedirectPages( int $count ): array {
48 $returnArray = [];
49 for ( $i = 0; $i < $count; $i++ ) {
50 $existingTestPage = $this->getExistingTestPage();
51 $this->editPage(
52 $existingTestPage,
53 $this->getServiceContainer()->getContentHandlerFactory()
54 ->getContentHandler( CONTENT_MODEL_WIKITEXT )
55 ->makeRedirectContent( $this->getExistingTestPage()->getTitle() )
57 $returnArray[] = $existingTestPage->getTitle();
59 return $returnArray;
62 /** @dataProvider provideExecute */
63 public function testExecute( $brokenCount, $validCount, $expectedRedirectsCount ) {
64 $redirectsAreValid = $this->getRedirectPages( $validCount );
65 $pagesThatNeedFixing = $this->getPagesIncorrectlyMarkedAsRedirects( $brokenCount );
66 // Run the maintenance script
67 $this->maintenance->execute();
68 // Verify that the count of redirects is expected.
69 $expectedOutputRegex = "/[\S\s]*Found $expectedRedirectsCount redirects[\S\s]*";
70 // Verify that the pages with an invalid page_is_redirect field are listed.
71 foreach ( $pagesThatNeedFixing as $title ) {
72 $expectedOutputRegex .= $title->getPrefixedText() . "\n";
74 $expectedOutputRegex .= "\nDone.\n/";
75 $this->expectOutputRegex( $expectedOutputRegex );
78 public static function provideExecute() {
79 return [
80 'No broken pages' => [ 0, 3, 3 ],
81 'Five broken pages' => [ 5, 2, 7 ],