Jeroen De Dauw has submitted this change and it was merged. Change subject: (bug 53796) Add numeric validation for globe precision ......................................................................
(bug 53796) Add numeric validation for globe precision Change-Id: I6dc1aa81206f0d1a3a5b14389f0eb7fdeae97406 --- M lib/WikibaseLib.classes.php A lib/includes/Validators/NumberValidator.php M lib/includes/WikibaseDataTypeBuilders.php A lib/tests/phpunit/Validators/NumberValidatorTest.php M lib/tests/phpunit/WikibaseDataTypeBuildersTest.php 5 files changed, 129 insertions(+), 6 deletions(-) Approvals: Jeroen De Dauw: Looks good to me, approved jenkins-bot: Verified diff --git a/lib/WikibaseLib.classes.php b/lib/WikibaseLib.classes.php index ca402ab..b8f1716 100644 --- a/lib/WikibaseLib.classes.php +++ b/lib/WikibaseLib.classes.php @@ -186,6 +186,7 @@ 'Wikibase\Validators\DataValueValidator' => 'includes/Validators/DataValueValidator.php', 'Wikibase\Validators\EntityExistsValidator' => 'includes/Validators/EntityExistsValidator.php', 'Wikibase\Validators\EntityIdValidator' => 'includes/Validators/EntityIdValidator.php', + 'Wikibase\Validators\NumberValidator' => 'includes/Validators/NumberValidator.php', 'Wikibase\Validators\RegexValidator' => 'includes/Validators/RegexValidator.php', 'Wikibase\Validators\StringLengthValidator' => 'includes/Validators/StringLengthValidator.php', 'Wikibase\Validators\TypeValidator' => 'includes/Validators/TypeValidator.php', diff --git a/lib/includes/Validators/NumberValidator.php b/lib/includes/Validators/NumberValidator.php new file mode 100644 index 0000000..85442a0 --- /dev/null +++ b/lib/includes/Validators/NumberValidator.php @@ -0,0 +1,43 @@ +<?php +namespace Wikibase\Validators; + +use ValueValidators\Error; +use ValueValidators\Result; +use ValueValidators\ValueValidator; + +/** + * @license GPL 2+ + * + * @author Katie Filbert < aude.w...@gmail.com > + */ +class NumberValidator implements ValueValidator { + + /** + * @see ValueValidator::validate() + * + * @param mixed $value The value to validate + * + * @return Result + * @throws InvalidArgumentException + */ + public function validate( $value ) { + $isValid = ( is_float( $value ) || is_int( $value ) ); + + if ( $isValid ) { + return Result::newSuccess(); + } + + return Result::newError( array( + Error::newError( 'Bad type, expected an integer or float value', null, 'bad-type' ) + ) ); + } + + /** + * @see ValueValidator::setOptions() + * + * @param array $options + */ + public function setOptions( array $options ) { + // Do nothing. This method shouldn't even be in the interface. + } +} diff --git a/lib/includes/WikibaseDataTypeBuilders.php b/lib/includes/WikibaseDataTypeBuilders.php index 5177595..22dc925 100644 --- a/lib/includes/WikibaseDataTypeBuilders.php +++ b/lib/includes/WikibaseDataTypeBuilders.php @@ -10,6 +10,7 @@ use Wikibase\Validators\DataFieldValidator; use Wikibase\Validators\DataValueValidator; use Wikibase\Validators\EntityExistsValidator; +use Wikibase\Validators\NumberValidator; use Wikibase\Validators\RegexValidator; use Wikibase\Validators\StringLengthValidator; use Wikibase\Validators\TypeValidator; @@ -173,6 +174,13 @@ $globeIdValidators[] = $urlValidator = $this->buildUrlValidator( array( 'http', 'https' ), 255 ); //TODO: enforce well known reference globes from config + $precisionValidators = array(); + $precisionValidators[] = new NumberValidator(); + + $validators[] = new DataFieldValidator( 'precision', + new CompositeValidator( $precisionValidators, true ) + ); + $validators[] = new DataFieldValidator( 'globe', // Note: validate the 'calendarmodel' field new CompositeValidator( $globeIdValidators, true ) //Note: each validator is fatal ); diff --git a/lib/tests/phpunit/Validators/NumberValidatorTest.php b/lib/tests/phpunit/Validators/NumberValidatorTest.php new file mode 100644 index 0000000..4e5baf4 --- /dev/null +++ b/lib/tests/phpunit/Validators/NumberValidatorTest.php @@ -0,0 +1,49 @@ +<?php + +namespace Wikibase\Test\Validators; + +use DataValues\NumberValue; +use DataValues\StringValue; +use Wikibase\Validators\NumberValidator; +use Wikibase\Validators\ValidatorErrorLocalizer; + +/** + * @covers Wikibase\Validators\NumericValidator + * + * @license GPL 2+ + * + * @ingroup WikibaseLib + * @ingroup Test + * + * @group WikibaseLib + * @group Wikibase + * @group WikibaseValidators + * + * @author Katie Filbert < aude.w...@gmail.com > + */ +class NumberValidatorTest extends \PHPUnit_Framework_TestCase { + + /** + * @dataProvider validateProvider + */ + public function testValidate( $value, $expected ) { + $validator = new NumberValidator(); + $result = $validator->validate( $value ); + + $this->assertEquals( $expected, $result->isValid() ); + } + + public function validateProvider() { + $data = array( + array( 2, true, 'integer is valid' ), + array( 3.5, true, 'float is valid' ), + array( -20, true, 'negative integer is valid' ), + array( '3.4', false, 'string is invalid' ), + array( false, false, 'boolean is invalid' ), + array( null, false, 'null is invalid' ) + ); + + return $data; + } + +} diff --git a/lib/tests/phpunit/WikibaseDataTypeBuildersTest.php b/lib/tests/phpunit/WikibaseDataTypeBuildersTest.php index 0c53487..4fe2084 100644 --- a/lib/tests/phpunit/WikibaseDataTypeBuildersTest.php +++ b/lib/tests/phpunit/WikibaseDataTypeBuildersTest.php @@ -48,6 +48,8 @@ } public function provideDataTypeValidation() { + $latLonValue = new LatLongValue( 0, 0 ); + $cases = array( //wikibase-item array( 'wikibase-item', 'q8', false, 'Expected EntityId, string supplied' ), @@ -91,7 +93,7 @@ array( 'time', new TimeValue( '+0000000000002013-06-06T11:22:33Z', 0, 0, 0, 0, ' javascript:alert(1)' ), false, 'calendar: bad URL' ), //time['time'] - //NOTE: The below will fail with a IllevalValueExcpetion once the TimeValue constructor enforces the time format. + //NOTE: The below will fail with a IllegalValueExcpetion once the TimeValue constructor enforces the time format. // Once that is done, this test and the respective validator can and should both be removed. //array( 'string', new TimeValue( '2013-06-06 11:22:33', 0, 0, 0, 0, 'http://acme.com/calendar' ), false, 'time: not ISO 8601' ), @@ -102,13 +104,33 @@ array( 'globe-coordinate', 'Foo', false, 'GlobeCoordinateValue expected, string supplied' ), array( 'globe-coordinate', new NumberValue( 7 ), false, 'GlobeCoordinateValue expected' ), + //globe-coordinate[precision] + array( + 'globe-coordinate', + new GlobeCoordinateValue( $latLonValue, 1, 'http://www.wikidata.org/entity/Q2' ), + true, + 'integer precision is valid' + ), + array( + 'globe-coordinate', + new GlobeCoordinateValue( $latLonValue, 0.2, 'http://www.wikidata.org/entity/Q2' ), + true, + 'float precision is valid' + ), + array( + 'globe-coordinate', + new GlobeCoordinateValue( $latLonValue, null, 'http://www.wikdiata.org/entity/Q2' ), + false, + 'null precision is invalid' + ), + //globe-coordinate[globe] // FIXME: this is testing unimplemented behaviour? Probably broken... - array( 'globe-coordinate', new GlobeCoordinateValue( new LatLongValue( 0, 0 ), 1, '' ), false, 'globe: empty string should be invalid' ), - array( 'globe-coordinate', new GlobeCoordinateValue( new LatLongValue( 0, 0 ), 1, 'http://' . str_repeat('x', 256) ), false, 'globe: too long' ), - array( 'globe-coordinate', new GlobeCoordinateValue( new LatLongValue( 0, 0 ), 1, 'http://acme.com/globe' ), true, 'globe: URL' ), - array( 'globe-coordinate', new GlobeCoordinateValue( new LatLongValue( 0, 0 ), 1, ' http://acme.com/globe ' ), false, 'globe: untrimmed' ), - array( 'globe-coordinate', new GlobeCoordinateValue( new LatLongValue( 0, 0 ), 1, ' javascript:alert(1) ' ), false, 'globe: bad URL scheme' ), + array( 'globe-coordinate', new GlobeCoordinateValue( $latLonValue, 1, '' ), false, 'globe: empty string should be invalid' ), + array( 'globe-coordinate', new GlobeCoordinateValue( $latLonValue, 1, 'http://' . str_repeat('x', 256) ), false, 'globe: too long' ), + array( 'globe-coordinate', new GlobeCoordinateValue( $latLonValue, 1, 'http://acme.com/globe' ), true, 'globe: URL' ), + array( 'globe-coordinate', new GlobeCoordinateValue( $latLonValue, 1, ' http://acme.com/globe ' ), false, 'globe: untrimmed' ), + array( 'globe-coordinate', new GlobeCoordinateValue( $latLonValue, 1, ' javascript:alert(1) ' ), false, 'globe: bad URL scheme' ), //TODO: globe must be an item reference //TODO: globe must be from a list of configured values ); -- To view, visit https://gerrit.wikimedia.org/r/87545 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I6dc1aa81206f0d1a3a5b14389f0eb7fdeae97406 Gerrit-PatchSet: 2 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Aude <aude.w...@gmail.com> Gerrit-Reviewer: Addshore <addshorew...@gmail.com> Gerrit-Reviewer: Daniel Kinzler <daniel.kinz...@wikimedia.de> Gerrit-Reviewer: Jeroen De Dauw <jeroended...@gmail.com> Gerrit-Reviewer: Tobias Gritschacher <tobias.gritschac...@wikimedia.de> Gerrit-Reviewer: jenkins-bot _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits