2023-10-05 17:39:46 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Wikimedia\Tests\Unit;
|
|
|
|
|
|
2024-04-02 23:32:58 +00:00
|
|
|
use MediaWiki\Message\Message;
|
2023-10-05 17:39:46 +00:00
|
|
|
use MediaWikiUnitTestCase;
|
|
|
|
|
use StatusValue;
|
|
|
|
|
|
2024-05-28 21:14:33 +00:00
|
|
|
/**
|
|
|
|
|
* @covers \StatusValue
|
|
|
|
|
*/
|
2023-10-05 17:39:46 +00:00
|
|
|
class StatusValueTest extends MediaWikiUnitTestCase {
|
|
|
|
|
|
|
|
|
|
public function provideToString() {
|
|
|
|
|
yield [
|
|
|
|
|
true, null, null,
|
|
|
|
|
'<OK, no errors detected, no value set>',
|
|
|
|
|
'Empty, good state'
|
|
|
|
|
];
|
|
|
|
|
yield [
|
|
|
|
|
false, null, null,
|
|
|
|
|
'<Error, no errors detected, no value set>',
|
|
|
|
|
'Empty, error state'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
yield [
|
|
|
|
|
true, 'TestValue', null,
|
|
|
|
|
'<OK, no errors detected, string value set>',
|
|
|
|
|
'Simple string, good state'
|
|
|
|
|
];
|
|
|
|
|
yield [
|
|
|
|
|
false, 'TestValue', null,
|
|
|
|
|
'<Error, no errors detected, string value set>',
|
|
|
|
|
'Simple string, error state'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
yield [
|
2024-04-19 22:25:15 +00:00
|
|
|
true, 42, null,
|
2024-07-31 16:56:55 +00:00
|
|
|
'<OK, no errors detected, int value set>',
|
2024-04-19 22:25:15 +00:00
|
|
|
'Simple int, good state'
|
2023-10-05 17:39:46 +00:00
|
|
|
];
|
|
|
|
|
yield [
|
2024-04-19 22:25:15 +00:00
|
|
|
false, 42, null,
|
2024-07-31 16:56:55 +00:00
|
|
|
'<Error, no errors detected, int value set>',
|
2024-04-19 22:25:15 +00:00
|
|
|
'Simple int, error state'
|
2023-10-05 17:39:46 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
yield [
|
2024-04-19 22:25:15 +00:00
|
|
|
true, [ 'TestValue' => false ], null,
|
|
|
|
|
'<OK, no errors detected, array value set>',
|
|
|
|
|
'Simple array, good state'
|
2023-10-05 17:39:46 +00:00
|
|
|
];
|
|
|
|
|
yield [
|
2024-04-19 22:25:15 +00:00
|
|
|
false, [ 'TestValue' => false ], null,
|
|
|
|
|
'<Error, no errors detected, array value set>',
|
|
|
|
|
'Simple array, error state'
|
2023-10-05 17:39:46 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$basicErrorReport = "\n"
|
|
|
|
|
. "+----------+---------------------------+--------------------------------------+\n"
|
|
|
|
|
. "| error | This is the error | |\n"
|
|
|
|
|
. "+----------+---------------------------+--------------------------------------+\n";
|
|
|
|
|
|
|
|
|
|
yield [
|
2024-04-19 22:25:15 +00:00
|
|
|
true, null, [ 'This is the error' ],
|
|
|
|
|
'<OK, collected 1 message(s) on the way, no value set>' . $basicErrorReport,
|
|
|
|
|
'Empty, string error, good state'
|
2023-10-05 17:39:46 +00:00
|
|
|
];
|
|
|
|
|
yield [
|
2024-04-19 22:25:15 +00:00
|
|
|
false, null, [ 'This is the error' ],
|
|
|
|
|
'<Error, collected 1 message(s) on the way, no value set>' . $basicErrorReport,
|
|
|
|
|
'Empty, string error, error state'
|
2023-10-05 17:39:46 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
yield [
|
2024-04-19 22:25:15 +00:00
|
|
|
false, 'TestValue', [ 'This is the error' ],
|
|
|
|
|
'<Error, collected 1 message(s) on the way, string value set>' . $basicErrorReport,
|
|
|
|
|
'Simple string, string error, error state'
|
2023-10-05 17:39:46 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
yield [
|
|
|
|
|
false, 42, [ 'This is the error' ],
|
2024-07-31 16:56:55 +00:00
|
|
|
'<Error, collected 1 message(s) on the way, int value set>' . $basicErrorReport,
|
2023-10-05 17:39:46 +00:00
|
|
|
'Simple int, string error, error state'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
yield [
|
|
|
|
|
false, [ 'TestValue' => false ], [ 'This is the error' ],
|
|
|
|
|
'<Error, collected 1 message(s) on the way, array value set>' . $basicErrorReport,
|
|
|
|
|
'Simple array, string error, error state'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
yield [
|
|
|
|
|
false, null, [ [ 'message' => 'This is the error', 'params' => false ] ],
|
|
|
|
|
'<Error, collected 1 message(s) on the way, no value set>' . $basicErrorReport,
|
|
|
|
|
'Error with false value shows as no parameter'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$specialErrorReport = "\n"
|
|
|
|
|
. "+----------+---------------------------+--------------------------------------+\n"
|
|
|
|
|
. "| error | This is the error | 1 |\n"
|
|
|
|
|
. "+----------+---------------------------+--------------------------------------+\n";
|
|
|
|
|
yield [
|
|
|
|
|
false, null, [ [ 'message' => 'This is the error', 'params' => true ] ],
|
|
|
|
|
'<Error, collected 1 message(s) on the way, no value set>' . $specialErrorReport,
|
|
|
|
|
'Error with true value shows as 1 int'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$specialErrorReport = "\n"
|
|
|
|
|
. "+----------+---------------------------+--------------------------------------+\n"
|
|
|
|
|
. "| error | This is the error | 0 |\n"
|
|
|
|
|
. "+----------+---------------------------+--------------------------------------+\n";
|
|
|
|
|
yield [
|
|
|
|
|
false, null, [ [ 'message' => 'This is the error', 'params' => 0 ] ],
|
|
|
|
|
'<Error, collected 1 message(s) on the way, no value set>' . $specialErrorReport,
|
|
|
|
|
'Error with 0 int value'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$specialErrorReport = "\n"
|
|
|
|
|
. "+----------+---------------------------+--------------------------------------+\n"
|
|
|
|
|
. "| error | This is the error | 42 |\n"
|
|
|
|
|
. "+----------+---------------------------+--------------------------------------+\n";
|
|
|
|
|
yield [
|
|
|
|
|
false, null, [ [ 'message' => 'This is the error', 'params' => 42 ] ],
|
|
|
|
|
'<Error, collected 1 message(s) on the way, no value set>' . $specialErrorReport,
|
|
|
|
|
'Error with 42 int value'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$specialErrorReport = "\n"
|
|
|
|
|
. "+----------+---------------------------+--------------------------------------+\n"
|
|
|
|
|
. "| error | This is the error | TestValue |\n"
|
|
|
|
|
. "+----------+---------------------------+--------------------------------------+\n";
|
|
|
|
|
yield [
|
|
|
|
|
false, null, [ [ 'message' => 'This is the error', 'params' => 'TestValue' ] ],
|
|
|
|
|
'<Error, collected 1 message(s) on the way, no value set>' . $specialErrorReport,
|
|
|
|
|
'Error with a string parameter'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$specialErrorReport = "\n"
|
|
|
|
|
. "+----------+---------------------------+--------------------------------------+\n"
|
|
|
|
|
. "| error | This is the error | [ TestValue, 42, 1, [ foo, baz ] ] |\n"
|
|
|
|
|
. "+----------+---------------------------+--------------------------------------+\n";
|
|
|
|
|
yield [
|
|
|
|
|
false, null, [
|
|
|
|
|
[ 'message' => 'This is the error', 'params' => [ 'TestValue', 42, true, [ 'foo', 'bar' => 'baz' ] ] ]
|
|
|
|
|
],
|
|
|
|
|
'<Error, collected 1 message(s) on the way, no value set>' . $specialErrorReport,
|
|
|
|
|
'Error with an array of parameters'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$multiErrorReport = "\n"
|
|
|
|
|
. "+----------+---------------------------+--------------------------------------+\n"
|
|
|
|
|
. "| error | Basic string parsing | Naïve string parsing |\n"
|
|
|
|
|
. "| error | Wrapped string | This is a longer input parameter and |\n"
|
|
|
|
|
. "| | | thus will wrap |\n"
|
2023-09-30 02:53:30 +00:00
|
|
|
. "| error | Multi-byte string | 캐나다∂는 북미에 있는 나라로 면적이 매우 넓습니다. |\n"
|
|
|
|
|
. "| error | Multi-byte wrapped string | 캐나다는 태평양에서 대서양까지, 북쪽으로는 북극과 접해 있는 북미 |\n"
|
|
|
|
|
. "| | | 의 큰 나라입니다. |\n"
|
2023-10-05 17:39:46 +00:00
|
|
|
. "+----------+---------------------------+--------------------------------------+\n";
|
|
|
|
|
yield [
|
|
|
|
|
false, null, [
|
|
|
|
|
[ 'message' => 'Basic string parsing', 'params' => 'Naïve string parsing' ],
|
|
|
|
|
[ 'message' => 'Wrapped string', 'params' => 'This is a longer input parameter and thus will wrap' ],
|
2023-09-30 02:53:30 +00:00
|
|
|
[ 'message' => 'Multi-byte string', 'params' => '캐나다∂는 북미에 있는 나라로 면적이 매우 넓습니다.' ],
|
|
|
|
|
[ 'message' => 'Multi-byte wrapped string', 'params' => '캐나다는 태평양에서 대서양까지, 북쪽으로는 북극과 접해 있는 북미의 큰 나라입니다.' ]
|
2023-10-05 17:39:46 +00:00
|
|
|
],
|
2023-09-30 02:53:30 +00:00
|
|
|
'<Error, collected 4 message(s) on the way, no value set>' . $multiErrorReport,
|
2023-10-05 17:39:46 +00:00
|
|
|
'Three errors with different kinds of string parameters including long strings that are split when simple'
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideToString
|
|
|
|
|
*/
|
|
|
|
|
public function testToString( bool $sucess, $message, $errors, string $expected, string $testExplanation ) {
|
|
|
|
|
$status = StatusValue::newGood();
|
|
|
|
|
|
|
|
|
|
$status->setResult( $sucess, $message );
|
|
|
|
|
|
|
|
|
|
if ( isset( $errors ) ) {
|
|
|
|
|
foreach ( $errors as $key => $error ) {
|
|
|
|
|
if ( is_string( $error ) ) {
|
|
|
|
|
$status->error( $error );
|
|
|
|
|
} else {
|
|
|
|
|
$status->error( $error['message'], $error['params'] );
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( $expected, $status->__toString(), $testExplanation );
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-02 23:32:58 +00:00
|
|
|
public function testGetErrorsByType() {
|
|
|
|
|
$status = new StatusValue();
|
|
|
|
|
$warning = new Message( 'warning111' );
|
|
|
|
|
$error = new Message( 'error111' );
|
|
|
|
|
$status->warning( $warning );
|
|
|
|
|
$status->error( $error );
|
|
|
|
|
|
|
|
|
|
$this->assertCount( 2, $status->getErrors() );
|
|
|
|
|
$this->assertCount( 1, $status->getErrorsByType( 'warning' ) );
|
|
|
|
|
$this->assertCount( 1, $status->getErrorsByType( 'error' ) );
|
|
|
|
|
$this->assertEquals( $warning, $status->getErrorsByType( 'warning' )[0]['message'] );
|
|
|
|
|
$this->assertEquals( $error, $status->getErrorsByType( 'error' )[0]['message'] );
|
|
|
|
|
|
|
|
|
|
$this->assertCount( 2, $status->getMessages() );
|
|
|
|
|
$this->assertCount( 1, $status->getMessages( 'warning' ) );
|
|
|
|
|
$this->assertCount( 1, $status->getMessages( 'error' ) );
|
|
|
|
|
$this->assertEquals( 'warning111', $status->getMessages( 'warning' )[0]->getKey() );
|
|
|
|
|
$this->assertEquals( 'error111', $status->getMessages( 'error' )[0]->getKey() );
|
|
|
|
|
}
|
2023-10-05 17:39:46 +00:00
|
|
|
}
|