Completed
Pull Request — master (#2)
by Stephen
13:19
created

Tests_Media::wpTearDownAfterClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
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
	protected static $large_id;
9
	protected static $_sizes;
10
11
	public static function wpSetUpBeforeClass( $factory ) {
12
		self::$_sizes = wp_get_additional_image_sizes();
13
		$GLOBALS['_wp_additional_image_sizes'] = array();
14
15
		$filename = DIR_TESTDATA . '/images/test-image-large.png';
16
		self::$large_id = $factory->attachment->create_upload_object( $filename );
17
	}
18
19
	public static function wpTearDownAfterClass() {
20
		$GLOBALS['_wp_additional_image_sizes'] = self::$_sizes;
21
	}
22
23
	function setUp() {
24
		parent::setUp();
25
		$this->caption = 'A simple caption.';
26
		$this->html_content = <<<CAP
27
A <strong class='classy'>bolded</strong> <em>caption</em> with a <a href="#">link</a>.
28
CAP;
29
		$this->img_content = <<<CAP
30
<img src="pic.jpg" id='anId' alt="pic"/>
31
CAP;
32
		$this->img_name = 'image.jpg';
33
		$this->img_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $this->img_name;
34
		$this->img_html = '<img src="' . $this->img_url . '"/>';
35
		$this->img_meta = array( 'width' => 100, 'height' => 100, 'sizes' => '' );
36
	}
37
38
	function test_img_caption_shortcode_added() {
39
		global $shortcode_tags;
40
		$this->assertEquals( 'img_caption_shortcode', $shortcode_tags['caption'] );
41
		$this->assertEquals( 'img_caption_shortcode', $shortcode_tags['wp_caption'] );
42
	}
43
44
	function test_img_caption_shortcode_with_empty_params() {
45
		$result = img_caption_shortcode( array() );
46
		$this->assertNull( $result );
47
	}
48
49
	function test_img_caption_shortcode_with_bad_attr() {
50
		$result = img_caption_shortcode( array(), 'content' );
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
51
		$this->assertEquals( 'content', 'content' );
52
	}
53
54
	function test_img_caption_shortcode_with_old_format() {
55
		$result = img_caption_shortcode(
56
			array( 'width' => 20, 'caption' => $this->caption )
57
		);
58
59
		$this->assertEquals( 2, preg_match_all( '/wp-caption/', $result, $_r ) );
60
		$this->assertEquals( 1, preg_match_all( '/alignnone/', $result, $_r ) );
61
		$this->assertEquals( 1, preg_match_all( "/{$this->caption}/", $result, $_r ) );
62
63
		if ( current_theme_supports( 'html5', 'caption' ) ) {
64
			$this->assertEquals( 1, preg_match_all( "/width: 20/", $result, $_r ) );
65
		} else {
66
			$this->assertEquals( 1, preg_match_all( "/width: 30/", $result, $_r ) );
67
		}
68
	}
69
70
	function test_img_caption_shortcode_with_old_format_id_and_align() {
71
		$result = img_caption_shortcode(
72
			array(
73
				'width' => 20,
74
				'caption' => $this->caption,
75
				'id' => '"myId',
76
				'align' => '&myAlignment'
77
			)
78
		);
79
		$this->assertEquals( 1, preg_match_all( '/wp-caption &amp;myAlignment/', $result, $_r ) );
80
		$this->assertEquals( 1, preg_match_all( '/id="myId"/', $result, $_r ) );
81
		$this->assertEquals( 1, preg_match_all( "/{$this->caption}/", $result, $_r ) );
82
	}
83
84
	function test_new_img_caption_shortcode_with_html_caption() {
85
		$result = img_caption_shortcode(
86
			array( 'width' => 20, 'caption' => $this->html_content )
87
		);
88
		$our_preg = preg_quote( $this->html_content );
89
90
		$this->assertEquals( 1, preg_match_all( "~{$our_preg}~", $result, $_r ) );
91
	}
92
93
	function test_new_img_caption_shortcode_new_format() {
94
		$result = img_caption_shortcode(
95
			array( 'width' => 20 ),
96
			$this->img_content . $this->html_content
97
		);
98
		$img_preg = preg_quote( $this->img_content );
99
		$content_preg = preg_quote( $this->html_content );
100
101
		$this->assertEquals( 1, preg_match_all( "~{$img_preg}.*wp-caption-text~", $result, $_r ) );
102
		$this->assertEquals( 1, preg_match_all( "~wp-caption-text.*{$content_preg}~", $result, $_r ) );
103
	}
104
105
	function test_new_img_caption_shortcode_new_format_and_linked_image() {
106
		$linked_image = "<a href='#'>{$this->img_content}</a>";
107
		$result = img_caption_shortcode(
108
			array( 'width' => 20 ),
109
			$linked_image . $this->html_content
110
		);
111
		$img_preg = preg_quote( $linked_image );
112
		$content_preg = preg_quote( $this->html_content );
113
114
		$this->assertEquals( 1, preg_match_all( "~{$img_preg}.*wp-caption-text~", $result, $_r ) );
115
		$this->assertEquals( 1, preg_match_all( "~wp-caption-text.*{$content_preg}~", $result, $_r ) );
116
	}
117
118
	function test_new_img_caption_shortcode_new_format_and_linked_image_with_newline() {
119
		$linked_image = "<a href='#'>{$this->img_content}</a>";
120
		$result = img_caption_shortcode(
121
			array( 'width' => 20 ),
122
			$linked_image . "\n\n" . $this->html_content
123
		);
124
		$img_preg = preg_quote( $linked_image );
125
		$content_preg = preg_quote( $this->html_content );
126
127
		$this->assertEquals( 1, preg_match_all( "~{$img_preg}.*wp-caption-text~", $result, $_r ) );
128
		$this->assertEquals( 1, preg_match_all( "~wp-caption-text.*{$content_preg}~", $result, $_r ) );
129
	}
130
131
	function test_add_remove_oembed_provider() {
132
		wp_oembed_add_provider( 'http://foo.bar/*', 'http://foo.bar/oembed' );
133
		$this->assertTrue( wp_oembed_remove_provider( 'http://foo.bar/*' ) );
134
		$this->assertFalse( wp_oembed_remove_provider( 'http://foo.bar/*' ) );
135
	}
136
137
	/**
138
	 * @ticket 23776
139
	 */
140
	function test_autoembed_empty() {
141
		global $wp_embed;
142
143
		$content = '';
144
145
		$result = $wp_embed->autoembed( $content );
146
		$this->assertEquals( $content, $result );
147
	}
148
149
	/**
150
	 * @ticket 23776
151
	 */
152
	function test_autoembed_no_paragraphs_around_urls() {
153
		global $wp_embed;
154
155
		$content = <<<EOF
156
$ my command
157
First line.
158
159
http://example.com/1/
160
http://example.com/2/
161
Last line.
162
163
<pre>http://some.link/
164
http://some.other.link/</pre>
165
EOF;
166
167
		$result = $wp_embed->autoembed( $content );
168
		$this->assertEquals( $content, $result );
169
	}
170
171
	function data_autoembed() {
172
		return array(
173
174
			// Should embed
175
			array(
176
'https://w.org',
177
'[embed]'
178
			),
179
			array(
180
'test
181
 https://w.org
182
test',
183
'test
184
 [embed]
185
test'
186
			),
187
			array(
188
'<p class="test">https://w.org</p>',
189
'<p class="test">[embed]</p>'
190
			),
191
			array(
192
'<p> https://w.org </p>',
193
'<p> [embed] </p>'
194
			),
195
			array(
196
'<p>test
197
https://w.org
198
test</p>',
199
'<p>test
200
[embed]
201
test</p>'
202
			),
203
			array(
204
'<p>https://w.org
205
</p>',
206
'<p>[embed]
207
</p>'
208
			),
209
210
			// Should NOT embed
211
			array(
212
'test https://w.org</p>'
213
			),
214
			array(
215
'<span>https://w.org</a>'
216
			),
217
			array(
218
'<pre>https://w.org
219
</p>'
220
			),
221
			array(
222
'<a href="https://w.org">
223
https://w.org</a>'
224
			),
225
		);
226
	}
227
228
	/**
229
	 * @dataProvider data_autoembed
230
	 */
231
	function test_autoembed( $content, $result = null ) {
232
		$wp_embed = new Test_Autoembed;
233
234
		$this->assertEquals( $wp_embed->autoembed( $content ), $result ? $result : $content );
235
	}
236
237
	function test_wp_prepare_attachment_for_js() {
238
		// Attachment without media
239
		$id = wp_insert_attachment(array(
240
			'post_status' => 'publish',
241
			'post_title' => 'Prepare',
242
			'post_content_filtered' => 'Prepare',
243
			'post_type' => 'post'
244
		));
245
		$post = get_post( $id );
246
247
		$prepped = wp_prepare_attachment_for_js( $post );
248
		$this->assertInternalType( 'array', $prepped );
249
		$this->assertEquals( 0, $prepped['uploadedTo'] );
250
		$this->assertEquals( '', $prepped['mime'] );
251
		$this->assertEquals( '', $prepped['type'] );
252
		$this->assertEquals( '', $prepped['subtype'] );
253
		// #21963, there will be a guid always, so there will be a URL
254
		$this->assertNotEquals( '', $prepped['url'] );
255
		$this->assertEquals( site_url( 'wp-includes/images/media/default.png' ), $prepped['icon'] );
256
257
		// Fake a mime
258
		$post->post_mime_type = 'image/jpeg';
259
		$prepped = wp_prepare_attachment_for_js( $post );
260
		$this->assertEquals( 'image/jpeg', $prepped['mime'] );
261
		$this->assertEquals( 'image', $prepped['type'] );
262
		$this->assertEquals( 'jpeg', $prepped['subtype'] );
263
264
		// Fake a mime without a slash. See #WP22532
265
		$post->post_mime_type = 'image';
266
		$prepped = wp_prepare_attachment_for_js( $post );
267
		$this->assertEquals( 'image', $prepped['mime'] );
268
		$this->assertEquals( 'image', $prepped['type'] );
269
		$this->assertEquals( '', $prepped['subtype'] );
270
271
		// Test that if author is not found, we return "(no author)" as `display_name`.
272
		// The previously used test post contains no author, so we can reuse it.
273
		$this->assertEquals( '(no author)', $prepped['authorName'] );
274
275
		// Test that if author has HTML entities in display_name, they're decoded correctly.
276
		$html_entity_author = self::factory()->user->create( array(
277
			'display_name' => 'You &amp; Me',
278
		) );
279
		$post->post_author = $html_entity_author;
280
		$prepped = wp_prepare_attachment_for_js( $post );
281
		$this->assertEquals( 'You & Me', $prepped['authorName'] );
282
	}
283
284
	/**
285
	 * @ticket 38965
286
	 */
287
	function test_wp_prepare_attachment_for_js_without_image_sizes() {
288
		// Create the attachement post.
289
		$id = wp_insert_attachment( array(
290
			'post_title' => 'Attachment Title',
291
			'post_type' => 'attachment',
292
			'post_parent' => 0,
293
			'post_mime_type' => 'image/jpeg',
294
			'guid' => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test-image.jpg',
295
		) );
296
297
		// Add attachment metadata without sizes.
298
		wp_update_attachment_metadata( $id, array(
299
			'width' => 50,
300
			'height' => 50,
301
			'file' => 'test-image.jpg',
302
		) );
303
304
		$prepped = wp_prepare_attachment_for_js( get_post( $id ) );
305
306
		$this->assertTrue( isset( $prepped['sizes'] ) );
307
	}
308
309
	/**
310
	 * @ticket 19067
311
	 * @expectedDeprecated wp_convert_bytes_to_hr
312
	 */
313
	function test_wp_convert_bytes_to_hr() {
314
		$kb = 1024;
315
		$mb = $kb * 1024;
316
		$gb = $mb * 1024;
317
		$tb = $gb * 1024;
318
319
		// test if boundaries are correct
320
		$this->assertEquals( '1TB', wp_convert_bytes_to_hr( $tb ) );
0 ignored issues
show
Deprecated Code introduced by
The function wp_convert_bytes_to_hr() has been deprecated with message: 3.6.0 Use size_format()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
321
		$this->assertEquals( '1GB', wp_convert_bytes_to_hr( $gb ) );
0 ignored issues
show
Deprecated Code introduced by
The function wp_convert_bytes_to_hr() has been deprecated with message: 3.6.0 Use size_format()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
322
		$this->assertEquals( '1MB', wp_convert_bytes_to_hr( $mb ) );
0 ignored issues
show
Deprecated Code introduced by
The function wp_convert_bytes_to_hr() has been deprecated with message: 3.6.0 Use size_format()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
323
		$this->assertEquals( '1KB', wp_convert_bytes_to_hr( $kb ) );
0 ignored issues
show
Deprecated Code introduced by
The function wp_convert_bytes_to_hr() has been deprecated with message: 3.6.0 Use size_format()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
324
325
		$this->assertEquals( '1 TB', size_format( $tb ) );
326
		$this->assertEquals( '1 GB', size_format( $gb ) );
327
		$this->assertEquals( '1 MB', size_format( $mb ) );
328
		$this->assertEquals( '1 KB', size_format( $kb ) );
329
330
		// now some values around
331
		$hr = wp_convert_bytes_to_hr( $tb + $tb / 2 + $mb );
0 ignored issues
show
Deprecated Code introduced by
The function wp_convert_bytes_to_hr() has been deprecated with message: 3.6.0 Use size_format()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
332
		$this->assertEquals( 1.50000095367, (float) str_replace( ',', '.', $hr ), 'The values should be equal', 0.0001 );
333
334
		$hr = wp_convert_bytes_to_hr( $tb - $mb - $kb );
0 ignored issues
show
Deprecated Code introduced by
The function wp_convert_bytes_to_hr() has been deprecated with message: 3.6.0 Use size_format()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
335
		$this->assertEquals( 1023.99902248, (float) str_replace( ',', '.', $hr ), 'The values should be equal', 0.0001 );
336
337
		$hr = wp_convert_bytes_to_hr( $gb + $gb / 2 + $mb );
0 ignored issues
show
Deprecated Code introduced by
The function wp_convert_bytes_to_hr() has been deprecated with message: 3.6.0 Use size_format()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
338
		$this->assertEquals( 1.5009765625, (float) str_replace( ',', '.', $hr ), 'The values should be equal', 0.0001 );
339
340
		$hr = wp_convert_bytes_to_hr( $gb - $mb - $kb );
0 ignored issues
show
Deprecated Code introduced by
The function wp_convert_bytes_to_hr() has been deprecated with message: 3.6.0 Use size_format()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
341
		$this->assertEquals( 1022.99902344, (float) str_replace( ',', '.', $hr ), 'The values should be equal', 0.0001 );
342
343
		// edge
344
		$this->assertEquals( '-1B', wp_convert_bytes_to_hr( -1 ) );
0 ignored issues
show
Deprecated Code introduced by
The function wp_convert_bytes_to_hr() has been deprecated with message: 3.6.0 Use size_format()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
345
		$this->assertEquals( '0B', wp_convert_bytes_to_hr( 0 ) );
0 ignored issues
show
Deprecated Code introduced by
The function wp_convert_bytes_to_hr() has been deprecated with message: 3.6.0 Use size_format()

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
346
	}
347
348
	/**
349
	 * @ticket 22960
350
	 */
351
	function test_get_attached_images() {
352
		$post_id = self::factory()->post->create();
353
		$attachment_id = self::factory()->attachment->create_object( $this->img_name, $post_id, array(
354
			'post_mime_type' => 'image/jpeg',
355
			'post_type' => 'attachment'
356
		) );
357
358
		$images = get_attached_media( 'image', $post_id );
359
		$this->assertEquals( $images, array( $attachment_id => get_post( $attachment_id ) ) );
360
	}
361
362
	/**
363
	 * @ticket 22960
364
	 */
365
	function test_post_galleries_images() {
366
		$ids1 = array();
367
		$ids1_srcs = array();
368
		foreach ( range( 1, 3 ) as $i ) {
369
			$attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array(
370
				'post_mime_type' => 'image/jpeg',
371
				'post_type' => 'attachment'
372
			) );
373
			$metadata = array_merge( array( "file" => "image$i.jpg" ), $this->img_meta );
374
			wp_update_attachment_metadata( $attachment_id, $metadata );
375
			$ids1[] = $attachment_id;
376
			$ids1_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
377
		}
378
379
		$ids2 = array();
380
		$ids2_srcs = array();
381
		foreach ( range( 4, 6 ) as $i ) {
382
			$attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array(
383
				'post_mime_type' => 'image/jpeg',
384
				'post_type' => 'attachment'
385
			) );
386
			$metadata = array_merge( array( "file" => "image$i.jpg" ), $this->img_meta );
387
			wp_update_attachment_metadata( $attachment_id, $metadata );
388
			$ids2[] = $attachment_id;
389
			$ids2_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
390
		}
