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