Tests_Media::test_oembed_explicit_media_link()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 0
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @group media
5
 * @group shortcode
6
 */
7
class Tests_Media extends WP_UnitTestCase
8
{
9
    protected static $large_id;
10
    protected static $_sizes;
11
12
    public static function wpSetUpBeforeClass( $factory ) 
0 ignored issues
show
Coding Style introduced by
The function name wpSetUpBeforeClass is in camel caps, but expected wp_set_up_before_class instead as per the coding standard.
Loading history...
13
    {
14
        self::$_sizes = wp_get_additional_image_sizes();
15
        $GLOBALS['_wp_additional_image_sizes'] = array();
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
16
17
        $filename = DIR_TESTDATA . '/images/test-image-large.png';
18
        self::$large_id = $factory->attachment->create_upload_object($filename);
19
    }
20
21
    public static function wpTearDownAfterClass() 
0 ignored issues
show
Coding Style introduced by
The function name wpTearDownAfterClass is in camel caps, but expected wp_tear_down_after_class instead as per the coding standard.
Loading history...
22
    {
23
        $GLOBALS['_wp_additional_image_sizes'] = self::$_sizes;
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
24
    }
25
26
    function setUp() 
0 ignored issues
show
Coding Style introduced by
The function name setUp is in camel caps, but expected set_up instead as per the coding standard.
Loading history...
27
    {
28
        parent::setUp();
29
        $this->caption = 'A simple caption.';
30
        $this->html_content = <<<CAP
31
A <strong class='classy'>bolded</strong> <em>caption</em> with a <a href="#">link</a>.
32
CAP;
33
        $this->img_content = <<<CAP
34
<img src="pic.jpg" id='anId' alt="pic"/>
35
CAP;
36
        $this->img_name = 'image.jpg';
37
        $this->img_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $this->img_name;
38
        $this->img_html = '<img src="' . $this->img_url . '"/>';
39
        $this->img_meta = array( 'width' => 100, 'height' => 100, 'sizes' => '' );
40
    }
41
42
    function test_img_caption_shortcode_added() 
43
    {
44
        global $shortcode_tags;
45
        $this->assertEquals('img_caption_shortcode', $shortcode_tags['caption']);
46
        $this->assertEquals('img_caption_shortcode', $shortcode_tags['wp_caption']);
47
    }
48
49
    function test_img_caption_shortcode_with_empty_params() 
50
    {
51
        $result = img_caption_shortcode(array());
52
        $this->assertNull($result);
53
    }
54
55
    function test_img_caption_shortcode_with_bad_attr() 
56
    {
57
        $result = img_caption_shortcode(array(), 'content');
58
        $this->assertEquals('content', 'content');
59
    }
60
61
    function test_img_caption_shortcode_with_old_format() 
62
    {
63
        $result = img_caption_shortcode(
64
            array( 'width' => 20, 'caption' => $this->caption )
65
        );
66
67
        $this->assertEquals(2, preg_match_all('/wp-caption/', $result, $_r));
68
        $this->assertEquals(1, preg_match_all('/alignnone/', $result, $_r));
69
        $this->assertEquals(1, preg_match_all("/{$this->caption}/", $result, $_r));
70
71
        if (current_theme_supports('html5', 'caption') ) {
72
               $this->assertEquals(1, preg_match_all("/width: 20/", $result, $_r));
73
        } else {
74
             $this->assertEquals(1, preg_match_all("/width: 30/", $result, $_r));
75
        }
76
    }
77
78
    function test_img_caption_shortcode_with_old_format_id_and_align() 
79
    {
80
        $result = img_caption_shortcode(
81
            array(
82
            'width' => 20,
83
            'caption' => $this->caption,
84
            'id' => '"myId',
85
            'align' => '&myAlignment'
86
            )
87
        );
88
        $this->assertEquals(1, preg_match_all('/wp-caption &amp;myAlignment/', $result, $_r));
89
        $this->assertEquals(1, preg_match_all('/id="myId"/', $result, $_r));
90
        $this->assertEquals(1, preg_match_all("/{$this->caption}/", $result, $_r));
91
    }
92
93
    function test_new_img_caption_shortcode_with_html_caption() 
94
    {
95
        $result = img_caption_shortcode(
96
            array( 'width' => 20, 'caption' => $this->html_content )
97
        );
98
        $our_preg = preg_quote($this->html_content);
99
100
        $this->assertEquals(1, preg_match_all("~{$our_preg}~", $result, $_r));
101
    }
102
103
    function test_new_img_caption_shortcode_new_format() 
104
    {
105
        $result = img_caption_shortcode(
106
            array( 'width' => 20 ),
107
            $this->img_content . $this->html_content
108
        );
109
        $img_preg = preg_quote($this->img_content);
110
        $content_preg = preg_quote($this->html_content);
111
112
        $this->assertEquals(1, preg_match_all("~{$img_preg}.*wp-caption-text~", $result, $_r));
113
        $this->assertEquals(1, preg_match_all("~wp-caption-text.*{$content_preg}~", $result, $_r));
114
    }
115
116 View Code Duplication
    function test_new_img_caption_shortcode_new_format_and_linked_image() 
117
    {
118
        $linked_image = "<a href='#'>{$this->img_content}</a>";
119
        $result = img_caption_shortcode(
120
            array( 'width' => 20 ),
121
            $linked_image . $this->html_content
122
        );
123
        $img_preg = preg_quote($linked_image);
124
        $content_preg = preg_quote($this->html_content);
125
126
        $this->assertEquals(1, preg_match_all("~{$img_preg}.*wp-caption-text~", $result, $_r));
127
        $this->assertEquals(1, preg_match_all("~wp-caption-text.*{$content_preg}~", $result, $_r));
128
    }
129
130 View Code Duplication
    function test_new_img_caption_shortcode_new_format_and_linked_image_with_newline() 
131
    {
132
        $linked_image = "<a href='#'>{$this->img_content}</a>";
133
        $result = img_caption_shortcode(
134
            array( 'width' => 20 ),
135
            $linked_image . "\n\n" . $this->html_content
136
        );
137
        $img_preg = preg_quote($linked_image);
138
        $content_preg = preg_quote($this->html_content);
139
140
        $this->assertEquals(1, preg_match_all("~{$img_preg}.*wp-caption-text~", $result, $_r));
141
        $this->assertEquals(1, preg_match_all("~wp-caption-text.*{$content_preg}~", $result, $_r));
142
    }
143
144
    function test_add_remove_oembed_provider() 
145
    {
146
        wp_oembed_add_provider('http://foo.bar/*', 'http://foo.bar/oembed');
147
        $this->assertTrue(wp_oembed_remove_provider('http://foo.bar/*'));
148
        $this->assertFalse(wp_oembed_remove_provider('http://foo.bar/*'));
149
    }
150
151
    /**
152
     * @ticket 23776
153
     */
154
    function test_autoembed_empty() 
155
    {
156
        global $wp_embed;
157
158
        $content = '';
159
160
        $result = $wp_embed->autoembed($content);
161
        $this->assertEquals($content, $result);
162
    }
163
164
    /**
165
     * @ticket 23776
166
     */
167
    function test_autoembed_no_paragraphs_around_urls() 
168
    {
169
        global $wp_embed;
170
171
        $content = <<<EOF
172
$ my command
173
First line.
174
175
http://example.com/1/
176
http://example.com/2/
177
Last line.
178
179
<pre>http://some.link/
180
http://some.other.link/</pre>
181
EOF;
182
183
        $result = $wp_embed->autoembed($content);
184
        $this->assertEquals($content, $result);
185
    }
186
187 View Code Duplication
    function data_autoembed() 
188
    {
189
        return array(
190
191
         // Should embed
192
         array(
193
        'https://w.org',
194
        '[embed]'
195
         ),
196
         array(
197
        'test
198
 https://w.org
199
test',
200
        'test
201
 [embed]
202
test'
203
         ),
204
         array(
205
        '<p class="test">https://w.org</p>',
206
        '<p class="test">[embed]</p>'
207
         ),
208
         array(
209
        '<p> https://w.org </p>',
210
        '<p> [embed] </p>'
211
         ),
212
         array(
213
        '<p>test
214
https://w.org
215
test</p>',
216
        '<p>test
217
[embed]
218
test</p>'
219
         ),
220
         array(
221
        '<p>https://w.org
222
</p>',
223
        '<p>[embed]
224
</p>'
225
         ),
226
227
         // Should NOT embed
228
         array(
229
        'test https://w.org</p>'
230
         ),
231
         array(
232
        '<span>https://w.org</a>'
233
         ),
234
         array(
235
        '<pre>https://w.org
236
</p>'
237
         ),
238
         array(
239
        '<a href="https://w.org">
240
https://w.org</a>'
241
         ),
242
        );
243
    }
244
245
    /**
246
     * @dataProvider data_autoembed
247
     */
248
    function test_autoembed( $content, $result = null ) 
249
    {
250
        $wp_embed = new Test_Autoembed;
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
251
252
        $this->assertEquals($wp_embed->autoembed($content), $result ? $result : $content);
253
    }
254
255
    function test_wp_prepare_attachment_for_js() 
256
    {
257
        // Attachment without media
258
        $id = wp_insert_attachment(
259
            array(
260
            'post_status' => 'publish',
261
            'post_title' => 'Prepare',
262
            'post_content_filtered' => 'Prepare',
263
            'post_type' => 'post'
264
            )
265
        );
266
        $post = get_post($id);
267
268
        $prepped = wp_prepare_attachment_for_js($post);
269
        $this->assertInternalType('array', $prepped);
270
        $this->assertEquals(0, $prepped['uploadedTo']);
271
        $this->assertEquals('', $prepped['mime']);
272
        $this->assertEquals('', $prepped['type']);
273
        $this->assertEquals('', $prepped['subtype']);
274
        // #21963, there will be a guid always, so there will be a URL
275
        $this->assertNotEquals('', $prepped['url']);
276
        $this->assertEquals(site_url('wp-includes/images/media/default.png'), $prepped['icon']);
277
278
        // Fake a mime
279
        $post->post_mime_type = 'image/jpeg';
280
        $prepped = wp_prepare_attachment_for_js($post);
281
        $this->assertEquals('image/jpeg', $prepped['mime']);
282
        $this->assertEquals('image', $prepped['type']);
283
        $this->assertEquals('jpeg', $prepped['subtype']);
284
285
        // Fake a mime without a slash. See #WP22532
286
        $post->post_mime_type = 'image';
287
        $prepped = wp_prepare_attachment_for_js($post);
288
        $this->assertEquals('image', $prepped['mime']);
289
        $this->assertEquals('image', $prepped['type']);
290
        $this->assertEquals('', $prepped['subtype']);
291
    }
292
293
    /**
294
     * @ticket 38965
295
     */
296
    function test_wp_prepare_attachment_for_js_without_image_sizes() 
297
    {
298
        // Create the attachement post.
299
        $id = wp_insert_attachment(
300
            array(
301
            'post_title' => 'Attachment Title',
302
            'post_type' => 'attachment',
303
            'post_parent' => 0,
304
            'post_mime_type' => 'image/jpeg',
305
            'guid' => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test-image.jpg',
306
            ) 
307
        );
308
309
        // Add attachment metadata without sizes.
310
        wp_update_attachment_metadata(
311
            $id, array(
312
            'width' => 50,
313
            'height' => 50,
314
            'file' => 'test-image.jpg',
315
            ) 
316
        );
317
318
        $prepped = wp_prepare_attachment_for_js(get_post($id));
319
320
        $this->assertTrue(isset($prepped['sizes']));
321
    }
322
323
    /**
324
     * @ticket 19067
325
     * @expectedDeprecated wp_convert_bytes_to_hr
326
     */
327
    function test_wp_convert_bytes_to_hr() 
328
    {
329
        $kb = 1024;
330
        $mb = $kb * 1024;
331
        $gb = $mb * 1024;
332
        $tb = $gb * 1024;
333
334
        // test if boundaries are correct
335
        $this->assertEquals('1TB', wp_convert_bytes_to_hr($tb));
336
        $this->assertEquals('1GB', wp_convert_bytes_to_hr($gb));
337
        $this->assertEquals('1MB', wp_convert_bytes_to_hr($mb));
338
        $this->assertEquals('1KB', wp_convert_bytes_to_hr($kb));
339
340
        $this->assertEquals('1 TB', size_format($tb));
341
        $this->assertEquals('1 GB', size_format($gb));
342
        $this->assertEquals('1 MB', size_format($mb));
343
        $this->assertEquals('1 KB', size_format($kb));
344
345
        // now some values around
346
        $hr = wp_convert_bytes_to_hr($tb + $tb / 2 + $mb);
347
        $this->assertEquals(1.50000095367, (float) str_replace(',', '.', $hr), 'The values should be equal', 0.0001);
348
349
        $hr = wp_convert_bytes_to_hr($tb - $mb - $kb);
350
        $this->assertEquals(1023.99902248, (float) str_replace(',', '.', $hr), 'The values should be equal', 0.0001);
351
352
        $hr = wp_convert_bytes_to_hr($gb + $gb / 2 + $mb);
353
        $this->assertEquals(1.5009765625, (float) str_replace(',', '.', $hr), 'The values should be equal', 0.0001);
354
355
        $hr = wp_convert_bytes_to_hr($gb - $mb - $kb);
356
        $this->assertEquals(1022.99902344, (float) str_replace(',', '.', $hr), 'The values should be equal', 0.0001);
357
358
        // edge
359
        $this->assertEquals('-1B', wp_convert_bytes_to_hr(-1));
360
        $this->assertEquals('0B', wp_convert_bytes_to_hr(0));
361
    }
362
363
    /**
364
     * @ticket 22960
365
     */
366 View Code Duplication
    function test_get_attached_images() 
367
    {
368
        $post_id = self::factory()->post->create();
369
        $attachment_id = self::factory()->attachment->create_object(
370
            $this->img_name, $post_id, array(
371
            'post_mime_type' => 'image/jpeg',
372
            'post_type' => 'attachment'
373
            ) 
374
        );
375
376
        $images = get_attached_media('image', $post_id);
377
        $this->assertEquals($images, array( $attachment_id => get_post($attachment_id) ));
378
    }
379
380
    /**
381
     * @ticket 22960
382
     */
383 View Code Duplication
    function test_post_galleries_images() 
384
    {
385
        $ids1 = array();
386
        $ids1_srcs = array();
387
        foreach ( range(1, 3) as $i ) {
388
            $attachment_id = self::factory()->attachment->create_object(
389
                "image$i.jpg", 0, array(
390
                'post_mime_type' => 'image/jpeg',
391
                'post_type' => 'attachment'
392
                ) 
393
            );
394
            $metadata = array_merge(array( "file" => "image$i.jpg" ), $this->img_meta);
395
            wp_update_attachment_metadata($attachment_id, $metadata);
396
            $ids1[] = $attachment_id;
397
            $ids1_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
398
        }
399
400
        $ids2 = array();
401
        $ids2_srcs = array();
402
        foreach ( range(4, 6) as $i ) {
403
            $attachment_id = self::factory()->attachment->create_object(
404
                "image$i.jpg", 0, array(
405
                'post_mime_type' => 'image/jpeg',
406
                'post_type' => 'attachment'
407
                ) 
408
            );
409
            $metadata = array_merge(array( "file" => "image$i.jpg" ), $this->img_meta);
410
            wp_update_attachment_metadata($attachment_id, $metadata);
411
            $ids2[] = $attachment_id;
412
            $ids2_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
413
        }
414
415
        $ids1_joined = join(',', $ids1);
416
        $ids2_joined = join(',', $ids2);
417
418
        $blob =<<<BLOB
0 ignored issues
show
introduced by
Expected 1 space after "="; 0 found
Loading history...
419
[gallery ids="$ids1_joined"]
420
421
[gallery ids="$ids2_joined"]
422
BLOB;
423
        $post_id = self::factory()->post->create(array( 'post_content' => $blob ));
424
        $srcs = get_post_galleries_images($post_id);
425
        $this->assertEquals($srcs, array( $ids1_srcs, $ids2_srcs ));
426
    }
427
428
    /**
429
     * @ticket 22960
430
     */
431 View Code Duplication
    function test_post_gallery_images() 
432
    {
433
        $ids1 = array();
434
        $ids1_srcs = array();
435
        foreach ( range(1, 3) as $i ) {
436
            $attachment_id = self::factory()->attachment->create_object(
437
                "image$i.jpg", 0, array(
438
                'post_mime_type' => 'image/jpeg',
439
                'post_type' => 'attachment'
440
                ) 
441
            );
442
            $metadata = array_merge(array( "file" => "image$i.jpg" ), $this->img_meta);
443
            wp_update_attachment_metadata($attachment_id, $metadata);
444
            $ids1[] = $attachment_id;
445
            $ids1_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
446
        }
447
448
        $ids2 = array();
449
        $ids2_srcs = array();
450
        foreach ( range(4, 6) as $i ) {
451
            $attachment_id = self::factory()->attachment->create_object(
452
                "image$i.jpg", 0, array(
453
                'post_mime_type' => 'image/jpeg',
454
                'post_type' => 'attachment'
455
                ) 
456
            );
457
            $metadata = array_merge(array( "file" => "image$i.jpg" ), $this->img_meta);
458
            wp_update_attachment_metadata($attachment_id, $metadata);
459
            $ids2[] = $attachment_id;
460
            $ids2_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
461
        }
462
463
        $ids1_joined = join(',', $ids1);
464
        $ids2_joined = join(',', $ids2);
465
466
        $blob =<<<BLOB
0 ignored issues
show
introduced by
Expected 1 space after "="; 0 found
Loading history...
467
[gallery ids="$ids1_joined"]
468
469
[gallery ids="$ids2_joined"]
470
BLOB;
471
        $post_id = self::factory()->post->create(array( 'post_content' => $blob ));
472
        $srcs = get_post_gallery_images($post_id);
473
        $this->assertEquals($srcs, $ids1_srcs);
474
    }
475
476
    function test_get_media_embedded_in_content() 
477
    {
478
        $object =<<<OBJ
0 ignored issues
show
introduced by
Expected 1 space after "="; 0 found
Loading history...
479
<object src="this" data="that">
480
	<param name="value"/>
481
</object>
482
OBJ;
483
        $embed =<<<EMBED
0 ignored issues
show
introduced by
Expected 1 space after "="; 0 found
Loading history...
484
<embed src="something.mp4"/>
485
EMBED;
486
        $iframe =<<<IFRAME
0 ignored issues
show
introduced by
Expected 1 space after "="; 0 found
Loading history...
487
<iframe src="youtube.com" width="7000" />
488
IFRAME;
489
        $audio =<<<AUDIO
0 ignored issues
show
introduced by
Expected 1 space after "="; 0 found
Loading history...
490
<audio preload="none">
491
	<source />
492
</audio>
493
AUDIO;
494
        $video =<<<VIDEO
0 ignored issues
show
introduced by
Expected 1 space after "="; 0 found
Loading history...
495
<video preload="none">
496
	<source />
497
</video>
498
VIDEO;
499
500
        $content =<<<CONTENT
0 ignored issues
show
introduced by
Expected 1 space after "="; 0 found
Loading history...
501
This is a comment
502
$object
503
504
This is a comment
505
$embed
506
507
This is a comment
508
$iframe
509
510
This is a comment
511
$audio
512
513
This is a comment
514
$video
515
516
This is a comment
517
CONTENT;
518
519
        $types = array( 'object', 'embed', 'iframe', 'audio', 'video' );
520
        $contents = array_values(compact($types));
521
522
        $matches = get_media_embedded_in_content($content, 'audio');
523
        $this->assertEquals(array( $audio ), $matches);
524
525
        $matches = get_media_embedded_in_content($content, 'video');
526
        $this->assertEquals(array( $video ), $matches);
527
528
        $matches = get_media_embedded_in_content($content, 'object');
529
        $this->assertEquals(array( $object ), $matches);
530
531
        $matches = get_media_embedded_in_content($content, 'embed');
532
        $this->assertEquals(array( $embed ), $matches);
533
534
        $matches = get_media_embedded_in_content($content, 'iframe');
535
        $this->assertEquals(array( $iframe ), $matches);
536
537
        $matches = get_media_embedded_in_content($content, $types);
538
        $this->assertEquals($contents, $matches);
539
    }
540
541
    function test_get_media_embedded_in_content_order() 
542
    {
543
        $audio =<<<AUDIO
0 ignored issues
show
introduced by
Expected 1 space after "="; 0 found
Loading history...
544
<audio preload="none">
545
	<source />
546
</audio>
547
AUDIO;
548
        $video =<<<VIDEO
0 ignored issues
show
introduced by
Expected 1 space after "="; 0 found
Loading history...
549
<video preload="none">
550
	<source />
551
</video>
552
VIDEO;
553
        $content = $audio . $video;
554
555
        $matches1 = get_media_embedded_in_content($content, array( 'audio', 'video' ));
556
        $this->assertEquals(array( $audio, $video ), $matches1);
557
558
        $reversed = $video . $audio;
559
        $matches2 = get_media_embedded_in_content($reversed, array( 'audio', 'video' ));
560
        $this->assertEquals(array( $video, $audio ), $matches2);
561
    }
562
563
    /**
564
     * @ticket 35367
565
     */
566
    function test_wp_audio_shortcode_with_empty_params() 
567
    {
568
        $this->assertNull(wp_audio_shortcode(array()));
569
    }
570
571
    /**
572
     * @ticket 35367
573
     */
574
    function test_wp_audio_shortcode_with_bad_attr() 
575
    {
576
        $this->assertSame(
577
            '<a class="wp-embedded-audio" href="https://example.com/foo.php">https://example.com/foo.php</a>',
578
            wp_audio_shortcode(
579
                array(
580
                'src' => 'https://example.com/foo.php',
581
                ) 
582
            )
583
        );
584
    }
585
586
    /**
587
     * @ticket 35367
588
     */
589
    function test_wp_audio_shortcode_attributes() 
590
    {
591
        $actual = wp_audio_shortcode(
592
            array(
593
            'src' => 'https://example.com/foo.mp3',
594
            ) 
595
        );
596
597
        $this->assertContains('src="https://example.com/foo.mp3', $actual);
598
        $this->assertNotContains('loop', $actual);
599
        $this->assertNotContains('autoplay', $actual);
600
        $this->assertContains('preload="none"', $actual);
601
        $this->assertContains('class="wp-audio-shortcode"', $actual);
602
        $this->assertContains('style="width: 100%;"', $actual);
603
604
        $actual = wp_audio_shortcode(
605
            array(
606
            'src'      => 'https://example.com/foo.mp3',
607
            'loop'     => true,
608
            'autoplay' => true,
609
            'preload'  => true,
610
            'class'    => 'foobar',
611
            'style'    => 'padding:0;',
612
            ) 
613
        );
614
615
        $this->assertContains('src="https://example.com/foo.mp3', $actual);
616
        $this->assertContains('loop="1"', $actual);
617
        $this->assertContains('autoplay="1"', $actual);
618
        $this->assertContains('preload="1"', $actual);
619
        $this->assertContains('class="foobar"', $actual);
620
        $this->assertContains('style="padding:0;"', $actual);
621
    }
622
623
    /**
624
     * @ticket  35367
625
     * @depends test_video_shortcode_body
626
     */
627
    function test_wp_video_shortcode_with_empty_params() 
628
    {
629
        $this->assertNull(wp_video_shortcode(array()));
630
    }
631
632
    /**
633
     * @ticket  35367
634
     * @depends test_video_shortcode_body
635
     */
636
    function test_wp_video_shortcode_with_bad_attr() 
637
    {
638
        $this->assertSame(
639
            '<a class="wp-embedded-video" href="https://example.com/foo.php">https://example.com/foo.php</a>',
640
            wp_video_shortcode(
641
                array(
642
                'src' => 'https://example.com/foo.php',
643
                ) 
644
            )
645
        );
646
    }
647
648
    /**
649
     * @ticket  35367
650
     * @depends test_video_shortcode_body
651
     */
652
    function test_wp_video_shortcode_attributes() 
653
    {
654
        $actual = wp_video_shortcode(
655
            array(
656
            'src' => 'https://example.com/foo.mp4',
657
            ) 
658
        );
659
660
        $this->assertContains('src="https://example.com/foo.mp4', $actual);
661
        $this->assertNotContains('loop', $actual);
662
        $this->assertNotContains('autoplay', $actual);
663
        $this->assertContains('preload="metadata"', $actual);
664
        $this->assertContains('width="640"', $actual);
665
        $this->assertContains('height="360"', $actual);
666
        $this->assertContains('class="wp-video-shortcode"', $actual);
667
668
        $actual = wp_video_shortcode(
669
            array(
670
            'src'      => 'https://example.com/foo.mp4',
671
            'poster'   => 'https://example.com/foo.png',
672
            'loop'     => true,
673
            'autoplay' => true,
674
            'preload'  => true,
675
            'width'    => 123,
676
            'height'   => 456,
677
            'class'    => 'foobar',
678
            ) 
679
        );
680
681
        $this->assertContains('src="https://example.com/foo.mp4', $actual);
682
        $this->assertContains('poster="https://example.com/foo.png', $actual);
683
        $this->assertContains('loop="1"', $actual);
684
        $this->assertContains('autoplay="1"', $actual);
685
        $this->assertContains('preload="1"', $actual);
686
        $this->assertContains('width="123"', $actual);
687
        $this->assertContains('height="456"', $actual);
688
        $this->assertContains('class="foobar"', $actual);
689
    }
690
691
    /**
692
     * Test [video] shortcode processing
693
     */
694
    function test_video_shortcode_body() 
695
    {
696
        $width = 720;
697
        $height = 480;
698
699
        $w = empty($GLOBALS['content_width']) ? 640 : $GLOBALS['content_width'];
700
        if ($width > $w ) {
701
            $width = $w;
702
        }
703
704
        $post_id = get_post() ? get_the_ID() : 0;
705
706
        $video =<<<VIDEO
0 ignored issues
show
introduced by
Expected 1 space after "="; 0 found
Loading history...
707
[video width="$width" height="480" mp4="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4"]
708
<!-- WebM/VP8 for Firefox4, Opera, and Chrome -->
709
<source type="video/webm" src="myvideo.webm" />
710
<!-- Ogg/Vorbis for older Firefox and Opera versions -->
711
<source type="video/ogg" src="myvideo.ogv" />
712
<!-- Optional: Add subtitles for each language -->
713
<track kind="subtitles" src="subtitles.srt" srclang="en" />
714
<!-- Optional: Add chapters -->
715
<track kind="chapters" src="chapters.srt" srclang="en" />
716
[/video]
717
VIDEO;
718
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
719
720
        $h = ceil(( $height * $width ) / $width);
721
722
        $content = apply_filters('the_content', $video);
723
724
        $expected = '<div style="width: ' . $width . 'px;" class="wp-video">' .
725
         "<!--[if lt IE 9]><script>document.createElement('video');</script><![endif]-->\n" .
726
         '<video class="wp-video-shortcode" id="video-' . $post_id . '-1" width="' . $width . '" height="' . $h . '" preload="metadata" controls="controls">' .
727
         '<source type="video/mp4" src="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4?_=1" />' .
728
         '<!-- WebM/VP8 for Firefox4, Opera, and Chrome --><source type="video/webm" src="myvideo.webm" />' .
729
         '<!-- Ogg/Vorbis for older Firefox and Opera versions --><source type="video/ogg" src="myvideo.ogv" />' .
730
         '<!-- Optional: Add subtitles for each language --><track kind="subtitles" src="subtitles.srt" srclang="en" />' .
731
         '<!-- Optional: Add chapters --><track kind="chapters" src="chapters.srt" srclang="en" />' .
732
         '<a href="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4">' .
733
         "http://domain.tld/wp-content/uploads/2013/12/xyz.mp4</a></video></div>\n";
734
735
        $this->assertEquals($expected, $content);
736
    }
737
738
    /**
739
     * @ticket 26768
740
     */
741
    function test_add_image_size() 
742
    {
743
        $_wp_additional_image_sizes = wp_get_additional_image_sizes();
744
745
        remove_image_size('test-size');
746
747
        $this->assertArrayNotHasKey('test-size', $_wp_additional_image_sizes);
748
        add_image_size('test-size', 200, 600);
749
750
        $sizes = wp_get_additional_image_sizes();
751
752
        // Clean up
753
        remove_image_size('test-size');
754
755
        $this->assertArrayHasKey('test-size', $sizes);
756
        $this->assertEquals(200, $sizes['test-size']['width']);
757
        $this->assertEquals(600, $sizes['test-size']['height']);
758
    }
759
760
    /**
761
     * @ticket 26768
762
     */
763
    function test_remove_image_size() 
764
    {
765
        add_image_size('test-size', 200, 600);
766
        $this->assertTrue(has_image_size('test-size'));
767
        remove_image_size('test-size');
768
        $this->assertFalse(has_image_size('test-size'));
769
    }
770
771
    /**
772
     * @ticket 26951
773
     */
774
    function test_has_image_size() 
775
    {
776
        add_image_size('test-size', 200, 600);
777
        $this->assertTrue(has_image_size('test-size'));
778
779
        // Clean up
780
        remove_image_size('test-size');
781
    }
782
783
    /**
784
     * @ticket 30346
785
     */
786 View Code Duplication
    function test_attachment_url_to_postid() 
787
    {
788
        $image_path = '2014/11/' . $this->img_name;
789
        $attachment_id = self::factory()->attachment->create_object(
790
            $image_path, 0, array(
0 ignored issues
show
Documentation introduced by
$image_path is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
791
            'post_mime_type' => 'image/jpeg',
792
            'post_type'      => 'attachment',
793
            ) 
794
        );
795
796
        $image_url  = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_path;
797
        $this->assertEquals($attachment_id, attachment_url_to_postid($image_url));
798
    }
799
800 View Code Duplication
    function test_attachment_url_to_postid_schemes() 
801
    {
802
        $image_path = '2014/11/' . $this->img_name;
803
        $attachment_id = self::factory()->attachment->create_object(
804
            $image_path, 0, array(
0 ignored issues
show
Documentation introduced by
$image_path is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
805
            'post_mime_type' => 'image/jpeg',
806
            'post_type'      => 'attachment',
807
            ) 
808
        );
809
810
        /**
811
         * @ticket 33109 Testing protocols not matching
812
         */
813
        $image_url  = 'https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_path;
814
        $this->assertEquals($attachment_id, attachment_url_to_postid($image_url));
815
    }
816
817
    function test_attachment_url_to_postid_filtered() 
818
    {
819
        $image_path = '2014/11/' . $this->img_name;
820
        $attachment_id = self::factory()->attachment->create_object(
821
            $image_path, 0, array(
0 ignored issues
show
Documentation introduced by
$image_path is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
822
            'post_mime_type' => 'image/jpeg',
823
            'post_type'      => 'attachment',
824
            ) 
825
        );
826
827
        add_filter('upload_dir', array( $this, '_upload_dir' ));
828
        $image_url = 'http://192.168.1.20.com/wp-content/uploads/' . $image_path;
829
        $this->assertEquals($attachment_id, attachment_url_to_postid($image_url));
830
        remove_filter('upload_dir', array( $this, '_upload_dir' ));
831
    }
832
833
    function _upload_dir( $dir ) 
834
    {
835
        $dir['baseurl'] = 'http://192.168.1.20.com/wp-content/uploads';
836
        return $dir;
837
    }
838
839
    /**
840
     * @ticket 31044
841
     */
842
    function test_attachment_url_to_postid_with_empty_url() 
843
    {
844
        $post_id = attachment_url_to_postid('');
845
        $this->assertInternalType('int', $post_id);
846
        $this->assertEquals(0, $post_id);
847
    }
848
849
    /**
850
     * @ticket 22768
851
     */
852 View Code Duplication
    public function test_media_handle_upload_sets_post_excerpt() 
853
    {
854
        $iptc_file = DIR_TESTDATA . '/images/test-image-iptc.jpg';
855
856
        // Make a copy of this file as it gets moved during the file upload
857
        $tmp_name = wp_tempnam($iptc_file);
858
859
        copy($iptc_file, $tmp_name);
860
861
        $_FILES['upload'] = array(
862
         'tmp_name' => $tmp_name,
863
         'name'     => 'test-image-iptc.jpg',
864
         'type'     => 'image/jpeg',
865
         'error'    => 0,
866
         'size'     => filesize($iptc_file)
867
        );
868
869
        $post_id = media_handle_upload('upload', 0, array(), array( 'action' => 'test_iptc_upload', 'test_form' => false ));
870
871
        unset($_FILES['upload']);
872
873
        $post = get_post($post_id);
874
875
        // Clean up.
876
        wp_delete_attachment($post_id);
877
878
        $this->assertEquals('This is a comment. / Это комментарий. / Βλέπετε ένα σχόλιο.', $post->post_excerpt);
879
    }
880
881
    /**
882
     * @ticket 37989
883
     */
884 View Code Duplication
    public function test_media_handle_upload_expected_titles() 
885
    {
886
        $test_file = DIR_TESTDATA . '/images/test-image.jpg';
887
888
        // Make a copy of this file as it gets moved during the file upload
889
        $tmp_name = wp_tempnam($test_file);
890
891
        copy($test_file, $tmp_name);
892
893
        $_FILES['upload'] = array(
894
         'tmp_name' => $tmp_name,
895
         'name'     => 'This is a test.jpg',
896
         'type'     => 'image/jpeg',
897
         'error'    => 0,
898
         'size'     => filesize($test_file),
899
        );
900
901
        $post_id = media_handle_upload('upload', 0, array(), array( 'action' => 'test_upload_titles', 'test_form' => false ));
902
903
        unset($_FILES['upload']);
904
905
        $post = get_post($post_id);
906
907
        // Clean up.
908
        wp_delete_attachment($post_id);
909
910
        $this->assertEquals('This is a test', $post->post_title);
911
    }
912
913
    /**
914
     * @ticket 33016
915
     */
916
    function test_multiline_cdata() 
917
    {
918
        global $wp_embed;
919
920
        $content = <<<EOF
921
<script>// <![CDATA[
922
_my_function('data');
923
// ]]>
924
</script>
925
EOF;
926
927
        $result = $wp_embed->autoembed($content);
928
        $this->assertEquals($content, $result);
929
    }
930
931
    /**
932
     * @ticket 33016
933
     */
934
    function test_multiline_comment() 
935
    {
936
        global $wp_embed;
937
938
        $content = <<<EOF
939
<script><!--
940
my_function();
941
// --> </script>
942
EOF;
943
944
        $result = $wp_embed->autoembed($content);
945
        $this->assertEquals($content, $result);
946
    }
947
948
949
    /**
950
     * @ticket 33016
951
     */
952
    function test_multiline_comment_with_embeds() 
953
    {
954
        $content = <<<EOF
955
Start.
956
[embed]http://www.youtube.com/embed/TEST01YRHA0[/embed]
957
<script><!--
958
my_function();
959
// --> </script>
960
http://www.youtube.com/embed/TEST02YRHA0
961
[embed]http://www.example.com/embed/TEST03YRHA0[/embed]
962
http://www.example.com/embed/TEST04YRHA0
963
Stop.
964
EOF;
965
966
        $expected = <<<EOF
967
<p>Start.<br />
968
https://youtube.com/watch?v=TEST01YRHA0<br />
969
<script><!--
970
my_function();
971
// --> </script><br />
972
https://youtube.com/watch?v=TEST02YRHA0<br />
973
<a href="http://www.example.com/embed/TEST03YRHA0">http://www.example.com/embed/TEST03YRHA0</a><br />
974
http://www.example.com/embed/TEST04YRHA0<br />
975
Stop.</p>
976
977
EOF;
978
979
        $result = apply_filters('the_content', $content);
980
        $this->assertEquals($expected, $result);
981
    }
982
983
    /**
984
     * @ticket 33016
985
     */
986
    function filter_wp_embed_shortcode_custom( $content, $url ) 
987
    {
988
        if ('https://www.example.com/?video=1' == $url ) {
989
            $content = '@embed URL was replaced@';
990
        }
991
        return $content;
992
    }
993
994
    /**
995
     * @ticket 33016
996
     */
997
    function test_oembed_explicit_media_link() 
998
    {
999
        global $wp_embed;
1000
        add_filter('embed_maybe_make_link', array( $this, 'filter_wp_embed_shortcode_custom' ), 10, 2);
1001
1002
        $content = <<<EOF
1003
https://www.example.com/?video=1
1004
EOF;
1005
1006
        $expected = <<<EOF
1007
@embed URL was replaced@
1008
EOF;
1009
1010
        $result = $wp_embed->autoembed($content);
1011
        $this->assertEquals($expected, $result);
1012
1013
        $content = <<<EOF
1014
<a href="https://www.example.com/?video=1">https://www.example.com/?video=1</a>
1015
<script>// <![CDATA[
1016
_my_function('data');
1017
myvar = 'Hello world
1018
https://www.example.com/?video=1
1019
do not break this';
1020
// ]]>
1021
</script>
1022
EOF;
1023
1024
        $result = $wp_embed->autoembed($content);
1025
        $this->assertEquals($content, $result);
1026
1027
        remove_filter('embed_maybe_make_link', array( $this, 'filter_wp_embed_shortcode_custom' ), 10);
1028
    }
1029
1030
    /**
1031
     * Tests the default output of `wp_get_attachment_image()`.
1032
  *
1033
     * @ticket 34635
1034
     */
1035
    function test_wp_get_attachment_image_defaults() 
1036
    {
1037
        $image = image_downsize(self::$large_id, 'thumbnail');
1038
        $expected = sprintf('<img width="%1$d" height="%2$d" src="%3$s" class="attachment-thumbnail size-thumbnail" alt="" />', $image[1], $image[2], $image[0]);
1039
1040
        $this->assertEquals($expected, wp_get_attachment_image(self::$large_id));
1041
    }
1042
1043
    /**
1044
     * Test that `wp_get_attachment_image()` returns a proper alt value.
1045
  *
1046
     * @ticket 34635
1047
     */
1048
    function test_wp_get_attachment_image_with_alt() 
1049
    {
1050
        // Add test alt metadata.
1051
        update_post_meta(self::$large_id, '_wp_attachment_image_alt', 'Some very clever alt text', true);
1052
1053
        $image = image_downsize(self::$large_id, 'thumbnail');
1054
        $expected = sprintf('<img width="%1$d" height="%2$d" src="%3$s" class="attachment-thumbnail size-thumbnail" alt="Some very clever alt text" />', $image[1], $image[2], $image[0]);
1055
1056
        $this->assertEquals($expected, wp_get_attachment_image(self::$large_id));
1057
1058
        // Cleanup.
1059
        update_post_meta(self::$large_id, '_wp_attachment_image_alt', '', true);
1060
    }
1061
1062
    /**
1063
     * @ticket 33878
1064
     */
1065 View Code Duplication
    function test_wp_get_attachment_image_url() 
1066
    {
1067
        $this->assertFalse(wp_get_attachment_image_url(0));
1068
1069
        $post_id = self::factory()->post->create();
1070
        $attachment_id = self::factory()->attachment->create_object(
1071
            $this->img_name, $post_id, array(
1072
            'post_mime_type' => 'image/jpeg',
1073
            'post_type' => 'attachment',
1074
            ) 
1075
        );
1076
1077
        $image = wp_get_attachment_image_src($attachment_id, 'thumbnail', false);
1078
1079
        $this->assertEquals($image[0], wp_get_attachment_image_url($attachment_id));
1080
    }
1081
1082
    /**
1083
     * @ticket 12235
1084
     */
1085 View Code Duplication
    function test_wp_get_attachment_caption() 
1086
    {
1087
        $this->assertFalse(wp_get_attachment_caption(0));
1088
1089
        $caption = 'This is a caption.';
1090
1091
        $post_id = self::factory()->post->create();
1092
        $attachment_id = self::factory()->attachment->create_object(
1093
            $this->img_name, $post_id, array(
1094
            'post_mime_type' => 'image/jpeg',
1095
            'post_type'      => 'attachment',
1096
            'post_excerpt'   => $caption,
1097
            ) 
1098
        );
1099
1100
        $this->assertFalse(wp_get_attachment_caption($post_id));
1101
1102
        $this->assertEquals($caption, wp_get_attachment_caption($attachment_id));
1103
    }
1104
1105
    /**
1106
     * @ticket 12235
1107
     */
1108 View Code Duplication
    function test_wp_get_attachment_caption_empty() 
1109
    {
1110
        $post_id = self::factory()->post->create();
1111
        $attachment_id = self::factory()->attachment->create_object(
1112
            $this->img_name, $post_id, array(
1113
            'post_mime_type' => 'image/jpeg',
1114
            'post_type'      => 'attachment',
1115
            'post_excerpt'   => '',
1116
            ) 
1117
        );
1118
1119
        $this->assertEquals('', wp_get_attachment_caption($attachment_id));
1120
    }
1121
1122
    /**
1123
     * Helper function to get image size array from size "name"
1124
     */
1125
    function _get_image_size_array_from_meta( $image_meta, $size_name ) 
1126
    {
1127
        $array = false;
1128
1129
        if (is_array($image_meta) ) {
1130
            if ('full' === $size_name && isset($image_meta['width']) && isset($image_meta['height']) ) {
1131
                $array = array( $image_meta['width'], $image_meta['height'] );
1132
            } elseif (isset($image_meta['sizes'][ $size_name ]['width']) && isset($image_meta['sizes'][ $size_name ]['height']) ) {
1133
                $array = array( $image_meta['sizes'][ $size_name ]['width'], $image_meta['sizes'][ $size_name ]['height'] );
1134
            }
1135
        }
1136
1137
        return $array;
1138
    }
1139
1140
    /**
1141
     * Helper function to move the src image to the first position in the expected srcset string.
1142
     */
1143
    function _src_first( $srcset, $src_url, $src_width ) 
1144
    {
1145
        $src_string = $src_url . ' ' . $src_width . 'w';
1146
        $src_not_first = ', ' . $src_string;
1147
1148
        if (strpos($srcset, $src_not_first) ) {
1149
            $srcset = str_replace($src_not_first, '', $srcset);
1150
            $srcset = $src_string . ', ' . $srcset;
1151
        }
1152
1153
        return $srcset;
1154
    }
1155
1156
    /**
1157
     * @ticket 33641
1158
     */
1159
    function test_wp_calculate_image_srcset() 
1160
    {
1161
        $_wp_additional_image_sizes = wp_get_additional_image_sizes();
1162
1163
        $year_month = date('Y/m');
1164
        $image_meta = wp_get_attachment_metadata(self::$large_id);
1165
        $uploads_dir_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/';
1166
1167
        // Set up test cases for all expected size names.
1168
        $intermediates = array( 'medium', 'medium_large', 'large', 'full' );
1169
1170
        // Add any soft crop intermediate sizes.
1171 View Code Duplication
        foreach ( $_wp_additional_image_sizes as $name => $additional_size ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1172
            if (! $_wp_additional_image_sizes[$name]['crop'] || 0 === $_wp_additional_image_sizes[$name]['height'] ) {
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
introduced by
Expected 1 space before "!"; 0 found
Loading history...
1173
                $intermediates[] = $name;
1174
            }
1175
        }
1176
1177
        $expected = '';
1178
1179 View Code Duplication
        foreach ( $image_meta['sizes'] as $name => $size ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1180
            // Whitelist the sizes that should be included so we pick up 'medium_large' in 4.4.
1181
            if (in_array($name, $intermediates) ) {
1182
                $expected .= $uploads_dir_url . $year_month . '/' . $size['file'] . ' ' . $size['width'] . 'w, ';
1183
            }
1184
        }
1185
1186
        // Add the full size width at the end.
1187
        $expected .= $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] .'w';
1188
1189 View Code Duplication
        foreach ( $intermediates as $int ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1190
            $image_url = wp_get_attachment_image_url(self::$large_id, $int);
1191
            $size_array = $this->_get_image_size_array_from_meta($image_meta, $int);
1192
            $expected_srcset = $this->_src_first($expected, $image_url, $size_array[0]);
1193
            $this->assertSame($expected_srcset, wp_calculate_image_srcset($size_array, $image_url, $image_meta));
1194
        }
1195
    }
