Debian Codegraph @

tests/phpunit/includes/media/ThumbnailImageTest.php

raw symbols SCIP
1 <?php
2
3 use Wikimedia\FileBackend\FileBackend;
4
5 /**
6 * @group Media
7 * @group medium
8 *
9 * @covers \ThumbnailImage
10 * @covers \MediaTransformOutput
11 */
12 class ThumbnailImageTest extends MediaWikiMediaTestCase {
13
14 private function newFile( $name = 'Test.jpg' ) {
15 return $this->dataFile( $name );
16 }
17
18 private function newThumbnail( $file = null, $url = null, $path = false, $parameters = [] ) {
19 $file ??= $this->newFile();
20 $path ??= 'thumb/a/ab/Test.jpg/123px-Test.jpg';
21 $url ??= "https://example.com/w/images/$path";
22
23 $parameters += [
24 'width' => 200,
25 'height' => 100,
26 ];
27
28 return new ThumbnailImage( $file, $url, $path, $parameters );
29 }
30
31 public function testConstructor() {
32 $file = $this->newFile();
33 $path = 'thumb/a/ab/Test.jpg/123px-Test.jpg';
34 $url = "https://example.com/w/images/$path";
35
36 $parameters = [
37 'width' => 300,
38 'height' => 200,
39 ];
40
41 $thumbnail = $this->newThumbnail(
42 $file,
43 $url,
44 $path,
45 $parameters
46 );
47
48 $this->assertSame( $file, $thumbnail->getFile() );
49 $this->assertSame( $url, $thumbnail->getUrl() );
50 $this->assertSame( $parameters['width'], $thumbnail->getWidth() );
51 $this->assertSame( $parameters['height'], $thumbnail->getHeight() );
52 $this->assertFalse( $thumbnail->isError() );
53 }
54
55 /**
56 * Check that we can stream data from a file system path
57 */
58 public function testStreamFileWithStatus_fsPath() {
59 $fsPath = $this->getFilePath() . 'test.jpg';
60 $data = file_get_contents( $fsPath );
61 $file = $this->newFile();
62
63 // NOTE: We need the FileRepo in $file for the streamer option,
64 // to prevent a real reset of the output buffer.
65 $thumbnail = $this->newThumbnail( $file, null, $fsPath );
66
67 ob_start();
68 $thumbnail->streamFileWithStatus();
69 $output = ob_get_clean();
70
71 $this->assertSame( $data, $output );
72 }
73
74 /**
75 * Check that we can stream using the FileBackend
76 */
77 public function testStreamFileWithStatus_thumbStoragePath() {
78 $this->backend = $this->createNoOpMock( FileBackend::class, [ 'streamFile' ] );
79
80 $this->backend->expects( $this->once() )
81 ->method( 'streamFile' )
82 ->wilLreturn( StatusValue::newGood() );
83
84 $this->repo = new FileRepo( $this->getRepoOptions() );
85
86 $file = $this->newFile( 'test.jpg' );
87 $thumbnail = $this->newThumbnail(
88 $file,
89 $file->getThumbUrl(),
90 $file->getThumbPath()
91 );
92
93 $thumbnail->streamFileWithStatus();
94
95 // no assertion needed, we just expect streamFile() to be called.
96 }
97
98 /**
99 * Check that we don't explode if no file repo is known
100 */
101 public function testStreamFileWithStatus_UnregisteredLocalFile() {
102 // Use a non-existing file, so streaming will fail.
103 // If streaming was successful, we'd generate real output, since
104 // without a file backend, we have no way to disable a full reset
105 // of output buffers.
106 $fsPath = $this->getFilePath() . 'this does not exist';
107
108 // No file repo or backend!
109 $file = new UnregisteredLocalFile( false, false, $fsPath );
110 $thumbnail = $this->newThumbnail( $file );
111
112 // Check that streaming fails gracefully
113 $status = $thumbnail->streamFileWithStatus();
114 $this->assertStatusError( 'backend-fail-stream', $status );
115 }
116
117 }