391
392
		$ids1_joined = join( ',', $ids1 );
393
		$ids2_joined = join( ',', $ids2 );
394
395
		$blob =<<<BLOB
396
[gallery ids="$ids1_joined"]
397
398
[gallery ids="$ids2_joined"]
399
BLOB;
400
		$post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
401
		$srcs = get_post_galleries_images( $post_id );
402
		$this->assertEquals( $srcs, array( $ids1_srcs, $ids2_srcs ) );
403
	}
404
405
	/**
406
	 * @ticket 39304
407
	 */
408
	function test_post_galleries_images_without_global_post() {
409
		// Set up an unattached image.
410
		$this->factory->attachment->create_object( array(
411
			'file' => 'test.jpg',
412
			'post_parent' => 0,
413
			'post_mime_type' => 'image/jpeg',
414
			'post_type' => 'attachment'
415
		) );
416
417
		$post_id = $this->factory->post->create( array(
418
			'post_content' => '[gallery]',
419
		) );
420
421
		$galleries = get_post_galleries( $post_id, false );
422
423
		$this->assertEmpty( $galleries[0]['src'] );
424
	}
425
426
	/**
427
	 * @ticket 39304
428
	 */
429
	function test_post_galleries_ignores_global_post() {
430
		$global_post_id = $this->factory->post->create( array(
431
			'post_content' => 'Global Post',
432
		) );
433
		$post_id = $this->factory->post->create( array(
434
			'post_content' => '[gallery]',
435
		) );
436
		$this->factory->attachment->create_object( array(
437
			'file' => 'test.jpg',
438
			'post_parent' => $post_id,
439
			'post_mime_type' => 'image/jpeg',
440
			'post_type' => 'attachment'
441
		) );
442
		$expected_srcs = array(
443
			'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg'
444
		);
445
446
		// Set the global $post context to the other post.
447
		$GLOBALS['post'] = get_post( $global_post_id );
448
449
		$galleries = get_post_galleries( $post_id, false );
450
451
		$this->assertNotEmpty( $galleries[0]['src'] );
452
		$this->assertSame( $galleries[0]['src'], $expected_srcs );
453
	}
454
455
	/**
456
	 * @ticket 39304
457
	 */
458
	function test_post_galleries_respects_id_attrs() {
459
		$post_id = $this->factory->post->create( array(
460
			'post_content' => 'No gallery defined',
461
		) );
462
		$post_id_two = $this->factory->post->create( array(
463
			'post_content' => "[gallery id='$post_id']",
464
		) );
465
		$this->factory->attachment->create_object( array(
466
			'file' => 'test.jpg',
467
			'post_parent' => $post_id,
468
			'post_mime_type' => 'image/jpeg',
469
			'post_type' => 'attachment'
470
		) );
471
		$expected_srcs = array(
472
			'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg'
473
		);
474
475
		$galleries = get_post_galleries( $post_id_two, false );
476
477
		// Set the global $post context
478
		$GLOBALS['post'] = get_post( $post_id_two );
479
		$galleries_with_global_context = get_post_galleries( $post_id_two, false );
480
481
		// Check that the global post state doesn't affect the results
482
		$this->assertSame( $galleries, $galleries_with_global_context );
483
484
		$this->assertNotEmpty( $galleries[0]['src'] );
485
		$this->assertSame( $galleries[0]['src'], $expected_srcs );
486
	}
487
488
	/**
489
	 * @ticket 22960
490
	 */
491
	function test_post_gallery_images() {
492
		$ids1 = array();
493
		$ids1_srcs = array();
494
		foreach ( range( 1, 3 ) as $i ) {
495
			$attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array(
496
				'post_mime_type' => 'image/jpeg',
497
				'post_type' => 'attachment'
498
			) );
499
			$metadata = array_merge( array( "file" => "image$i.jpg" ), $this->img_meta );
500
			wp_update_attachment_metadata( $attachment_id, $metadata );
501
			$ids1[] = $attachment_id;
502
			$ids1_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
503
		}
504
505
		$ids2 = array();
506
		$ids2_srcs = array();
507
		foreach ( range( 4, 6 ) as $i ) {
508
			$attachment_id = self::factory()->attachment->create_object( "image$i.jpg", 0, array(
509
				'post_mime_type' => 'image/jpeg',
510
				'post_type' => 'attachment'
511
			) );
512
			$metadata = array_merge( array( "file" => "image$i.jpg" ), $this->img_meta );
513
			wp_update_attachment_metadata( $attachment_id, $metadata );
514
			$ids2[] = $attachment_id;
515
			$ids2_srcs[] = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . "image$i.jpg";
516
		}
517
518
		$ids1_joined = join( ',', $ids1 );
519
		$ids2_joined = join( ',', $ids2 );
520
521
		$blob =<<<BLOB
522
[gallery ids="$ids1_joined"]
523
524
[gallery ids="$ids2_joined"]
525
BLOB;
526
		$post_id = self::factory()->post->create( array( 'post_content' => $blob ) );
