4 * @covers MediaWiki\Interwiki\InterwikiLookupAdapter
9 use MediaWiki\Interwiki\InterwikiLookupAdapter
;
11 class InterwikiLookupAdapterTest
extends MediaWikiTestCase
{
14 * @var InterwikiLookupAdapter
16 private $interwikiLookup;
18 protected function setUp() {
21 $this->interwikiLookup
= new InterwikiLookupAdapter(
22 $this->getSiteLookup( $this->getSites() )
26 public function testIsValidInterwiki() {
28 $this->interwikiLookup
->isValidInterwiki( 'enwt' ),
29 'enwt known prefix is valid'
32 $this->interwikiLookup
->isValidInterwiki( 'foo' ),
33 'foo site known prefix is valid'
36 $this->interwikiLookup
->isValidInterwiki( 'xyz' ),
37 'unknown prefix is not valid'
41 public function testFetch() {
43 $interwiki = $this->interwikiLookup
->fetch( '' );
44 $this->assertNull( $interwiki );
46 $interwiki = $this->interwikiLookup
->fetch( 'xyz' );
47 $this->assertFalse( $interwiki );
49 $interwiki = $this->interwikiLookup
->fetch( 'foo' );
50 $this->assertInstanceOf( Interwiki
::class, $interwiki );
51 $this->assertSame( 'foobar', $interwiki->getWikiID() );
53 $interwiki = $this->interwikiLookup
->fetch( 'enwt' );
54 $this->assertInstanceOf( Interwiki
::class, $interwiki );
56 $this->assertSame( 'https://en.wiktionary.org/wiki/$1', $interwiki->getURL(), 'getURL' );
57 $this->assertSame( 'https://en.wiktionary.org/w/api.php', $interwiki->getAPI(), 'getAPI' );
58 $this->assertSame( 'enwiktionary', $interwiki->getWikiID(), 'getWikiID' );
59 $this->assertTrue( $interwiki->isLocal(), 'isLocal' );
62 public function testGetAllPrefixes() {
65 $this->interwikiLookup
->getAllPrefixes(),
71 $this->interwikiLookup
->getAllPrefixes( false ),
72 'get external prefixes'
77 $this->interwikiLookup
->getAllPrefixes( true ),
82 private function getSiteLookup( SiteList
$sites ) {
83 $siteLookup = $this->getMockBuilder( SiteLookup
::class )
84 ->disableOriginalConstructor()
87 $siteLookup->expects( $this->any() )
88 ->method( 'getSites' )
89 ->will( $this->returnValue( $sites ) );
94 private function getSites() {
98 $site->setGlobalId( 'foobar' );
99 $site->addInterwikiId( 'foo' );
100 $site->setSource( 'external' );
103 $site = new MediaWikiSite();
104 $site->setGlobalId( 'enwiktionary' );
105 $site->setGroup( 'wiktionary' );
106 $site->setLanguageCode( 'en' );
107 $site->addNavigationId( 'enwiktionary' );
108 $site->addInterwikiId( 'enwt' );
109 $site->setSource( 'local' );
110 $site->setPath( MediaWikiSite
::PATH_PAGE
, "https://en.wiktionary.org/wiki/$1" );
111 $site->setPath( MediaWikiSite
::PATH_FILE
, "https://en.wiktionary.org/w/$1" );
114 return new SiteList( $sites );