Debian Codegraph @

tests/phpunit/integration/includes/Rest/Handler/LanguageLinksHandlerTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Rest\Handler;
4
5 use MediaWiki\Config\ServiceOptions;
6 use MediaWiki\Interwiki\ClassicInterwikiLookup;
7 use MediaWiki\Languages\LanguageNameUtils;
8 use MediaWiki\MainConfigNames;
9 use MediaWiki\Page\PageIdentity;
10 use MediaWiki\Rest\Handler\LanguageLinksHandler;
11 use MediaWiki\Rest\LocalizedHttpException;
12 use MediaWiki\Rest\RequestData;
13 use MediaWiki\Tests\Unit\DummyServicesTrait;
14 use MediaWiki\Title\Title;
15 use MediaWikiIntegrationTestCase;
16 use Wikimedia\Message\MessageValue;
17
18 /**
19 * @covers \MediaWiki\Rest\Handler\LanguageLinksHandler
20 *
21 * @group Database
22 */
23 class LanguageLinksHandlerTest extends MediaWikiIntegrationTestCase {
24 use DummyServicesTrait;
25 use HandlerTestTrait;
26
27 public function addDBData() {
28 $defaults = [
29 'iw_local' => 0,
30 'iw_api' => '/w/api.php',
31 'iw_url' => ''
32 ];
33
34 $base = 'https://wiki.test/';
35
36 $this->overrideConfigValue(
37 MainConfigNames::InterwikiCache,
38 ClassicInterwikiLookup::buildCdbHash( [
39 [ 'iw_prefix' => 'de', 'iw_url' => $base . '/de', 'iw_wikiid' => 'dewiki' ] + $defaults,
40 [ 'iw_prefix' => 'en', 'iw_url' => $base . '/en', 'iw_wikiid' => 'enwiki' ] + $defaults,
41 [ 'iw_prefix' => 'fr', 'iw_url' => $base . '/fr', 'iw_wikiid' => 'frwiki' ] + $defaults
42 ] )
43 );
44
45 $this->editPage( __CLASS__ . '_Foo', 'Foo [[fr:Fou baux]] [[de:Füh bär]]' );
46 }
47
48 private function newHandler() {
49 $languageNameUtils = new LanguageNameUtils(
50 new ServiceOptions(
51 LanguageNameUtils::CONSTRUCTOR_OPTIONS,
52 [
53 MainConfigNames::ExtraLanguageNames => [],
54 MainConfigNames::UsePigLatinVariant => false,
55 MainConfigNames::UseXssLanguage => false,
56 ]
57 ),
58 $this->getServiceContainer()->getHookContainer()
59 );
60
61 $titleCodec = $this->getDummyMediaWikiTitleCodec();
62
63 return new LanguageLinksHandler(
64 $this->getServiceContainer()->getConnectionProvider(),
65 $languageNameUtils,
66 $titleCodec,
67 $titleCodec,
68 $this->getServiceContainer()->getPageStore(),
69 $this->getServiceContainer()->getPageRestHelperFactory()
70 );
71 }
72
73 private function assertLink( $expected, $actual ) {
74 foreach ( $expected as $key => $value ) {
75 $this->assertArrayHasKey( $key, $actual );
76 $this->assertSame( $value, $actual[$key], $key );
77 }
78 }
79
80 public function testExecute() {
81 $title = __CLASS__ . '_Foo';
82 $request = new RequestData( [ 'pathParams' => [ 'title' => $title ] ] );
83
84 $handler = $this->newHandler();
85 $data = $this->executeHandlerAndGetBodyData( $handler, $request );
86
87 $this->assertCount( 2, $data );
88
89 $links = [];
90 foreach ( $data as $row ) {
91 $links[$row['code']] = $row;
92 }
93
94 $this->assertArrayHasKey( 'de', $links );
95 $this->assertArrayHasKey( 'fr', $links );
96
97 $this->assertLink( [
98 'code' => 'de',
99 'name' => 'Deutsch',
100 'title' => 'Füh bär',
101 'key' => 'Füh_bär',
102 ], $links['de'] );
103
104 $this->assertLink( [
105 'code' => 'fr',
106 'name' => 'français',
107 'title' => 'Fou baux',
108 'key' => 'Fou_baux',
109 ], $links['fr'] );
110 }
111
112 public function testCacheControl() {
113 $title = Title::newFromText( __METHOD__ );
114 $this->editPage( $title, 'First' );
115
116 $request = new RequestData( [ 'pathParams' => [ 'title' => $title->getPrefixedDBkey() ] ] );
117
118 $handler = $this->newHandler();
119 $response = $this->executeHandler( $handler, $request );
120
121 $firstETag = $response->getHeaderLine( 'ETag' );
122 $this->assertSame(
123 wfTimestamp( TS_RFC2822, $title->getTouched() ),
124 $response->getHeaderLine( 'Last-Modified' )
125 );
126
127 $this->editPage( $title, 'Second' );
128
129 Title::clearCaches();
130 $handler = $this->newHandler();
131 $response = $this->executeHandler( $handler, $request );
132
133 $this->assertNotEquals( $response->getHeaderLine( 'ETag' ), $firstETag );
134 $this->assertSame(
135 wfTimestamp( TS_RFC2822, $title->getTouched() ),
136 $response->getHeaderLine( 'Last-Modified' )
137 );
138 }
139
140 public function testExecute_notFound() {
141 $title = __CLASS__ . '_Xyzzy';
142 $request = new RequestData( [ 'pathParams' => [ 'title' => $title ] ] );
143
144 $handler = $this->newHandler();
145
146 $this->expectExceptionObject(
147 new LocalizedHttpException( new MessageValue( 'rest-nonexistent-title' ), 404 )
148 );
149 $this->executeHandler( $handler, $request );
150 }
151
152 public function testExecute_forbidden() {
153 // The mock PermissionHandler forbids access to pages that have "Forbidden" in the name
154 $title = __CLASS__ . '_Forbidden';
155 $this->editPage( $title, 'Forbidden text' );
156 $request = new RequestData( [ 'pathParams' => [ 'title' => $title ] ] );
157
158 $handler = $this->newHandler();
159
160 $this->expectExceptionObject(
161 new LocalizedHttpException( new MessageValue( 'rest-permission-denied-title' ), 403 )
162 );
163 $this->executeHandler( $handler, $request, [ 'userCan' => false ], [], [], [],
164 $this->mockAnonAuthority( static function ( string $permission, ?PageIdentity $target ) {
165 return $target && !preg_match( '/Forbidden/', $target->getDBkey() );
166 } ) );
167 }
168
169 }