Stats: Add support for chaining TimingMetric->start()
[mediawiki.git] / tests / phpunit / includes / site / SiteTest.php
blobb719dc6c2598ce2b9788ff195b138ffd55d82b07
1 <?php
3 namespace MediaWiki\Tests\Site;
5 use MediaWiki\Site\Site;
6 use MediaWikiIntegrationTestCase;
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 SiteTest extends MediaWikiIntegrationTestCase {
36 public function instanceProvider() {
37 return $this->arrayWrap( TestSites::getSites() );
40 /**
41 * @dataProvider instanceProvider
42 * @param Site $site
43 * @covers \MediaWiki\Site\Site::getInterwikiIds
45 public function testGetInterwikiIds( Site $site ) {
46 $this->assertIsArray( $site->getInterwikiIds() );
49 /**
50 * @dataProvider instanceProvider
51 * @param Site $site
52 * @covers \MediaWiki\Site\Site::getNavigationIds
54 public function testGetNavigationIds( Site $site ) {
55 $this->assertIsArray( $site->getNavigationIds() );
58 /**
59 * @dataProvider instanceProvider
60 * @param Site $site
61 * @covers \MediaWiki\Site\Site::addNavigationId
63 public function testAddNavigationId( Site $site ) {
64 $site->addNavigationId( 'foobar' );
65 $this->assertContains( 'foobar', $site->getNavigationIds() );
68 /**
69 * @dataProvider instanceProvider
70 * @param Site $site
71 * @covers \MediaWiki\Site\Site::addInterwikiId
73 public function testAddInterwikiId( Site $site ) {
74 $site->addInterwikiId( 'foobar' );
75 $this->assertContains( 'foobar', $site->getInterwikiIds() );
78 /**
79 * @dataProvider instanceProvider
80 * @param Site $site
81 * @covers \MediaWiki\Site\Site::getLanguageCode
83 public function testGetLanguageCode( Site $site ) {
84 $this->assertThat(
85 $site->getLanguageCode(),
86 $this->logicalOr( $this->isNull(), $this->isType( 'string' ) )
90 /**
91 * @dataProvider instanceProvider
92 * @param Site $site
93 * @covers \MediaWiki\Site\Site::setLanguageCode
95 public function testSetLanguageCode( Site $site ) {
96 $site->setLanguageCode( 'en' );
97 $this->assertEquals( 'en', $site->getLanguageCode() );
101 * @dataProvider instanceProvider
102 * @param Site $site
103 * @covers \MediaWiki\Site\Site::normalizePageName
105 public function testNormalizePageName( Site $site ) {
106 $this->assertIsString( $site->normalizePageName( 'Foobar' ) );
110 * @dataProvider instanceProvider
111 * @param Site $site
112 * @covers \MediaWiki\Site\Site::getGlobalId
114 public function testGetGlobalId( Site $site ) {
115 $this->assertThat(
116 $site->getGlobalId(),
117 $this->logicalOr( $this->isNull(), $this->isType( 'string' ) )
122 * @dataProvider instanceProvider
123 * @param Site $site
124 * @covers \MediaWiki\Site\Site::setGlobalId
126 public function testSetGlobalId( Site $site ) {
127 $site->setGlobalId( 'foobar' );
128 $this->assertEquals( 'foobar', $site->getGlobalId() );
132 * @dataProvider instanceProvider
133 * @param Site $site
134 * @covers \MediaWiki\Site\Site::getType
136 public function testGetType( Site $site ) {
137 $this->assertIsString( $site->getType() );
141 * @dataProvider instanceProvider
142 * @param Site $site
143 * @covers \MediaWiki\Site\Site::getPath
145 public function testGetPath( Site $site ) {
146 $this->assertThat(
147 $site->getPath( 'page_path' ),
148 $this->logicalOr( $this->isNull(), $this->isType( 'string' ) )
150 $this->assertThat(
151 $site->getPath( 'file_path' ),
152 $this->logicalOr( $this->isNull(), $this->isType( 'string' ) )
154 $this->assertThat(
155 $site->getPath( 'foobar' ),
156 $this->logicalOr( $this->isNull(), $this->isType( 'string' ) )
161 * @dataProvider instanceProvider
162 * @param Site $site
163 * @covers \MediaWiki\Site\Site::getAllPaths
165 public function testGetAllPaths( Site $site ) {
166 $this->assertIsArray( $site->getAllPaths() );
170 * @dataProvider instanceProvider
171 * @param Site $site
172 * @covers \MediaWiki\Site\Site::setPath
173 * @covers \MediaWiki\Site\Site::removePath
175 public function testSetAndRemovePath( Site $site ) {
176 $count = count( $site->getAllPaths() );
178 $site->setPath( 'spam', 'http://www.wikidata.org/$1' );
179 $site->setPath( 'spam', 'http://www.wikidata.org/foo/$1' );
180 $site->setPath( 'foobar', 'http://www.wikidata.org/bar/$1' );
182 $this->assertCount( $count + 2, $site->getAllPaths() );
184 $this->assertIsString( $site->getPath( 'foobar' ) );
185 $this->assertEquals( 'http://www.wikidata.org/foo/$1', $site->getPath( 'spam' ) );
187 $site->removePath( 'spam' );
188 $site->removePath( 'foobar' );
190 $this->assertCount( $count, $site->getAllPaths() );
192 $this->assertNull( $site->getPath( 'foobar' ) );
193 $this->assertNull( $site->getPath( 'spam' ) );
197 * @covers \MediaWiki\Site\Site::setLinkPath
199 public function testSetLinkPath() {
200 $site = new Site();
201 $path = "TestPath/$1";
203 $site->setLinkPath( $path );
204 $this->assertEquals( $path, $site->getLinkPath() );
208 * @covers \MediaWiki\Site\Site::getLinkPathType
210 public function testGetLinkPathType() {
211 $site = new Site();
213 $path = 'TestPath/$1';
214 $site->setLinkPath( $path );
215 $this->assertEquals( $path, $site->getPath( $site->getLinkPathType() ) );
217 $path = 'AnotherPath/$1';
218 $site->setPath( $site->getLinkPathType(), $path );
219 $this->assertEquals( $path, $site->getLinkPath() );
223 * @covers \MediaWiki\Site\Site::setPath
225 public function testSetPath() {
226 $site = new Site();
228 $path = 'TestPath/$1';
229 $site->setPath( 'foo', $path );
231 $this->assertEquals( $path, $site->getPath( 'foo' ) );
235 * @covers \MediaWiki\Site\Site::setPath
236 * @covers \MediaWiki\Site\Site::getProtocol
238 public function testProtocolRelativePath() {
239 $site = new Site();
241 $type = $site->getLinkPathType();
242 $path = '//acme.com/'; // protocol-relative URL
243 $site->setPath( $type, $path );
245 $this->assertSame( '', $site->getProtocol() );
248 public static function provideGetPageUrl() {
249 // NOTE: the assumption that the URL is built by replacing $1
250 // with the urlencoded version of $page
251 // is true for Site but not guaranteed for subclasses.
252 // Subclasses need to override this provider appropriately.
254 return [
255 [ # 0
256 'http://acme.test/TestPath/$1',
257 'Foo',
258 '/TestPath/Foo',
260 [ # 1
261 'http://acme.test/TestScript?x=$1&y=bla',
262 'Foo',
263 'TestScript?x=Foo&y=bla',
265 [ # 2
266 'http://acme.test/TestPath/$1',
267 'foo & bar/xyzzy (quux-shmoox?)',
268 '/TestPath/foo%20%26%20bar%2Fxyzzy%20%28quux-shmoox%3F%29',
274 * @dataProvider provideGetPageUrl
275 * @covers \MediaWiki\Site\Site::getPageUrl
277 public function testGetPageUrl( $path, $page, $expected ) {
278 $site = new Site();
280 // NOTE: the assumption that getPageUrl is based on getLinkPath
281 // is true for Site but not guaranteed for subclasses.
282 // Subclasses need to override this test case appropriately.
283 $site->setLinkPath( $path );
284 $this->assertStringContainsString( $path, $site->getPageUrl() );
286 $this->assertStringContainsString( $expected, $site->getPageUrl( $page ) );
290 * @dataProvider instanceProvider
291 * @param Site $site
292 * @covers \MediaWiki\Site\Site::__serialize
293 * @covers \MediaWiki\Site\Site::__unserialize
295 public function testSerialization( Site $site ) {
296 $serialization = serialize( $site );
297 $newInstance = unserialize( $serialization );
299 $this->assertInstanceOf( Site::class, $newInstance );
301 $this->assertEquals( $serialization, serialize( $newInstance ) );