MediaWiki  1.30.0
SiteTest.php
Go to the documentation of this file.
1 <?php
2 
31 class SiteTest extends MediaWikiTestCase {
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', $site );
287 
288  $serialization = serialize( $site );
289  $newInstance = unserialize( $serialization );
290 
291  $this->assertInstanceOf( 'Site', $newInstance );
292 
293  $this->assertEquals( $serialization, serialize( $newInstance ) );
294  }
295 }
SiteTest\testGetLinkPathType
testGetLinkPathType()
Site::getLinkPathType.
Definition: SiteTest.php:192
SiteTest\testSetLanguageCode
testSetLanguageCode(Site $site)
instanceProvider
Definition: SiteTest.php:89
Site\getInterwikiIds
getInterwikiIds()
Returns the interwiki link identifiers that can be used for this site.
Definition: Site.php:544
SiteTest\testSetGlobalId
testSetGlobalId(Site $site)
instanceProvider
Definition: SiteTest.php:117
SiteTest\testAddInterwikiId
testAddInterwikiId(Site $site)
instanceProvider
Definition: SiteTest.php:70
TestSites\getSites
static getSites()
Definition: TestSites.php:38
Site\getType
getType()
Returns the type of the site (ie mediawiki).
Definition: Site.php:168
SiteTest\assertTypeOrFalse
assertTypeOrFalse( $type, $value)
Definition: SiteTest.php:271
captcha-old.count
count
Definition: captcha-old.py:249
unserialize
unserialize( $serialized)
Definition: ApiMessage.php:185
Site\getPath
getPath( $pathType)
Returns the path of the provided type or false if there is no such path.
Definition: Site.php:607
SiteTest\testGetInterwikiIds
testGetInterwikiIds(Site $site)
instanceProvider
Definition: SiteTest.php:42
serialize
serialize()
Definition: ApiMessage.php:177
Site\getLanguageCode
getLanguageCode()
Returns language code of the sites primary language.
Definition: Site.php:452
SiteTest\instanceProvider
instanceProvider()
Definition: SiteTest.php:33
SiteTest\testGetPageUrl
testGetPageUrl( $path, $page, $expected)
provideGetPageUrl Site::getPageUrl
Definition: SiteTest.php:259
php
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:35
SiteTest\provideGetPageUrl
static provideGetPageUrl()
Definition: SiteTest.php:230
SiteTest\testAddNavigationId
testAddNavigationId(Site $site)
instanceProvider
Definition: SiteTest.php:60
SiteTest\testSetPath
testSetPath()
Site::setPath.
Definition: SiteTest.php:207
SiteTest\testGetAllPaths
testGetAllPaths(Site $site)
instanceProvider
Definition: SiteTest.php:147
SiteTest\testSetLinkPath
testSetLinkPath()
Site::setLinkPath.
Definition: SiteTest.php:181
Site\getGlobalId
getGlobalId()
Returns the global site identifier (ie enwiktionary).
Definition: Site.php:140
MediaWikiTestCase
Definition: MediaWikiTestCase.php:15
SiteTest\testGetNavigationIds
testGetNavigationIds(Site $site)
instanceProvider
Definition: SiteTest.php:51
SiteTest\testNormalizePageName
testNormalizePageName(Site $site)
instanceProvider
Definition: SiteTest.php:99
Site\setLanguageCode
setLanguageCode( $languageCode)
Sets language code of the sites primary language.
Definition: Site.php:463
SiteTest\testGetType
testGetType(Site $site)
instanceProvider
Definition: SiteTest.php:127
SiteTest\testGetPath
testGetPath(Site $site)
instanceProvider
Definition: SiteTest.php:136
Site\addInterwikiId
addInterwikiId( $identifier)
Adds an interwiki id to the site.
Definition: Site.php:522
SiteTest\testSerialization
testSerialization(Site $site)
instanceProvider
Definition: SiteTest.php:285
MediaWikiTestCase\arrayWrap
arrayWrap(array $elements)
Utility method taking an array of elements and wrapping each element in its own array.
Definition: MediaWikiTestCase.php:1531
Site
Definition: Site.php:29
Site\addNavigationId
addNavigationId( $identifier)
Adds a navigation id to the site.
Definition: Site.php:533
$value
$value
Definition: styleTest.css.php:45
MediaWikiTestCase\assertTypeOrValue
assertTypeOrValue( $type, $actual, $value=false, $message='')
Asserts that the provided variable is of the specified internal type or equals the $value argument.
Definition: MediaWikiTestCase.php:1640
SiteTest
Definition: SiteTest.php:31
Site\setPath
setPath( $pathType, $fullUrl)
Sets the path used to construct links with.
Definition: Site.php:586
Site\getNavigationIds
getNavigationIds()
Returns the equivalent link identifiers that can be used to make the site show up in interfaces such ...
Definition: Site.php:558
Site\getAllPaths
getAllPaths()
Returns the paths as associative array.
Definition: Site.php:620
Site\removePath
removePath( $pathType)
Removes the path of the provided type if it's set.
Definition: Site.php:631
SiteTest\testGetGlobalId
testGetGlobalId(Site $site)
instanceProvider
Definition: SiteTest.php:108
SiteTest\testGetLanguageCode
testGetLanguageCode(Site $site)
instanceProvider
Definition: SiteTest.php:80
Site\setGlobalId
setGlobalId( $globalId)
Sets the global site identifier (ie enwiktionary).
Definition: Site.php:153
$path
$path
Definition: NoLocalSettings.php:26
Site\normalizePageName
normalizePageName( $pageName)
Returns $pageName without changes.
Definition: Site.php:396
true
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1965
SiteTest\testProtocolRelativePath
testProtocolRelativePath()
Site::setPath Site::getProtocol.
Definition: SiteTest.php:220
SiteTest\testSetAndRemovePath
testSetAndRemovePath(Site $site)
instanceProvider
Definition: SiteTest.php:157
$type
$type
Definition: testCompression.php:48