mirror of
git://develop.git.wordpress.org/
synced 2025-02-07 08:04:27 +01:00
Tests: Minimize file copying in WP_REST_Attachments_Controller
tests.
The tests use two images that were deleted on teardown and recreated on setup for every single test. This appears to be unnecessary, as the files can instead only be recreated if they are missing, and deleted after the test run is complete. This commit reduces ~200 redundant file copying operations to ~5 when running this test class. Follow-up to [38832], [48291], [54424]. See #55652. git-svn-id: https://develop.svn.wordpress.org/trunk@54428 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
f1396626d1
commit
21c38bb6b3
@ -22,12 +22,12 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
/**
|
||||
* @var string The path to a test file.
|
||||
*/
|
||||
private $test_file;
|
||||
private static $test_file;
|
||||
|
||||
/**
|
||||
* @var string The path to a second test file.
|
||||
*/
|
||||
private $test_file2;
|
||||
private static $test_file2;
|
||||
|
||||
/**
|
||||
* @var array The recorded posts query clauses.
|
||||
@ -68,6 +68,13 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
}
|
||||
|
||||
public static function wpTearDownAfterClass() {
|
||||
if ( file_exists( self::$test_file ) ) {
|
||||
unlink( self::$test_file );
|
||||
}
|
||||
if ( file_exists( self::$test_file2 ) ) {
|
||||
unlink( self::$test_file2 );
|
||||
}
|
||||
|
||||
self::delete_user( self::$editor_id );
|
||||
self::delete_user( self::$author_id );
|
||||
self::delete_user( self::$contributor_id );
|
||||
@ -85,11 +92,16 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
$role->add_cap( 'level_0' );
|
||||
|
||||
$orig_file = DIR_TESTDATA . '/images/canola.jpg';
|
||||
$this->test_file = get_temp_dir() . 'canola.jpg';
|
||||
copy( $orig_file, $this->test_file );
|
||||
self::$test_file = get_temp_dir() . 'canola.jpg';
|
||||
if ( ! file_exists( self::$test_file ) ) {
|
||||
copy( $orig_file, self::$test_file );
|
||||
}
|
||||
|
||||
$orig_file2 = DIR_TESTDATA . '/images/codeispoetry.png';
|
||||
$this->test_file2 = get_temp_dir() . 'codeispoetry.png';
|
||||
copy( $orig_file2, $this->test_file2 );
|
||||
self::$test_file2 = get_temp_dir() . 'codeispoetry.png';
|
||||
if ( ! file_exists( self::$test_file2 ) ) {
|
||||
copy( $orig_file2, self::$test_file2 );
|
||||
}
|
||||
|
||||
add_filter( 'rest_pre_dispatch', array( $this, 'wpSetUpBeforeRequest' ), 10, 3 );
|
||||
add_filter( 'posts_clauses', array( $this, 'save_posts_clauses' ), 10, 2 );
|
||||
@ -106,13 +118,6 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
}
|
||||
|
||||
public function tear_down() {
|
||||
if ( file_exists( $this->test_file ) ) {
|
||||
unlink( $this->test_file );
|
||||
}
|
||||
if ( file_exists( $this->test_file2 ) ) {
|
||||
unlink( $this->test_file2 );
|
||||
}
|
||||
|
||||
$this->remove_added_uploads();
|
||||
|
||||
if ( class_exists( WP_Image_Editor_Mock::class ) ) {
|
||||
@ -183,7 +188,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
$this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
|
||||
// Single.
|
||||
$attachment_id = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
0,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
@ -244,7 +249,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
|
||||
public function test_registered_get_item_params() {
|
||||
$id1 = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
0,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
@ -264,7 +269,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
*/
|
||||
public function test_allow_header_sent_on_options_request() {
|
||||
$id1 = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
0,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
@ -292,7 +297,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
public function test_get_items() {
|
||||
wp_set_current_user( 0 );
|
||||
$id1 = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
0,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
@ -301,7 +306,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
);
|
||||
$draft_post = self::factory()->post->create( array( 'post_status' => 'draft' ) );
|
||||
$id2 = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
$draft_post,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
@ -310,7 +315,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
);
|
||||
$published_post = self::factory()->post->create( array( 'post_status' => 'publish' ) );
|
||||
$id3 = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
$published_post,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
@ -332,7 +337,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
public function test_get_items_logged_in_editor() {
|
||||
wp_set_current_user( self::$editor_id );
|
||||
$id1 = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
0,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
@ -341,7 +346,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
);
|
||||
$draft_post = self::factory()->post->create( array( 'post_status' => 'draft' ) );
|
||||
$id2 = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
$draft_post,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
@ -350,7 +355,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
);
|
||||
$published_post = self::factory()->post->create( array( 'post_status' => 'publish' ) );
|
||||
$id3 = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
$published_post,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
@ -370,7 +375,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
|
||||
public function test_get_items_media_type() {
|
||||
$id1 = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
0,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
@ -393,7 +398,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
|
||||
public function test_get_items_mime_type() {
|
||||
$id1 = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
0,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
@ -417,7 +422,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
public function test_get_items_parent() {
|
||||
$post_id = self::factory()->post->create( array( 'post_title' => 'Test Post' ) );
|
||||
$attachment_id = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
$post_id,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
@ -425,7 +430,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
)
|
||||
);
|
||||
$attachment_id2 = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
0,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
@ -461,7 +466,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
public function test_get_items_invalid_status_param_is_error_response() {
|
||||
wp_set_current_user( self::$editor_id );
|
||||
self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
0,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
@ -479,7 +484,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
// Logged out users can't make the request.
|
||||
wp_set_current_user( 0 );
|
||||
$attachment_id1 = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
0,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
@ -503,7 +508,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
// Logged out users can't make the request.
|
||||
wp_set_current_user( 0 );
|
||||
$attachment_id1 = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
0,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
@ -512,7 +517,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
)
|
||||
);
|
||||
$attachment_id2 = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
0,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
@ -548,7 +553,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
|
||||
public function test_get_items_valid_date() {
|
||||
$id1 = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
0,
|
||||
array(
|
||||
'post_date' => '2016-01-15T00:00:00Z',
|
||||
@ -557,7 +562,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
)
|
||||
);
|
||||
$id2 = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
0,
|
||||
array(
|
||||
'post_date' => '2016-01-16T00:00:00Z',
|
||||
@ -566,7 +571,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
)
|
||||
);
|
||||
$id3 = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
0,
|
||||
array(
|
||||
'post_date' => '2016-01-17T00:00:00Z',
|
||||
@ -599,7 +604,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
*/
|
||||
public function test_get_items_valid_modified_date() {
|
||||
$id1 = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
0,
|
||||
array(
|
||||
'post_date' => '2016-01-01 00:00:00',
|
||||
@ -608,7 +613,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
)
|
||||
);
|
||||
$id2 = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
0,
|
||||
array(
|
||||
'post_date' => '2016-01-02 00:00:00',
|
||||
@ -617,7 +622,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
)
|
||||
);
|
||||
$id3 = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
0,
|
||||
array(
|
||||
'post_date' => '2016-01-03 00:00:00',
|
||||
@ -659,7 +664,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
*/
|
||||
public function test_get_items_with_empty_page_runs_count_query_after() {
|
||||
self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
0,
|
||||
array(
|
||||
'post_date' => '2022-06-12T00:00:00Z',
|
||||
@ -681,7 +686,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
|
||||
public function test_get_item() {
|
||||
$attachment_id = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
0,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
@ -701,17 +706,17 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
*/
|
||||
public function test_get_item_sizes() {
|
||||
$attachment_id = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
0,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
'post_excerpt' => 'A sample caption',
|
||||
),
|
||||
$this->test_file
|
||||
self::$test_file
|
||||
);
|
||||
|
||||
add_image_size( 'rest-api-test', 119, 119, true );
|
||||
wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $this->test_file ) );
|
||||
wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, self::$test_file ) );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/media/' . $attachment_id );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
@ -732,17 +737,17 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
*/
|
||||
public function test_get_item_sizes_with_no_url() {
|
||||
$attachment_id = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
0,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
'post_excerpt' => 'A sample caption',
|
||||
),
|
||||
$this->test_file
|
||||
self::$test_file
|
||||
);
|
||||
|
||||
add_image_size( 'rest-api-test', 119, 119, true );
|
||||
wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $this->test_file ) );
|
||||
wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, self::$test_file ) );
|
||||
|
||||
add_filter( 'wp_get_attachment_image_src', '__return_false' );
|
||||
|
||||
@ -760,7 +765,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
wp_set_current_user( 0 );
|
||||
$draft_post = self::factory()->post->create( array( 'post_status' => 'draft' ) );
|
||||
$id1 = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
$draft_post,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
@ -774,7 +779,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
|
||||
public function test_get_item_inherit_status_with_invalid_parent() {
|
||||
$attachment_id = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
REST_TESTS_IMPOSSIBLY_HIGH_NUMBER,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
@ -791,7 +796,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
|
||||
public function test_get_item_auto_status_with_invalid_parent_not_authenticated_returns_error() {
|
||||
$attachment_id = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
REST_TESTS_IMPOSSIBLY_HIGH_NUMBER,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
@ -819,7 +824,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
$request->set_param( 'description', 'Without a description, my attachment is descriptionless.' );
|
||||
$request->set_param( 'alt_text', 'Alt text is stored outside post schema.' );
|
||||
|
||||
$request->set_body( file_get_contents( $this->test_file ) );
|
||||
$request->set_body( file_get_contents( self::$test_file ) );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
@ -843,14 +848,14 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
$request->set_file_params(
|
||||
array(
|
||||
'file' => array(
|
||||
'file' => file_get_contents( $this->test_file2 ),
|
||||
'file' => file_get_contents( self::$test_file2 ),
|
||||
'name' => 'codeispoetry.png',
|
||||
'size' => filesize( $this->test_file2 ),
|
||||
'tmp_name' => $this->test_file2,
|
||||
'size' => filesize( self::$test_file2 ),
|
||||
'tmp_name' => self::$test_file2,
|
||||
),
|
||||
)
|
||||
);
|
||||
$request->set_header( 'Content-MD5', md5_file( $this->test_file2 ) );
|
||||
$request->set_header( 'Content-MD5', md5_file( self::$test_file2 ) );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$this->assertSame( 201, $response->get_status() );
|
||||
$data = $response->get_data();
|
||||
@ -866,14 +871,14 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
$request->set_file_params(
|
||||
array(
|
||||
'file' => array(
|
||||
'file' => file_get_contents( $this->test_file ),
|
||||
'file' => file_get_contents( self::$test_file ),
|
||||
'name' => 'canola.jpg',
|
||||
'size' => filesize( $this->test_file ),
|
||||
'tmp_name' => $this->test_file,
|
||||
'size' => filesize( self::$test_file ),
|
||||
'tmp_name' => self::$test_file,
|
||||
),
|
||||
)
|
||||
);
|
||||
$request->set_header( 'Content-MD5', md5_file( $this->test_file ) );
|
||||
$request->set_header( 'Content-MD5', md5_file( self::$test_file ) );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$this->assertSame( 201, $response->get_status() );
|
||||
}
|
||||
@ -887,14 +892,14 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
$request->set_file_params(
|
||||
array(
|
||||
'file' => array(
|
||||
'file' => file_get_contents( $this->test_file ),
|
||||
'file' => file_get_contents( self::$test_file ),
|
||||
'name' => 'canola.jpg',
|
||||
'size' => filesize( $this->test_file ),
|
||||
'tmp_name' => $this->test_file,
|
||||
'size' => filesize( self::$test_file ),
|
||||
'tmp_name' => self::$test_file,
|
||||
),
|
||||
)
|
||||
);
|
||||
$request->set_header( 'Content-MD5', md5_file( $this->test_file ) );
|
||||
$request->set_header( 'Content-MD5', md5_file( self::$test_file ) );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$this->assertSame( 201, $response->get_status() );
|
||||
}
|
||||
@ -909,7 +914,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
public function test_create_item_missing_content_type() {
|
||||
wp_set_current_user( self::$author_id );
|
||||
$request = new WP_REST_Request( 'POST', '/wp/v2/media' );
|
||||
$request->set_body( file_get_contents( $this->test_file ) );
|
||||
$request->set_body( file_get_contents( self::$test_file ) );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$this->assertErrorResponse( 'rest_upload_no_content_type', $response, 400 );
|
||||
}
|
||||
@ -918,7 +923,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
wp_set_current_user( self::$author_id );
|
||||
$request = new WP_REST_Request( 'POST', '/wp/v2/media' );
|
||||
$request->set_header( 'Content-Type', 'image/jpeg' );
|
||||
$request->set_body( file_get_contents( $this->test_file ) );
|
||||
$request->set_body( file_get_contents( self::$test_file ) );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$this->assertErrorResponse( 'rest_upload_no_content_disposition', $response, 400 );
|
||||
}
|
||||
@ -929,7 +934,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
$request->set_header( 'Content-Type', 'image/jpeg' );
|
||||
$request->set_header( 'Content-Disposition', 'attachment; filename=canola.jpg' );
|
||||
$request->set_header( 'Content-MD5', 'abc123' );
|
||||
$request->set_body( file_get_contents( $this->test_file ) );
|
||||
$request->set_body( file_get_contents( self::$test_file ) );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$this->assertErrorResponse( 'rest_upload_hash_mismatch', $response, 412 );
|
||||
}
|
||||
@ -940,10 +945,10 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
$request->set_file_params(
|
||||
array(
|
||||
'file' => array(
|
||||
'file' => file_get_contents( $this->test_file ),
|
||||
'file' => file_get_contents( self::$test_file ),
|
||||
'name' => 'canola.jpg',
|
||||
'size' => filesize( $this->test_file ),
|
||||
'tmp_name' => $this->test_file,
|
||||
'size' => filesize( self::$test_file ),
|
||||
'tmp_name' => self::$test_file,
|
||||
),
|
||||
)
|
||||
);
|
||||
@ -989,7 +994,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
$request = new WP_REST_Request( 'POST', '/wp/v2/media' );
|
||||
$request->set_header( 'Content-Type', 'image/jpeg' );
|
||||
$request->set_header( 'Content-Disposition', 'attachment; filename=canola.jpg' );
|
||||
$request->set_body( file_get_contents( $this->test_file ) );
|
||||
$request->set_body( file_get_contents( self::$test_file ) );
|
||||
$request->set_param( 'post', $attachment_id );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
|
||||
@ -1004,7 +1009,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
$request->set_header( 'Content-Type', 'image/jpeg' );
|
||||
$request->set_header( 'Content-Disposition', 'attachment; filename=canola.jpg' );
|
||||
|
||||
$request->set_body( file_get_contents( $this->test_file ) );
|
||||
$request->set_body( file_get_contents( self::$test_file ) );
|
||||
$request->set_param( 'alt_text', 'test alt text' );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$attachment = $response->get_data();
|
||||
@ -1019,7 +1024,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
$request = new WP_REST_Request( 'POST', '/wp/v2/media' );
|
||||
$request->set_header( 'Content-Type', 'image/jpeg' );
|
||||
$request->set_header( 'Content-Disposition', 'attachment; filename=canola.jpg' );
|
||||
$request->set_body( file_get_contents( $this->test_file ) );
|
||||
$request->set_body( file_get_contents( self::$test_file ) );
|
||||
$request->set_param( 'alt_text', '<script>alert(document.cookie)</script>' );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$attachment = $response->get_data();
|
||||
@ -1035,7 +1040,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
$request = new WP_REST_Request( 'POST', '/wp/v2/media' );
|
||||
$request->set_header( 'Content-Type', 'image/jpeg' );
|
||||
$request->set_header( 'Content-Disposition', 'attachment; filename=canola.jpg' );
|
||||
$request->set_body( file_get_contents( $this->test_file ) );
|
||||
$request->set_body( file_get_contents( self::$test_file ) );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$attachment = $response->get_data();
|
||||
$this->assertStringNotContainsString( ABSPATH, get_post_meta( $attachment['id'], '_wp_attached_file', true ) );
|
||||
@ -1044,7 +1049,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
public function test_update_item() {
|
||||
wp_set_current_user( self::$editor_id );
|
||||
$attachment_id = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
0,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
@ -1074,7 +1079,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
wp_set_current_user( self::$editor_id );
|
||||
$original_parent = self::factory()->post->create( array() );
|
||||
$attachment_id = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
$original_parent,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
@ -1098,7 +1103,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
public function test_update_item_invalid_permissions() {
|
||||
wp_set_current_user( self::$author_id );
|
||||
$attachment_id = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
0,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
@ -1122,7 +1127,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
);
|
||||
wp_set_current_user( self::$editor_id );
|
||||
$attachment_id = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
0,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
@ -1143,7 +1148,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
wp_set_current_user( self::$editor_id );
|
||||
$parent_id = self::factory()->post->create( array() );
|
||||
$attachment_id = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
$parent_id,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
@ -1166,7 +1171,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
public function test_update_item_with_new_inherit_status() {
|
||||
wp_set_current_user( self::$editor_id );
|
||||
$attachment_id = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
0,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
@ -1188,7 +1193,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
$request = new WP_REST_Request( 'POST', '/wp/v2/media' );
|
||||
$request->set_header( 'Content-Type', 'image/jpeg' );
|
||||
$request->set_header( 'Content-Disposition', 'attachment; filename=canola.jpg' );
|
||||
$request->set_body( file_get_contents( $this->test_file ) );
|
||||
$request->set_body( file_get_contents( self::$test_file ) );
|
||||
|
||||
foreach ( $input as $name => $value ) {
|
||||
$request->set_param( $name, $value );
|
||||
@ -1446,7 +1451,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
public function test_delete_item() {
|
||||
wp_set_current_user( self::$editor_id );
|
||||
$attachment_id = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
0,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
@ -1462,7 +1467,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
public function test_delete_item_no_trash() {
|
||||
wp_set_current_user( self::$editor_id );
|
||||
$attachment_id = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
0,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
@ -1487,7 +1492,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
public function test_delete_item_invalid_delete_permissions() {
|
||||
wp_set_current_user( self::$author_id );
|
||||
$attachment_id = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
0,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
@ -1502,7 +1507,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
|
||||
public function test_prepare_item() {
|
||||
$attachment_id = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
0,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
@ -1521,7 +1526,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
|
||||
public function test_prepare_item_limit_fields() {
|
||||
$attachment_id = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
0,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
@ -1612,7 +1617,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
$this->assertSame( $schema, $data['schema']['properties']['my_custom_int'] );
|
||||
|
||||
$attachment_id = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
0,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
@ -1649,7 +1654,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
|
||||
wp_set_current_user( self::$editor_id );
|
||||
$attachment_id = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
0,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
@ -1675,21 +1680,21 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
|
||||
public function test_search_item_by_filename() {
|
||||
$id1 = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
0,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
)
|
||||
);
|
||||
$id2 = self::factory()->attachment->create_object(
|
||||
$this->test_file2,
|
||||
self::$test_file2,
|
||||
0,
|
||||
array(
|
||||
'post_mime_type' => 'image/png',
|
||||
)
|
||||
);
|
||||
|
||||
$filename = wp_basename( $this->test_file2 );
|
||||
$filename = wp_basename( self::$test_file2 );
|
||||
|
||||
$request = new WP_REST_Request( 'GET', '/wp/v2/media' );
|
||||
$request->set_param( 'search', $filename );
|
||||
@ -1801,16 +1806,16 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
array(
|
||||
'file' => array(
|
||||
'error' => '0',
|
||||
'file' => file_get_contents( $this->test_file ),
|
||||
'file' => file_get_contents( self::$test_file ),
|
||||
'name' => 'canola.jpg',
|
||||
'size' => filesize( $this->test_file ),
|
||||
'tmp_name' => $this->test_file,
|
||||
'size' => filesize( self::$test_file ),
|
||||
'tmp_name' => self::$test_file,
|
||||
),
|
||||
)
|
||||
);
|
||||
$request->set_param( 'title', 'My title is very cool' );
|
||||
$request->set_param( 'caption', 'This is a better caption.' );
|
||||
$request->set_header( 'Content-MD5', md5_file( $this->test_file ) );
|
||||
$request->set_header( 'Content-MD5', md5_file( self::$test_file ) );
|
||||
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$this->assertErrorResponse( 'rest_upload_file_too_big', $response, 400 );
|
||||
@ -1829,7 +1834,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
$request = new WP_REST_Request( 'POST', '/wp/v2/media' );
|
||||
$request->set_header( 'Content-Type', 'image/jpeg' );
|
||||
$request->set_header( 'Content-Disposition', 'attachment; filename=canola.jpg' );
|
||||
$request->set_body( file_get_contents( $this->test_file ) );
|
||||
$request->set_body( file_get_contents( self::$test_file ) );
|
||||
$request->set_param( 'title', 'My title is very cool' );
|
||||
$request->set_param( 'caption', 'This is a better caption.' );
|
||||
|
||||
@ -1852,16 +1857,16 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
array(
|
||||
'file' => array(
|
||||
'error' => '0',
|
||||
'file' => file_get_contents( $this->test_file ),
|
||||
'file' => file_get_contents( self::$test_file ),
|
||||
'name' => 'canola.jpg',
|
||||
'size' => filesize( $this->test_file ),
|
||||
'tmp_name' => $this->test_file,
|
||||
'size' => filesize( self::$test_file ),
|
||||
'tmp_name' => self::$test_file,
|
||||
),
|
||||
)
|
||||
);
|
||||
$request->set_param( 'title', 'My title is very cool' );
|
||||
$request->set_param( 'caption', 'This is a better caption.' );
|
||||
$request->set_header( 'Content-MD5', md5_file( $this->test_file ) );
|
||||
$request->set_header( 'Content-MD5', md5_file( self::$test_file ) );
|
||||
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$this->assertErrorResponse( 'rest_upload_limited_space', $response, 400 );
|
||||
@ -1880,7 +1885,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
$request = new WP_REST_Request( 'POST', '/wp/v2/media' );
|
||||
$request->set_header( 'Content-Type', 'image/jpeg' );
|
||||
$request->set_header( 'Content-Disposition', 'attachment; filename=canola.jpg' );
|
||||
$request->set_body( file_get_contents( $this->test_file ) );
|
||||
$request->set_body( file_get_contents( self::$test_file ) );
|
||||
$request->set_param( 'title', 'My title is very cool' );
|
||||
$request->set_param( 'caption', 'This is a better caption.' );
|
||||
|
||||
@ -1910,7 +1915,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
$request->set_param( 'description', 'Without a description, my attachment is descriptionless.' );
|
||||
$request->set_param( 'alt_text', 'Alt text is stored outside post schema.' );
|
||||
|
||||
$request->set_body( file_get_contents( $this->test_file ) );
|
||||
$request->set_body( file_get_contents( self::$test_file ) );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
$this->assertSame( 201, $response->get_status() );
|
||||
@ -1933,7 +1938,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
|
||||
wp_set_current_user( self::$editor_id );
|
||||
$attachment_id = self::factory()->attachment->create_object(
|
||||
$this->test_file,
|
||||
self::$test_file,
|
||||
0,
|
||||
array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
@ -1971,7 +1976,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
$request->set_header( 'Content-Disposition', 'attachment; filename=cannoli.jpg' );
|
||||
$request->set_param( 'meta', array( 'best_cannoli' => 'Chocolate-dipped, no filling' ) );
|
||||
|
||||
$request->set_body( file_get_contents( $this->test_file ) );
|
||||
$request->set_body( file_get_contents( self::$test_file ) );
|
||||
$response = rest_get_server()->dispatch( $request );
|
||||
$data = $response->get_data();
|
||||
|
||||
@ -1992,7 +1997,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
* @requires function imagejpeg
|
||||
*/
|
||||
public function test_edit_image_returns_error_if_logged_out() {
|
||||
$attachment = self::factory()->attachment->create_upload_object( $this->test_file );
|
||||
$attachment = self::factory()->attachment->create_upload_object( self::$test_file );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', "/wp/v2/media/{$attachment}/edit" );
|
||||
$request->set_body_params( array( 'src' => wp_get_attachment_image_url( $attachment, 'full' ) ) );
|
||||
@ -2009,7 +2014,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
$user->add_cap( 'upload_files', false );
|
||||
|
||||
wp_set_current_user( $user->ID );
|
||||
$attachment = self::factory()->attachment->create_upload_object( $this->test_file );
|
||||
$attachment = self::factory()->attachment->create_upload_object( self::$test_file );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', "/wp/v2/media/{$attachment}/edit" );
|
||||
$request->set_body_params( array( 'src' => wp_get_attachment_image_url( $attachment, 'full' ) ) );
|
||||
@ -2023,7 +2028,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
*/
|
||||
public function test_edit_image_returns_error_if_cannot_edit() {
|
||||
wp_set_current_user( self::$uploader_id );
|
||||
$attachment = self::factory()->attachment->create_upload_object( $this->test_file );
|
||||
$attachment = self::factory()->attachment->create_upload_object( self::$test_file );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', "/wp/v2/media/{$attachment}/edit" );
|
||||
$request->set_body_params( array( 'src' => wp_get_attachment_image_url( $attachment, 'full' ) ) );
|
||||
@ -2050,7 +2055,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
*/
|
||||
public function test_edit_image_returns_error_if_unsupported_mime_type() {
|
||||
wp_set_current_user( self::$superadmin_id );
|
||||
$attachment = self::factory()->attachment->create_upload_object( $this->test_file );
|
||||
$attachment = self::factory()->attachment->create_upload_object( self::$test_file );
|
||||
wp_update_post(
|
||||
array(
|
||||
'ID' => $attachment,
|
||||
@ -2070,7 +2075,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
*/
|
||||
public function test_edit_image_returns_error_if_no_edits() {
|
||||
wp_set_current_user( self::$superadmin_id );
|
||||
$attachment = self::factory()->attachment->create_upload_object( $this->test_file );
|
||||
$attachment = self::factory()->attachment->create_upload_object( self::$test_file );
|
||||
|
||||
$request = new WP_REST_Request( 'POST', "/wp/v2/media/{$attachment}/edit" );
|
||||
$request->set_body_params( array( 'src' => wp_get_attachment_image_url( $attachment, 'full' ) ) );
|
||||
@ -2084,7 +2089,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
*/
|
||||
public function test_edit_image_rotate() {
|
||||
wp_set_current_user( self::$superadmin_id );
|
||||
$attachment = self::factory()->attachment->create_upload_object( $this->test_file );
|
||||
$attachment = self::factory()->attachment->create_upload_object( self::$test_file );
|
||||
|
||||
$this->setup_mock_editor();
|
||||
WP_Image_Editor_Mock::$edit_return['rotate'] = new WP_Error();
|
||||
@ -2109,7 +2114,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
*/
|
||||
public function test_edit_image_crop() {
|
||||
wp_set_current_user( self::$superadmin_id );
|
||||
$attachment = self::factory()->attachment->create_upload_object( $this->test_file );
|
||||
$attachment = self::factory()->attachment->create_upload_object( self::$test_file );
|
||||
|
||||
$this->setup_mock_editor();
|
||||
WP_Image_Editor_Mock::$size_return = array(
|
||||
@ -2146,7 +2151,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
*/
|
||||
public function test_edit_image() {
|
||||
wp_set_current_user( self::$superadmin_id );
|
||||
$attachment = self::factory()->attachment->create_upload_object( $this->test_file );
|
||||
$attachment = self::factory()->attachment->create_upload_object( self::$test_file );
|
||||
|
||||
$params = array(
|
||||
'rotation' => 60,
|
||||
@ -2173,7 +2178,7 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
*/
|
||||
public function test_batch_edit_image() {
|
||||
wp_set_current_user( self::$superadmin_id );
|
||||
$attachment = self::factory()->attachment->create_upload_object( $this->test_file );
|
||||
$attachment = self::factory()->attachment->create_upload_object( self::$test_file );
|
||||
|
||||
$params = array(
|
||||
'modifiers' => array(
|
||||
@ -2216,8 +2221,8 @@ class WP_Test_REST_Attachments_Controller extends WP_Test_REST_Post_Type_Control
|
||||
*/
|
||||
public function test_edit_image_returns_error_if_mismatched_src() {
|
||||
wp_set_current_user( self::$superadmin_id );
|
||||
$attachment_id_image1 = self::factory()->attachment->create_upload_object( $this->test_file );
|
||||
$attachment_id_image2 = self::factory()->attachment->create_upload_object( $this->test_file2 );
|
||||
$attachment_id_image1 = self::factory()->attachment->create_upload_object( self::$test_file );
|
||||
$attachment_id_image2 = self::factory()->attachment->create_upload_object( self::$test_file2 );
|
||||
$attachment_id_file = self::factory()->attachment->create();
|
||||
|
||||
// URL to the first uploaded image.
|
||||
|
Loading…
x
Reference in New Issue
Block a user