| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Maintenance; |
| 4 |
|
| 5 |
use PHPUnit\Framework\ExpectationFailedException; |
| 6 |
use PurgePage; |
| 7 |
use WikiPage; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class SemiMockedPurgePage extends PurgePage { |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
private ?string $mockStdinFile = null; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public function mockStdin( string $filepath ) { |
| 25 |
$this->mockStdinFile = $filepath; |
| 26 |
} |
| 27 |
|
| 28 |
public function getStdin( $len = null ) { |
| 29 |
if ( $len !== null ) { |
| 30 |
throw new ExpectationFailedException( "::getStdin call was expected to not pass any arguments" ); |
| 31 |
} |
| 32 |
|
| 33 |
return fopen( $this->mockStdinFile, 'rt' ); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
class PurgePageTest extends MaintenanceBaseTestCase { |
| 43 |
|
| 44 |
|
| 45 |
protected $maintenance; |
| 46 |
|
| 47 |
protected function getMaintenanceClass() { |
| 48 |
return SemiMockedPurgePage::class; |
| 49 |
} |
| 50 |
|
| 51 |
private function getFileWithContent( string $content ): string { |
| 52 |
$testFilename = $this->getNewTempFile(); |
| 53 |
$testFile = fopen( $testFilename, 'w' ); |
| 54 |
fwrite( $testFile, $content ); |
| 55 |
fclose( $testFile ); |
| 56 |
return $testFilename; |
| 57 |
} |
| 58 |
|
| 59 |
public function testPurgeForInvalidTitle() { |
| 60 |
$this->setTemporaryHook( 'ArticlePurge', function () { |
| 61 |
$this->fail( 'No pages should be purged if the title is invalid.' ); |
| 62 |
} ); |
| 63 |
$this->maintenance->mockStdin( $this->getFileWithContent( ":::\n\n\n\n" ) ); |
| 64 |
$this->maintenance->execute(); |
| 65 |
$this->expectOutputRegex( '/Invalid page title/' ); |
| 66 |
} |
| 67 |
|
| 68 |
public function testPurgeForNonExistingPage() { |
| 69 |
$this->setTemporaryHook( 'ArticlePurge', function () { |
| 70 |
$this->fail( |
| 71 |
'No pages should be purged if the title does not exist and --skip-exists-check is not specified.' |
| 72 |
); |
| 73 |
} ); |
| 74 |
$this->maintenance->mockStdin( $this->getFileWithContent( "NonExistingTestPage1234" ) ); |
| 75 |
$this->maintenance->execute(); |
| 76 |
$this->expectOutputRegex( '/Page doesn\'t exist/' ); |
| 77 |
} |
| 78 |
|
| 79 |
public function testPurgeForFailedPurge() { |
| 80 |
|
| 81 |
$title = $this->getExistingTestPage()->getTitle(); |
| 82 |
$hookCalled = false; |
| 83 |
$this->setTemporaryHook( |
| 84 |
'ArticlePurge', |
| 85 |
function ( WikiPage $actualTitle ) use ( &$hookCalled, $title ) { |
| 86 |
$this->assertTrue( $actualTitle->isSamePageAs( $title ) ); |
| 87 |
$hookCalled = true; |
| 88 |
return false; |
| 89 |
} |
| 90 |
); |
| 91 |
|
| 92 |
$this->maintenance->mockStdin( $this->getFileWithContent( $title ) ); |
| 93 |
$this->maintenance->execute(); |
| 94 |
$this->expectOutputRegex( '/Purge failed for ' . preg_quote( $title, '/' ) . '/' ); |
| 95 |
$this->assertTrue( $hookCalled ); |
| 96 |
} |
| 97 |
|
| 98 |
public function testPurge() { |
| 99 |
|
| 100 |
$title = $this->getExistingTestPage()->getTitle(); |
| 101 |
$hookCalled = false; |
| 102 |
$this->setTemporaryHook( |
| 103 |
'ArticlePurge', |
| 104 |
function ( WikiPage $actualTitle ) use ( &$hookCalled, $title ) { |
| 105 |
$this->assertTrue( $actualTitle->isSamePageAs( $title ) ); |
| 106 |
$hookCalled = true; |
| 107 |
} |
| 108 |
); |
| 109 |
|
| 110 |
$this->maintenance->mockStdin( $this->getFileWithContent( $title ) ); |
| 111 |
$this->maintenance->execute(); |
| 112 |
$this->expectOutputRegex( '/Purged ' . preg_quote( $title, '/' ) . '/' ); |
| 113 |
$this->assertTrue( $hookCalled ); |
| 114 |
} |
| 115 |
} |