527
		$srcs = get_post_gallery_images( $post_id );
528
		$this->assertEquals( $srcs, $ids1_srcs );
529
	}
530
531
	function test_get_media_embedded_in_content() {
532
		$object =<<<OBJ
533
<object src="this" data="that">
534
	<param name="value"/>
535
</object>
536
OBJ;
537
		$embed =<<<EMBED
538
<embed src="something.mp4"/>
539
EMBED;
540
		$iframe =<<<IFRAME
541
<iframe src="youtube.com" width="7000" />
542
IFRAME;
543
		$audio =<<<AUDIO
544
<audio preload="none">
545
	<source />
546
</audio>
547
AUDIO;
548
		$video =<<<VIDEO
549
<video preload="none">
550
	<source />
551
</video>
552
VIDEO;
553
554
		$content =<<<CONTENT
555
This is a comment
556
$object
557
558
This is a comment
559
$embed
560
561
This is a comment
562
$iframe
563
564
This is a comment
565
$audio
566
567
This is a comment
568
$video
569
570
This is a comment
571
CONTENT;
572
573
		$types = array( 'object', 'embed', 'iframe', 'audio', 'video' );
574
		$contents = array_values( compact( $types ) );
575
576
		$matches = get_media_embedded_in_content( $content, 'audio' );
0 ignored issues
show
Documentation introduced by
'audio' is of type string, but the function expects a array|null.

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...
577
		$this->assertEquals( array( $audio ), $matches );
578
579
		$matches = get_media_embedded_in_content( $content, 'video' );
0 ignored issues
show
Documentation introduced by
'video' is of type string, but the function expects a array|null.

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...
580
		$this->assertEquals( array( $video ), $matches );
581
582
		$matches = get_media_embedded_in_content( $content, 'object' );
0 ignored issues
show
Documentation introduced by
'object' is of type string, but the function expects a array|null.

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...
583
		$this->assertEquals( array( $object ), $matches );
584
585
		$matches = get_media_embedded_in_content( $content, 'embed' );
0 ignored issues
show
Documentation introduced by
'embed' is of type string, but the function expects a array|null.

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...
586
		$this->assertEquals( array( $embed ), $matches );
587
588
		$matches = get_media_embedded_in_content( $content, 'iframe' );
0 ignored issues
show
Documentation introduced by
'iframe' is of type string, but the function expects a array|null.

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...
589
		$this->assertEquals( array( $iframe ), $matches );
590
591
		$matches = get_media_embedded_in_content( $content, $types );
592
		$this->assertEquals( $contents, $matches );
593
	}
594
595
	function test_get_media_embedded_in_content_order() {
596
		$audio =<<<AUDIO
597
<audio preload="none">
598
	<source />
599
</audio>
600
AUDIO;
601
		$video =<<<VIDEO
602
<video preload="none">
603
	<source />
604
</video>
605
VIDEO;
606
		$content = $audio . $video;
607
608
		$matches1 = get_media_embedded_in_content( $content, array( 'audio', 'video' ) );
609
		$this->assertEquals( array( $audio, $video ), $matches1 );
610
611
		$reversed = $video . $audio;
612
		$matches2 = get_media_embedded_in_content( $reversed, array( 'audio', 'video' ) );
613
		$this->assertEquals( array( $video, $audio ), $matches2 );
614
	}
615
616
	/**
617
	 * @ticket 35367
618
	 */
619
	function test_wp_audio_shortcode_with_empty_params() {
620
		$this->assertNull( wp_audio_shortcode( array() ) );
621
	}
622
623
	/**
624
	 * @ticket 35367
625
	 */
626
	function test_wp_audio_shortcode_with_bad_attr() {
627
		$this->assertSame(
628
			'<a class="wp-embedded-audio" href="https://example.com/foo.php">https://example.com/foo.php</a>',
629
			wp_audio_shortcode( array(
630
				'src' => 'https://example.com/foo.php',
631
			) )
632
		);
633
	}
634
635
	/**
636
	 * @ticket 35367
637
	 */
638
	function test_wp_audio_shortcode_attributes() {
639
		$actual = wp_audio_shortcode( array(
640
			'src' => 'https://example.com/foo.mp3',
641
		) );
642
643
		$this->assertContains( 'src="https://example.com/foo.mp3', $actual );
644
		$this->assertNotContains( 'loop', $actual );
645
		$this->assertNotContains( 'autoplay', $actual );
646
		$this->assertContains( 'preload="none"', $actual );
647
		$this->assertContains( 'class="wp-audio-shortcode"', $actual );
648
		$this->assertContains( 'style="width: 100%;"', $actual );
649
650
		$actual = wp_audio_shortcode( array(
651
			'src'      => 'https://example.com/foo.mp3',
652
			'loop'     => true,
653
			'autoplay' => true,
654
			'preload'  => true,
655
			'class'    => 'foobar',
656
			'style'    => 'padding:0;',
657
		) );
658
659
		$this->assertContains( 'src="https://example.com/foo.mp3', $actual );
660
		$this->assertContains( 'loop="1"', $actual );
661
		$this->assertContains( 'autoplay="1"', $actual );
662
		$this->assertContains( 'preload="1"', $actual );
663
		$this->assertContains( 'class="foobar"', $actual );
664
		$this->assertContains( 'style="padding:0;"', $actual );
665
	}
666
667
	/**
668
	 * @ticket  35367
669
	 * @depends test_video_shortcode_body
670
	 */
671
	function test_wp_video_shortcode_with_empty_params() {
672
		$this->assertNull( wp_video_shortcode( array() ) );
673
	}
674
675
	/**
676
	 * @ticket  35367
677
	 * @depends test_video_shortcode_body
678
	 */
679
	function test_wp_video_shortcode_with_bad_attr() {
680
		$this->assertSame(
681
			'<a class="wp-embedded-video" href="https://example.com/foo.php">https://example.com/foo.php</a>',
682
			wp_video_shortcode( array(
683
				'src' => 'https://example.com/foo.php',
684
			) )
685
		);
686
	}
687
688
	/**
689
	 * @ticket  35367
690
	 * @depends test_video_shortcode_body
691
	 */
692
	function test_wp_video_shortcode_attributes() {
693
		$actual = wp_video_shortcode( array(
694
			'src' => 'https://example.com/foo.mp4',
695
		) );
696
697
		$this->assertContains( 'src="https://example.com/foo.mp4', $actual );
698
		$this->assertNotContains( 'loop', $actual );
699
		$this->assertNotContains( 'autoplay', $actual );
700
		$this->assertContains( 'preload="metadata"', $actual );
701
		$this->assertContains( 'width="640"', $actual );
702
		$this->assertContains( 'height="360"', $actual );
703
		$this->assertContains( 'class="wp-video-shortcode"', $actual );
704
705
		$actual = wp_video_shortcode( array(
706
			'src'      => 'https://example.com/foo.mp4',
707
			'poster'   => 'https://example.com/foo.png',
708
			'loop'     => true,
709
			'autoplay' => true,
710
			'preload'  => true,
711
			'width'    => 123,
712
			'height'   => 456,
713
			'class'    => 'foobar',
714
		) );
715
716
		$this->assertContains( 'src="https://example.com/foo.mp4', $actual );
717
		$this->assertContains( 'poster="https://example.com/foo.png', $actual );
718
		$this->assertContains( 'loop="1"', $actual );
719
		$this->assertContains( 'autoplay="1"', $actual );
720
		$this->assertContains( 'preload="1"', $actual );
721
		$this->assertContains( 'width="123"', $actual );
722
		$this->assertContains( 'height="456"', $actual );
723
		$this->assertContains( 'class="foobar"', $actual );
724
	}
725
726
	/**
727
	 * @ticket 40866
728
	 * @depends test_video_shortcode_body
729
	 */
730
	function test_wp_video_shortcode_youtube_remove_feature() {
731
		$actual = wp_video_shortcode( array(
732
			'src' => 'https://www.youtube.com/watch?v=i_cVJgIz_Cs&feature=youtu.be',
733
		) );
734
735
		$this->assertNotContains( 'feature=youtu.be', $actual );
736
	}
737
738
	/**
739
	 * @ticket 40866
740
	 * @depends test_video_shortcode_body
741
	 */
742
	function test_wp_video_shortcode_youtube_force_ssl() {
743
		$actual = wp_video_shortcode( array(
744
			'src' => 'http://www.youtube.com/watch?v=i_cVJgIz_Cs',
745
		) );
746
747
		$this->assertContains( 'src="https://www.youtube.com/watch?v=i_cVJgIz_Cs', $actual );
748
	}
749
750
	/**
751
	 * @ticket 40866
752
	 * @depends test_video_shortcode_body
753
	 */
754
	function test_wp_video_shortcode_vimeo_force_ssl_remove_query_args() {
755
		$actual = wp_video_shortcode( array(
756
			'src' => 'http://vimeo.com/190372437?blah=meh',
757
		) );
758
759
		$this->assertContains( 'src="https://vimeo.com/190372437', $actual );
760
		$this->assertNotContains( 'blah=meh', $actual );
761
	}
762
763
	/**
764
	 * @ticket 40977
765
	 * @depends test_video_shortcode_body
766
	 */
767
	function test_wp_video_shortcode_vimeo_adds_loop() {
768
		$actual = wp_video_shortcode( array(
769
			'src' => 'http://vimeo.com/190372437',
770
		) );
771
772
		$this->assertContains( 'src="https://vimeo.com/190372437?loop=0', $actual );
773
	}
774
775
	/**
776
	 * @ticket 40977
777
	 * @depends test_video_shortcode_body
778
	 */
779
	function test_wp_video_shortcode_vimeo_force_adds_loop_true() {
780
		$actual = wp_video_shortcode( array(
781
			'src' => 'http://vimeo.com/190372437',
782
			'loop' => true,
783
		) );
784
785
		$this->assertContains( 'src="https://vimeo.com/190372437?loop=1', $actual );
786
	}
787
788
	/**
789
	 * Test [video] shortcode processing
790
	 *
791
	 */
