| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Maintenance; |
| 4 |
|
| 5 |
use CheckBadRedirects; |
| 6 |
use MediaWiki\Title\Title; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class CheckBadRedirectsTest extends MaintenanceBaseTestCase { |
| 14 |
|
| 15 |
protected function getMaintenanceClass() { |
| 16 |
return CheckBadRedirects::class; |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
private function getPagesIncorrectlyMarkedAsRedirects( int $count ): array { |
| 24 |
$returnArray = []; |
| 25 |
for ( $i = 0; $i < $count; $i++ ) { |
| 26 |
$existingTestPage = $this->getExistingTestPage(); |
| 27 |
$returnArray[] = $existingTestPage->getTitle(); |
| 28 |
} |
| 29 |
|
| 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(); |
| 39 |
} |
| 40 |
return $returnArray; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 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() ) |
| 56 |
); |
| 57 |
$returnArray[] = $existingTestPage->getTitle(); |
| 58 |
} |
| 59 |
return $returnArray; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
public function testExecute( $brokenCount, $validCount, $expectedRedirectsCount ) { |
| 64 |
$redirectsAreValid = $this->getRedirectPages( $validCount ); |
| 65 |
$pagesThatNeedFixing = $this->getPagesIncorrectlyMarkedAsRedirects( $brokenCount ); |
| 66 |
|
| 67 |
$this->maintenance->execute(); |
| 68 |
|
| 69 |
$expectedOutputRegex = "/[\S\s]*Found $expectedRedirectsCount redirects[\S\s]*"; |
| 70 |
|
| 71 |
foreach ( $pagesThatNeedFixing as $title ) { |
| 72 |
$expectedOutputRegex .= $title->getPrefixedText() . "\n"; |
| 73 |
} |
| 74 |
$expectedOutputRegex .= "\nDone.\n/"; |
| 75 |
$this->expectOutputRegex( $expectedOutputRegex ); |
| 76 |
} |
| 77 |
|
| 78 |
public static function provideExecute() { |
| 79 |
return [ |
| 80 |
'No broken pages' => [ 0, 3, 3 ], |
| 81 |
'Five broken pages' => [ 5, 2, 7 ], |
| 82 |
]; |
| 83 |
} |
| 84 |
} |