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
41 * @covers Site::getInterwikiIds
43 public function testGetInterwikiIds( Site
$site ) {
44 $this->assertInternalType( 'array', $site->getInterwikiIds() );
48 * @dataProvider instanceProvider
50 * @covers Site::getNavigationIds
52 public function testGetNavigationIds( Site
$site ) {
53 $this->assertInternalType( 'array', $site->getNavigationIds() );
57 * @dataProvider instanceProvider
59 * @covers Site::addNavigationId
61 public function testAddNavigationId( Site
$site ) {
62 $site->addNavigationId( 'foobar' );
63 $this->assertTrue( in_array( 'foobar', $site->getNavigationIds(), true ) );
67 * @dataProvider instanceProvider
69 * @covers Site::addInterwikiId
71 public function testAddInterwikiId( Site
$site ) {
72 $site->addInterwikiId( 'foobar' );
73 $this->assertTrue( in_array( 'foobar', $site->getInterwikiIds(), true ) );
77 * @dataProvider instanceProvider
79 * @covers Site::getLanguageCode
81 public function testGetLanguageCode( Site
$site ) {
82 $this->assertTypeOrValue( 'string', $site->getLanguageCode(), null );
86 * @dataProvider instanceProvider
88 * @covers Site::setLanguageCode
90 public function testSetLanguageCode( Site
$site ) {
91 $site->setLanguageCode( 'en' );
92 $this->assertEquals( 'en', $site->getLanguageCode() );
96 * @dataProvider instanceProvider
98 * @covers Site::normalizePageName
100 public function testNormalizePageName( Site
$site ) {
101 $this->assertInternalType( 'string', $site->normalizePageName( 'Foobar' ) );
105 * @dataProvider instanceProvider
107 * @covers Site::getGlobalId
109 public function testGetGlobalId( Site
$site ) {
110 $this->assertTypeOrValue( 'string', $site->getGlobalId(), null );
114 * @dataProvider instanceProvider
116 * @covers Site::setGlobalId
118 public function testSetGlobalId( Site
$site ) {
119 $site->setGlobalId( 'foobar' );
120 $this->assertEquals( 'foobar', $site->getGlobalId() );
124 * @dataProvider instanceProvider
126 * @covers Site::getType
128 public function testGetType( Site
$site ) {
129 $this->assertInternalType( 'string', $site->getType() );
133 * @dataProvider instanceProvider
135 * @covers Site::getPath
137 public function testGetPath( Site
$site ) {
138 $this->assertTypeOrValue( 'string', $site->getPath( 'page_path' ), null );
139 $this->assertTypeOrValue( 'string', $site->getPath( 'file_path' ), null );
140 $this->assertTypeOrValue( 'string', $site->getPath( 'foobar' ), null );
144 * @dataProvider instanceProvider
146 * @covers Site::getAllPaths
148 public function testGetAllPaths( Site
$site ) {
149 $this->assertInternalType( 'array', $site->getAllPaths() );
153 * @dataProvider instanceProvider
155 * @covers Site::setPath
156 * @covers Site::removePath
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' ) );
180 * @covers Site::setLinkPath
182 public function testSetLinkPath() {
184 $path = "TestPath/$1";
186 $site->setLinkPath( $path );
187 $this->assertEquals( $path, $site->getLinkPath() );
191 * @covers Site::getLinkPathType
193 public function testGetLinkPathType() {
196 $path = 'TestPath/$1';
197 $site->setLinkPath( $path );
198 $this->assertEquals( $path, $site->getPath( $site->getLinkPathType() ) );
200 $path = 'AnotherPath/$1';
201 $site->setPath( $site->getLinkPathType(), $path );
202 $this->assertEquals( $path, $site->getLinkPath() );
206 * @covers Site::setPath
208 public function testSetPath() {
211 $path = 'TestPath/$1';
212 $site->setPath( 'foo', $path );
214 $this->assertEquals( $path, $site->getPath( 'foo' ) );
218 * @covers Site::setPath
219 * @covers Site::getProtocol
221 public function testProtocolRelativePath() {
224 $type = $site->getLinkPathType();
225 $path = '//acme.com/'; // protocol-relative URL
226 $site->setPath( $type, $path );
228 $this->assertEquals( '', $site->getProtocol() );
231 public static function provideGetPageUrl() {
232 //NOTE: the assumption that the URL is built by replacing $1
233 // with the urlencoded version of $page
234 // is true for Site but not guaranteed for subclasses.
235 // Subclasses need to override this provider appropriately.
239 'http://acme.test/TestPath/$1',
244 'http://acme.test/TestScript?x=$1&y=bla',
246 'TestScript?x=Foo&y=bla',
249 'http://acme.test/TestPath/$1',
250 'foo & bar/xyzzy (quux-shmoox?)',
251 '/TestPath/foo%20%26%20bar%2Fxyzzy%20%28quux-shmoox%3F%29',
257 * @dataProvider provideGetPageUrl
258 * @covers Site::getPageUrl
260 public function testGetPageUrl( $path, $page, $expected ) {
263 //NOTE: the assumption that getPageUrl is based on getLinkPath
264 // is true for Site but not guaranteed for subclasses.
265 // Subclasses need to override this test case appropriately.
266 $site->setLinkPath( $path );
267 $this->assertContains( $path, $site->getPageUrl() );
269 $this->assertContains( $expected, $site->getPageUrl( $page ) );
272 protected function assertTypeOrFalse( $type, $value ) {
273 if ( $value === false ) {
274 $this->assertTrue( true );
276 $this->assertInternalType( $type, $value );
281 * @dataProvider instanceProvider
283 * @covers Site::serialize
284 * @covers Site::unserialize
286 public function testSerialization( Site
$site ) {
287 $this->assertInstanceOf( 'Serializable', $site );
289 $serialization = serialize( $site );
290 $newInstance = unserialize( $serialization );
292 $this->assertInstanceOf( 'Site', $newInstance );
294 $this->assertEquals( $serialization, serialize( $newInstance ) );