2013-10-24 22:11:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
2016-01-27 09:59:31 +00:00
|
|
|
* @author Addshore
|
2013-10-24 22:11:15 +00:00
|
|
|
*/
|
2013-11-25 20:19:23 +00:00
|
|
|
class StatusTest extends MediaWikiLangTestCase {
|
2013-10-24 22:11:15 +00:00
|
|
|
|
2013-11-19 18:03:54 +00:00
|
|
|
public function testCanConstruct() {
|
2013-10-24 22:11:15 +00:00
|
|
|
new Status();
|
|
|
|
|
$this->assertTrue( true );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideValues
|
|
|
|
|
* @covers Status::newGood
|
|
|
|
|
*/
|
2013-11-19 18:03:54 +00:00
|
|
|
public function testNewGood( $value = null ) {
|
2013-10-24 22:11:15 +00:00
|
|
|
$status = Status::newGood( $value );
|
|
|
|
|
$this->assertTrue( $status->isGood() );
|
|
|
|
|
$this->assertTrue( $status->isOK() );
|
|
|
|
|
$this->assertEquals( $value, $status->getValue() );
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-19 18:03:54 +00:00
|
|
|
public static function provideValues() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[],
|
|
|
|
|
[ 'foo' ],
|
|
|
|
|
[ [ 'foo' => 'bar' ] ],
|
|
|
|
|
[ new Exception() ],
|
|
|
|
|
[ 1234 ],
|
|
|
|
|
];
|
2013-10-24 22:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-10-25 01:12:47 +00:00
|
|
|
* @covers Status::newFatal
|
2013-10-24 22:11:15 +00:00
|
|
|
*/
|
|
|
|
|
public function testNewFatalWithMessage() {
|
|
|
|
|
$message = $this->getMockBuilder( 'Message' )
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
|
|
$status = Status::newFatal( $message );
|
|
|
|
|
$this->assertFalse( $status->isGood() );
|
|
|
|
|
$this->assertFalse( $status->isOK() );
|
|
|
|
|
$this->assertEquals( $message, $status->getMessage() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-10-25 01:12:47 +00:00
|
|
|
* @covers Status::newFatal
|
2013-10-24 22:11:15 +00:00
|
|
|
*/
|
|
|
|
|
public function testNewFatalWithString() {
|
|
|
|
|
$message = 'foo';
|
|
|
|
|
$status = Status::newFatal( $message );
|
|
|
|
|
$this->assertFalse( $status->isGood() );
|
|
|
|
|
$this->assertFalse( $status->isOK() );
|
2013-11-15 15:43:42 +00:00
|
|
|
$this->assertEquals( $message, $status->getMessage()->getKey() );
|
2013-10-24 22:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
2015-02-13 17:29:58 +00:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public function testOkAndErrors() {
|
|
|
|
|
$status = Status::newGood( 'foo' );
|
|
|
|
|
$this->assertTrue( $status->ok );
|
|
|
|
|
$status = Status::newFatal( 'foo', 1, 2 );
|
|
|
|
|
$this->assertFalse( $status->ok );
|
2015-09-26 13:09:05 +00:00
|
|
|
$this->assertArrayEquals(
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
|
|
|
|
[
|
2015-09-26 13:09:05 +00:00
|
|
|
'type' => 'error',
|
|
|
|
|
'message' => 'foo',
|
2016-02-17 09:09:32 +00:00
|
|
|
'params' => [ 1, 2 ]
|
|
|
|
|
]
|
|
|
|
|
],
|
2015-09-26 13:09:05 +00:00
|
|
|
$status->errors
|
|
|
|
|
);
|
2015-02-13 17:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
2013-10-24 22:11:15 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideSetResult
|
2013-11-15 15:43:42 +00:00
|
|
|
* @covers Status::setResult
|
2013-10-24 22:11:15 +00:00
|
|
|
*/
|
|
|
|
|
public function testSetResult( $ok, $value = null ) {
|
|
|
|
|
$status = new Status();
|
|
|
|
|
$status->setResult( $ok, $value );
|
|
|
|
|
$this->assertEquals( $ok, $status->isOK() );
|
|
|
|
|
$this->assertEquals( $value, $status->getValue() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideSetResult() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ true ],
|
|
|
|
|
[ false ],
|
|
|
|
|
[ true, 'value' ],
|
|
|
|
|
[ false, 'value' ],
|
|
|
|
|
];
|
2013-10-24 22:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
2013-11-15 15:43:42 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideIsOk
|
|
|
|
|
* @covers Status::isOk
|
|
|
|
|
*/
|
|
|
|
|
public function testIsOk( $ok ) {
|
|
|
|
|
$status = new Status();
|
|
|
|
|
$status->ok = $ok;
|
|
|
|
|
$this->assertEquals( $ok, $status->isOK() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideIsOk() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ true ],
|
|
|
|
|
[ false ],
|
|
|
|
|
];
|
2013-11-15 15:43:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers Status::getValue
|
|
|
|
|
*/
|
|
|
|
|
public function testGetValue() {
|
|
|
|
|
$status = new Status();
|
|
|
|
|
$status->value = 'foobar';
|
|
|
|
|
$this->assertEquals( 'foobar', $status->getValue() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideIsGood
|
|
|
|
|
* @covers Status::isGood
|
|
|
|
|
*/
|
|
|
|
|
public function testIsGood( $ok, $errors, $expected ) {
|
|
|
|
|
$status = new Status();
|
|
|
|
|
$status->ok = $ok;
|
2015-01-21 01:23:21 +00:00
|
|
|
foreach ( $errors as $error ) {
|
|
|
|
|
$status->warning( $error );
|
|
|
|
|
}
|
2013-11-15 15:43:42 +00:00
|
|
|
$this->assertEquals( $expected, $status->isGood() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideIsGood() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ true, [], true ],
|
|
|
|
|
[ true, [ 'foo' ], false ],
|
|
|
|
|
[ false, [], false ],
|
|
|
|
|
[ false, [ 'foo' ], false ],
|
|
|
|
|
];
|
2013-11-15 15:43:42 +00:00
|
|
|
}
|
|
|
|
|
|
2013-10-24 22:11:15 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideMockMessageDetails
|
|
|
|
|
* @covers Status::warning
|
|
|
|
|
* @covers Status::getWarningsArray
|
2013-11-19 18:12:16 +00:00
|
|
|
* @covers Status::getStatusArray
|
2013-10-24 22:11:15 +00:00
|
|
|
*/
|
|
|
|
|
public function testWarningWithMessage( $mockDetails ) {
|
|
|
|
|
$status = new Status();
|
|
|
|
|
$messages = $this->getMockMessages( $mockDetails );
|
|
|
|
|
|
2013-11-19 18:03:54 +00:00
|
|
|
foreach ( $messages as $message ) {
|
2013-10-24 22:11:15 +00:00
|
|
|
$status->warning( $message );
|
|
|
|
|
}
|
|
|
|
|
$warnings = $status->getWarningsArray();
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( count( $messages ), count( $warnings ) );
|
2013-11-19 18:03:54 +00:00
|
|
|
foreach ( $messages as $key => $message ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$expectedArray = array_merge( [ $message->getKey() ], $message->getParams() );
|
2013-10-24 22:11:15 +00:00
|
|
|
$this->assertEquals( $warnings[$key], $expectedArray );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideMockMessageDetails
|
|
|
|
|
* @covers Status::error
|
|
|
|
|
* @covers Status::getErrorsArray
|
2013-11-19 18:12:16 +00:00
|
|
|
* @covers Status::getStatusArray
|
2013-10-24 22:11:15 +00:00
|
|
|
*/
|
|
|
|
|
public function testErrorWithMessage( $mockDetails ) {
|
|
|
|
|
$status = new Status();
|
|
|
|
|
$messages = $this->getMockMessages( $mockDetails );
|
|
|
|
|
|
2013-11-19 18:03:54 +00:00
|
|
|
foreach ( $messages as $message ) {
|
2013-10-24 22:11:15 +00:00
|
|
|
$status->error( $message );
|
|
|
|
|
}
|
|
|
|
|
$errors = $status->getErrorsArray();
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( count( $messages ), count( $errors ) );
|
2013-11-19 18:03:54 +00:00
|
|
|
foreach ( $messages as $key => $message ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$expectedArray = array_merge( [ $message->getKey() ], $message->getParams() );
|
2013-10-24 22:11:15 +00:00
|
|
|
$this->assertEquals( $errors[$key], $expectedArray );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-25 15:11:57 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideMockMessageDetails
|
|
|
|
|
* @covers Status::fatal
|
|
|
|
|
* @covers Status::getErrorsArray
|
|
|
|
|
* @covers Status::getStatusArray
|
|
|
|
|
*/
|
|
|
|
|
public function testFatalWithMessage( $mockDetails ) {
|
|
|
|
|
$status = new Status();
|
|
|
|
|
$messages = $this->getMockMessages( $mockDetails );
|
|
|
|
|
|
|
|
|
|
foreach ( $messages as $message ) {
|
|
|
|
|
$status->fatal( $message );
|
|
|
|
|
}
|
|
|
|
|
$errors = $status->getErrorsArray();
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( count( $messages ), count( $errors ) );
|
|
|
|
|
foreach ( $messages as $key => $message ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$expectedArray = array_merge( [ $message->getKey() ], $message->getParams() );
|
2014-01-25 15:11:57 +00:00
|
|
|
$this->assertEquals( $errors[$key], $expectedArray );
|
|
|
|
|
}
|
|
|
|
|
$this->assertFalse( $status->isOK() );
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
protected function getMockMessage( $key = 'key', $params = [] ) {
|
2013-10-24 22:11:15 +00:00
|
|
|
$message = $this->getMockBuilder( 'Message' )
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
|
|
|
|
$message->expects( $this->atLeastOnce() )
|
|
|
|
|
->method( 'getKey' )
|
|
|
|
|
->will( $this->returnValue( $key ) );
|
|
|
|
|
$message->expects( $this->atLeastOnce() )
|
|
|
|
|
->method( 'getParams' )
|
|
|
|
|
->will( $this->returnValue( $params ) );
|
|
|
|
|
return $message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-07-24 12:55:43 +00:00
|
|
|
* @param array $messageDetails E.g. array( 'KEY' => array(/PARAMS/) )
|
2013-10-24 22:11:15 +00:00
|
|
|
* @return Message[]
|
|
|
|
|
*/
|
2013-11-19 18:03:54 +00:00
|
|
|
protected function getMockMessages( $messageDetails ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$messages = [];
|
2013-11-19 18:03:54 +00:00
|
|
|
foreach ( $messageDetails as $key => $paramsArray ) {
|
2013-10-24 22:11:15 +00:00
|
|
|
$messages[] = $this->getMockMessage( $key, $paramsArray );
|
|
|
|
|
}
|
|
|
|
|
return $messages;
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-19 18:03:54 +00:00
|
|
|
public static function provideMockMessageDetails() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ [ 'key1' => [ 'foo' => 'bar' ] ] ],
|
|
|
|
|
[ [ 'key1' => [ 'foo' => 'bar' ], 'key2' => [ 'foo2' => 'bar2' ] ] ],
|
|
|
|
|
];
|
2013-10-24 22:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers Status::merge
|
|
|
|
|
*/
|
2013-11-19 18:03:54 +00:00
|
|
|
public function testMerge() {
|
2013-10-24 22:11:15 +00:00
|
|
|
$status1 = new Status();
|
|
|
|
|
$status2 = new Status();
|
|
|
|
|
$message1 = $this->getMockMessage( 'warn1' );
|
|
|
|
|
$message2 = $this->getMockMessage( 'error2' );
|
|
|
|
|
$status1->warning( $message1 );
|
|
|
|
|
$status2->error( $message2 );
|
|
|
|
|
|
|
|
|
|
$status1->merge( $status2 );
|
2014-04-24 12:50:36 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
2,
|
|
|
|
|
count( $status1->getWarningsArray() ) + count( $status1->getErrorsArray() )
|
|
|
|
|
);
|
2013-10-24 22:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
2014-01-25 15:08:49 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Status::merge
|
|
|
|
|
*/
|
|
|
|
|
public function testMergeWithOverwriteValue() {
|
|
|
|
|
$status1 = new Status();
|
|
|
|
|
$status2 = new Status();
|
|
|
|
|
$message1 = $this->getMockMessage( 'warn1' );
|
|
|
|
|
$message2 = $this->getMockMessage( 'error2' );
|
|
|
|
|
$status1->warning( $message1 );
|
|
|
|
|
$status2->error( $message2 );
|
|
|
|
|
$status2->value = 'FooValue';
|
|
|
|
|
|
|
|
|
|
$status1->merge( $status2, true );
|
2014-04-24 12:50:36 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
2,
|
|
|
|
|
count( $status1->getWarningsArray() ) + count( $status1->getErrorsArray() )
|
|
|
|
|
);
|
2014-01-25 15:08:49 +00:00
|
|
|
$this->assertEquals( 'FooValue', $status1->getValue() );
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-26 00:52:46 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Status::hasMessage
|
|
|
|
|
*/
|
|
|
|
|
public function testHasMessage() {
|
|
|
|
|
$status = new Status();
|
|
|
|
|
$status->fatal( 'bad' );
|
2014-04-16 19:52:32 +00:00
|
|
|
$status->fatal( wfMessage( 'bad-msg' ) );
|
2013-10-26 00:52:46 +00:00
|
|
|
$this->assertTrue( $status->hasMessage( 'bad' ) );
|
2014-04-16 19:52:32 +00:00
|
|
|
$this->assertTrue( $status->hasMessage( 'bad-msg' ) );
|
|
|
|
|
$this->assertTrue( $status->hasMessage( wfMessage( 'bad-msg' ) ) );
|
2013-10-26 00:52:46 +00:00
|
|
|
$this->assertFalse( $status->hasMessage( 'good' ) );
|
2013-11-15 15:43:42 +00:00
|
|
|
}
|
2013-10-26 00:52:46 +00:00
|
|
|
|
2013-11-15 15:43:42 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideCleanParams
|
|
|
|
|
* @covers Status::cleanParams
|
|
|
|
|
*/
|
|
|
|
|
public function testCleanParams( $cleanCallback, $params, $expected ) {
|
|
|
|
|
$method = new ReflectionMethod( 'Status', 'cleanParams' );
|
2013-11-21 17:19:44 +00:00
|
|
|
$method->setAccessible( true );
|
2013-11-15 15:43:42 +00:00
|
|
|
$status = new Status();
|
|
|
|
|
$status->cleanCallback = $cleanCallback;
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( $expected, $method->invoke( $status, $params ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideCleanParams() {
|
2014-07-20 19:41:41 +00:00
|
|
|
$cleanCallback = function ( $value ) {
|
2014-01-25 15:14:49 +00:00
|
|
|
return '-' . $value . '-';
|
|
|
|
|
};
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ false, [ 'foo' => 'bar' ], [ 'foo' => 'bar' ] ],
|
|
|
|
|
[ $cleanCallback, [ 'foo' => 'bar' ], [ 'foo' => '-bar-' ] ],
|
|
|
|
|
];
|
2013-11-15 15:43:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-11-15 22:42:59 +00:00
|
|
|
* @dataProvider provideGetWikiTextAndHtml
|
2013-11-15 15:43:42 +00:00
|
|
|
* @covers Status::getWikiText
|
|
|
|
|
* @todo test long and short context messages generated through this method
|
|
|
|
|
* this can not really be done now due to use of wfMessage()->plain()
|
|
|
|
|
* It is possible to mock such methods but only if namespaces are used
|
|
|
|
|
*/
|
2013-12-06 17:53:18 +00:00
|
|
|
public function testGetWikiText( Status $status, $wikitext, $html ) {
|
|
|
|
|
$this->assertEquals( $wikitext, $status->getWikiText() );
|
2013-11-15 15:43:42 +00:00
|
|
|
}
|
|
|
|
|
|
2013-11-15 22:42:59 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideGetWikiTextAndHtml
|
|
|
|
|
* @covers Status::getHtml
|
|
|
|
|
* @todo test long and short context messages generated through this method
|
2014-04-24 12:50:36 +00:00
|
|
|
* this can not really be done now due to use of $this->getWikiText using
|
|
|
|
|
* wfMessage()->plain(). It is possible to mock such methods but only if
|
|
|
|
|
* namespaces are used.
|
2013-11-15 22:42:59 +00:00
|
|
|
*/
|
2013-12-15 19:56:09 +00:00
|
|
|
public function testGetHtml( Status $status, $wikitext, $html ) {
|
2013-12-06 17:53:18 +00:00
|
|
|
$this->assertEquals( $html, $status->getHTML() );
|
2013-11-15 22:42:59 +00:00
|
|
|
}
|
|
|
|
|
|
2013-11-15 15:43:42 +00:00
|
|
|
/**
|
2014-08-13 17:54:49 +00:00
|
|
|
* @return array Array of arrays with values;
|
2013-11-15 15:43:42 +00:00
|
|
|
* 0 => status object
|
|
|
|
|
* 1 => expected string (with no context)
|
|
|
|
|
*/
|
2013-11-15 22:42:59 +00:00
|
|
|
public static function provideGetWikiTextAndHtml() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$testCases = [];
|
2013-11-15 15:43:42 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$testCases['GoodStatus'] = [
|
2013-11-15 15:43:42 +00:00
|
|
|
new Status(),
|
|
|
|
|
"Internal error: Status::getWikiText called for a good result, this is incorrect\n",
|
2013-12-06 17:53:18 +00:00
|
|
|
"<p>Internal error: Status::getWikiText called for a good result, this is incorrect\n</p>",
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2013-11-15 15:43:42 +00:00
|
|
|
|
|
|
|
|
$status = new Status();
|
|
|
|
|
$status->ok = false;
|
2016-02-17 09:09:32 +00:00
|
|
|
$testCases['GoodButNoError'] = [
|
2013-11-15 15:43:42 +00:00
|
|
|
$status,
|
|
|
|
|
"Internal error: Status::getWikiText: Invalid result object: no error text but not OK\n",
|
2013-12-06 17:53:18 +00:00
|
|
|
"<p>Internal error: Status::getWikiText: Invalid result object: no error text but not OK\n</p>",
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2013-11-15 15:43:42 +00:00
|
|
|
|
|
|
|
|
$status = new Status();
|
|
|
|
|
$status->warning( 'fooBar!' );
|
2016-02-17 09:09:32 +00:00
|
|
|
$testCases['1StringWarning'] = [
|
2013-11-15 15:43:42 +00:00
|
|
|
$status,
|
|
|
|
|
"<fooBar!>",
|
2013-12-06 17:53:18 +00:00
|
|
|
"<p><fooBar!>\n</p>",
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2013-11-15 15:43:42 +00:00
|
|
|
|
|
|
|
|
$status = new Status();
|
|
|
|
|
$status->warning( 'fooBar!' );
|
|
|
|
|
$status->warning( 'fooBar2!' );
|
2016-02-17 09:09:32 +00:00
|
|
|
$testCases['2StringWarnings'] = [
|
2013-11-15 15:43:42 +00:00
|
|
|
$status,
|
|
|
|
|
"* <fooBar!>\n* <fooBar2!>\n",
|
2014-05-20 16:14:21 +00:00
|
|
|
"<ul><li> <fooBar!></li>\n<li> <fooBar2!></li></ul>\n",
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2013-11-15 15:43:42 +00:00
|
|
|
|
|
|
|
|
$status = new Status();
|
2016-02-17 09:09:32 +00:00
|
|
|
$status->warning( new Message( 'fooBar!', [ 'foo', 'bar' ] ) );
|
|
|
|
|
$testCases['1MessageWarning'] = [
|
2013-11-15 15:43:42 +00:00
|
|
|
$status,
|
|
|
|
|
"<fooBar!>",
|
2013-12-06 17:53:18 +00:00
|
|
|
"<p><fooBar!>\n</p>",
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2013-11-15 15:43:42 +00:00
|
|
|
|
|
|
|
|
$status = new Status();
|
2016-02-17 09:09:32 +00:00
|
|
|
$status->warning( new Message( 'fooBar!', [ 'foo', 'bar' ] ) );
|
2013-11-15 15:43:42 +00:00
|
|
|
$status->warning( new Message( 'fooBar2!' ) );
|
2016-02-17 09:09:32 +00:00
|
|
|
$testCases['2MessageWarnings'] = [
|
2013-11-15 15:43:42 +00:00
|
|
|
$status,
|
|
|
|
|
"* <fooBar!>\n* <fooBar2!>\n",
|
2014-05-20 16:14:21 +00:00
|
|
|
"<ul><li> <fooBar!></li>\n<li> <fooBar2!></li></ul>\n",
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2013-11-15 15:43:42 +00:00
|
|
|
|
|
|
|
|
return $testCases;
|
2013-10-26 00:52:46 +00:00
|
|
|
}
|
|
|
|
|
|
2013-11-15 22:42:59 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideGetMessage
|
|
|
|
|
* @covers Status::getMessage
|
2014-03-06 20:19:15 +00:00
|
|
|
* @todo test long and short context messages generated through this method
|
2013-11-15 22:42:59 +00:00
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
public function testGetMessage( Status $status, $expectedParams = [], $expectedKey ) {
|
2014-03-06 20:19:15 +00:00
|
|
|
$message = $status->getMessage();
|
2013-11-15 22:42:59 +00:00
|
|
|
$this->assertInstanceOf( 'Message', $message );
|
2014-03-06 20:16:27 +00:00
|
|
|
$this->assertEquals( $expectedParams, $message->getParams(), 'Message::getParams' );
|
|
|
|
|
$this->assertEquals( $expectedKey, $message->getKey(), 'Message::getKey' );
|
2013-11-15 22:42:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-08-13 17:54:49 +00:00
|
|
|
* @return array Array of arrays with values;
|
2013-11-15 22:42:59 +00:00
|
|
|
* 0 => status object
|
2014-03-06 20:16:27 +00:00
|
|
|
* 1 => expected Message parameters (with no context)
|
|
|
|
|
* 2 => expected Message key
|
2013-11-15 22:42:59 +00:00
|
|
|
*/
|
|
|
|
|
public static function provideGetMessage() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$testCases = [];
|
2013-11-15 22:42:59 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$testCases['GoodStatus'] = [
|
2013-11-15 22:42:59 +00:00
|
|
|
new Status(),
|
2016-02-17 09:09:32 +00:00
|
|
|
[ "Status::getMessage called for a good result, this is incorrect\n" ],
|
2013-11-15 22:42:59 +00:00
|
|
|
'internalerror_info'
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2013-11-15 22:42:59 +00:00
|
|
|
|
|
|
|
|
$status = new Status();
|
|
|
|
|
$status->ok = false;
|
2016-02-17 09:09:32 +00:00
|
|
|
$testCases['GoodButNoError'] = [
|
2013-11-15 22:42:59 +00:00
|
|
|
$status,
|
2016-02-17 09:09:32 +00:00
|
|
|
[ "Status::getMessage: Invalid result object: no error text but not OK\n" ],
|
2013-11-15 22:42:59 +00:00
|
|
|
'internalerror_info'
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2013-11-15 22:42:59 +00:00
|
|
|
|
|
|
|
|
$status = new Status();
|
|
|
|
|
$status->warning( 'fooBar!' );
|
2016-02-17 09:09:32 +00:00
|
|
|
$testCases['1StringWarning'] = [
|
2013-11-15 22:42:59 +00:00
|
|
|
$status,
|
2016-02-17 09:09:32 +00:00
|
|
|
[],
|
2014-03-06 20:16:27 +00:00
|
|
|
'fooBar!'
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2013-11-15 22:42:59 +00:00
|
|
|
|
2014-03-06 20:16:27 +00:00
|
|
|
// FIXME: Assertion tries to compare a StubUserLang with a Language object, because
|
|
|
|
|
// "data providers are executed before both the call to the setUpBeforeClass static method
|
|
|
|
|
// and the first call to the setUp method. Because of that you can't access any variables
|
|
|
|
|
// you create there from within a data provider."
|
|
|
|
|
// http://phpunit.de/manual/3.7/en/writing-tests-for-phpunit.html
|
2015-09-11 13:44:59 +00:00
|
|
|
// $status = new Status();
|
|
|
|
|
// $status->warning( 'fooBar!' );
|
|
|
|
|
// $status->warning( 'fooBar2!' );
|
|
|
|
|
// $testCases[ '2StringWarnings' ] = array(
|
|
|
|
|
// $status,
|
|
|
|
|
// array( new Message( 'fooBar!' ), new Message( 'fooBar2!' ) ),
|
|
|
|
|
// "* \$1\n* \$2"
|
|
|
|
|
// );
|
2013-11-15 22:42:59 +00:00
|
|
|
|
|
|
|
|
$status = new Status();
|
2016-02-17 09:09:32 +00:00
|
|
|
$status->warning( new Message( 'fooBar!', [ 'foo', 'bar' ] ) );
|
|
|
|
|
$testCases['1MessageWarning'] = [
|
2013-11-15 22:42:59 +00:00
|
|
|
$status,
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'foo', 'bar' ],
|
2014-03-06 20:16:27 +00:00
|
|
|
'fooBar!'
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2013-11-15 22:42:59 +00:00
|
|
|
|
2014-03-06 20:16:27 +00:00
|
|
|
$status = new Status();
|
2016-02-17 09:09:32 +00:00
|
|
|
$status->warning( new Message( 'fooBar!', [ 'foo', 'bar' ] ) );
|
2014-03-06 20:16:27 +00:00
|
|
|
$status->warning( new Message( 'fooBar2!' ) );
|
2016-02-17 09:09:32 +00:00
|
|
|
$testCases['2MessageWarnings'] = [
|
2014-03-06 20:16:27 +00:00
|
|
|
$status,
|
2016-02-17 09:09:32 +00:00
|
|
|
[ new Message( 'fooBar!', [ 'foo', 'bar' ] ), new Message( 'fooBar2!' ) ],
|
2014-03-06 20:16:27 +00:00
|
|
|
"* \$1\n* \$2"
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2013-11-15 22:42:59 +00:00
|
|
|
|
|
|
|
|
return $testCases;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers Status::replaceMessage
|
|
|
|
|
*/
|
|
|
|
|
public function testReplaceMessage() {
|
|
|
|
|
$status = new Status();
|
2016-02-17 09:09:32 +00:00
|
|
|
$message = new Message( 'key1', [ 'foo1', 'bar1' ] );
|
2013-11-15 22:42:59 +00:00
|
|
|
$status->error( $message );
|
2016-02-17 09:09:32 +00:00
|
|
|
$newMessage = new Message( 'key2', [ 'foo2', 'bar2' ] );
|
2013-11-15 22:42:59 +00:00
|
|
|
|
|
|
|
|
$status->replaceMessage( $message, $newMessage );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( $newMessage, $status->errors[0]['message'] );
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-19 18:12:16 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Status::getErrorMessage
|
|
|
|
|
*/
|
|
|
|
|
public function testGetErrorMessage() {
|
|
|
|
|
$method = new ReflectionMethod( 'Status', 'getErrorMessage' );
|
2013-12-01 19:58:51 +00:00
|
|
|
$method->setAccessible( true );
|
2013-11-19 18:12:16 +00:00
|
|
|
$status = new Status();
|
|
|
|
|
$key = 'foo';
|
2016-02-17 09:09:32 +00:00
|
|
|
$params = [ 'bar' ];
|
2013-11-19 18:12:16 +00:00
|
|
|
|
|
|
|
|
/** @var Message $message */
|
2016-02-17 09:09:32 +00:00
|
|
|
$message = $method->invoke( $status, array_merge( [ $key ], $params ) );
|
2013-11-19 18:12:16 +00:00
|
|
|
$this->assertInstanceOf( 'Message', $message );
|
|
|
|
|
$this->assertEquals( $key, $message->getKey() );
|
|
|
|
|
$this->assertEquals( $params, $message->getParams() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers Status::getErrorMessageArray
|
|
|
|
|
*/
|
|
|
|
|
public function testGetErrorMessageArray() {
|
|
|
|
|
$method = new ReflectionMethod( 'Status', 'getErrorMessageArray' );
|
2013-12-01 19:58:51 +00:00
|
|
|
$method->setAccessible( true );
|
2013-11-19 18:12:16 +00:00
|
|
|
$status = new Status();
|
|
|
|
|
$key = 'foo';
|
2016-02-17 09:09:32 +00:00
|
|
|
$params = [ 'bar' ];
|
2013-11-19 18:12:16 +00:00
|
|
|
|
|
|
|
|
/** @var Message[] $messageArray */
|
|
|
|
|
$messageArray = $method->invoke(
|
|
|
|
|
$status,
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
|
|
|
|
array_merge( [ $key ], $params ),
|
|
|
|
|
array_merge( [ $key ], $params )
|
|
|
|
|
]
|
2013-11-19 18:12:16 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertInternalType( 'array', $messageArray );
|
|
|
|
|
$this->assertCount( 2, $messageArray );
|
2013-12-01 19:58:51 +00:00
|
|
|
foreach ( $messageArray as $message ) {
|
2013-11-19 18:12:16 +00:00
|
|
|
$this->assertInstanceOf( 'Message', $message );
|
|
|
|
|
$this->assertEquals( $key, $message->getKey() );
|
|
|
|
|
$this->assertEquals( $params, $message->getParams() );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers Status::getErrorsByType
|
|
|
|
|
*/
|
|
|
|
|
public function testGetErrorsByType() {
|
|
|
|
|
$status = new Status();
|
|
|
|
|
$warning = new Message( 'warning111' );
|
|
|
|
|
$error = new Message( 'error111' );
|
|
|
|
|
$status->warning( $warning );
|
|
|
|
|
$status->error( $error );
|
|
|
|
|
|
|
|
|
|
$warnings = $status->getErrorsByType( 'warning' );
|
|
|
|
|
$errors = $status->getErrorsByType( 'error' );
|
|
|
|
|
|
|
|
|
|
$this->assertCount( 1, $warnings );
|
|
|
|
|
$this->assertCount( 1, $errors );
|
|
|
|
|
$this->assertEquals( $warning, $warnings[0]['message'] );
|
|
|
|
|
$this->assertEquals( $error, $errors[0]['message'] );
|
|
|
|
|
}
|
2013-10-24 22:11:15 +00:00
|
|
|
|
2014-02-22 10:41:17 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Status::__wakeup
|
|
|
|
|
*/
|
|
|
|
|
public function testWakeUpSanitizesCallback() {
|
|
|
|
|
$status = new Status();
|
2014-07-20 19:41:41 +00:00
|
|
|
$status->cleanCallback = function ( $value ) {
|
2014-02-22 10:41:17 +00:00
|
|
|
return '-' . $value . '-';
|
|
|
|
|
};
|
|
|
|
|
$status->__wakeup();
|
|
|
|
|
$this->assertEquals( false, $status->cleanCallback );
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-22 11:00:18 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider provideNonObjectMessages
|
|
|
|
|
* @covers Status::getStatusArray
|
|
|
|
|
*/
|
|
|
|
|
public function testGetStatusArrayWithNonObjectMessages( $nonObjMsg ) {
|
|
|
|
|
$status = new Status();
|
2014-03-20 18:59:20 +00:00
|
|
|
if ( !array_key_exists( 1, $nonObjMsg ) ) {
|
2014-02-22 11:00:18 +00:00
|
|
|
$status->warning( $nonObjMsg[0] );
|
|
|
|
|
} else {
|
|
|
|
|
$status->warning( $nonObjMsg[0], $nonObjMsg[1] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$array = $status->getWarningsArray(); // We use getWarningsArray to access getStatusArray
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( 1, count( $array ) );
|
|
|
|
|
$this->assertEquals( $nonObjMsg, $array[0] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideNonObjectMessages() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ [ 'ImaString', [ 'param1' => 'value1' ] ] ],
|
|
|
|
|
[ [ 'ImaString' ] ],
|
|
|
|
|
];
|
2014-02-22 11:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
2013-10-25 01:12:47 +00:00
|
|
|
}
|