3 namespace MediaWiki\Tests\Site
;
5 use MediaWiki\Site\Site
;
6 use MediaWikiIntegrationTestCase
;
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
32 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
34 class SiteTest
extends MediaWikiIntegrationTestCase
{
36 public function instanceProvider() {
37 return $this->arrayWrap( TestSites
::getSites() );
41 * @dataProvider instanceProvider
43 * @covers \MediaWiki\Site\Site::getInterwikiIds
45 public function testGetInterwikiIds( Site
$site ) {
46 $this->assertIsArray( $site->getInterwikiIds() );
50 * @dataProvider instanceProvider
52 * @covers \MediaWiki\Site\Site::getNavigationIds
54 public function testGetNavigationIds( Site
$site ) {
55 $this->assertIsArray( $site->getNavigationIds() );
59 * @dataProvider instanceProvider
61 * @covers \MediaWiki\Site\Site::addNavigationId
63 public function testAddNavigationId( Site
$site ) {
64 $site->addNavigationId( 'foobar' );
65 $this->assertContains( 'foobar', $site->getNavigationIds() );
69 * @dataProvider instanceProvider
71 * @covers \MediaWiki\Site\Site::addInterwikiId
73 public function testAddInterwikiId( Site
$site ) {
74 $site->addInterwikiId( 'foobar' );
75 $this->assertContains( 'foobar', $site->getInterwikiIds() );
79 * @dataProvider instanceProvider
81 * @covers \MediaWiki\Site\Site::getLanguageCode
83 public function testGetLanguageCode( Site
$site ) {
85 $site->getLanguageCode(),
86 $this->logicalOr( $this->isNull(), $this->isType( 'string' ) )
91 * @dataProvider instanceProvider
93 * @covers \MediaWiki\Site\Site::setLanguageCode
95 public function testSetLanguageCode( Site
$site ) {
96 $site->setLanguageCode( 'en' );
97 $this->assertEquals( 'en', $site->getLanguageCode() );
101 * @dataProvider instanceProvider
103 * @covers \MediaWiki\Site\Site::normalizePageName
105 public function testNormalizePageName( Site
$site ) {
106 $this->assertIsString( $site->normalizePageName( 'Foobar' ) );
110 * @dataProvider instanceProvider
112 * @covers \MediaWiki\Site\Site::getGlobalId
114 public function testGetGlobalId( Site
$site ) {
116 $site->getGlobalId(),
117 $this->logicalOr( $this->isNull(), $this->isType( 'string' ) )
122 * @dataProvider instanceProvider
124 * @covers \MediaWiki\Site\Site::setGlobalId
126 public function testSetGlobalId( Site
$site ) {
127 $site->setGlobalId( 'foobar' );
128 $this->assertEquals( 'foobar', $site->getGlobalId() );
132 * @dataProvider instanceProvider
134 * @covers \MediaWiki\Site\Site::getType
136 public function testGetType( Site
$site ) {
137 $this->assertIsString( $site->getType() );
141 * @dataProvider instanceProvider
143 * @covers \MediaWiki\Site\Site::getPath
145 public function testGetPath( Site
$site ) {
147 $site->getPath( 'page_path' ),
148 $this->logicalOr( $this->isNull(), $this->isType( 'string' ) )
151 $site->getPath( 'file_path' ),
152 $this->logicalOr( $this->isNull(), $this->isType( 'string' ) )
155 $site->getPath( 'foobar' ),
156 $this->logicalOr( $this->isNull(), $this->isType( 'string' ) )
161 * @dataProvider instanceProvider
163 * @covers \MediaWiki\Site\Site::getAllPaths
165 public function testGetAllPaths( Site
$site ) {
166 $this->assertIsArray( $site->getAllPaths() );
170 * @dataProvider instanceProvider
172 * @covers \MediaWiki\Site\Site::setPath
173 * @covers \MediaWiki\Site\Site::removePath
175 public function testSetAndRemovePath( Site
$site ) {
176 $count = count( $site->getAllPaths() );
178 $site->setPath( 'spam', 'http://www.wikidata.org/$1' );
179 $site->setPath( 'spam', 'http://www.wikidata.org/foo/$1' );
180 $site->setPath( 'foobar', 'http://www.wikidata.org/bar/$1' );
182 $this->assertCount( $count +
2, $site->getAllPaths() );
184 $this->assertIsString( $site->getPath( 'foobar' ) );
185 $this->assertEquals( 'http://www.wikidata.org/foo/$1', $site->getPath( 'spam' ) );
187 $site->removePath( 'spam' );
188 $site->removePath( 'foobar' );
190 $this->assertCount( $count, $site->getAllPaths() );
192 $this->assertNull( $site->getPath( 'foobar' ) );
193 $this->assertNull( $site->getPath( 'spam' ) );
197 * @covers \MediaWiki\Site\Site::setLinkPath
199 public function testSetLinkPath() {
201 $path = "TestPath/$1";
203 $site->setLinkPath( $path );
204 $this->assertEquals( $path, $site->getLinkPath() );
208 * @covers \MediaWiki\Site\Site::getLinkPathType
210 public function testGetLinkPathType() {
213 $path = 'TestPath/$1';
214 $site->setLinkPath( $path );
215 $this->assertEquals( $path, $site->getPath( $site->getLinkPathType() ) );
217 $path = 'AnotherPath/$1';
218 $site->setPath( $site->getLinkPathType(), $path );
219 $this->assertEquals( $path, $site->getLinkPath() );
223 * @covers \MediaWiki\Site\Site::setPath
225 public function testSetPath() {
228 $path = 'TestPath/$1';
229 $site->setPath( 'foo', $path );
231 $this->assertEquals( $path, $site->getPath( 'foo' ) );
235 * @covers \MediaWiki\Site\Site::setPath
236 * @covers \MediaWiki\Site\Site::getProtocol
238 public function testProtocolRelativePath() {
241 $type = $site->getLinkPathType();
242 $path = '//acme.com/'; // protocol-relative URL
243 $site->setPath( $type, $path );
245 $this->assertSame( '', $site->getProtocol() );
248 public static function provideGetPageUrl() {
249 // NOTE: the assumption that the URL is built by replacing $1
250 // with the urlencoded version of $page
251 // is true for Site but not guaranteed for subclasses.
252 // Subclasses need to override this provider appropriately.
256 'http://acme.test/TestPath/$1',
261 'http://acme.test/TestScript?x=$1&y=bla',
263 'TestScript?x=Foo&y=bla',
266 'http://acme.test/TestPath/$1',
267 'foo & bar/xyzzy (quux-shmoox?)',
268 '/TestPath/foo%20%26%20bar%2Fxyzzy%20%28quux-shmoox%3F%29',
274 * @dataProvider provideGetPageUrl
275 * @covers \MediaWiki\Site\Site::getPageUrl
277 public function testGetPageUrl( $path, $page, $expected ) {
280 // NOTE: the assumption that getPageUrl is based on getLinkPath
281 // is true for Site but not guaranteed for subclasses.
282 // Subclasses need to override this test case appropriately.
283 $site->setLinkPath( $path );
284 $this->assertStringContainsString( $path, $site->getPageUrl() );
286 $this->assertStringContainsString( $expected, $site->getPageUrl( $page ) );
290 * @dataProvider instanceProvider
292 * @covers \MediaWiki\Site\Site::__serialize
293 * @covers \MediaWiki\Site\Site::__unserialize
295 public function testSerialization( Site
$site ) {
296 $serialization = serialize( $site );
297 $newInstance = unserialize( $serialization );
299 $this->assertInstanceOf( Site
::class, $newInstance );
301 $this->assertEquals( $serialization, serialize( $newInstance ) );