Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / tests / phpunit / includes / site / SiteTest.php
blob59f046b2ef6530705d33709cacc4179cf96392eb
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 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
31 class SiteTest extends MediaWikiTestCase {
33 public function instanceProvider() {
34 return $this->arrayWrap( TestSites::getSites() );
37 /**
38 * @dataProvider instanceProvider
39 * @param Site $site
40 * @covers Site::getInterwikiIds
42 public function testGetInterwikiIds( Site $site ) {
43 $this->assertInternalType( 'array', $site->getInterwikiIds() );
46 /**
47 * @dataProvider instanceProvider
48 * @param Site $site
49 * @covers Site::getNavigationIds
51 public function testGetNavigationIds( Site $site ) {
52 $this->assertInternalType( 'array', $site->getNavigationIds() );
55 /**
56 * @dataProvider instanceProvider
57 * @param Site $site
58 * @covers Site::addNavigationId
60 public function testAddNavigationId( Site $site ) {
61 $site->addNavigationId( 'foobar' );
62 $this->assertTrue( in_array( 'foobar', $site->getNavigationIds(), true ) );
65 /**
66 * @dataProvider instanceProvider
67 * @param Site $site
68 * @covers Site::addInterwikiId
70 public function testAddInterwikiId( Site $site ) {
71 $site->addInterwikiId( 'foobar' );
72 $this->assertTrue( in_array( 'foobar', $site->getInterwikiIds(), true ) );
75 /**
76 * @dataProvider instanceProvider
77 * @param Site $site
78 * @covers Site::getLanguageCode
80 public function testGetLanguageCode( Site $site ) {
81 $this->assertTypeOrValue( 'string', $site->getLanguageCode(), null );
84 /**
85 * @dataProvider instanceProvider
86 * @param Site $site
87 * @covers Site::setLanguageCode
89 public function testSetLanguageCode( Site $site ) {
90 $site->setLanguageCode( 'en' );
91 $this->assertEquals( 'en', $site->getLanguageCode() );
94 /**
95 * @dataProvider instanceProvider
96 * @param Site $site
97 * @covers Site::normalizePageName
99 public function testNormalizePageName( Site $site ) {
100 $this->assertInternalType( 'string', $site->normalizePageName( 'Foobar' ) );
104 * @dataProvider instanceProvider
105 * @param Site $site
106 * @covers Site::getGlobalId
108 public function testGetGlobalId( Site $site ) {
109 $this->assertTypeOrValue( 'string', $site->getGlobalId(), null );
113 * @dataProvider instanceProvider
114 * @param Site $site
115 * @covers Site::setGlobalId
117 public function testSetGlobalId( Site $site ) {
118 $site->setGlobalId( 'foobar' );
119 $this->assertEquals( 'foobar', $site->getGlobalId() );
123 * @dataProvider instanceProvider
124 * @param Site $site
125 * @covers Site::getType
127 public function testGetType( Site $site ) {
128 $this->assertInternalType( 'string', $site->getType() );
132 * @dataProvider instanceProvider
133 * @param Site $site
134 * @covers Site::getPath
136 public function testGetPath( Site $site ) {
137 $this->assertTypeOrValue( 'string', $site->getPath( 'page_path' ), null );
138 $this->assertTypeOrValue( 'string', $site->getPath( 'file_path' ), null );
139 $this->assertTypeOrValue( 'string', $site->getPath( 'foobar' ), null );
143 * @dataProvider instanceProvider
144 * @param Site $site
145 * @covers Site::getAllPaths
147 public function testGetAllPaths( Site $site ) {
148 $this->assertInternalType( 'array', $site->getAllPaths() );
152 * @dataProvider instanceProvider
153 * @param Site $site
154 * @covers Site::setPath
155 * @covers Site::removePath
157 public function testSetAndRemovePath( Site $site ) {
158 $count = count( $site->getAllPaths() );
160 $site->setPath( 'spam', 'http://www.wikidata.org/$1' );
161 $site->setPath( 'spam', 'http://www.wikidata.org/foo/$1' );
162 $site->setPath( 'foobar', 'http://www.wikidata.org/bar/$1' );
164 $this->assertEquals( $count + 2, count( $site->getAllPaths() ) );
166 $this->assertInternalType( 'string', $site->getPath( 'foobar' ) );
167 $this->assertEquals( 'http://www.wikidata.org/foo/$1', $site->getPath( 'spam' ) );
169 $site->removePath( 'spam' );
170 $site->removePath( 'foobar' );
172 $this->assertEquals( $count, count( $site->getAllPaths() ) );
174 $this->assertNull( $site->getPath( 'foobar' ) );
175 $this->assertNull( $site->getPath( 'spam' ) );
179 * @covers Site::setLinkPath
181 public function testSetLinkPath() {
182 $site = new Site();
183 $path = "TestPath/$1";
185 $site->setLinkPath( $path );
186 $this->assertEquals( $path, $site->getLinkPath() );
190 * @covers Site::getLinkPathType
192 public function testGetLinkPathType() {
193 $site = new Site();
195 $path = 'TestPath/$1';
196 $site->setLinkPath( $path );
197 $this->assertEquals( $path, $site->getPath( $site->getLinkPathType() ) );
199 $path = 'AnotherPath/$1';
200 $site->setPath( $site->getLinkPathType(), $path );
201 $this->assertEquals( $path, $site->getLinkPath() );
205 * @covers Site::setPath
207 public function testSetPath() {
208 $site = new Site();
210 $path = 'TestPath/$1';
211 $site->setPath( 'foo', $path );
213 $this->assertEquals( $path, $site->getPath( 'foo' ) );
217 * @covers Site::setPath
218 * @covers Site::getProtocol
220 public function testProtocolRelativePath() {
221 $site = new Site();
223 $type = $site->getLinkPathType();
224 $path = '//acme.com/'; // protocol-relative URL
225 $site->setPath( $type, $path );
227 $this->assertEquals( '', $site->getProtocol() );
230 public static function provideGetPageUrl() {
231 // NOTE: the assumption that the URL is built by replacing $1
232 // with the urlencoded version of $page
233 // is true for Site but not guaranteed for subclasses.
234 // Subclasses need to override this provider appropriately.
236 return [
237 [ # 0
238 'http://acme.test/TestPath/$1',
239 'Foo',
240 '/TestPath/Foo',
242 [ # 1
243 'http://acme.test/TestScript?x=$1&y=bla',
244 'Foo',
245 'TestScript?x=Foo&y=bla',
247 [ # 2
248 'http://acme.test/TestPath/$1',
249 'foo & bar/xyzzy (quux-shmoox?)',
250 '/TestPath/foo%20%26%20bar%2Fxyzzy%20%28quux-shmoox%3F%29',
256 * @dataProvider provideGetPageUrl
257 * @covers Site::getPageUrl
259 public function testGetPageUrl( $path, $page, $expected ) {
260 $site = new Site();
262 // NOTE: the assumption that getPageUrl is based on getLinkPath
263 // is true for Site but not guaranteed for subclasses.
264 // Subclasses need to override this test case appropriately.
265 $site->setLinkPath( $path );
266 $this->assertContains( $path, $site->getPageUrl() );
268 $this->assertContains( $expected, $site->getPageUrl( $page ) );
271 protected function assertTypeOrFalse( $type, $value ) {
272 if ( $value === false ) {
273 $this->assertTrue( true );
274 } else {
275 $this->assertInternalType( $type, $value );
280 * @dataProvider instanceProvider
281 * @param Site $site
282 * @covers Site::serialize
283 * @covers Site::unserialize
285 public function testSerialization( Site $site ) {
286 $this->assertInstanceOf( 'Serializable', $site );
288 $serialization = serialize( $site );
289 $newInstance = unserialize( $serialization );
291 $this->assertInstanceOf( 'Site', $newInstance );
293 $this->assertEquals( $serialization, serialize( $newInstance ) );