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