Ladsgroup has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/339805 )
Change subject: [WIP] Start LexemeValidatorFactory ...................................................................... [WIP] Start LexemeValidatorFactory Things to do: - Fix other tests - Add tests for the validator - (optional) Move the methods to a dedicated class for code reusablity Bug: T158780 Change-Id: Ib046d6ade16fd826813906d7dbc80f219ef3caa4 --- M WikibaseLexeme.entitytypes.php M src/Validators/LexemeValidatorFactory.php M tests/phpunit/mediawiki/Validators/LexemeValidatorFactoryTest.php 3 files changed, 90 insertions(+), 14 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseLexeme refs/changes/05/339805/1 diff --git a/WikibaseLexeme.entitytypes.php b/WikibaseLexeme.entitytypes.php index 7566b13..e5d9ae2 100644 --- a/WikibaseLexeme.entitytypes.php +++ b/WikibaseLexeme.entitytypes.php @@ -128,7 +128,8 @@ new TermChangeOpSerializationValidator( $wikibaseRepo->getTermsLanguages() ), new LexemeValidatorFactory( 1000, // TODO: move to setting, at least change to some reasonable hard-coded value - $wikibaseRepo->getTermValidatorFactory() + $wikibaseRepo->getTermValidatorFactory(), + $wikibaseRepo->getDefaultValidatorBuilders() ), $wikibaseRepo->getStringNormalizer() ) diff --git a/src/Validators/LexemeValidatorFactory.php b/src/Validators/LexemeValidatorFactory.php index cf59d14..bf8fa64 100644 --- a/src/Validators/LexemeValidatorFactory.php +++ b/src/Validators/LexemeValidatorFactory.php @@ -2,8 +2,9 @@ namespace Wikibase\Lexeme\Validators; -use ValueValidators\NullValidator; use ValueValidators\ValueValidator; +use Wikibase\DataModel\Entity\ItemId; +use Wikibase\Repo\ValidatorBuilders; use Wikibase\Repo\Validators\CompositeValidator; use Wikibase\Repo\Validators\RegexValidator; use Wikibase\Repo\Validators\StringLengthValidator; @@ -29,14 +30,25 @@ private $termValidatorFactory; /** + * @var ValidatorBuilders + */ + private $validatorBuilders; + + /** * @param int $maxTermLength max string length for lemma term * @param TermValidatorFactory $termValidatorFactory + * @param ValidatorBuilders $validatorBuilders */ - public function __construct( $maxTermLength, TermValidatorFactory $termValidatorFactory ) { + public function __construct( + $maxTermLength, + TermValidatorFactory $termValidatorFactory, + ValidatorBuilders $validatorBuilders + ) { Assert::parameterType( 'integer', $maxTermLength, '$maxLength' ); $this->maxTermLength = $maxTermLength; $this->termValidatorFactory = $termValidatorFactory; + $this->validatorBuilders = $validatorBuilders; } /** @@ -65,8 +77,10 @@ * @return ValueValidator */ public function getLanguageValidator() { - // TODO: Implement this - return new NullValidator(); + return new CompositeValidator( + $this->validatorBuilders->buildItemValidators(), + true + ); } } diff --git a/tests/phpunit/mediawiki/Validators/LexemeValidatorFactoryTest.php b/tests/phpunit/mediawiki/Validators/LexemeValidatorFactoryTest.php index 02baee7..44e2253 100644 --- a/tests/phpunit/mediawiki/Validators/LexemeValidatorFactoryTest.php +++ b/tests/phpunit/mediawiki/Validators/LexemeValidatorFactoryTest.php @@ -3,9 +3,14 @@ namespace Wikibase\Lexeme\Tests\Validators; use InvalidArgumentException; +use ValueValidators\Error; +use ValueValidators\Result; use Wikibase\DataModel\Entity\BasicEntityIdParser; +use Wikibase\DataModel\Entity\ItemId; use Wikibase\Lexeme\Validators\LexemeValidatorFactory; use Wikibase\Repo\Tests\ChangeOp\ChangeOpTestMockProvider; +use Wikibase\Repo\ValidatorBuilders; +use Wikibase\Repo\Validators\EntityExistsValidator; use Wikibase\Repo\Validators\TermValidatorFactory; /** @@ -22,7 +27,7 @@ */ public function testGivenInvalidMaxLength_constructorThrowsException( $maxLength ) { $this->setExpectedException( InvalidArgumentException::class ); - new LexemeValidatorFactory( $maxLength, $this->getTermValidatorFactory() ); + $this->getLexemeValidatorFactory( $maxLength ); } public function invalidConstructorArgsProvider() { @@ -43,10 +48,9 @@ $dupeDetector ); - $languageCodeValidator = ( new LexemeValidatorFactory( - 100, - $termValidatorFactory - ) )->getLanguageCodeValidator(); + $languageCodeValidator = $this + ->getLexemeValidatorFactory( 100, $termValidatorFactory ) + ->getLanguageCodeValidator(); $this->assertTrue( $languageCodeValidator->validate( 'en' )->isValid() ); $this->assertTrue( $languageCodeValidator->validate( 'fr' )->isValid() ); @@ -57,10 +61,9 @@ * @dataProvider lemmaTermProvider */ public function testGetLemmaTermValidator( $isValid, $lemmaTerm ) { - $lemmaValidator = ( new LexemeValidatorFactory( - 10, - $this->getTermValidatorFactory() ) - )->getLemmaTermValidator(); + $lemmaValidator = $this + ->getLexemeValidatorFactory( 10 ) + ->getLemmaTermValidator(); $this->assertSame( $isValid, @@ -84,4 +87,62 @@ return $mockProvider->getMockTermValidatorFactory(); } + private function getItemValidator( $itemId ) { + if ( $itemId === null ) { + return Result::newSuccess(); + } + + if ( !$itemId instanceof ItemId ) { + return Error::newError( + "Wrong entity type: " . $itemId->getEntityType(), + null, + 'bad-entity-type', + [ $itemId->getEntityType() ] + ); + } + + $existedItemIds = [ 'Q123', 'Q321' ]; + if ( array_key_exists( $existedItemIds, $itemId->getSerialization() ) ) { + return Result::newSuccess(); + } + + return Error::newError( + "Entity not found: " . $itemId->getSerialization(), + null, 'no-such-entity', + [ $itemId->getSerialization() ] + ); + } + + private function getValidatorBuilder() { + $builderMock = $this->getMockBuilder( ValidatorBuilders::class ) + ->disableOriginalConstructor() + ->getMock(); + $builderMock->expects( $this->any() ) + ->method( 'buildItemValidators' ) + ->will( $this->returnCallback( function () { + $validatorMock = $this->getMockBuilder( EntityExistsValidator::class ) + ->disableOriginalConstructor() + ->getMock(); + $validatorMock ->expects( $this->any() ) + ->method( 'validate' ) + ->will( $this->returnCallback( function ( $itemId ) { + $this->getItemValidator( $itemId ); + } ) ); + return $validatorMock; + } ) ); + + return $builderMock; + } + + private function getLexemeValidatorFactory( + $maxLength, + TermValidatorFactory $termValidatorFactory = null + ) { + return new LexemeValidatorFactory( + $maxLength, + $termValidatorFactory === null ? $this->getTermValidatorFactory() : $termValidatorFactory, + $this->getValidatorBuilder() + ); + } + } -- To view, visit https://gerrit.wikimedia.org/r/339805 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ib046d6ade16fd826813906d7dbc80f219ef3caa4 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/WikibaseLexeme Gerrit-Branch: master Gerrit-Owner: Ladsgroup <ladsgr...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits