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() {
37 $instances[] = new Site();
40 $site->setGlobalId( 'enwiki' );
41 $site->setInternalId( 42 );
44 $site = new MediaWikiSite();
45 $site->setGlobalId( 'nlwiki' );
46 $site->setLanguageCode( 'nl' );
49 return $this->arrayWrap( $instances );
53 * @dataProvider instanceProvider
56 public function testGetInterwikiIds( Site
$site ) {
57 $this->assertInternalType( 'array', $site->getInterwikiIds() );
61 * @dataProvider instanceProvider
64 public function testGetNavigationIds( Site
$site ) {
65 $this->assertInternalType( 'array', $site->getNavigationIds() );
69 * @dataProvider instanceProvider
72 public function testAddNavigationId( Site
$site ) {
73 $site->addNavigationId( 'foobar' );
74 $this->assertTrue( in_array( 'foobar', $site->getNavigationIds(), true ) );
78 * @dataProvider instanceProvider
81 public function testAddInterwikiId( Site
$site ) {
82 $site->addInterwikiId( 'foobar' );
83 $this->assertTrue( in_array( 'foobar', $site->getInterwikiIds(), true ) );
87 * @dataProvider instanceProvider
90 public function testGetLanguageCode( Site
$site ) {
91 $this->assertTypeOrValue( 'string', $site->getLanguageCode(), null );
95 * @dataProvider instanceProvider
98 public function testSetLanguageCode( Site
$site ) {
99 $site->setLanguageCode( 'en' );
100 $this->assertEquals( 'en', $site->getLanguageCode() );
104 * @dataProvider instanceProvider
107 public function testNormalizePageName( Site
$site ) {
108 $this->assertInternalType( 'string', $site->normalizePageName( 'Foobar' ) );
112 * @dataProvider instanceProvider
115 public function testGetGlobalId( Site
$site ) {
116 $this->assertTypeOrValue( 'string', $site->getGlobalId(), null );
120 * @dataProvider instanceProvider
123 public function testSetGlobalId( Site
$site ) {
124 $site->setGlobalId( 'foobar' );
125 $this->assertEquals( 'foobar', $site->getGlobalId() );
129 * @dataProvider instanceProvider
132 public function testGetType( Site
$site ) {
133 $this->assertInternalType( 'string', $site->getType() );
137 * @dataProvider instanceProvider
140 public function testGetPath( Site
$site ) {
141 $this->assertTypeOrValue( 'string', $site->getPath( 'page_path' ), null );
142 $this->assertTypeOrValue( 'string', $site->getPath( 'file_path' ), null );
143 $this->assertTypeOrValue( 'string', $site->getPath( 'foobar' ), null );
147 * @dataProvider instanceProvider
150 public function testGetAllPaths( Site
$site ) {
151 $this->assertInternalType( 'array', $site->getAllPaths() );
155 * @dataProvider instanceProvider
158 public function testSetAndRemovePath( Site
$site ) {
159 $count = count( $site->getAllPaths() );
161 $site->setPath( 'spam', 'http://www.wikidata.org/$1' );
162 $site->setPath( 'spam', 'http://www.wikidata.org/foo/$1' );
163 $site->setPath( 'foobar', 'http://www.wikidata.org/bar/$1' );
165 $this->assertEquals( $count +
2, count( $site->getAllPaths() ) );
167 $this->assertInternalType( 'string', $site->getPath( 'foobar' ) );
168 $this->assertEquals( 'http://www.wikidata.org/foo/$1', $site->getPath( 'spam' ) );
170 $site->removePath( 'spam' );
171 $site->removePath( 'foobar' );
173 $this->assertEquals( $count, count( $site->getAllPaths() ) );
175 $this->assertNull( $site->getPath( 'foobar' ) );
176 $this->assertNull( $site->getPath( 'spam' ) );
179 public function testSetLinkPath() {
181 $path = "TestPath/$1";
183 $site->setLinkPath( $path );
184 $this->assertEquals( $path, $site->getLinkPath() );
187 public function testGetLinkPathType() {
190 $path = 'TestPath/$1';
191 $site->setLinkPath( $path );
192 $this->assertEquals( $path, $site->getPath( $site->getLinkPathType() ) );
194 $path = 'AnotherPath/$1';
195 $site->setPath( $site->getLinkPathType(), $path );
196 $this->assertEquals( $path, $site->getLinkPath() );
199 public function testSetPath() {
202 $path = 'TestPath/$1';
203 $site->setPath( 'foo', $path );
205 $this->assertEquals( $path, $site->getPath( 'foo' ) );
208 public function testProtocolRelativePath() {
211 $type = $site->getLinkPathType();
212 $path = '//acme.com/'; // protocol-relative URL
213 $site->setPath( $type, $path );
215 $this->assertEquals( '', $site->getProtocol() );
218 public function provideGetPageUrl() {
219 //NOTE: the assumption that the URL is built by replacing $1
220 // with the urlencoded version of $page
221 // is true for Site but not guaranteed for subclasses.
222 // Subclasses need to override this provider appropriately.
226 'http://acme.test/TestPath/$1',
231 'http://acme.test/TestScript?x=$1&y=bla',
233 'TestScript?x=Foo&y=bla',
236 'http://acme.test/TestPath/$1',
237 'foo & bar/xyzzy (quux-shmoox?)',
238 '/TestPath/foo%20%26%20bar%2Fxyzzy%20%28quux-shmoox%3F%29',
244 * @dataProvider provideGetPageUrl
246 public function testGetPageUrl( $path, $page, $expected ) {
249 //NOTE: the assumption that getPageUrl is based on getLinkPath
250 // is true for Site but not guaranteed for subclasses.
251 // Subclasses need to override this test case appropriately.
252 $site->setLinkPath( $path );
253 $this->assertContains( $path, $site->getPageUrl() );
255 $this->assertContains( $expected, $site->getPageUrl( $page ) );
258 protected function assertTypeOrFalse( $type, $value ) {
259 if ( $value === false ) {
260 $this->assertTrue( true );
263 $this->assertInternalType( $type, $value );