Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Rest\Handler;
4
5 use MediaWiki\Context\RequestContext;
6 use MediaWiki\MainConfigNames;
7 use MediaWiki\Rest\Handler\MediaLinksHandler;
8 use MediaWiki\Rest\LocalizedHttpException;
9 use MediaWiki\Rest\RequestData;
10 use MediaWiki\Title\Title;
11 use MediaWikiIntegrationTestCase;
12 use Wikimedia\Message\MessageValue;
13
14 /**
15 * @covers \MediaWiki\Rest\Handler\MediaLinksHandler
16 *
17 * @group Database
18 */
19 class MediaLinksHandlerTest extends MediaWikiIntegrationTestCase {
20
21 use MediaTestTrait;
22
23 public function addDBDataOnce() {
24 $this->editPage( __CLASS__ . '_Foo', 'Foo [[Image:Existing.jpg]] [[Image:Missing.jpg]]' );
25 }
26
27 private function newHandler() {
28 return new MediaLinksHandler(
29 $this->getServiceContainer()->getConnectionProvider(),
30 $this->makeMockRepoGroup( [ 'Existing.jpg' ] ),
31 $this->getServiceContainer()->getPageStore()
32 );
33 }
34
35 private function assertLink( $expected, $actual ) {
36 foreach ( $expected as $key => $value ) {
37 $this->assertArrayHasKey( $key, $actual );
38 $this->assertSame( $value, $actual[$key], $key );
39 }
40 }
41
42 public function testExecute() {
43 $title = __CLASS__ . '_Foo';
44 $request = new RequestData( [ 'pathParams' => [ 'title' => $title ] ] );
45
46 $user = RequestContext::getMain()->getUser();
47 $userOptionsManager = $this->getServiceContainer()->getUserOptionsManager();
48 $this->overrideConfigValue( MainConfigNames::ImageLimits, [
49 $userOptionsManager->getIntOption( $user, 'imagesize' ) => [ 100, 100 ],
50 ] );
51
52 $handler = $this->newHandler();
53 $data = $this->executeHandlerAndGetBodyData( $handler, $request );
54
55 $this->assertArrayHasKey( 'files', $data );
56 $this->assertCount( 2, $data['files'] );
57
58 $links = [];
59 foreach ( $data['files'] as $row ) {
60 $links[$row['title']] = $row;
61 }
62
63 $this->assertArrayHasKey( 'Existing.jpg', $links );
64 $this->assertArrayHasKey( 'Missing.jpg', $links );
65
66 // NOTE: See MediaTestTrait::makeMockFile() for hard-coded values.
67 $this->assertLink( [
68 'title' => 'Existing.jpg',
69 // File repo mocks will end up calling File namespace ns6
70 'file_description_url' => 'https://example.com/wiki/ns6:Existing.jpg',
71 'latest' => [
72 'timestamp' => '2020-01-02T03:04:05Z',
73 'user' => [ 'id' => 7, 'name' => 'Alice' ]
74 ],
75 'preferred' => [
76 'mediatype' => 'test',
77 'size' => null,
78 'width' => 100,
79 'height' => 67,
80 'duration' => 678,
81 'url' => 'https://media.example.com/static/thumb/Existing.jpg',
82 ],
83 'original' => [
84 'mediatype' => 'test',
85 'size' => 12345,
86 'width' => 600,
87 'height' => 400,
88 'duration' => 678,
89 'url' => 'https://media.example.com/static/Existing.jpg',
90 ],
91 ], $links['Existing.jpg'] );
92
93 // NOTE: MediaTestTrait::makeMockRepoGroup() treats files with "missing" in the
94 // name as non-existent.
95 $this->assertLink( [
96 'title' => 'Missing.jpg',
97 // File repo mocks will end up calling File namespace ns6
98 'file_description_url' => 'https://example.com/wiki/ns6:Missing.jpg',
99 'latest' => null,
100 'preferred' => null,
101 'original' => null,
102 ], $links['Missing.jpg'] );
103 }
104
105 public function testCacheControl() {
106 $title = Title::newFromText( __METHOD__ );
107 $this->editPage( $title, 'First' );
108
109 $request = new RequestData( [ 'pathParams' => [ 'title' => $title->getPrefixedDBkey() ] ] );
110
111 $handler = $this->newHandler();
112 $response = $this->executeHandler( $handler, $request );
113
114 $firstETag = $response->getHeaderLine( 'ETag' );
115 $this->assertSame(
116 wfTimestamp( TS_RFC2822, $title->getTouched() ),
117 $response->getHeaderLine( 'Last-Modified' )
118 );
119
120 $this->editPage( $title, 'Second' );
121
122 Title::clearCaches();
123 $handler = $this->newHandler();
124 $response = $this->executeHandler( $handler, $request );
125
126 $this->assertNotEquals( $response->getHeaderLine( 'ETag' ), $firstETag );
127 $this->assertSame(
128 wfTimestamp( TS_RFC2822, $title->getTouched() ),
129 $response->getHeaderLine( 'Last-Modified' )
130 );
131 }
132
133 public function testExecute_notFound() {
134 $title = __CLASS__ . '_Xyzzy';
135 $request = new RequestData( [ 'pathParams' => [ 'title' => $title ] ] );
136
137 $handler = $this->newHandler();
138
139 $this->expectExceptionObject(
140 new LocalizedHttpException( new MessageValue( 'rest-nonexistent-title' ), 404 )
141 );
142 $this->executeHandler( $handler, $request );
143 }
144
145 public function testMaxNumLinks() {
146 $title = __CLASS__ . '_Foo';
147
148 $handler = new class (
149 $this->getServiceContainer()->getConnectionProvider(),
150 $this->makeMockRepoGroup( [ 'Existing.jpg' ] ),
151 $this->getServiceContainer()->getPageStore()
152 ) extends MediaLinksHandler {
153 protected function getMaxNumLinks(): int {
154 return 1;
155 }
156 };
157
158 $request = new RequestData( [ 'pathParams' => [ 'title' => $title ] ] );
159
160 $this->expectExceptionObject(
161 new LocalizedHttpException( new MessageValue( 'rest-media-too-many-links' ), 400 )
162 );
163
164 $data = $this->executeHandlerAndGetBodyData( $handler, $request );
165 }
166 }