From 8a28e1980869d4544c811d58f49a6bb5ee153cdc Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Fri, 6 Mar 2015 20:37:08 +0000 Subject: [PATCH] Add unit tests for `wp_attachment_is()`, checks the whitelist and arbitrary extension. See #25275. git-svn-id: https://develop.svn.wordpress.org/trunk@31647 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/post.php | 7 ++++++- tests/phpunit/tests/post/attachments.php | 25 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 334a0b8c0c..f7654f0f72 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -5091,11 +5091,16 @@ function wp_attachment_is( $type, $post_id = 0 ) { } $check = wp_check_filetype( $file ); - if ( empty( $check['ext'] ) || 'import' !== $post->post_mime_type ) { + if ( empty( $check['ext'] ) ) { return false; } + $ext = $check['ext']; + if ( 'import' !== $post->post_mime_type ) { + return $type === $ext; + } + switch ( $type ) { case 'image': $image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png' ); diff --git a/tests/phpunit/tests/post/attachments.php b/tests/phpunit/tests/post/attachments.php index f7e8f9c826..833a56f1e3 100644 --- a/tests/phpunit/tests/post/attachments.php +++ b/tests/phpunit/tests/post/attachments.php @@ -439,4 +439,29 @@ class Tests_Post_Attachments extends WP_UnitTestCase { $_SERVER['HTTP_HOST'] = $http_host; } + public function test_wp_attachment_is() { + $filename = DIR_TESTDATA . '/images/test-image.jpg'; + $contents = file_get_contents( $filename ); + + $upload = wp_upload_bits( 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() { + $filename = DIR_TESTDATA . '/images/test-image.psd'; + $contents = file_get_contents( $filename ); + + $upload = wp_upload_bits( 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 ) ); + } }