diff options
author | Cindy Cicalese <cindom@gmail.com> | 2021-02-02 17:23:54 -0500 |
---|---|---|
committer | Cindy Cicalese <cindom@gmail.com> | 2021-02-06 10:26:29 -0500 |
commit | 546ae552a9f2b5128ce80daa364ff638654b658d (patch) | |
tree | 199d5821afa5df91f98f45e940458e21b9d40bb2 /tests/phpunit/includes/StatusTest.php | |
parent | c5caf7b546b70adb01849ef37e554592b11faab9 (diff) | |
download | mediawikicore-546ae552a9f2b5128ce80daa364ff638654b658d.tar.gz mediawikicore-546ae552a9f2b5128ce80daa364ff638654b658d.zip |
Status/StatusValue errors/warnings should be unique
Bug: T272100
Depends-On: I4a45d2db5de350384e7f57a728b2089f29dc1767
Change-Id: Idfc93029ef177b92830866de941394b383fcbb34
Diffstat (limited to 'tests/phpunit/includes/StatusTest.php')
-rw-r--r-- | tests/phpunit/includes/StatusTest.php | 234 |
1 files changed, 234 insertions, 0 deletions
diff --git a/tests/phpunit/includes/StatusTest.php b/tests/phpunit/includes/StatusTest.php index d9264c10b314..c00eafbbf21b 100644 --- a/tests/phpunit/includes/StatusTest.php +++ b/tests/phpunit/includes/StatusTest.php @@ -751,4 +751,238 @@ class StatusTest extends MediaWikiLangTestCase { $status->getWikiText( 'wrap-short', 'wrap-long' ); } + public function provideDuplicates() { + yield [ [ 'foo', 1, 2 ], [ 'foo', 1, 2 ] ]; + $message = new Message( 'foo', [ 1, 2 ] ); + yield[ $message, $message ]; + yield [ $message, array_merge( [ $message->getKey() ], $message->getParams() ) ]; + yield [ array_merge( [ $message->getKey() ], $message->getParams() ), $message ]; + } + + /** + * @dataProvider provideDuplicates + * @covers Status::error + */ + public function testDuplicateError( $error1, $error2 ) { + $status = Status::newGood(); + if ( $error1 instanceof MessageSpecifier ) { + $status->error( $error1 ); + $expected = [ + 'type' => 'error', + 'message' => $error1, + 'params' => [] + ]; + } else { + $status->error( ...$error1 ); + $message1 = $error1[0]; + array_shift( $error1 ); + $expected = [ + 'type' => 'error', + 'message' => $message1, + 'params' => $error1 + ]; + } + if ( $error2 instanceof MessageSpecifier ) { + $status->error( $error2 ); + } else { + $message2 = $error2[0]; + array_shift( $error2 ); + $status->error( $message2, ...$error2 ); + } + $this->assertArrayEquals( [ $expected ], $status->errors ); + } + + /** + * @covers Status::warning + */ + public function testDuplicateWarning() { + $status = Status::newGood(); + $status->warning( 'foo', 1, 2 ); + $status->warning( 'foo', 1, 2 ); + $this->assertArrayEquals( + [ + [ + 'type' => 'warning', + 'message' => 'foo', + 'params' => [ 1, 2 ] + ] + ], + $status->errors + ); + } + + /** + * @covers Status::error + * @covers Status::warning + */ + public function testErrorNotDowngradedToWarning() { + $status = Status::newGood(); + $status->error( 'foo', 1, 2 ); + $status->warning( 'foo', 1, 2 ); + $this->assertArrayEquals( + [ + [ + 'type' => 'error', + 'message' => 'foo', + 'params' => [ 1, 2 ] + ] + ], + $status->errors + ); + } + + /** + * @covers Status::error + * @covers Status::warning + */ + public function testErrorNotDowngradedToWarningMessage() { + $status = Status::newGood(); + $message = new Message( 'foo', [ 1, 2 ] ); + $status->error( $message ); + $status->warning( $message ); + $this->assertArrayEquals( + [ + [ + 'type' => 'error', + 'message' => $message, + 'params' => [] + ] + ], + $status->errors + ); + } + + /** + * @covers Status::error + * @covers Status::warning + */ + public function testWarningUpgradedToError() { + $status = Status::newGood(); + $status->warning( 'foo', 1, 2 ); + $status->error( 'foo', 1, 2 ); + $this->assertArrayEquals( + [ + [ + 'type' => 'error', + 'message' => 'foo', + 'params' => [ 1, 2 ] + ] + ], + $status->errors + ); + } + + /** + * @covers Status::error + * @covers Status::warning + */ + public function testWarningUpgradedToErrorMessage() { + $status = Status::newGood(); + $message = new Message( 'foo', [ 1, 2 ] ); + $status->warning( $message ); + $status->error( $message ); + $this->assertArrayEquals( + [ + [ + 'type' => 'error', + 'message' => $message, + 'params' => [] + ] + ], + $status->errors + ); + } + + /** + * Ensure that two MessageSpecifiers that have the same key and params are considered + * identical even if they are different instances. + * @covers Status::error + */ + public function testCompareMessages() { + $status = Status::newGood(); + $message1 = new Message( 'foo', [ 1, 2 ] ); + $status->error( $message1 ); + $message2 = new Message( 'foo', [ 1, 2 ] ); + $status->error( $message2 ); + $this->assertEquals( count( $status->errors ), 1 ); + } + + /** + * @covers Status::merge + */ + public function testDuplicateMerge() { + $status1 = Status::newGood(); + $status1->error( 'cat', 1, 2 ); + $status1->warning( 'dog', 3, 4 ); + $status2 = Status::newGood(); + $status2->warning( 'cat', 1, 2 ); + $status1->error( 'dog', 3, 4 ); + $status2->warning( 'rabbit', 5, 6 ); + $status1->merge( $status2 ); + $this->assertArrayEquals( + [ + [ + 'type' => 'error', + 'message' => 'cat', + 'params' => [ 1, 2 ] + ], + [ + 'type' => 'error', + 'message' => 'dog', + 'params' => [ 3, 4 ] + ], + [ + 'type' => 'warning', + 'message' => 'rabbit', + 'params' => [ 5, 6 ] + ] + ], + $status1->errors + ); + } + + /** + * @covers Status::error + */ + public function testNotDuplicateIfKeyDiffers() { + $status = Status::newGood(); + $status->error( 'foo', 1, 2 ); + $status->error( 'bar', 1, 2 ); + $this->assertArrayEquals( + [ + [ + 'type' => 'error', + 'message' => 'foo', + 'params' => [ 1, 2 ] + ], + [ + 'type' => 'error', + 'message' => 'bar', + 'params' => [ 1, 2 ] + ] + ], + $status->errors + ); + } + + /** + * @covers Status::error + */ + public function testNotDuplicateIfParamsDiffer() { + $status = Status::newGood(); + $status->error( 'foo', 1, 2 ); + $status->error( 'foo', 3, 4 ); + $this->assertArrayEquals( [ + [ + 'type' => 'error', + 'message' => 'foo', + 'params' => [ 1, 2 ] + ], + [ + 'type' => 'error', + 'message' => 'foo', + 'params' => [ 3, 4 ] + ] + ], $status->errors ); + } } |