Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / tests / phpunit / includes / interwiki / InterwikiLookupAdapterTest.php
blob4754b040f97ff5c16c601edd76d16b58e1736a23
1 <?php
3 /**
4 * @covers MediaWiki\Interwiki\InterwikiLookupAdapter
6 * @group MediaWiki
7 * @group Interwiki
8 */
9 use MediaWiki\Interwiki\InterwikiLookupAdapter;
11 class InterwikiLookupAdapterTest extends MediaWikiTestCase {
13 /**
14 * @var InterwikiLookupAdapter
16 private $interwikiLookup;
18 protected function setUp() {
19 parent::setUp();
21 $this->interwikiLookup = new InterwikiLookupAdapter(
22 $this->getSiteLookup( $this->getSites() )
26 public function testIsValidInterwiki() {
27 $this->assertTrue(
28 $this->interwikiLookup->isValidInterwiki( 'enwt' ),
29 'enwt known prefix is valid'
31 $this->assertTrue(
32 $this->interwikiLookup->isValidInterwiki( 'foo' ),
33 'foo site known prefix is valid'
35 $this->assertFalse(
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() {
63 $this->assertEquals(
64 [ 'foo', 'enwt' ],
65 $this->interwikiLookup->getAllPrefixes(),
66 'getAllPrefixes()'
69 $this->assertEquals(
70 [ 'foo' ],
71 $this->interwikiLookup->getAllPrefixes( false ),
72 'get external prefixes'
75 $this->assertEquals(
76 [ 'enwt' ],
77 $this->interwikiLookup->getAllPrefixes( true ),
78 'get local prefixes'
82 private function getSiteLookup( SiteList $sites ) {
83 $siteLookup = $this->getMockBuilder( SiteLookup::class )
84 ->disableOriginalConstructor()
85 ->getMock();
87 $siteLookup->expects( $this->any() )
88 ->method( 'getSites' )
89 ->will( $this->returnValue( $sites ) );
91 return $siteLookup;
94 private function getSites() {
95 $sites = [];
97 $site = new Site();
98 $site->setGlobalId( 'foobar' );
99 $site->addInterwikiId( 'foo' );
100 $site->setSource( 'external' );
101 $sites[] = $site;
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" );
112 $sites[] = $site;
114 return new SiteList( $sites );