3 use MediaWiki\Context\DerivativeContext
;
4 use MediaWiki\Context\RequestContext
;
5 use MediaWiki\Interwiki\InterwikiLookupAdapter
;
6 use MediaWiki\Site\HashSiteStore
;
7 use MediaWiki\Title\Title
;
10 * @covers \MediaWiki\Specials\SpecialGoToInterwiki
12 class SpecialGoToInterwikiTest
extends MediaWikiIntegrationTestCase
{
14 public function testExecute() {
15 $this->setService( 'InterwikiLookup', new InterwikiLookupAdapter(
16 new HashSiteStore(), // won't be used
18 'local' => new Interwiki( 'local', 'https://local.example.com/$1',
19 'https://local.example.com/api.php', 'unittest_localwiki', 1 ),
20 'nonlocal' => new Interwiki( 'nonlocal', 'https://nonlocal.example.com/$1',
21 'https://nonlocal.example.com/api.php', 'unittest_nonlocalwiki', 0 ),
24 $this->getServiceContainer()->resetServiceForTesting( 'TitleFormatter' );
25 $this->getServiceContainer()->resetServiceForTesting( 'TitleParser' );
26 $this->getServiceContainer()->resetServiceForTesting( '_MediaWikiTitleCodec' );
28 $this->assertNotTrue( Title
::newFromText( 'Foo' )->isExternal() );
29 $this->assertTrue( Title
::newFromText( 'local:Foo' )->isExternal() );
30 $this->assertTrue( Title
::newFromText( 'nonlocal:Foo' )->isExternal() );
31 $this->assertTrue( Title
::newFromText( 'local:Foo' )->isLocal() );
32 $this->assertNotTrue( Title
::newFromText( 'nonlocal:Foo' )->isLocal() );
34 $goToInterwiki = $this->getServiceContainer()->getSpecialPageFactory()
35 ->getPage( 'GoToInterwiki' );
37 RequestContext
::resetMain();
38 $context = new DerivativeContext( RequestContext
::getMain() );
39 $goToInterwiki->setContext( $context );
40 $goToInterwiki->execute( 'Foo' );
41 $this->assertSame( Title
::newFromText( 'Foo' )->getFullURL(),
42 $context->getOutput()->getRedirect() );
44 RequestContext
::resetMain();
45 $context = new DerivativeContext( RequestContext
::getMain() );
46 $goToInterwiki->setContext( $context );
47 $goToInterwiki->execute( 'local:Foo' );
48 $this->assertSame( Title
::newFromText( 'local:Foo' )->getFullURL(),
49 $context->getOutput()->getRedirect() );
51 RequestContext
::resetMain();
52 $context = new DerivativeContext( RequestContext
::getMain() );
53 $goToInterwiki->setContext( $context );
54 $goToInterwiki->execute( 'nonlocal:Foo' );
55 $this->assertSame( '', $context->getOutput()->getRedirect() );
56 $this->assertStringContainsString( Title
::newFromText( 'nonlocal:Foo' )->getFullURL(),
57 $context->getOutput()->getHTML() );
59 RequestContext
::resetMain();
60 $context = new DerivativeContext( RequestContext
::getMain() );
61 $goToInterwiki->setContext( $context );
62 $goToInterwiki->execute( 'force/Foo' );
63 $this->assertSame( Title
::newFromText( 'Foo' )->getFullURL(),
64 $context->getOutput()->getRedirect() );
66 RequestContext
::resetMain();
67 $context = new DerivativeContext( RequestContext
::getMain() );
68 $goToInterwiki->setContext( $context );
69 $goToInterwiki->execute( 'force/local:Foo' );
70 $this->assertSame( '', $context->getOutput()->getRedirect() );
71 $this->assertStringContainsString( Title
::newFromText( 'local:Foo' )->getFullURL(),
72 $context->getOutput()->getHTML() );
74 RequestContext
::resetMain();
75 $context = new DerivativeContext( RequestContext
::getMain() );
76 $goToInterwiki->setContext( $context );
77 $goToInterwiki->execute( 'force/nonlocal:Foo' );
78 $this->assertSame( '', $context->getOutput()->getRedirect() );
79 $this->assertStringContainsString( Title
::newFromText( 'nonlocal:Foo' )->getFullURL(),
80 $context->getOutput()->getHTML() );