MediaWiki  REL1_22
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 
00043     public function testGetInterwikiIds( Site $site ) {
00044         $this->assertInternalType( 'array', $site->getInterwikiIds() );
00045     }
00046 
00052     public function testGetNavigationIds( Site $site ) {
00053         $this->assertInternalType( 'array', $site->getNavigationIds() );
00054     }
00055 
00061     public function testAddNavigationId( Site $site ) {
00062         $site->addNavigationId( 'foobar' );
00063         $this->assertTrue( in_array( 'foobar', $site->getNavigationIds(), true ) );
00064     }
00065 
00071     public function testAddInterwikiId( Site $site ) {
00072         $site->addInterwikiId( 'foobar' );
00073         $this->assertTrue( in_array( 'foobar', $site->getInterwikiIds(), true ) );
00074     }
00075 
00081     public function testGetLanguageCode( Site $site ) {
00082         $this->assertTypeOrValue( 'string', $site->getLanguageCode(), null );
00083     }
00084 
00090     public function testSetLanguageCode( Site $site ) {
00091         $site->setLanguageCode( 'en' );
00092         $this->assertEquals( 'en', $site->getLanguageCode() );
00093     }
00094 
00100     public function testNormalizePageName( Site $site ) {
00101         $this->assertInternalType( 'string', $site->normalizePageName( 'Foobar' ) );
00102     }
00103 
00109     public function testGetGlobalId( Site $site ) {
00110         $this->assertTypeOrValue( 'string', $site->getGlobalId(), null );
00111     }
00112 
00118     public function testSetGlobalId( Site $site ) {
00119         $site->setGlobalId( 'foobar' );
00120         $this->assertEquals( 'foobar', $site->getGlobalId() );
00121     }
00122 
00128     public function testGetType( Site $site ) {
00129         $this->assertInternalType( 'string', $site->getType() );
00130     }
00131 
00137     public function testGetPath( Site $site ) {
00138         $this->assertTypeOrValue( 'string', $site->getPath( 'page_path' ), null );
00139         $this->assertTypeOrValue( 'string', $site->getPath( 'file_path' ), null );
00140         $this->assertTypeOrValue( 'string', $site->getPath( 'foobar' ), null );
00141     }
00142 
00148     public function testGetAllPaths( Site $site ) {
00149         $this->assertInternalType( 'array', $site->getAllPaths() );
00150     }
00151 
00158     public function testSetAndRemovePath( Site $site ) {
00159         $count = count( $site->getAllPaths() );
00160 
00161         $site->setPath( 'spam', 'http://www.wikidata.org/$1' );
00162         $site->setPath( 'spam', 'http://www.wikidata.org/foo/$1' );
00163         $site->setPath( 'foobar', 'http://www.wikidata.org/bar/$1' );
00164 
00165         $this->assertEquals( $count + 2, count( $site->getAllPaths() ) );
00166 
00167         $this->assertInternalType( 'string', $site->getPath( 'foobar' ) );
00168         $this->assertEquals( 'http://www.wikidata.org/foo/$1', $site->getPath( 'spam' ) );
00169 
00170         $site->removePath( 'spam' );
00171         $site->removePath( 'foobar' );
00172 
00173         $this->assertEquals( $count, count( $site->getAllPaths() ) );
00174 
00175         $this->assertNull( $site->getPath( 'foobar' ) );
00176         $this->assertNull( $site->getPath( 'spam' ) );
00177     }
00178 
00182     public function testSetLinkPath() {
00183         $site = new Site();
00184         $path = "TestPath/$1";
00185 
00186         $site->setLinkPath( $path );
00187         $this->assertEquals( $path, $site->getLinkPath() );
00188     }
00189 
00193     public function testGetLinkPathType() {
00194         $site = new Site();
00195 
00196         $path = 'TestPath/$1';
00197         $site->setLinkPath( $path );
00198         $this->assertEquals( $path, $site->getPath( $site->getLinkPathType() ) );
00199 
00200         $path = 'AnotherPath/$1';
00201         $site->setPath( $site->getLinkPathType(), $path );
00202         $this->assertEquals( $path, $site->getLinkPath() );
00203     }
00204 
00208     public function testSetPath() {
00209         $site = new Site();
00210 
00211         $path = 'TestPath/$1';
00212         $site->setPath( 'foo', $path );
00213 
00214         $this->assertEquals( $path, $site->getPath( 'foo' ) );
00215     }
00216 
00221     public function testProtocolRelativePath() {
00222         $site = new Site();
00223 
00224         $type = $site->getLinkPathType();
00225         $path = '//acme.com/'; // protocol-relative URL
00226         $site->setPath( $type, $path );
00227 
00228         $this->assertEquals( '', $site->getProtocol() );
00229     }
00230 
00231     public static function provideGetPageUrl() {
00232         //NOTE: the assumption that the URL is built by replacing $1
00233         //      with the urlencoded version of $page
00234         //      is true for Site but not guaranteed for subclasses.
00235         //      Subclasses need to override this provider appropriately.
00236 
00237         return array(
00238             array( #0
00239                 'http://acme.test/TestPath/$1',
00240                 'Foo',
00241                 '/TestPath/Foo',
00242             ),
00243             array( #1
00244                 'http://acme.test/TestScript?x=$1&y=bla',
00245                 'Foo',
00246                 'TestScript?x=Foo&y=bla',
00247             ),
00248             array( #2
00249                 'http://acme.test/TestPath/$1',
00250                 'foo & bar/xyzzy (quux-shmoox?)',
00251                 '/TestPath/foo%20%26%20bar%2Fxyzzy%20%28quux-shmoox%3F%29',
00252             ),
00253         );
00254     }
00255 
00260     public function testGetPageUrl( $path, $page, $expected ) {
00261         $site = new Site();
00262 
00263         //NOTE: the assumption that getPageUrl is based on getLinkPath
00264         //      is true for Site but not guaranteed for subclasses.
00265         //      Subclasses need to override this test case appropriately.
00266         $site->setLinkPath( $path );
00267         $this->assertContains( $path, $site->getPageUrl() );
00268 
00269         $this->assertContains( $expected, $site->getPageUrl( $page ) );
00270     }
00271 
00272     protected function assertTypeOrFalse( $type, $value ) {
00273         if ( $value === false ) {
00274             $this->assertTrue( true );
00275         } else {
00276             $this->assertInternalType( $type, $value );
00277         }
00278     }
00279 
00286     public function testSerialization( Site $site ) {
00287         $this->assertInstanceOf( 'Serializable', $site );
00288 
00289         $serialization = serialize( $site );
00290         $newInstance = unserialize( $serialization );
00291 
00292         $this->assertInstanceOf( 'Site', $newInstance );
00293 
00294         $this->assertEquals( $serialization, serialize( $newInstance ) );
00295     }
00296 }