-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathtest-helper.php
More file actions
693 lines (587 loc) · 23.6 KB
/
test-helper.php
File metadata and controls
693 lines (587 loc) · 23.6 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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
<?php
/**
* Tests for webp-uploads plugin helper.php.
*
* @package webp-uploads
*/
use WebP_Uploads\Tests\TestCase;
class Test_WebP_Uploads_Helper extends TestCase {
public function set_up(): void {
parent::set_up();
$this->set_image_output_type( 'webp' );
}
/**
* Return an error when creating an additional image source with invalid parameters
*
* @dataProvider data_provider_invalid_arguments_for_webp_uploads_generate_additional_image_source
*
* @param int $attachment_id The ID of the attachment from where this image would be created.
* @param string $image_size The size name that would be used to create this image, out of the registered subsizes.
* @param array{ width: int, height: int, crop: bool } $size_data An array with the dimensions of the image.
* @param string $mime The target mime in which the image should be created.
*/
public function test_it_should_return_an_error_when_creating_an_additional_image_source_with_invalid_parameters( int $attachment_id, string $image_size, array $size_data, string $mime ): void {
$this->assertInstanceOf( WP_Error::class, webp_uploads_generate_additional_image_source( $attachment_id, $image_size, $size_data, $mime ) );
}
public function data_provider_invalid_arguments_for_webp_uploads_generate_additional_image_source(): Generator {
yield 'when trying to use an attachment ID that does not exists' => array(
PHP_INT_MAX,
'medium',
array(),
'image/webp',
);
add_filter( 'wp_image_editors', '__return_empty_array' );
yield 'when no editor is present' => array(
self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/car.jpeg' ),
'medium',
array(),
'image/avif',
);
remove_filter( 'wp_image_editors', '__return_empty_array' );
yield 'when using a mime that is not supported' => array(
self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/car.jpeg' ),
'medium',
array(),
'image/avif',
);
yield 'when no dimension is provided' => array(
self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/car.jpeg' ),
'medium',
array(),
'image/webp',
);
yield 'when both dimensions are negative numbers' => array(
self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/car.jpeg' ),
'medium',
array(
'width' => -10,
'height' => -20,
),
'image/webp',
);
yield 'when both dimensions are zero' => array(
self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/car.jpeg' ),
'medium',
array(
'width' => 0,
'height' => 0,
),
'image/webp',
);
}
/**
* Create an image with the default suffix in the same location when no destination is specified
*/
public function test_it_should_create_an_image_with_the_default_suffix_in_the_same_location_when_no_destination_is_specified(): void {
if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
$this->markTestSkipped( 'Mime type image/webp is not supported.' );
}
// Create JPEG and WebP so that both versions are generated.
$this->opt_in_to_jpeg_and_webp();
$attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/car.jpeg' );
$size_data = array(
'width' => 300,
'height' => 300,
'crop' => true,
);
$result = webp_uploads_generate_additional_image_source( $attachment_id, 'medium', $size_data, 'image/webp' );
$file = get_attached_file( $attachment_id );
$directory = trailingslashit( pathinfo( $file, PATHINFO_DIRNAME ) );
$name = pathinfo( $file, PATHINFO_FILENAME );
$this->assertIsArray( $result );
$this->assertArrayHasKey( 'filesize', $result );
$this->assertArrayHasKey( 'file', $result );
$this->assertStringEndsWith( '300x300-jpeg.webp', $result['file'] );
$this->assertFileExists( "{$directory}{$name}-300x300-jpeg.webp" );
}
/**
* Create a file in the specified location with the specified name
*/
public function test_it_should_create_a_file_in_the_specified_location_with_the_specified_name(): void {
if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
$this->markTestSkipped( 'Mime type image/webp is not supported.' );
}
$attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/car.jpeg' );
$size_data = array(
'width' => 300,
'height' => 300,
'crop' => true,
);
$result = webp_uploads_generate_additional_image_source( $attachment_id, 'medium', $size_data, 'image/webp', '/tmp/image.jpg' );
$this->assertIsArray( $result );
$this->assertArrayHasKey( 'filesize', $result );
$this->assertArrayHasKey( 'file', $result );
$this->assertStringEndsWith( 'image.webp', $result['file'] );
$this->assertFileExists( '/tmp/image.webp' );
}
/**
* Prevent processing an image with corrupted metadata
*
* @dataProvider provider_with_modified_metadata
*/
public function test_it_should_prevent_processing_an_image_with_corrupted_metadata( callable $callback, string $size ): void {
$attachment_id = self::factory()->attachment->create_upload_object(
TESTS_PLUGIN_DIR . '/tests/data/images/balloons.webp'
);
$metadata = wp_get_attachment_metadata( $attachment_id );
wp_update_attachment_metadata( $attachment_id, $callback( $metadata ) );
$result = webp_uploads_generate_image_size( $attachment_id, $size, 'image/webp' );
$this->assertWPError( $result );
$this->assertSame( 'image_mime_type_invalid_metadata', $result->get_error_code() );
}
public function provider_with_modified_metadata(): Generator {
yield 'using a size that does not exists' => array(
static function ( $metadata ) {
return $metadata;
},
'not-existing-size',
);
yield 'removing an existing metadata simulating that the image size still does not exists' => array(
static function ( $metadata ) {
unset( $metadata['sizes']['medium'] );
return $metadata;
},
'medium',
);
yield 'when the specified size is not a valid array' => array(
static function ( $metadata ) {
$metadata['sizes']['medium'] = null;
return $metadata;
},
'medium',
);
}
/**
* Prevent to create an image size when attached file does not exists
*/
public function test_it_should_prevent_to_create_an_image_size_when_attached_file_does_not_exists(): void {
if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
$this->markTestSkipped( 'Mime type image/webp is not supported.' );
}
$attachment_id = self::factory()->attachment->create_upload_object(
TESTS_PLUGIN_DIR . '/tests/data/images/leaves.jpg'
);
$file = get_attached_file( $attachment_id );
$original_file = wp_get_original_image_path( $attachment_id );
$this->assertFileExists( $file );
$this->assertFileExists( $original_file );
wp_delete_file( $file );
wp_delete_file( $original_file );
$this->assertFileDoesNotExist( $file );
$this->assertFileDoesNotExist( $original_file );
$result = webp_uploads_generate_image_size( $attachment_id, 'medium', 'image/webp' );
$this->assertWPError( $result );
$this->assertSame( 'original_image_file_not_found', $result->get_error_code() );
}
/**
* Prevent to create a subsize if the image editor does not exists
*/
public function test_it_should_prevent_to_create_a_subsize_if_the_image_editor_does_not_exists(): void {
$attachment_id = self::factory()->attachment->create_upload_object(
TESTS_PLUGIN_DIR . '/tests/data/images/leaves.jpg'
);
update_option( 'perflab_modern_image_format', 'webp' );
// Make sure no editor is available.
add_filter( 'wp_image_editors', '__return_empty_array' );
$result = webp_uploads_generate_image_size( $attachment_id, 'medium', 'image/webp' );
$this->assertWPError( $result );
$this->assertSame( 'image_mime_type_not_supported', $result->get_error_code() );
}
/**
* Prevent to upload a mime that is not supported by WordPress
*/
public function test_it_should_prevent_to_upload_a_mime_that_is_not_supported_by_wordpress(): void {
$attachment_id = self::factory()->attachment->create_upload_object(
TESTS_PLUGIN_DIR . '/tests/data/images/leaves.jpg'
);
$result = webp_uploads_generate_image_size( $attachment_id, 'medium', 'image/vnd.zbrush.pcx' );
$this->assertWPError( $result );
$this->assertSame( 'image_mime_type_invalid', $result->get_error_code() );
}
/**
* Prevent to process an image when the editor does not support the format
*/
public function test_it_should_prevent_to_process_an_image_when_the_editor_does_not_support_the_format(): void {
// Make sure no editor is available.
$attachment_id = self::factory()->attachment->create_upload_object(
TESTS_PLUGIN_DIR . '/tests/data/images/leaves.jpg'
);
add_filter(
'wp_image_editors',
static function () {
return array( 'WP_Image_Doesnt_Support_Modern_Images' );
}
);
$result = webp_uploads_generate_image_size( $attachment_id, 'medium', 'image/webp' );
$this->assertWPError( $result );
$this->assertSame( 'image_mime_type_not_supported', $result->get_error_code() );
}
/**
* Create an image with the filter webp_uploads_pre_generate_additional_image_source added.
*/
public function test_it_should_create_an_image_with_filter_webp_uploads_pre_generate_additional_image_source(): void {
remove_all_filters( 'webp_uploads_pre_generate_additional_image_source' );
$attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/car.jpeg' );
add_filter(
'webp_uploads_pre_generate_additional_image_source',
static function () {
return array(
'file' => 'image.webp',
'filesize' => 1024,
);
}
);
$size_data = array(
'width' => 300,
'height' => 300,
'crop' => true,
);
$result = webp_uploads_generate_additional_image_source( $attachment_id, 'medium', $size_data, 'image/webp', '/tmp/image.jpg' );
$this->assertIsArray( $result );
$this->assertArrayHasKey( 'filesize', $result );
$this->assertArrayHasKey( 'file', $result );
$this->assertStringEndsWith( 'image.webp', $result['file'] );
}
/**
* Tests the webp_uploads_pre_generate_additional_image_source filter returning filesize property.
*/
public function test_it_should_use_filesize_when_filter_webp_uploads_pre_generate_additional_image_source_returns_filesize(): void {
remove_all_filters( 'webp_uploads_pre_generate_additional_image_source' );
$attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/car.jpeg' );
add_filter(
'webp_uploads_pre_generate_additional_image_source',
static function () {
return array(
'file' => 'image.webp',
'filesize' => 777,
);
}
);
$size_data = array(
'width' => 300,
'height' => 300,
'crop' => true,
);
$result = webp_uploads_generate_additional_image_source( $attachment_id, 'medium', $size_data, 'image/webp', '/tmp/image.jpg' );
$this->assertIsArray( $result );
$this->assertArrayHasKey( 'filesize', $result );
$this->assertEquals( 777, $result['filesize'] );
$this->assertArrayHasKey( 'file', $result );
$this->assertStringEndsWith( 'image.webp', $result['file'] );
}
/**
* Return an error when filter webp_uploads_pre_generate_additional_image_source returns WP_Error.
*/
public function test_it_should_return_an_error_when_filter_webp_uploads_pre_generate_additional_image_source_returns_wp_error(): void {
remove_all_filters( 'webp_uploads_pre_generate_additional_image_source' );
$attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/car.jpeg' );
add_filter(
'webp_uploads_pre_generate_additional_image_source',
static function () {
return new WP_Error( 'image_additional_generated_error', __( 'Additional image was not generated.', 'webp-uploads' ) );
}
);
$size_data = array(
'width' => 300,
'height' => 300,
'crop' => true,
);
$result = webp_uploads_generate_additional_image_source( $attachment_id, 'medium', $size_data, 'image/webp', '/tmp/image.jpg' );
$this->assertWPError( $result );
$this->assertSame( 'image_additional_generated_error', $result->get_error_code() );
}
/**
* Test that image is cropped correctly when crop is an array.
*
* @covers ::webp_uploads_generate_additional_image_source
*/
public function test_it_should_crop_image_with_array_crop_value(): void {
if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
$this->markTestSkipped( 'Mime type image/webp is not supported.' );
}
$attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/car.jpeg' );
$size_data = array(
'width' => 300,
'height' => 300,
'crop' => array( 'left', 'top' ),
);
$captured_crop = null;
// Add filter to intercept the crop value.
remove_all_filters( 'image_resize_dimensions' );
add_filter(
'image_resize_dimensions',
static function ( $passthrough, $orig_w, $orig_h, $dest_w, $dest_h, $crop ) use ( &$captured_crop ) {
$captured_crop = $crop;
return $passthrough;
},
10,
6
);
$result = webp_uploads_generate_additional_image_source( $attachment_id, 'medium', $size_data, 'image/webp', '/tmp/image.jpg' );
$this->assertIsArray( $result );
$this->assertArrayHasKey( 'filesize', $result );
$this->assertArrayHasKey( 'file', $result );
$this->assertStringEndsWith( 'image.webp', $result['file'] );
$this->assertFileExists( '/tmp/image.webp' );
// Get image dimensions to verify crop worked.
$image_editor = wp_get_image_editor( '/tmp/image.webp' );
$size = $image_editor->get_size();
// Verify dimensions are as expected.
$this->assertEquals( 300, $size['width'] );
$this->assertEquals( 300, $size['height'] );
// Verify crop value is as expected.
$this->assertEquals( array( 'left', 'top' ), $captured_crop );
}
/**
* Returns an empty array when the overwritten with empty array by webp_uploads_upload_image_mime_transforms filter.
*/
public function test_it_should_return_empty_array_when_filter_returns_empty_array(): void {
add_filter( 'webp_uploads_upload_image_mime_transforms', '__return_empty_array' );
$transforms = webp_uploads_get_upload_image_mime_transforms();
$this->assertSame( array(), $transforms );
}
/**
* Returns default transforms when the overwritten with non array type by webp_uploads_upload_image_mime_transforms filter.
*/
public function test_it_should_return_default_transforms_when_filter_returns_non_array_type(): void {
add_filter( 'webp_uploads_upload_image_mime_transforms', '__return_zero' );
if ( webp_uploads_mime_type_supported( 'image/avif' ) ) {
$this->set_image_output_type( 'avif' );
$default_transforms = array(
'image/jpeg' => array( 'image/avif' ),
'image/webp' => array( 'image/avif' ),
'image/avif' => array( 'image/avif' ),
'image/png' => array( 'image/avif' ),
'application/pdf' => array( 'image/avif' ),
);
} else {
$default_transforms = array(
'image/jpeg' => array( 'image/webp' ),
'image/webp' => array( 'image/webp' ),
'image/avif' => array( 'image/avif' ),
'image/png' => array( 'image/webp' ),
'application/pdf' => array( 'image/webp' ),
);
}
$transforms = webp_uploads_get_upload_image_mime_transforms();
$this->assertSame( $default_transforms, $transforms );
}
/**
* Returns transforms array with fallback to original mime with invalid transforms array.
*/
public function test_it_should_return_fallback_transforms_when_overwritten_invalid_transforms(): void {
add_filter(
'webp_uploads_upload_image_mime_transforms',
static function () {
return array( 'image/jpeg' => array() );
}
);
$transforms = webp_uploads_get_upload_image_mime_transforms();
$this->assertSame( array( 'image/jpeg' => array( 'image/jpeg' ) ), $transforms );
}
/**
* Returns custom transforms array when overwritten by webp_uploads_upload_image_mime_transforms filter.
*/
public function test_it_should_return_custom_transforms_when_overwritten_by_filter(): void {
add_filter(
'webp_uploads_upload_image_mime_transforms',
static function () {
return array( 'image/jpeg' => array( 'image/jpeg', 'image/webp' ) );
}
);
$transforms = webp_uploads_get_upload_image_mime_transforms();
$this->assertSame( array( 'image/jpeg' => array( 'image/jpeg', 'image/webp' ) ), $transforms );
}
/**
* Returns JPG and WebP transforms array when perflab_generate_webp_and_jpeg option is true.
*/
public function test_it_should_return_jpeg_and_webp_transforms_when_option_generate_webp_and_jpeg_set(): void {
remove_all_filters( 'webp_uploads_get_upload_image_mime_transforms' );
if ( webp_uploads_mime_type_supported( 'image/avif' ) ) {
$this->set_image_output_type( 'avif' );
}
update_option( 'perflab_generate_webp_and_jpeg', true );
$transforms = webp_uploads_get_upload_image_mime_transforms();
// The returned value depends on whether the server supports AVIF.
if ( webp_uploads_mime_type_supported( 'image/avif' ) ) {
$this->assertSame(
array(
'image/jpeg' => array( 'image/jpeg', 'image/avif' ),
'image/png' => array( 'image/png', 'image/avif' ),
'image/avif' => array( 'image/avif', 'image/jpeg' ),
),
$transforms
);
} else {
$this->assertSame(
array(
'image/jpeg' => array( 'image/jpeg', 'image/webp' ),
'image/png' => array( 'image/png', 'image/webp' ),
'image/webp' => array( 'image/webp', 'image/jpeg' ),
),
$transforms
);
}
}
/**
* Returns true if 'application/pdf' is included in the MIME transforms array.
*/
public function test_it_should_include_pdf_in_mime_transforms(): void {
$transforms = webp_uploads_get_upload_image_mime_transforms();
$this->assertArrayHasKey( 'application/pdf', $transforms );
$this->assertContains( 'image/webp', $transforms['application/pdf'] );
}
/**
* @dataProvider data_provider_image_filesize
*
* @param array{ filesize?: int } $original_filesize Original file size.
* @param array{ filesize?: int } $additional_filesize Additional file size.
* @param bool $expected_status Expected status.
*/
public function test_it_should_discard_additional_image_if_larger_than_the_original_image( array $original_filesize, array $additional_filesize, bool $expected_status ): void {
add_filter( 'webp_uploads_discard_larger_generated_images', '__return_true' );
$output = webp_uploads_should_discard_additional_image_file( $original_filesize, $additional_filesize );
$this->assertSame( $output, $expected_status );
}
/** @return array<int, mixed> */
public function data_provider_image_filesize(): array {
return array(
array(
array( 'filesize' => 120101 ),
array( 'filesize' => 100101 ),
false,
),
array(
array( 'filesize' => 100101 ),
array( 'filesize' => 120101 ),
true,
),
array(
array( 'filesize' => 10101 ),
array( 'filesize' => 10101 ),
true,
),
);
}
/**
* @dataProvider data_provider_image_filesize
*
* @param array{ filesize?: int } $original_filesize Original file size.
* @param array{ filesize?: int } $additional_filesize Additional file size.
*/
public function test_it_should_never_discard_additional_image_if_filter_is_false( array $original_filesize, array $additional_filesize ): void {
add_filter( 'webp_uploads_discard_larger_generated_images', '__return_false' );
$output = webp_uploads_should_discard_additional_image_file( $original_filesize, $additional_filesize );
$this->assertFalse( $output );
}
public function test_webp_uploads_in_frontend_body_without_wp_query(): void {
unset( $GLOBALS['wp_query'] );
$this->assertFalse( webp_uploads_in_frontend_body() );
}
public function test_webp_uploads_in_frontend_body_with_feed(): void {
$this->mock_empty_action( 'template_redirect' );
$GLOBALS['wp_query']->is_feed = true;
$this->assertFalse( webp_uploads_in_frontend_body() );
}
public function test_webp_uploads_in_frontend_body_without_template_redirect(): void {
$this->assertFalse( webp_uploads_in_frontend_body() );
}
public function test_webp_uploads_in_frontend_body_before_template_redirect(): void {
$result = webp_uploads_in_frontend_body();
$this->mock_empty_action( 'template_redirect' );
$this->assertFalse( $result );
}
public function test_webp_uploads_in_frontend_body_after_template_redirect(): void {
$this->mock_empty_action( 'template_redirect' );
$result = webp_uploads_in_frontend_body();
$this->assertTrue( $result );
}
public function test_webp_uploads_in_frontend_body_within_wp_head(): void {
$this->mock_empty_action( 'template_redirect' );
// Call function within a 'wp_head' callback.
remove_all_actions( 'wp_head' );
$result = null;
add_action(
'wp_head',
static function () use ( &$result ): void {
$result = webp_uploads_in_frontend_body();
}
);
do_action( 'wp_head' );
$this->assertFalse( $result );
}
private function mock_empty_action( string $action ): void {
remove_all_actions( $action );
do_action( $action );
}
/**
* Add the original image's extension to the WebP file name to ensure it is unique
*
* @dataProvider data_provider_same_image_name
*/
public function test_it_should_add_original_image_extension_to_the_webp_file_name_to_ensure_it_is_unique( string $jpeg_image, string $jpg_image ): void {
if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
$this->markTestSkipped( 'Mime type image/webp is not supported.' );
}
$jpeg_image_attachment_id = self::factory()->attachment->create_upload_object( $jpeg_image );
$jpg_image_attachment_id = self::factory()->attachment->create_upload_object( $jpg_image );
$size_data = array(
'width' => 300,
'height' => 300,
'crop' => true,
);
$jpeg_image_result = webp_uploads_generate_additional_image_source( $jpeg_image_attachment_id, 'medium', $size_data, 'image/webp' );
$jpg_image_result = webp_uploads_generate_additional_image_source( $jpg_image_attachment_id, 'medium', $size_data, 'image/webp' );
$this->assertIsArray( $jpeg_image_result );
$this->assertIsArray( $jpg_image_result );
$this->assertStringEndsWith( '300x300-jpeg.webp', $jpeg_image_result['file'] );
$this->assertStringEndsWith( '300x300-jpg.webp', $jpg_image_result['file'] );
$this->assertNotSame( $jpeg_image_result['file'], $jpg_image_result['file'] );
}
/** @return array<int, mixed> */
public function data_provider_same_image_name(): array {
return array(
array(
TESTS_PLUGIN_DIR . '/tests/data/images/image.jpeg',
TESTS_PLUGIN_DIR . '/tests/data/images/image.jpg',
),
);
}
/**
* @return array<string, mixed>
*/
public function data_provider_to_test_webp_uploads_sanitize_image_format(): array {
return array(
'null' => array(
'input' => null,
'expected' => 'webp',
),
'array' => array(
'input' => array( 'tiff' ),
'expected' => 'webp',
),
'webp' => array(
'input' => 'webp',
'expected' => 'webp',
),
'avif' => array(
'input' => 'avif',
'expected' => 'avif',
),
'bmp' => array(
'input' => 'bmp',
'expected' => 'webp',
),
);
}
/**
* @dataProvider data_provider_to_test_webp_uploads_sanitize_image_format
* @covers ::webp_uploads_sanitize_image_format
*
* @param mixed $input Input.
* @param string $expected Expected.
*/
public function test_webp_uploads_sanitize_image_format( $input, string $expected ): void {
$this->assertSame( $expected, webp_uploads_sanitize_image_format( $input ) );
}
}