3 namespace MediaWiki\Tests\Maintenance
;
6 use MediaWiki\Title\Title
;
9 * @covers \CheckBadRedirects
13 class CheckBadRedirectsTest
extends MaintenanceBaseTestCase
{
15 protected function getMaintenanceClass() {
16 return CheckBadRedirects
::class;
21 * @return Title[] The titles which are bad redirects
23 private function getPagesIncorrectlyMarkedAsRedirects( int $count ): array {
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();
33 if ( count( $pageIds ) ) {
34 $this->getDb()->newUpdateQueryBuilder()
36 ->set( [ 'page_is_redirect' => 1 ] )
37 ->where( [ 'page_id' => $pageIds ] )
45 * @return Title[] The titles which are good redirects
47 private function getRedirectPages( int $count ): array {
49 for ( $i = 0; $i < $count; $i++
) {
50 $existingTestPage = $this->getExistingTestPage();
53 $this->getServiceContainer()->getContentHandlerFactory()
54 ->getContentHandler( CONTENT_MODEL_WIKITEXT
)
55 ->makeRedirectContent( $this->getExistingTestPage()->getTitle() )
57 $returnArray[] = $existingTestPage->getTitle();
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() {
80 'No broken pages' => [ 0, 3, 3 ],
81 'Five broken pages' => [ 5, 2, 7 ],