1196
1197
    /**
1198
     * @ticket 33641
1199
     */
1200
    function test_wp_calculate_image_srcset_no_date_uploads() 
1201
    {
1202
        $_wp_additional_image_sizes = wp_get_additional_image_sizes();
1203
1204
        // Disable date organized uploads
1205
        add_filter('upload_dir', '_upload_dir_no_subdir');
1206
1207
        // Make an image.
1208
        $filename = DIR_TESTDATA . '/images/test-image-large.png';
1209
        $id = self::factory()->attachment->create_upload_object($filename);
1210
1211
        $image_meta = wp_get_attachment_metadata($id);
1212
        $uploads_dir_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/';
1213
1214
        // Set up test cases for all expected size names.
1215
        $intermediates = array( 'medium', 'medium_large', 'large', 'full' );
1216
1217 View Code Duplication
        foreach ( $_wp_additional_image_sizes as $name => $additional_size ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1218
            if (! $_wp_additional_image_sizes[$name]['crop'] || 0 === $_wp_additional_image_sizes[$name]['height'] ) {
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
introduced by
Expected 1 space before "!"; 0 found
Loading history...
1219
                $intermediates[] = $name;
1220
            }
1221
        }
1222
1223
        $expected = '';
1224
1225 View Code Duplication
        foreach ( $image_meta['sizes'] as $name => $size ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1226
            // Whitelist the sizes that should be included so we pick up 'medium_large' in 4.4.
1227
            if (in_array($name, $intermediates) ) {
1228
                $expected .= $uploads_dir_url . $size['file'] . ' ' . $size['width'] . 'w, ';
1229
            }
1230
        }
1231
1232
        // Add the full size width at the end.
1233
        $expected .= $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] .'w';
