2019-08-18 18:19:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
2020-05-11 08:58:38 +00:00
|
|
|
use MediaWiki\HookContainer\HookContainer;
|
2020-02-11 22:13:25 +00:00
|
|
|
use PHPUnit\Framework\Constraint\Constraint;
|
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2021-04-02 01:18:18 +00:00
|
|
|
use Psr\Container\ContainerInterface;
|
2022-03-09 22:16:22 +00:00
|
|
|
use Wikimedia\ObjectFactory\ObjectFactory;
|
2021-04-02 01:18:18 +00:00
|
|
|
use Wikimedia\Services\NoSuchServiceException;
|
2021-04-14 18:34:41 +00:00
|
|
|
use Wikimedia\Timestamp\ConvertibleTimestamp;
|
2020-02-11 22:13:25 +00:00
|
|
|
|
2019-08-18 18:19:05 +00:00
|
|
|
/**
|
|
|
|
|
* For code common to both MediaWikiUnitTestCase and MediaWikiIntegrationTestCase.
|
|
|
|
|
*/
|
|
|
|
|
trait MediaWikiTestCaseTrait {
|
2020-05-09 01:15:03 +00:00
|
|
|
/** @var int|null */
|
|
|
|
|
private $originalPhpErrorFilter;
|
|
|
|
|
|
2022-04-04 19:41:23 +00:00
|
|
|
/** @var array */
|
|
|
|
|
private $expectedDeprecations = [];
|
|
|
|
|
|
|
|
|
|
/** @var array */
|
|
|
|
|
private $actualDeprecations = [];
|
|
|
|
|
|
2019-08-18 18:19:05 +00:00
|
|
|
/**
|
2021-03-07 14:06:24 +00:00
|
|
|
* Returns a PHPUnit constraint that matches (with `===`) anything other than a fixed set of values.
|
|
|
|
|
* This can be used to list accepted values, e.g.
|
2019-08-18 18:19:05 +00:00
|
|
|
* $mock->expects( $this->never() )->method( $this->anythingBut( 'foo', 'bar' ) );
|
|
|
|
|
* which will throw if any unexpected method is called.
|
|
|
|
|
*
|
|
|
|
|
* @param mixed ...$values Values that are not matched
|
2020-02-11 22:13:25 +00:00
|
|
|
* @return Constraint
|
2019-08-18 18:19:05 +00:00
|
|
|
*/
|
|
|
|
|
protected function anythingBut( ...$values ) {
|
2021-04-21 14:57:51 +00:00
|
|
|
if ( !in_array( '__destruct', $values, true ) ) {
|
|
|
|
|
// Ensure that __destruct is always included. PHPUnit will fail very hard with no
|
|
|
|
|
// useful output if __destruct ends up being called (T280780).
|
|
|
|
|
$values[] = '__destruct';
|
|
|
|
|
}
|
2019-08-18 18:19:05 +00:00
|
|
|
return $this->logicalNot( $this->logicalOr(
|
2021-03-07 14:06:24 +00:00
|
|
|
...array_map( [ $this, 'identicalTo' ], $values )
|
2019-08-18 18:19:05 +00:00
|
|
|
) );
|
|
|
|
|
}
|
2019-08-28 10:01:39 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return a PHPUnit mock that is expected to never have any methods called on it.
|
|
|
|
|
*
|
2022-10-11 18:30:25 +00:00
|
|
|
* @psalm-template RealInstanceType of object
|
|
|
|
|
*
|
|
|
|
|
* @psalm-param class-string<RealInstanceType> $type
|
|
|
|
|
* @psalm-param list<string> $allow Methods to allow
|
|
|
|
|
*
|
2019-08-28 10:01:39 +00:00
|
|
|
* @param string $type
|
2022-10-11 18:30:25 +00:00
|
|
|
* @param string[] $allow Methods to allow
|
2020-02-27 22:00:28 +00:00
|
|
|
*
|
2022-10-11 18:30:25 +00:00
|
|
|
* @return MockObject&RealInstanceType
|
2019-08-28 10:01:39 +00:00
|
|
|
*/
|
2020-02-27 22:00:28 +00:00
|
|
|
protected function createNoOpMock( $type, $allow = [] ) {
|
2019-08-28 10:01:39 +00:00
|
|
|
$mock = $this->createMock( $type );
|
2020-02-27 22:00:28 +00:00
|
|
|
$mock->expects( $this->never() )->method( $this->anythingBut( '__destruct', ...$allow ) );
|
2019-08-28 10:01:39 +00:00
|
|
|
return $mock;
|
|
|
|
|
}
|
2019-09-17 15:23:17 +00:00
|
|
|
|
2019-11-08 21:24:00 +00:00
|
|
|
/**
|
|
|
|
|
* Return a PHPUnit mock that is expected to never have any methods called on it.
|
|
|
|
|
*
|
2022-10-11 18:30:25 +00:00
|
|
|
* @psalm-template RealInstanceType of object
|
|
|
|
|
*
|
|
|
|
|
* @psalm-param class-string<RealInstanceType> $type
|
|
|
|
|
* @psalm-param list<string> $allow Methods to allow
|
|
|
|
|
*
|
2019-11-08 21:24:00 +00:00
|
|
|
* @param string $type
|
2020-12-03 08:24:06 +00:00
|
|
|
* @param string[] $allow methods to allow
|
2022-10-11 18:30:25 +00:00
|
|
|
*
|
|
|
|
|
* @return MockObject&RealInstanceType
|
2019-11-08 21:24:00 +00:00
|
|
|
*/
|
2020-12-03 08:24:06 +00:00
|
|
|
protected function createNoOpAbstractMock( $type, $allow = [] ) {
|
2019-11-08 21:24:00 +00:00
|
|
|
$mock = $this->getMockBuilder( $type )
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->disableOriginalClone()
|
|
|
|
|
->disableArgumentCloning()
|
|
|
|
|
->disallowMockingUnknownTypes()
|
|
|
|
|
->getMockForAbstractClass();
|
2020-12-03 08:24:06 +00:00
|
|
|
$mock->expects( $this->never() )->method( $this->anythingBut( '__destruct', ...$allow ) );
|
2019-11-08 21:24:00 +00:00
|
|
|
return $mock;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-30 02:26:44 +00:00
|
|
|
/**
|
2021-04-02 01:18:18 +00:00
|
|
|
* Create an ObjectFactory with no dependencies and no services
|
2020-11-30 02:26:44 +00:00
|
|
|
*
|
|
|
|
|
* @return ObjectFactory
|
|
|
|
|
*/
|
|
|
|
|
protected function createSimpleObjectFactory() {
|
2021-04-02 01:18:18 +00:00
|
|
|
$serviceContainer = $this->createMock( ContainerInterface::class );
|
|
|
|
|
$serviceContainer->method( 'has' )->willReturn( false );
|
|
|
|
|
$serviceContainer->method( 'get' )->willReturnCallback(
|
|
|
|
|
static function ( $serviceName ) {
|
|
|
|
|
throw new NoSuchServiceException( $serviceName );
|
|
|
|
|
}
|
2020-11-30 02:26:44 +00:00
|
|
|
);
|
2021-04-02 01:18:18 +00:00
|
|
|
return new ObjectFactory( $serviceContainer );
|
2020-11-30 02:26:44 +00:00
|
|
|
}
|
|
|
|
|
|
2020-05-11 08:58:38 +00:00
|
|
|
/**
|
|
|
|
|
* Create an initially empty HookContainer with an empty service container
|
|
|
|
|
* attached. Register only the hooks specified in the parameter.
|
|
|
|
|
*
|
|
|
|
|
* @param callable[] $hooks
|
|
|
|
|
* @return HookContainer
|
|
|
|
|
*/
|
|
|
|
|
protected function createHookContainer( $hooks = [] ) {
|
|
|
|
|
$hookContainer = new HookContainer(
|
|
|
|
|
new \MediaWiki\HookContainer\StaticHookRegistry(),
|
2020-11-30 02:26:44 +00:00
|
|
|
$this->createSimpleObjectFactory()
|
2020-05-11 08:58:38 +00:00
|
|
|
);
|
|
|
|
|
foreach ( $hooks as $name => $callback ) {
|
|
|
|
|
$hookContainer->register( $name, $callback );
|
|
|
|
|
}
|
|
|
|
|
return $hookContainer;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-24 09:17:21 +00:00
|
|
|
/**
|
|
|
|
|
* Don't throw a warning if $function is deprecated and called later
|
|
|
|
|
*
|
|
|
|
|
* @since 1.19
|
|
|
|
|
*
|
|
|
|
|
* @param string $function
|
|
|
|
|
*/
|
|
|
|
|
public function hideDeprecated( $function ) {
|
2020-04-17 04:41:04 +00:00
|
|
|
// Construct a regex that will match the message generated by
|
|
|
|
|
// wfDeprecated() if it is called for the specified function.
|
|
|
|
|
$this->filterDeprecated( '/Use of ' . preg_quote( $function, '/' ) . ' /' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Don't throw a warning for deprecation messages matching a regex.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.35
|
|
|
|
|
*
|
|
|
|
|
* @param string $regex
|
|
|
|
|
*/
|
|
|
|
|
public function filterDeprecated( $regex ) {
|
|
|
|
|
MWDebug::filterDeprecationForTest( $regex );
|
2019-10-24 09:17:21 +00:00
|
|
|
}
|
|
|
|
|
|
2022-04-04 19:41:23 +00:00
|
|
|
/**
|
|
|
|
|
* Expect a deprecation notice, but suppress it and continue operation so we can test that the
|
|
|
|
|
* deprecated functionality works as intended for compatibility.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.39
|
|
|
|
|
*
|
|
|
|
|
* @param string $regex Deprecation message that must be triggered.
|
|
|
|
|
*/
|
|
|
|
|
public function expectDeprecationAndContinue( string $regex ): void {
|
|
|
|
|
$this->expectedDeprecations[] = $regex;
|
|
|
|
|
MWDebug::filterDeprecationForTest( $regex, function () use ( $regex ): void {
|
|
|
|
|
$this->actualDeprecations[] = $regex;
|
|
|
|
|
} );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @after
|
|
|
|
|
*/
|
|
|
|
|
public function checkExpectedDeprecationsOnTearDown(): void {
|
|
|
|
|
if ( $this->expectedDeprecations ) {
|
|
|
|
|
$this->assertSame( [],
|
|
|
|
|
array_diff( $this->expectedDeprecations, $this->actualDeprecations ),
|
|
|
|
|
'Expected deprecation warning(s) were not emitted' );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-17 15:23:17 +00:00
|
|
|
/**
|
|
|
|
|
* Check whether file contains given data.
|
|
|
|
|
* @param string $fileName
|
|
|
|
|
* @param string $actualData
|
|
|
|
|
* @param bool $createIfMissing If true, and file does not exist, create it with given data
|
|
|
|
|
* and skip the test.
|
|
|
|
|
* @param string $msg
|
|
|
|
|
* @since 1.30
|
|
|
|
|
*/
|
|
|
|
|
protected function assertFileContains(
|
|
|
|
|
$fileName,
|
|
|
|
|
$actualData,
|
|
|
|
|
$createIfMissing = false,
|
|
|
|
|
$msg = ''
|
|
|
|
|
) {
|
|
|
|
|
if ( $createIfMissing ) {
|
2022-01-03 20:47:04 +00:00
|
|
|
if ( !is_file( $fileName ) ) {
|
2019-09-17 15:23:17 +00:00
|
|
|
file_put_contents( $fileName, $actualData );
|
|
|
|
|
$this->markTestSkipped( "Data file $fileName does not exist" );
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2019-09-20 16:50:42 +00:00
|
|
|
$this->assertFileExists( $fileName );
|
2019-09-17 15:23:17 +00:00
|
|
|
}
|
2019-09-20 16:50:42 +00:00
|
|
|
$this->assertEquals( file_get_contents( $fileName ), $actualData, $msg );
|
2019-09-17 15:23:17 +00:00
|
|
|
}
|
2020-01-08 00:05:26 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Assert that two arrays are equal. By default this means that both arrays need to hold
|
|
|
|
|
* the same set of values. Using additional arguments, order and associated key can also
|
|
|
|
|
* be set as relevant.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.20
|
|
|
|
|
*
|
|
|
|
|
* @param array $expected
|
|
|
|
|
* @param array $actual
|
|
|
|
|
* @param bool $ordered If the order of the values should match
|
|
|
|
|
* @param bool $named If the keys should match
|
2021-01-03 07:39:23 +00:00
|
|
|
* @param string $message
|
|
|
|
|
* @param float $delta Deprecated in assertEquals()
|
|
|
|
|
* @param int $maxDepth Deprecated in assertEquals()
|
|
|
|
|
* @param bool $canonicalize Deprecated in assertEquals()
|
|
|
|
|
* @param bool $ignoreCase Deprecated in assertEquals()
|
2020-01-08 00:05:26 +00:00
|
|
|
*/
|
2020-10-02 20:36:23 +00:00
|
|
|
public function assertArrayEquals(
|
2021-01-03 07:39:23 +00:00
|
|
|
array $expected, array $actual, $ordered = false, $named = false, string $message = '',
|
|
|
|
|
float $delta = 0.0, int $maxDepth = 10, bool $canonicalize = false, bool $ignoreCase = false
|
2020-01-08 00:05:26 +00:00
|
|
|
) {
|
|
|
|
|
if ( !$ordered ) {
|
|
|
|
|
$this->objectAssociativeSort( $expected );
|
|
|
|
|
$this->objectAssociativeSort( $actual );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !$named ) {
|
|
|
|
|
$expected = array_values( $expected );
|
|
|
|
|
$actual = array_values( $actual );
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-03 07:39:23 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
$expected, $actual, $message,
|
|
|
|
|
// Deprecated args
|
|
|
|
|
$delta, $maxDepth, $canonicalize, $ignoreCase
|
|
|
|
|
);
|
2020-01-08 00:05:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Does an associative sort that works for objects.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.20
|
|
|
|
|
*
|
|
|
|
|
* @param array &$array
|
|
|
|
|
*/
|
|
|
|
|
protected function objectAssociativeSort( array &$array ) {
|
|
|
|
|
uasort(
|
|
|
|
|
$array,
|
2021-02-06 19:40:52 +00:00
|
|
|
static function ( $a, $b ) {
|
2020-01-08 00:05:26 +00:00
|
|
|
return serialize( $a ) <=> serialize( $b );
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-05-09 01:15:03 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @before
|
|
|
|
|
*/
|
|
|
|
|
protected function phpErrorFilterSetUp() {
|
phpunit: Fix phpErrorFilter check in TestCaseTrait to avoid PHP bug
```
function check($txt) {
echo sprintf("er=%+5s, ini=%+5s # $txt\n",
error_reporting(), intval( ini_get( 'error_reporting' ) )
);
}
function turningItOffAndOnAgain() { // have you tried...
check('... enter');
$old = error_reporting(42);
check('... off');
error_reporting($old);
check('... on');
}
error_reporting(2);
check('start'); # er=2, ini=2
turningItOffAndOnAgain();
check('toggled'); # er=2, ini=2
@turningItOffAndOnAgain();
check('silence-toggled'); # er=2, ini=0
```
PHP correctly reflects the silenced state during the silenced state,
the same as our AtEase library.
PHP also correctly ignores changse to error_reporting while being
silenced. That is, it always restores it back to how it was regardless
of what a confused nested function may have done.
Where it fails is that it doesn't seem to sync the INI setting, which
caused this PHPUnit check to cause test failures.
While this an upstream bug, it was also a mistake on my part to
write the PHPUnit check based on the ini_get value. That's not the
idiomatic way to check the current state and apart from these two
lines of code, it seems nothing in any of our Codesearch-indexed
code bases, including third party, do this.
Bug: T291278
Change-Id: Ic3e66cb2e5b2b44b9997d323abbc87b7f8fe3c41
2021-09-17 17:20:11 +00:00
|
|
|
$this->originalPhpErrorFilter = error_reporting();
|
2020-05-09 01:15:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @after
|
|
|
|
|
*/
|
|
|
|
|
protected function phpErrorFilterTearDown() {
|
phpunit: Fix phpErrorFilter check in TestCaseTrait to avoid PHP bug
```
function check($txt) {
echo sprintf("er=%+5s, ini=%+5s # $txt\n",
error_reporting(), intval( ini_get( 'error_reporting' ) )
);
}
function turningItOffAndOnAgain() { // have you tried...
check('... enter');
$old = error_reporting(42);
check('... off');
error_reporting($old);
check('... on');
}
error_reporting(2);
check('start'); # er=2, ini=2
turningItOffAndOnAgain();
check('toggled'); # er=2, ini=2
@turningItOffAndOnAgain();
check('silence-toggled'); # er=2, ini=0
```
PHP correctly reflects the silenced state during the silenced state,
the same as our AtEase library.
PHP also correctly ignores changse to error_reporting while being
silenced. That is, it always restores it back to how it was regardless
of what a confused nested function may have done.
Where it fails is that it doesn't seem to sync the INI setting, which
caused this PHPUnit check to cause test failures.
While this an upstream bug, it was also a mistake on my part to
write the PHPUnit check based on the ini_get value. That's not the
idiomatic way to check the current state and apart from these two
lines of code, it seems nothing in any of our Codesearch-indexed
code bases, including third party, do this.
Bug: T291278
Change-Id: Ic3e66cb2e5b2b44b9997d323abbc87b7f8fe3c41
2021-09-17 17:20:11 +00:00
|
|
|
$phpErrorFilter = error_reporting();
|
2020-05-09 01:15:03 +00:00
|
|
|
|
|
|
|
|
if ( $phpErrorFilter !== $this->originalPhpErrorFilter ) {
|
phpunit: Fix phpErrorFilter check in TestCaseTrait to avoid PHP bug
```
function check($txt) {
echo sprintf("er=%+5s, ini=%+5s # $txt\n",
error_reporting(), intval( ini_get( 'error_reporting' ) )
);
}
function turningItOffAndOnAgain() { // have you tried...
check('... enter');
$old = error_reporting(42);
check('... off');
error_reporting($old);
check('... on');
}
error_reporting(2);
check('start'); # er=2, ini=2
turningItOffAndOnAgain();
check('toggled'); # er=2, ini=2
@turningItOffAndOnAgain();
check('silence-toggled'); # er=2, ini=0
```
PHP correctly reflects the silenced state during the silenced state,
the same as our AtEase library.
PHP also correctly ignores changse to error_reporting while being
silenced. That is, it always restores it back to how it was regardless
of what a confused nested function may have done.
Where it fails is that it doesn't seem to sync the INI setting, which
caused this PHPUnit check to cause test failures.
While this an upstream bug, it was also a mistake on my part to
write the PHPUnit check based on the ini_get value. That's not the
idiomatic way to check the current state and apart from these two
lines of code, it seems nothing in any of our Codesearch-indexed
code bases, including third party, do this.
Bug: T291278
Change-Id: Ic3e66cb2e5b2b44b9997d323abbc87b7f8fe3c41
2021-09-17 17:20:11 +00:00
|
|
|
error_reporting( $this->originalPhpErrorFilter );
|
2020-05-09 01:15:03 +00:00
|
|
|
$message = "PHP error_reporting setting found dirty."
|
|
|
|
|
. " Did you forget AtEase::restoreWarnings?";
|
|
|
|
|
$this->fail( $message );
|
|
|
|
|
}
|
2021-02-05 21:46:52 +00:00
|
|
|
}
|
2021-02-02 00:56:11 +00:00
|
|
|
|
2021-02-05 21:46:52 +00:00
|
|
|
/**
|
|
|
|
|
* Re-enable any disabled deprecation warnings and allow same deprecations to be thrown
|
|
|
|
|
* multiple times in different tests, so the PHPUnit expectDeprecation() works.
|
2021-04-14 18:34:41 +00:00
|
|
|
*
|
2021-02-05 21:46:52 +00:00
|
|
|
* @after
|
|
|
|
|
*/
|
|
|
|
|
protected function mwDebugTearDown() {
|
2021-02-02 00:56:11 +00:00
|
|
|
MWDebug::clearLog();
|
2021-04-27 20:53:30 +00:00
|
|
|
MWDebug::clearDeprecationFilters();
|
2020-05-09 01:15:03 +00:00
|
|
|
}
|
2020-05-20 19:29:41 +00:00
|
|
|
|
2021-04-14 18:34:41 +00:00
|
|
|
/**
|
|
|
|
|
* Reset any fake timestamps so that they don't mess with any other tests.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.37 before that, integration tests had it reset in
|
|
|
|
|
* MediaWikiIntegrationTestCase::mediaWikiTearDown, and unit tests didn't at all
|
|
|
|
|
*
|
|
|
|
|
* @after
|
|
|
|
|
*/
|
|
|
|
|
protected function fakeTimestampTearDown() {
|
|
|
|
|
ConvertibleTimestamp::setFakeTime( null );
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-20 19:29:41 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $text
|
|
|
|
|
* @param array $params
|
|
|
|
|
* @return Message|MockObject
|
|
|
|
|
* @since 1.35
|
|
|
|
|
*/
|
|
|
|
|
protected function getMockMessage( $text = '', $params = [] ) {
|
|
|
|
|
/** @var MockObject $msg */
|
2022-07-14 12:42:07 +00:00
|
|
|
$msg = $this->createMock( Message::class );
|
2020-05-20 19:29:41 +00:00
|
|
|
$msg->method( 'toString' )->willReturn( $text );
|
|
|
|
|
$msg->method( '__toString' )->willReturn( $text );
|
|
|
|
|
$msg->method( 'text' )->willReturn( $text );
|
|
|
|
|
$msg->method( 'parse' )->willReturn( $text );
|
|
|
|
|
$msg->method( 'plain' )->willReturn( $text );
|
|
|
|
|
$msg->method( 'parseAsBlock' )->willReturn( $text );
|
|
|
|
|
$msg->method( 'escaped' )->willReturn( $text );
|
|
|
|
|
$msg->method( 'title' )->willReturn( $msg );
|
|
|
|
|
$msg->method( 'getKey' )->willReturn( $text );
|
|
|
|
|
$msg->method( 'params' )->willReturn( $msg );
|
|
|
|
|
$msg->method( 'getParams' )->willReturn( $params );
|
|
|
|
|
$msg->method( 'rawParams' )->willReturn( $msg );
|
2021-02-17 21:11:16 +00:00
|
|
|
$msg->method( 'numParams' )->willReturn( $msg );
|
2020-05-20 19:29:41 +00:00
|
|
|
$msg->method( 'inLanguage' )->willReturn( $msg );
|
|
|
|
|
$msg->method( 'inContentLanguage' )->willReturn( $msg );
|
|
|
|
|
$msg->method( 'useDatabase' )->willReturn( $msg );
|
|
|
|
|
$msg->method( 'setContext' )->willReturn( $msg );
|
|
|
|
|
$msg->method( 'exists' )->willReturn( true );
|
|
|
|
|
return $msg;
|
|
|
|
|
}
|
2022-03-04 22:00:02 +00:00
|
|
|
|
|
|
|
|
private function failStatus( StatusValue $status, $reason, $message = '' ) {
|
|
|
|
|
$reason = $message === '' ? $reason : "$message\n$reason";
|
|
|
|
|
$this->fail( "$reason\n$status" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function assertStatusOK( StatusValue $status, $message = '' ) {
|
|
|
|
|
if ( !$status->isOK() ) {
|
2022-06-03 11:44:11 +00:00
|
|
|
$errors = $status->splitByErrorType()[0];
|
|
|
|
|
$this->failStatus( $errors, 'Status should be OK', $message );
|
2022-03-04 22:00:02 +00:00
|
|
|
} else {
|
|
|
|
|
$this->addToAssertionCount( 1 );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function assertStatusGood( StatusValue $status, $message = '' ) {
|
|
|
|
|
if ( !$status->isGood() ) {
|
|
|
|
|
$this->failStatus( $status, 'Status should be Good', $message );
|
|
|
|
|
} else {
|
|
|
|
|
$this->addToAssertionCount( 1 );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function assertStatusNotOK( StatusValue $status, $message = '' ) {
|
|
|
|
|
if ( $status->isOK() ) {
|
|
|
|
|
$this->failStatus( $status, 'Status should not be OK', $message );
|
|
|
|
|
} else {
|
|
|
|
|
$this->addToAssertionCount( 1 );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function assertStatusNotGood( StatusValue $status, $message = '' ) {
|
|
|
|
|
if ( $status->isGood() ) {
|
|
|
|
|
$this->failStatus( $status, 'Status should not be Good', $message );
|
|
|
|
|
} else {
|
|
|
|
|
$this->addToAssertionCount( 1 );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function assertStatusMessage( $messageKey, StatusValue $status, $message = '' ) {
|
|
|
|
|
if ( !$status->hasMessage( $messageKey ) ) {
|
|
|
|
|
$this->failStatus( $status, "Status should have message $messageKey", $message );
|
|
|
|
|
} else {
|
|
|
|
|
$this->addToAssertionCount( 1 );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function assertStatusValue( $expected, StatusValue $status, $message = 'Status value' ) {
|
|
|
|
|
$this->assertEquals( $expected, $status->getValue(), $message );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function assertStatusError( $messageKey, StatusValue $status, $message = '' ) {
|
|
|
|
|
$this->assertStatusNotOK( $status, $message );
|
|
|
|
|
$this->assertStatusMessage( $messageKey, $status, $message );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function assertStatusWarning( $messageKey, StatusValue $status, $message = '' ) {
|
|
|
|
|
$this->assertStatusNotGood( $status, $message );
|
|
|
|
|
$this->assertStatusOK( $status, $message );
|
|
|
|
|
$this->assertStatusMessage( $messageKey, $status, $message );
|
|
|
|
|
}
|
2022-06-13 15:25:23 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Put each HTML element on its own line and then equals() the results
|
|
|
|
|
*
|
|
|
|
|
* Use for nicely formatting of PHPUnit diff output when comparing very
|
|
|
|
|
* simple HTML
|
|
|
|
|
*
|
|
|
|
|
* @since 1.20
|
|
|
|
|
* @since 1.39 available in MediaWikiUnitTestCase
|
|
|
|
|
*
|
|
|
|
|
* @param string $expected HTML on oneline
|
|
|
|
|
* @param string $actual HTML on oneline
|
|
|
|
|
* @param string $msg Optional message
|
|
|
|
|
*/
|
|
|
|
|
protected function assertHTMLEquals( $expected, $actual, $msg = '' ) {
|
|
|
|
|
$expected = str_replace( '>', ">\n", $expected );
|
|
|
|
|
$actual = str_replace( '>', ">\n", $actual );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( $expected, $actual, $msg );
|
|
|
|
|
}
|
2019-08-18 18:19:05 +00:00
|
|
|
}
|