Merge "FormatJson: Remove whitespace from empty arrays and objects"
[lhc/web/wiklou.git] / tests / phpunit / includes / site / SiteTest.php
1 <?php
2
3 /**
4 * Tests for the Site class.
5 *
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.
10 *
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.
15 *
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
20 *
21 * @file
22 * @since 1.21
23 *
24 * @ingroup Site
25 * @ingroup Test
26 *
27 * @group Site
28 *
29 * @licence GNU GPL v2+
30 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
31 */
32 class SiteTest extends MediaWikiTestCase {
33
34 public function instanceProvider() {
35 return $this->arrayWrap( TestSites::getSites() );
36 }
37
38 /**
39 * @dataProvider instanceProvider
40 * @param Site $site
41 * @covers Site::getInterwikiIds
42 */
43 public function testGetInterwikiIds( Site $site ) {
44 $this->assertInternalType( 'array', $site->getInterwikiIds() );
45 }
46
47 /**
48 * @dataProvider instanceProvider
49 * @param Site $site
50 * @covers Site::getNavigationIds
51 */
52 public function testGetNavigationIds( Site $site ) {
53 $this->assertInternalType( 'array', $site->getNavigationIds() );
54 }
55
56 /**
57 * @dataProvider instanceProvider
58 * @param Site $site
59 * @covers Site::addNavigationId
60 */
61 public function testAddNavigationId( Site $site ) {
62 $site->addNavigationId( 'foobar' );
63 $this->assertTrue( in_array( 'foobar', $site->getNavigationIds(), true ) );
64 }
65
66 /**
67 * @dataProvider instanceProvider
68 * @param Site $site
69 * @covers Site::addInterwikiId
70 */
71 public function testAddInterwikiId( Site $site ) {
72 $site->addInterwikiId( 'foobar' );
73 $this->assertTrue( in_array( 'foobar', $site->getInterwikiIds(), true ) );
74 }
75
76 /**
77 * @dataProvider instanceProvider
78 * @param Site $site
79 * @covers Site::getLanguageCode
80 */
81 public function testGetLanguageCode( Site $site ) {
82 $this->assertTypeOrValue( 'string', $site->getLanguageCode(), null );
83 }
84
85 /**
86 * @dataProvider instanceProvider
87 * @param Site $site
88 * @covers Site::setLanguageCode
89 */
90 public function testSetLanguageCode( Site $site ) {
91 $site->setLanguageCode( 'en' );
92 $this->assertEquals( 'en', $site->getLanguageCode() );
93 }
94
95 /**
96 * @dataProvider instanceProvider
97 * @param Site $site
98 * @covers Site::normalizePageName
99 */
100 public function testNormalizePageName( Site $site ) {
101 $this->assertInternalType( 'string', $site->normalizePageName( 'Foobar' ) );
102 }
103
104 /**
105 * @dataProvider instanceProvider
106 * @param Site $site
107 * @covers Site::getGlobalId
108 */
109 public function testGetGlobalId( Site $site ) {
110 $this->assertTypeOrValue( 'string', $site->getGlobalId(), null );
111 }
112
113 /**
114 * @dataProvider instanceProvider
115 * @param Site $site
116 * @covers Site::setGlobalId
117 */
118 public function testSetGlobalId( Site $site ) {
119 $site->setGlobalId( 'foobar' );
120 $this->assertEquals( 'foobar', $site->getGlobalId() );
121 }
122
123 /**
124 * @dataProvider instanceProvider
125 * @param Site $site
126 * @covers Site::getType
127 */
128 public function testGetType( Site $site ) {
129 $this->assertInternalType( 'string', $site->getType() );
130 }
131
132 /**
133 * @dataProvider instanceProvider
134 * @param Site $site
135 * @covers Site::getPath
136 */
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 );
141 }
142
143 /**
144 * @dataProvider instanceProvider
145 * @param Site $site
146 * @covers Site::getAllPaths
147 */
148 public function testGetAllPaths( Site $site ) {
149 $this->assertInternalType( 'array', $site->getAllPaths() );
150 }
151
152 /**
153 * @dataProvider instanceProvider
154 * @param Site $site
155 * @covers Site::setPath
156 * @covers Site::removePath
157 */
158 public function testSetAndRemovePath( Site $site ) {
159 $count = count( $site->getAllPaths() );
160
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' );
164
165 $this->assertEquals( $count + 2, count( $site->getAllPaths() ) );
166
167 $this->assertInternalType( 'string', $site->getPath( 'foobar' ) );
168 $this->assertEquals( 'http://www.wikidata.org/foo/$1', $site->getPath( 'spam' ) );
169
170 $site->removePath( 'spam' );
171 $site->removePath( 'foobar' );
172
173 $this->assertEquals( $count, count( $site->getAllPaths() ) );
174
175 $this->assertNull( $site->getPath( 'foobar' ) );
176 $this->assertNull( $site->getPath( 'spam' ) );
177 }
178
179 /**
180 * @covers Site::setLinkPath
181 */
182 public function testSetLinkPath() {
183 $site = new Site();
184 $path = "TestPath/$1";
185
186 $site->setLinkPath( $path );
187 $this->assertEquals( $path, $site->getLinkPath() );
188 }
189
190 /**
191 * @covers Site::getLinkPathType
192 */
193 public function testGetLinkPathType() {
194 $site = new Site();
195
196 $path = 'TestPath/$1';
197 $site->setLinkPath( $path );
198 $this->assertEquals( $path, $site->getPath( $site->getLinkPathType() ) );
199
200 $path = 'AnotherPath/$1';
201 $site->setPath( $site->getLinkPathType(), $path );
202 $this->assertEquals( $path, $site->getLinkPath() );
203 }
204
205 /**
206 * @covers Site::setPath
207 */
208 public function testSetPath() {
209 $site = new Site();
210
211 $path = 'TestPath/$1';
212 $site->setPath( 'foo', $path );
213
214 $this->assertEquals( $path, $site->getPath( 'foo' ) );
215 }
216
217 /**
218 * @covers Site::setPath
219 * @covers Site::getProtocol
220 */
221 public function testProtocolRelativePath() {
222 $site = new Site();
223
224 $type = $site->getLinkPathType();
225 $path = '//acme.com/'; // protocol-relative URL
226 $site->setPath( $type, $path );
227
228 $this->assertEquals( '', $site->getProtocol() );
229 }
230
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.
236
237 return array(
238 array( #0
239 'http://acme.test/TestPath/$1',
240 'Foo',
241 '/TestPath/Foo',
242 ),
243 array( #1
244 'http://acme.test/TestScript?x=$1&y=bla',
245 'Foo',
246 'TestScript?x=Foo&y=bla',
247 ),
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',
252 ),
253 );
254 }
255
256 /**
257 * @dataProvider provideGetPageUrl
258 * @covers Site::getPageUrl
259 */
260 public function testGetPageUrl( $path, $page, $expected ) {
261 $site = new Site();
262
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() );
268
269 $this->assertContains( $expected, $site->getPageUrl( $page ) );
270 }
271
272 protected function assertTypeOrFalse( $type, $value ) {
273 if ( $value === false ) {
274 $this->assertTrue( true );
275 } else {
276 $this->assertInternalType( $type, $value );
277 }
278 }
279
280 /**
281 * @dataProvider instanceProvider
282 * @param Site $site
283 * @covers Site::serialize
284 * @covers Site::unserialize
285 */
286 public function testSerialization( Site $site ) {
287 $this->assertInstanceOf( 'Serializable', $site );
288
289 $serialization = serialize( $site );
290 $newInstance = unserialize( $serialization );
291
292 $this->assertInstanceOf( 'Site', $newInstance );
293
294 $this->assertEquals( $serialization, serialize( $newInstance ) );
295 }
296 }