1234
1235 View Code Duplication
        foreach ( $intermediates as $int ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1236
            $size_array = $this->_get_image_size_array_from_meta($image_meta, $int);
1237
            $image_url = wp_get_attachment_image_url($id, $int);
1238
            $expected_srcset = $this->_src_first($expected, $image_url, $size_array[0]);
1239
            $this->assertSame($expected_srcset, wp_calculate_image_srcset($size_array, $image_url, $image_meta));
1240
        }
1241
1242
        // Remove the attachment
1243
        wp_delete_attachment($id);
1244
        remove_filter('upload_dir', '_upload_dir_no_subdir');
1245
    }
1246
1247
    /**
1248
     * @ticket 33641
1249
     */
1250
    function test_wp_calculate_image_srcset_with_edits() 
1251
    {
1252
        // For this test we're going to mock metadata changes from an edit.
1253
        // Start by getting the attachment metadata.
1254
        $image_meta = wp_get_attachment_metadata(self::$large_id);
1255
        $image_url = wp_get_attachment_image_url(self::$large_id, 'medium');
1256
        $size_array = $this->_get_image_size_array_from_meta($image_meta, 'medium');
1257
1258
        // Copy hash generation method used in wp_save_image().
1259
        $hash = 'e' . time() . rand(100, 999);
1260
1261
        $filename_base = basename($image_meta['file'], '.png');
1262
1263
        // Add the hash to the image URL
1264
        $image_url = str_replace($filename_base, $filename_base . '-' . $hash, $image_url);
1265
1266
        // Replace file paths for full and medium sizes with hashed versions.
1267
        $image_meta['file'] = str_replace($filename_base, $filename_base . '-' . $hash, $image_meta['file']);
1268
        $image_meta['sizes']['medium']['file'] = str_replace($filename_base, $filename_base . '-' . $hash, $image_meta['sizes']['medium']['file']);
1269
        $image_meta['sizes']['medium_large']['file'] = str_replace($filename_base, $filename_base . '-' . $hash, $image_meta['sizes']['medium_large']['file']);
1270
        $image_meta['sizes']['large']['file'] = str_replace($filename_base, $filename_base . '-' . $hash, $image_meta['sizes']['large']['file']);
1271
1272
        // Calculate a srcset array.
1273
        $sizes = explode(', ', wp_calculate_image_srcset($size_array, $image_url, $image_meta));
1274
1275
        // Test to confirm all sources in the array include the same edit hash.
1276
        foreach ( $sizes as $size ) {
1277
            $this->assertTrue(false !== strpos($size, $hash));
1278
        }
1279
    }