792
	function test_video_shortcode_body() {
793
		$width = 720;
794
		$height = 480;
795
796
		$w = empty( $GLOBALS['content_width'] ) ? 640 : $GLOBALS['content_width'];
797
		if ( $width > $w ) {
798
			$width = $w;
799
		}
800
801
		$post_id = get_post() ? get_the_ID() : 0;
802
803
		$video =<<<VIDEO
804
[video width="$width" height="480" mp4="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4"]
805
<!-- WebM/VP8 for Firefox4, Opera, and Chrome -->
806
<source type="video/webm" src="myvideo.webm" />
807
<!-- Ogg/Vorbis for older Firefox and Opera versions -->
808
<source type="video/ogg" src="myvideo.ogv" />
809
<!-- Optional: Add subtitles for each language -->
810
<track kind="subtitles" src="subtitles.srt" srclang="en" />
811
<!-- Optional: Add chapters -->
812
<track kind="chapters" src="chapters.srt" srclang="en" />
813
[/video]
814
VIDEO;
815
816
817
		$h = ceil( ( $height * $width ) / $width );
818
819
		$content = apply_filters( 'the_content', $video );
820
821
		$expected = '<div style="width: ' . $width . 'px;" class="wp-video">' .
822
			"<!--[if lt IE 9]><script>document.createElement('video');</script><![endif]-->\n" .
823
			'<video class="wp-video-shortcode" id="video-' . $post_id . '-1" width="' . $width . '" height="' . $h . '" preload="metadata" controls="controls">' .
824
			'<source type="video/mp4" src="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4?_=1" />' .
825
			'<!-- WebM/VP8 for Firefox4, Opera, and Chrome --><source type="video/webm" src="myvideo.webm" />' .
826
			'<!-- Ogg/Vorbis for older Firefox and Opera versions --><source type="video/ogg" src="myvideo.ogv" />' .
827
			'<!-- Optional: Add subtitles for each language --><track kind="subtitles" src="subtitles.srt" srclang="en" />' .
828
			'<!-- Optional: Add chapters --><track kind="chapters" src="chapters.srt" srclang="en" />' .
829
			'<a href="http://domain.tld/wp-content/uploads/2013/12/xyz.mp4">' .
830
			"http://domain.tld/wp-content/uploads/2013/12/xyz.mp4</a></video></div>\n";
831
832
		$this->assertEquals( $expected, $content );
833
	}
834
835
	/**
836
	 * @ticket 26768
837
	 */
838
	function test_add_image_size() {
839
		$_wp_additional_image_sizes = wp_get_additional_image_sizes();
840
841
		remove_image_size( 'test-size' );
842
843
		$this->assertArrayNotHasKey( 'test-size', $_wp_additional_image_sizes );
844
		add_image_size( 'test-size', 200, 600 );
845
846
		$sizes = wp_get_additional_image_sizes();
847
848
		// Clean up
849
		remove_image_size( 'test-size' );
850
851
		$this->assertArrayHasKey( 'test-size', $sizes );
852
		$this->assertEquals( 200, $sizes['test-size']['width'] );
853
		$this->assertEquals( 600, $sizes['test-size']['height'] );
854
	}
855
856
	/**
857
	 * @ticket 26768
858
	 */
859
	function test_remove_image_size() {
860
		add_image_size( 'test-size', 200, 600 );
861
		$this->assertTrue( has_image_size( 'test-size' ) );
862
		remove_image_size( 'test-size' );
863
		$this->assertFalse( has_image_size( 'test-size' ) );
864
	}
865
866
	/**
867
	 * @ticket 26951
868
	 */
869
	function test_has_image_size() {
870
		add_image_size( 'test-size', 200, 600 );
871
		$this->assertTrue( has_image_size( 'test-size' ) );
872
873
		// Clean up
874
		remove_image_size( 'test-size' );
875
	}
876
877
	/**
878
	 * @ticket 30346
879
	 */
880
	function test_attachment_url_to_postid() {
881
		$image_path = '2014/11/' . $this->img_name;
882
		$attachment_id = self::factory()->attachment->create_object( $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...
883
			'post_mime_type' => 'image/jpeg',
884
			'post_type'      => 'attachment',
885
		) );
886
887
		$image_url  = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_path;
888
		$this->assertEquals( $attachment_id, attachment_url_to_postid( $image_url ) );
889
	}
890
891
	function test_attachment_url_to_postid_schemes() {
892
		$image_path = '2014/11/' . $this->img_name;
893
		$attachment_id = self::factory()->attachment->create_object( $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...
894
			'post_mime_type' => 'image/jpeg',
895
			'post_type'      => 'attachment',
896
		) );
897
898
		/**
899
		 * @ticket 33109 Testing protocols not matching
900
		 */
901
		$image_url  = 'https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_path;
902
		$this->assertEquals( $attachment_id, attachment_url_to_postid( $image_url ) );
903
	}
904
905
	function test_attachment_url_to_postid_filtered() {
906
		$image_path = '2014/11/' . $this->img_name;
907
		$attachment_id = self::factory()->attachment->create_object( $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...
908
			'post_mime_type' => 'image/jpeg',
909
			'post_type'      => 'attachment',
910
		) );
911
912
		add_filter( 'upload_dir', array( $this, '_upload_dir' ) );
913
		$image_url = 'http://192.168.1.20.com/wp-content/uploads/' . $image_path;
914
		$this->assertEquals( $attachment_id, attachment_url_to_postid( $image_url ) );
915
		remove_filter( 'upload_dir', array( $this, '_upload_dir' ) );
916
	}
917
918
	function _upload_dir( $dir ) {
919
		$dir['baseurl'] = 'http://192.168.1.20.com/wp-content/uploads';
920
		return $dir;
921
	}
922
923
	/**
924
	 * @ticket 31044
925
	 */
926
	function test_attachment_url_to_postid_with_empty_url() {
927
		$post_id = attachment_url_to_postid( '' );
928
		$this->assertInternalType( 'int', $post_id );
929
		$this->assertEquals( 0, $post_id );
930
	}
931
932
	/**
933
	 * @ticket 22768
934
	 */
935
	public function test_media_handle_upload_sets_post_excerpt() {
936
		$iptc_file = DIR_TESTDATA . '/images/test-image-iptc.jpg';
937
938
		// Make a copy of this file as it gets moved during the file upload
939
		$tmp_name = wp_tempnam( $iptc_file );
940
941
		copy( $iptc_file, $tmp_name );
942
943
		$_FILES['upload'] = array(
944
			'tmp_name' => $tmp_name,
945
			'name'     => 'test-image-iptc.jpg',
946
			'type'     => 'image/jpeg',
947
			'error'    => 0,
948
			'size'     => filesize( $iptc_file )
949
		);
950
951
		$post_id = media_handle_upload( 'upload', 0, array(), array( 'action' => 'test_iptc_upload', 'test_form' => false ) );
952
953
		unset( $_FILES['upload'] );
954
955
		$post = get_post( $post_id );
956
957
		// Clean up.
958
		wp_delete_attachment( $post_id );
959
960
		$this->assertEquals( 'This is a comment. / Это комментарий. / Βλέπετε ένα σχόλιο.', $post->post_excerpt );
961
	}
962
963
	/**
964
	 * @ticket 37989
965
	 */
966
	public function test_media_handle_upload_expected_titles() {
967
		$test_file = DIR_TESTDATA . '/images/test-image.jpg';
968
969
		// Make a copy of this file as it gets moved during the file upload
970
		$tmp_name = wp_tempnam( $test_file );
971
972
		copy( $test_file, $tmp_name );
973
974
		$_FILES['upload'] = array(
975
			'tmp_name' => $tmp_name,
976
			'name'     => 'This is a test.jpg',
977
			'type'     => 'image/jpeg',
978
			'error'    => 0,
979
			'size'     => filesize( $test_file ),
980
		);
981
982
		$post_id = media_handle_upload( 'upload', 0, array(), array( 'action' => 'test_upload_titles', 'test_form' => false ) );
983
984
		unset( $_FILES['upload'] );
985
986
		$post = get_post( $post_id );
987
988
		// Clean up.
989
		wp_delete_attachment( $post_id );
990
991
		$this->assertEquals( 'This is a test', $post->post_title );
992
	}
993
994
	/**
995
	 * @ticket 33016
996
	 */
997
	function test_multiline_cdata() {
998
		global $wp_embed;
999
1000
		$content = <<<EOF
1001
<script>// <![CDATA[
1002
_my_function('data');
1003
// ]]>
1004
</script>
1005
EOF;
1006
1007
		$result = $wp_embed->autoembed( $content );
1008
		$this->assertEquals( $content, $result );
1009
	}
1010
1011
	/**
1012
	 * @ticket 33016
1013
	 */
1014
	function test_multiline_comment() {
1015
		global $wp_embed;
1016
1017
		$content = <<<EOF
1018
<script><!--
1019
my_function();
1020
// --> </script>
1021
EOF;
1022
1023
		$result = $wp_embed->autoembed( $content );
1024
		$this->assertEquals( $content, $result );
1025
	}
1026
1027
1028
	/**
1029
	 * @ticket 33016
1030
	 */
1031
	function test_multiline_comment_with_embeds() {
1032
		$content = <<<EOF
1033
Start.
1034
[embed]http://www.youtube.com/embed/TEST01YRHA0[/embed]
1035
<script><!--
1036
my_function();
1037
// --> </script>
1038
http://www.youtube.com/embed/TEST02YRHA0
1039
[embed]http://www.example.com/embed/TEST03YRHA0[/embed]
1040
http://www.example.com/embed/TEST04YRHA0
1041
Stop.
1042
EOF;
1043
1044
		$expected = <<<EOF
1045
<p>Start.<br />
1046
https://youtube.com/watch?v=TEST01YRHA0<br />
1047
<script><!--
1048
my_function();
1049
// --> </script><br />
1050
https://youtube.com/watch?v=TEST02YRHA0<br />
1051
<a href="http://www.example.com/embed/TEST03YRHA0">http://www.example.com/embed/TEST03YRHA0</a><br />
1052
http://www.example.com/embed/TEST04YRHA0<br />
1053
Stop.</p>
1054
1055
EOF;
1056
1057
		$result = apply_filters( 'the_content', $content );
1058
		$this->assertEquals( $expected, $result );
1059
	}
1060
1061
	/**
1062
	 * @ticket 33016
1063
	 */
1064
	function filter_wp_embed_shortcode_custom( $content, $url ) {
1065
		if ( 'https://www.example.com/?video=1' == $url ) {
1066
			$content = '@embed URL was replaced@';
1067
		}
1068
		return $content;
1069
	}
1070
1071
	/**
1072
	 * @ticket 33016
1073
	 */
1074
	function test_oembed_explicit_media_link() {
1075
		global $wp_embed;
1076
		add_filter( 'embed_maybe_make_link', array( $this, 'filter_wp_embed_shortcode_custom' ), 10, 2 );
1077
1078
		$content = <<<EOF
1079
https://www.example.com/?video=1
1080
EOF;
1081
1082
		$expected = <<<EOF
1083
@embed URL was replaced@
1084
EOF;
1085
1086
		$result = $wp_embed->autoembed( $content );
1087
		$this->assertEquals( $expected, $result );
1088
1089
		$content = <<<EOF
1090
<a href="https://www.example.com/?video=1">https://www.example.com/?video=1</a>
1091
<script>// <![CDATA[
1092
_my_function('data');
1093
myvar = 'Hello world
1094
https://www.example.com/?video=1
1095
do not break this';
1096
// ]]>
1097
</script>
1098
EOF;
1099
1100
		$result = $wp_embed->autoembed( $content );
1101
		$this->assertEquals( $content, $result );
1102
1103
		remove_filter( 'embed_maybe_make_link', array( $this, 'filter_wp_embed_shortcode_custom' ), 10 );
1104
	}
1105
1106
	/**
1107
	 * Tests the default output of `wp_get_attachment_image()`.
1108
	 * @ticket 34635
1109
	 */
