MediaWiki  1.33.0
SiteTest.php
Go to the documentation of this file.
1 <?php
2 
29 class SiteTest extends MediaWikiTestCase {
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 }
SiteTest\testGetLinkPathType
testGetLinkPathType()
Site::getLinkPathType.
Definition: SiteTest.php:190
SiteTest\testSetLanguageCode
testSetLanguageCode(Site $site)
instanceProvider
Definition: SiteTest.php:87
Site\getInterwikiIds
getInterwikiIds()
Returns the interwiki link identifiers that can be used for this site.
Definition: Site.php:546
SiteTest\testSetGlobalId
testSetGlobalId(Site $site)
instanceProvider
Definition: SiteTest.php:115
SiteTest\testAddInterwikiId
testAddInterwikiId(Site $site)
instanceProvider
Definition: SiteTest.php:68
TestSites\getSites
static getSites()
Definition: TestSites.php:36
Site\getType
getType()
Returns the type of the site (ie mediawiki).
Definition: Site.php:168
SiteTest\assertTypeOrFalse
assertTypeOrFalse( $type, $value)
Definition: SiteTest.php:269
captcha-old.count
count
Definition: captcha-old.py:249
Site\getPath
getPath( $pathType)
Returns the path of the provided type or false if there is no such path.
Definition: Site.php:609
SiteTest\testGetInterwikiIds
testGetInterwikiIds(Site $site)
instanceProvider
Definition: SiteTest.php:40
Site\getLanguageCode
getLanguageCode()
Returns language code of the sites primary language.
Definition: Site.php:454
SiteTest\instanceProvider
instanceProvider()
Definition: SiteTest.php:31
serialize
serialize()
Definition: ApiMessageTrait.php:134
SiteTest\testGetPageUrl
testGetPageUrl( $path, $page, $expected)
provideGetPageUrl Site::getPageUrl
Definition: SiteTest.php:257
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:228
SiteTest\testAddNavigationId
testAddNavigationId(Site $site)
instanceProvider
Definition: SiteTest.php:58
SiteTest\testSetPath
testSetPath()
Site::setPath.
Definition: SiteTest.php:205
SiteTest\testGetAllPaths
testGetAllPaths(Site $site)
instanceProvider
Definition: SiteTest.php:145
SiteTest\testSetLinkPath
testSetLinkPath()
Site::setLinkPath.
Definition: SiteTest.php:179
Site\getGlobalId
getGlobalId()
Returns the global site identifier (ie enwiktionary).
Definition: Site.php:140
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
SiteTest\testGetNavigationIds
testGetNavigationIds(Site $site)
instanceProvider
Definition: SiteTest.php:49
SiteTest\testNormalizePageName
testNormalizePageName(Site $site)
instanceProvider
Definition: SiteTest.php:97
Site\setLanguageCode
setLanguageCode( $languageCode)
Sets language code of the sites primary language.
Definition: Site.php:465
SiteTest\testGetType
testGetType(Site $site)
instanceProvider
Definition: SiteTest.php:125
SiteTest\testGetPath
testGetPath(Site $site)
instanceProvider
Definition: SiteTest.php:134
Site\addInterwikiId
addInterwikiId( $identifier)
Adds an interwiki id to the site.
Definition: Site.php:524
SiteTest\testSerialization
testSerialization(Site $site)
instanceProvider
Definition: SiteTest.php:283
MediaWikiTestCase\arrayWrap
arrayWrap(array $elements)
Utility method taking an array of elements and wrapping each element in its own array.
Definition: MediaWikiTestCase.php:2049
Site
Definition: Site.php:29
null
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
Site\addNavigationId
addNavigationId( $identifier)
Adds a navigation id to the site.
Definition: Site.php:535
$value
$value
Definition: styleTest.css.php:49
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:2158
SiteTest
Definition: SiteTest.php:29
Site\setPath
setPath( $pathType, $fullUrl)
Sets the path used to construct links with.
Definition: Site.php:588
Site\getNavigationIds
getNavigationIds()
Returns the equivalent link identifiers that can be used to make the site show up in interfaces such ...
Definition: Site.php:560
Site\getAllPaths
getAllPaths()
Returns the paths as associative array.
Definition: Site.php:622
Site\removePath
removePath( $pathType)
Removes the path of the provided type if it's set.
Definition: Site.php:633
unserialize
unserialize( $serialized)
Definition: ApiMessageTrait.php:142
SiteTest\testGetGlobalId
testGetGlobalId(Site $site)
instanceProvider
Definition: SiteTest.php:106
SiteTest\testGetLanguageCode
testGetLanguageCode(Site $site)
instanceProvider
Definition: SiteTest.php:78
Site\setGlobalId
setGlobalId( $globalId)
Sets the global site identifier (ie enwiktionary).
Definition: Site.php:153
$path
$path
Definition: NoLocalSettings.php:25
Site\normalizePageName
normalizePageName( $pageName)
Attempt to normalize the page name in some fashion.
Definition: Site.php:398
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:1985
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
SiteTest\testProtocolRelativePath
testProtocolRelativePath()
Site::setPath Site::getProtocol.
Definition: SiteTest.php:218
SiteTest\testSetAndRemovePath
testSetAndRemovePath(Site $site)
instanceProvider
Definition: SiteTest.php:155
$type
$type
Definition: testCompression.php:48