4 * Tests for the Site class.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
29 * @licence GNU GPL v2+
30 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
32 class SiteTest
extends MediaWikiTestCase
{
34 public function instanceProvider() {
35 return $this->arrayWrap( TestSites
::getSites() );
39 * @dataProvider instanceProvider
42 public function testGetInterwikiIds( Site
$site ) {
43 $this->assertInternalType( 'array', $site->getInterwikiIds() );
47 * @dataProvider instanceProvider
50 public function testGetNavigationIds( Site
$site ) {
51 $this->assertInternalType( 'array', $site->getNavigationIds() );
55 * @dataProvider instanceProvider
58 public function testAddNavigationId( Site
$site ) {
59 $site->addNavigationId( 'foobar' );
60 $this->assertTrue( in_array( 'foobar', $site->getNavigationIds(), true ) );
64 * @dataProvider instanceProvider
67 public function testAddInterwikiId( Site
$site ) {
68 $site->addInterwikiId( 'foobar' );
69 $this->assertTrue( in_array( 'foobar', $site->getInterwikiIds(), true ) );
73 * @dataProvider instanceProvider
76 public function testGetLanguageCode( Site
$site ) {
77 $this->assertTypeOrValue( 'string', $site->getLanguageCode(), null );
81 * @dataProvider instanceProvider
84 public function testSetLanguageCode( Site
$site ) {
85 $site->setLanguageCode( 'en' );
86 $this->assertEquals( 'en', $site->getLanguageCode() );
90 * @dataProvider instanceProvider
93 public function testNormalizePageName( Site
$site ) {
94 $this->assertInternalType( 'string', $site->normalizePageName( 'Foobar' ) );
98 * @dataProvider instanceProvider
101 public function testGetGlobalId( Site
$site ) {
102 $this->assertTypeOrValue( 'string', $site->getGlobalId(), null );
106 * @dataProvider instanceProvider
109 public function testSetGlobalId( Site
$site ) {
110 $site->setGlobalId( 'foobar' );
111 $this->assertEquals( 'foobar', $site->getGlobalId() );
115 * @dataProvider instanceProvider
118 public function testGetType( Site
$site ) {
119 $this->assertInternalType( 'string', $site->getType() );
123 * @dataProvider instanceProvider
126 public function testGetPath( Site
$site ) {
127 $this->assertTypeOrValue( 'string', $site->getPath( 'page_path' ), null );
128 $this->assertTypeOrValue( 'string', $site->getPath( 'file_path' ), null );
129 $this->assertTypeOrValue( 'string', $site->getPath( 'foobar' ), null );
133 * @dataProvider instanceProvider
136 public function testGetAllPaths( Site
$site ) {
137 $this->assertInternalType( 'array', $site->getAllPaths() );
141 * @dataProvider instanceProvider
144 public function testSetAndRemovePath( Site
$site ) {
145 $count = count( $site->getAllPaths() );
147 $site->setPath( 'spam', 'http://www.wikidata.org/$1' );
148 $site->setPath( 'spam', 'http://www.wikidata.org/foo/$1' );
149 $site->setPath( 'foobar', 'http://www.wikidata.org/bar/$1' );
151 $this->assertEquals( $count +
2, count( $site->getAllPaths() ) );
153 $this->assertInternalType( 'string', $site->getPath( 'foobar' ) );
154 $this->assertEquals( 'http://www.wikidata.org/foo/$1', $site->getPath( 'spam' ) );
156 $site->removePath( 'spam' );
157 $site->removePath( 'foobar' );
159 $this->assertEquals( $count, count( $site->getAllPaths() ) );
161 $this->assertNull( $site->getPath( 'foobar' ) );
162 $this->assertNull( $site->getPath( 'spam' ) );
165 public function testSetLinkPath() {
167 $path = "TestPath/$1";
169 $site->setLinkPath( $path );
170 $this->assertEquals( $path, $site->getLinkPath() );
173 public function testGetLinkPathType() {
176 $path = 'TestPath/$1';
177 $site->setLinkPath( $path );
178 $this->assertEquals( $path, $site->getPath( $site->getLinkPathType() ) );
180 $path = 'AnotherPath/$1';
181 $site->setPath( $site->getLinkPathType(), $path );
182 $this->assertEquals( $path, $site->getLinkPath() );
185 public function testSetPath() {
188 $path = 'TestPath/$1';
189 $site->setPath( 'foo', $path );
191 $this->assertEquals( $path, $site->getPath( 'foo' ) );
194 public function testProtocolRelativePath() {
197 $type = $site->getLinkPathType();
198 $path = '//acme.com/'; // protocol-relative URL
199 $site->setPath( $type, $path );
201 $this->assertEquals( '', $site->getProtocol() );
204 public static function provideGetPageUrl() {
205 //NOTE: the assumption that the URL is built by replacing $1
206 // with the urlencoded version of $page
207 // is true for Site but not guaranteed for subclasses.
208 // Subclasses need to override this provider appropriately.
212 'http://acme.test/TestPath/$1',
217 'http://acme.test/TestScript?x=$1&y=bla',
219 'TestScript?x=Foo&y=bla',
222 'http://acme.test/TestPath/$1',
223 'foo & bar/xyzzy (quux-shmoox?)',
224 '/TestPath/foo%20%26%20bar%2Fxyzzy%20%28quux-shmoox%3F%29',
230 * @dataProvider provideGetPageUrl
232 public function testGetPageUrl( $path, $page, $expected ) {
235 //NOTE: the assumption that getPageUrl is based on getLinkPath
236 // is true for Site but not guaranteed for subclasses.
237 // Subclasses need to override this test case appropriately.
238 $site->setLinkPath( $path );
239 $this->assertContains( $path, $site->getPageUrl() );
241 $this->assertContains( $expected, $site->getPageUrl( $page ) );
244 protected function assertTypeOrFalse( $type, $value ) {
245 if ( $value === false ) {
246 $this->assertTrue( true );
248 $this->assertInternalType( $type, $value );
253 * @dataProvider instanceProvider
256 public function testSerialization( Site
$site ) {
257 $this->assertInstanceOf( 'Serializable', $site );
259 $serialization = serialize( $site );
260 $newInstance = unserialize( $serialization );
262 $this->assertInstanceOf( 'Site', $newInstance );
264 $this->assertEquals( $serialization, serialize( $newInstance ) );