File: InterwikiLookupAdapterTest.php

package info (click to toggle)
mediawiki 1%3A1.43.5%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 420,592 kB
  • sloc: php: 1,064,309; javascript: 664,315; sql: 9,737; python: 5,743; xml: 3,489; sh: 1,131; makefile: 64
file content (131 lines) | stat: -rw-r--r-- 3,388 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php

use MediaWiki\Interwiki\InterwikiLookupAdapter;
use MediaWiki\Site\MediaWikiSite;
use MediaWiki\Site\Site;
use MediaWiki\Site\SiteList;
use MediaWiki\Site\SiteLookup;

/**
 * @covers \MediaWiki\Interwiki\InterwikiLookupAdapter
 * @group Interwiki
 */
class InterwikiLookupAdapterTest extends \MediaWikiUnitTestCase {

	/**
	 * @var InterwikiLookupAdapter
	 */
	private $interwikiLookup;

	protected function setUp(): void {
		parent::setUp();

		$this->interwikiLookup = new InterwikiLookupAdapter(
			$this->getSiteLookup( $this->getSites() )
		);
	}

	public function testIsValidInterwiki() {
		$this->assertTrue(
			$this->interwikiLookup->isValidInterwiki( 'enwt' ),
			'enwt known prefix is valid'
		);
		$this->assertTrue(
			$this->interwikiLookup->isValidInterwiki( 'foo' ),
			'foo site known prefix is valid'
		);
		$this->assertFalse(
			$this->interwikiLookup->isValidInterwiki( 'xyz' ),
			'unknown prefix is not valid'
		);
	}

	public function testFetch() {
		$interwiki = $this->interwikiLookup->fetch( '' );
		$this->assertNull( $interwiki );

		$interwiki = $this->interwikiLookup->fetch( 'xyz' );
		$this->assertFalse( $interwiki );

		$interwiki = $this->interwikiLookup->fetch( 'foo' );
		$this->assertInstanceOf( Interwiki::class, $interwiki );
		$this->assertSame( 'foobar', $interwiki->getWikiID() );

		$interwiki = $this->interwikiLookup->fetch( 'enwt' );
		$this->assertInstanceOf( Interwiki::class, $interwiki );

		$this->assertSame( 'https://en.wiktionary.org/wiki/$1', $interwiki->getURL(), 'getURL' );
		$this->assertSame( 'https://en.wiktionary.org/w/api.php', $interwiki->getAPI(), 'getAPI' );
		$this->assertSame( 'enwiktionary', $interwiki->getWikiID(), 'getWikiID' );
		$this->assertTrue( $interwiki->isLocal(), 'isLocal' );
	}

	public function testGetAllPrefixes() {
		$foo = [
			'iw_prefix' => 'foo',
			'iw_url' => '',
			'iw_api' => '',
			'iw_wikiid' => 'foobar',
			'iw_local' => false,
			'iw_trans' => false,
		];
		$enwt = [
			'iw_prefix' => 'enwt',
			'iw_url' => 'https://en.wiktionary.org/wiki/$1',
			'iw_api' => 'https://en.wiktionary.org/w/api.php',
			'iw_wikiid' => 'enwiktionary',
			'iw_local' => true,
			'iw_trans' => false,
		];

		$this->assertEquals(
			[ $foo, $enwt ],
			$this->interwikiLookup->getAllPrefixes(),
			'getAllPrefixes()'
		);

		$this->assertEquals(
			[ $foo ],
			$this->interwikiLookup->getAllPrefixes( false ),
			'get external prefixes'
		);

		$this->assertEquals(
			[ $enwt ],
			$this->interwikiLookup->getAllPrefixes( true ),
			'get local prefixes'
		);
	}

	private function getSiteLookup( SiteList $sites ) {
		$siteLookup = $this->createMock( SiteLookup::class );

		$siteLookup->method( 'getSites' )
			->willReturn( $sites );

		return $siteLookup;
	}

	private function getSites() {
		$sites = [];

		$site = new Site();
		$site->setGlobalId( 'foobar' );
		$site->addInterwikiId( 'foo' );
		$site->setSource( 'external' );
		$sites[] = $site;

		$site = new MediaWikiSite();
		$site->setGlobalId( 'enwiktionary' );
		$site->setGroup( 'wiktionary' );
		$site->addNavigationId( 'enwiktionary' );
		$site->addInterwikiId( 'enwt' );
		$site->setSource( 'local' );
		$site->setPath( MediaWikiSite::PATH_PAGE, "https://en.wiktionary.org/wiki/$1" );
		$site->setPath( MediaWikiSite::PATH_FILE, "https://en.wiktionary.org/w/$1" );
		$sites[] = $site;

		return new SiteList( $sites );
	}

}