Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F7402428
PurgePageTest.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
3 KB
Referenced Files
None
Subscribers
None
PurgePageTest.php
View Options
<?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
);
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sat, Nov 21, 18:03 (18 h, 10 m)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
79/6a/22ef1636e223e53209c6498d3c18
Default Alt Text
PurgePageTest.php (3 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment