-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathTinyImageTest.php
More file actions
399 lines (340 loc) · 13.7 KB
/
TinyImageTest.php
File metadata and controls
399 lines (340 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<?php
require_once dirname( __FILE__ ) . '/TinyTestCase.php';
class Tiny_Image_Test extends Tiny_TestCase {
protected $subject;
protected $settings;
public function set_up() {
parent::set_up();
$this->wp->createImagesFromJSON( $this->json( 'image_filesystem_data' ) );
$this->wp->setTinyMetadata( 1, $this->json( 'image_database_metadata' ) );
$this->settings = new Tiny_Settings();
$this->subject = new Tiny_Image( $this->settings, 1, $this->json( '_wp_attachment_metadata' ) );
}
public function test_tiny_post_meta_key_may_never_change() {
$this->assertEquals( '61b16225f107e6f0a836bf19d47aa0fd912f8925', sha1( Tiny_Config::META_KEY ) );
}
public function test_update_wp_metadata_should_not_update_with_no_resized_original() {
$tiny_image = new Tiny_Image( new Tiny_Settings(), 150, $this->json( '_wp_attachment_metadata' ) );
$tiny_image_metadata = $tiny_image->get_wp_metadata();
$this->assertEquals( 1256, $tiny_image_metadata['width'] );
$this->assertEquals( 1256, $tiny_image_metadata['height'] );
}
public function test_update_wp_metadata_should_update_with_resized_original() {
$tiny_image = new Tiny_Image( new Tiny_Settings(), 150, $this->json( '_wp_attachment_metadata' ) );
$response = array(
'output' => array(
'width' => 200,
'height' => 100,
'size' => 100,
),
);
$tiny_image->get_image_size()->add_tiny_meta_start();
$tiny_image->get_image_size()->add_tiny_meta( $response );
$tiny_image->add_wp_metadata( Tiny_Image::ORIGINAL, $tiny_image->get_image_size() );
$tiny_image_metadata = $tiny_image->get_wp_metadata();
$this->assertEquals( 200, $tiny_image_metadata['width'] );
$this->assertEquals( 100, $tiny_image_metadata['height'] );
$this->assertEquals( 100, $tiny_image_metadata['filesize'] );
}
public function test_parse_wp_metadata_should_ignore_invalid_sizes() {
$invalid_metadata = array(
'width' => 1256,
'height' => 1256,
'file' => '2015/09/tinypng_gravatar.png',
'sizes' => array(
'valid' => array(
'file' => 'tinypng_gravatar-200x200.png',
'width' => 200,
'height' => 200,
'mime-type' => 'image/png',
),
'missing-file' => array(
'width' => 50,
'height' => 50,
'mime-type' => 'image/png',
),
'scalar-size' => 'tinypng_gravatar-300x300.png',
'null-size' => null,
'valid-second' => array(
'file' => 'tinypng_gravatar-400x400.png',
'mime-type' => 'image/png',
),
),
'image_meta' => array(),
);
$tiny_image = new Tiny_Image( $this->settings, 999, $invalid_metadata );
$this->assertEquals(
array(
'valid' => array(
'file' => 'tinypng_gravatar-200x200.png',
'width' => 200,
'height' => 200,
'mime-type' => 'image/png',
),
'valid-second' => array(
'file' => 'tinypng_gravatar-400x400.png',
'mime-type' => 'image/png',
),
),
$tiny_image->get_wp_metadata()['sizes']
);
}
public function test_get_images_should_return_all_images() {
$this->assertEquals( array(
Tiny_Image::ORIGINAL,
'medium',
'thumbnail',
'twentyfourteen-full-width',
'failed',
'large',
'small',
), array_keys( $this->subject->get_image_sizes() ) );
}
public function test_filter_images_should_filter_correctly() {
$this->assertEquals( array(
Tiny_Image::ORIGINAL,
'medium',
'thumbnail',
), array_keys( $this->subject->filter_image_sizes( 'compressed' ) ) );
}
public function test_filter_images_should_filter_correctly_when_sizes_are_given() {
$this->assertEquals( array(
Tiny_Image::ORIGINAL
), array_keys( $this->subject->filter_image_sizes( 'compressed', array( Tiny_Image::ORIGINAL, 'invalid' ) ) )
);
}
public function test_get_count_should_add_count_correctly() {
$this->assertEquals(array(
'compressed' => 3,
'resized' => 1,
), $this->subject->get_count( array( 'compressed', 'resized' ) )
);
}
public function test_get_count_should_add_count_correctly_when_sizes_are_given() {
$this->assertEquals(array(
'compressed' => 1,
'resized' => 1,
), $this->subject->get_count( array( 'compressed', 'resized' ), array( Tiny_Image::ORIGINAL, 'invalid' ) )
);
}
public function test_get_latest_error_should_return_message() {
$this->subject->get_image_size()->add_tiny_meta_start( 'large' );
$this->subject->get_image_size()->add_tiny_meta_error( new Tiny_Exception( 'Could not download output', 'OutputError' ), 'large' );
$this->assertEquals( 'Could not download output', $this->subject->get_latest_error() );
}
public function test_get_latest_error_should_return_trimmed_message_if_message_is_huge() {
$this->subject->get_image_size()->add_tiny_meta_start( 'large' );
$this->subject->get_image_size()->add_tiny_meta_error(
new Tiny_Exception(
'Request body has unknown keys DOCTYPE HTML PUBLIC 3.2 Final <html> <head> <title>Index</title> </head> <body> <h1>Index of page</h1> <ul><li> 4m planets super nova by netbaby.jpg</li> <li> 75006 lego planets jedi starfighter planet kamino.jpg</li> </body> </html>',
'OutputError'
),
'large'
);
$this->assertEquals(
'Request body has unknown keys DOCTYPE HTML PUBLIC 3.2 Final <html> <head> <title>Index</title> </head> <body> <h1>Index of page</h1> <ul>...',
$this->subject->get_latest_error()
);
}
public function test_get_statistics() {
$active_sizes = $this->settings->get_sizes();
$active_tinify_sizes = $this->settings->get_active_tinify_sizes();
$this->assertEquals( array(
'initial_total_size' => 360542,
'compressed_total_size' => 328670,
'image_sizes_compressed' => 3,
'available_uncompressed_sizes' => 1,
'image_sizes_converted' => 0,
'available_unconverted_sizes' => 4
), $this->subject->get_statistics( $active_sizes, $active_tinify_sizes ) );
}
public function test_get_image_sizes_available_for_compression_when_file_modified() {
$active_sizes = $this->settings->get_sizes();
$active_tinify_sizes = $this->settings->get_active_tinify_sizes();
$this->wp->createImage( 37857, '2015/09', 'tinypng_gravatar-150x150.png' );
$statistics = $this->subject->get_statistics( $active_sizes, $active_tinify_sizes );
$this->assertEquals( 2, $statistics['available_uncompressed_sizes'] );
}
public function test_get_savings() {
$active_sizes = $this->settings->get_sizes();
$active_tinify_sizes = $this->settings->get_active_tinify_sizes();
$this->assertEquals( 8.8, $this->subject->get_savings( $this->subject->get_statistics( $active_sizes, $active_tinify_sizes ) ) );
}
public function test_is_retina_for_retina_size() {
$this->assertEquals( true, Tiny_Image::is_retina( 'small_wr2x' ) );
}
public function test_is_retina_for_non_retina_size() {
$this->assertEquals( false, Tiny_Image::is_retina( 'small' ) );
}
public function test_is_retina_for_non_retina_size_with_short_name() {
$this->assertEquals( false, Tiny_Image::is_retina( 'file' ) );
}
public function test_update_tiny_post_data_should_call_do_action() {
$this->wp->addOption( 'tinypng_api_key', 'test123' );
$this->wp->addOption( 'tinypng_sizes[0]', 'on' );
$this->wp->addOption( 'tinypng_sizes[large]', 'on' );
$this->wp->addOption( 'tinypng_sizes[post-thumbnail]', 'on' );
$this->wp->addImageSize( 'post-thumbnail', array( 'width' => 825, 'height' => 510 ) );
$this->wp->createImages();
$this->wp->stub( 'get_post_mime_type', function( $i ) { return "image/png"; } );
$testmeta = $this->wp->getTestMetadata();
$tiny_image = new Tiny_Image( new Tiny_Settings(), 1, $testmeta );
$tiny_image->get_image_size()->add_tiny_meta_start();
$tiny_image->update_tiny_post_meta();
$do_action_calls = array();
foreach ($this->wp->getCalls( 'do_action' ) as $action) {
array_push($do_action_calls, $action[0]);
}
$this->assertEquals(array('updated_tiny_postmeta'), $do_action_calls);
}
/**
* When an image is already compressed, we still need to be able to convert it
* In case a customer has already compressed a couple of images and then turns
* on the conversion feature.
*/
public function test_compressed_images_can_be_converted() {
// Enable conversion and all image sizes
$this->wp->addOption('tinypng_conversion_enabled', true);
$this->wp->addOption('tinypng_convert_to', 'smallest');
$settings = new Tiny_Settings();
$this->subject = new Tiny_Image($settings, 1, $this->json('_wp_attachment_metadata'));
$active_tinify_sizes = $settings->get_active_tinify_sizes();
$uncompressed_sizes = $this->subject->filter_image_sizes('uncompressed', $active_tinify_sizes);
$unconverted_sizes = $this->subject->filter_image_sizes('unconverted', $active_tinify_sizes);
$unprocessed_sizes = $uncompressed_sizes + $unconverted_sizes;
$this->assertCount(1, $uncompressed_sizes, 'should be 1 size compressed');
$this->assertCount(4, $unconverted_sizes, 'All 4 sizes should be converted');
$this->assertCount(4, $unprocessed_sizes, 'All sizes should be processed');
}
/**
* Test conversion to see if follow-up conversion will be done with the same mimetype
*/
public function test_conversion_same_mimetype()
{
$this->wp->addOption('tinypng_convert_format', array(
'convert' => 'on',
'convert_to' => 'smallest',
));
$this->wp->addOption('tinypng_sizes', array(
Tiny_Image::ORIGINAL => 'on',
'thumbnail' => 'on',
));
$this->wp->createImages(array(
'thumbnail' => 1000,
));
$this->wp->stub('get_post_mime_type', function () {
return 'image/png';
});
$metadata = $this->wp->getTestMetadata();
$settings = new Tiny_Settings();
// create a mock compressor to spy on calls
$mock_compressor = $this->createMock(Tiny_Compress::class);
// we expect for all sizes a webp
$converted_type = 'image/webp';
$responses = array(
array(
'input' => array('size' => 1000),
'output' => array('size' => 800, 'type' => 'image/png'),
'convert' => array('type' => $converted_type, 'size' => 750, 'path' => 'vfs://root/converted-1.webp'),
),
array(
'input' => array('size' => 1000),
'output' => array('size' => 780, 'type' => 'image/png'),
'convert' => array('type' => $converted_type, 'size' => 720, 'path' => 'vfs://root/converted-2.webp'),
),
);
$compress_calls = array();
$mock_compressor->expects($this->exactly(2))
->method('compress_file')
->willReturnCallback(function ($file, $resize, $preserve, $convert_to) use (&$compress_calls, &$responses) {
$compress_calls[] = array(
'file' => $file,
'convert_to' => $convert_to,
);
return array_shift($responses);
});
$settings->set_compressor($mock_compressor);
$tinyimg = new Tiny_Image($settings, 999, $metadata);
$tinyimg->compress();
// should have been 2 calls to our mock 'compress_file'
$this->assertCount(2, $compress_calls);
// first call would have been width all mimetypes
$this->assertEquals(array('image/avif', 'image/webp'), $compress_calls[0]['convert_to']);
// second call should be only with image/webp because first call was a image/webp
$this->assertEquals(array('image/webp'), $compress_calls[1]['convert_to']);
}
public function test_creates_backup_of_original_image() {
$this->wp->addOption('tinypng_backup', array(
'enabled' => 'on',
));
$this->wp->addOption('tinypng_sizes', array(
Tiny_Image::ORIGINAL => 'on',
));
$this->wp->stub('get_post_mime_type', function () {
return 'image/png';
});
$input_dir = '26/03';
$input_name = 'testforbackup';
$this->wp->createImages(array(), 1000, $input_dir, $input_name);
$settings = new Tiny_Settings();
$mock_compressor = $this->createMock(Tiny_Compress::class);
$settings->set_compressor($mock_compressor);
$metadata = $this->wp->getTestMetadata($input_dir, $input_name);
$tinyimg = new Tiny_Image($settings, 999, $metadata);
$tinyimg->compress();
$this->assertTrue(
file_exists( $this->vfs->url() . '/wp-content/uploads/' . $input_dir . '/' . $input_name . '.bak.png' ) ,
'backup of file should be created');
}
public function test_will_not_backup_other_sizes() {
$this->wp->addOption('tinypng_backup', array(
'enabled' => 'on',
));
$this->wp->addOption('tinypng_sizes', array(
'thumbnail' => 'on',
));
$this->wp->stub('get_post_mime_type', function () {
return 'image/png';
});
$input_dir = '26/03';
$input_name = 'testforbackup';
$this->wp->createImages(array(
'thumbnail' => 1000,
), 1000, $input_dir, $input_name);
$settings = new Tiny_Settings();
$mock_compressor = $this->createMock(Tiny_Compress::class);
$settings->set_compressor($mock_compressor);
$metadata = $this->wp->getTestMetadata($input_dir, $input_name);
$tinyimg = new Tiny_Image($settings, 999, $metadata);
$tinyimg->compress();
$this->assertFalse(
file_exists( $this->vfs->url() . '/wp-content/uploads/' . $input_dir . '/' . $input_name . '.bak.png' ) ,
'backup of original should not exist');
$this->assertFalse(
file_exists( $this->vfs->url() . '/wp-content/uploads/' . $input_dir . '/' . $input_name . '-thumbnail.bak.png' ) ,
'backup of thumbnail should not exist');
}
public function test_will_not_backup_when_disabled() {
$this->wp->addOption('tinypng_backup', array(
'enabled' => false,
));
$this->wp->addOption('tinypng_sizes', array(
Tiny_Image::ORIGINAL => 'on',
));
$this->wp->stub('get_post_mime_type', function () {
return 'image/png';
});
$input_dir = '26/03';
$input_name = 'testforbackup';
$this->wp->createImages(array(), 1000, $input_dir, $input_name);
$settings = new Tiny_Settings();
$mock_compressor = $this->createMock(Tiny_Compress::class);
$settings->set_compressor($mock_compressor);
$metadata = $this->wp->getTestMetadata($input_dir, $input_name);
$tinyimg = new Tiny_Image($settings, 999, $metadata);
$tinyimg->compress();
$this->assertFalse(
file_exists( $this->vfs->url() . '/wp-content/uploads/' . $input_dir . '/' . $input_name . '.bak.png' ) ,
'backup of original should not exist');
}
}