MediaWiki  1.28.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 }
getInterwikiIds()
Returns the interwiki link identifiers that can be used for this site.
Definition: Site.php:543
static getSites()
Definition: TestSites.php:38
testGetLinkPathType()
Site::getLinkPathType.
Definition: SiteTest.php:192
testAddInterwikiId(Site $site)
instanceProvider
Definition: SiteTest.php:70
addNavigationId($identifier)
Adds a navigation id to the site.
Definition: Site.php:532
testProtocolRelativePath()
Site::setPath Site::getProtocol.
Definition: SiteTest.php:220
assertTypeOrFalse($type, $value)
Definition: SiteTest.php:271
removePath($pathType)
Removes the path of the provided type if it's set.
Definition: Site.php:630
testAddNavigationId(Site $site)
instanceProvider
Definition: SiteTest.php:60
normalizePageName($pageName)
Returns $pageName without changes.
Definition: Site.php:398
testSetLanguageCode(Site $site)
instanceProvider
Definition: SiteTest.php:89
setLanguageCode($languageCode)
Sets language code of the sites primary language.
Definition: Site.php:465
testSetAndRemovePath(Site $site)
instanceProvider
Definition: SiteTest.php:157
$value
testGetGlobalId(Site $site)
instanceProvider
Definition: SiteTest.php:108
getLanguageCode()
Returns language code of the sites primary language.
Definition: Site.php:454
arrayWrap(array $elements)
Utility method taking an array of elements and wrapping each element in its own array.
setPath($pathType, $fullUrl)
Sets the path used to construct links with.
Definition: Site.php:585
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:1936
testSerialization(Site $site)
instanceProvider
Definition: SiteTest.php:285
unserialize($serialized)
Definition: ApiMessage.php:102
getAllPaths()
Returns the paths as associative array.
Definition: Site.php:619
instanceProvider()
Definition: SiteTest.php:33
testGetNavigationIds(Site $site)
instanceProvider
Definition: SiteTest.php:51
testGetAllPaths(Site $site)
instanceProvider
Definition: SiteTest.php:147
testNormalizePageName(Site $site)
instanceProvider
Definition: SiteTest.php:99
testSetLinkPath()
Site::setLinkPath.
Definition: SiteTest.php:181
getPath($pathType)
Returns the path of the provided type or false if there is no such path.
Definition: Site.php:606
testGetPageUrl($path, $page, $expected)
provideGetPageUrl Site::getPageUrl
Definition: SiteTest.php:259
assertTypeOrValue($type, $actual, $value=false, $message= '')
Asserts that the provided variable is of the specified internal type or equals the $value argument...
addInterwikiId($identifier)
Adds an interwiki id to the site.
Definition: Site.php:521
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
getNavigationIds()
Returns the equivalent link identifiers that can be used to make the site show up in interfaces such ...
Definition: Site.php:557
getGlobalId()
Returns the global site identifier (ie enwiktionary).
Definition: Site.php:142
Definition: Site.php:29
getType()
Returns the type of the site (ie mediawiki).
Definition: Site.php:170
$count
serialize()
Definition: ApiMessage.php:94
testGetPath(Site $site)
instanceProvider
Definition: SiteTest.php:136
testGetLanguageCode(Site $site)
instanceProvider
Definition: SiteTest.php:80
testGetInterwikiIds(Site $site)
instanceProvider
Definition: SiteTest.php:42
static provideGetPageUrl()
Definition: SiteTest.php:230
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached one of or reset my talk my contributions etc etc otherwise the built in rate limiting checks are if enabled allows for interception of redirect as a string mapping parameter names to values & $type
Definition: hooks.txt:2491
testSetPath()
Site::setPath.
Definition: SiteTest.php:207
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached $page
Definition: hooks.txt:2491
setGlobalId($globalId)
Sets the global site identifier (ie enwiktionary).
Definition: Site.php:155
testSetGlobalId(Site $site)
instanceProvider
Definition: SiteTest.php:117
testGetType(Site $site)
instanceProvider
Definition: SiteTest.php:127