Debian Codegraph @

tests/phpunit/integration/includes/Rest/Handler/MediaFileHandlerTest.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\MediaFileHandler;
8 use MediaWiki\Rest\LocalizedHttpException;
9 use MediaWiki\Rest\RequestData;
10 use MediaWiki\Title\Title;
11 use MediaWikiLangTestCase;
12 use Wikimedia\Message\MessageValue;
13
14 /**
15 * @covers \MediaWiki\Rest\Handler\MediaFileHandler
16 *
17 * @group Database
18 */
19 class MediaFileHandlerTest extends MediaWikiLangTestCase {
20
21 use MediaTestTrait;
22
23 public function addDBDataOnce() {
24 $this->editPage( 'File:' . __CLASS__ . '.jpg', 'Test image description' );
25 }
26
27 private function newHandler() {
28 return new MediaFileHandler(
29 $this->makeMockRepoGroup( [ __CLASS__ . '.jpg' ] ),
30 $this->getServiceContainer()->getPageStore()
31 );
32 }
33
34 private function assertFile( $expected, $actual ) {
35 foreach ( $expected as $key => $value ) {
36 $this->assertArrayHasKey( $key, $actual );
37 $this->assertSame( $value, $actual[$key], $key );
38 }
39 }
40
41 public function testExecute() {
42 // NOTE: "File:" namespace prefix is optional for title parameter.
43 $title = __CLASS__ . '.jpg';
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 $userOptionsManager->getIntOption( $user, 'thumbsize' ) => [ 20, 20 ],
51 ] );
52
53 $handler = $this->newHandler();
54 $data = $this->executeHandlerAndGetBodyData( $handler, $request );
55
56 $this->assertFile(
57 [
58 'title' => $title,
59 // File repo mocks will end up calling File namespace ns6
60 'file_description_url' => 'https://example.com/wiki/ns6:' . $title,
61 'latest' => [
62 'timestamp' => '2020-01-02T03:04:05Z',
63 'user' => [
64 'id' => 7,
65 'name' => 'Alice',
66 ],
67 ],
68 'preferred' => [
69 'mediatype' => 'test',
70 'size' => null,
71 'width' => 100,
72 'height' => 67,
73 'duration' => 678,
74 'url' => 'https://media.example.com/static/thumb/' . $title,
75 ],
76 'original' => [
77 'mediatype' => 'test',
78 'size' => 12345,
79 'width' => 600,
80 'height' => 400,
81 'duration' => 678,
82 'url' => 'https://media.example.com/static/' . $title,
83 ],
84 'thumbnail' => [
85 'mediatype' => 'test',
86 'size' => null,
87 'width' => 20,
88 'height' => 13,
89 'duration' => 678,
90 'url' => 'https://media.example.com/static/thumb/' . $title,
91 ],
92 ],
93 $data
94 );
95 }
96
97 public function testCacheControl() {
98 $title = 'File:' . __CLASS__ . '.jpg';
99 $request = new RequestData( [ 'pathParams' => [ 'title' => $title ] ] );
100
101 $handler = $this->newHandler();
102 $response = $this->executeHandler( $handler, $request );
103
104 // Mock image timestamp from MediaTestTrait::makeMockFile
105 $this->assertSame(
106 'Thu, 02 Jan 2020 03:04:05 GMT',
107 $response->getHeaderLine( 'Last-Modified' )
108 );
109 // Mock image hash from MediaTestTrait::makeMockFile
110 $this->assertSame(
111 '"DEADBEEF"',
112 $response->getHeaderLine( 'ETag' )
113 );
114 }
115
116 public function testExecute_notFound() {
117 // NOTE: MediaTestTrait::makeMockRepoGroup() will treat files with "missing" in
118 // the name as non-existent.
119 $title = __CLASS__ . '_Missing.png';
120 $request = new RequestData( [ 'pathParams' => [ 'title' => $title ] ] );
121
122 $handler = $this->newHandler();
123
124 $this->expectExceptionObject(
125 new LocalizedHttpException( new MessageValue( 'rest-nonexistent-title' ), 404 )
126 );
127 $this->executeHandler( $handler, $request );
128 }
129
130 public function testExecute_wrongNamespace() {
131 $title = Title::newFromText( 'User:' . __CLASS__ . '.jpg' );
132 $this->editPage( $title, 'First' );
133 $request = new RequestData( [ 'pathParams' => [ 'title' => $title->getPrefixedDBkey() ] ] );
134
135 $handler = $this->newHandler();
136
137 $this->expectExceptionObject(
138 new LocalizedHttpException( new MessageValue( 'rest-cannot-load-file' ), 404 )
139 );
140 $this->executeHandler( $handler, $request );
141 }
142
143 }