1110
	function test_wp_get_attachment_image_defaults() {
1111
		$image = image_downsize( self::$large_id, 'thumbnail' );
1112
		$expected = sprintf( '<img width="%1$d" height="%2$d" src="%3$s" class="attachment-thumbnail size-thumbnail" alt="" />', $image[1], $image[2], $image[0] );
1113
1114
		$this->assertEquals( $expected, wp_get_attachment_image( self::$large_id ) );
1115
	}
1116
1117
	/**
1118
	 * Test that `wp_get_attachment_image()` returns a proper alt value.
1119
	 * @ticket 34635
1120
	 */
1121
	function test_wp_get_attachment_image_with_alt() {
1122
		// Add test alt metadata.
1123
		update_post_meta( self::$large_id, '_wp_attachment_image_alt', 'Some very clever alt text', true );
1124
1125
		$image = image_downsize( self::$large_id, 'thumbnail' );
1126
		$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] );
1127
1128
		$this->assertEquals( $expected, wp_get_attachment_image( self::$large_id ) );
1129
1130
		// Cleanup.
1131
		update_post_meta( self::$large_id, '_wp_attachment_image_alt', '', true );
1132
	}
1133
1134
	/**
1135
	 * @ticket 33878
1136
	 */
1137
	function test_wp_get_attachment_image_url() {
1138
		$this->assertFalse( wp_get_attachment_image_url( 0 ) );
1139
1140
		$post_id = self::factory()->post->create();
1141
		$attachment_id = self::factory()->attachment->create_object( $this->img_name, $post_id, array(
1142
			'post_mime_type' => 'image/jpeg',
1143
			'post_type' => 'attachment',
1144
		) );
1145
1146
		$image = wp_get_attachment_image_src( $attachment_id, 'thumbnail', false );
1147
1148
		$this->assertEquals( $image[0], wp_get_attachment_image_url( $attachment_id ) );
1149
	}
1150
1151
	/**
1152
	 * @ticket 12235
1153
	 */
1154
	function test_wp_get_attachment_caption() {
1155
		$this->assertFalse( wp_get_attachment_caption( 0 ) );
1156
1157
		$caption = 'This is a caption.';
1158
1159
		$post_id = self::factory()->post->create();
1160
		$attachment_id = self::factory()->attachment->create_object( $this->img_name, $post_id, array(
1161
			'post_mime_type' => 'image/jpeg',
1162
			'post_type'      => 'attachment',
1163
			'post_excerpt'   => $caption,
1164
		) );
1165
1166
		$this->assertFalse( wp_get_attachment_caption( $post_id ) );
1167
1168
		$this->assertEquals( $caption, wp_get_attachment_caption( $attachment_id ) );
1169
	}
1170
1171
	/**
1172
	 * @ticket 12235
1173
	 */
1174
	function test_wp_get_attachment_caption_empty() {
1175
		$post_id = self::factory()->post->create();
1176
		$attachment_id = self::factory()->attachment->create_object( $this->img_name, $post_id, array(
1177
			'post_mime_type' => 'image/jpeg',
1178
			'post_type'      => 'attachment',
1179
			'post_excerpt'   => '',
1180
		) );
1181
1182
		$this->assertEquals( '', wp_get_attachment_caption( $attachment_id ) );
1183
	}
1184
1185
	/**
1186
	 * Helper function to get image size array from size "name"
1187
	 */
1188
	function _get_image_size_array_from_meta( $image_meta, $size_name ) {
1189
		$array = false;
1190
1191
		if ( is_array( $image_meta ) ) {
1192
			if ( 'full' === $size_name && isset( $image_meta['width'] ) && isset( $image_meta['height'] ) ) {
1193
				$array = array( $image_meta['width'], $image_meta['height'] );
1194
			} elseif ( isset( $image_meta['sizes'][ $size_name ]['width'] ) && isset( $image_meta['sizes'][ $size_name ]['height'] ) ) {
1195
				$array = array( $image_meta['sizes'][ $size_name ]['width'], $image_meta['sizes'][ $size_name ]['height'] );
1196
			}
1197
		}
1198
1199
		return $array;
1200
	}
1201
1202
	/**
1203
	 * Helper function to move the src image to the first position in the expected srcset string.
1204
	 */
1205
	function _src_first( $srcset, $src_url, $src_width ) {
1206
		$src_string = $src_url . ' ' . $src_width . 'w';
1207
		$src_not_first = ', ' . $src_string;
1208
1209
		if ( strpos( $srcset, $src_not_first ) ) {
1210
			$srcset = str_replace( $src_not_first, '', $srcset );
1211
			$srcset = $src_string . ', ' . $srcset;
1212
		}
1213
1214
		return $srcset;
1215
	}
1216
1217
	/**
1218
	 * @ticket 33641
1219
	 */
1220
	function test_wp_calculate_image_srcset() {
1221
		$_wp_additional_image_sizes = wp_get_additional_image_sizes();
1222
1223
		$year_month = date('Y/m');
1224
		$image_meta = wp_get_attachment_metadata( self::$large_id );
1225
		$uploads_dir_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/';
1226
1227
		// Set up test cases for all expected size names.
1228
		$intermediates = array( 'medium', 'medium_large', 'large', 'full' );
1229
1230
		// Add any soft crop intermediate sizes.
1231
		foreach ( $_wp_additional_image_sizes as $name => $additional_size ) {
1232
			if ( ! $_wp_additional_image_sizes[$name]['crop'] || 0 === $_wp_additional_image_sizes[$name]['height'] ) {
1233
				$intermediates[] = $name;
1234
			}
1235
		}
1236
1237
		$expected = '';
1238
1239
		foreach ( $image_meta['sizes'] as $name => $size ) {
1240
			// Whitelist the sizes that should be included so we pick up 'medium_large' in 4.4.
1241
			if ( in_array( $name, $intermediates ) ) {
1242
				$expected .= $uploads_dir_url . $year_month . '/' . $size['file'] . ' ' . $size['width'] . 'w, ';
1243
			}
1244
		}
1245
1246
		// Add the full size width at the end.
1247
		$expected .= $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] .'w';
1248
1249
		foreach ( $intermediates as $int ) {
1250
			$image_url = wp_get_attachment_image_url( self::$large_id, $int );
1251
			$size_array = $this->_get_image_size_array_from_meta( $image_meta, $int );
1252
			$expected_srcset = $this->_src_first( $expected, $image_url, $size_array[0] );
1253
			$this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) );
0 ignored issues
show
Security Bug introduced by
It seems like $size_array defined by $this->_get_image_size_a...meta($image_meta, $int) on line 1251 can also be of type false; however, wp_calculate_image_srcset() does only seem to accept array, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
Security Bug introduced by
It seems like $image_url defined by wp_get_attachment_image_url(self::$large_id, $int) on line 1250 can also be of type false; however, wp_calculate_image_srcset() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
1254
		}
1255
	}
1256
1257
	/**
1258
	 * @ticket 33641
1259
	 */
1260
	function test_wp_calculate_image_srcset_no_date_uploads() {
1261
		$_wp_additional_image_sizes = wp_get_additional_image_sizes();
1262
1263
		// Disable date organized uploads
1264
		add_filter( 'upload_dir', '_upload_dir_no_subdir' );
1265
1266
		// Make an image.
1267
		$filename = DIR_TESTDATA . '/images/test-image-large.png';
1268
		$id = self::factory()->attachment->create_upload_object( $filename );
1269
1270
		$image_meta = wp_get_attachment_metadata( $id );
1271
		$uploads_dir_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/';
1272
1273
		// Set up test cases for all expected size names.
1274
		$intermediates = array( 'medium', 'medium_large', 'large', 'full' );
1275
1276
		foreach ( $_wp_additional_image_sizes as $name => $additional_size ) {
1277
			if ( ! $_wp_additional_image_sizes[$name]['crop'] || 0 === $_wp_additional_image_sizes[$name]['height'] ) {
1278
				$intermediates[] = $name;
1279
			}
1280
		}
1281
1282
		$expected = '';
1283
1284
		foreach ( $image_meta['sizes'] as $name => $size ) {
1285
			// Whitelist the sizes that should be included so we pick up 'medium_large' in 4.4.
1286
			if ( in_array( $name, $intermediates ) ) {
1287
				$expected .= $uploads_dir_url . $size['file'] . ' ' . $size['width'] . 'w, ';
1288
			}
1289
		}
1290
1291
		// Add the full size width at the end.
1292
		$expected .= $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] .'w';
1293
1294
		foreach ( $intermediates as $int ) {
1295
			$size_array = $this->_get_image_size_array_from_meta( $image_meta, $int );
1296
			$image_url = wp_get_attachment_image_url( $id, $int );
1297
			$expected_srcset = $this->_src_first( $expected, $image_url, $size_array[0] );
1298
			$this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) );
0 ignored issues
show
Security Bug introduced by
It seems like $size_array defined by $this->_get_image_size_a...meta($image_meta, $int) on line 1295 can also be of type false; however, wp_calculate_image_srcset() does only seem to accept array, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
Security Bug introduced by
It seems like $image_url defined by wp_get_attachment_image_url($id, $int) on line 1296 can also be of type false; however, wp_calculate_image_srcset() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
1299
		}
1300
1301
		// Remove the attachment
1302
		wp_delete_attachment( $id );
1303
		remove_filter( 'upload_dir', '_upload_dir_no_subdir' );
1304
	}
1305
1306
	/**
1307
	 * @ticket 33641
1308
	 */