1280
1281
    /**
1282
     * @ticket 35106
1283
     */
1284
    function test_wp_calculate_image_srcset_with_absolute_path_in_meta() 
1285
    {
1286
        $_wp_additional_image_sizes = wp_get_additional_image_sizes();
1287
1288
        $year_month = date('Y/m');
1289
        $image_meta = wp_get_attachment_metadata(self::$large_id);
1290
        $uploads_dir_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/';
1291
1292
        // Set up test cases for all expected size names.
1293
        $intermediates = array( 'medium', 'medium_large', 'large', 'full' );
1294
1295
        // Add any soft crop intermediate sizes.
1296 View Code Duplication
        foreach ( $_wp_additional_image_sizes as $name => $additional_size ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1297
            if (! $_wp_additional_image_sizes[$name]['crop'] || 0 === $_wp_additional_image_sizes[$name]['height'] ) {
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
introduced by
Expected 1 space before "!"; 0 found
Loading history...
1298
                $intermediates[] = $name;
1299
            }
1300
        }
1301
1302
        $expected = '';
1303
1304 View Code Duplication
        foreach( $image_meta['sizes'] as $name => $size ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1305
            // Whitelist the sizes that should be included so we pick up 'medium_large' in 4.4.
1306
            if (in_array($name, $intermediates) ) {
1307
                $expected .= $uploads_dir_url . $year_month . '/' . $size['file'] . ' ' . $size['width'] . 'w, ';
1308
            }
1309
        }
1310
1311
        // Add the full size width at the end.
1312
        $expected .= $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] .'w';
