forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
1155 lines (1010 loc) · 31.8 KB
/
functions.php
File metadata and controls
1155 lines (1010 loc) · 31.8 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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @group image
* @group media
* @group upload
*/
class Tests_Image_Functions extends WP_UnitTestCase {
/**
* Includes the required files.
*/
public function set_up() {
parent::set_up();
require_once ABSPATH . WPINC . '/class-wp-image-editor.php';
require_once ABSPATH . WPINC . '/class-wp-image-editor-gd.php';
require_once ABSPATH . WPINC . '/class-wp-image-editor-imagick.php';
require_once DIR_TESTDATA . '/../includes/mock-image-editor.php';
// Ensure no legacy / failed tests detritus.
$folder = get_temp_dir() . 'wordpress-gsoc-flyer*.*';
foreach ( glob( $folder ) as $file ) {
unlink( $file );
}
}
/**
* Gets the available image editor engine classes.
*
* @return string[] Available image editor classes; empty array when none are available.
*/
private function get_image_editor_engine_classes() {
$classes = array( 'WP_Image_Editor_GD', 'WP_Image_Editor_Imagick' );
foreach ( $classes as $key => $class ) {
if ( ! call_user_func( array( $class, 'test' ) ) ) {
// If the image editor isn't available, skip it.
unset( $classes[ $key ] );
}
}
return $classes;
}
/**
* Data provider with the available image editor engine classes.
*
* @return array
*/
public function data_image_editor_engine_classes() {
return $this->text_array_to_dataprovider( $this->get_image_editor_engine_classes() );
}
/**
* Gets the MIME type of a file.
*
* @param string $filename
* @return string
*/
protected function get_mime_type( $filename ) {
$mime_type = '';
if ( extension_loaded( 'fileinfo' ) ) {
$finfo = new finfo();
$mime_type = $finfo->file( $filename, FILEINFO_MIME );
}
if ( false !== strpos( $mime_type, ';' ) ) {
list( $mime_type, $charset ) = explode( ';', $mime_type, 2 );
}
return $mime_type;
}
/**
* @dataProvider data_file_is_valid_image_positive
*
* @covers ::file_is_valid_image
* @covers ::wp_getimagesize
*
* @param string $file File name.
*/
public function test_file_is_valid_image_positive( $file ) {
$this->assertTrue(
file_is_valid_image( DIR_TESTDATA . '/images/' . $file ),
"file_is_valid_image( '$file' ) should return true."
);
}
/**
* Data provider.
*
* @return array
*/
public function data_file_is_valid_image_positive() {
// These are all image files recognized by PHP.
$files = array(
'test-image-cmyk.jpg',
'test-image-grayscale.jpg',
'test-image.bmp',
'test-image.gif',
'test-image.png',
'test-image.tiff',
'test-image-lzw.tiff',
'test-image.jp2',
'test-image.psd',
'test-image-zip.tiff',
'test-image.jpg',
'test-image.ico',
'webp-animated.webp',
'webp-lossless.webp',
'webp-lossy.webp',
'webp-transparent.webp',
'avif-animated.avif',
'avif-lossless.avif',
'avif-lossy.avif',
'avif-transparent.avif',
);
return $this->text_array_to_dataprovider( $files );
}
/**
* @dataProvider data_file_is_valid_image_negative
*
* @covers ::file_is_valid_image
* @covers ::wp_getimagesize
*
* @param string $file File name.
*/
public function test_file_is_valid_image_negative( $file ) {
$this->assertFalse(
file_is_valid_image( DIR_TESTDATA . '/images/' . $file ),
"file_is_valid_image( '$file' ) should return false."
);
}
/**
* Data provider.
*
* @return array
*/
public function data_file_is_valid_image_negative() {
// These are actually image files but aren't recognized or usable by PHP.
$files = array(
'test-image.pct',
'test-image.tga',
'test-image.sgi',
);
return $this->text_array_to_dataprovider( $files );
}
/**
* @dataProvider data_file_is_displayable_image_positive
*
* @covers ::file_is_displayable_image
*
* @param string $file File name.
*/
public function test_file_is_displayable_image_positive( $file ) {
$this->assertTrue(
file_is_displayable_image( DIR_TESTDATA . '/images/' . $file ),
"file_is_displayable_image( '$file' ) should return true."
);
}
/**
* Data provider.
*
* @return array
*/
public function data_file_is_displayable_image_positive() {
// These are all usable in typical web browsers.
$files = array(
'test-image.gif',
'test-image.png',
'test-image.jpg',
'test-image.ico',
);
// Add WebP images if the image editor supports them.
$file = DIR_TESTDATA . '/images/test-image.webp';
$editor = wp_get_image_editor( $file );
if ( ! is_wp_error( $editor ) && $editor->supports_mime_type( 'image/webp' ) ) {
$files[] = 'webp-animated.webp';
$files[] = 'webp-lossless.webp';
$files[] = 'webp-lossy.webp';
$files[] = 'webp-transparent.webp';
}
// Add AVIF images if the image editor supports them.
$file = DIR_TESTDATA . '/images/avif-lossless.avif';
$editor = wp_get_image_editor( $file );
if ( ! is_wp_error( $editor ) && $editor->supports_mime_type( 'image/avif' ) ) {
$files[] = 'avif-animated.avif';
$files[] = 'avif-lossless.avif';
$files[] = 'avif-lossy.avif';
$files[] = 'avif-transparent.avif';
}
return $this->text_array_to_dataprovider( $files );
}
/**
* @dataProvider data_file_is_displayable_image_negative
*
* @covers ::file_is_displayable_image
*
* @param string $file File name.
*/
public function test_file_is_displayable_image_negative( $file ) {
$this->assertFalse(
file_is_displayable_image( DIR_TESTDATA . '/images/' . $file ),
"file_is_displayable_image( '$file' ) should return false."
);
}
/**
* Data provider.
*
* @return array
*/
public function data_file_is_displayable_image_negative() {
// These are image files but aren't suitable for web pages because of compatibility or size issues.
$files = array(
// 'test-image-cmyk.jpg', Allowed in r9727.
// 'test-image-grayscale.jpg', Allowed in r9727.
// 'test-image.bmp', Allowed in r28589.
'test-image.pct',
'test-image.tga',
'test-image.sgi',
'test-image.tiff',
'test-image-lzw.tiff',
'test-image.jp2',
'test-image.psd',
'test-image-zip.tiff',
);
return $this->text_array_to_dataprovider( $files );
}
/**
* @ticket 50833
* @requires extension gd
*/
public function test_is_gd_image_valid_types() {
$this->assertTrue( is_gd_image( imagecreate( 5, 5 ) ) );
}
/**
* @ticket 50833
*/
public function test_is_gd_image_invalid_types() {
$this->assertFalse( is_gd_image( new stdClass() ) );
$this->assertFalse( is_gd_image( array() ) );
$this->assertFalse( is_gd_image( null ) );
$handle = fopen( __FILE__, 'r' );
$this->assertFalse( is_gd_image( $handle ) );
fclose( $handle );
}
/**
* Tests wp_save_image_file() and mime types.
*
* @dataProvider data_wp_save_image_file
*
* @ticket 6821
* @covers ::wp_save_image_file
* @requires extension fileinfo
*
* @param string $class_name Name of the image editor engine class to be tested.
* @param string $mime_type The mime type to test.
*/
public function test_wp_save_image_file( $class_name, $mime_type ) {
require_once ABSPATH . 'wp-admin/includes/image-edit.php';
$img = new $class_name( DIR_TESTDATA . '/images/canola.jpg' );
$loaded = $img->load();
$this->assertNotWPError( $loaded, 'Image failed to load - WP_Error returned.' );
if ( ! $img->supports_mime_type( $mime_type ) ) {
$this->markTestSkipped(
sprintf(
'The %s mime type is not supported by the %s engine.',
$mime_type,
str_replace( 'WP_Image_Editor_', '', $class_name )
)
);
}
// Save the file.
$file = wp_tempnam();
$ret = wp_save_image_file( $file, $img, $mime_type, 1 );
// Make assertions.
$this->assertNotWPError( $ret, 'Image failed to save - WP_Error returned.' );
$this->assertIsArray( $ret, 'Image failed to save - non-array response returned.' );
$this->assertSame( $mime_type, $this->get_mime_type( $ret['path'] ), 'Mime type of the saved image does not match.' );
// Clean up.
unlink( $file );
unlink( $ret['path'] );
unset( $img );
}
/**
* Data provider.
*
* @return array
*/
public function data_wp_save_image_file() {
$classes = $this->get_image_editor_engine_classes();
// Mime types.
$mime_types = array(
'image/jpeg',
'image/gif',
'image/png',
);
// Include WebP in tests when platform supports it.
if ( function_exists( 'imagewebp' ) ) {
$mime_types[] = 'image/webp';
}
$data = array();
foreach ( $classes as $class ) {
foreach ( $mime_types as $mime_type ) {
$data[ $class . '; ' . $mime_type ] = array(
'class_name' => $class,
'mime_type' => $mime_type,
);
}
}
return $data;
}
/**
* Tests that wp_save_image_file() throws a deprecated argument notice when passed a GD resource.
*
* @ticket 6821
* @expectedDeprecated wp_save_image_file
* @requires function imagejpeg
*
* @covers ::wp_save_image_file
*/
public function test_wp_save_image_file_deprecated_argument_with_gd_resource() {
require_once ABSPATH . 'wp-admin/includes/image-edit.php';
// Call wp_save_image_file().
$file = wp_tempnam();
$img = imagecreatefromjpeg( DIR_TESTDATA . '/images/canola.jpg' );
$ret = wp_save_image_file( $file, $img, 'image/jpeg', 1 );
imagedestroy( $img );
unlink( $file );
$this->assertTrue( $ret, 'Image failed to save.' );
}
/**
* Tests that `wp_image_editor()` applies 'image_edit_thumbnails_separately' filters.
*
* @ticket 53161
*
* @covers ::wp_image_editor
*/
public function test_wp_image_editor_should_apply_image_edit_thumbnails_separately_filters() {
require_once ABSPATH . 'wp-admin/includes/image-edit.php';
$filename = DIR_TESTDATA . '/images/canola.jpg';
$contents = file_get_contents( $filename );
$upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
$id = $this->_make_attachment( $upload );
$filter = new MockAction();
add_filter( 'image_edit_thumbnails_separately', array( &$filter, 'filter' ) );
ob_start();
wp_image_editor( $id );
ob_end_clean();
$this->assertSame( 1, $filter->get_call_count() );
}
/**
* Tests that `wp_image_editor()` conditionally outputs markup for editing thumbnails separately
* based on the result of applying 'image_edit_thumbnails_separately' filters.
*
* @ticket 53161
*
* @covers ::wp_image_editor
*
* @dataProvider data_wp_image_editor_should_respect_image_edit_thumbnails_separately_filters
*
* @param string $callback The name of the callback for the 'image_edit_thumbnails_separately' hook.
* @param bool $expected Whether the markup should be output.
*/
public function test_wp_image_editor_should_respect_image_edit_thumbnails_separately_filters( $callback, $expected ) {
require_once ABSPATH . 'wp-admin/includes/image-edit.php';
$filename = DIR_TESTDATA . '/images/canola.jpg';
$contents = file_get_contents( $filename );
$upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
$id = $this->_make_attachment( $upload );
add_filter( 'image_edit_thumbnails_separately', $callback );
ob_start();
wp_image_editor( $id );
$actual = ob_get_clean();
if ( $expected ) {
$this->assertStringContainsString(
'imgedit-applyto',
$actual,
'The markup should have been output.'
);
} else {
$this->assertStringNotContainsString(
'imgedit-applyto',
$actual,
'The markup should not have been output.'
);
}
}
/**
* Data provider.
*
* @return array[]
*/
public function data_wp_image_editor_should_respect_image_edit_thumbnails_separately_filters() {
return array(
'true' => array(
'callback' => '__return_true',
'expected' => true,
),
'false' => array(
'callback' => '__return_false',
'expected' => false,
),
);
}
/**
* Tests that a passed mime type overrides the extension in the filename when saving an image.
*
* @dataProvider data_image_editor_engine_classes
*
* @ticket 6821
* @covers WP_Image_Editor::get_mime_type
* @covers WP_Image_Editor::get_output_format
* @requires extension fileinfo
*
* @param string $class_name Name of the image editor engine class to be tested.
*/
public function test_mime_overrides_filename_when_saving_an_image( $class_name ) {
$img = new $class_name( DIR_TESTDATA . '/images/canola.jpg' );
$loaded = $img->load();
$this->assertNotWPError( $loaded, 'Image failed to load - WP_Error returned.' );
// Save the file.
$mime_type = 'image/gif';
$file = wp_tempnam( 'tmp.jpg' );
$ret = $img->save( $file, $mime_type );
// Make assertions.
$this->assertNotWPError( $ret, 'Image failed to save - WP_Error returned.' );
$this->assertIsArray( $ret, 'Image failed to save - non-array response returned.' );
$this->assertSame( $mime_type, $this->get_mime_type( $ret['path'] ), 'Mime type of the saved image did not override file name.' );
// Clean up.
unlink( $file );
unlink( $ret['path'] );
unset( $img );
}
/**
* Tests that mime types are correctly inferred from file extensions when saving an image.
*
* @dataProvider data_inferred_mime_types_when_saving_an_image
*
* @ticket 6821
* @covers WP_Image_Editor::get_mime_type
* @covers WP_Image_Editor::get_output_format
* @requires extension fileinfo
*
* @param string $class_name Name of the image editor engine class to be tested.
* @param string $extension File extension.
* @param string $mime_type The mime type to test.
*/
public function test_inferred_mime_types_when_saving_an_image( $class_name, $extension, $mime_type ) {
$img = new $class_name( DIR_TESTDATA . '/images/canola.jpg' );
$loaded = $img->load();
$this->assertNotWPError( $loaded, 'Image failed to load - WP_Error returned.' );
if ( ! $img->supports_mime_type( $mime_type ) ) {
$this->markTestSkipped(
sprintf(
'The %s mime type is not supported by the %s engine.',
$mime_type,
str_replace( 'WP_Image_Editor_', '', $class_name )
)
);
}
// Save the file.
$temp = get_temp_dir();
$file = wp_unique_filename( $temp, uniqid() . ".$extension" );
$ret = $img->save( trailingslashit( $temp ) . $file );
// Make assertions.
$this->assertNotWPError( $ret, 'Image failed to save - WP Error returned.' );
$this->assertIsArray( $ret, 'Image failed to save - non-array response returned.' );
$this->assertSame( $mime_type, $this->get_mime_type( $ret['path'] ), 'Mime type of the saved image was not inferred correctly.' );
// Clean up.
unlink( $ret['path'] );
unset( $img );
}
/**
* Data provider.
*
* @return array
*/
public function data_inferred_mime_types_when_saving_an_image() {
$classes = $this->get_image_editor_engine_classes();
// Mime types.
$mime_types = array(
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'jpe' => 'image/jpeg',
'gif' => 'image/gif',
'png' => 'image/png',
'webp' => 'image/webp',
'unk' => 'image/jpeg', // Default, unknown.
);
$data = array();
foreach ( $classes as $class ) {
foreach ( $mime_types as $ext => $mime_type ) {
$data[ $class . '; Extension: ' . $ext . '; Mime type: ' . $mime_type ] = array(
'class_name' => $class,
'extension' => $ext,
'mime_type' => $mime_type,
);
}
}
return $data;
}
/**
* Tests that the deprecated wp_load_image() function fails when loading a directory.
*
* @ticket 17814
* @covers ::wp_load_image
* @expectedDeprecated wp_load_image
*/
public function test_wp_load_image_should_fail_with_error_message_when_loading_a_directory() {
$editor = wp_load_image( DIR_TESTDATA );
$this->assertIsString( $editor );
}
/**
* Tests that the wp_get_image_editor() function fails when loading a directory.
*
* @ticket 17814
* @covers ::wp_get_image_editor
*/
public function test_wp_get_image_editor_should_fail_with_wp_error_object_when_loading_a_directory() {
$editor = wp_get_image_editor( DIR_TESTDATA );
$this->assertInstanceOf( 'WP_Error', $editor );
}
/**
* Tests that the load() method in an image editor class fails when loading a directory.
*
* @dataProvider data_image_editor_engine_classes
*
* @ticket 17814
* @covers WP_Image_Editor_GD::load
* @covers WP_Image_Editor_Imagick::load
*
* @param string $class_name Name of the image editor engine class to be tested.
*/
public function test_image_editor_classes_should_fail_with_wp_error_object_when_loading_a_directory( $class_name ) {
$editor = new $class_name( DIR_TESTDATA );
$loaded = $editor->load();
$this->assertInstanceOf( 'WP_Error', $loaded, 'Loading a directory did not result in a WP_Error.' );
$this->assertSame( 'error_loading_image', $loaded->get_error_code(), 'Error code from WP_Error did not match expectation.' );
}
/**
* @covers ::wp_crop_image
* @requires function imagejpeg
*/
public function test_wp_crop_image_with_file() {
$file = wp_crop_image(
DIR_TESTDATA . '/images/canola.jpg',
0,
0,
100,
100,
100,
100
);
$this->assertNotWPError( $file, 'Cropping the image resulted in a WP_Error.' );
$this->assertFileExists( $file, "The file $file does not exist." );
$image = wp_get_image_editor( $file );
$size = $image->get_size();
$this->assertSame( 100, $size['height'], 'Cropped image height does not match expectation.' );
$this->assertSame( 100, $size['width'], 'Cropped image width does not match expectation.' );
unlink( $file );
}
/**
* @covers ::wp_crop_image
* @requires function imagejpeg
* @requires extension openssl
*/
public function test_wp_crop_image_with_url() {
$file = wp_crop_image(
'https://s.w.org/screenshots/3.9/dashboard.png',
0,
0,
100,
100,
100,
100,
false,
DIR_TESTDATA . '/images/' . __FUNCTION__ . '.png'
);
if ( is_wp_error( $file ) && $file->get_error_code() === 'invalid_image' ) {
$this->markTestSkipped( 'Tests_Image_Functions::test_wp_crop_image_url() cannot access remote image.' );
}
$this->assertNotWPError( $file, 'Cropping the image resulted in a WP_Error.' );
$this->assertFileExists( $file, "The file $file does not exist." );
$image = wp_get_image_editor( $file );
$size = $image->get_size();
$this->assertSame( 100, $size['height'], 'Cropped image height does not match expectation.' );
$this->assertSame( 100, $size['width'], 'Cropped image width does not match expectation.' );
unlink( $file );
}
/**
* @covers ::wp_crop_image
*/
public function test_wp_crop_image_should_fail_with_wp_error_object_if_file_does_not_exist() {
$file = wp_crop_image(
DIR_TESTDATA . '/images/canoladoesnotexist.jpg',
0,
0,
100,
100,
100,
100
);
$this->assertInstanceOf( 'WP_Error', $file );
}
/**
* @covers ::wp_crop_image
* @requires extension openssl
*/
public function test_wp_crop_image_should_fail_with_wp_error_object_if_url_does_not_exist() {
$file = wp_crop_image(
'https://wordpress.org/screenshots/3.9/canoladoesnotexist.jpg',
0,
0,
100,
100,
100,
100
);
$this->assertInstanceOf( 'WP_Error', $file );
}
/**
* @ticket 23325
* @covers ::wp_crop_image
*/
public function test_wp_crop_image_should_fail_with_wp_error_object_if_there_was_an_error_on_saving() {
WP_Image_Editor_Mock::$save_return = new WP_Error();
add_filter(
'wp_image_editors',
static function ( $editors ) {
return array( 'WP_Image_Editor_Mock' );
}
);
$file = wp_crop_image(
DIR_TESTDATA . '/images/canola.jpg',
0,
0,
100,
100,
100,
100
);
$this->assertInstanceOf( 'WP_Error', $file );
WP_Image_Editor_Mock::$save_return = array();
}
/**
* @ticket 55403
* @covers ::wp_crop_image
*/
public function test_wp_crop_image_should_return_correct_file_extension_if_output_format_was_modified() {
add_filter(
'image_editor_output_format',
static function () {
return array_fill_keys( array( 'image/jpg', 'image/jpeg', 'image/png' ), 'image/webp' );
}
);
$file = wp_crop_image(
DIR_TESTDATA . '/images/canola.jpg',
0,
0,
100,
100,
100,
100
);
$this->assertNotWPError( $file, 'Cropping the image resulted in a WP_Error.' );
$this->assertFileExists( $file, "The file $file does not exist." );
unlink( $file );
}
/**
* @ticket 31050
*/
public function test_wp_generate_attachment_metadata_pdf() {
if ( ! wp_image_editor_supports( array( 'mime_type' => 'application/pdf' ) ) ) {
$this->markTestSkipped( 'Rendering PDFs is not supported on this system.' );
}
$orig_file = DIR_TESTDATA . '/images/wordpress-gsoc-flyer.pdf';
$test_file = get_temp_dir() . 'wordpress-gsoc-flyer.pdf';
copy( $orig_file, $test_file );
$editor = wp_get_image_editor( $test_file );
if ( is_wp_error( $editor ) ) {
$this->markTestSkipped( $editor->get_error_message() );
}
$attachment_id = self::factory()->attachment->create_object(
$test_file,
0,
array(
'post_mime_type' => 'application/pdf',
)
);
$this->assertNotWPError( $attachment_id, 'Could not create attachment - WP_Error returned.' );
$this->assertIsInt( $attachment_id, 'Could not create attachment - non-integer response returned.' );
$temp_dir = get_temp_dir();
$metadata = wp_generate_attachment_metadata( $attachment_id, $test_file );
$expected = array(
'sizes' => array(
'full' => array(
'file' => 'wordpress-gsoc-flyer-pdf.jpg',
'width' => 1088,
'height' => 1408,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf.jpg' ),
),
'medium' => array(
'file' => 'wordpress-gsoc-flyer-pdf-232x300.jpg',
'width' => 232,
'height' => 300,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-232x300.jpg' ),
),
'large' => array(
'file' => 'wordpress-gsoc-flyer-pdf-791x1024.jpg',
'width' => 791,
'height' => 1024,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-791x1024.jpg' ),
),
'thumbnail' => array(
'file' => 'wordpress-gsoc-flyer-pdf-116x150.jpg',
'width' => 116,
'height' => 150,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-116x150.jpg' ),
),
),
'filesize' => wp_filesize( $test_file ),
);
$this->assertSame( $expected, $metadata );
unlink( $test_file );
foreach ( $metadata['sizes'] as $size ) {
unlink( $temp_dir . $size['file'] );
}
}
/**
* Tests crop setting for PDF.
*
* @ticket 43226
*/
public function test_crop_setting_for_pdf() {
if ( ! wp_image_editor_supports( array( 'mime_type' => 'application/pdf' ) ) ) {
$this->markTestSkipped( 'Rendering PDFs is not supported on this system.' );
}
update_option( 'medium_crop', 1 );
$orig_file = DIR_TESTDATA . '/images/wordpress-gsoc-flyer.pdf';
$test_file = get_temp_dir() . 'wordpress-gsoc-flyer.pdf';
copy( $orig_file, $test_file );
$editor = wp_get_image_editor( $test_file );
if ( is_wp_error( $editor ) ) {
$this->markTestSkipped( $editor->get_error_message() );
}
$attachment_id = self::factory()->attachment->create_object(
$test_file,
0,
array(
'post_mime_type' => 'application/pdf',
)
);
$this->assertNotWPError( $attachment_id, 'Could not create attachment - WP_Error returned.' );
$this->assertIsInt( $attachment_id, 'Could not create attachment - non-integer response returned.' );
$temp_dir = get_temp_dir();
$metadata = wp_generate_attachment_metadata( $attachment_id, $test_file );
$expected = array(
'sizes' => array(
'full' => array(
'file' => 'wordpress-gsoc-flyer-pdf.jpg',
'width' => 1088,
'height' => 1408,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf.jpg' ),
),
'medium' => array(
'file' => 'wordpress-gsoc-flyer-pdf-300x300.jpg',
'width' => 300,
'height' => 300,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-300x300.jpg' ),
),
'large' => array(
'file' => 'wordpress-gsoc-flyer-pdf-791x1024.jpg',
'width' => 791,
'height' => 1024,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-791x1024.jpg' ),
),
'thumbnail' => array(
'file' => 'wordpress-gsoc-flyer-pdf-116x150.jpg',
'width' => 116,
'height' => 150,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-116x150.jpg' ),
),
),
'filesize' => wp_filesize( $test_file ),
);
$this->assertSame( $expected, $metadata );
unlink( $test_file );
foreach ( $metadata['sizes'] as $size ) {
unlink( $temp_dir . $size['file'] );
}
}
/**
* @ticket 39231
*/
public function test_fallback_intermediate_image_sizes() {
if ( ! wp_image_editor_supports( array( 'mime_type' => 'application/pdf' ) ) ) {
$this->markTestSkipped( 'Rendering PDFs is not supported on this system.' );
}
$orig_file = DIR_TESTDATA . '/images/wordpress-gsoc-flyer.pdf';
$test_file = get_temp_dir() . 'wordpress-gsoc-flyer.pdf';
copy( $orig_file, $test_file );
$editor = wp_get_image_editor( $test_file );
if ( is_wp_error( $editor ) ) {
$this->markTestSkipped( $editor->get_error_message() );
}
$attachment_id = self::factory()->attachment->create_object(
$test_file,
0,
array(
'post_mime_type' => 'application/pdf',
)
);
$this->assertNotWPError( $attachment_id, 'Could not create attachment - WP_Error returned.' );
$this->assertIsInt( $attachment_id, 'Could not create attachment - non-integer response returned.' );
add_image_size( 'test-size', 100, 100 );
add_filter( 'fallback_intermediate_image_sizes', array( $this, 'filter_fallback_intermediate_image_sizes' ), 10, 2 );
$metadata = wp_generate_attachment_metadata( $attachment_id, $test_file );
$temp_dir = get_temp_dir();
$expected = array(
'file' => 'wordpress-gsoc-flyer-pdf-77x100.jpg',
'width' => 77,
'height' => 100,
'mime-type' => 'image/jpeg',
'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-77x100.jpg' ),
);
// Different environments produce slightly different filesize results.
$this->assertSame( $metadata['sizes']['test-size'], $expected );
$this->assertArrayHasKey( 'test-size', $metadata['sizes'], 'The `test-size` was not added to the metadata.' );
$this->assertSame( $expected, $metadata['sizes']['test-size'] );
remove_image_size( 'test-size' );
remove_filter( 'fallback_intermediate_image_sizes', array( $this, 'filter_fallback_intermediate_image_sizes' ), 10 );
unlink( $test_file );
foreach ( $metadata['sizes'] as $size ) {
unlink( $temp_dir . $size['file'] );
}
}
public function filter_fallback_intermediate_image_sizes( $fallback_sizes, $metadata ) {
// Add the 'test-size' to the list of fallback sizes.
$fallback_sizes[] = 'test-size';
return $fallback_sizes;
}
/**
* Tests that PDF preview does not overwrite existing JPEG.
*
* @ticket 39875
*/
public function test_pdf_preview_doesnt_overwrite_existing_jpeg() {
if ( ! wp_image_editor_supports( array( 'mime_type' => 'application/pdf' ) ) ) {
$this->markTestSkipped( 'Rendering PDFs is not supported on this system.' );
}
$temp_dir = get_temp_dir();
// Dummy JPEGs.
$jpg1_path = $temp_dir . 'test.jpg'; // Straight.
file_put_contents( $jpg1_path, 'asdf' );
$jpg2_path = $temp_dir . 'test-pdf.jpg'; // With PDF marker.
file_put_contents( $jpg2_path, 'fdsa' );
// PDF with same name as JPEG.
$pdf_path = $temp_dir . 'test.pdf';
copy( DIR_TESTDATA . '/images/wordpress-gsoc-flyer.pdf', $pdf_path );
$editor = wp_get_image_editor( $pdf_path );
if ( is_wp_error( $editor ) ) {
$this->markTestSkipped( $editor->get_error_message() );
}