Debian Codegraph @

tests/phpunit/includes/site/SiteTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Site;
4
5 use MediaWiki\Site\Site;
6 use MediaWikiIntegrationTestCase;
7
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.
13 *
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.
18 *
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
23 *
24 * @file
25 * @since 1.21
26 *
27 * @ingroup Site
28 * @ingroup Test
29 *
30 * @group Site
31 *
32 * @author Jeroen De Dauw < [email protected] >
33 */
34 class SiteTest extends MediaWikiIntegrationTestCase {
35
36 public function instanceProvider() {
37 return $this->arrayWrap( TestSites::getSites() );
38 }
39
40 /**
41 * @dataProvider instanceProvider
42 * @param Site $site
43 * @covers \MediaWiki\Site\Site::getInterwikiIds
44 */
45 public function testGetInterwikiIds( Site $site ) {
46 $this->assertIsArray( $site->getInterwikiIds() );
47 }
48
49 /**
50 * @dataProvider instanceProvider
51 * @param Site $site
52 * @covers \MediaWiki\Site\Site::getNavigationIds
53 */
54 public function testGetNavigationIds( Site $site ) {
55 $this->assertIsArray( $site->getNavigationIds() );
56 }
57
58 /**
59 * @dataProvider instanceProvider
60 * @param Site $site
61 * @covers \MediaWiki\Site\Site::addNavigationId
62 */
63 public function testAddNavigationId( Site $site ) {
64 $site->addNavigationId( 'foobar' );
65 $this->assertContains( 'foobar', $site->getNavigationIds() );
66 }
67
68 /**
69 * @dataProvider instanceProvider
70 * @param Site $site
71 * @covers \MediaWiki\Site\Site::addInterwikiId
72 */
73 public function testAddInterwikiId( Site $site ) {
74 $site->addInterwikiId( 'foobar' );
75 $this->assertContains( 'foobar', $site->getInterwikiIds() );
76 }
77
78 /**
79 * @dataProvider instanceProvider
80 * @param Site $site
81 * @covers \MediaWiki\Site\Site::getLanguageCode
82 */
83 public function testGetLanguageCode( Site $site ) {
84 $this->assertThat(
85 $site->getLanguageCode(),
86 $this->logicalOr( $this->isNull(), $this->isType( 'string' ) )
87 );
88 }
89
90 /**
91 * @dataProvider instanceProvider
92 * @param Site $site
93 * @covers \MediaWiki\Site\Site::setLanguageCode
94 */
95 public function testSetLanguageCode( Site $site ) {
96 $site->setLanguageCode( 'en' );
97 $this->assertEquals( 'en', $site->getLanguageCode() );
98 }
99
100 /**
101 * @dataProvider instanceProvider
102 * @param Site $site
103 * @covers \MediaWiki\Site\Site::normalizePageName
104 */
105 public function testNormalizePageName( Site $site ) {
106 $this->assertIsString( $site->normalizePageName( 'Foobar' ) );
107 }
108
109 /**
110 * @dataProvider instanceProvider
111 * @param Site $site
112 * @covers \MediaWiki\Site\Site::getGlobalId
113 */
114 public function testGetGlobalId( Site $site ) {
115 $this->assertThat(
116 $site->getGlobalId(),
117 $this->logicalOr( $this->isNull(), $this->isType( 'string' ) )
118 );
119 }
120
121 /**
122 * @dataProvider instanceProvider
123 * @param Site $site
124 * @covers \MediaWiki\Site\Site::setGlobalId
125 */
126 public function testSetGlobalId( Site $site ) {
127 $site->setGlobalId( 'foobar' );
128 $this->assertEquals( 'foobar', $site->getGlobalId() );
129 }
130
131 /**
132 * @dataProvider instanceProvider
133 * @param Site $site
134 * @covers \MediaWiki\Site\Site::getType
135 */
136 public function testGetType( Site $site ) {
137 $this->assertIsString( $site->getType() );
138 }
139
140 /**
141 * @dataProvider instanceProvider
142 * @param Site $site
143 * @covers \MediaWiki\Site\Site::getPath
144 */
145 public function testGetPath( Site $site ) {
146 $this->assertThat(
147 $site->getPath( 'page_path' ),
148 $this->logicalOr( $this->isNull(), $this->isType( 'string' ) )
149 );
150 $this->assertThat(
151 $site->getPath( 'file_path' ),
152 $this->logicalOr( $this->isNull(), $this->isType( 'string' ) )
153 );
154 $this->assertThat(
155 $site->getPath( 'foobar' ),
156 $this->logicalOr( $this->isNull(), $this->isType( 'string' ) )
157 );
158 }
159
160 /**
161 * @dataProvider instanceProvider
162 * @param Site $site
163 * @covers \MediaWiki\Site\Site::getAllPaths
164 */
165 public function testGetAllPaths( Site $site ) {
166 $this->assertIsArray( $site->getAllPaths() );
167 }
168
169 /**
170 * @dataProvider instanceProvider
171 * @param Site $site
172 * @covers \MediaWiki\Site\Site::setPath
173 * @covers \MediaWiki\Site\Site::removePath
174 */
175 public function testSetAndRemovePath( Site $site ) {
176 $count = count( $site->getAllPaths() );
177
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' );
181
182 $this->assertCount( $count + 2, $site->getAllPaths() );
183
184 $this->assertIsString( $site->getPath( 'foobar' ) );
185 $this->assertEquals( 'http://www.wikidata.org/foo/$1', $site->getPath( 'spam' ) );
186
187 $site->removePath( 'spam' );
188 $site->removePath( 'foobar' );
189
190 $this->assertCount( $count, $site->getAllPaths() );
191
192 $this->assertNull( $site->getPath( 'foobar' ) );
193 $this->assertNull( $site->getPath( 'spam' ) );
194 }
195
196 /**
197 * @covers \MediaWiki\Site\Site::setLinkPath
198 */
199 public function testSetLinkPath() {
200 $site = new Site();
201 $path = "TestPath/$1";
202
203 $site->setLinkPath( $path );
204 $this->assertEquals( $path, $site->getLinkPath() );
205 }
206
207 /**
208 * @covers \MediaWiki\Site\Site::getLinkPathType
209 */
210 public function testGetLinkPathType() {
211 $site = new Site();
212
213 $path = 'TestPath/$1';
214 $site->setLinkPath( $path );
215 $this->assertEquals( $path, $site->getPath( $site->getLinkPathType() ) );
216
217 $path = 'AnotherPath/$1';
218 $site->setPath( $site->getLinkPathType(), $path );
219 $this->assertEquals( $path, $site->getLinkPath() );
220 }
221
222 /**
223 * @covers \MediaWiki\Site\Site::setPath
224 */
225 public function testSetPath() {
226 $site = new Site();
227
228 $path = 'TestPath/$1';
229 $site->setPath( 'foo', $path );
230
231 $this->assertEquals( $path, $site->getPath( 'foo' ) );
232 }
233
234 /**
235 * @covers \MediaWiki\Site\Site::setPath
236 * @covers \MediaWiki\Site\Site::getProtocol
237 */
238 public function testProtocolRelativePath() {
239 $site = new Site();
240
241 $type = $site->getLinkPathType();
242 $path = '//acme.com/'; // protocol-relative URL
243 $site->setPath( $type, $path );
244
245 $this->assertSame( '', $site->getProtocol() );
246 }
247
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.
253
254 return [
255 [ # 0
256 'http://acme.test/TestPath/$1',
257 'Foo',
258 '/TestPath/Foo',
259 ],
260 [ # 1
261 'http://acme.test/TestScript?x=$1&y=bla',
262 'Foo',
263 'TestScript?x=Foo&y=bla',
264 ],
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',
269 ],
270 ];
271 }
272
273 /**
274 * @dataProvider provideGetPageUrl
275 * @covers \MediaWiki\Site\Site::getPageUrl
276 */
277 public function testGetPageUrl( $path, $page, $expected ) {
278 $site = new Site();
279
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() );
285
286 $this->assertStringContainsString( $expected, $site->getPageUrl( $page ) );
287 }
288
289 /**
290 * @dataProvider instanceProvider
291 * @param Site $site
292 * @covers \MediaWiki\Site\Site::__serialize
293 * @covers \MediaWiki\Site\Site::__unserialize
294 */
295 public function testSerialization( Site $site ) {
296 $serialization = serialize( $site );
297 $newInstance = unserialize( $serialization );
298
299 $this->assertInstanceOf( Site::class, $newInstance );
300
301 $this->assertEquals( $serialization, serialize( $newInstance ) );
302 }
303 }