1313
1314
        // Prepend an absolute path to simulate a pre-2.7 upload
1315
        $image_meta['file'] = 'H:\home\wordpress\trunk/wp-content/uploads/' . $image_meta['file'];
1316
1317 View Code Duplication
        foreach ( $intermediates as $int ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1318
            $image_url = wp_get_attachment_image_url(self::$large_id, $int);
1319
            $size_array = $this->_get_image_size_array_from_meta($image_meta, $int);
1320
            $expected_srcset = $this->_src_first($expected, $image_url, $size_array[0]);
1321
            $this->assertSame($expected_srcset, wp_calculate_image_srcset($size_array, $image_url, $image_meta));
1322
        }
1323
    }
1324
1325
    /**
1326
     * @ticket 33641
1327
     */
1328
    function test_wp_calculate_image_srcset_false() 
1329
    {
1330
        $sizes = wp_calculate_image_srcset(array( 400, 300 ), 'file.png', array());
1331
1332
        // For canola.jpg we should return
1333
        $this->assertFalse($sizes);
1334
    }
1335
1336
    /**
1337
     * @ticket 33641
1338
     */
1339
    function test_wp_calculate_image_srcset_no_width() 
1340
    {
1341
        $file = get_attached_file(self::$large_id);
1342
        $image_url = wp_get_attachment_image_url(self::$large_id, 'medium');
1343
        $image_meta = wp_generate_attachment_metadata(self::$large_id, $file);
1344
1345
        $size_array = array(0, 0);
1346
1347
        $srcset = wp_calculate_image_srcset($size_array, $image_url, $image_meta);
1348
1349
        // The srcset should be false.
1350
        $this->assertFalse($srcset);
1351
    }
