Debian Codegraph @

tests/phpunit/includes/filerepo/Thumbnail404EntryPointTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Context\RequestContext;
4 use MediaWiki\FileRepo\Thumbnail404EntryPoint;
5 use MediaWiki\MainConfigNames;
6 use MediaWiki\Request\FauxRequest;
7 use MediaWiki\Tests\FileRepo\TestRepoTrait;
8 use MediaWiki\Tests\MockEnvironment;
9 use MediaWiki\Title\Title;
10
11 /**
12 * @covers \MediaWiki\FileRepo\Thumbnail404EntryPoint
13 * @group Database
14 */
15 class Thumbnail404EntryPointTest extends MediaWikiIntegrationTestCase {
16
17 use TestRepoTrait;
18 use MockHttpTrait;
19
20 private const PNG_MAGIC = "\x89\x50\x4e\x47";
21 private const JPEG_MAGIC = "\xff\xd8\xff\xe0";
22
23 private const IMAGES_DIR = __DIR__ . '/../../data/media';
24
25 /**
26 * will be called only once per test class
27 */
28 public function addDBDataOnce() {
29 // Set a named user account for the request context as the default,
30 // so that these tests do not fail with temp accounts enabled
31 RequestContext::getMain()->setUser( $this->getTestUser()->getUser() );
32 // Create mock repo with test files
33 $this->initTestRepoGroup();
34
35 $this->importFileToTestRepo( self::IMAGES_DIR . '/greyscale-png.png', 'Test.png' );
36 $this->importFileToTestRepo( self::IMAGES_DIR . '/Animated_PNG_example_bouncing_beach_ball.png' );
37 $this->importFileToTestRepo( self::IMAGES_DIR . '/test.jpg', 'Icon.jpg' );
38
39 // Create a second version of Test.png
40 $this->importFileToTestRepo( self::IMAGES_DIR . '/greyscale-na-png.png', 'Test.png' );
41
42 // Create a redirect
43 $title = Title::makeTitle( NS_FILE, 'Redirect_to_Test.png' );
44 $this->editPage( $title, '#REDIRECT [[File:Test.png]]' );
45 }
46
47 public static function tearDownAfterClass(): void {
48 self::destroyTestRepo();
49 parent::tearDownAfterClass();
50 }
51
52 public function setUp(): void {
53 parent::setUp();
54
55 $this->installTestRepoGroup();
56 }
57
58 /**
59 * @param FauxRequest|string|null $request
60 *
61 * @return MockEnvironment
62 */
63 private function makeEnvironment( $request ): MockEnvironment {
64 if ( !$request ) {
65 $request = new FauxRequest();
66 }
67
68 if ( is_string( $request ) ) {
69 $req = new FauxRequest( [] );
70 $req->setRequestURL( $request );
71 $request = $req;
72 }
73
74 return new MockEnvironment( $request );
75 }
76
77 /**
78 * @param MockEnvironment|null $environment
79 * @param FauxRequest|RequestContext|string|array|null $request
80 *
81 * @return Thumbnail404EntryPoint
82 */
83 private function getEntryPoint(
84 ?MockEnvironment $environment = null,
85 $request = null
86 ) {
87 if ( !$request && $environment ) {
88 $request = $environment->getFauxRequest();
89 }
90
91 if ( $request instanceof RequestContext ) {
92 $context = $request;
93 $request = $context->getRequest();
94 } else {
95 $context = new RequestContext();
96 $context->setRequest( $request );
97 $context->setUser( $this->getTestUser()->getUser() );
98 }
99
100 if ( !$environment ) {
101 $environment = $this->makeEnvironment( $request );
102 }
103
104 $entryPoint = new Thumbnail404EntryPoint(
105 $context,
106 $environment,
107 $this->getServiceContainer()
108 );
109
110 $entryPoint->enableOutputCapture();
111 return $entryPoint;
112 }
113
114 public static function provideNotFound() {
115 yield 'non-existing image' => [
116 '/w/images/thumb/a/aa/Xyzzy.png/13px-Xyzzy.png',
117 404
118 ];
119 yield 'malformed name' => [
120 '/w/images/thumb/x/xx/XyzzyXyzzy',
121 400
122 ];
123 }
124
125 /**
126 * @dataProvider provideNotFound
127 */
128 public function testNotFound( $req, $expectedStatus ) {
129 $env = $this->makeEnvironment( $req );
130 $entryPoint = $this->getEntryPoint( $env );
131
132 $entryPoint->run();
133 $output = $entryPoint->getCapturedOutput();
134
135 $env->assertStatusCode( $expectedStatus );
136 $env->assertHeaderValue(
137 'text/html; charset=utf-8',
138 'Content-Type'
139 );
140
141 $this->assertStringContainsString(
142 '<title>Error generating thumbnail</title>',
143 $output
144 );
145 }
146
147 public function testStreamFile() {
148 $file = $this->getTestRepo()->newFile( 'Test.png' );
149 $rel = $file->getRel();
150 $name = $file->getName();
151
152 $env = $this->makeEnvironment( "/w/images/thumb/$rel/13px-$name" );
153 $entryPoint = $this->getEntryPoint( $env );
154
155 $entryPoint->run();
156 $output = $entryPoint->getCapturedOutput();
157
158 $env->assertStatusCode( 200 );
159
160 $this->assertThumbnail(
161 [ 'magic' => self::PNG_MAGIC, 'width' => 13, ],
162 $output
163 );
164
165 return [ 'data' => $output, 'width' => 13 ];
166 }
167
168 public function testStreamFileWithThumbPath() {
169 $this->overrideConfigValue( MainConfigNames::ThumbPath, '/thumbnails/' );
170
171 $file = $this->getTestRepo()->newFile( 'Test.png' );
172 $rel = $file->getRel();
173
174 $env = $this->makeEnvironment( "/thumbnails/$rel/13px-Test.png" );
175 $entryPoint = $this->getEntryPoint( $env );
176
177 $entryPoint->run();
178 $output = $entryPoint->getCapturedOutput();
179
180 $env->assertStatusCode( 200 );
181
182 $this->assertThumbnail(
183 [ 'magic' => self::PNG_MAGIC, 'width' => 13, ],
184 $output
185 );
186 }
187
188 public function testStreamFileWithLongName() {
189 $this->overrideConfigValue( MainConfigNames::VaryOnXFP, true );
190
191 // Note that abbrvThreshold is 16 per MockRepTrait
192 $file = $this->getTestRepo()->newFile( 'Animated_PNG_example_bouncing_beach_ball.png' );
193 $rel = $file->getRel();
194 $name = $file->getName();
195
196 // use abbreviated name
197 $env = $this->makeEnvironment( "/w/images/thumb/$rel/13px-thumbnail.png" );
198 $entryPoint = $this->getEntryPoint( $env );
199
200 $entryPoint->run();
201 $output = $entryPoint->getCapturedOutput();
202
203 $env->assertStatusCode( 200, $output );
204 $env->assertHeaderValue( null, 'Vary' );
205
206 // use long name
207 $env = $this->makeEnvironment( "/w/images/thumb/$rel/13px-$name" );
208 $entryPoint = $this->getEntryPoint( $env );
209
210 $entryPoint->run();
211 $output = $entryPoint->getCapturedOutput();
212
213 $env->assertStatusCode( 301, $output );
214 $env->assertHeaderValue( 'X-Forwarded-Proto', 'Vary' );
215
216 $this->assertStringEndsWith(
217 "/w/images/thumb/$rel/13px-thumbnail.png",
218 $env->getFauxResponse()->getHeader( 'Location' )
219 );
220 }
221
222 /**
223 * @depends testStreamFile
224 */
225 public function testStreamOldFile( array $latestThumbnailInfo ) {
226 $file = $this->getTestRepo()->newFile( 'Test.png' );
227 $history = $file->getHistory();
228 $oldFile = $history[0];
229
230 $uri = '/w/images/thumb/' . $oldFile->getArchiveRel()
231 . '/' . $oldFile->getArchiveName() . '/13px-Test.png';
232
233 $env = $this->makeEnvironment( $uri );
234 $entryPoint = $this->getEntryPoint( $env );
235
236 $entryPoint->run();
237 $output = $entryPoint->getCapturedOutput();
238 $env->assertStatusCode( 200 );
239
240 $this->assertNotSame(
241 $latestThumbnailInfo['data'],
242 $output,
243 'Thumbnail for the old version should not be the same as the ' .
244 'thumbnail for the latest version'
245 );
246
247 $this->assertThumbnail(
248 [ 'magic' => self::PNG_MAGIC, 'width' => 13, ],
249 $output
250 );
251 }
252
253 public function testStreamTempFile() {
254 $user = $this->getTestUser()->getUser();
255 $stash = new UploadStash( $this->getTestRepo(), $user );
256 $file = $stash->stashFile( self::IMAGES_DIR . '/adobergb.jpg' );
257
258 $uri = '/w/images/thumb/temp/' . $file->getRel()
259 . '/13px-' . $file->getName();
260
261 $env = $this->makeEnvironment( $uri );
262 $entryPoint = $this->getEntryPoint( $env );
263
264 $entryPoint->run();
265 $output = $entryPoint->getCapturedOutput();
266
267 $env->assertStatusCode( 200 );
268 $this->assertThumbnail(
269 [ 'magic' => self::JPEG_MAGIC, 'width' => 13, ],
270 $output
271 );
272 }
273
274 public function testBadPath() {
275 $file = $this->getTestRepo()->newFile( 'Test.png' );
276 $rel = $file->getRel();
277
278 $uri = "/w/images/thumb/$rel/148px-XYZZY";
279
280 $env = $this->makeEnvironment( $uri );
281 $entryPoint = $this->getEntryPoint( $env );
282
283 $entryPoint->run();
284 $entryPoint->getCapturedOutput();
285
286 $env->assertStatusCode( 404 );
287 }
288
289 /**
290 * @param array $props
291 * @param string $output binary data
292 */
293 private function assertThumbnail( array $props, string $output ): void {
294 if ( isset( $props['magic'] ) ) {
295 $this->assertStringStartsWith(
296 $props['magic'],
297 $output,
298 'Magic number should match'
299 );
300 }
301
302 if ( isset( $props['width'] ) && function_exists( 'getimagesizefromstring' ) ) {
303 [ $width, ] = getimagesizefromstring( $output );
304 $this->assertSame(
305 $props['width'],
306 $width
307 );
308 }
309 }
310
311 }