Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 0 additions & 44 deletions tests/phpunit/includes/abstract-testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1526,50 +1526,6 @@ public function set_permalink_structure( $structure = '' ) {
$wp_rewrite->flush_rules();
}

/**
* Creates an attachment post from an uploaded file.
*
* @since 4.4.0
* @since 6.2.0 Returns a WP_Error object on failure.
*
* @param array $upload Array of information about the uploaded file, provided by wp_upload_bits().
* @param int $parent_post_id Optional. Parent post ID.
* @return int|WP_Error The attachment ID on success, WP_Error object on failure.
*/
public function _make_attachment( $upload, $parent_post_id = 0 ) {
$type = '';
if ( ! empty( $upload['type'] ) ) {
$type = $upload['type'];
} else {
$mime = wp_check_filetype( $upload['file'] );
if ( $mime ) {
$type = $mime['type'];
}
}

$attachment = array(
'post_title' => wp_basename( $upload['file'] ),
'post_content' => '',
'post_type' => 'attachment',
'post_parent' => $parent_post_id,
'post_mime_type' => $type,
'guid' => $upload['url'],
);

$attachment_id = wp_insert_attachment( $attachment, $upload['file'], $parent_post_id, true );

if ( is_wp_error( $attachment_id ) ) {
return $attachment_id;
}

wp_update_attachment_metadata(
$attachment_id,
wp_generate_attachment_metadata( $attachment_id, $upload['file'] )
);

return $attachment_id;
}