1309
	function test_wp_calculate_image_srcset_with_edits() {
1310
		// For this test we're going to mock metadata changes from an edit.
1311
		// Start by getting the attachment metadata.
1312
		$image_meta = wp_get_attachment_metadata( self::$large_id );
1313
		$image_url = wp_get_attachment_image_url( self::$large_id, 'medium' );
1314
		$size_array = $this->_get_image_size_array_from_meta( $image_meta, 'medium' );
1315
1316
		// Copy hash generation method used in wp_save_image().
1317
		$hash = 'e' . time() . rand(100, 999);
1318
1319
		$filename_base = basename( $image_meta['file'], '.png' );
1320
1321
		// Add the hash to the image URL
1322
		$image_url = str_replace( $filename_base, $filename_base . '-' . $hash, $image_url );
1323
1324
		// Replace file paths for full and medium sizes with hashed versions.
1325
		$image_meta['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['file'] );
1326
		$image_meta['sizes']['medium']['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['sizes']['medium']['file'] );
1327
		$image_meta['sizes']['medium_large']['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['sizes']['medium_large']['file'] );
1328
		$image_meta['sizes']['large']['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['sizes']['large']['file'] );
1329
1330
		// Calculate a srcset array.
1331
		$sizes = explode( ', ', wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) );
0 ignored issues
show
Security Bug introduced by
It seems like $size_array defined by $this->_get_image_size_a...($image_meta, 'medium') on line 1314 can also be of type false; however, wp_calculate_image_srcset() does only seem to accept array, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
1332
1333
		// Test to confirm all sources in the array include the same edit hash.
1334
		foreach ( $sizes as $size ) {
1335
			$this->assertNotFalse( strpos( $size, $hash ) );
1336
		}
1337
	}
1338
1339
	/**
1340
	 * @ticket 35106
1341
	 */
1342
	function test_wp_calculate_image_srcset_with_absolute_path_in_meta() {
1343
		$_wp_additional_image_sizes = wp_get_additional_image_sizes();
1344
1345
		$year_month = date('Y/m');
1346
		$image_meta = wp_get_attachment_metadata( self::$large_id );
1347
		$uploads_dir_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/';
1348
1349
		// Set up test cases for all expected size names.
1350
		$intermediates = array( 'medium', 'medium_large', 'large', 'full' );
1351
1352
		// Add any soft crop intermediate sizes.
1353
		foreach ( $_wp_additional_image_sizes as $name => $additional_size ) {
1354
			if ( ! $_wp_additional_image_sizes[$name]['crop'] || 0 === $_wp_additional_image_sizes[$name]['height'] ) {
1355
				$intermediates[] = $name;
1356
			}
1357
		}
1358
1359
		$expected = '';
1360
1361
		foreach( $image_meta['sizes'] as $name => $size ) {
1362
			// Whitelist the sizes that should be included so we pick up 'medium_large' in 4.4.
1363
			if ( in_array( $name, $intermediates ) ) {
1364
				$expected .= $uploads_dir_url . $year_month . '/' . $size['file'] . ' ' . $size['width'] . 'w, ';
1365
			}
1366
		}
1367
1368
		// Add the full size width at the end.
1369
		$expected .= $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] .'w';
1370
1371
		// Prepend an absolute path to simulate a pre-2.7 upload
1372
		$image_meta['file'] = 'H:\home\wordpress\trunk/wp-content/uploads/' . $image_meta['file'];
1373
1374
		foreach ( $intermediates as $int ) {
1375
			$image_url = wp_get_attachment_image_url( self::$large_id, $int );
1376
			$size_array = $this->_get_image_size_array_from_meta( $image_meta, $int );
1377
			$expected_srcset = $this->_src_first( $expected, $image_url, $size_array[0] );
1378
			$this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) );
0 ignored issues
show
Security Bug introduced by
It seems like $size_array defined by $this->_get_image_size_a...meta($image_meta, $int) on line 1376 can also be of type false; however, wp_calculate_image_srcset() does only seem to accept array, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
Security Bug introduced by
It seems like $image_url defined by wp_get_attachment_image_url(self::$large_id, $int) on line 1375 can also be of type false; however, wp_calculate_image_srcset() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
1379
		}
1380
	}
1381
1382
	/**
1383
	 * @ticket 33641
1384
	 */
1385
	function test_wp_calculate_image_srcset_false() {
1386
		$sizes = wp_calculate_image_srcset( array( 400, 300 ), 'file.png', array() );
1387
1388
		// For canola.jpg we should return
1389
		$this->assertFalse( $sizes );
1390
	}
1391
1392
	/**
1393
	 * @ticket 33641
1394
	 */
1395
	function test_wp_calculate_image_srcset_no_width() {
1396
		$file = get_attached_file( self::$large_id );
1397
		$image_url = wp_get_attachment_image_url( self::$large_id, 'medium' );
1398
		$image_meta = wp_generate_attachment_metadata( self::$large_id, $file );
0 ignored issues
show
Security Bug introduced by
It seems like $file defined by get_attached_file(self::$large_id) on line 1396 can also be of type false; however, wp_generate_attachment_metadata() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
1399
1400
		$size_array = array(0, 0);
1401
1402
		$srcset = wp_calculate_image_srcset( $size_array, $image_url, $image_meta );
0 ignored issues
show
Security Bug introduced by
It seems like $image_url defined by wp_get_attachment_image_...f::$large_id, 'medium') on line 1397 can also be of type false; however, wp_calculate_image_srcset() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
1403
1404
		// The srcset should be false.
1405
		$this->assertFalse( $srcset );
1406
	}
1407
1408
	/**
1409
	 * @ticket 34955
1410
	 * @ticket 33641
1411
	 */
1412
	function test_wp_calculate_image_srcset_ratio_variance() {
1413
		// Mock data for this test.
1414
		$size_array = array( 218, 300 );
1415
		$image_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-768x1055-218x300.png';
1416
		$image_meta = array(
1417
			'width' => 768,
1418
			'height' => 1055,
1419
			'file' => '2015/12/test-768x1055.png',
1420
			'sizes' => array(
1421
				'thumbnail' => array(
1422
					'file' => 'test-768x1055-150x150.png',
1423
					'width' => 150,
1424
					'height' => 150,
1425
					'mime-type' => 'image/png',
1426
				),
1427
				'medium' => array(
1428
					'file' => 'test-768x1055-218x300.png',
1429
					'width' => 218,
1430
					'height' => 300,
1431
					'mime-type' => 'image/png',
1432
				),
1433
				'custom-600' => array(
1434
					'file' => 'test-768x1055-600x824.png',
1435
					'width' => 600,
1436
					'height' => 824,
1437
					'mime-type' => 'image/png',
1438
				),
1439
				'post-thumbnail' => array(
1440
					'file' => 'test-768x1055-768x510.png',
1441
					'width' => 768,
1442
					'height' => 510,
1443
					'mime-type' => 'image/png',
1444
				),
1445
			),
1446
		);
1447
1448
		$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';
1449
1450
		$this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_src, $image_meta ) );
1451
	}
1452
1453
	/**
1454
	 * @ticket 35108
1455
	 * @ticket 33641
1456
	 */
1457
	function test_wp_calculate_image_srcset_include_src() {
1458
		// Mock data for this test.
1459
		$size_array = array( 2000, 1000 );
1460
		$image_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test.png';
1461
		$image_meta = array(
1462
			'width' => 2000,
1463
			'height' => 1000,
1464
			'file' => '2015/12/test.png',
1465
			'sizes' => array(
1466
				'thumbnail' => array(
1467
					'file' => 'test-150x150.png',
1468
					'width' => 150,
1469
					'height' => 150,
1470
					'mime-type' => 'image/png',
1471
				),
1472
				'medium' => array(
1473
					'file' => 'test-300x150.png',
1474
					'width' => 300,
1475
					'height' => 150,
1476
					'mime-type' => 'image/png',
1477
				),
1478
				'medium_large' => array(
1479
					'file' => 'test-768x384.png',
1480
					'width' => 768,
1481
					'height' => 384,
1482
					'mime-type' => 'image/png',
1483
				),
1484
				'large' => array(
1485
					'file' => 'test-1024x512.png',
1486
					'width' => 1024,
1487
					'height' => 512,
1488
					'mime-type' => 'image/png',
1489
				),
1490
			),
1491
		);
1492
1493
		$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';
1494
1495
		$this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_src, $image_meta ) );
1496
	}
1497
1498
	/**
1499
	 * @ticket 35480
1500
	 */
1501
	function test_wp_calculate_image_srcset_corrupted_image_meta() {
1502
		$size_array = array( 300, 150 );
1503
		$image_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-300x150.png';
1504
		$image_meta = array(
1505
			'width' => 1600,
1506
			'height' => 800,
1507
			'file' => '2015/12/test.png',
1508
			'sizes' => array(
1509
				'thumbnail' => array(
1510
					'file' => 'test-150x150.png',
1511
					'width' => 150,
1512
					'height' => 150,
1513
					'mime-type' => 'image/png',
1514
				),
1515
				'medium' => array(
1516
					'file' => 'test-300x150.png',
1517
					'width' => 300,
1518
					'height' => 150,
1519
					'mime-type' => 'image/png',
1520
				),
1521
				'medium_large' => array(
1522
					'file' => 'test-768x384.png',
1523
					'width' => 768,
1524
					'height' => 384,
1525
					'mime-type' => 'image/png',
1526
				),
1527
				'large' => array(
1528
					'file' => 'test-1024x512.png',
1529
					'width' => 1024,
1530
					'height' => 512,
1531
					'mime-type' => 'image/png',
1532
				),
1533
			),
1534
		);
1535
1536
		$srcset = array(
1537
			300  => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-300x150.png 300w',
1538
			768  => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-768x384.png 768w',
1539
			1024 => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-1024x512.png 1024w',
1540
			1600 => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test.png 1600w',
1541
		);
1542
1543
		// No sizes array
1544
		$image_meta1 = $image_meta;
1545
		unset( $image_meta1['sizes'] );
1546
		$this->assertFalse( wp_calculate_image_srcset( $size_array, $image_src, $image_meta1 ) );
1547
1548
		// Sizes is string instead of array; only full size available means no srcset.
1549
		$image_meta2 = $image_meta;
1550
		$image_meta2['sizes'] = '';
1551
		$this->assertFalse( wp_calculate_image_srcset( $size_array, $image_src, $image_meta2 ) );
1552
1553
		// File name is incorrect
1554
		$image_meta3 = $image_meta;
1555
		$image_meta3['file'] = '/';
1556
		$this->assertFalse( wp_calculate_image_srcset( $size_array, $image_src, $image_meta3 ) );
1557
1558
		// File name is incorrect
1559
		$image_meta4 = $image_meta;
1560
		unset( $image_meta4['file'] );
1561
		$this->assertFalse( wp_calculate_image_srcset( $size_array, $image_src, $image_meta4 ) );
1562
1563
		// Intermediate size is string instead of array.
1564
		$image_meta5 = $image_meta;
1565
		$image_meta5['sizes']['medium_large'] = '';
1566
		unset( $srcset[768] );
1567
		$expected_srcset = implode( ', ', $srcset );
1568
		$this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_src, $image_meta5 ) );
1569
	}
1570
1571
	/**
1572
	 * @ticket 36549
1573
	 * @ticket 33641
1574
	 */
1575
	function test_wp_calculate_image_srcset_with_spaces_in_filenames() {
1576
		// Mock data for this test.
1577
		$image_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test image-300x150.png';
1578
		$image_meta = array(
1579
			'width' => 2000,
1580
			'height' => 1000,
1581
			'file' => '2015/12/test image.png',
1582
			'sizes' => array(
1583
				'thumbnail' => array(
1584
					'file' => 'test image-150x150.png',
1585
					'width' => 150,
1586
					'height' => 150,
1587
					'mime-type' => 'image/png',
1588
				),
1589
				'medium' => array(
1590
					'file' => 'test image-300x150.png',
1591
					'width' => 300,
1592
					'height' => 150,
1593
					'mime-type' => 'image/png',
1594
				),
1595
				'medium_large' => array(
1596
					'file' => 'test image-768x384.png',
1597
					'width' => 768,
1598
					'height' => 384,
1599
					'mime-type' => 'image/png',
1600
				),
1601
				'large' => array(
1602
					'file' => 'test image-1024x512.png',
1603
					'width' => 1024,
1604
					'height' => 512,
1605
					'mime-type' => 'image/png',
1606
				),
1607
			),
1608
		);
1609
1610
		$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';
1611
1612
		$this->assertSame( $expected_srcset, wp_calculate_image_srcset( array( 300, 150 ), $image_src, $image_meta ) );
1613
	}
