MediaWiki  REL1_21
SiteTest.php
Go to the documentation of this file.
00001 <?php
00002 
00032 class SiteTest extends MediaWikiTestCase {
00033 
00034         public function instanceProvider() {
00035                 return $this->arrayWrap( TestSites::getSites() );
00036         }
00037 
00042         public function testGetInterwikiIds( Site $site ) {
00043                 $this->assertInternalType( 'array', $site->getInterwikiIds() );
00044         }
00045 
00050         public function testGetNavigationIds( Site $site ) {
00051                 $this->assertInternalType( 'array', $site->getNavigationIds() );
00052         }
00053 
00058         public function testAddNavigationId( Site $site ) {
00059                 $site->addNavigationId( 'foobar' );
00060                 $this->assertTrue( in_array( 'foobar', $site->getNavigationIds(), true ) );
00061         }
00062 
00067         public function testAddInterwikiId( Site $site ) {
00068                 $site->addInterwikiId( 'foobar' );
00069                 $this->assertTrue( in_array( 'foobar', $site->getInterwikiIds(), true ) );
00070         }
00071 
00076         public function testGetLanguageCode( Site $site ) {
00077                 $this->assertTypeOrValue( 'string', $site->getLanguageCode(), null );
00078         }
00079 
00084         public function testSetLanguageCode( Site $site ) {
00085                 $site->setLanguageCode( 'en' );
00086                 $this->assertEquals( 'en', $site->getLanguageCode() );
00087         }
00088 
00093         public function testNormalizePageName( Site $site ) {
00094                 $this->assertInternalType( 'string', $site->normalizePageName( 'Foobar' ) );
00095         }
00096 
00101         public function testGetGlobalId( Site $site ) {
00102                 $this->assertTypeOrValue( 'string', $site->getGlobalId(), null );
00103         }
00104 
00109         public function testSetGlobalId( Site $site ) {
00110                 $site->setGlobalId( 'foobar' );
00111                 $this->assertEquals( 'foobar', $site->getGlobalId() );
00112         }
00113 
00118         public function testGetType( Site $site ) {
00119                 $this->assertInternalType( 'string', $site->getType() );
00120         }
00121 
00126         public function testGetPath( Site $site ) {
00127                 $this->assertTypeOrValue( 'string', $site->getPath( 'page_path' ), null );
00128                 $this->assertTypeOrValue( 'string', $site->getPath( 'file_path' ), null );
00129                 $this->assertTypeOrValue( 'string', $site->getPath( 'foobar' ), null );
00130         }
00131 
00136         public function testGetAllPaths( Site $site ) {
00137                 $this->assertInternalType( 'array', $site->getAllPaths() );
00138         }
00139 
00144         public function testSetAndRemovePath( Site $site ) {
00145                 $count = count( $site->getAllPaths() );
00146 
00147                 $site->setPath( 'spam', 'http://www.wikidata.org/$1' );
00148                 $site->setPath( 'spam', 'http://www.wikidata.org/foo/$1' );
00149                 $site->setPath( 'foobar', 'http://www.wikidata.org/bar/$1' );
00150 
00151                 $this->assertEquals( $count + 2, count( $site->getAllPaths() ) );
00152 
00153                 $this->assertInternalType( 'string', $site->getPath( 'foobar' ) );
00154                 $this->assertEquals( 'http://www.wikidata.org/foo/$1', $site->getPath( 'spam' ) );
00155 
00156                 $site->removePath( 'spam' );
00157                 $site->removePath( 'foobar' );
00158 
00159                 $this->assertEquals( $count, count( $site->getAllPaths() ) );
00160 
00161                 $this->assertNull( $site->getPath( 'foobar' ) );
00162                 $this->assertNull( $site->getPath( 'spam' ) );
00163         }
00164 
00165         public function testSetLinkPath() {
00166                 $site = new Site();
00167                 $path = "TestPath/$1";
00168 
00169                 $site->setLinkPath( $path );
00170                 $this->assertEquals( $path, $site->getLinkPath() );
00171         }
00172 
00173         public function testGetLinkPathType() {
00174                 $site = new Site();
00175 
00176                 $path = 'TestPath/$1';
00177                 $site->setLinkPath( $path );
00178                 $this->assertEquals( $path, $site->getPath( $site->getLinkPathType() ) );
00179 
00180                 $path = 'AnotherPath/$1';
00181                 $site->setPath( $site->getLinkPathType(), $path );
00182                 $this->assertEquals( $path, $site->getLinkPath() );
00183         }
00184 
00185         public function testSetPath() {
00186                 $site = new Site();
00187 
00188                 $path = 'TestPath/$1';
00189                 $site->setPath( 'foo', $path );
00190 
00191                 $this->assertEquals( $path, $site->getPath( 'foo' ) );
00192         }
00193 
00194         public function testProtocolRelativePath() {
00195                 $site = new Site();
00196 
00197                 $type = $site->getLinkPathType();
00198                 $path = '//acme.com/'; // protocol-relative URL
00199                 $site->setPath( $type, $path );
00200 
00201                 $this->assertEquals( '', $site->getProtocol() );
00202         }
00203 
00204         public function provideGetPageUrl() {
00205                 //NOTE: the assumption that the URL is built by replacing $1
00206                 //      with the urlencoded version of $page
00207                 //      is true for Site but not guaranteed for subclasses.
00208                 //      Subclasses need to override this provider appropriately.
00209 
00210                 return array(
00211                         array( #0
00212                                 'http://acme.test/TestPath/$1',
00213                                 'Foo',
00214                                 '/TestPath/Foo',
00215                         ),
00216                         array( #1
00217                                 'http://acme.test/TestScript?x=$1&y=bla',
00218                                 'Foo',
00219                                 'TestScript?x=Foo&y=bla',
00220                         ),
00221                         array( #2
00222                                 'http://acme.test/TestPath/$1',
00223                                 'foo & bar/xyzzy (quux-shmoox?)',
00224                                 '/TestPath/foo%20%26%20bar%2Fxyzzy%20%28quux-shmoox%3F%29',
00225                         ),
00226                 );
00227         }
00228 
00232         public function testGetPageUrl( $path, $page, $expected ) {
00233                 $site = new Site();
00234 
00235                 //NOTE: the assumption that getPageUrl is based on getLinkPath
00236                 //      is true for Site but not guaranteed for subclasses.
00237                 //      Subclasses need to override this test case appropriately.
00238                 $site->setLinkPath( $path );
00239                 $this->assertContains( $path, $site->getPageUrl() );
00240 
00241                 $this->assertContains( $expected, $site->getPageUrl( $page ) );
00242         }
00243 
00244         protected function assertTypeOrFalse( $type, $value ) {
00245                 if ( $value === false ) {
00246                         $this->assertTrue( true );
00247                 } else {
00248                         $this->assertInternalType( $type, $value );
00249                 }
00250         }
00251 
00256         public function testSerialization( Site $site ) {
00257                 $this->assertInstanceOf( 'Serializable', $site );
00258 
00259                 $serialization = serialize( $site );
00260                 $newInstance = unserialize( $serialization );
00261 
00262                 $this->assertInstanceOf( 'Site', $newInstance );
00263 
00264                 $this->assertEquals( $serialization, serialize( $newInstance ) );
00265         }
00266 
00267 }