4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
27 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
29 class SiteTest
extends MediaWikiTestCase
{
31 public function instanceProvider() {
32 return $this->arrayWrap( TestSites
::getSites() );
36 * @dataProvider instanceProvider
38 * @covers Site::getInterwikiIds
40 public function testGetInterwikiIds( Site
$site ) {
41 $this->assertInternalType( 'array', $site->getInterwikiIds() );
45 * @dataProvider instanceProvider
47 * @covers Site::getNavigationIds
49 public function testGetNavigationIds( Site
$site ) {
50 $this->assertInternalType( 'array', $site->getNavigationIds() );
54 * @dataProvider instanceProvider
56 * @covers Site::addNavigationId
58 public function testAddNavigationId( Site
$site ) {
59 $site->addNavigationId( 'foobar' );
60 $this->assertTrue( in_array( 'foobar', $site->getNavigationIds(), true ) );
64 * @dataProvider instanceProvider
66 * @covers Site::addInterwikiId
68 public function testAddInterwikiId( Site
$site ) {
69 $site->addInterwikiId( 'foobar' );
70 $this->assertTrue( in_array( 'foobar', $site->getInterwikiIds(), true ) );
74 * @dataProvider instanceProvider
76 * @covers Site::getLanguageCode
78 public function testGetLanguageCode( Site
$site ) {
79 $this->assertTypeOrValue( 'string', $site->getLanguageCode(), null );
83 * @dataProvider instanceProvider
85 * @covers Site::setLanguageCode
87 public function testSetLanguageCode( Site
$site ) {
88 $site->setLanguageCode( 'en' );
89 $this->assertEquals( 'en', $site->getLanguageCode() );
93 * @dataProvider instanceProvider
95 * @covers Site::normalizePageName
97 public function testNormalizePageName( Site
$site ) {
98 $this->assertInternalType( 'string', $site->normalizePageName( 'Foobar' ) );
102 * @dataProvider instanceProvider
104 * @covers Site::getGlobalId
106 public function testGetGlobalId( Site
$site ) {
107 $this->assertTypeOrValue( 'string', $site->getGlobalId(), null );
111 * @dataProvider instanceProvider
113 * @covers Site::setGlobalId
115 public function testSetGlobalId( Site
$site ) {
116 $site->setGlobalId( 'foobar' );
117 $this->assertEquals( 'foobar', $site->getGlobalId() );
121 * @dataProvider instanceProvider
123 * @covers Site::getType
125 public function testGetType( Site
$site ) {
126 $this->assertInternalType( 'string', $site->getType() );
130 * @dataProvider instanceProvider
132 * @covers Site::getPath
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 );
141 * @dataProvider instanceProvider
143 * @covers Site::getAllPaths
145 public function testGetAllPaths( Site
$site ) {
146 $this->assertInternalType( 'array', $site->getAllPaths() );
150 * @dataProvider instanceProvider
152 * @covers Site::setPath
153 * @covers Site::removePath
155 public function testSetAndRemovePath( Site
$site ) {
156 $count = count( $site->getAllPaths() );
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' );
162 $this->assertEquals( $count +
2, count( $site->getAllPaths() ) );
164 $this->assertInternalType( 'string', $site->getPath( 'foobar' ) );
165 $this->assertEquals( 'http://www.wikidata.org/foo/$1', $site->getPath( 'spam' ) );
167 $site->removePath( 'spam' );
168 $site->removePath( 'foobar' );
170 $this->assertEquals( $count, count( $site->getAllPaths() ) );
172 $this->assertNull( $site->getPath( 'foobar' ) );
173 $this->assertNull( $site->getPath( 'spam' ) );
177 * @covers Site::setLinkPath
179 public function testSetLinkPath() {
181 $path = "TestPath/$1";
183 $site->setLinkPath( $path );
184 $this->assertEquals( $path, $site->getLinkPath() );
188 * @covers Site::getLinkPathType
190 public function testGetLinkPathType() {
193 $path = 'TestPath/$1';
194 $site->setLinkPath( $path );
195 $this->assertEquals( $path, $site->getPath( $site->getLinkPathType() ) );
197 $path = 'AnotherPath/$1';
198 $site->setPath( $site->getLinkPathType(), $path );
199 $this->assertEquals( $path, $site->getLinkPath() );
203 * @covers Site::setPath
205 public function testSetPath() {
208 $path = 'TestPath/$1';
209 $site->setPath( 'foo', $path );
211 $this->assertEquals( $path, $site->getPath( 'foo' ) );
215 * @covers Site::setPath
216 * @covers Site::getProtocol
218 public function testProtocolRelativePath() {
221 $type = $site->getLinkPathType();
222 $path = '//acme.com/'; // protocol-relative URL
223 $site->setPath( $type, $path );
225 $this->assertEquals( '', $site->getProtocol() );
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.
236 'http://acme.test/TestPath/$1',
241 'http://acme.test/TestScript?x=$1&y=bla',
243 'TestScript?x=Foo&y=bla',
246 'http://acme.test/TestPath/$1',
247 'foo & bar/xyzzy (quux-shmoox?)',
248 '/TestPath/foo%20%26%20bar%2Fxyzzy%20%28quux-shmoox%3F%29',
254 * @dataProvider provideGetPageUrl
255 * @covers Site::getPageUrl
257 public function testGetPageUrl( $path, $page, $expected ) {
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() );
266 $this->assertContains( $expected, $site->getPageUrl( $page ) );
269 protected function assertTypeOrFalse( $type, $value ) {
270 if ( $value === false ) {
271 $this->assertTrue( true );
273 $this->assertInternalType( $type, $value );
278 * @dataProvider instanceProvider
280 * @covers Site::serialize
281 * @covers Site::unserialize
283 public function testSerialization( Site
$site ) {
284 $this->assertInstanceOf( Serializable
::class, $site );
286 $serialization = serialize( $site );
287 $newInstance = unserialize( $serialization );
289 $this->assertInstanceOf( Site
::class, $newInstance );
291 $this->assertEquals( $serialization, serialize( $newInstance ) );