1352
1353
    /**
1354
     * @ticket 34955
1355
     * @ticket 33641
1356
     */
1357 View Code Duplication
    function test_wp_calculate_image_srcset_ratio_variance() 
1358
    {
1359
        // Mock data for this test.
1360
        $size_array = array( 218, 300 );
1361
        $image_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-768x1055-218x300.png';
1362
        $image_meta = array(
1363
         'width' => 768,
1364
         'height' => 1055,
1365
         'file' => '2015/12/test-768x1055.png',
1366
         'sizes' => array(
1367
          'thumbnail' => array(
1368
        'file' => 'test-768x1055-150x150.png',
1369
        'width' => 150,
1370
        'height' => 150,
1371
        'mime-type' => 'image/png',
1372
          ),
1373
          'medium' => array(
1374
           'file' => 'test-768x1055-218x300.png',
1375
           'width' => 218,
1376
           'height' => 300,
1377
           'mime-type' => 'image/png',
1378
          ),
1379
          'custom-600' => array(
1380
           'file' => 'test-768x1055-600x824.png',
1381
           'width' => 600,
1382
           'height' => 824,
1383
           'mime-type' => 'image/png',
1384
          ),
1385
          'post-thumbnail' => array(
1386
           'file' => 'test-768x1055-768x510.png',
1387
           'width' => 768,
1388
           'height' => 510,
1389
           'mime-type' => 'image/png',
1390
          ),
1391
         ),
1392
        );
1393
1394
        $expected_srcset = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-768x1055-218x300.png 218w, http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-768x1055-600x824.png 600w, http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-768x1055.png 768w';
1395
1396
        $this->assertSame($expected_srcset, wp_calculate_image_srcset($size_array, $image_src, $image_meta));
1397
    }
1398
1399
    /**
1400
     * @ticket 35108
1401
     * @ticket 33641
1402
     */
1403 View Code Duplication
    function test_wp_calculate_image_srcset_include_src() 
1404
    {
1405
        // Mock data for this test.
1406
        $size_array = array( 2000, 1000 );
1407
        $image_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test.png';
1408
        $image_meta = array(
1409
         'width' => 2000,
1410
         'height' => 1000,
1411
         'file' => '2015/12/test.png',
1412
         'sizes' => array(
1413
          'thumbnail' => array(
1414
        'file' => 'test-150x150.png',
1415
        'width' => 150,
1416
        'height' => 150,
1417
        'mime-type' => 'image/png',
1418
          ),
1419
          'medium' => array(
1420
           'file' => 'test-300x150.png',
1421
           'width' => 300,
1422
           'height' => 150,
1423
           'mime-type' => 'image/png',
1424
          ),
1425
          'medium_large' => array(
1426
           'file' => 'test-768x384.png',
1427
           'width' => 768,
1428
           'height' => 384,
1429
           'mime-type' => 'image/png',
1430
          ),
1431
          'large' => array(
1432
           'file' => 'test-1024x512.png',
1433
           'width' => 1024,
1434
           'height' => 512,
1435
           'mime-type' => 'image/png',
1436
          ),
1437
         ),
1438
        );
1439
1440
        $expected_srcset = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test.png 2000w, http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-300x150.png 300w, http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-768x384.png 768w, http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-1024x512.png 1024w';
1441
1442
        $this->assertSame($expected_srcset, wp_calculate_image_srcset($size_array, $image_src, $image_meta));
1443
    }
1444
1445
    /**
1446
     * @ticket 35480
1447
     */
1448
    function test_wp_calculate_image_srcset_corrupted_image_meta() 
1449
    {
1450
        $size_array = array( 300, 150 );
1451
        $image_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-300x150.png';
1452
        $image_meta = array(
1453
         'width' => 1600,
1454
         'height' => 800,
1455
         'file' => '2015/12/test.png',
1456
         'sizes' => array(
1457
          'thumbnail' => array(
1458
        'file' => 'test-150x150.png',
1459
        'width' => 150,
1460
        'height' => 150,
1461
        'mime-type' => 'image/png',
1462
          ),
1463
          'medium' => array(
1464
           'file' => 'test-300x150.png',
1465
           'width' => 300,
1466
           'height' => 150,
1467
           'mime-type' => 'image/png',
1468
          ),
1469
          'medium_large' => array(
1470
           'file' => 'test-768x384.png',
1471
           'width' => 768,
1472
           'height' => 384,
1473
           'mime-type' => 'image/png',
1474
          ),
1475
          'large' => array(
1476
           'file' => 'test-1024x512.png',
1477
           'width' => 1024,
1478
           'height' => 512,
1479
           'mime-type' => 'image/png',
1480
          ),
1481
         ),
1482
        );
1483
1484
        $srcset = array(
1485
         300  => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-300x150.png 300w',
1486
         768  => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-768x384.png 768w',
1487
         1024 => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-1024x512.png 1024w',
1488
         1600 => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test.png 1600w',
1489
        );
1490
1491
        // No sizes array
1492
        $image_meta1 = $image_meta;
1493
        unset($image_meta1['sizes']);
1494
        $this->assertFalse(wp_calculate_image_srcset($size_array, $image_src, $image_meta1));
1495
1496
        // Sizes is string instead of array; only full size available means no srcset.
1497
        $image_meta2 = $image_meta;
1498
        $image_meta2['sizes'] = '';
1499
        $this->assertFalse(wp_calculate_image_srcset($size_array, $image_src, $image_meta2));
1500
1501
        // File name is incorrect
1502
        $image_meta3 = $image_meta;
1503
        $image_meta3['file'] = '/';
1504
        $this->assertFalse(wp_calculate_image_srcset($size_array, $image_src, $image_meta3));
1505
1506
        // File name is incorrect
1507
        $image_meta4 = $image_meta;
1508
        unset($image_meta4['file']);
1509
        $this->assertFalse(wp_calculate_image_srcset($size_array, $image_src, $image_meta4));
1510
1511
        // Intermediate size is string instead of array.
1512
        $image_meta5 = $image_meta;
1513
        $image_meta5['sizes']['medium_large'] = '';
1514
        unset($srcset[768]);
1515
        $expected_srcset = implode(', ', $srcset);
1516
        $this->assertSame($expected_srcset, wp_calculate_image_srcset($size_array, $image_src, $image_meta5));
1517
    }
1518
1519
    /**
1520
     * @ticket 36549
1521
     * @ticket 33641
1522
     */
1523 View Code Duplication
    function test_wp_calculate_image_srcset_with_spaces_in_filenames() 
1524
    {
1525
        // Mock data for this test.
1526
        $image_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test image-300x150.png';
1527
        $image_meta = array(
1528
         'width' => 2000,
1529
         'height' => 1000,
1530
         'file' => '2015/12/test image.png',
1531
         'sizes' => array(
1532
          'thumbnail' => array(
1533
        'file' => 'test image-150x150.png',
1534
        'width' => 150,
1535
        'height' => 150,
1536
        'mime-type' => 'image/png',
1537
          ),
1538
          'medium' => array(
1539
           'file' => 'test image-300x150.png',
1540
           'width' => 300,
1541
           'height' => 150,
1542
           'mime-type' => 'image/png',
1543
          ),
1544
          'medium_large' => array(
1545
           'file' => 'test image-768x384.png',
1546
           'width' => 768,
1547
           'height' => 384,
1548
           'mime-type' => 'image/png',
1549
          ),
1550
          'large' => array(
1551
           'file' => 'test image-1024x512.png',
1552
           'width' => 1024,
1553
           'height' => 512,
1554
           'mime-type' => 'image/png',
1555
          ),
1556
         ),
1557
        );
1558
1559
        $expected_srcset = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test%20image-300x150.png 300w, http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test%20image-768x384.png 768w, http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test%20image-1024x512.png 1024w';
1560
1561
        $this->assertSame($expected_srcset, wp_calculate_image_srcset(array( 300, 150 ), $image_src, $image_meta));
1562
    }
1563
1564
    /**
1565
     * @ticket 33641
1566
     */
1567
    function test_wp_get_attachment_image_srcset() 
