| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Maintenance; |
| 4 |
|
| 5 |
use DeleteSelfExternals; |
| 6 |
use MediaWiki\MainConfigNames; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class DeleteSelfExternalsTest extends MaintenanceBaseTestCase { |
| 14 |
|
| 15 |
protected function getMaintenanceClass() { |
| 16 |
return DeleteSelfExternals::class; |
| 17 |
} |
| 18 |
|
| 19 |
public function testExecuteForInvalidServerConfig() { |
| 20 |
$this->overrideConfigValue( MainConfigNames::Server, '::::::' ); |
| 21 |
$this->expectOutputRegex( '/Could not parse \$wgServer/' ); |
| 22 |
$this->expectCallToFatalError(); |
| 23 |
$this->maintenance->execute(); |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
public function testExecute( $serverConfigValue, $expectedRowsAfterExecution ) { |
| 28 |
|
| 29 |
|
| 30 |
$firstTestPage = $this->getExistingTestPage( 'TestPage1' ); |
| 31 |
$secondTestPage = $this->getExistingTestPage( 'TestPage2' ); |
| 32 |
$thirdTestPage = $this->getExistingTestPage( 'TestPage3' ); |
| 33 |
$this->editPage( $firstTestPage, '[https://en.wikipedia.org link 1] [https://de.wikipedia.org link 2]' ); |
| 34 |
$this->editPage( $secondTestPage, '[http://en.wikipedia.org link 4]' ); |
| 35 |
$this->editPage( $thirdTestPage, '[//en.wikipedia.org:345 link 5]' ); |
| 36 |
|
| 37 |
$actualRowsBeforeExecution = $this->newSelectQueryBuilder() |
| 38 |
->select( 'el_to_domain_index' ) |
| 39 |
->from( 'externallinks' ) |
| 40 |
->fetchFieldValues(); |
| 41 |
$this->assertArrayEquals( |
| 42 |
[ |
| 43 |
'https://org.wikipedia.de.', 'http://org.wikipedia.en.', 'https://org.wikipedia.en.', |
| 44 |
'https://org.wikipedia.en.:345', |
| 45 |
], |
| 46 |
$actualRowsBeforeExecution |
| 47 |
); |
| 48 |
|
| 49 |
$this->overrideConfigValue( MainConfigNames::Server, $serverConfigValue ); |
| 50 |
|
| 51 |
$this->maintenance->execute(); |
| 52 |
|
| 53 |
$actualRowsAfterExecution = $this->newSelectQueryBuilder() |
| 54 |
->select( 'el_to_domain_index' ) |
| 55 |
->from( 'externallinks' ) |
| 56 |
->fetchFieldValues(); |
| 57 |
$this->assertArrayEquals( $expectedRowsAfterExecution, $actualRowsAfterExecution ); |
| 58 |
$expectedRowsDeleted = count( $actualRowsBeforeExecution ) - count( $actualRowsAfterExecution ); |
| 59 |
$this->expectOutputString( |
| 60 |
"Deleting self externals from $serverConfigValue\n" . |
| 61 |
"Deleting self-externals with el_id 0 to 1000\n" . |
| 62 |
"Deleting self-externals with el_id 1000 to 2000\n" . |
| 63 |
"done; deleted $expectedRowsDeleted rows\n" |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
public static function provideExecute() { |
| 68 |
return [ |
| 69 |
'wgServer specifies scheme' => [ |
| 70 |
'https://en.wikipedia.org', [ 'https://org.wikipedia.de.', 'http://org.wikipedia.en.' ], |
| 71 |
], |
| 72 |
'wgServer does not specify scheme' => [ '//en.wikipedia.org', [ 'https://org.wikipedia.de.' ] ], |
| 73 |
'wgServer includes port' => [ |
| 74 |
'//en.wikipedia.org:345', |
| 75 |
[ 'https://org.wikipedia.de.', 'http://org.wikipedia.en.', 'https://org.wikipedia.en.' ], |
| 76 |
], |
| 77 |
]; |
| 78 |
} |
| 79 |
} |