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