1568
    {
1569
        $_wp_additional_image_sizes = wp_get_additional_image_sizes();
1570
1571
        $image_meta = wp_get_attachment_metadata(self::$large_id);
1572
        $size_array = array( 1600, 1200 ); // full size
1573
1574
        $srcset = wp_get_attachment_image_srcset(self::$large_id, $size_array, $image_meta);
1575
1576
        $year_month = date('Y/m');
1577
        $uploads_dir = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/';
1578
1579
        // Set up test cases for all expected size names.
1580
        $intermediates = array( 'medium', 'medium_large', 'large', 'full' );
1581
1582 View Code Duplication
        foreach ( $_wp_additional_image_sizes as $name => $additional_size ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1583
            if (! $_wp_additional_image_sizes[$name]['crop'] || 0 === $_wp_additional_image_sizes[$name]['height'] ) {
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
introduced by
Expected 1 space before "!"; 0 found
Loading history...
1584
                $intermediates[] = $name;
1585
            }
1586
        }
1587
1588
        $expected = "";
1589
1590 View Code Duplication
        foreach ( $image_meta['sizes'] as $name => $size ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1591
            // Whitelist the sizes that should be included so we pick up 'medium_large' in 4.4.
1592
            if (in_array($name, $intermediates) ) {
1593
                $expected .= $uploads_dir . $year_month . '/' . $size['file'] . ' ' . $size['width'] . 'w, ';
1594
            }
1595
        }
1596
1597
        $expected .= $uploads_dir . $image_meta['file'] . ' ' . $image_meta['width'] .'w';
1598
1599
        $expected_srcset = $this->_src_first($expected, $uploads_dir . $image_meta['file'], $size_array[0]);
1600
1601
        $this->assertSame($expected_srcset, $srcset);
1602
    }
1603
1604
    /**
1605
     * @ticket 33641
1606
     */
1607
    function test_wp_get_attachment_image_srcset_single_srcset() 
1608
    {
1609
        $image_meta = wp_get_attachment_metadata(self::$large_id);
1610
        $size_array = array( 150, 150 );
1611
        /*
1612
         * In our tests, thumbnails will only return a single srcset candidate,
1613
         * so we shouldn't return a srcset value in order to avoid unneeded markup.
1614
         */
1615
        $sizes = wp_get_attachment_image_srcset(self::$large_id, $size_array, $image_meta);
1616
1617
        $this->assertFalse($sizes);
1618
    }
1619
1620
    /**
1621
     * @ticket 33641
1622
     */
1623
    function test_wp_get_attachment_image_srcset_invalidsize() 
1624
    {
1625
        $image_meta = wp_get_attachment_metadata(self::$large_id);
1626
        $invalid_size = 'nailthumb';
1627
        $original_size = array( 1600, 1200 );
1628
1629
        $srcset = wp_get_attachment_image_srcset(self::$large_id, $invalid_size, $image_meta);
1630
1631
        // Expect a srcset for the original full size image to be returned.
1632
        $expected = wp_get_attachment_image_srcset(self::$large_id, $original_size, $image_meta);
1633
1634
        $this->assertSame($expected, $srcset);
1635
    }
1636
1637
    /**
1638
     * @ticket 33641
1639
     */
1640
    function test_wp_get_attachment_image_sizes() 
1641
    {
1642
        // Test sizes against the default WP sizes.
1643
        $intermediates = array('thumbnail', 'medium', 'medium_large', 'large');
1644
1645
        // Make sure themes aren't filtering the sizes array.
1646
        remove_all_filters('wp_calculate_image_sizes');
1647
1648
        foreach( $intermediates as $int_size ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
1649
            $image = wp_get_attachment_image_src(self::$large_id, $int_size);
1650
1651
            $expected = '(max-width: ' . $image[1] . 'px) 100vw, ' . $image[1] . 'px';
1652
            $sizes = wp_get_attachment_image_sizes(self::$large_id, $int_size);
1653
1654
            $this->assertSame($expected, $sizes);
1655
        }
1656
    }
1657
1658
    /**
1659
     * @ticket 33641
1660
     */
1661
    function test_wp_calculate_image_sizes() 
1662
    {
1663
        // Test sizes against the default WP sizes.
1664
        $intermediates = array( 'thumbnail', 'medium', 'medium_large', 'large' );
1665
        $image_meta = wp_get_attachment_metadata(self::$large_id);
1666
1667
        // Make sure themes aren't filtering the sizes array.
1668
        remove_all_filters('wp_calculate_image_sizes');
1669
1670
        foreach ( $intermediates as $int_size ) {
1671
            $size_array = $this->_get_image_size_array_from_meta($image_meta, $int_size);
1672
            $image_src = $image_meta['sizes'][ $int_size ]['file'];
1673
            list( $width, $height ) = $size_array;
0 ignored issues
show
Unused Code introduced by
The assignment to $height is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
1674
1675
            $expected = '(max-width: ' . $width . 'px) 100vw, ' . $width . 'px';
1676
            $sizes = wp_calculate_image_sizes($size_array, $image_src, $image_meta);
1677
1678
            $this->assertSame($expected, $sizes);
1679
        }
1680
    }
1681
1682
    /**
1683
     * @ticket 33641
1684
     */
1685
    function test_wp_make_content_images_responsive() 