/**
* Updates the modified and modified GMT date of a post in the database.
*
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/includes/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ function wp_tests_options( $value ) {
require __DIR__ . '/class-wp-sitemaps-test-provider.php';
require __DIR__ . '/class-wp-sitemaps-empty-test-provider.php';
require __DIR__ . '/class-wp-sitemaps-large-test-provider.php';
require __DIR__ . '/trait-runs-file-upload-tests.php';

/**
* A class to handle additional command line arguments passed to the script.
Expand Down
68 changes: 68 additions & 0 deletions tests/phpunit/includes/trait-runs-file-upload-tests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/**
* Defines helper functions for dealing with file uploads and attachment creation.
*/
trait WP_Test_RunsFileUploadTests {

/**
* Creates an attachment post from an uploaded file.
*
* @since 4.4.0
* @since 6.2.0 Returns a WP_Error object on failure.
*
* @param array $upload Array of information about the uploaded file, provided by wp_upload_bits().
* @param int $parent_post_id Optional. Parent post ID.
* @return int|WP_Error The attachment ID on success, WP_Error object on failure.
*/
public function _make_attachment( $upload, $parent_post_id = 0 ) {
$type = '';
if ( ! empty( $upload['type'] ) ) {
$type = $upload['type'];
} else {
$mime = wp_check_filetype( $upload['file'] );
if ( $mime ) {
$type = $mime['type'];
}
}

$attachment = array(
'post_title' => wp_basename( $upload['file'] ),
'post_content' => '',
'post_type' => 'attachment',
'post_parent' => $parent_post_id,
'post_mime_type' => $type,
'guid' => $upload['url'],
);

$attachment_id = wp_insert_attachment( $attachment, $upload['file'], $parent_post_id, true );

if ( is_wp_error( $attachment_id ) ) {
return $attachment_id;
}

wp_update_attachment_metadata(
$attachment_id,
wp_generate_attachment_metadata( $attachment_id, $upload['file'] )
);

return $attachment_id;
}

/**
* Uploads given file and creates an attachment post from it.
*
* @since 6.2.0
*
* @param array $filename Absolute path to the file to upload.
* @param int $parent_post_id Optional. Parent post ID.
*
* @return int|WP_Error The attachment ID on success, WP_Error object on failure.
*/
public function _upload_file_and_make_attachment( $filename, $parent_post_id = 0 ) {
$contents = file_get_contents( $filename );
$upload = wp_upload_bits( wp_basename( $filename ), null, $contents );

return $this->_make_attachment( $upload, $parent_post_id );
}
}
2 changes: 2 additions & 0 deletions tests/phpunit/tests/ajax/wpAjaxCropImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
class Tests_Ajax_WpAjaxCropImage extends WP_Ajax_UnitTestCase {

use WP_Test_RunsFileUploadTests;

/**
* @var WP_Post|null
*/
Expand Down
14 changes: 4 additions & 10 deletions tests/phpunit/tests/ajax/wpAjaxImageEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
class Tests_Ajax_wpAjaxImageEditor extends WP_Ajax_UnitTestCase {

use WP_Test_RunsFileUploadTests;

/**
* Tear down the test fixture.
*/
Expand All @@ -38,11 +40,7 @@ public function tear_down() {
public function testCropImageThumbnail() {
require_once ABSPATH . 'wp-admin/includes/image-edit.php';

$filename = DIR_TESTDATA . '/images/canola.jpg';
$contents = file_get_contents( $filename );

$upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
$id = $this->_make_attachment( $upload );
$id = $this->_upload_file_and_make_attachment( DIR_TESTDATA . '/images/canola.jpg' );

$_REQUEST['action'] = 'image-editor';
$_REQUEST['context'] = 'edit-attachment';
Expand Down Expand Up @@ -73,11 +71,7 @@ public function testImageEditOverwriteConstant() {

require_once ABSPATH . 'wp-admin/includes/image-edit.php';

$filename = DIR_TESTDATA . '/images/canola.jpg';
$contents = file_get_contents( $filename );

$upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
$id = $this->_make_attachment( $upload );
$id = $this->_upload_file_and_make_attachment( DIR_TESTDATA . '/images/canola.jpg' );

$_REQUEST['action'] = 'image-editor';
$_REQUEST['context'] = 'edit-attachment';
Expand Down
14 changes: 4 additions & 10 deletions tests/phpunit/tests/ajax/wpAjaxSendAttachmentToEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
class Tests_Ajax_wpAjaxSendAttachmentToEditor extends WP_Ajax_UnitTestCase {

use WP_Test_RunsFileUploadTests;

/**
* @ticket 36578
*
Expand All @@ -22,11 +24,7 @@ public function test_wp_ajax_send_attachment_to_editor_should_return_an_image()
// Become an administrator.
$this->_setRole( 'administrator' );

$filename = DIR_TESTDATA . '/images/canola.jpg';
$contents = file_get_contents( $filename );

$upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
$attachment = $this->_make_attachment( $upload );
$attachment = $this->_upload_file_and_make_attachment( DIR_TESTDATA . '/images/canola.jpg' );

// Set up a default request.
$_POST['nonce'] = wp_create_nonce( 'media-send-to-editor' );
Expand Down Expand Up @@ -65,11 +63,7 @@ public function test_wp_ajax_send_attachment_to_editor_should_return_a_link() {
// Become an administrator.
$this->_setRole( 'administrator' );

$filename = DIR_TESTDATA . '/formatting/entities.txt';
$contents = file_get_contents( $filename );

$upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
$attachment = $this->_make_attachment( $upload );
$attachment = $this->_upload_file_and_make_attachment( DIR_TESTDATA . '/formatting/entities.txt' );

// Set up a default request.
$_POST['nonce'] = wp_create_nonce( 'media-send-to-editor' );
Expand Down
3 changes: 3 additions & 0 deletions tests/phpunit/tests/general/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
require_once ABSPATH . 'wp-admin/includes/class-wp-site-icon.php';

class Tests_General_Template extends WP_UnitTestCase {

use WP_Test_RunsFileUploadTests;

protected $wp_site_icon;
public $site_icon_id;
public $site_icon_url;
Expand Down
28 changes: 12 additions & 16 deletions tests/phpunit/tests/image/intermediateSize.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* @group upload
*/
class Tests_Image_Intermediate_Size extends WP_UnitTestCase {

use WP_Test_RunsFileUploadTests;

public function tear_down() {
$this->remove_added_uploads();

Expand All @@ -15,13 +18,6 @@ public function tear_down() {
parent::tear_down();
}

public function _make_attachment( $file, $parent_post_id = 0 ) {
$contents = file_get_contents( $file );
$upload = wp_upload_bits( wp_basename( $file ), null, $contents );

return parent::_make_attachment( $upload, $parent_post_id );
}

public function test_make_intermediate_size_no_size() {
$image = image_make_intermediate_size( DIR_TESTDATA . '/images/a2-small.jpg', 0, 0, false );

Expand Down Expand Up @@ -96,7 +92,7 @@ public function test_get_intermediate_sizes_by_name() {
add_image_size( 'test-size', 330, 220, true );

$file = DIR_TESTDATA . '/images/waffles.jpg';
$id = $this->_make_attachment( $file, 0 );
$id = $this->_upload_file_and_make_attachment( $file );

// Look for a size by name.
$image = image_get_intermediate_size( $id, 'test-size' );
Expand All @@ -120,7 +116,7 @@ public function test_get_intermediate_sizes_by_array_exact() {
add_image_size( 'false-width', 600, 220, true );

$file = DIR_TESTDATA . '/images/waffles.jpg';
$id = $this->_make_attachment( $file, 0 );
$id = $this->_upload_file_and_make_attachment( $file );

// Look for a size by array that exists.
// Note: Staying larger than 300px to miss default medium crop.
Expand All @@ -143,7 +139,7 @@ public function test_get_intermediate_sizes_by_array_nearest() {
add_image_size( 'false-width', 150, 220, true );

$file = DIR_TESTDATA . '/images/waffles.jpg';
$id = $this->_make_attachment( $file, 0 );
$id = $this->_upload_file_and_make_attachment( $file );

// Look for a size by array that doesn't exist.
// Note: Staying larger than 300px to miss default medium crop.
Expand All @@ -165,7 +161,7 @@ public function test_get_intermediate_sizes_by_array_nearest_false() {
add_image_size( 'false-width', 150, 220, true );

$file = DIR_TESTDATA . '/images/waffles.jpg';
$id = $this->_make_attachment( $file, 0 );
$id = $this->_upload_file_and_make_attachment( $file );

// Look for a size by array that doesn't exist.
// Note: Staying larger than 300px to miss default medium crop.
Expand All @@ -189,7 +185,7 @@ public function test_get_intermediate_sizes_by_array_zero_height() {
add_image_size( 'false-height', $width, 100, true );

$file = DIR_TESTDATA . '/images/waffles.jpg';
$id = $this->_make_attachment( $file, 0 );
$id = $this->_upload_file_and_make_attachment( $file );

$original = wp_get_attachment_metadata( $id );
$image_w = $width;
Expand Down Expand Up @@ -218,7 +214,7 @@ public function test_get_intermediate_sizes_by_array_zero_width() {
add_image_size( 'false-height', 300, $height, true );

$file = DIR_TESTDATA . '/images/waffles.jpg';
$id = $this->_make_attachment( $file, 0 );
$id = $this->_upload_file_and_make_attachment( $file );

$original = wp_get_attachment_metadata( $id );
$image_h = $height;
Expand All @@ -245,7 +241,7 @@ public function test_get_intermediate_sizes_should_match_size_with_off_by_one_as
add_image_size( 'off-by-one', $width, $height, true );

$file = DIR_TESTDATA . '/images/waffles.jpg';
$id = $this->_make_attachment( $file, 0 );
$id = $this->_upload_file_and_make_attachment( $file );

$original = wp_get_attachment_metadata( $id );
$image_h = $height;
Expand All @@ -267,7 +263,7 @@ public function test_get_intermediate_size_with_small_size_array() {
add_image_size( 'test-size', 200, 100, true );

$file = DIR_TESTDATA . '/images/waffles.jpg';
$id = $this->_make_attachment( $file, 0 );
$id = $this->_upload_file_and_make_attachment( $file );

// Request a size by array that doesn't exist and is smaller than the 'thumbnail'.
$image = image_get_intermediate_size( $id, array( 50, 25 ) );
Expand All @@ -282,7 +278,7 @@ public function test_get_intermediate_size_with_small_size_array() {
*/
public function test_get_intermediate_size_with_small_size_array_fallback() {
$file = DIR_TESTDATA . '/images/waffles.jpg';
$id = $this->_make_attachment( $file, 0 );
$id = $this->_upload_file_and_make_attachment( $file );

$original = wp_get_attachment_metadata( $id );
$thumbnail_file = $original['sizes']['thumbnail']['file'];
Expand Down
10 changes: 4 additions & 6 deletions tests/phpunit/tests/image/siteIcon.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
require_once ABSPATH . 'wp-admin/includes/class-wp-site-icon.php';

class Tests_WP_Site_Icon extends WP_UnitTestCase {

use WP_Test_RunsFileUploadTests;

protected $wp_site_icon;

public $attachment_id = 0;
Expand Down Expand Up @@ -160,12 +163,7 @@ private function insert_attachment() {
return $this->attachment_id;
}

$filename = DIR_TESTDATA . '/images/test-image.jpg';
$contents = file_get_contents( $filename );

$upload = wp_upload_bits( wp_basename( $filename ), null, $contents );

$this->attachment_id = $this->_make_attachment( $upload );
$this->attachment_id = $this->_upload_file_and_make_attachment( DIR_TESTDATA . '/images/test-image.jpg' );
return $this->attachment_id;
}
}
14 changes: 4 additions & 10 deletions tests/phpunit/tests/post/attachments.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/
class Tests_Post_Attachments extends WP_UnitTestCase {

use WP_Test_RunsFileUploadTests;

public function tear_down() {
// Remove all uploads.
$this->remove_added_uploads();
Expand Down Expand Up @@ -441,11 +443,7 @@ public function test_wp_get_attachment_url_should_force_https_when_administering
}

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 );
$attachment_id = $this->_upload_file_and_make_attachment( DIR_TESTDATA . '/images/test-image.jpg' );

$this->assertTrue( wp_attachment_is_image( $attachment_id ) );
$this->assertTrue( wp_attachment_is( 'image', $attachment_id ) );
Expand All @@ -459,11 +457,7 @@ public function test_wp_attachment_is_default() {
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 );
$attachment_id = $this->_upload_file_and_make_attachment( DIR_TESTDATA . '/images/test-image.psd' );

$this->assertFalse( wp_attachment_is_image( $attachment_id ) );
$this->assertTrue( wp_attachment_is( 'psd', $attachment_id ) );
Expand Down
Loading