Thiemo Mättig (WMDE) has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/343105 )
Change subject: Introduce providers for 4 more data types in
ValidatorBuildersTest
......................................................................
Introduce providers for 4 more data types in ValidatorBuildersTest
All this patch does is moving existing test cases around, but does not
change any detail.
I'm introducing 4 more dedicated test case providers for 4 data types.
The idea is to make the giant provider that was there before much smaller
and easier to maintain. (And probably turn it into an "mismatching data
type" provider if only cases with mismatching data types are left.)
Note that I'm turning the last element in the arrays into array keys.
This is not possible with the giant array because there are a lot of
duplicate messages in there.
Change-Id: Ieef40557a77e2b8dabf19578c7e66f89c4252027
---
M repo/tests/phpunit/includes/ValidatorBuildersTest.php
1 file changed, 145 insertions(+), 134 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase
refs/changes/05/343105/1
diff --git a/repo/tests/phpunit/includes/ValidatorBuildersTest.php
b/repo/tests/phpunit/includes/ValidatorBuildersTest.php
index b87a7d3..fa40779 100644
--- a/repo/tests/phpunit/includes/ValidatorBuildersTest.php
+++ b/repo/tests/phpunit/includes/ValidatorBuildersTest.php
@@ -187,8 +187,152 @@
$this->assertValidation( $expected, $validators, $value );
}
+ public function provideGlobeCoordinateValidation() {
+ $wikidataUri = 'http://www.wikidata.org/entity/';
+
+ return [
+ 'Integer precision is valid' => [ 1, $wikidataUri .
'Q2', true ],
+ 'Float precision is valid' => [ 0.2, $wikidataUri .
'Q2', true ],
+ 'Null precision is invalid' => [ null, $wikidataUri .
'Q2', false ],
+
+ // FIXME: This is testing unimplemented behaviour?
Probably broken...
+ 'Globe should not be empty' => [ 1, '', false ],
+ 'Globe too long' => [ 1, $wikidataUri . 'Q' .
str_repeat( '6', 224 ), false ],
+ 'Valid globe' => [ 1, $wikidataUri . 'Q2', true ],
+ 'Untrimmed globe' => [ 1, ' ' . $wikidataUri . 'Q2 ',
false ],
+ 'Bad URL scheme' => [ 1, ' javascript:alert(1) ', false
],
+
+ // TODO: Globe must be an item reference
+ // TODO: Globe must be from a list of configured values
+ ];
+ }
+
+ /**
+ * @dataProvider provideGlobeCoordinateValidation
+ */
+ public function testGlobeCoordinateValidation( $precision, $globe,
$expected ) {
+ $value = new GlobeCoordinateValue( new LatLongValue( 0, 0 ),
$precision, $globe );
+ $validators =
$this->newValidatorBuilders()->buildCoordinateValidators();
+
+ $this->assertValidation( $expected, $validators, $value );
+ }
+
+ public function provideStringValidation() {
+ return [
+ 'Should not be empty' => [ '', false ],
+ 'Simple string' => [ 'Foo', true ],
+ 'Unicode support' => [ 'Äöü', true ],
+ 'Long, but not too long' => [ str_repeat( 'x', 400 ),
true ],
+ 'Too long' => [ str_repeat( 'x', 401 ), false ],
+ 'Leading space' => [ ' Foo', false ],
+ 'Trailing space' => [ 'Foo ', false ],
+ ];
+ }
+
+ /**
+ * @dataProvider provideStringValidation
+ */
+ public function testStringValidation( $string, $expected ) {
+ $value = new StringValue( $string );
+ $validators =
$this->newValidatorBuilders()->buildStringValidators();
+
+ $this->assertValidation( $expected, $validators, $value );
+ }
+
+ public function provideTimeValidation() {
+ $wikidataUri = 'http://www.wikidata.org/entity/';
+
+ return [
+ 'Calendar model is not a URL' => [
+ '+2013-06-06T00:00:00Z',
+ TimeValue::PRECISION_DAY,
+ '1',
+ false
+ ],
+ 'Calendar model too long' => [
+ '+2013-06-06T00:00:00Z',
+ TimeValue::PRECISION_DAY,
+ $wikidataUri . 'Q' . str_repeat( '6', 224 ),
+ false
+ ],
+ 'Valid calendar model' => [
+ '+2013-06-06T00:00:00Z',
+ TimeValue::PRECISION_DAY,
+ $wikidataUri . 'Q1985727',
+ true
+ ],
+ 'Untrimmed calendar model' => [
+ '+2013-06-06T00:00:00Z',
+ TimeValue::PRECISION_DAY,
+ ' ' . $wikidataUri . 'Q1985727 ',
+ false
+ ],
+ 'Bad URL scheme' => [
+ '+2013-06-06T00:00:00Z',
+ TimeValue::PRECISION_DAY,
+ ' javascript:alert(1)',
+ false
+ ],
+
+ 'Values more precise than a day are currently not
allowed' => [
+ '+2013-06-06T11:22:33Z',
+ TimeValue::PRECISION_DAY,
+ $wikidataUri . 'Q1985727',
+ false
+ ],
+ 'Precisions more fine-grained than a day are currently
not allowed' => [
+ '+2013-06-06T00:00:00Z',
+ TimeValue::PRECISION_SECOND,
+ $wikidataUri . 'Q1985727',
+ false
+ ],
+
+ // TODO: Calendar must be an item reference
+ // TODO: Calendar must be from a list of configured
values
+ ];
+ }
+
+ /**
+ * @dataProvider provideTimeValidation
+ */
+ public function testTimeValidation( $timestamp, $precision,
$calendarModel, $expected ) {
+ $value = new TimeValue( $timestamp, 0, 0, 0, $precision,
$calendarModel );
+ $validators =
$this->newValidatorBuilders()->buildTimeValidators();
+
+ $this->assertValidation( $expected, $validators, $value );
+ }
+
+ public function provideUrlValidation() {
+ return [
+ 'Simple HTTP URL' => [ 'http://acme.com', true ],
+ 'Simple HTTPS URL' => [ 'https://acme.com', true ],
+ 'Simple FTP URL' => [ 'ftp://acme.com', true ],
+ 'Complex HTTP URL' => [
'http://acme.com/foo/bar?some=stuff#fragment', true ],
+
+ // Evil URLs
+ 'Protocol-relative' => [ '//bla', false ],
+ 'Relative path' => [ '/bla/bla', false ],
+ 'Just words' => [ 'just stuff', false ],
+ 'JavaScript' => [ 'javascript:alert("evil")', false ],
+ 'Bad HTTP URL' => [ 'http://', false ],
+ 'Too long' => [ 'http://' . str_repeat( 'x', 494 ),
false ],
+
+ 'Leading space' => [ ' http://acme.com', false ],
+ 'Trailing space' => [ 'http://acme.com ', false ],
+ ];
+ }
+
+ /**
+ * @dataProvider provideUrlValidation
+ */
+ public function testUrlValidation( $string, $expected ) {
+ $value = new StringValue( $string );
+ $validators =
$this->newValidatorBuilders()->buildUrlValidators();
+
+ $this->assertValidation( $expected, $validators, $value );
+ }
+
public function provideDataTypeValidation() {
- $latLonValue = new LatLongValue( 0, 0 );
$wikidataUri = 'http://www.wikidata.org/entity/';
$cases = array(
@@ -223,151 +367,18 @@
//string
array( 'string', 'Foo', false, 'StringValue expected,
string supplied' ),
array( 'string', new NumberValue( 7 ), false,
'StringValue expected' ),
- array( 'string', new StringValue( '' ), false, 'empty
string should be invalid' ),
- array( 'string', new StringValue( 'Foo' ), true,
'simple string' ),
- array( 'string', new StringValue( 'Äöü' ), true,
'Unicode support' ),
- array( 'string', new StringValue( str_repeat( 'x', 400
) ), true, 'long, but not too long' ),
- array( 'string', new StringValue( str_repeat( 'x', 401
) ), false, 'too long' ),
- array( 'string', new StringValue( ' Foo' ), false,
'string with leading space' ),
- array( 'string', new StringValue( 'Foo ' ), false,
'string with trailing space' ),
//time
array( 'time', 'Foo', false, 'TimeValue expected,
string supplied' ),
array( 'time', new NumberValue( 7 ), false, 'TimeValue
expected' ),
- //time['calendar-model']
- array(
- 'time',
- new TimeValue( '+2013-06-06T00:00:00Z', 0, 0,
0, TimeValue::PRECISION_DAY, '1' ),
- false,
- 'calendar: too short'
- ),
- array(
- 'time',
- new TimeValue( '+2013-06-06T00:00:00Z', 0, 0,
0, TimeValue::PRECISION_DAY,
- $wikidataUri . 'Q' . str_repeat( '6',
224 ) ),
- false,
- 'calendar: too long'
- ),
- array(
- 'time',
- new TimeValue( '+2013-06-06T00:00:00Z', 0, 0,
0, TimeValue::PRECISION_DAY,
- $wikidataUri . 'Q1985727' ),
- true,
- 'calendar: URL'
- ),
- array(
- 'time',
- new TimeValue( '+2013-06-06T00:00:00Z', 0, 0,
0, TimeValue::PRECISION_DAY,
- ' ' . $wikidataUri . 'Q1985727 ' ),
- false,
- 'calendar: untrimmed'
- ),
- array(
- 'time',
- new TimeValue( '+2013-06-06T00:00:00Z', 0, 0,
0, TimeValue::PRECISION_DAY,
- ' javascript:alert(1)' ),
- false,
- 'calendar: bad URL'
- ),
-
- //precision to the second (currently not allowed)
- array(
- 'time',
- new TimeValue( '+2013-06-06T11:22:33Z', 0, 0,
0, TimeValue::PRECISION_DAY,
- $wikidataUri . 'Q1985727' ),
- false,
- 'time given to the second'
- ),
- array(
- 'time',
- new TimeValue( '+2013-06-06T00:00:00Z', 0, 0,
0, TimeValue::PRECISION_SECOND,
- $wikidataUri . 'Q1985727' ),
- false,
- 'precision: second'
- ),
-
- //TODO: calendar must be an item reference
- //TODO: calendar must be from a list of configured
values
-
//globe-coordinate
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,
$wikidataUri . 'Q2' ),
- true,
- 'integer precision is valid'
- ),
- array(
- 'globe-coordinate',
- new GlobeCoordinateValue( $latLonValue, 0.2,
$wikidataUri . 'Q2' ),
- true,
- 'float precision is valid'
- ),
- array(
- 'globe-coordinate',
- new GlobeCoordinateValue( $latLonValue, null,
$wikidataUri . 'Q2' ),
- false,
- 'null precision is invalid'
- ),
-
- //globe-coordinate[globe]
- // FIXME: this is testing unimplemented behaviour?
Probably broken...
- array(
- 'globe-coordinate',
- new GlobeCoordinateValue( $latLonValue, 1, '' ),
- false,
- 'globe: empty string should be invalid'
- ),
- array(
- 'globe-coordinate',
- new GlobeCoordinateValue( $latLonValue, 1,
$wikidataUri . 'Q' . str_repeat( '6', 224 ) ),
- false,
- 'globe: too long'
- ),
- array(
- 'globe-coordinate',
- new GlobeCoordinateValue( $latLonValue, 1,
$wikidataUri . 'Q2' ),
- true,
- 'globe: URL'
- ),
- array(
- 'globe-coordinate',
- new GlobeCoordinateValue( $latLonValue, 1, ' '
. $wikidataUri . 'Q2 ' ),
- 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
-
// url
array( 'url', 'Foo', false, 'StringValue expected,
string supplied' ),
array( 'url', new NumberValue( 7 ), false, 'StringValue
expected' ),
-
- array( 'url', new StringValue( 'http://acme.com' ),
true, 'Simple HTTP URL' ),
- array( 'url', new StringValue( 'https://acme.com' ),
true, 'Simple HTTPS URL' ),
- array( 'url', new StringValue( 'ftp://acme.com' ),
true, 'Simple FTP URL' ),
- array( 'url', new StringValue(
'http://acme.com/foo/bar?some=stuff#fragment' ), true, 'Complex HTTP URL' ),
-
- // evil url
- array( 'url', new StringValue( '//bla' ), false,
'Protocol-relative' ),
- array( 'url', new StringValue( '/bla/bla' ), false,
'relative path' ),
- array( 'url', new StringValue( 'just stuff' ), false,
'just words' ),
- array( 'url', new StringValue(
'javascript:alert("evil")' ), false, 'JavaScript URL' ),
- array( 'url', new StringValue( 'http://' ), false, 'bad
http URL' ),
- array( 'url', new StringValue( 'http://' . str_repeat(
'x', 494 ) ), false, 'URL too long' ),
-
- array( 'url', new StringValue( ' http://acme.com' ),
false, 'URL with leading space' ),
- array( 'url', new StringValue( 'http://acme.com ' ),
false, 'URL with trailing space' ),
//quantity
array( 'quantity',
UnboundedQuantityValue::newFromNumber( 5 ), true, 'Unbounded' ),
--
To view, visit https://gerrit.wikimedia.org/r/343105
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ieef40557a77e2b8dabf19578c7e66f89c4252027
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Thiemo Mättig (WMDE) <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits