diff options
Diffstat (limited to 'MLEB/Translate/tests/phpunit/MessageGroupBaseTest.php')
-rw-r--r-- | MLEB/Translate/tests/phpunit/MessageGroupBaseTest.php | 166 |
1 files changed, 144 insertions, 22 deletions
diff --git a/MLEB/Translate/tests/phpunit/MessageGroupBaseTest.php b/MLEB/Translate/tests/phpunit/MessageGroupBaseTest.php index 305db391..7353d911 100644 --- a/MLEB/Translate/tests/phpunit/MessageGroupBaseTest.php +++ b/MLEB/Translate/tests/phpunit/MessageGroupBaseTest.php @@ -1,15 +1,20 @@ <?php +declare( strict_types = 1 ); -class MessageGroupBaseTest extends MediaWikiTestCase { +use MediaWiki\Extensions\Translate\Validation\MessageValidator as MessageValidatorInterface; +use MediaWiki\Extensions\Translate\Validation\ValidationIssues; - /** - * @var MessageGroup - */ +/** + * @license GPL-2.0-or-later + * @covers MessageGroupBase + */ +class MessageGroupBaseTest extends MediaWikiIntegrationTestCase { + /** @var MessageGroup */ protected $group; protected $groupConfiguration = [ 'BASIC' => [ - 'class' => 'FileBasedMessageGroup', + 'class' => FileBasedMessageGroup::class, 'id' => 'test-id', 'label' => 'Test Label', 'namespace' => 'NS_MEDIAWIKI', @@ -17,12 +22,12 @@ class MessageGroupBaseTest extends MediaWikiTestCase { ], ]; - protected function setUp() { + protected function setUp() : void { parent::setUp(); $this->group = MessageGroupBase::factory( $this->groupConfiguration ); } - protected function tearDown() { + protected function tearDown() : void { unset( $this->group ); parent::tearDown(); } @@ -85,13 +90,12 @@ class MessageGroupBaseTest extends MediaWikiTestCase { public function testInsertablesSuggesterClass() { $conf = $this->groupConfiguration; - $conf['INSERTABLES']['class'] = 'FakeInsertablesSuggester'; + $conf['INSERTABLES']['class'] = FakeInsertablesSuggester::class; $this->group = MessageGroupBase::factory( $conf ); $this->assertArrayEquals( [ new Insertable( 'Fake', 'Insertables', 'Suggester' ) ], - $this->group->getInsertablesSuggester()->getInsertables( '' ), - 'should correctly get an InsertablesSuggester using \'class\' option.' + $this->group->getInsertablesSuggester()->getInsertables( '' ) ); } @@ -108,15 +112,14 @@ class MessageGroupBaseTest extends MediaWikiTestCase { new Insertable( 'Fake', 'Insertables', 'Suggester' ), new Insertable( 'AnotherFake', 'Insertables', 'Suggester' ), ], - $this->group->getInsertablesSuggester()->getInsertables( '' ), - 'should correctly get InsertablesSuggesters using \'classes\' option.' + $this->group->getInsertablesSuggester()->getInsertables( '' ) ); } public function testInsertablesSuggesterClassAndClasses() { $conf = $this->groupConfiguration; - $conf['INSERTABLES']['class'] = 'FakeInsertablesSuggester'; - $conf['INSERTABLES']['classes'] = [ 'AnotherFakeInsertablesSuggester' ]; + $conf['INSERTABLES']['class'] = FakeInsertablesSuggester::class; + $conf['INSERTABLES']['classes'] = [ AnotherFakeInsertablesSuggester::class ]; $this->group = MessageGroupBase::factory( $conf ); $this->assertArrayEquals( @@ -124,8 +127,7 @@ class MessageGroupBaseTest extends MediaWikiTestCase { new Insertable( 'Fake', 'Insertables', 'Suggester' ), new Insertable( 'AnotherFake', 'Insertables', 'Suggester' ), ], - $this->group->getInsertablesSuggester()->getInsertables( '' ), - 'should correctly get InsertablesSuggesters using both \'class\' and \'classes\' options.' + $this->group->getInsertablesSuggester()->getInsertables( '' ) ); $conf['INSERTABLES']['classes'][] = 'FakeInsertablesSuggester'; @@ -138,20 +140,120 @@ class MessageGroupBaseTest extends MediaWikiTestCase { new Insertable( 'AnotherFake', 'Insertables', 'Suggester' ), ], $this->group->getInsertablesSuggester()->getInsertables( '' ), - 'should correctly get InsertablesSuggesters using ' . - 'both \'class\' and \'classes\' options and removing duplicates.' + false, + false, + "should correctly get InsertablesSuggesters using " . + "both 'class' and 'classes' options and removing duplicates." ); } - /** - * @expectedException MWException - * @expectedExceptionMessage No valid namespace defined - */ public function testGetNamespaceInvalid() { $conf = $this->groupConfiguration; $conf['BASIC']['namespace'] = 'ergweofijwef'; + $this->expectException( MWException::class ); + $this->expectExceptionMessage( 'No valid namespace defined' ); MessageGroupBase::factory( $conf ); } + + public function testModifyMessageGroupStates() { + // Create a basic workflow. + $this->setMwGlobals( [ + 'wgTranslateWorkflowStates' => [ + 'progress' => [ 'color' => 'd33' ], + 'proofreading' => [ 'color' => 'fc3' ], + ], + ] ); + // Install a special permission when the group ID is matched. + $this->setTemporaryHook( + 'Translate:modifyMessageGroupStates', + function ( $groupId, &$conf ) { + if ( $groupId === 'test-id' ) { + // No users have this. + $conf['proofreading']['right'] = 'inobtanium'; + } + } + ); + + $expectedStates = [ + 'progress' => [ 'color' => 'd33' ], + 'proofreading' => [ 'color' => 'fc3', 'right' => 'inobtanium' ], + ]; + $states = $this->group->getMessageGroupStates()->getStates(); + $this->assertEquals( $expectedStates, $states ); + } + + public function testInsertableValidatorConfiguration() { + $conf = $this->groupConfiguration; + + unset( $conf['INSERTABLES']['class'] ); + $conf['INSERTABLES']['classes'] = [ AnotherFakeInsertablesSuggester::class ]; + $conf['VALIDATORS'] = []; + $conf['VALIDATORS'][] = [ + 'class' => FakeInsertableValidator::class, + 'insertable' => true, + 'params' => 'TEST' + ]; + + $conf['VALIDATORS'][] = [ + 'class' => AnotherFakeInsertableValidator::class, + 'insertable' => false, + 'params' => 'TEST2' + ]; + + $this->group = MessageGroupBase::factory( $conf ); + $messageValidators = $this->group->getValidator(); + $insertables = $this->group->getInsertablesSuggester()->getInsertables( '' ); + + $this->assertInstanceOf( MessageValidator::class, $messageValidators, + "should correctly fetch a 'MessageValidator' using the 'VALIDATOR' configuration." + ); + + // Returns insertables from, + // 1. INSERTABLES > AnotherFakeInsertablesSuggester + // 2. VALIDATORS > FakeInsertableValidator ( insertable => true ) + // Does not return VALIDATORS > AnotherFakeInsertableValidator ( insertable => false ) + $this->assertCount( 2, $insertables, + "should not add non-insertable validator when 'insertable' is false." + ); + + $this->assertEquals( + new Insertable( 'Fake', 'Insertable', 'Validator' ), + $insertables[1], + "should correctly fetch an 'InsertableValidator' when 'insertable' is true." + ); + } + + public function testInsertableArrayConfiguration() { + $conf = $this->groupConfiguration; + unset( $conf['INSERTABLES']['class'] ); + unset( $conf['INSERTABLES']['classes'] ); + + $conf['INSERTABLES'] = [ + [ + 'class' => FakeInsertableValidator::class, + 'params' => 'Regex' + ], + [ + 'class' => AnotherFakeInsertableValidator::class, + 'params' => 'Regex' + ] + ]; + + $this->group = MessageGroupBase::factory( $conf ); + $insertables = $this->group->getInsertablesSuggester()->getInsertables( '' ); + + $this->assertCount( 2, $insertables, + "should fetch the correct count of 'Insertables' when 'InsertablesSuggesters' " . + "are configured using the array configuration." + ); + + $this->assertEquals( + new Insertable( 'Another', 'Fake Insertable', 'Validator' ), + $insertables[1], + "should fetch the correct 'Insertables' when 'InsertablesSuggesters' " . + "are configured using the array configuration." + ); + } } class FakeInsertablesSuggester implements InsertablesSuggester { @@ -165,3 +267,23 @@ class AnotherFakeInsertablesSuggester implements InsertablesSuggester { return [ new Insertable( 'AnotherFake', 'Insertables', 'Suggester' ) ]; } } + +class FakeInsertableValidator implements MessageValidatorInterface, InsertablesSuggester { + public function getIssues( TMessage $message, string $targetLanguage ): ValidationIssues { + return new ValidationIssues(); + } + + public function getInsertables( $text ) { + return [ new Insertable( 'Fake', 'Insertable', 'Validator' ) ]; + } +} + +class AnotherFakeInsertableValidator implements MessageValidatorInterface, InsertablesSuggester { + public function getIssues( TMessage $message, string $targetLanguage ): ValidationIssues { + return new ValidationIssues(); + } + + public function getInsertables( $text ) { + return [ new Insertable( 'Another', 'Fake Insertable', 'Validator' ) ]; + } +} |