From ea651b1fc19eb560ee87dd6cb2e64c522713ed3c Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 22 Nov 2022 19:43:25 +0000 Subject: [PATCH] =?UTF-8?q?Tests:=20Add=20unit=20tests=20for=20attachment?= =?UTF-8?q?=E2=80=99s=20file=20size=20being=20included=20in=20metadata.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit These tests ensure that `wp_generate_attachment_metadata()` stores the file size of all newly uploaded attachments under the `filesize` array key. The tests were initially supposed to be committed with [52837]. Follow-up to [52837], [52932], [54861]. Props spacedmonkey, johnwatkins0, mitogh, adamsilverstein, pbearne, SergeyBiryukov. Fixes #57171. git-svn-id: https://develop.svn.wordpress.org/trunk@54863 602fd350-edb4-49c9-b593-d223f7449a82 --- .../media/wpGenerateAttachmentMetadata.php | 89 +++++++++++++++++++ .../tests/media/wpImageTagAddDecodingAttr.php | 2 +- 2 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/tests/media/wpGenerateAttachmentMetadata.php diff --git a/tests/phpunit/tests/media/wpGenerateAttachmentMetadata.php b/tests/phpunit/tests/media/wpGenerateAttachmentMetadata.php new file mode 100644 index 0000000000..82fe7dabd6 --- /dev/null +++ b/tests/phpunit/tests/media/wpGenerateAttachmentMetadata.php @@ -0,0 +1,89 @@ +remove_added_uploads(); + + parent::tear_down(); + } + + /** + * Tests that filesize meta is generated for JPEGs. + * + * @ticket 49412 + * + * @covers ::wp_create_image_subsizes + */ + public function test_wp_generate_attachment_metadata_includes_filesize_in_jpg_meta() { + $attachment = $this->factory->attachment->create_upload_object( DIR_TESTDATA . '/images/canola.jpg' ); + + $metadata = wp_get_attachment_metadata( $attachment ); + + $this->assertSame( wp_filesize( get_attached_file( $attachment ) ), $metadata['filesize'] ); + + foreach ( $metadata['sizes'] as $intermediate_size ) { + $this->assertArrayHasKey( 'filesize', $intermediate_size ); + $this->assertNotEmpty( $intermediate_size['filesize'] ); + $this->assertIsNumeric( $intermediate_size['filesize'] ); + } + } + + /** + * Checks that filesize meta is generated for PNGs. + * + * @ticket 49412 + * + * @covers ::wp_create_image_subsizes + */ + public function test_wp_generate_attachment_metadata_includes_filesize_in_png_meta() { + $attachment = $this->factory->attachment->create_upload_object( DIR_TESTDATA . '/images/test-image.png' ); + + $metadata = wp_get_attachment_metadata( $attachment ); + + $this->assertSame( wp_filesize( get_attached_file( $attachment ) ), $metadata['filesize'] ); + } + + /** + * Checks that filesize meta is generated for PDFs. + * + * @ticket 49412 + */ + public function test_wp_generate_attachment_metadata_includes_filesize_in_pdf_meta() { + $attachment = $this->factory->attachment->create_upload_object( DIR_TESTDATA . '/images/wordpress-gsoc-flyer.pdf' ); + + $metadata = wp_get_attachment_metadata( $attachment ); + + $this->assertSame( wp_filesize( get_attached_file( $attachment ) ), $metadata['filesize'] ); + } + + /** + * Checks that filesize meta is generated for PSDs. + * + * @ticket 49412 + */ + public function test_wp_generate_attachment_metadata_includes_filesize_in_psd_meta() { + if ( is_multisite() ) { + // PSD mime type is not allowed by default on multisite. + add_filter( + 'upload_mimes', + static function( $mimes ) { + $mimes['psd'] = 'application/octet-stream'; + return $mimes; + } + ); + } + + $attachment = $this->factory->attachment->create_upload_object( DIR_TESTDATA . '/images/test-image.psd' ); + + $metadata = wp_get_attachment_metadata( $attachment ); + + $this->assertSame( wp_filesize( get_attached_file( $attachment ) ), $metadata['filesize'] ); + } +} diff --git a/tests/phpunit/tests/media/wpImageTagAddDecodingAttr.php b/tests/phpunit/tests/media/wpImageTagAddDecodingAttr.php index 76fa203b7e..3ffe1bc6b4 100644 --- a/tests/phpunit/tests/media/wpImageTagAddDecodingAttr.php +++ b/tests/phpunit/tests/media/wpImageTagAddDecodingAttr.php @@ -1,7 +1,7 @@