Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / maintenance / PurgePageTest.php
blobc7b338f24453b2440644bf121cdec2d576b121a3
1 <?php
3 namespace MediaWiki\Tests\Maintenance;
5 use PHPUnit\Framework\ExpectationFailedException;
6 use PurgePage;
7 use WikiPage;
9 /**
10 * The PurgePage maintenance script with the input mocked to allow easier testing.
12 class SemiMockedPurgePage extends PurgePage {
14 /**
15 * @var string|null The filename to a file which contains the mock input to the script.
17 private ?string $mockStdinFile = null;
19 /**
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' );
37 /**
38 * @covers \PurgePage
39 * @group Database
40 * @author Dreamy Jazz
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 );
55 fclose( $testFile );
56 return $testFilename;
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/' );
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.'
73 } );
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();
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;
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();
101 $hookCalled = false;
102 $this->setTemporaryHook(
103 'ArticlePurge',
104 function ( WikiPage $actualTitle ) use ( &$hookCalled, $title ) {
105 $this->assertTrue( $actualTitle->isSamePageAs( $title ) );
106 $hookCalled = true;
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 );