2015-12-20 21:00:20 +00:00
|
|
|
<?php
|
|
|
|
|
|
2022-12-11 15:36:56 +00:00
|
|
|
use MediaWiki\Http\HttpRequestFactory;
|
2015-12-20 21:00:20 +00:00
|
|
|
use MediaWiki\Site\MediaWikiPageNameNormalizer;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers MediaWiki\Site\MediaWikiPageNameNormalizer
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @since 1.27
|
|
|
|
|
*
|
|
|
|
|
* @group Site
|
|
|
|
|
* @group medium
|
|
|
|
|
*
|
|
|
|
|
* @author Marius Hoch
|
|
|
|
|
*/
|
2021-03-11 18:45:36 +00:00
|
|
|
class MediaWikiPageNameNormalizerTest extends MediaWikiUnitTestCase {
|
2017-12-29 23:22:37 +00:00
|
|
|
|
2015-12-20 21:00:20 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider normalizePageTitleProvider
|
|
|
|
|
*/
|
2016-06-24 15:24:10 +00:00
|
|
|
public function testNormalizePageTitle( $expected, $pageName, $getResponse ) {
|
2022-12-11 15:36:56 +00:00
|
|
|
$httpRequestFactory = $this->createMock( HttpRequestFactory::class );
|
|
|
|
|
$httpRequestFactory->method( 'get' )
|
|
|
|
|
->willReturn( $getResponse );
|
2016-06-24 15:24:10 +00:00
|
|
|
|
2022-12-11 15:36:56 +00:00
|
|
|
$normalizer = new MediaWikiPageNameNormalizer( $httpRequestFactory );
|
2015-12-20 21:00:20 +00:00
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
$expected,
|
|
|
|
|
$normalizer->normalizePageName( $pageName, 'https://www.wikidata.org/w/api.php' )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function normalizePageTitleProvider() {
|
2016-06-24 15:24:10 +00:00
|
|
|
// Response are taken from wikidata and kkwiki using the following API request
|
|
|
|
|
// api.php?action=query&prop=info&redirects=1&converttitles=1&format=json&titles=…
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
'universe (Q1)' => [
|
2016-06-24 15:24:10 +00:00
|
|
|
'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}}}}'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'Q404 redirects to Q395' => [
|
2016-06-24 15:24:10 +00:00
|
|
|
'Q395',
|
|
|
|
|
'Q404',
|
|
|
|
|
'{"batchcomplete":"","query":{"redirects":[{"from":"Q404","to":"Q395"}],"pages"'
|
|
|
|
|
. ':{"601":{"pageid":601,"ns":0,"title":"Q395","contentmodel":"wikibase-item",'
|
|
|
|
|
. '"pagelanguage":"en","pagelanguagehtmlcode":"en","pagelanguagedir":"ltr",'
|
|
|
|
|
. '"touched":"2016-06-23T08:00:20Z","lastrevid":350021914,"length":60108}}}}'
|
|
|
|
|
],
|
|
|
|
|
'D converted to Д (Latin to Cyrillic) (taken from kkwiki)' => [
|
|
|
|
|
'Д',
|
|
|
|
|
'D',
|
|
|
|
|
'{"batchcomplete":"","query":{"converted":[{"from":"D","to":"\u0414"}],'
|
|
|
|
|
. '"pages":{"510541":{"pageid":510541,"ns":0,"title":"\u0414",'
|
|
|
|
|
. '"contentmodel":"wikitext","pagelanguage":"kk","pagelanguagehtmlcode":"kk",'
|
|
|
|
|
. '"pagelanguagedir":"ltr","touched":"2015-11-22T09:16:18Z",'
|
|
|
|
|
. '"lastrevid":2373618,"length":3501}}}}'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'there is no Q0' => [
|
2016-06-24 15:24:10 +00:00
|
|
|
false,
|
|
|
|
|
'Q0',
|
|
|
|
|
'{"batchcomplete":"","query":{"pages":{"-1":{"ns":0,"title":"Q0",'
|
|
|
|
|
. '"missing":"","contentmodel":"wikibase-item","pagelanguage":"en",'
|
|
|
|
|
. '"pagelanguagehtmlcode":"en","pagelanguagedir":"ltr"}}}}'
|
|
|
|
|
],
|
|
|
|
|
'invalid title' => [
|
|
|
|
|
false,
|
|
|
|
|
'{{',
|
|
|
|
|
'{"batchcomplete":"","query":{"pages":{"-1":{"title":"{{",'
|
|
|
|
|
. '"invalidreason":"The requested page title contains invalid '
|
|
|
|
|
. 'characters: \"{\".","invalid":""}}}}'
|
|
|
|
|
],
|
|
|
|
|
'error on get' => [ false, 'ABC', false ]
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2015-12-20 21:00:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|