1686
    {
1687
        $image_meta = wp_get_attachment_metadata(self::$large_id);
1688
        $size_array = $this->_get_image_size_array_from_meta($image_meta, 'medium');
1689
1690
        $srcset = sprintf('srcset="%s"', wp_get_attachment_image_srcset(self::$large_id, $size_array, $image_meta));
1691
        $sizes = sprintf('sizes="%s"', wp_get_attachment_image_sizes(self::$large_id, $size_array, $image_meta));
1692
1693
        // Function used to build HTML for the editor.
1694
        $img = get_image_tag(self::$large_id, '', '', '', 'medium');
1695
        $img_no_size_in_class = str_replace('size-', '', $img);
1696
        $img_no_width_height = str_replace(' width="' . $size_array[0] . '"', '', $img);
1697
        $img_no_width_height = str_replace(' height="' . $size_array[1] . '"', '', $img_no_width_height);
1698
        $img_no_size_id = str_replace('wp-image-', 'id-', $img);
1699
        $img_with_sizes_attr = str_replace('<img ', '<img sizes="99vw" ', $img);
1700
        $img_xhtml = str_replace(' />', '/>', $img);
1701
        $img_html5 = str_replace(' />', '>', $img);
1702
1703
        // Manually add srcset and sizes to the markup from get_image_tag();
1704
        $respimg = preg_replace('|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img);
1705
        $respimg_no_size_in_class = preg_replace('|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_no_size_in_class);
1706
        $respimg_no_width_height = preg_replace('|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_no_width_height);
1707
        $respimg_with_sizes_attr = preg_replace('|<img ([^>]+) />|', '<img $1 ' . $srcset . ' />', $img_with_sizes_attr);
1708
        $respimg_xhtml = preg_replace('|<img ([^>]+)/>|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_xhtml);
1709
        $respimg_html5 = preg_replace('|<img ([^>]+)>|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_html5);
1710
1711
        $content = '
1712
			<p>Image, standard. Should have srcset and sizes.</p>
1713
			%1$s
1714
1715
			<p>Image, no size class. Should have srcset and sizes.</p>
1716
			%2$s
1717
1718
			<p>Image, no width and height attributes. Should have srcset and sizes (from matching the file name).</p>
1719
			%3$s
1720
1721
			<p>Image, no attachment ID class. Should NOT have srcset and sizes.</p>
1722
			%4$s
1723
1724
			<p>Image, with sizes attribute. Should NOT have two sizes attributes.</p>
1725
			%5$s
1726
1727
			<p>Image, XHTML 1.0 style (no space before the closing slash). Should have srcset and sizes.</p>
1728
			%6$s
1729
1730
			<p>Image, HTML 5.0 style. Should have srcset and sizes.</p>
1731
			%7$s';
1732
1733
        $content_unfiltered = sprintf($content, $img, $img_no_size_in_class, $img_no_width_height, $img_no_size_id, $img_with_sizes_attr, $img_xhtml, $img_html5);
1734
        $content_filtered = sprintf($content, $respimg, $respimg_no_size_in_class, $respimg_no_width_height, $img_no_size_id, $respimg_with_sizes_attr, $respimg_xhtml, $respimg_html5);
1735
1736
        $this->assertSame($content_filtered, wp_make_content_images_responsive($content_unfiltered));
1737
    }
1738
1739
    /**
1740
     * When rendering attributes for responsive images,
1741
     * we rely on the 'wp-image-*' class to find the image by ID.
1742
     * The class name may not be consistent with attachment IDs in DB when
1743
     * working with imported content or when a user has edited
1744
     * the 'src' attribute manually. To avoid incorrect images
1745
     * being displayed, ensure we don't add attributes in this case.
1746
     *
1747
     * @ticket 34898
1748
     * @ticket 33641
1749
     */
1750 View Code Duplication
    function test_wp_make_content_images_responsive_wrong() 
1751
    {
1752
        $image = get_image_tag(self::$large_id, '', '', '', 'medium');
1753
1754
        // Replace the src URL
1755
        $image_wrong_src = preg_replace('|src="[^"]+"|', 'src="http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/foo.jpg"', $image);
1756
1757
        $this->assertSame($image_wrong_src, wp_make_content_images_responsive($image_wrong_src));
1758
    }
1759
1760
    /**
1761
     * @ticket 33641
1762
     */
1763 View Code Duplication
    function test_wp_make_content_images_responsive_with_preexisting_srcset() 
1764
    {
1765
        // Generate HTML and add a dummy srcset attribute.
1766
        $image_html = get_image_tag(self::$large_id, '', '', '', 'medium');
1767
        $image_html = preg_replace('|<img ([^>]+) />|', '<img $1 ' . 'srcset="image2x.jpg 2x" />', $image_html);
1768
1769
        // The content filter should return the image unchanged.
1770
        $this->assertSame($image_html, wp_make_content_images_responsive($image_html));
1771
    }
1772
1773
    /**
1774
     * @ticket 33641
1775
     * @ticket 34528
1776
     */
1777
    function test_wp_calculate_image_srcset_animated_gifs() 
1778
    {
1779
        // Mock meta for an animated gif.
1780
        $image_meta = array(
1781
         'width' => 1200,
1782
         'height' => 600,
1783
         'file' => 'animated.gif',
1784
         'sizes' => array(
1785
          'thumbnail' => array(
1786
        'file' => 'animated-150x150.gif',
1787
        'width' => 150,
1788
        'height' => 150,
1789
        'mime-type' => 'image/gif'
1790
          ),
1791
          'medium' => array(
1792
           'file' => 'animated-300x150.gif',
1793
           'width' => 300,
1794
           'height' => 150,
1795
           'mime-type' => 'image/gif'
1796
          ),
1797
          'large' => array(
1798
           'file' => 'animated-1024x512.gif',
1799
           'width' => 1024,
1800
           'height' => 512,
1801
           'mime-type' => 'image/gif'
1802
          ),
1803
         )
1804
        );
1805
1806
        $full_src  = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['file'];
1807
        $large_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['sizes']['large']['file'];
1808
1809
        // Test with soft resized size array.
1810
        $size_array = array(900, 450);
1811
1812
        // Full size GIFs should not return a srcset.
1813
        $this->assertFalse(wp_calculate_image_srcset($size_array, $full_src, $image_meta));
1814
        // Intermediate sized GIFs should not include the full size in the srcset.
1815
        $this->assertFalse(strpos(wp_calculate_image_srcset($size_array, $large_src, $image_meta), $full_src));
1816
    }
1817
1818
    /**
1819
     * @ticket 35045
1820
     * @ticket 33641
1821
     */
1822
    function test_wp_make_content_images_responsive_schemes() 
1823
    {
1824
        $image_meta = wp_get_attachment_metadata(self::$large_id);
1825
        $size_array = $this->_get_image_size_array_from_meta($image_meta, 'medium');
1826
1827
        $srcset = sprintf('srcset="%s"', wp_get_attachment_image_srcset(self::$large_id, $size_array, $image_meta));
1828
        $sizes  = sprintf('sizes="%s"', wp_get_attachment_image_sizes(self::$large_id, $size_array, $image_meta));
1829
1830
        // Build HTML for the editor.
1831
        $img          = get_image_tag(self::$large_id, '', '', '', 'medium');
1832
        $img_https    = str_replace('http://', 'https://', $img);
1833
        $img_relative = str_replace('http://', '//', $img);
1834
1835
        // Manually add srcset and sizes to the markup from get_image_tag().
1836
        $respimg          = preg_replace('|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img);
1837
        $respimg_https    = preg_replace('|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_https);
1838
        $respimg_relative = preg_replace('|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_relative);
1839
1840
        $content = '
1841
			<p>Image, http: protocol. Should have srcset and sizes.</p>
1842
			%1$s
1843
1844
			<p>Image, https: protocol. Should have srcset and sizes.</p>
1845
			%2$s
1846
1847
			<p>Image, protocol-relative. Should have srcset and sizes.</p>
1848
			%3$s';
1849
1850
        $unfiltered = sprintf($content, $img, $img_https, $img_relative);
1851
        $expected   = sprintf($content, $respimg, $respimg_https, $respimg_relative);
1852
        $actual     = wp_make_content_images_responsive($unfiltered);
1853
1854
        $this->assertSame($expected, $actual);
1855
    }
1856
1857
    /**
1858
     * @ticket 34945
1859
     * @ticket 33641
1860
     */
1861
    function test_wp_get_attachment_image_with_https_on() 
1862
    {
1863
        // Mock meta for the image.
1864
        $image_meta = array(
1865
         'width'  => 1200,
1866
         'height' => 600,
1867
         'file'   => 'test.jpg',
1868
         'sizes'  => array(
1869
          'thumbnail' => array(
1870
        'file'   => 'test-150x150.jpg',
1871
        'width'  => 150,
1872
        'height' => 150,
1873
          ),
1874
          'medium'    => array(
1875
           'file'   => 'test-300x150.jpg',
1876
           'width'  => 300,
1877
           'height' => 150,
1878
          ),
1879
          'large'     => array(
1880
           'file'   => 'test-1024x512.jpg',
1881
           'width'  => 1024,
1882
           'height' => 512,
1883
          ),
1884
         )
1885
        );
1886
1887
        // Test using the large file size.
1888
        $size_array = array( 1024, 512 );
1889
        $image_url  = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['sizes']['large']['file'];
1890
1891
        $_SERVER['HTTPS'] = 'on';
1892
1893
        $expected = 'https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test-1024x512.jpg 1024w, https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test-300x150.jpg 300w, https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg 1200w';
1894
        $actual   = wp_calculate_image_srcset($size_array, $image_url, $image_meta);
1895
1896
        $this->assertSame($expected, $actual);
1897
    }
1898
1899
    /**
1900
     * @ticket 36084
1901
     */
1902
    function test_get_image_send_to_editor_defaults() 
1903
    {
1904
        $id      = self::$large_id;
1905
        $caption = '';
1906
        $title   = 'A test title value.';
1907
        $align   = 'left';
1908
1909
        // Calculate attachment data (default is medium).
1910
        $attachment = wp_get_attachment_image_src($id, 'medium');
1911
1912
        $html = '<img src="%1$s" alt="" width="%2$d" height="%3$d" class="align%4$s size-medium wp-image-%5$d" />';
1913
        $expected = sprintf($html, $attachment[0], $attachment[1], $attachment[2], $align, $id);
1914
1915
        $this->assertSame($expected, get_image_send_to_editor($id, $caption, $title, $align));
1916
1917
        $this->assertSame($expected, get_image_send_to_editor($id, $caption, $title, $align));
1918
    }
1919
1920
    /**
1921
     * @ticket 36084
1922
     */
1923
    function test_get_image_send_to_editor_defaults_with_optional_params() 
1924
    {
1925
        $id      = self::$large_id;
1926
        $caption = 'A test caption.';
1927
        $title   = 'A test title value.';
1928
        $align   = 'left';
1929
        $url     = get_permalink($id);
1930
        $rel     = true;
1931
        $size    = 'thumbnail';
1932
        $alt     = 'An example alt value.';
1933
1934
        // Calculate attachment data.
1935
        $attachment = wp_get_attachment_image_src($id, $size);
1936
1937
        $html = '<a href="%1$s" rel="%2$s"><img src="%3$s" alt="%4$s" width="%5$d" height="%6$d" class="size-%8$s wp-image-%9$d" /></a>';
1938
        $html = '[caption id="attachment_%9$d" align="align%7$s" width="%5$d"]' . $html . ' %10$s[/caption]';
1939
1940
        $expected = sprintf($html, $url, 'attachment wp-att-' . $id, $attachment[0], $alt, $attachment[1], $attachment[2], $align, $size, $id, $caption);
1941
1942
        $this->assertSame($expected, get_image_send_to_editor($id, $caption, $title, $align, $url, $rel, $size, $alt));
1943
    }
1944
1945
    /**
1946
     * @ticket 36084
1947
     */
1948
    function test_get_image_send_to_editor_defaults_no_caption_no_rel() 
1949
    {
1950
        $id      = self::$large_id;
1951
        $caption = '';
1952
        $title   = 'A test title value.';
1953
        $align   = 'left';
1954
        $url     = get_permalink($id);
1955
        $rel     = '';
1956
        $size    = 'thumbnail';
1957
        $alt     = 'An example alt value.';
1958
1959
        // Calculate attachment data.
1960
        $attachment = wp_get_attachment_image_src($id, $size);
1961
1962
        $html = '<a href="%1$s"><img src="%2$s" alt="%3$s" width="%4$d" height="%5$d" class="align%6$s size-%7$s wp-image-%8$d" /></a>';
1963
1964
        $expected = sprintf($html, $url, $attachment[0], $alt, $attachment[1], $attachment[2], $align, $size, $id);
1965
1966
        $this->assertSame($expected, get_image_send_to_editor($id, $caption, $title, $align, $url, $rel, $size, $alt));
1967
    }
1968
1969
    /**
1970
     * Tests if wp_get_attachment_image() uses wp_get_attachment_metadata().
1971
     *
1972
     * In this way, the meta data can be filtered using the filter
1973
     * `wp_get_attachment_metadata`.
1974
     *
1975
     * The test checks if the image size that is added in the filter is
1976
     * used in the output of `wp_get_attachment_image()`.
1977
     *
1978
     * @ticket 36246
1979
     */
1980
    function test_wp_get_attachment_image_should_use_wp_get_attachment_metadata() 
1981
    {
1982
        add_filter('wp_get_attachment_metadata', array( $this, '_filter_36246' ), 10, 2);
1983
1984
        remove_all_filters('wp_calculate_image_sizes');
1985
1986
        $actual = wp_get_attachment_image(self::$large_id, 'testsize');
1987
        $year = date('Y');
1988
        $month = date('m');
1989
1990
        $expected = '<img width="999" height="999" src="http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year . '/' . $month . '/test-image-testsize-999x999.png"' .
1991
         ' class="attachment-testsize size-testsize" alt=""' .
1992
         ' srcset="http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year . '/' . $month . '/test-image-testsize-999x999.png 999w,' .
1993
          ' http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year . '/' . $month . '/test-image-large-150x150.png 150w"' .
1994
          ' sizes="(max-width: 999px) 100vw, 999px" />';
1995
1996
        remove_filter('wp_get_attachment_metadata', array( $this, '_filter_36246' ));
1997
1998
        $this->assertSame($expected, $actual);
1999
    }
2000
2001
    function _filter_36246( $data, $attachment_id ) 
2002
    {
2003
        $data['sizes']['testsize'] = array(
2004
         'file' => 'test-image-testsize-999x999.png',
2005
         'width' => 999,
2006
         'height' => 999,
2007
         'mime-type' => 'image/png',
2008
        );
2009
        return $data;
2010
    }
2011
2012
    /**
2013
     * @ticket 37813
2014
     */
2015
    public function test_return_type_when_inserting_attachment_with_error_in_data() 
2016
    {
2017
        $data = array(
2018
         'post_status'  => 'public',
2019
         'post_content' => 'Attachment content',
2020
         'post_title'   => 'Attachment Title',
2021
         'post_date'    => '2012-02-30 00:00:00',
2022
        );
2023
2024
        $attachment_id = wp_insert_attachment($data, '', 0, true);
2025
        $this->assertWPError($attachment_id);
2026
        $this->assertEquals('invalid_date', $attachment_id->get_error_code());
2027
2028
        $attachment_id = wp_insert_attachment($data, '', 0);
2029
        $this->assertSame(0, $attachment_id);
2030
    }
2031
}
2032
2033
/**
2034
 * Helper class for `test_autoembed`.
2035
 */
2036
class Test_Autoembed extends WP_Embed
2037
{
2038
    public function shortcode( $attr, $url = '' ) 
2039
    {
2040
        return '[embed]';
2041
    }
2042
}
2043