MediaWiki REL1_33
SiteTest.php
Go to the documentation of this file.
1<?php
2
30
31 public function instanceProvider() {
32 return $this->arrayWrap( TestSites::getSites() );
33 }
34
40 public function testGetInterwikiIds( Site $site ) {
41 $this->assertInternalType( 'array', $site->getInterwikiIds() );
42 }
43
49 public function testGetNavigationIds( Site $site ) {
50 $this->assertInternalType( 'array', $site->getNavigationIds() );
51 }
52
58 public function testAddNavigationId( Site $site ) {
59 $site->addNavigationId( 'foobar' );
60 $this->assertTrue( in_array( 'foobar', $site->getNavigationIds(), true ) );
61 }
62
68 public function testAddInterwikiId( Site $site ) {
69 $site->addInterwikiId( 'foobar' );
70 $this->assertTrue( in_array( 'foobar', $site->getInterwikiIds(), true ) );
71 }
72
78 public function testGetLanguageCode( Site $site ) {
79 $this->assertTypeOrValue( 'string', $site->getLanguageCode(), null );
80 }
81
87 public function testSetLanguageCode( Site $site ) {
88 $site->setLanguageCode( 'en' );
89 $this->assertEquals( 'en', $site->getLanguageCode() );
90 }
91
97 public function testNormalizePageName( Site $site ) {
98 $this->assertInternalType( 'string', $site->normalizePageName( 'Foobar' ) );
99 }
100
106 public function testGetGlobalId( Site $site ) {
107 $this->assertTypeOrValue( 'string', $site->getGlobalId(), null );
108 }
109
115 public function testSetGlobalId( Site $site ) {
116 $site->setGlobalId( 'foobar' );
117 $this->assertEquals( 'foobar', $site->getGlobalId() );
118 }
119
125 public function testGetType( Site $site ) {
126 $this->assertInternalType( 'string', $site->getType() );
127 }
128
134 public function testGetPath( Site $site ) {
135 $this->assertTypeOrValue( 'string', $site->getPath( 'page_path' ), null );
136 $this->assertTypeOrValue( 'string', $site->getPath( 'file_path' ), null );
137 $this->assertTypeOrValue( 'string', $site->getPath( 'foobar' ), null );
138 }
139
145 public function testGetAllPaths( Site $site ) {
146 $this->assertInternalType( 'array', $site->getAllPaths() );
147 }
148
155 public function testSetAndRemovePath( Site $site ) {
156 $count = count( $site->getAllPaths() );
157
158 $site->setPath( 'spam', 'http://www.wikidata.org/$1' );
159 $site->setPath( 'spam', 'http://www.wikidata.org/foo/$1' );
160 $site->setPath( 'foobar', 'http://www.wikidata.org/bar/$1' );
161
162 $this->assertEquals( $count + 2, count( $site->getAllPaths() ) );
163
164 $this->assertInternalType( 'string', $site->getPath( 'foobar' ) );
165 $this->assertEquals( 'http://www.wikidata.org/foo/$1', $site->getPath( 'spam' ) );
166
167 $site->removePath( 'spam' );
168 $site->removePath( 'foobar' );
169
170 $this->assertEquals( $count, count( $site->getAllPaths() ) );
171
172 $this->assertNull( $site->getPath( 'foobar' ) );
173 $this->assertNull( $site->getPath( 'spam' ) );
174 }
175
179 public function testSetLinkPath() {
180 $site = new Site();
181 $path = "TestPath/$1";
182
183 $site->setLinkPath( $path );
184 $this->assertEquals( $path, $site->getLinkPath() );
185 }
186
190 public function testGetLinkPathType() {
191 $site = new Site();
192
193 $path = 'TestPath/$1';
194 $site->setLinkPath( $path );
195 $this->assertEquals( $path, $site->getPath( $site->getLinkPathType() ) );
196
197 $path = 'AnotherPath/$1';
198 $site->setPath( $site->getLinkPathType(), $path );
199 $this->assertEquals( $path, $site->getLinkPath() );
200 }
201
205 public function testSetPath() {
206 $site = new Site();
207
208 $path = 'TestPath/$1';
209 $site->setPath( 'foo', $path );
210
211 $this->assertEquals( $path, $site->getPath( 'foo' ) );
212 }
213
218 public function testProtocolRelativePath() {
219 $site = new Site();
220
221 $type = $site->getLinkPathType();
222 $path = '//acme.com/'; // protocol-relative URL
223 $site->setPath( $type, $path );
224
225 $this->assertEquals( '', $site->getProtocol() );
226 }
227
228 public static function provideGetPageUrl() {
229 // NOTE: the assumption that the URL is built by replacing $1
230 // with the urlencoded version of $page
231 // is true for Site but not guaranteed for subclasses.
232 // Subclasses need to override this provider appropriately.
233
234 return [
235 [ # 0
236 'http://acme.test/TestPath/$1',
237 'Foo',
238 '/TestPath/Foo',
239 ],
240 [ # 1
241 'http://acme.test/TestScript?x=$1&y=bla',
242 'Foo',
243 'TestScript?x=Foo&y=bla',
244 ],
245 [ # 2
246 'http://acme.test/TestPath/$1',
247 'foo & bar/xyzzy (quux-shmoox?)',
248 '/TestPath/foo%20%26%20bar%2Fxyzzy%20%28quux-shmoox%3F%29',
249 ],
250 ];
251 }
252
257 public function testGetPageUrl( $path, $page, $expected ) {
258 $site = new Site();
259
260 // NOTE: the assumption that getPageUrl is based on getLinkPath
261 // is true for Site but not guaranteed for subclasses.
262 // Subclasses need to override this test case appropriately.
263 $site->setLinkPath( $path );
264 $this->assertContains( $path, $site->getPageUrl() );
265
266 $this->assertContains( $expected, $site->getPageUrl( $page ) );
267 }
268
269 protected function assertTypeOrFalse( $type, $value ) {
270 if ( $value === false ) {
271 $this->assertTrue( true );
272 } else {
273 $this->assertInternalType( $type, $value );
274 }
275 }
276
283 public function testSerialization( Site $site ) {
284 $this->assertInstanceOf( Serializable::class, $site );
285
286 $serialization = serialize( $site );
287 $newInstance = unserialize( $serialization );
288
289 $this->assertInstanceOf( Site::class, $newInstance );
290
291 $this->assertEquals( $serialization, serialize( $newInstance ) );
292 }
293}
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:106
assertTypeOrFalse( $type, $value)
Definition SiteTest.php:269
testGetLanguageCode(Site $site)
instanceProvider
Definition SiteTest.php:78
instanceProvider()
Definition SiteTest.php:31
testProtocolRelativePath()
Site::setPath Site::getProtocol.
Definition SiteTest.php:218
testNormalizePageName(Site $site)
instanceProvider
Definition SiteTest.php:97
testSetLanguageCode(Site $site)
instanceProvider
Definition SiteTest.php:87
testGetAllPaths(Site $site)
instanceProvider
Definition SiteTest.php:145
testAddInterwikiId(Site $site)
instanceProvider
Definition SiteTest.php:68
testGetPageUrl( $path, $page, $expected)
provideGetPageUrl Site::getPageUrl
Definition SiteTest.php:257
testGetNavigationIds(Site $site)
instanceProvider
Definition SiteTest.php:49
testGetLinkPathType()
Site::getLinkPathType.
Definition SiteTest.php:190
testSetLinkPath()
Site::setLinkPath.
Definition SiteTest.php:179
static provideGetPageUrl()
Definition SiteTest.php:228
testGetType(Site $site)
instanceProvider
Definition SiteTest.php:125
testSetAndRemovePath(Site $site)
instanceProvider
Definition SiteTest.php:155
testSetPath()
Site::setPath.
Definition SiteTest.php:205
testGetInterwikiIds(Site $site)
instanceProvider
Definition SiteTest.php:40
testGetPath(Site $site)
instanceProvider
Definition SiteTest.php:134
testSerialization(Site $site)
instanceProvider
Definition SiteTest.php:283
testSetGlobalId(Site $site)
instanceProvider
Definition SiteTest.php:115
testAddNavigationId(Site $site)
instanceProvider
Definition SiteTest.php:58
Definition Site.php:29
normalizePageName( $pageName)
Attempt to normalize the page name in some fashion.
Definition Site.php:398
setPath( $pathType, $fullUrl)
Sets the path used to construct links with.
Definition Site.php:588
addInterwikiId( $identifier)
Adds an interwiki id to the site.
Definition Site.php:524
getPath( $pathType)
Returns the path of the provided type or false if there is no such path.
Definition Site.php:609
getType()
Returns the type of the site (ie mediawiki).
Definition Site.php:168
removePath( $pathType)
Removes the path of the provided type if it's set.
Definition Site.php:633
setLanguageCode( $languageCode)
Sets language code of the sites primary language.
Definition Site.php:465
getGlobalId()
Returns the global site identifier (ie enwiktionary).
Definition Site.php:140
setGlobalId( $globalId)
Sets the global site identifier (ie enwiktionary).
Definition Site.php:153
addNavigationId( $identifier)
Adds a navigation id to the site.
Definition Site.php:535
getAllPaths()
Returns the paths as associative array.
Definition Site.php:622
getLanguageCode()
Returns language code of the sites primary language.
Definition Site.php:454
getNavigationIds()
Returns the equivalent link identifiers that can be used to make the site show up in interfaces such ...
Definition Site.php:560
getInterwikiIds()
Returns the interwiki link identifiers that can be used for this site.
Definition Site.php:546
static getSites()
Definition TestSites.php:36
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37