MediaWiki REL1_31
SiteTest.php
Go to the documentation of this file.
1<?php
2
32
33 public function instanceProvider() {
34 return $this->arrayWrap( TestSites::getSites() );
35 }
36
42 public function testGetInterwikiIds( Site $site ) {
43 $this->assertInternalType( 'array', $site->getInterwikiIds() );
44 }
45
51 public function testGetNavigationIds( Site $site ) {
52 $this->assertInternalType( 'array', $site->getNavigationIds() );
53 }
54
60 public function testAddNavigationId( Site $site ) {
61 $site->addNavigationId( 'foobar' );
62 $this->assertTrue( in_array( 'foobar', $site->getNavigationIds(), true ) );
63 }
64
70 public function testAddInterwikiId( Site $site ) {
71 $site->addInterwikiId( 'foobar' );
72 $this->assertTrue( in_array( 'foobar', $site->getInterwikiIds(), true ) );
73 }
74
80 public function testGetLanguageCode( Site $site ) {
81 $this->assertTypeOrValue( 'string', $site->getLanguageCode(), null );
82 }
83
89 public function testSetLanguageCode( Site $site ) {
90 $site->setLanguageCode( 'en' );
91 $this->assertEquals( 'en', $site->getLanguageCode() );
92 }
93
99 public function testNormalizePageName( Site $site ) {
100 $this->assertInternalType( 'string', $site->normalizePageName( 'Foobar' ) );
101 }
102
108 public function testGetGlobalId( Site $site ) {
109 $this->assertTypeOrValue( 'string', $site->getGlobalId(), null );
110 }
111
117 public function testSetGlobalId( Site $site ) {
118 $site->setGlobalId( 'foobar' );
119 $this->assertEquals( 'foobar', $site->getGlobalId() );
120 }
121
127 public function testGetType( Site $site ) {
128 $this->assertInternalType( 'string', $site->getType() );
129 }
130
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 );
140 }
141
147 public function testGetAllPaths( Site $site ) {
148 $this->assertInternalType( 'array', $site->getAllPaths() );
149 }
150
157 public function testSetAndRemovePath( Site $site ) {
158 $count = count( $site->getAllPaths() );
159
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' );
163
164 $this->assertEquals( $count + 2, count( $site->getAllPaths() ) );
165
166 $this->assertInternalType( 'string', $site->getPath( 'foobar' ) );
167 $this->assertEquals( 'http://www.wikidata.org/foo/$1', $site->getPath( 'spam' ) );
168
169 $site->removePath( 'spam' );
170 $site->removePath( 'foobar' );
171
172 $this->assertEquals( $count, count( $site->getAllPaths() ) );
173
174 $this->assertNull( $site->getPath( 'foobar' ) );
175 $this->assertNull( $site->getPath( 'spam' ) );
176 }
177
181 public function testSetLinkPath() {
182 $site = new Site();
183 $path = "TestPath/$1";
184
185 $site->setLinkPath( $path );
186 $this->assertEquals( $path, $site->getLinkPath() );
187 }
188
192 public function testGetLinkPathType() {
193 $site = new Site();
194
195 $path = 'TestPath/$1';
196 $site->setLinkPath( $path );
197 $this->assertEquals( $path, $site->getPath( $site->getLinkPathType() ) );
198
199 $path = 'AnotherPath/$1';
200 $site->setPath( $site->getLinkPathType(), $path );
201 $this->assertEquals( $path, $site->getLinkPath() );
202 }
203
207 public function testSetPath() {
208 $site = new Site();
209
210 $path = 'TestPath/$1';
211 $site->setPath( 'foo', $path );
212
213 $this->assertEquals( $path, $site->getPath( 'foo' ) );
214 }
215
220 public function testProtocolRelativePath() {
221 $site = new Site();
222
223 $type = $site->getLinkPathType();
224 $path = '//acme.com/'; // protocol-relative URL
225 $site->setPath( $type, $path );
226
227 $this->assertEquals( '', $site->getProtocol() );
228 }
229
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.
235
236 return [
237 [ # 0
238 'http://acme.test/TestPath/$1',
239 'Foo',
240 '/TestPath/Foo',
241 ],
242 [ # 1
243 'http://acme.test/TestScript?x=$1&y=bla',
244 'Foo',
245 'TestScript?x=Foo&y=bla',
246 ],
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',
251 ],
252 ];
253 }
254
259 public function testGetPageUrl( $path, $page, $expected ) {
260 $site = new Site();
261
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() );
267
268 $this->assertContains( $expected, $site->getPageUrl( $page ) );
269 }
270
271 protected function assertTypeOrFalse( $type, $value ) {
272 if ( $value === false ) {
273 $this->assertTrue( true );
274 } else {
275 $this->assertInternalType( $type, $value );
276 }
277 }
278
285 public function testSerialization( Site $site ) {
286 $this->assertInstanceOf( Serializable::class, $site );
287
288 $serialization = serialize( $site );
289 $newInstance = unserialize( $serialization );
290
291 $this->assertInstanceOf( Site::class, $newInstance );
292
293 $this->assertEquals( $serialization, serialize( $newInstance ) );
294 }
295}
serialize()
unserialize( $serialized)
arrayWrap(array $elements)
Utility method taking an array of elements and wrapping each element in its own array.
assertTypeOrValue( $type, $actual, $value=false, $message='')
Asserts that the provided variable is of the specified internal type or equals the $value argument.
testGetGlobalId(Site $site)
instanceProvider
Definition SiteTest.php:108
assertTypeOrFalse( $type, $value)
Definition SiteTest.php:271
testGetLanguageCode(Site $site)
instanceProvider
Definition SiteTest.php:80
instanceProvider()
Definition SiteTest.php:33
testProtocolRelativePath()
Site::setPath Site::getProtocol.
Definition SiteTest.php:220
testNormalizePageName(Site $site)
instanceProvider
Definition SiteTest.php:99
testSetLanguageCode(Site $site)
instanceProvider
Definition SiteTest.php:89
testGetAllPaths(Site $site)
instanceProvider
Definition SiteTest.php:147
testAddInterwikiId(Site $site)
instanceProvider
Definition SiteTest.php:70
testGetPageUrl( $path, $page, $expected)
provideGetPageUrl Site::getPageUrl
Definition SiteTest.php:259
testGetNavigationIds(Site $site)
instanceProvider
Definition SiteTest.php:51
testGetLinkPathType()
Site::getLinkPathType.
Definition SiteTest.php:192
testSetLinkPath()
Site::setLinkPath.
Definition SiteTest.php:181
static provideGetPageUrl()
Definition SiteTest.php:230
testGetType(Site $site)
instanceProvider
Definition SiteTest.php:127
testSetAndRemovePath(Site $site)
instanceProvider
Definition SiteTest.php:157
testSetPath()
Site::setPath.
Definition SiteTest.php:207
testGetInterwikiIds(Site $site)
instanceProvider
Definition SiteTest.php:42
testGetPath(Site $site)
instanceProvider
Definition SiteTest.php:136
testSerialization(Site $site)
instanceProvider
Definition SiteTest.php:285
testSetGlobalId(Site $site)
instanceProvider
Definition SiteTest.php:117
testAddNavigationId(Site $site)
instanceProvider
Definition SiteTest.php:60
Definition Site.php:29
getPath( $pathType)
Returns the path of the provided type or false if there is no such path.
Definition Site.php:609
getGlobalId()
Returns the global site identifier (ie enwiktionary).
Definition Site.php:140
getLanguageCode()
Returns language code of the sites primary language.
Definition Site.php:454
static getSites()
Definition TestSites.php:36