MediaWiki  1.33.0
WebPHandlerTest.php
Go to the documentation of this file.
1 <?php
2 
7  public function setUp() {
8  parent::setUp();
9  // Allocated file for testing
10  $this->tempFileName = tempnam( wfTempDir(), 'WEBP' );
11  }
12 
13  public function tearDown() {
14  parent::tearDown();
15  unlink( $this->tempFileName );
16  }
17 
21  public function testExtractMetaData( $header, $expectedResult ) {
22  // Put header into file
23  file_put_contents( $this->tempFileName, $header );
24 
25  $this->assertEquals( $expectedResult, WebPHandler::extractMetadata( $this->tempFileName ) );
26  }
27 
28  public function provideTestExtractMetaData() {
29  // phpcs:disable Generic.Files.LineLength
30  return [
31  // Files from https://developers.google.com/speed/webp/gallery2
32  [ "\x52\x49\x46\x46\x90\x68\x01\x00\x57\x45\x42\x50\x56\x50\x38\x4C\x83\x68\x01\x00\x2F\x8F\x01\x4B\x10\x8D\x38\x6C\xDB\x46\x92\xE0\xE0\x82\x7B\x6C",
33  [ 'compression' => 'lossless', 'width' => 400, 'height' => 301 ] ],
34  [ "\x52\x49\x46\x46\x64\x5B\x00\x00\x57\x45\x42\x50\x56\x50\x38\x58\x0A\x00\x00\x00\x10\x00\x00\x00\x8F\x01\x00\x2C\x01\x00\x41\x4C\x50\x48\xE5\x0E",
35  [ 'compression' => 'unknown', 'animated' => false, 'transparency' => true, 'width' => 400, 'height' => 301 ] ],
36  [ "\x52\x49\x46\x46\xA8\x72\x00\x00\x57\x45\x42\x50\x56\x50\x38\x4C\x9B\x72\x00\x00\x2F\x81\x81\x62\x10\x8D\x40\x8C\x24\x39\x6E\x73\x73\x38\x01\x96",
37  [ 'compression' => 'lossless', 'width' => 386, 'height' => 395 ] ],
38  [ "\x52\x49\x46\x46\xE0\x42\x00\x00\x57\x45\x42\x50\x56\x50\x38\x58\x0A\x00\x00\x00\x10\x00\x00\x00\x81\x01\x00\x8A\x01\x00\x41\x4C\x50\x48\x56\x10",
39  [ 'compression' => 'unknown', 'animated' => false, 'transparency' => true, 'width' => 386, 'height' => 395 ] ],
40  [ "\x52\x49\x46\x46\x70\x61\x02\x00\x57\x45\x42\x50\x56\x50\x38\x4C\x63\x61\x02\x00\x2F\x1F\xC3\x95\x10\x8D\xC8\x72\xDB\xC8\x92\x24\xD8\x91\xD9\x91",
41  [ 'compression' => 'lossless', 'width' => 800, 'height' => 600 ] ],
42  [ "\x52\x49\x46\x46\x1C\x1D\x01\x00\x57\x45\x42\x50\x56\x50\x38\x58\x0A\x00\x00\x00\x10\x00\x00\x00\x1F\x03\x00\x57\x02\x00\x41\x4C\x50\x48\x25\x8B",
43  [ 'compression' => 'unknown', 'animated' => false, 'transparency' => true, 'width' => 800, 'height' => 600 ] ],
44  [ "\x52\x49\x46\x46\xFA\xC5\x00\x00\x57\x45\x42\x50\x56\x50\x38\x4C\xEE\xC5\x00\x00\x2F\xA4\x81\x28\x10\x8D\x40\x68\x24\xC9\x91\xA4\xAE\xF3\x97\x75",
45  [ 'compression' => 'lossless', 'width' => 421, 'height' => 163 ] ],
46  [ "\x52\x49\x46\x46\xF6\x5D\x00\x00\x57\x45\x42\x50\x56\x50\x38\x58\x0A\x00\x00\x00\x10\x00\x00\x00\xA4\x01\x00\xA2\x00\x00\x41\x4C\x50\x48\x38\x1A",
47  [ 'compression' => 'unknown', 'animated' => false, 'transparency' => true, 'width' => 421, 'height' => 163 ] ],
48  [ "\x52\x49\x46\x46\xC4\x96\x01\x00\x57\x45\x42\x50\x56\x50\x38\x4C\xB8\x96\x01\x00\x2F\x2B\xC1\x4A\x10\x11\x87\x6D\xDB\x48\x12\xFC\x60\xB0\x83\x24",
49  [ 'compression' => 'lossless', 'width' => 300, 'height' => 300 ] ],
50  [ "\x52\x49\x46\x46\x0A\x11\x01\x00\x57\x45\x42\x50\x56\x50\x38\x58\x0A\x00\x00\x00\x10\x00\x00\x00\x2B\x01\x00\x2B\x01\x00\x41\x4C\x50\x48\x67\x6E",
51  [ 'compression' => 'unknown', 'animated' => false, 'transparency' => true, 'width' => 300, 'height' => 300 ] ],
52 
53  // Lossy files from https://developers.google.com/speed/webp/gallery1
54  [ "\x52\x49\x46\x46\x68\x76\x00\x00\x57\x45\x42\x50\x56\x50\x38\x20\x5C\x76\x00\x00\xD2\xBE\x01\x9D\x01\x2A\x26\x02\x70\x01\x3E\xD5\x4E\x97\x43\xA2",
55  [ 'compression' => 'lossy', 'width' => 550, 'height' => 368 ] ],
56  [ "\x52\x49\x46\x46\xB0\xEC\x00\x00\x57\x45\x42\x50\x56\x50\x38\x20\xA4\xEC\x00\x00\xB2\x4B\x02\x9D\x01\x2A\x26\x02\x94\x01\x3E\xD1\x50\x96\x46\x26",
57  [ 'compression' => 'lossy', 'width' => 550, 'height' => 404 ] ],
58  [ "\x52\x49\x46\x46\x7A\x19\x03\x00\x57\x45\x42\x50\x56\x50\x38\x20\x6E\x19\x03\x00\xB2\xF8\x09\x9D\x01\x2A\x00\x05\xD0\x02\x3E\xAD\x46\x99\x4A\xA5",
59  [ 'compression' => 'lossy', 'width' => 1280, 'height' => 720 ] ],
60  [ "\x52\x49\x46\x46\x44\xB3\x02\x00\x57\x45\x42\x50\x56\x50\x38\x20\x38\xB3\x02\x00\x52\x57\x06\x9D\x01\x2A\x00\x04\x04\x03\x3E\xA5\x44\x96\x49\x26",
61  [ 'compression' => 'lossy', 'width' => 1024, 'height' => 772 ] ],
62  [ "\x52\x49\x46\x46\x02\x43\x01\x00\x57\x45\x42\x50\x56\x50\x38\x20\xF6\x42\x01\x00\x12\xC0\x05\x9D\x01\x2A\x00\x04\xF0\x02\x3E\x79\x34\x93\x47\xA4",
63  [ 'compression' => 'lossy', 'width' => 1024, 'height' => 752 ] ],
64 
65  // Animated file from https://groups.google.com/a/chromium.org/d/topic/blink-dev/Y8tRC4mdQz8/discussion
66  [ "\x52\x49\x46\x46\xD0\x0B\x02\x00\x57\x45\x42\x50\x56\x50\x38\x58\x0A\x00\x00\x00\x12\x00\x00\x00\x3F\x01\x00\x3F\x01\x00\x41\x4E",
67  [ 'compression' => 'unknown', 'animated' => true, 'transparency' => true, 'width' => 320, 'height' => 320 ] ],
68 
69  // Error cases
70  [ '', false ],
71  [ ' ', false ],
72  [ 'RIFF ', false ],
73  [ 'RIFF1234WEBP ', false ],
74  [ 'RIFF1234WEBPVP8 ', false ],
75  [ 'RIFF1234WEBPVP8L ', false ],
76  ];
77  // phpcs:enable
78  }
79 
83  public function testWithFileExtractMetaData( $filename, $expectedResult ) {
84  $this->assertEquals( $expectedResult, WebPHandler::extractMetadata( $filename ) );
85  }
86 
88  return [
89  [ __DIR__ . '/../../data/media/2_webp_ll.webp',
90  [
91  'compression' => 'lossless',
92  'width' => 386,
93  'height' => 395
94  ]
95  ],
96  [ __DIR__ . '/../../data/media/2_webp_a.webp',
97  [
98  'compression' => 'lossy',
99  'animated' => false,
100  'transparency' => true,
101  'width' => 386,
102  'height' => 395
103  ]
104  ],
105  ];
106  }
107 
111  public function testGetImageSize( $path, $expectedResult ) {
112  $handler = new WebPHandler();
113  $this->assertEquals( $expectedResult, $handler->getImageSize( null, $path ) );
114  }
115 
116  public function provideTestGetImageSize() {
117  return [
118  // Public domain files from https://developers.google.com/speed/webp/gallery2
119  [ __DIR__ . '/../../data/media/2_webp_a.webp', [ 386, 395 ] ],
120  [ __DIR__ . '/../../data/media/2_webp_ll.webp', [ 386, 395 ] ],
121  [ __DIR__ . '/../../data/media/webp_animated.webp', [ 300, 225 ] ],
122 
123  // Error cases
124  [ __FILE__, false ],
125  ];
126  }
127 
134  public function testGuessMimeType( $path ) {
135  $mime = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
136  $this->assertEquals( 'image/webp', $mime->guessMimeType( $path, false ) );
137  }
138 
139  public function provideTestGetMimeType() {
140  return [
141  // Public domain files from https://developers.google.com/speed/webp/gallery2
142  [ __DIR__ . '/../../data/media/2_webp_a.webp' ],
143  [ __DIR__ . '/../../data/media/2_webp_ll.webp' ],
144  [ __DIR__ . '/../../data/media/webp_animated.webp' ],
145  ];
146  }
147 }
148 
149 /* Python code to extract a header and convert to PHP format:
150  * print '"%s"' % ''.implode( '\\x%02X' % ord(c) for c in urllib.urlopen(url).read(36) )
151  */
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
WebPHandlerTest\provideTestExtractMetaData
provideTestExtractMetaData()
Definition: WebPHandlerTest.php:28
WebPHandlerTest\testExtractMetaData
testExtractMetaData( $header, $expectedResult)
provideTestExtractMetaData
Definition: WebPHandlerTest.php:21
$handler
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check set to true or false to override the $wgMaxImageArea check result gives extension the possibility to transform it themselves $handler
Definition: hooks.txt:780
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
WebPHandlerTest\testGetImageSize
testGetImageSize( $path, $expectedResult)
provideTestGetImageSize
Definition: WebPHandlerTest.php:111
WebPHandlerTest\provideTestWithFileExtractMetaData
provideTestWithFileExtractMetaData()
Definition: WebPHandlerTest.php:87
WebPHandlerTest\testWithFileExtractMetaData
testWithFileExtractMetaData( $filename, $expectedResult)
provideTestWithFileExtractMetaData
Definition: WebPHandlerTest.php:83
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:124
WebPHandlerTest\provideTestGetImageSize
provideTestGetImageSize()
Definition: WebPHandlerTest.php:116
$header
$header
Definition: updateCredits.php:41
WebPHandlerTest\setUp
setUp()
Definition: WebPHandlerTest.php:7
wfTempDir
wfTempDir()
Tries to get the system directory for temporary files.
Definition: GlobalFunctions.php:1989
$path
$path
Definition: NoLocalSettings.php:25
WebPHandlerTest\tearDown
tearDown()
Definition: WebPHandlerTest.php:13
WebPHandlerTest\provideTestGetMimeType
provideTestGetMimeType()
Definition: WebPHandlerTest.php:139
WebPHandler
Handler for Google's WebP format https://developers.google.com/speed/webp/
Definition: WebPHandler.php:29
WebPHandler\extractMetadata
static extractMetadata( $filename)
Extracts the image size and WebP type from a file.
Definition: WebPHandler.php:94
WebPHandlerTest
WebPHandler.
Definition: WebPHandlerTest.php:6
WebPHandlerTest\testGuessMimeType
testGuessMimeType( $path)
Tests the WebP MIME detection.
Definition: WebPHandlerTest.php:134