Tests: Add unit tests for attachment’s file size being included in metadata.

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
This commit is contained in:
Sergey Biryukov 2022-11-22 19:43:25 +00:00
parent 9ed27339b1
commit ea651b1fc1
2 changed files with 90 additions and 1 deletions

View File

@ -0,0 +1,89 @@
<?php
/**
* Tests for the `wp_generate_attachment_metadata()` function.
*
* @group media
* @covers ::wp_generate_attachment_metadata
*/
class Tests_Media_wpGenerateAttachmentMetadata extends WP_UnitTestCase {
public function tear_down() {
$this->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'] );
}
}

View File

@ -1,7 +1,7 @@
<?php
/**
* Tests the `wp_img_tag_add_decoding_attr()` function.
* Tests for the `wp_img_tag_add_decoding_attr()` function.
*
* @group media
* @covers ::wp_img_tag_add_decoding_attr