Debian Codegraph @

tests/phpunit/unit/includes/site/MediaWikiPageNameNormalizerTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Site;
4
5 use MediaWiki\Http\HttpRequestFactory;
6 use MediaWiki\Site\MediaWikiPageNameNormalizer;
7 use MediaWikiUnitTestCase;
8
9 /**
10 * @covers \MediaWiki\Site\MediaWikiPageNameNormalizer
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 * http://www.gnu.org/copyleft/gpl.html
26 *
27 * @since 1.27
28 *
29 * @group Site
30 * @group medium
31 *
32 * @author Marius Hoch
33 */
34 class MediaWikiPageNameNormalizerTest extends MediaWikiUnitTestCase {
35
36 /**
37 * @dataProvider normalizePageTitleProvider
38 */
39 public function testNormalizePageTitle( $expected, $pageName, $getResponse ) {
40 $httpRequestFactory = $this->createMock( HttpRequestFactory::class );
41 $httpRequestFactory->method( 'get' )
42 ->willReturn( $getResponse );
43
44 $normalizer = new MediaWikiPageNameNormalizer( $httpRequestFactory );
45
46 $this->assertSame(
47 $expected,
48 $normalizer->normalizePageName( $pageName, 'https://www.wikidata.org/w/api.php' )
49 );
50 }
51
52 public static function normalizePageTitleProvider() {
53 // Response are taken from wikidata and kkwiki using the following API request
54 // api.php?action=query&prop=info&redirects=1&converttitles=1&format=json&titles=…
55 return [
56 'universe (Q1)' => [
57 'Q1',
58 'Q1',
59 '{"batchcomplete":"","query":{"pages":{"129":{"pageid":129,"ns":0,'
60 . '"title":"Q1","contentmodel":"wikibase-item","pagelanguage":"en",'
61 . '"pagelanguagehtmlcode":"en","pagelanguagedir":"ltr",'
62 . '"touched":"2016-06-23T05:11:21Z","lastrevid":350004448,"length":58001}}}}'
63 ],
64 'Q404 redirects to Q395' => [
65 'Q395',
66 'Q404',
67 '{"batchcomplete":"","query":{"redirects":[{"from":"Q404","to":"Q395"}],"pages"'
68 . ':{"601":{"pageid":601,"ns":0,"title":"Q395","contentmodel":"wikibase-item",'
69 . '"pagelanguage":"en","pagelanguagehtmlcode":"en","pagelanguagedir":"ltr",'
70 . '"touched":"2016-06-23T08:00:20Z","lastrevid":350021914,"length":60108}}}}'
71 ],
72 'D converted to Д (Latin to Cyrillic) (taken from kkwiki)' => [
73 'Д',
74 'D',
75 '{"batchcomplete":"","query":{"converted":[{"from":"D","to":"\u0414"}],'
76 . '"pages":{"510541":{"pageid":510541,"ns":0,"title":"\u0414",'
77 . '"contentmodel":"wikitext","pagelanguage":"kk","pagelanguagehtmlcode":"kk",'
78 . '"pagelanguagedir":"ltr","touched":"2015-11-22T09:16:18Z",'
79 . '"lastrevid":2373618,"length":3501}}}}'
80 ],
81 'there is no Q0' => [
82 false,
83 'Q0',
84 '{"batchcomplete":"","query":{"pages":{"-1":{"ns":0,"title":"Q0",'
85 . '"missing":"","contentmodel":"wikibase-item","pagelanguage":"en",'
86 . '"pagelanguagehtmlcode":"en","pagelanguagedir":"ltr"}}}}'
87 ],
88 'invalid title' => [
89 false,
90 '{{',
91 '{"batchcomplete":"","query":{"pages":{"-1":{"title":"{{",'
92 . '"invalidreason":"The requested page title contains invalid '
93 . 'characters: \"{\".","invalid":""}}}}'
94 ],
95 'error on get' => [ false, 'ABC', false ]
96 ];
97 }
98
99 }