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