1614
1615
	/**
1616
	 * @ticket 33641
1617
	 */
1618
	function test_wp_get_attachment_image_srcset() {
1619
		$_wp_additional_image_sizes = wp_get_additional_image_sizes();
1620
1621
		$image_meta = wp_get_attachment_metadata( self::$large_id );
1622
		$size_array = array( 1600, 1200 ); // full size
1623
1624
		$srcset = wp_get_attachment_image_srcset( self::$large_id, $size_array, $image_meta );
1625
1626
		$year_month = date('Y/m');
1627
		$uploads_dir = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/';
1628
1629
		// Set up test cases for all expected size names.
1630
		$intermediates = array( 'medium', 'medium_large', 'large', 'full' );
1631
1632
		foreach ( $_wp_additional_image_sizes as $name => $additional_size ) {
1633
			if ( ! $_wp_additional_image_sizes[$name]['crop'] || 0 === $_wp_additional_image_sizes[$name]['height'] ) {
1634
				$intermediates[] = $name;
1635
			}
1636
		}
1637
1638
		$expected = "";
1639
1640
		foreach ( $image_meta['sizes'] as $name => $size ) {
1641
			// Whitelist the sizes that should be included so we pick up 'medium_large' in 4.4.
1642
			if ( in_array( $name, $intermediates ) ) {
1643
				$expected .= $uploads_dir . $year_month . '/' . $size['file'] . ' ' . $size['width'] . 'w, ';
1644
			}
1645
		}
1646
1647
		$expected .= $uploads_dir . $image_meta['file'] . ' ' . $image_meta['width'] .'w';
1648
1649
		$expected_srcset = $this->_src_first( $expected, $uploads_dir . $image_meta['file'], $size_array[0] );
1650
1651
		$this->assertSame( $expected_srcset, $srcset );
1652
	}
1653
1654
	/**
1655
	 * @ticket 33641
1656
	 */
1657
	function test_wp_get_attachment_image_srcset_single_srcset() {
1658
		$image_meta = wp_get_attachment_metadata( self::$large_id );
1659
		$size_array = array( 150, 150 );
1660
		/*
1661
		 * In our tests, thumbnails will only return a single srcset candidate,
1662
		 * so we shouldn't return a srcset value in order to avoid unneeded markup.
1663
		 */
1664
		$sizes = wp_get_attachment_image_srcset( self::$large_id, $size_array, $image_meta );
1665
1666
		$this->assertFalse( $sizes );
1667
	}
1668
1669
	/**
1670
	 * @ticket 33641
1671
	 */
1672
	function test_wp_get_attachment_image_srcset_invalidsize() {
1673
		$image_meta = wp_get_attachment_metadata( self::$large_id );
1674
		$invalid_size = 'nailthumb';
1675
		$original_size = array( 1600, 1200 );
1676
1677
		$srcset = wp_get_attachment_image_srcset( self::$large_id, $invalid_size, $image_meta );
1678
1679
		// Expect a srcset for the original full size image to be returned.
1680
		$expected = wp_get_attachment_image_srcset( self::$large_id, $original_size, $image_meta );
1681
1682
		$this->assertSame( $expected, $srcset );
1683
	}
1684
1685
	/**
1686
	 * @ticket 33641
1687
	 */
1688
	function test_wp_get_attachment_image_sizes() {
1689
		// Test sizes against the default WP sizes.
1690
		$intermediates = array('thumbnail', 'medium', 'medium_large', 'large');
1691
1692
		// Make sure themes aren't filtering the sizes array.
1693
		remove_all_filters( 'wp_calculate_image_sizes' );
1694
1695
		foreach( $intermediates as $int_size ) {
1696
			$image = wp_get_attachment_image_src( self::$large_id, $int_size );
1697
1698
			$expected = '(max-width: ' . $image[1] . 'px) 100vw, ' . $image[1] . 'px';
1699
			$sizes = wp_get_attachment_image_sizes( self::$large_id, $int_size );
1700
1701
			$this->assertSame( $expected, $sizes );
1702
		}
1703
	}
1704
1705
	/**
1706
	 * @ticket 33641
1707
	 */
1708
	function test_wp_calculate_image_sizes() {
1709
		// Test sizes against the default WP sizes.
1710
		$intermediates = array( 'thumbnail', 'medium', 'medium_large', 'large' );
1711
		$image_meta = wp_get_attachment_metadata( self::$large_id );
1712
1713
		// Make sure themes aren't filtering the sizes array.
1714
		remove_all_filters( 'wp_calculate_image_sizes' );
1715
1716
		foreach ( $intermediates as $int_size ) {
1717
			$size_array = $this->_get_image_size_array_from_meta( $image_meta, $int_size );
1718
			$image_src = $image_meta['sizes'][ $int_size ]['file'];
1719
			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...
1720
1721
			$expected = '(max-width: ' . $width . 'px) 100vw, ' . $width . 'px';
1722
			$sizes = wp_calculate_image_sizes( $size_array, $image_src, $image_meta );
0 ignored issues
show
Security Bug introduced by
It seems like $size_array defined by $this->_get_image_size_a...$image_meta, $int_size) on line 1717 can also be of type false; however, wp_calculate_image_sizes() does only seem to accept array|string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
1723
1724
			$this->assertSame( $expected, $sizes );
1725
		}
1726
	}
1727
1728
	/**
1729
	 * @ticket 33641
1730
	 */
1731
	function test_wp_make_content_images_responsive() {
1732
		$image_meta = wp_get_attachment_metadata( self::$large_id );
1733
		$size_array = $this->_get_image_size_array_from_meta( $image_meta, 'medium' );
1734
1735
		$srcset = sprintf( 'srcset="%s"', wp_get_attachment_image_srcset( self::$large_id, $size_array, $image_meta ) );
0 ignored issues
show
Security Bug introduced by
It seems like $size_array defined by $this->_get_image_size_a...($image_meta, 'medium') on line 1733 can also be of type false; however, wp_get_attachment_image_srcset() does only seem to accept string|array, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
1736
		$sizes = sprintf( 'sizes="%s"', wp_get_attachment_image_sizes( self::$large_id, $size_array, $image_meta ) );
0 ignored issues
show
Security Bug introduced by
It seems like $size_array defined by $this->_get_image_size_a...($image_meta, 'medium') on line 1733 can also be of type false; however, wp_get_attachment_image_sizes() does only seem to accept string|array, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
1737
1738
		// Function used to build HTML for the editor.
1739
		$img = get_image_tag( self::$large_id, '', '', '', 'medium' );
1740
		$img_no_size_in_class = str_replace( 'size-', '', $img );
1741
		$img_no_width_height = str_replace( ' width="' . $size_array[0] . '"', '', $img );
1742
		$img_no_width_height = str_replace( ' height="' . $size_array[1] . '"', '', $img_no_width_height );
1743
		$img_no_size_id = str_replace( 'wp-image-', 'id-', $img );
1744
		$img_with_sizes_attr = str_replace( '<img ', '<img sizes="99vw" ', $img );
1745
		$img_xhtml = str_replace( ' />', '/>', $img );
1746
		$img_html5 = str_replace( ' />', '>', $img );
1747
1748
		// Manually add srcset and sizes to the markup from get_image_tag();
1749
		$respimg = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img );
1750
		$respimg_no_size_in_class = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_no_size_in_class );
1751
		$respimg_no_width_height = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_no_width_height );
1752
		$respimg_with_sizes_attr = preg_replace('|<img ([^>]+) />|', '<img $1 ' . $srcset . ' />', $img_with_sizes_attr );
1753
		$respimg_xhtml = preg_replace( '|<img ([^>]+)/>|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_xhtml );
1754
		$respimg_html5 = preg_replace( '|<img ([^>]+)>|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_html5 );
1755
1756
		$content = '
1757
			<p>Image, standard. Should have srcset and sizes.</p>
1758
			%1$s
1759
1760
			<p>Image, no size class. Should have srcset and sizes.</p>
1761
			%2$s
1762
1763
			<p>Image, no width and height attributes. Should have srcset and sizes (from matching the file name).</p>
1764
			%3$s
1765
1766
			<p>Image, no attachment ID class. Should NOT have srcset and sizes.</p>
1767
			%4$s
1768
1769
			<p>Image, with sizes attribute. Should NOT have two sizes attributes.</p>
1770
			%5$s
1771
1772
			<p>Image, XHTML 1.0 style (no space before the closing slash). Should have srcset and sizes.</p>
1773
			%6$s
1774
1775
			<p>Image, HTML 5.0 style. Should have srcset and sizes.</p>
1776
			%7$s';
1777
1778
		$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 );
1779
		$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 );
1780
1781
		$this->assertSame( $content_filtered, wp_make_content_images_responsive( $content_unfiltered ) );
1782
	}
1783
1784
	/**
1785
	 * When rendering attributes for responsive images,
1786
	 * we rely on the 'wp-image-*' class to find the image by ID.
1787
	 * The class name may not be consistent with attachment IDs in DB when
1788
	 * working with imported content or when a user has edited
1789
	 * the 'src' attribute manually. To avoid incorrect images
1790
	 * being displayed, ensure we don't add attributes in this case.
1791
	 *
1792
	 * @ticket 34898
1793
	 * @ticket 33641
1794
	 */
1795
	function test_wp_make_content_images_responsive_wrong() {
1796
		$image = get_image_tag( self::$large_id, '', '', '', 'medium' );
1797
1798
		// Replace the src URL
1799
		$image_wrong_src = preg_replace( '|src="[^"]+"|', 'src="http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/foo.jpg"', $image );
1800
1801
		$this->assertSame( $image_wrong_src, wp_make_content_images_responsive( $image_wrong_src ) );
1802
	}
1803
1804
	/**
1805
	 * @ticket 33641
1806
	 */
1807
	function test_wp_make_content_images_responsive_with_preexisting_srcset() {
1808
		// Generate HTML and add a dummy srcset attribute.
1809
		$image_html = get_image_tag( self::$large_id, '', '', '', 'medium' );
1810
		$image_html = preg_replace('|<img ([^>]+) />|', '<img $1 ' . 'srcset="image2x.jpg 2x" />', $image_html );
1811
1812
		// The content filter should return the image unchanged.
1813
		$this->assertSame( $image_html, wp_make_content_images_responsive( $image_html ) );
1814
	}
1815
1816
	/**
1817
	 * @ticket 33641
1818
	 * @ticket 34528
1819
	 */
