3 namespace MediaWiki\Tests\Maintenance
;
5 use PHPUnit\Framework\ExpectationFailedException
;
10 * The PurgePage maintenance script with the input mocked to allow easier testing.
12 class SemiMockedPurgePage
extends PurgePage
{
15 * @var string|null The filename to a file which contains the mock input to the script.
17 private ?
string $mockStdinFile = null;
20 * Data for the fake stdin
22 * @param string $filepath The string to be used instead of stdin
24 public function mockStdin( string $filepath ) {
25 $this->mockStdinFile
= $filepath;
28 public function getStdin( $len = null ) {
29 if ( $len !== null ) {
30 throw new ExpectationFailedException( "::getStdin call was expected to not pass any arguments" );
33 return fopen( $this->mockStdinFile
, 'rt' );
42 class PurgePageTest
extends MaintenanceBaseTestCase
{
44 /** @var SemiMockedPurgePage */
45 protected $maintenance;
47 protected function getMaintenanceClass() {
48 return SemiMockedPurgePage
::class;
51 private function getFileWithContent( string $content ): string {
52 $testFilename = $this->getNewTempFile();
53 $testFile = fopen( $testFilename, 'w' );
54 fwrite( $testFile, $content );
59 public function testPurgeForInvalidTitle() {
60 $this->setTemporaryHook( 'ArticlePurge', function () {
61 $this->fail( 'No pages should be purged if the title is invalid.' );
63 $this->maintenance
->mockStdin( $this->getFileWithContent( ":::\n\n\n\n" ) );
64 $this->maintenance
->execute();
65 $this->expectOutputRegex( '/Invalid page title/' );
68 public function testPurgeForNonExistingPage() {
69 $this->setTemporaryHook( 'ArticlePurge', function () {
71 'No pages should be purged if the title does not exist and --skip-exists-check is not specified.'
74 $this->maintenance
->mockStdin( $this->getFileWithContent( "NonExistingTestPage1234" ) );
75 $this->maintenance
->execute();
76 $this->expectOutputRegex( '/Page doesn\'t exist/' );
79 public function testPurgeForFailedPurge() {
80 // Expect that the ArticlePurge hook is called, as a purge attempt should be made.
81 $title = $this->getExistingTestPage()->getTitle();
83 $this->setTemporaryHook(
85 function ( WikiPage
$actualTitle ) use ( &$hookCalled, $title ) {
86 $this->assertTrue( $actualTitle->isSamePageAs( $title ) );
91 // Call the maintenance script and expect that the purge failed (because of the hook).
92 $this->maintenance
->mockStdin( $this->getFileWithContent( $title ) );
93 $this->maintenance
->execute();
94 $this->expectOutputRegex( '/Purge failed for ' . preg_quote( $title, '/' ) . '/' );
95 $this->assertTrue( $hookCalled );
98 public function testPurge() {
99 // Expect that the ArticlePurge hook is called, as a purge attempt should be made.
100 $title = $this->getExistingTestPage()->getTitle();
102 $this->setTemporaryHook(
104 function ( WikiPage
$actualTitle ) use ( &$hookCalled, $title ) {
105 $this->assertTrue( $actualTitle->isSamePageAs( $title ) );
109 // Call the maintenance script and expect that the purge worked.
110 $this->maintenance
->mockStdin( $this->getFileWithContent( $title ) );
111 $this->maintenance
->execute();
112 $this->expectOutputRegex( '/Purged ' . preg_quote( $title, '/' ) . '/' );
113 $this->assertTrue( $hookCalled );