forked from WordPress/wordpress-develop
/
media.php
2993 lines (2504 loc) · 87.7 KB
/
media.php
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 media
* @group shortcode
*/
class Tests_Media extends WP_UnitTestCase {
protected static $large_id;
protected static $_sizes;
protected static $large_filename = 'test-image-large.jpg';
public static function wpSetUpBeforeClass( $factory ) {
self::$_sizes = wp_get_additional_image_sizes();
$GLOBALS['_wp_additional_image_sizes'] = array();
$filename = DIR_TESTDATA . '/images/' . self::$large_filename;
self::$large_id = $factory->attachment->create_upload_object( $filename );
}
public static function wpTearDownAfterClass() {
$GLOBALS['_wp_additional_image_sizes'] = self::$_sizes;
}
public static function tearDownAfterClass(): void {
wp_delete_post( self::$large_id, true );
parent::tearDownAfterClass();
}
function setUp(): void {
parent::setUp();
$this->caption = 'A simple caption.';
$this->alternate_caption = 'Alternate caption.';
$this->html_content = <<<CAP
A <strong class='classy'>bolded</strong> <em>caption</em> with a <a href="#">link</a>.
CAP;
$this->img_content = <<<CAP
<img src="pic.jpg" id='anId' alt="pic"/>
CAP;
$this->img_name = 'image.jpg';
$this->img_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $this->img_name;
$this->img_html = '<img src="' . $this->img_url . '"/>';
$this->img_meta = array(
'width' => 100,
'height' => 100,
'sizes' => '',
);
}
function test_img_caption_shortcode_added() {
global $shortcode_tags;
$this->assertEquals( 'img_caption_shortcode', $shortcode_tags['caption'] );
$this->assertEquals( 'img_caption_shortcode', $shortcode_tags['wp_caption'] );
}
function test_img_caption_shortcode_with_empty_params() {
$result = img_caption_shortcode( array() );
$this->assertNull( $result );
}
/**
* @ticket 33981
*/
function test_img_caption_shortcode_with_empty_params_but_content() {
$result = img_caption_shortcode( array(), $this->caption );
$this->assertEquals( $this->caption, $result );
}
/**
* @ticket 33981
*/
function test_img_caption_shortcode_short_circuit_filter() {
add_filter( 'img_caption_shortcode', array( $this, '_return_alt_caption' ) );
$result = img_caption_shortcode( array(), $this->caption );
$this->assertEquals( $this->alternate_caption, $result );
}
/**
* Filter used in test_img_caption_shortcode_short_circuit_filter()
*/
function _return_alt_caption() {
return $this->alternate_caption;
}
/**
* @ticket 33981
*/
function test_img_caption_shortcode_empty_width() {
$result = img_caption_shortcode(
array(
'width' => 0,
),
$this->caption
);
$this->assertEquals( $this->caption, $result );
}
/**
* @ticket 33981
*/
function test_img_caption_shortcode_empty_caption() {
$result = img_caption_shortcode(
array(
'caption' => '',
)
);
$this->assertNull( $result );
}
/**
* @ticket 33981
*/
function test_img_caption_shortcode_empty_caption_and_content() {
$result = img_caption_shortcode(
array(
'caption' => '',
),
$this->caption
);
$this->assertEquals( $this->caption, $result );
}
function test_img_caption_shortcode_with_old_format() {
$result = img_caption_shortcode(
array(
'width' => 20,
'caption' => $this->caption,
)
);
$this->assertEquals( 2, preg_match_all( '/wp-caption/', $result, $_r ) );
$this->assertEquals( 1, preg_match_all( '/alignnone/', $result, $_r ) );
$this->assertEquals( 1, preg_match_all( "/{$this->caption}/", $result, $_r ) );
if ( current_theme_supports( 'html5', 'caption' ) ) {
$this->assertEquals( 1, preg_match_all( '/width: 20/', $result, $_r ) );
} else {
$this->assertEquals( 1, preg_match_all( '/width: 30/', $result, $_r ) );
}
}
function test_img_caption_shortcode_with_old_format_id_and_align() {
$result = img_caption_shortcode(
array(
'width' => 20,
'caption' => $this->caption,
'id' => '"myId',
'align' => '&myAlignment',
)
);
$this->assertEquals( 1, preg_match_all( '/wp-caption &myAlignment/', $result, $_r ) );
$this->assertEquals( 1, preg_match_all( '/id="myId"/', $result, $_r ) );
$this->assertEquals( 1, preg_match_all( "/{$this->caption}/", $result, $_r ) );
}
function test_img_caption_shortcode_with_old_format_and_class() {
$result = img_caption_shortcode(
array(
'width' => 20,
'class' => 'some-class another-class',
'caption' => $this->caption,
)
);
$this->assertEquals( 1, preg_match_all( '/wp-caption alignnone some-class another-class/', $result, $_r ) );
}
function test_new_img_caption_shortcode_with_html_caption() {
$result = img_caption_shortcode(
array(
'width' => 20,
'caption' => $this->html_content,
)
);
$our_preg = preg_quote( $this->html_content );
$this->assertEquals( 1, preg_match_all( "~{$our_preg}~", $result, $_r ) );
}
function test_new_img_caption_shortcode_new_format() {
$result = img_caption_shortcode(
array( 'width' => 20 ),
$this->img_content . $this->html_content
);
$img_preg = preg_quote( $this->img_content );
$content_preg = preg_quote( $this->html_content );
$this->assertEquals( 1, preg_match_all( "~{$img_preg}.*wp-caption-text~", $result, $_r ) );
$this->assertEquals( 1, preg_match_all( "~wp-caption-text.*{$content_preg}~", $result, $_r ) );
}
function test_new_img_caption_shortcode_new_format_and_linked_image() {
$linked_image = "<a href='#'>{$this->img_content}</a>";
$result = img_caption_shortcode(
array( 'width' => 20 ),
$linked_image . $this->html_content
);
$img_preg = preg_quote( $linked_image );
$content_preg = preg_quote( $this->html_content );
$this->assertEquals( 1, preg_match_all( "~{$img_preg}.*wp-caption-text~", $result, $_r ) );
$this->assertEquals( 1, preg_match_all( "~wp-caption-text.*{$content_preg}~", $result, $_r ) );
}
function test_new_img_caption_shortcode_new_format_and_linked_image_with_newline() {
$linked_image = "<a href='#'>{$this->img_content}</a>";
$result = img_caption_shortcode(
array( 'width' => 20 ),
$linked_image . "\n\n" . $this->html_content
);
$img_preg = preg_quote( $linked_image );
$content_preg = preg_quote( $this->html_content );
$this->assertEquals( 1, preg_match_all( "~{$img_preg}.*wp-caption-text~", $result, $_r ) );
$this->assertEquals( 1, preg_match_all( "~wp-caption-text.*{$content_preg}~", $result, $_r ) );
}
/**
* @ticket 34595
*/
function test_img_caption_shortcode_has_aria_describedby() {
$result = img_caption_shortcode(
array(
'width' => 20,
'id' => 'myId',
),
$this->img_content . $this->html_content
);
$this->assertEquals( 1, preg_match_all( '/aria-describedby="caption-myId"/', $result, $_r ) );
}
function test_add_remove_oembed_provider() {
wp_oembed_add_provider( 'http://foo.bar/*', 'http://foo.bar/oembed' );
$this->assertTrue( wp_oembed_remove_provider( 'http://foo.bar/*' ) );
$this->assertFalse( wp_oembed_remove_provider( 'http://foo.bar/*' ) );
}
/**
* @ticket 23776
*/
function test_autoembed_empty() {
global $wp_embed;
$content = '';
$result = $wp_embed->autoembed( $content );
$this->assertEquals( $content, $result );
}
/**
* @ticket 23776
*/
function test_autoembed_no_paragraphs_around_urls() {
global $wp_embed;
$content = <<<EOF
$ my command
First line.
http://example.com/1/
http://example.com/2/
Last line.
<pre>http://some.link/
http://some.other.link/</pre>
EOF;
$result = $wp_embed->autoembed( $content );
$this->assertEquals( $content, $result );
}
function data_autoembed() {
return array(
// Should embed.
array(
'https://w.org',
'[embed]',
),
array(
'test
https://w.org
test',
'test
[embed]
test',
),
array(
'<p class="test">https://w.org</p>',
'<p class="test">[embed]</p>',
),
array(
'<p> https://w.org </p>',
'<p> [embed] </p>',
),
array(
'<p>test
https://w.org
test</p>',
'<p>test
[embed]
test</p>',
),
array(
'<p>https://w.org
</p>',
'<p>[embed]
</p>',
),
// Should NOT embed.
array(
'test https://w.org</p>',
),
array(
'<span>https://w.org</a>',
),
array(
'<pre>https://w.org
</p>',
),
array(
'<a href="https://w.org">
https://w.org</a>',
),
);
}
/**
* @dataProvider data_autoembed
*/
function test_autoembed( $content, $result = null ) {
$wp_embed = new Test_Autoembed;
$this->assertEquals( $wp_embed->autoembed( $content ), $result ? $result : $content );
}
function test_wp_prepare_attachment_for_js() {
// Attachment without media.
$id = wp_insert_attachment(
array(
'post_status' => 'publish',
'post_title' => 'Prepare',
'post_content_filtered' => 'Prepare',
'post_type' => 'post',
)
);
$post = get_post( $id );
$prepped = wp_prepare_attachment_for_js( $post );
$this->assertIsArray( $prepped );
$this->assertEquals( 0, $prepped['uploadedTo'] );
$this->assertEquals( '', $prepped['mime'] );
$this->assertEquals( '', $prepped['type'] );
$this->assertEquals( '', $prepped['subtype'] );
// #21963, there will be a GUID always, so there will be a URL.
$this->assertNotEquals( '', $prepped['url'] );
$this->assertEquals( site_url( 'wp-includes/images/media/default.png' ), $prepped['icon'] );
// Fake a mime.
$post->post_mime_type = 'image/jpeg';
$prepped = wp_prepare_attachment_for_js( $post );
$this->assertEquals( 'image/jpeg', $prepped['mime'] );
$this->assertEquals( 'image', $prepped['type'] );
$this->assertEquals( 'jpeg', $prepped['subtype'] );
// Fake a mime without a slash. See #WP22532.
$post->post_mime_type = 'image';
$prepped = wp_prepare_attachment_for_js( $post );
$this->assertEquals( 'image', $prepped['mime'] );
$this->assertEquals( 'image', $prepped['type'] );
$this->assertEquals( '', $prepped['subtype'] );
// Test that if author is not found, we return "(no author)" as `display_name`.
// The previously used test post contains no author, so we can reuse it.
$this->assertEquals( '(no author)', $prepped['authorName'] );
// Test that if author has HTML entities in display_name, they're decoded correctly.
$html_entity_author = self::factory()->user->create(
array(
'display_name' => 'You & Me',
)
);
$post->post_author = $html_entity_author;
$prepped = wp_prepare_attachment_for_js( $post );
$this->assertEquals( 'You & Me', $prepped['authorName'] );
}
/**
* @ticket 38965
*/
function test_wp_prepare_attachment_for_js_without_image_sizes() {
// Create the attachement post.
$id = wp_insert_attachment(
array(
'post_title' => 'Attachment Title',
'post_type' => 'attachment',
'post_parent' => 0,
'post_mime_type' => 'image/jpeg',
'guid' => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test-image.jpg',
)
);
// Add attachment metadata without sizes.
wp_update_attachment_metadata(
$id,
array(
'width' => 50,
'height' => 50,
'file' => 'test-image.jpg',
)
);
$prepped = wp_prepare_attachment_for_js( get_post( $id ) );
$this->assertTrue( isset( $prepped['sizes'] ) );
}
/**
* @ticket 19067
* @expectedDeprecated wp_convert_bytes_to_hr
*/
function test_wp_convert_bytes_to_hr() {
$kb = 1024;
$mb = $kb * 1024;
$gb = $mb * 1024;
$tb = $gb * 1024;
// Test if boundaries are correct.
$this->assertEquals( '1TB', wp_convert_bytes_to_hr( $tb ) );
$this->assertEquals( '1GB', wp_convert_bytes_to_hr( $gb ) );
$this->assertEquals( '1MB', wp_convert_bytes_to_hr( $mb ) );
$this->assertEquals( '1KB', wp_convert_bytes_to_hr( $kb ) );
$this->assertEquals( '1 TB', size_format( $tb ) );
$this->assertEquals( '1 GB', size_format( $gb ) );
$this->assertEquals( '1 MB', size_format( $mb ) );
$this->assertEquals( '1 KB', size_format( $kb ) );
// Now some values around.
$hr = wp_convert_bytes_to_hr( $tb + $tb / 2 + $mb );
$this->assertEqualsWithDelta( 1.50000095367, (float) str_replace( ',', '.', $hr ), 0.0001, 'The values should be equal' );
$hr = wp_convert_bytes_to_hr( $tb - $mb - $kb );
$this->assertEqualsWithDelta( 1023.99902248, (float) str_replace( ',', '.', $hr ), 0.0001, 'The values should be equal' );
$hr = wp_convert_bytes_to_hr( $gb + $gb / 2 + $mb );
$this->assertEqualsWithDelta( 1.5009765625, (float) str_replace( ',', '.', $hr ), 0.0001, 'The values should be equal' );
$hr = wp_convert_bytes_to_hr( $gb - $mb - $kb );
$this->assertEqualsWithDelta( 1022.99902344, (float) str_replace( ',', '.', $hr ), 0.0001, 'The values should be equal' );
// Edge.
$this->assertEquals( '-1B', wp_convert_bytes_to_hr( -1 ) );
$this->assertEquals( '0B', wp_convert_bytes_to_hr( 0 ) );
}
/**
* @ticket 22960
*/
function test_get_attached_images() {
$post_id = self::factory()->post->create();
$attachment_id = self::factory()->attachment->create_object(
$this->img_name,
$post_id,
array(
'post_mime_type' => 'image/jpeg',
'post_type' => 'attachment',
)
);
$images = get_attached_media( 'image', $post_id );
$this->assertEquals( $images, array( $attachment_id => get_post( $attachment_id ) ) );
}
/**
* @ticket 22960
*/
function test_post_galleries_images() {
$ids1 = array();
$ids1_srcs = array();
foreach ( range( 1, 3 ) as $i ) {
$attachment_id = self::factory()->attachment->create_object(
"image$i.jpg",
0,
array(
'post_mime_type' => 'image/jpeg',
'post_type' => 'attachment',
)
);
$metadata = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
wp_update_attachment_metadata( $attachment_id, $metadata );
$ids1[] = $attachment_id;
$ids1_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
}
$ids2 = array();
$ids2_srcs = array();
foreach ( range( 4, 6 ) as $i ) {
$attachment_id = self::factory()->attachment->create_object(
"image$i.jpg",
0,
array(
'post_mime_type' => 'image/jpeg',
'post_type' => 'attachment',
)
);
$metadata = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
wp_update_attachment_metadata( $attachment_id, $metadata );
$ids2[] = $attachment_id;
$ids2_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
}
$ids1_joined = join( ',', $ids1 );
$ids2_joined = join( ',', $ids2 );
$blob = <<<BLOB
[gallery ids="$ids1_joined"]
[gallery ids="$ids2_joined"]
BLOB;
$post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
$srcs = get_post_galleries_images( $post_id );
$this->assertEquals( $srcs, array( $ids1_srcs, $ids2_srcs ) );
}
/**
* @ticket 39304
*/
function test_post_galleries_images_without_global_post() {
// Set up an unattached image.
$this->factory->attachment->create_object(
array(
'file' => 'test.jpg',
'post_parent' => 0,
'post_mime_type' => 'image/jpeg',
'post_type' => 'attachment',
)
);
$post_id = $this->factory->post->create(
array(
'post_content' => '[gallery]',
)
);
$galleries = get_post_galleries( $post_id, false );
$this->assertEmpty( $galleries[0]['src'] );
}
/**
* @ticket 39304
*/
function test_post_galleries_ignores_global_post() {
$global_post_id = $this->factory->post->create(
array(
'post_content' => 'Global Post',
)
);
$post_id = $this->factory->post->create(
array(
'post_content' => '[gallery]',
)
);
$this->factory->attachment->create_object(
array(
'file' => 'test.jpg',
'post_parent' => $post_id,
'post_mime_type' => 'image/jpeg',
'post_type' => 'attachment',
)
);
$expected_srcs = array(
'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg',
);
// Set the global $post context to the other post.
$GLOBALS['post'] = get_post( $global_post_id );
$galleries = get_post_galleries( $post_id, false );
$this->assertNotEmpty( $galleries[0]['src'] );
$this->assertSame( $galleries[0]['src'], $expected_srcs );
}
/**
* @ticket 39304
*/
function test_post_galleries_respects_id_attrs() {
$post_id = $this->factory->post->create(
array(
'post_content' => 'No gallery defined',
)
);
$post_id_two = $this->factory->post->create(
array(
'post_content' => "[gallery id='$post_id']",
)
);
$this->factory->attachment->create_object(
array(
'file' => 'test.jpg',
'post_parent' => $post_id,
'post_mime_type' => 'image/jpeg',
'post_type' => 'attachment',
)
);
$expected_srcs = array(
'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg',
);
$galleries = get_post_galleries( $post_id_two, false );
// Set the global $post context.
$GLOBALS['post'] = get_post( $post_id_two );
$galleries_with_global_context = get_post_galleries( $post_id_two, false );
// Check that the global post state doesn't affect the results.
$this->assertSame( $galleries, $galleries_with_global_context );
$this->assertNotEmpty( $galleries[0]['src'] );
$this->assertSame( $galleries[0]['src'], $expected_srcs );
}
/**
* @ticket 22960
*/
function test_post_gallery_images() {
$ids1 = array();
$ids1_srcs = array();
foreach ( range( 1, 3 ) as $i ) {
$attachment_id = self::factory()->attachment->create_object(
"image$i.jpg",
0,
array(
'post_mime_type' => 'image/jpeg',
'post_type' => 'attachment',
)
);
$metadata = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
wp_update_attachment_metadata( $attachment_id, $metadata );
$ids1[] = $attachment_id;
$ids1_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
}
$ids2 = array();
$ids2_srcs = array();
foreach ( range( 4, 6 ) as $i ) {
$attachment_id = self::factory()->attachment->create_object(
"image$i.jpg",
0,
array(
'post_mime_type' => 'image/jpeg',
'post_type' => 'attachment',
)
);
$metadata = array_merge( array( 'file' => "image$i.jpg" ), $this->img_meta );
wp_update_attachment_metadata( $attachment_id, $metadata );
$ids2[] = $attachment_id;
$ids2_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
}
$ids1_joined = join( ',', $ids1 );
$ids2_joined = join( ',', $ids2 );
$blob = <<<BLOB
[gallery ids="$ids1_joined"]
[gallery ids="$ids2_joined"]
BLOB;
$post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
$srcs = get_post_gallery_images( $post_id );
$this->assertEquals( $srcs, $ids1_srcs );
}
function test_get_media_embedded_in_content() {
$object = <<<OBJ
<object src="this" data="that">
<param name="value"/>
</object>
OBJ;
$embed = <<<EMBED
<embed src="something.mp4"/>
EMBED;
$iframe = <<<IFRAME
<iframe src="youtube.com" width="7000" />
IFRAME;
$audio = <<<AUDIO
<audio preload="none">
<source />
</audio>
AUDIO;
$video = <<<VIDEO
<video preload="none">
<source />
</video>
VIDEO;
$content = <<<CONTENT
This is a comment
$object
This is a comment
$embed
This is a comment
$iframe
This is a comment
$audio
This is a comment
$video
This is a comment
CONTENT;
$types = array( 'object', 'embed', 'iframe', 'audio', 'video' );
$contents = array_values( compact( $types ) );
$matches = get_media_embedded_in_content( $content, 'audio' );
$this->assertEquals( array( $audio ), $matches );
$matches = get_media_embedded_in_content( $content, 'video' );
$this->assertEquals( array( $video ), $matches );
$matches = get_media_embedded_in_content( $content, 'object' );
$this->assertEquals( array( $object ), $matches );
$matches = get_media_embedded_in_content( $content, 'embed' );
$this->assertEquals( array( $embed ), $matches );
$matches = get_media_embedded_in_content( $content, 'iframe' );
$this->assertEquals( array( $iframe ), $matches );
$matches = get_media_embedded_in_content( $content, $types );
$this->assertEquals( $contents, $matches );
}
function test_get_media_embedded_in_content_order() {
$audio = <<<AUDIO
<audio preload="none">
<source />
</audio>
AUDIO;
$video = <<<VIDEO
<video preload="none">
<source />
</video>
VIDEO;
$content = $audio . $video;
$matches1 = get_media_embedded_in_content( $content, array( 'audio', 'video' ) );
$this->assertEquals( array( $audio, $video ), $matches1 );
$reversed = $video . $audio;
$matches2 = get_media_embedded_in_content( $reversed, array( 'audio', 'video' ) );
$this->assertEquals( array( $video, $audio ), $matches2 );
}
/**
* @ticket 35367
*/
function test_wp_audio_shortcode_with_empty_params() {
$this->assertNull( wp_audio_shortcode( array() ) );
}
/**
* @ticket 35367
*/
function test_wp_audio_shortcode_with_bad_attr() {
$this->assertSame(
'<a class="wp-embedded-audio" href="https://example.com/foo.php">https://example.com/foo.php</a>',
wp_audio_shortcode(
array(
'src' => 'https://example.com/foo.php',
)
)
);
}
/**
* @ticket 35367
*/
function test_wp_audio_shortcode_attributes() {
$actual = wp_audio_shortcode(
array(
'src' => 'https://example.com/foo.mp3',
)
);
$this->assertStringContainsString( 'src="https://example.com/foo.mp3', $actual );
$this->assertStringNotContainsString( 'loop', $actual );
$this->assertStringNotContainsString( 'autoplay', $actual );
$this->assertStringContainsString( 'preload="none"', $actual );
$this->assertStringContainsString( 'class="wp-audio-shortcode"', $actual );
$this->assertStringContainsString( 'style="width: 100%;"', $actual );
$actual = wp_audio_shortcode(
array(
'src' => 'https://example.com/foo.mp3',
'loop' => true,
'autoplay' => true,
'preload' => true,
'class' => 'foobar',
'style' => 'padding:0;',
)
);
$this->assertStringContainsString( 'src="https://example.com/foo.mp3', $actual );
$this->assertStringContainsString( 'loop="1"', $actual );
$this->assertStringContainsString( 'autoplay="1"', $actual );
$this->assertStringContainsString( 'preload="1"', $actual );
$this->assertStringContainsString( 'class="foobar"', $actual );
$this->assertStringContainsString( 'style="padding:0;"', $actual );
}
/**
* Test [video] shortcode processing
*/
function test_video_shortcode_body() {
$width = 720;
$height = 480;
$w = empty( $GLOBALS['content_width'] ) ? 640 : $GLOBALS['content_width'];
if ( $width > $w ) {
$width = $w;
}
$post_id = get_post() ? get_the_ID() : 0;
$video = <<<VIDEO
[video width="$width" height="480" mp4="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4"]
<!-- WebM/VP8 for Firefox4, Opera, and Chrome -->
<source type="video/webm" src="myvideo.webm" />
<!-- Ogg/Vorbis for older Firefox and Opera versions -->
<source type="video/ogg" src="myvideo.ogv" />
<!-- Optional: Add subtitles for each language -->
<track kind="subtitles" src="subtitles.srt" srclang="en" />
<!-- Optional: Add chapters -->
<track kind="chapters" src="chapters.srt" srclang="en" />
[/video]
VIDEO;
$h = ceil( ( $height * $width ) / $width );
$content = apply_filters( 'the_content', $video );
$expected = '<div style="width: ' . $width . 'px;" class="wp-video">' .
"<!--[if lt IE 9]><script>document.createElement('video');</script><![endif]-->\n" .
'<video class="wp-video-shortcode" id="video-' . $post_id . '-1" width="' . $width . '" height="' . $h . '" preload="metadata" controls="controls">' .
'<source type="video/mp4" src="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4?_=1" />' .
'<!-- WebM/VP8 for Firefox4, Opera, and Chrome --><source type="video/webm" src="myvideo.webm" />' .
'<!-- Ogg/Vorbis for older Firefox and Opera versions --><source type="video/ogg" src="myvideo.ogv" />' .
'<!-- Optional: Add subtitles for each language --><track kind="subtitles" src="subtitles.srt" srclang="en" />' .
'<!-- Optional: Add chapters --><track kind="chapters" src="chapters.srt" srclang="en" />' .
'<a href="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4">' .
"http://domain.tld/wp-content/uploads/2013/12/xyz.mp4</a></video></div>\n";
$this->assertEquals( $expected, $content );
}
/**
* @ticket 35367
* @depends test_video_shortcode_body
*/
function test_wp_video_shortcode_with_empty_params() {
$this->assertNull( wp_video_shortcode( array() ) );
}
/**
* @ticket 35367
* @depends test_video_shortcode_body
*/
function test_wp_video_shortcode_with_bad_attr() {
$this->assertSame(
'<a class="wp-embedded-video" href="https://example.com/foo.php">https://example.com/foo.php</a>',
wp_video_shortcode(
array(
'src' => 'https://example.com/foo.php',
)
)
);
}
/**
* @ticket 35367
* @depends test_video_shortcode_body
*/
function test_wp_video_shortcode_attributes() {
$actual = wp_video_shortcode(
array(
'src' => 'https://example.com/foo.mp4',
)
);
$this->assertStringContainsString( 'src="https://example.com/foo.mp4', $actual );
$this->assertStringNotContainsString( 'loop', $actual );
$this->assertStringNotContainsString( 'autoplay', $actual );
$this->assertStringContainsString( 'preload="metadata"', $actual );
$this->assertStringContainsString( 'width="640"', $actual );
$this->assertStringContainsString( 'height="360"', $actual );
$this->assertStringContainsString( 'class="wp-video-shortcode"', $actual );
$actual = wp_video_shortcode(
array(
'src' => 'https://example.com/foo.mp4',
'poster' => 'https://example.com/foo.png',
'loop' => true,
'autoplay' => true,
'preload' => true,
'width' => 123,
'height' => 456,
'class' => 'foobar',
)
);
$this->assertStringContainsString( 'src="https://example.com/foo.mp4', $actual );
$this->assertStringContainsString( 'poster="https://example.com/foo.png', $actual );
$this->assertStringContainsString( 'loop="1"', $actual );
$this->assertStringContainsString( 'autoplay="1"', $actual );
$this->assertStringContainsString( 'preload="1"', $actual );
$this->assertStringContainsString( 'width="123"', $actual );
$this->assertStringContainsString( 'height="456"', $actual );
$this->assertStringContainsString( 'class="foobar"', $actual );
}
/**
* @ticket 40866
* @depends test_video_shortcode_body
*/
function test_wp_video_shortcode_youtube_remove_feature() {
$actual = wp_video_shortcode(
array(
'src' => 'https://www.youtube.com/watch?v=i_cVJgIz_Cs&feature=youtu.be',
)
);
$this->assertStringNotContainsString( 'feature=youtu.be', $actual );
}
/**
* @ticket 40866
* @depends test_video_shortcode_body
*/
function test_wp_video_shortcode_youtube_force_ssl() {
$actual = wp_video_shortcode(
array(
'src' => 'http://www.youtube.com/watch?v=i_cVJgIz_Cs',
)
);
$this->assertStringContainsString( 'src="https://www.youtube.com/watch?v=i_cVJgIz_Cs', $actual );
}
/**
* @ticket 40866
* @depends test_video_shortcode_body
*/
function test_wp_video_shortcode_vimeo_force_ssl_remove_query_args() {
$actual = wp_video_shortcode(
array(
'src' => 'http://vimeo.com/190372437?blah=meh',
)
);
$this->assertStringContainsString( 'src="https://vimeo.com/190372437', $actual );
$this->assertStringNotContainsString( 'blah=meh', $actual );
}
/**
* @ticket 40977
* @depends test_video_shortcode_body
*/
function test_wp_video_shortcode_vimeo_adds_loop() {
$actual = wp_video_shortcode(
array(
'src' => 'http://vimeo.com/190372437',
)
);
$this->assertStringContainsString( 'src="https://vimeo.com/190372437?loop=0', $actual );
}
/**
* @ticket 40977
* @depends test_video_shortcode_body
*/
function test_wp_video_shortcode_vimeo_force_adds_loop_true() {
$actual = wp_video_shortcode(
array(
'src' => 'http://vimeo.com/190372437',
'loop' => true,
)
);
$this->assertStringContainsString( 'src="https://vimeo.com/190372437?loop=1', $actual );
}