forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattachments.php
More file actions
522 lines (390 loc) · 17.3 KB
/
attachments.php
File metadata and controls
522 lines (390 loc) · 17.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
<?php
/**
* @group post
* @group media
* @group upload
*/
class Tests_Post_Attachments extends WP_UnitTestCase {
function tearDown() {
// Remove all uploads.
$this->remove_added_uploads();
parent::tearDown();
}
function test_insert_bogus_image() {
$filename = rand_str() . '.jpg';
$contents = rand_str();
$upload = wp_upload_bits( $filename, null, $contents );
$this->assertEmpty( $upload['error'] );
}
function test_insert_image_no_thumb() {
// This image is smaller than the thumbnail size so it won't have one.
$filename = ( DIR_TESTDATA . '/images/test-image.jpg' );
$contents = file_get_contents( $filename );
$upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
$this->assertEmpty( $upload['error'] );
$id = $this->_make_attachment( $upload );
// Intermediate copies should not exist.
$this->assertFalse( image_get_intermediate_size( $id, 'thumbnail' ) );
$this->assertFalse( image_get_intermediate_size( $id, 'medium' ) );
$this->assertFalse( image_get_intermediate_size( $id, 'medium_large' ) );
// medium, medium_large, and full size will both point to the original.
$downsize = image_downsize( $id, 'medium' );
$this->assertSame( wp_basename( $upload['file'] ), wp_basename( $downsize[0] ) );
$this->assertSame( 50, $downsize[1] );
$this->assertSame( 50, $downsize[2] );
$downsize = image_downsize( $id, 'medium_large' );
$this->assertSame( wp_basename( $upload['file'] ), wp_basename( $downsize[0] ) );
$this->assertSame( 50, $downsize[1] );
$this->assertSame( 50, $downsize[2] );
$downsize = image_downsize( $id, 'full' );
$this->assertSame( wp_basename( $upload['file'] ), wp_basename( $downsize[0] ) );
$this->assertSame( 50, $downsize[1] );
$this->assertSame( 50, $downsize[2] );
}
/**
* @requires function imagejpeg
*/
function test_insert_image_thumb_only() {
update_option( 'medium_size_w', 0 );
update_option( 'medium_size_h', 0 );
$filename = ( DIR_TESTDATA . '/images/a2-small.jpg' );
$contents = file_get_contents( $filename );
$upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
$this->assertEmpty( $upload['error'] );
$id = $this->_make_attachment( $upload );
// Intermediate copies should exist: thumbnail only.
$thumb = image_get_intermediate_size( $id, 'thumbnail' );
$this->assertSame( 'a2-small-150x150.jpg', $thumb['file'] );
$uploads = wp_upload_dir();
$this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path'] ) );
$this->assertFalse( image_get_intermediate_size( $id, 'medium' ) );
$this->assertFalse( image_get_intermediate_size( $id, 'medium_large' ) );
// The thumb url should point to the thumbnail intermediate.
$this->assertSame( $thumb['url'], wp_get_attachment_thumb_url( $id ) );
// image_downsize() should return the correct images and sizes.
$downsize = image_downsize( $id, 'thumbnail' );
$this->assertSame( 'a2-small-150x150.jpg', wp_basename( $downsize[0] ) );
$this->assertSame( 150, $downsize[1] );
$this->assertSame( 150, $downsize[2] );
// medium, medium_large, and full will both point to the original.
$downsize = image_downsize( $id, 'medium' );
$this->assertSame( 'a2-small.jpg', wp_basename( $downsize[0] ) );
$this->assertSame( 400, $downsize[1] );
$this->assertSame( 300, $downsize[2] );
$downsize = image_downsize( $id, 'medium_large' );
$this->assertSame( 'a2-small.jpg', wp_basename( $downsize[0] ) );
$this->assertSame( 400, $downsize[1] );
$this->assertSame( 300, $downsize[2] );
$downsize = image_downsize( $id, 'full' );
$this->assertSame( 'a2-small.jpg', wp_basename( $downsize[0] ) );
$this->assertSame( 400, $downsize[1] );
$this->assertSame( 300, $downsize[2] );
}
/**
* @requires function imagejpeg
*/
function test_insert_image_medium_sizes() {
update_option( 'medium_size_w', 400 );
update_option( 'medium_size_h', 0 );
update_option( 'medium_large_size_w', 600 );
update_option( 'medium_large_size_h', 0 );
$filename = ( DIR_TESTDATA . '/images/2007-06-17DSC_4173.JPG' );
$contents = file_get_contents( $filename );
$upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
$this->assertEmpty( $upload['error'] );
$id = $this->_make_attachment( $upload );
$uploads = wp_upload_dir();
// Intermediate copies should exist: thumbnail and medium.
$thumb = image_get_intermediate_size( $id, 'thumbnail' );
$this->assertSame( '2007-06-17DSC_4173-150x150.jpg', $thumb['file'] );
$this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path'] ) );
$medium = image_get_intermediate_size( $id, 'medium' );
$this->assertSame( '2007-06-17DSC_4173-400x602.jpg', $medium['file'] );
$this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $medium['path'] ) );
$medium_large = image_get_intermediate_size( $id, 'medium_large' );
$this->assertSame( '2007-06-17DSC_4173-600x904.jpg', $medium_large['file'] );
$this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $medium_large['path'] ) );
// The thumb url should point to the thumbnail intermediate.
$this->assertSame( $thumb['url'], wp_get_attachment_thumb_url( $id ) );
// image_downsize() should return the correct images and sizes.
$downsize = image_downsize( $id, 'thumbnail' );
$this->assertSame( '2007-06-17DSC_4173-150x150.jpg', wp_basename( $downsize[0] ) );
$this->assertSame( 150, $downsize[1] );
$this->assertSame( 150, $downsize[2] );
$downsize = image_downsize( $id, 'medium' );
$this->assertSame( '2007-06-17DSC_4173-400x602.jpg', wp_basename( $downsize[0] ) );
$this->assertSame( 400, $downsize[1] );
$this->assertSame( 602, $downsize[2] );
$downsize = image_downsize( $id, 'medium_large' );
$this->assertSame( '2007-06-17DSC_4173-600x904.jpg', wp_basename( $downsize[0] ) );
$this->assertSame( 600, $downsize[1] );
$this->assertSame( 904, $downsize[2] );
$downsize = image_downsize( $id, 'full' );
$this->assertSame( '2007-06-17DSC_4173.jpg', wp_basename( $downsize[0] ) );
$this->assertSame( 680, $downsize[1] );
$this->assertSame( 1024, $downsize[2] );
}
/**
* @requires function imagejpeg
*/
function test_insert_image_delete() {
update_option( 'medium_size_w', 400 );
update_option( 'medium_size_h', 0 );
update_option( 'medium_large_size_w', 600 );
update_option( 'medium_large_size_h', 0 );
$filename = ( DIR_TESTDATA . '/images/2007-06-17DSC_4173.JPG' );
$contents = file_get_contents( $filename );
$upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
$this->assertEmpty( $upload['error'] );
$id = $this->_make_attachment( $upload );
$uploads = wp_upload_dir();
// Check that the file and intermediates exist.
$thumb = image_get_intermediate_size( $id, 'thumbnail' );
$this->assertSame( '2007-06-17DSC_4173-150x150.jpg', $thumb['file'] );
$this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $thumb['path'] ) );
$medium = image_get_intermediate_size( $id, 'medium' );
$this->assertSame( '2007-06-17DSC_4173-400x602.jpg', $medium['file'] );
$this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $medium['path'] ) );
$medium_large = image_get_intermediate_size( $id, 'medium_large' );
$this->assertSame( '2007-06-17DSC_4173-600x904.jpg', $medium_large['file'] );
$this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $medium_large['path'] ) );
$meta = wp_get_attachment_metadata( $id );
$original = $meta['file'];
$this->assertTrue( is_file( $uploads['basedir'] . DIRECTORY_SEPARATOR . $original ) );
// Now delete the attachment and make sure all files are gone.
wp_delete_attachment( $id );
$this->assertFalse( is_file( $thumb['path'] ) );
$this->assertFalse( is_file( $medium['path'] ) );
$this->assertFalse( is_file( $medium_large['path'] ) );
$this->assertFalse( is_file( $original ) );
}
/**
* GUID should never be empty
*
* @ticket 18310
* @ticket 21963
*/
function test_insert_image_without_guid() {
// This image is smaller than the thumbnail size so it won't have one.
$filename = ( DIR_TESTDATA . '/images/test-image.jpg' );
$contents = file_get_contents( $filename );
$upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
$this->assertEmpty( $upload['error'] );
$upload['url'] = '';
$id = $this->_make_attachment( $upload );
$guid = get_the_guid( $id );
$this->assertNotEmpty( $guid );
}
/**
* @ticket 21963
*/
function test_update_attachment_fields() {
$filename = ( DIR_TESTDATA . '/images/test-image.jpg' );
$contents = file_get_contents( $filename );
$upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
$this->assertEmpty( $upload['error'] );
$id = $this->_make_attachment( $upload );
$attached_file = get_post_meta( $id, '_wp_attached_file', true );
$post = get_post( $id, ARRAY_A );
$post['post_title'] = 'title';
$post['post_excerpt'] = 'caption';
$post['post_content'] = 'description';
wp_update_post( $post );
// Make sure the update didn't remove the attached file.
$this->assertSame( $attached_file, get_post_meta( $id, '_wp_attached_file', true ) );
}
/**
* @ticket 29646
*/
function test_update_orphan_attachment_parent() {
$filename = ( DIR_TESTDATA . '/images/test-image.jpg' );
$contents = file_get_contents( $filename );
$upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
$this->assertEmpty( $upload['error'] );
$attachment_id = $this->_make_attachment( $upload );
// Assert that the attachment is an orphan.
$attachment = get_post( $attachment_id );
$this->assertSame( $attachment->post_parent, 0 );
$post_id = wp_insert_post(
array(
'post_content' => rand_str(),
'post_title' => rand_str(),
)
);
// Assert that the attachment has a parent.
wp_insert_attachment( $attachment, '', $post_id );
$attachment = get_post( $attachment_id );
$this->assertSame( $attachment->post_parent, $post_id );
}
/**
* @ticket 15928
*/
public function test_wp_get_attachment_url_should_not_force_https_when_current_page_is_non_ssl_and_siteurl_is_non_ssl() {
$siteurl = get_option( 'siteurl' );
update_option( 'siteurl', set_url_scheme( $siteurl, 'http' ) );
$filename = DIR_TESTDATA . '/images/test-image.jpg';
$contents = file_get_contents( $filename );
$upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
$this->assertEmpty( $upload['error'] );
// Set attachment ID.
$attachment_id = $this->_make_attachment( $upload );
$_SERVER['HTTPS'] = 'off';
$url = wp_get_attachment_url( $attachment_id );
$this->assertSame( 'http', parse_url( $url, PHP_URL_SCHEME ) );
}
/**
* @ticket 15928
*
* This situation (current request is non-SSL but siteurl is https) should never arise.
*/
public function test_wp_get_attachment_url_should_not_force_https_when_current_page_is_non_ssl_and_siteurl_is_ssl() {
$siteurl = get_option( 'siteurl' );
update_option( 'siteurl', set_url_scheme( $siteurl, 'https' ) );
$filename = DIR_TESTDATA . '/images/test-image.jpg';
$contents = file_get_contents( $filename );
$upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
$this->assertEmpty( $upload['error'] );
// Set attachment ID.
$attachment_id = $this->_make_attachment( $upload );
$_SERVER['HTTPS'] = 'off';
$url = wp_get_attachment_url( $attachment_id );
$this->assertSame( 'http', parse_url( $url, PHP_URL_SCHEME ) );
}
/**
* @ticket 15928
*
* Canonical siteurl is non-SSL, but SSL support is available/optional.
*/
public function test_wp_get_attachment_url_should_force_https_with_https_on_same_host_when_siteurl_is_non_ssl_but_ssl_is_available() {
$siteurl = get_option( 'siteurl' );
update_option( 'siteurl', set_url_scheme( $siteurl, 'http' ) );
$filename = ( DIR_TESTDATA . '/images/test-image.jpg' );
$contents = file_get_contents( $filename );
$upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
$this->assertEmpty( $upload['error'] );
// Set attachment ID.
$attachment_id = $this->_make_attachment( $upload );
$_SERVER['HTTPS'] = 'on';
// Ensure that server host matches the host of wp_upload_dir().
$upload_dir = wp_upload_dir();
$_SERVER['HTTP_HOST'] = parse_url( $upload_dir['baseurl'], PHP_URL_HOST );
// Test that wp_get_attachemt_url returns with https scheme.
$url = wp_get_attachment_url( $attachment_id );
$this->assertSame( 'https', parse_url( $url, PHP_URL_SCHEME ) );
}
/**
* @ticket 15928
*/
public function test_wp_get_attachment_url_with_https_on_same_host_when_siteurl_is_https() {
$siteurl = get_option( 'siteurl' );
update_option( 'siteurl', set_url_scheme( $siteurl, 'https' ) );
$filename = ( DIR_TESTDATA . '/images/test-image.jpg' );
$contents = file_get_contents( $filename );
$upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
$this->assertEmpty( $upload['error'] );
// Set attachment ID.
$attachment_id = $this->_make_attachment( $upload );
$_SERVER['HTTPS'] = 'on';
// Ensure that server host matches the host of wp_upload_dir().
$upload_dir = wp_upload_dir();
$_SERVER['HTTP_HOST'] = parse_url( $upload_dir['baseurl'], PHP_URL_HOST );
// Test that wp_get_attachemt_url returns with https scheme.
$url = wp_get_attachment_url( $attachment_id );
$this->assertSame( 'https', parse_url( $url, PHP_URL_SCHEME ) );
}
/**
* @ticket 15928
*/
public function test_wp_get_attachment_url_should_not_force_https_when_administering_over_https_but_siteurl_is_not_https() {
$siteurl = get_option( 'siteurl' );
update_option( 'siteurl', set_url_scheme( $siteurl, 'http' ) );
$filename = ( DIR_TESTDATA . '/images/test-image.jpg' );
$contents = file_get_contents( $filename );
$upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
$this->assertEmpty( $upload['error'] );
// Set attachment ID.
$attachment_id = $this->_make_attachment( $upload );
$_SERVER['HTTPS'] = 'on';
set_current_screen( 'dashboard' );
$url = wp_get_attachment_url( $attachment_id );
$this->assertSame( set_url_scheme( $url, 'http' ), $url );
}
/**
* @ticket 15928
*/
public function test_wp_get_attachment_url_should_force_https_when_administering_over_https_and_siteurl_is_https() {
// Set https upload URL.
add_filter( 'upload_dir', '_upload_dir_https' );
$filename = ( DIR_TESTDATA . '/images/test-image.jpg' );
$contents = file_get_contents( $filename );
$upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
$this->assertEmpty( $upload['error'] );
// Set attachment ID.
$attachment_id = $this->_make_attachment( $upload );
$_SERVER['HTTPS'] = 'on';
set_current_screen( 'dashboard' );
$url = wp_get_attachment_url( $attachment_id );
// Cleanup.
remove_filter( 'upload_dir', '_upload_dir_https' );
$this->assertSame( 'https', parse_url( $url, PHP_URL_SCHEME ) );
}
public function test_wp_attachment_is() {
$filename = DIR_TESTDATA . '/images/test-image.jpg';
$contents = file_get_contents( $filename );
$upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
$attachment_id = $this->_make_attachment( $upload );
$this->assertTrue( wp_attachment_is_image( $attachment_id ) );
$this->assertTrue( wp_attachment_is( 'image', $attachment_id ) );
$this->assertFalse( wp_attachment_is( 'audio', $attachment_id ) );
$this->assertFalse( wp_attachment_is( 'video', $attachment_id ) );
}
public function test_wp_attachment_is_default() {
// On Multisite, psd is not an allowed mime type by default.
if ( is_multisite() ) {
add_filter( 'upload_mimes', array( $this, 'allow_psd_mime_type' ), 10, 2 );
}
$filename = DIR_TESTDATA . '/images/test-image.psd';
$contents = file_get_contents( $filename );
$upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
$attachment_id = $this->_make_attachment( $upload );
$this->assertFalse( wp_attachment_is_image( $attachment_id ) );
$this->assertTrue( wp_attachment_is( 'psd', $attachment_id ) );
$this->assertFalse( wp_attachment_is( 'audio', $attachment_id ) );
$this->assertFalse( wp_attachment_is( 'video', $attachment_id ) );
if ( is_multisite() ) {
remove_filter( 'upload_mimes', array( $this, 'allow_psd_mime_type' ), 10, 2 );
}
}
public function test_upload_mimes_filter_is_applied() {
$filename = DIR_TESTDATA . '/images/test-image.jpg';
$contents = file_get_contents( $filename );
$upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
$this->assertFalse( $upload['error'] );
add_filter( 'upload_mimes', array( $this, 'disallow_jpg_mime_type' ) );
$upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
remove_filter( 'upload_mimes', array( $this, 'disallow_jpg_mime_type' ) );
$this->assertNotEmpty( $upload['error'] );
}
public function allow_psd_mime_type( $mimes ) {
$mimes['psd'] = 'application/octet-stream';
return $mimes;
}
public function disallow_jpg_mime_type( $mimes ) {
unset( $mimes['jpg|jpeg|jpe'] );
return $mimes;
}
/**
* @ticket 33012
*/
public function test_wp_mime_type_icon() {
$icon = wp_mime_type_icon();
$this->assertStringContainsString( 'images/media/default.png', $icon );
}
/**
* @ticket 33012
*/
public function test_wp_mime_type_icon_video() {
$icon = wp_mime_type_icon( 'video/mp4' );
$this->assertStringContainsString( 'images/media/video.png', $icon );
}
}