Thiemo Mättig (WMDE) has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/313971

Change subject: Properly document and test bzr/cvs schemes in 
UrlSchemeValidators
......................................................................

Properly document and test bzr/cvs schemes in UrlSchemeValidators

This was added in I1c81304, but not very well documented.

Bug: T146692
Change-Id: If997bb9401ea19f19f61da34b7fcc90e7435edb1
---
M repo/config/Wikibase.default.php
M repo/includes/Validators/UrlSchemeValidators.php
M repo/tests/phpunit/includes/Validators/UrlSchemeValidatorsTest.php
M repo/tests/phpunit/includes/WikibaseRepoTest.php
4 files changed, 108 insertions(+), 62 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/71/313971/1

diff --git a/repo/config/Wikibase.default.php b/repo/config/Wikibase.default.php
index 75e931d..3f59c3f 100644
--- a/repo/config/Wikibase.default.php
+++ b/repo/config/Wikibase.default.php
@@ -35,7 +35,7 @@
        ],
 
        // URL schemes allowed for URL values. See UrlSchemeValidators for a 
full list.
-       'urlSchemes' => [ 'ftp', 'http', 'https', 'irc', 'mailto', 'svn', 
'git', 'ssh', 'bzr', 'cvs' ],
+       'urlSchemes' => [ 'bzr', 'cvs', 'ftp', 'git', 'http', 'https', 'irc', 
'mailto', 'ssh', 'svn' ],
 
        // Items allowed to be used as badges pointing to their CSS class names
        'badgeItems' => [],
