| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Interwiki\InterwikiLookupAdapter; |
| 4 |
use MediaWiki\Site\MediaWikiSite; |
| 5 |
use MediaWiki\Site\Site; |
| 6 |
use MediaWiki\Site\SiteList; |
| 7 |
use MediaWiki\Site\SiteLookup; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class InterwikiLookupAdapterTest extends \MediaWikiUnitTestCase { |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
private $interwikiLookup; |
| 19 |
|
| 20 |
protected function setUp(): void { |
| 21 |
parent::setUp(); |
| 22 |
|
| 23 |
$this->interwikiLookup = new InterwikiLookupAdapter( |
| 24 |
$this->getSiteLookup( $this->getSites() ) |
| 25 |
); |
| 26 |
} |
| 27 |
|
| 28 |
public function testIsValidInterwiki() { |
| 29 |
$this->assertTrue( |
| 30 |
$this->interwikiLookup->isValidInterwiki( 'enwt' ), |
| 31 |
'enwt known prefix is valid' |
| 32 |
); |
| 33 |
$this->assertTrue( |
| 34 |
$this->interwikiLookup->isValidInterwiki( 'foo' ), |
| 35 |
'foo site known prefix is valid' |
| 36 |
); |
| 37 |
$this->assertFalse( |
| 38 |
$this->interwikiLookup->isValidInterwiki( 'xyz' ), |
| 39 |
'unknown prefix is not valid' |
| 40 |
); |
| 41 |
} |
| 42 |
|
| 43 |
public function testFetch() { |
| 44 |
$interwiki = $this->interwikiLookup->fetch( '' ); |
| 45 |
$this->assertNull( $interwiki ); |
| 46 |
|
| 47 |
$interwiki = $this->interwikiLookup->fetch( 'xyz' ); |
| 48 |
$this->assertFalse( $interwiki ); |
| 49 |
|
| 50 |
$interwiki = $this->interwikiLookup->fetch( 'foo' ); |
| 51 |
$this->assertInstanceOf( Interwiki::class, $interwiki ); |
| 52 |
$this->assertSame( 'foobar', $interwiki->getWikiID() ); |
| 53 |
|
| 54 |
$interwiki = $this->interwikiLookup->fetch( 'enwt' ); |
| 55 |
$this->assertInstanceOf( Interwiki::class, $interwiki ); |
| 56 |
|
| 57 |
$this->assertSame( 'https://en.wiktionary.org/wiki/$1', $interwiki->getURL(), 'getURL' ); |
| 58 |
$this->assertSame( 'https://en.wiktionary.org/w/api.php', $interwiki->getAPI(), 'getAPI' ); |
| 59 |
$this->assertSame( 'enwiktionary', $interwiki->getWikiID(), 'getWikiID' ); |
| 60 |
$this->assertTrue( $interwiki->isLocal(), 'isLocal' ); |
| 61 |
} |
| 62 |
|
| 63 |
public function testGetAllPrefixes() { |
| 64 |
$foo = [ |
| 65 |
'iw_prefix' => 'foo', |
| 66 |
'iw_url' => '', |
| 67 |
'iw_api' => '', |
| 68 |
'iw_wikiid' => 'foobar', |
| 69 |
'iw_local' => false, |
| 70 |
'iw_trans' => false, |
| 71 |
]; |
| 72 |
$enwt = [ |
| 73 |
'iw_prefix' => 'enwt', |
| 74 |
'iw_url' => 'https://en.wiktionary.org/wiki/$1', |
| 75 |
'iw_api' => 'https://en.wiktionary.org/w/api.php', |
| 76 |
'iw_wikiid' => 'enwiktionary', |
| 77 |
'iw_local' => true, |
| 78 |
'iw_trans' => false, |
| 79 |
]; |
| 80 |
|
| 81 |
$this->assertEquals( |
| 82 |
[ $foo, $enwt ], |
| 83 |
$this->interwikiLookup->getAllPrefixes(), |
| 84 |
'getAllPrefixes()' |
| 85 |
); |
| 86 |
|
| 87 |
$this->assertEquals( |
| 88 |
[ $foo ], |
| 89 |
$this->interwikiLookup->getAllPrefixes( false ), |
| 90 |
'get external prefixes' |
| 91 |
); |
| 92 |
|
| 93 |
$this->assertEquals( |
| 94 |
[ $enwt ], |
| 95 |
$this->interwikiLookup->getAllPrefixes( true ), |
| 96 |
'get local prefixes' |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
private function getSiteLookup( SiteList $sites ) { |
| 101 |
$siteLookup = $this->createMock( SiteLookup::class ); |
| 102 |
|
| 103 |
$siteLookup->method( 'getSites' ) |
| 104 |
->willReturn( $sites ); |
| 105 |
|
| 106 |
return $siteLookup; |
| 107 |
} |
| 108 |
|
| 109 |
private function getSites() { |
| 110 |
$sites = []; |
| 111 |
|
| 112 |
$site = new Site(); |
| 113 |
$site->setGlobalId( 'foobar' ); |
| 114 |
$site->addInterwikiId( 'foo' ); |
| 115 |
$site->setSource( 'external' ); |
| 116 |
$sites[] = $site; |
| 117 |
|
| 118 |
$site = new MediaWikiSite(); |
| 119 |
$site->setGlobalId( 'enwiktionary' ); |
| 120 |
$site->setGroup( 'wiktionary' ); |
| 121 |
$site->addNavigationId( 'enwiktionary' ); |
| 122 |
$site->addInterwikiId( 'enwt' ); |
| 123 |
$site->setSource( 'local' ); |
| 124 |
$site->setPath( MediaWikiSite::PATH_PAGE, "https://en.wiktionary.org/wiki/$1" ); |
| 125 |
$site->setPath( MediaWikiSite::PATH_FILE, "https://en.wiktionary.org/w/$1" ); |
| 126 |
$sites[] = $site; |
| 127 |
|
| 128 |
return new SiteList( $sites ); |
| 129 |
} |
| 130 |
|
| 131 |
} |