d198462368d40c15032afb36f7116f410ccbf1d1
[lhc/web/wiklou.git] / tests / phpunit / includes / site / MediaWikiSiteTest.php
1 <?php
2
3 /**
4 * Tests for the MediaWikiSite class.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 * @since 1.21
23 *
24 * @ingroup Site
25 * @ingroup Test
26 *
27 * @group Site
28 *
29 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
30 */
31 class MediaWikiSiteTest extends SiteTest {
32
33 /**
34 * @covers MediaWikiSite::normalizePageName
35 */
36 public function testNormalizePageTitle() {
37 $this->setMwGlobals( [
38 'wgCapitalLinks' => true,
39 ] );
40
41 $site = new MediaWikiSite();
42 $site->setGlobalId( 'enwiki' );
43
44 // NOTE: this does not actually call out to the enwiki site to perform the normalization,
45 // but uses a local Title object to do so. This is hardcoded on SiteLink::normalizePageTitle
46 // for the case that MW_PHPUNIT_TEST is set.
47 $this->assertEquals( 'Foo', $site->normalizePageName( ' foo ' ) );
48 }
49
50 public function fileUrlProvider() {
51 return [
52 // url, filepath, path arg, expected
53 [ 'https://en.wikipedia.org', '/w/$1', 'api.php', 'https://en.wikipedia.org/w/api.php' ],
54 [ 'https://en.wikipedia.org', '/w/', 'api.php', 'https://en.wikipedia.org/w/' ],
55 [
56 'https://en.wikipedia.org',
57 '/foo/page.php?name=$1',
58 'api.php',
59 'https://en.wikipedia.org/foo/page.php?name=api.php'
60 ],
61 [
62 'https://en.wikipedia.org',
63 '/w/$1',
64 '',
65 'https://en.wikipedia.org/w/'
66 ],
67 [
68 'https://en.wikipedia.org',
69 '/w/$1',
70 'foo/bar/api.php',
71 'https://en.wikipedia.org/w/foo/bar/api.php'
72 ],
73 ];
74 }
75
76 /**
77 * @dataProvider fileUrlProvider
78 * @covers MediaWikiSite::getFileUrl
79 */
80 public function testGetFileUrl( $url, $filePath, $pathArgument, $expected ) {
81 $site = new MediaWikiSite();
82 $site->setFilePath( $url . $filePath );
83
84 $this->assertEquals( $expected, $site->getFileUrl( $pathArgument ) );
85 }
86
87 public static function provideGetPageUrl() {
88 return [
89 // path, page, expected substring
90 [ 'http://acme.test/wiki/$1', 'Berlin', '/wiki/Berlin' ],
91 [ 'http://acme.test/wiki/', 'Berlin', '/wiki/' ],
92 [ 'http://acme.test/w/index.php?title=$1', 'Berlin', '/w/index.php?title=Berlin' ],
93 [ 'http://acme.test/wiki/$1', '', '/wiki/' ],
94 [ 'http://acme.test/wiki/$1', 'Berlin/sub page', '/wiki/Berlin/sub_page' ],
95 [ 'http://acme.test/wiki/$1', 'Cork (city) ', '/Cork_(city)' ],
96 [ 'http://acme.test/wiki/$1', 'M&M', '/wiki/M%26M' ],
97 ];
98 }
99
100 /**
101 * @dataProvider provideGetPageUrl
102 * @covers MediaWikiSite::getPageUrl
103 */
104 public function testGetPageUrl( $path, $page, $expected ) {
105 $site = new MediaWikiSite();
106 $site->setLinkPath( $path );
107
108 $this->assertContains( $path, $site->getPageUrl() );
109 $this->assertContains( $expected, $site->getPageUrl( $page ) );
110 }
111 }