diff --git a/repo/includes/Validators/UrlSchemeValidators.php 
b/repo/includes/Validators/UrlSchemeValidators.php
index 3e5e484..1afa4df 100644
--- a/repo/includes/Validators/UrlSchemeValidators.php
+++ b/repo/includes/Validators/UrlSchemeValidators.php
@@ -28,6 +28,7 @@
         */
        public function getValidator( $scheme ) {
                switch ( $scheme ) {
+                       // Schemes with "://", copied from $wgUrlProtocols in 
MediaWiki's DefaultSettings.php
                        case 'ftp':
                        case 'ftps':
                        case 'git':
@@ -43,14 +44,19 @@
                        case 'ssh':
                        case 'svn':
                        case 'telnet':
+                       case 'worldwind':
+
+                       // Additional VCS schemes not supported by MediaWiki. 
"bzr" is GNU Bazaar from Canonical
+                       // Ltd. See https://phabricator.wikimedia.org/T146692
                        case 'bzr':
                        case 'cvs':
-                       case 'worldwind':
-                               $regex = '!^' . preg_quote( $scheme, '!' ) . 
'://(' . Parser::EXT_LINK_URL_CLASS . ')+\z!ui';
+                               $regex = '!^' . preg_quote( $scheme, '!' ) . 
'://(' . Parser::EXT_LINK_URL_CLASS
+                                       . ')+\z!ui';
                                break;
 
                        case 'mailto':
-                               $regex = '!^mailto:(' . 
Parser::EXT_LINK_URL_CLASS . ')+@(' . Parser::EXT_LINK_URL_CLASS . ')+\z!ui';
+                               $regex = '!^mailto:(' . 
Parser::EXT_LINK_URL_CLASS . ')+@('
+                                       . Parser::EXT_LINK_URL_CLASS . 
')+\z!ui';
                                break;
 
                        case '*':
@@ -75,7 +81,7 @@
         * @return ValueValidator[] a map of scheme names to ValueValidator 
objects.
         */
        public function getValidators( array $schemes ) {
-               $validators = array();
+               $validators = [];
 
                foreach ( $schemes as $scheme ) {
                        $validator = $this->getValidator( $scheme );
diff --git a/repo/tests/phpunit/includes/Validators/UrlSchemeValidatorsTest.php 
b/repo/tests/phpunit/includes/Validators/UrlSchemeValidatorsTest.php
index 1c6cc67..a2151b5 100644
--- a/repo/tests/phpunit/includes/Validators/UrlSchemeValidatorsTest.php
+++ b/repo/tests/phpunit/includes/Validators/UrlSchemeValidatorsTest.php
@@ -45,66 +45,69 @@
        }
 
        public function validUrlProvider() {
-               return array(
-                       array( 'http', 'http://acme.com' ),
-                       array( 'http', 
'http://foo:[email protected]/stuff/thingy.php?foo=bar#part' ),
-                       array( 'https', 'https://acme.com' ),
-                       array( 'https', 
'https://foo:[email protected]/stuff/thingy.php?foo=bar#part' ),
-                       array( 'https', 'https://ko.wikipedia.org/wiki/전_(요리)' 
),
-                       array( 'ftp', 'ftp://acme.com' ),
-                       array( 'ftp', 
'ftp://foo:[email protected]/stuff/thingy.php?foo=bar#part' ),
-                       array( 'irc', 'irc://chat.freenode.net/gimp' ),
-                       array( 'mailto', 'mailto:foo@bar' ),
-                       array( 'mailto', 'mailto:random.korean.character.전@bar' 
),
-                       array( 'mailto', 
'mailto:[email protected]?Subject=test' ),
-                       array( 'telnet', 'telnet://user:password@host:9999/' ),
-                       array( 'any', 'http://acme.com' ),
-                       array( 'any', 'dummy:some/stuff' ),
-                       array( 'any', 'dummy+me:other-stuff' ),
-                       array( 'any', 'dummy-you:some?things' ),
-                       array( 'any', 'dummy.do:other#things' ),
-                       array( 'any', 'random.korean.character:전' ),
-               );
+               return [
+                       [ 'http', 'http://acme.com' ],
+                       [ 'http', 
'http://foo:[email protected]/stuff/thingy.php?foo=bar#part' ],
+                       [ 'https', 'https://acme.com' ],
+                       [ 'https', 
'https://foo:[email protected]/stuff/thingy.php?foo=bar#part' ],
+                       [ 'https', 'https://ko.wikipedia.org/wiki/전_(요리)' ],
+                       [ 'ftp', 'ftp://acme.com' ],
+                       [ 'ftp', 
'ftp://foo:[email protected]/stuff/thingy.php?foo=bar#part' ],
+                       [ 'irc', 'irc://chat.freenode.net/gimp' ],
+                       [ 'bzr', 
'bzr://archonproject.bzr.sourceforge.net/bzrroot/archonproject' ],
+                       [ 'cvs', 
'cvs://pserver:[email protected]/cvs/djgpp' ],
+                       [ 'mailto', 'mailto:foo@bar' ],
+                       [ 'mailto', 'mailto:random.korean.character.전@bar' ],
+                       [ 'mailto', 
'mailto:[email protected]?Subject=test' ],
+                       [ 'telnet', 'telnet://user:password@host:9999/' ],
+                       [ 'any', 'http://acme.com' ],
+                       [ 'any', 'dummy:some/stuff' ],
+                       [ 'any', 'dummy+me:other-stuff' ],
+                       [ 'any', 'dummy-you:some?things' ],
+                       [ 'any', 'dummy.do:other#things' ],
+                       [ 'any', 'random.korean.character:전' ],
+               ];
        }
 
        public function invalidUrlProvider() {
-               return array(
+               return [
                        // Trailing newlines
-                       array( 'http', "http://example.com\n"; ),
-                       array( 'mailto', "mailto:[email protected]\n"; ),
-                       array( 'any', "http://example.com\n"; ),
+                       [ 'http', "http://example.com\n"; ],
+                       [ 'mailto', "mailto:[email protected]\n"; ],
+                       [ 'any', "http://example.com\n"; ],
 
-                       array( 'http', 'yadda' ),
-                       array( 'http', 'http:' ),
-                       array( 'http', 'http://' ),
-                       array( 'http', 'http://acme.com/foo' . "\n" . 'bar' ),
-                       array( 'http', '*http://acme.com/foo' ),
-                       array( 'https', 'yadda' ),
-                       array( 'https', 'https:' ),
-                       array( 'https', 'https://' ),
-                       array( 'https', 'https://acme.com/foo' . "\n" . 'bar' ),
-                       array( 'https', '*https://acme.com/foo' ),
-                       array( 'ftp', 'yadda' ),
-                       array( 'ftp', 'ftp:' ),
-                       array( 'ftp', 'ftp://' ),
-                       array( 'ftp', 'ftp://acme.com/foo' . "\n" . 'bar' ),
-                       array( 'ftp', '*ftp://acme.com/foo' ),
-                       array( 'mailto', 'yadda' ),
-                       array( 'mailto', 'mailto:stuff' ),
-                       array( 'mailto', 'mailto:james@thingy' . "\n" . '.com' 
),
-                       array( 'mailto', '*mailto:james@thingy' ),
-                       array( 'any', 'yadda' ),
-                       array( 'any', 'yadda/yadda' ),
-                       array( 'any', ':' ),
-                       array( 'any', 'foo:' ),
-                       array( 'any', ':bar' ),
-                       array( 'any', '+must:start-with-character' ),
-                       array( 'any', '.must:start-with-character' ),
-                       array( 'any', '-must:start-with-character' ),
-                       array( 'any', '0must:start-with-character' ),
-                       array( 'any', 'doo*da:foo' ),
-                       array( 'any', 'foo:' . "\n" . '.bar' ),
-               );
+                       [ 'http', 'yadda' ],
+                       [ 'http', 'http:' ],
+                       [ 'http', 'http://' ],
+                       [ 'http', 'http://acme.com/foo' . "\n" . 'bar' ],
+                       [ 'http', '*http://acme.com/foo' ],
+                       [ 'https', 'yadda' ],
+                       [ 'https', 'https:' ],
+                       [ 'https', 'https://' ],
+                       [ 'https', 'https://acme.com/foo' . "\n" . 'bar' ],
+                       [ 'https', '*https://acme.com/foo' ],
+                       [ 'ftp', 'yadda' ],
+                       [ 'ftp', 'ftp:' ],
+                       [ 'ftp', 'ftp://' ],
+                       [ 'ftp', 'ftp://acme.com/foo' . "\n" . 'bar' ],
+                       [ 'ftp', '*ftp://acme.com/foo' ],
+                       [ 'cvs', 
':pserver:[email protected]:/cvs/djgpp' ],
+                       [ 'mailto', 'yadda' ],
+                       [ 'mailto', 'mailto:stuff' ],
+                       [ 'mailto', 'mailto:james@thingy' . "\n" . '.com' ],
+                       [ 'mailto', '*mailto:james@thingy' ],
+                       [ 'any', 'yadda' ],
+                       [ 'any', 'yadda/yadda' ],
+                       [ 'any', ':' ],
+                       [ 'any', 'foo:' ],
+                       [ 'any', ':bar' ],
+                       [ 'any', '+must:start-with-character' ],
+                       [ 'any', '.must:start-with-character' ],
+                       [ 'any', '-must:start-with-character' ],
+                       [ 'any', '0must:start-with-character' ],
+                       [ 'any', 'doo*da:foo' ],
+                       [ 'any', 'foo:' . "\n" . '.bar' ],
+               ];
        }
 
        protected function assertErrorCodeLocalization( Result $result ) {
@@ -135,10 +138,10 @@
        public function testGetValidators() {
                $factory = new UrlSchemeValidators();
 
-               $schemes = array( 'http', 'https', 'ftp', 'dummy' );
+               $schemes = [ 'http', 'https', 'ftp', 'dummy' ];
                $validators = $factory->getValidators( $schemes );
 
-               $this->assertEquals( array( 'http', 'https', 'ftp' ), 
array_keys( $validators ) );
+               $this->assertEquals( [ 'http', 'https', 'ftp' ], array_keys( 
$validators ) );
                $this->assertContainsOnlyInstancesOf( ValueValidator::class, 
$validators );
        }
 
diff --git a/repo/tests/phpunit/includes/WikibaseRepoTest.php 
b/repo/tests/phpunit/includes/WikibaseRepoTest.php
index 13c3b6c..9a6b670 100644
--- a/repo/tests/phpunit/includes/WikibaseRepoTest.php
+++ b/repo/tests/phpunit/includes/WikibaseRepoTest.php
@@ -66,6 +66,7 @@
 use Wikibase\Repo\ParserOutput\EntityParserOutputGeneratorFactory;
 use Wikibase\Repo\SnakFactory;
 use Wikibase\Repo\ValidatorBuilders;
+use Wikibase\Repo\Validators\CompositeValidator;
 use Wikibase\Repo\ValueParserFactory;
 use Wikibase\Repo\WikibaseRepo;
 use Wikibase\SettingsArray;
@@ -95,6 +96,42 @@
                $this->assertSame( $first, $second );
        }
 
+       /**
+        * @dataProvider urlSchemesProvider
+        */
+       public function testDefaultUrlValidators( $input, $expected ) {
+               $validatorBuilders = 
$this->getWikibaseRepo()->getDefaultValidatorBuilders();
+               $urlValidator = new CompositeValidator( 
$validatorBuilders->buildUrlValidators() );
+               $result = $urlValidator->validate( new StringValue( $input ) );
+               $this->assertSame( $expected, $result->isValid() );
+       }
+
+       public function urlSchemesProvider() {
+               return [
+                       [ 'bzr://x', true ],
+                       [ 'cvs://x', true ],
+                       [ 'ftp://x', true ],
+                       [ 'git://x', true ],
+                       [ 'http://x', true ],
+                       [ 'https://x', true ],
+                       [ 'irc://x', true ],
+                       [ 'mailto:x@x', true ],
+                       [ 'ssh://x', true ],
+                       [ 'svn://x', true ],
+
+                       // Supported by UrlSchemeValidators, but not enabled by 
default.
+                       [ 'ftps://x', false ],
+                       [ 'gopher://x', false ],
+                       [ 'ircs://x', false ],
+                       [ 'mms://x', false ],
+                       [ 'nntp://x', false ],
+                       [ 'redis://x', false ],
+                       [ 'sftp://x', false ],
+                       [ 'telnet://x', false ],
+                       [ 'worldwind://x', false ],
+               ];
+       }
+
        public function testGetDefaultValueFormatterBuilders() {
                $first = 
$this->getWikibaseRepo()->getDefaultValueFormatterBuilders();
                $this->assertInstanceOf( WikibaseValueFormatterBuilders::class, 
$first );

-- 
To view, visit https://gerrit.wikimedia.org/r/313971
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If997bb9401ea19f19f61da34b7fcc90e7435edb1
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

Reply via email to