1820
	function test_wp_calculate_image_srcset_animated_gifs() {
1821
		// Mock meta for an animated gif.
1822
		$image_meta = array(
1823
			'width' => 1200,
1824
			'height' => 600,
1825
			'file' => 'animated.gif',
1826
			'sizes' => array(
1827
				'thumbnail' => array(
1828
					'file' => 'animated-150x150.gif',
1829
					'width' => 150,
1830
					'height' => 150,
1831
					'mime-type' => 'image/gif'
1832
				),
1833
				'medium' => array(
1834
					'file' => 'animated-300x150.gif',
1835
					'width' => 300,
1836
					'height' => 150,
1837
					'mime-type' => 'image/gif'
1838
				),
1839
				'large' => array(
1840
					'file' => 'animated-1024x512.gif',
1841
					'width' => 1024,
1842
					'height' => 512,
1843
					'mime-type' => 'image/gif'
1844
				),
1845
			)
1846
		);
1847
1848
		$full_src  = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['file'];
1849
		$large_src = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['sizes']['large']['file'];
1850
1851
		// Test with soft resized size array.
1852
		$size_array = array(900, 450);
1853
1854
		// Full size GIFs should not return a srcset.
1855
		$this->assertFalse( wp_calculate_image_srcset( $size_array, $full_src, $image_meta ) );
1856
		// Intermediate sized GIFs should not include the full size in the srcset.
1857
		$this->assertFalse( strpos( wp_calculate_image_srcset( $size_array, $large_src, $image_meta ), $full_src ) );
1858
	}
1859
1860
	/**
1861
	 * @ticket 35045
1862
	 * @ticket 33641
1863
	 */
1864
	function test_wp_make_content_images_responsive_schemes() {
1865
		$image_meta = wp_get_attachment_metadata( self::$large_id );
1866
		$size_array = $this->_get_image_size_array_from_meta( $image_meta, 'medium' );
1867
1868
		$srcset = sprintf( 'srcset="%s"', wp_get_attachment_image_srcset( self::$large_id, $size_array, $image_meta ) );
0 ignored issues
show
Security Bug introduced by
It seems like $size_array defined by $this->_get_image_size_a...($image_meta, 'medium') on line 1866 can also be of type false; however, wp_get_attachment_image_srcset() does only seem to accept string|array, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
1869
		$sizes  = sprintf( 'sizes="%s"', wp_get_attachment_image_sizes( self::$large_id, $size_array, $image_meta ) );
0 ignored issues
show
Security Bug introduced by
It seems like $size_array defined by $this->_get_image_size_a...($image_meta, 'medium') on line 1866 can also be of type false; however, wp_get_attachment_image_sizes() does only seem to accept string|array, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
1870
1871
		// Build HTML for the editor.
1872
		$img          = get_image_tag( self::$large_id, '', '', '', 'medium' );
1873
		$img_https    = str_replace( 'http://', 'https://', $img );
1874
		$img_relative = str_replace( 'http://', '//', $img );
1875
1876
		// Manually add srcset and sizes to the markup from get_image_tag().
1877
		$respimg          = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img );
1878
		$respimg_https    = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_https );
1879
		$respimg_relative = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_relative );
1880
1881
		$content = '
1882
			<p>Image, http: protocol. Should have srcset and sizes.</p>
1883
			%1$s
1884
1885
			<p>Image, https: protocol. Should have srcset and sizes.</p>
1886
			%2$s
1887
1888
			<p>Image, protocol-relative. Should have srcset and sizes.</p>
1889
			%3$s';
1890
1891
		$unfiltered = sprintf( $content, $img, $img_https, $img_relative );
1892
		$expected   = sprintf( $content, $respimg, $respimg_https, $respimg_relative );
1893
		$actual     = wp_make_content_images_responsive( $unfiltered );
1894
1895
		$this->assertSame( $expected, $actual );
1896
	}
1897
1898
	/**
1899
	 * @ticket 34945
1900
	 * @ticket 33641
1901
	 */
1902
	function test_wp_get_attachment_image_with_https_on() {
1903
		// Mock meta for the image.
1904
		$image_meta = array(
1905
			'width'  => 1200,
1906
			'height' => 600,
1907
			'file'   => 'test.jpg',
1908
			'sizes'  => array(
1909
				'thumbnail' => array(
1910
					'file'   => 'test-150x150.jpg',
1911
					'width'  => 150,
1912
					'height' => 150,
1913
				),
1914
				'medium'    => array(
1915
					'file'   => 'test-300x150.jpg',
1916
					'width'  => 300,
1917
					'height' => 150,
1918
				),
1919
				'large'     => array(
1920
					'file'   => 'test-1024x512.jpg',
1921
					'width'  => 1024,
1922
					'height' => 512,
1923
				),
1924
			)
1925
		);
1926
1927
		// Test using the large file size.
1928
		$size_array = array( 1024, 512 );
1929
		$image_url  = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['sizes']['large']['file'];
1930
1931
		$_SERVER['HTTPS'] = 'on';
1932
1933
		$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';
1934
		$actual   = wp_calculate_image_srcset( $size_array, $image_url, $image_meta );
1935
1936
		$this->assertSame( $expected, $actual );
1937
	}
1938
1939
	/**
1940
	 * @ticket 36084
1941
	 */
1942
	function test_get_image_send_to_editor_defaults() {
1943
		$id      = self::$large_id;
1944
		$caption = '';
1945
		$title   = 'A test title value.';
1946
		$align   = 'left';
1947
1948
		// Calculate attachment data (default is medium).
1949
		$attachment = wp_get_attachment_image_src( $id, 'medium' );
1950
1951
		$html = '<img src="%1$s" alt="" width="%2$d" height="%3$d" class="align%4$s size-medium wp-image-%5$d" />';
1952
		$expected = sprintf( $html, $attachment[0], $attachment[1], $attachment[2], $align, $id );
1953
1954
		$this->assertSame( $expected, get_image_send_to_editor( $id, $caption, $title, $align ) );
1955
1956
		$this->assertSame( $expected, get_image_send_to_editor( $id, $caption, $title, $align ) );
1957
	}
1958
1959
	/**
1960
	 * @ticket 36084
1961
	 */
1962
	function test_get_image_send_to_editor_defaults_with_optional_params() {
1963
		$id      = self::$large_id;
1964
		$caption = 'A test caption.';
1965
		$title   = 'A test title value.';
1966
		$align   = 'left';
1967
		$url     = get_permalink( $id );
1968
		$rel     = true;
1969
		$size    = 'thumbnail';
1970
		$alt     = 'An example alt value.';
1971
1972
		// Calculate attachment data.
1973
		$attachment = wp_get_attachment_image_src( $id, $size );
1974
1975
		$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>';
1976
		$html = '[caption id="attachment_%9$d" align="align%7$s" width="%5$d"]' . $html . ' %10$s[/caption]';
1977
1978
		$expected = sprintf( $html, $url, 'attachment wp-att-' . $id, $attachment[0], $alt, $attachment[1], $attachment[2], $align, $size, $id, $caption );
1979
1980
		$this->assertSame( $expected, get_image_send_to_editor( $id, $caption, $title, $align, $url, $rel, $size, $alt ) );
0 ignored issues
show
Security Bug introduced by
It seems like $url defined by get_permalink($id) on line 1967 can also be of type false; however, get_image_send_to_editor() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
1981
	}
1982
1983
	/**
1984
	 * @ticket 36084
1985
	 */
1986
	function test_get_image_send_to_editor_defaults_no_caption_no_rel() {
1987
		$id      = self::$large_id;
1988
		$caption = '';
1989
		$title   = 'A test title value.';
1990
		$align   = 'left';
1991
		$url     = get_permalink( $id );
1992
		$rel     = '';
1993
		$size    = 'thumbnail';
1994
		$alt     = 'An example alt value.';
1995
1996
		// Calculate attachment data.
1997
		$attachment = wp_get_attachment_image_src( $id, $size );
1998
1999
		$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>';
2000
2001
		$expected = sprintf( $html, $url, $attachment[0], $alt, $attachment[1], $attachment[2], $align, $size, $id );
2002
2003
		$this->assertSame( $expected, get_image_send_to_editor( $id, $caption, $title, $align, $url, $rel, $size, $alt ) );
0 ignored issues
show
Security Bug introduced by
It seems like $url defined by get_permalink($id) on line 1991 can also be of type false; however, get_image_send_to_editor() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
2004
	}
2005
2006
	/**
2007
	 * Tests if wp_get_attachment_image() uses wp_get_attachment_metadata().
2008
	 *
2009
	 * In this way, the meta data can be filtered using the filter
2010
	 * `wp_get_attachment_metadata`.
2011
	 *
2012
	 * The test checks if the image size that is added in the filter is
2013
	 * used in the output of `wp_get_attachment_image()`.
2014
	 *
2015
	 * @ticket 36246
2016
	 */
2017
	function test_wp_get_attachment_image_should_use_wp_get_attachment_metadata() {
2018
		add_filter( 'wp_get_attachment_metadata', array( $this, '_filter_36246' ), 10, 2 );
2019
2020
		remove_all_filters( 'wp_calculate_image_sizes' );
2021
2022
		$actual = wp_get_attachment_image( self::$large_id, 'testsize' );
2023
		$year = date( 'Y' );
2024
		$month = date( 'm' );
2025
2026
		$expected = '<img width="999" height="999" src="http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year . '/' . $month . '/test-image-testsize-999x999.png"' .
2027
			' class="attachment-testsize size-testsize" alt=""' .
2028
			' srcset="http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year . '/' . $month . '/test-image-testsize-999x999.png 999w,' .
2029
				' http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year . '/' . $month . '/test-image-large-150x150.png 150w"' .
2030
				' sizes="(max-width: 999px) 100vw, 999px" />';
2031
2032
		remove_filter( 'wp_get_attachment_metadata', array( $this, '_filter_36246' ) );
2033
2034
		$this->assertSame( $expected, $actual );
2035
	}
2036
2037
	function _filter_36246( $data, $attachment_id ) {
2038
		$data['sizes']['testsize'] = array(
2039
			'file' => 'test-image-testsize-999x999.png',
2040
			'width' => 999,
2041
			'height' => 999,
2042
			'mime-type' => 'image/png',
2043
		);
2044
		return $data;
2045
	}
2046
2047
	/**
2048
	 * @ticket 37813
2049
	 */
2050
	public function test_return_type_when_inserting_attachment_with_error_in_data() {
2051
		$data = array(
2052
			'post_status'  => 'public',
2053
			'post_content' => 'Attachment content',
2054
			'post_title'   => 'Attachment Title',
2055
			'post_date'    => '2012-02-30 00:00:00',
2056
		);
2057
2058
		$attachment_id = wp_insert_attachment( $data, '', 0, true );
2059
		$this->assertWPError( $attachment_id );
2060
		$this->assertEquals( 'invalid_date', $attachment_id->get_error_code() );
2061
2062
		$attachment_id = wp_insert_attachment( $data, '', 0 );
2063
		$this->assertSame( 0, $attachment_id );
2064
	}
2065
}
2066
2067
/**
2068
 * Helper class for `test_autoembed`.
2069
 */
2070
class Test_Autoembed extends WP_Embed {
2071
	public function shortcode( $attr, $url = '' ) {
2072
		return '[embed]';
2073
	}
2074
}
2075