Pass phpcs-strict on some test files (11/11)
[mediawiki.git] / tests / phpunit / includes / site / SiteTest.php
blob29c1ff330860185be79c63bd6f7a74cb3c27689e
1 <?php
3 /**
4 * Tests for the Site class.
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.
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.
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
21 * @file
22 * @since 1.21
24 * @ingroup Site
25 * @ingroup Test
27 * @group Site
29 * @licence GNU GPL v2+
30 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
32 class SiteTest extends MediaWikiTestCase {
34 public function instanceProvider() {
35 return $this->arrayWrap( TestSites::getSites() );
38 /**
39 * @dataProvider instanceProvider
40 * @param Site $site
41 * @covers Site::getInterwikiIds
43 public function testGetInterwikiIds( Site $site ) {
44 $this->assertInternalType( 'array', $site->getInterwikiIds() );
47 /**
48 * @dataProvider instanceProvider
49 * @param Site $site
50 * @covers Site::getNavigationIds
52 public function testGetNavigationIds( Site $site ) {
53 $this->assertInternalType( 'array', $site->getNavigationIds() );
56 /**
57 * @dataProvider instanceProvider
58 * @param Site $site
59 * @covers Site::addNavigationId
61 public function testAddNavigationId( Site $site ) {
62 $site->addNavigationId( 'foobar' );
63 $this->assertTrue( in_array( 'foobar', $site->getNavigationIds(), true ) );
66 /**
67 * @dataProvider instanceProvider
68 * @param Site $site
69 * @covers Site::addInterwikiId
71 public function testAddInterwikiId( Site $site ) {
72 $site->addInterwikiId( 'foobar' );
73 $this->assertTrue( in_array( 'foobar', $site->getInterwikiIds(), true ) );
76 /**
77 * @dataProvider instanceProvider
78 * @param Site $site
79 * @covers Site::getLanguageCode
81 public function testGetLanguageCode( Site $site ) {
82 $this->assertTypeOrValue( 'string', $site->getLanguageCode(), null );
85 /**
86 * @dataProvider instanceProvider
87 * @param Site $site
88 * @covers Site::setLanguageCode
90 public function testSetLanguageCode( Site $site ) {
91 $site->setLanguageCode( 'en' );
92 $this->assertEquals( 'en', $site->getLanguageCode() );
95 /**
96 * @dataProvider instanceProvider
97 * @param Site $site
98 * @covers Site::normalizePageName
100 public function testNormalizePageName( Site $site ) {
101 $this->assertInternalType( 'string', $site->normalizePageName( 'Foobar' ) );
105 * @dataProvider instanceProvider
106 * @param Site $site
107 * @covers Site::getGlobalId
109 public function testGetGlobalId( Site $site ) {
110 $this->assertTypeOrValue( 'string', $site->getGlobalId(), null );
114 * @dataProvider instanceProvider
115 * @param Site $site
116 * @covers Site::setGlobalId
118 public function testSetGlobalId( Site $site ) {
119 $site->setGlobalId( 'foobar' );
120 $this->assertEquals( 'foobar', $site->getGlobalId() );
124 * @dataProvider instanceProvider
125 * @param Site $site
126 * @covers Site::getType
128 public function testGetType( Site $site ) {
129 $this->assertInternalType( 'string', $site->getType() );
133 * @dataProvider instanceProvider
134 * @param Site $site
135 * @covers Site::getPath
137 public function testGetPath( Site $site ) {
138 $this->assertTypeOrValue( 'string', $site->getPath( 'page_path' ), null );
139 $this->assertTypeOrValue( 'string', $site->getPath( 'file_path' ), null );
140 $this->assertTypeOrValue( 'string', $site->getPath( 'foobar' ), null );
144 * @dataProvider instanceProvider
145 * @param Site $site
146 * @covers Site::getAllPaths
148 public function testGetAllPaths( Site $site ) {
149 $this->assertInternalType( 'array', $site->getAllPaths() );
153 * @dataProvider instanceProvider
154 * @param Site $site
155 * @covers Site::setPath
156 * @covers Site::removePath
158 public function testSetAndRemovePath( Site $site ) {
159 $count = count( $site->getAllPaths() );
161 $site->setPath( 'spam', 'http://www.wikidata.org/$1' );
162 $site->setPath( 'spam', 'http://www.wikidata.org/foo/$1' );
163 $site->setPath( 'foobar', 'http://www.wikidata.org/bar/$1' );
165 $this->assertEquals( $count + 2, count( $site->getAllPaths() ) );
167 $this->assertInternalType( 'string', $site->getPath( 'foobar' ) );
168 $this->assertEquals( 'http://www.wikidata.org/foo/$1', $site->getPath( 'spam' ) );
170 $site->removePath( 'spam' );
171 $site->removePath( 'foobar' );
173 $this->assertEquals( $count, count( $site->getAllPaths() ) );
175 $this->assertNull( $site->getPath( 'foobar' ) );
176 $this->assertNull( $site->getPath( 'spam' ) );
180 * @covers Site::setLinkPath
182 public function testSetLinkPath() {
183 $site = new Site();
184 $path = "TestPath/$1";
186 $site->setLinkPath( $path );
187 $this->assertEquals( $path, $site->getLinkPath() );
191 * @covers Site::getLinkPathType
193 public function testGetLinkPathType() {
194 $site = new Site();
196 $path = 'TestPath/$1';
197 $site->setLinkPath( $path );
198 $this->assertEquals( $path, $site->getPath( $site->getLinkPathType() ) );
200 $path = 'AnotherPath/$1';
201 $site->setPath( $site->getLinkPathType(), $path );
202 $this->assertEquals( $path, $site->getLinkPath() );
206 * @covers Site::setPath
208 public function testSetPath() {
209 $site = new Site();
211 $path = 'TestPath/$1';
212 $site->setPath( 'foo', $path );
214 $this->assertEquals( $path, $site->getPath( 'foo' ) );
218 * @covers Site::setPath
219 * @covers Site::getProtocol
221 public function testProtocolRelativePath() {
222 $site = new Site();
224 $type = $site->getLinkPathType();
225 $path = '//acme.com/'; // protocol-relative URL
226 $site->setPath( $type, $path );
228 $this->assertEquals( '', $site->getProtocol() );
231 public static function provideGetPageUrl() {
232 //NOTE: the assumption that the URL is built by replacing $1
233 // with the urlencoded version of $page
234 // is true for Site but not guaranteed for subclasses.
235 // Subclasses need to override this provider appropriately.
237 return array(
238 array( #0
239 'http://acme.test/TestPath/$1',
240 'Foo',
241 '/TestPath/Foo',
243 array( #1
244 'http://acme.test/TestScript?x=$1&y=bla',
245 'Foo',
246 'TestScript?x=Foo&y=bla',
248 array( #2
249 'http://acme.test/TestPath/$1',
250 'foo & bar/xyzzy (quux-shmoox?)',
251 '/TestPath/foo%20%26%20bar%2Fxyzzy%20%28quux-shmoox%3F%29',
257 * @dataProvider provideGetPageUrl
258 * @covers Site::getPageUrl
260 public function testGetPageUrl( $path, $page, $expected ) {
261 $site = new Site();
263 //NOTE: the assumption that getPageUrl is based on getLinkPath
264 // is true for Site but not guaranteed for subclasses.
265 // Subclasses need to override this test case appropriately.
266 $site->setLinkPath( $path );
267 $this->assertContains( $path, $site->getPageUrl() );
269 $this->assertContains( $expected, $site->getPageUrl( $page ) );
272 protected function assertTypeOrFalse( $type, $value ) {
273 if ( $value === false ) {
274 $this->assertTrue( true );
275 } else {
276 $this->assertInternalType( $type, $value );
281 * @dataProvider instanceProvider
282 * @param Site $site
283 * @covers Site::serialize
284 * @covers Site::unserialize
286 public function testSerialization( Site $site ) {
287 $this->assertInstanceOf( 'Serializable', $site );
289 $serialization = serialize( $site );
290 $newInstance = unserialize( $serialization );
292 $this->assertInstanceOf( 'Site', $newInstance );
294 $this->assertEquals( $serialization, serialize( $newInstance ) );