Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / tests / phpunit / includes / specials / SpecialGoToInterwikiTest.php
1 <?php
2
3 use MediaWiki\Interwiki\InterwikiLookupAdapter;
4 use MediaWiki\MediaWikiServices;
5
6 /**
7 * @covers SpecialGoToInterwiki
8 */
9 class SpecialGoToInterwikiTest extends MediaWikiTestCase {
10
11 public function testExecute() {
12 $this->setService( 'InterwikiLookup', new InterwikiLookupAdapter(
13 new HashSiteStore(), // won't be used
14 [
15 'local' => new Interwiki( 'local', 'https://local.example.com/$1',
16 'https://local.example.com/api.php', 'unittest_localwiki', 1 ),
17 'nonlocal' => new Interwiki( 'nonlocal', 'https://nonlocal.example.com/$1',
18 'https://nonlocal.example.com/api.php', 'unittest_nonlocalwiki', 0 ),
19 ]
20 ) );
21 MediaWikiServices::getInstance()->resetServiceForTesting( 'TitleFormatter' );
22 MediaWikiServices::getInstance()->resetServiceForTesting( 'TitleParser' );
23 MediaWikiServices::getInstance()->resetServiceForTesting( '_MediaWikiTitleCodec' );
24
25 // sanity check
26 $this->assertTrue( !Title::newFromText( 'Foo' )->isExternal() );
27 $this->assertTrue( Title::newFromText( 'local:Foo' )->isExternal() );
28 $this->assertTrue( Title::newFromText( 'nonlocal:Foo' )->isExternal() );
29 $this->assertTrue( Title::newFromText( 'local:Foo' )->isLocal() );
30 $this->assertTrue( !Title::newFromText( 'nonlocal:Foo' )->isLocal() );
31
32 $goToInterwiki = MediaWikiServices::getInstance()->getSpecialPageFactory()
33 ->getPage( 'GoToInterwiki' );
34
35 RequestContext::resetMain();
36 $context = new DerivativeContext( RequestContext::getMain() );
37 $goToInterwiki->setContext( $context );
38 $goToInterwiki->execute( 'Foo' );
39 $this->assertSame( Title::newFromText( 'Foo' )->getFullURL(),
40 $context->getOutput()->getRedirect() );
41
42 RequestContext::resetMain();
43 $context = new DerivativeContext( RequestContext::getMain() );
44 $goToInterwiki->setContext( $context );
45 $goToInterwiki->execute( 'local:Foo' );
46 $this->assertSame( Title::newFromText( 'local:Foo' )->getFullURL(),
47 $context->getOutput()->getRedirect() );
48
49 RequestContext::resetMain();
50 $context = new DerivativeContext( RequestContext::getMain() );
51 $goToInterwiki->setContext( $context );
52 $goToInterwiki->execute( 'nonlocal:Foo' );
53 $this->assertSame( '', $context->getOutput()->getRedirect() );
54 $this->assertContains( Title::newFromText( 'nonlocal:Foo' )->getFullURL(),
55 $context->getOutput()->getHTML() );
56
57 RequestContext::resetMain();
58 $context = new DerivativeContext( RequestContext::getMain() );
59 $goToInterwiki->setContext( $context );
60 $goToInterwiki->execute( 'force/Foo' );
61 $this->assertSame( Title::newFromText( 'Foo' )->getFullURL(),
62 $context->getOutput()->getRedirect() );
63
64 RequestContext::resetMain();
65 $context = new DerivativeContext( RequestContext::getMain() );
66 $goToInterwiki->setContext( $context );
67 $goToInterwiki->execute( 'force/local:Foo' );
68 $this->assertSame( '', $context->getOutput()->getRedirect() );
69 $this->assertContains( Title::newFromText( 'local:Foo' )->getFullURL(),
70 $context->getOutput()->getHTML() );
71
72 RequestContext::resetMain();
73 $context = new DerivativeContext( RequestContext::getMain() );
74 $goToInterwiki->setContext( $context );
75 $goToInterwiki->execute( 'force/nonlocal:Foo' );
76 $this->assertSame( '', $context->getOutput()->getRedirect() );
77 $this->assertContains( Title::newFromText( 'nonlocal:Foo' )->getFullURL(),
78 $context->getOutput()->getHTML() );
79 }
80
81 }