[MediaWiki-commits] [Gerrit] Mock Http in MediaWikiPageNameNormalizerTest and add more te... - change (mediawiki/core)
jenkins-bot has submitted this change and it was merged. Change subject: Mock Http in MediaWikiPageNameNormalizerTest and add more test cases .. Mock Http in MediaWikiPageNameNormalizerTest and add more test cases Bug: T138570 Change-Id: Ia156ba61afb90c9a3b1a2e1e0bf967056bd87fc8 --- M includes/site/MediaWikiPageNameNormalizer.php M tests/phpunit/includes/site/MediaWikiPageNameNormalizerTest.php 2 files changed, 80 insertions(+), 34 deletions(-) Approvals: Daniel Kinzler: Looks good to me, approved jenkins-bot: Verified diff --git a/includes/site/MediaWikiPageNameNormalizer.php b/includes/site/MediaWikiPageNameNormalizer.php index 2f71736..1a079b4 100644 --- a/includes/site/MediaWikiPageNameNormalizer.php +++ b/includes/site/MediaWikiPageNameNormalizer.php @@ -35,6 +35,22 @@ class MediaWikiPageNameNormalizer { /** +* @var Http +*/ + private $http; + + /** +* @param Http|null $http +*/ + public function __construct( Http $http = null ) { + if ( !$http ) { + $http = new Http(); + } + + $this->http = $http; + } + + /** * Returns the normalized form of the given page title, using the * normalization rules of the given site. If the given title is a redirect, * the redirect weill be resolved and the redirect target is returned. @@ -85,7 +101,7 @@ // Go on call the external site // @todo we need a good way to specify a timeout here. - $ret = Http::get( $url, [], __METHOD__ ); + $ret = $this->http->get( $url, [], __METHOD__ ); if ( $ret === false ) { wfDebugLog( "MediaWikiSite", "call to external site failed: $url" ); diff --git a/tests/phpunit/includes/site/MediaWikiPageNameNormalizerTest.php b/tests/phpunit/includes/site/MediaWikiPageNameNormalizerTest.php index 3f67b2b..64cdbaa 100644 --- a/tests/phpunit/includes/site/MediaWikiPageNameNormalizerTest.php +++ b/tests/phpunit/includes/site/MediaWikiPageNameNormalizerTest.php @@ -29,36 +29,15 @@ */ class MediaWikiPageNameNormalizerTest extends PHPUnit_Framework_TestCase { - protected function setUp() { - parent::setUp(); - - static $connectivity = null; - - if ( $connectivity === null ) { - // Check whether we have (reasonable fast) connectivity - $res = Http::get( - 'https://www.wikidata.org/w/api.php?action=query=siteinfo=json', - [ 'timeout' => 3 ], - __METHOD__ - ); - - if ( $res === false || strpos( $res, '"sitename":"Wikidata"' ) === false ) { - $connectivity = false; - } else { - $connectivity = true; - } - } - - if ( !$connectivity ) { - $this->markTestSkipped( 'MediaWikiPageNameNormalizerTest needs internet connectivity.' ); - } - } - /** * @dataProvider normalizePageTitleProvider */ - public function testNormalizePageTitle( $expected, $pageName ) { - $normalizer = new MediaWikiPageNameNormalizer(); + public function testNormalizePageTitle( $expected, $pageName, $getResponse ) { + MediaWikiPageNameNormalizerTestMockHttp::$response = $getResponse; + + $normalizer = new MediaWikiPageNameNormalizer( + new MediaWikiPageNameNormalizerTestMockHttp() + ); $this->assertSame( $expected, @@ -67,19 +46,70 @@ } public function normalizePageTitleProvider() { - // Note: This makes (very conservative) assumptions about pages on Wikidata - // existing or not. + // Response are taken from wikidata and kkwiki using the following API request + // api.php?action=query=info=1=1=json=… return [ 'universe (Q1)' => [ - 'Q1', 'Q1' + 'Q1', + 'Q1', + '{"batchcomplete":"","query":{"pages":{"129":{"pageid":129,"ns":0,' + . '"title":"Q1","contentmodel":"wikibase-item","pagelanguage":"en",' + . '"pagelanguagehtmlcode":"en","pagelanguagedir":"ltr",' + . '"touched":"2016-06-23T05:11:21Z","lastrevid":350004448,"length":58001' ], 'Q404 redirects to Q395' => [ - 'Q395', 'Q404' +
[MediaWiki-commits] [Gerrit] Mock Http in MediaWikiPageNameNormalizerTest and add more te... - change (mediawiki/core)
Hoo man has uploaded a new change for review. https://gerrit.wikimedia.org/r/295939 Change subject: Mock Http in MediaWikiPageNameNormalizerTest and add more test cases .. Mock Http in MediaWikiPageNameNormalizerTest and add more test cases Bug: T138570 Change-Id: Ia156ba61afb90c9a3b1a2e1e0bf967056bd87fc8 --- M includes/site/MediaWikiPageNameNormalizer.php M tests/phpunit/includes/site/MediaWikiPageNameNormalizerTest.php 2 files changed, 81 insertions(+), 34 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core refs/changes/39/295939/1 diff --git a/includes/site/MediaWikiPageNameNormalizer.php b/includes/site/MediaWikiPageNameNormalizer.php index 2f71736..1a079b4 100644 --- a/includes/site/MediaWikiPageNameNormalizer.php +++ b/includes/site/MediaWikiPageNameNormalizer.php @@ -35,6 +35,22 @@ class MediaWikiPageNameNormalizer { /** +* @var Http +*/ + private $http; + + /** +* @param Http|null $http +*/ + public function __construct( Http $http = null ) { + if ( !$http ) { + $http = new Http(); + } + + $this->http = $http; + } + + /** * Returns the normalized form of the given page title, using the * normalization rules of the given site. If the given title is a redirect, * the redirect weill be resolved and the redirect target is returned. @@ -85,7 +101,7 @@ // Go on call the external site // @todo we need a good way to specify a timeout here. - $ret = Http::get( $url, [], __METHOD__ ); + $ret = $this->http->get( $url, [], __METHOD__ ); if ( $ret === false ) { wfDebugLog( "MediaWikiSite", "call to external site failed: $url" ); diff --git a/tests/phpunit/includes/site/MediaWikiPageNameNormalizerTest.php b/tests/phpunit/includes/site/MediaWikiPageNameNormalizerTest.php index 3f67b2b..f9e45d3 100644 --- a/tests/phpunit/includes/site/MediaWikiPageNameNormalizerTest.php +++ b/tests/phpunit/includes/site/MediaWikiPageNameNormalizerTest.php @@ -29,36 +29,15 @@ */ class MediaWikiPageNameNormalizerTest extends PHPUnit_Framework_TestCase { - protected function setUp() { - parent::setUp(); - - static $connectivity = null; - - if ( $connectivity === null ) { - // Check whether we have (reasonable fast) connectivity - $res = Http::get( - 'https://www.wikidata.org/w/api.php?action=query=siteinfo=json', - [ 'timeout' => 3 ], - __METHOD__ - ); - - if ( $res === false || strpos( $res, '"sitename":"Wikidata"' ) === false ) { - $connectivity = false; - } else { - $connectivity = true; - } - } - - if ( !$connectivity ) { - $this->markTestSkipped( 'MediaWikiPageNameNormalizerTest needs internet connectivity.' ); - } - } - /** * @dataProvider normalizePageTitleProvider */ - public function testNormalizePageTitle( $expected, $pageName ) { - $normalizer = new MediaWikiPageNameNormalizer(); + public function testNormalizePageTitle( $expected, $pageName, $getResponse ) { + MediaWikiPageNameNormalizerTest_MockHttp::$response = $getResponse; + + $normalizer = new MediaWikiPageNameNormalizer( + new MediaWikiPageNameNormalizerTest_MockHttp() + ); $this->assertSame( $expected, @@ -67,19 +46,71 @@ } public function normalizePageTitleProvider() { - // Note: This makes (very conservative) assumptions about pages on Wikidata - // existing or not. + // Response are taken from: + // https://www.wikidata.org/w/api.php?action=query=info=1=1=json=… + // https://sr.wikipedia.org/w/api.php?action=query=info=1=1=json=… return [ 'universe (Q1)' => [ - 'Q1', 'Q1' + 'Q1', + 'Q1', + '{"batchcomplete":"","query":{"pages":{"129":{"pageid":129,"ns":0,' + . '"title":"Q1","contentmodel":"wikibase-item","pagelanguage":"en",' + . '"pagelanguagehtmlcode":"en","pagelanguagedir":"ltr",' + . '"touched":"2016-06-23T05:11:21Z","lastrevid":350004448,"length":58001' ],