Skip to content

Commit 92eb256

Browse files
committed
Tests: Cover attachment field inheritance when editing images.
Verify that edited attachments inherit omitted title, caption, description, and alt text from the original. Cover absent alt metadata and requests that override selected fields while inheriting the others. Developed in: #14893 Props irozum. See #50675. git-svn-id: https://develop.svn.wordpress.org/trunk@65089 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 29e4849 commit 92eb256

1 file changed

Lines changed: 118 additions & 0 deletions

File tree

‎tests/phpunit/tests/rest-api/rest-attachments-controller.php‎

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3751,6 +3751,124 @@ public function test_edit_image_updates_attachment_fields() {
37513751
$this->assertSame( 'Test Alt Text', get_post_meta( $new_attachment_id, '_wp_attachment_image_alt', true ), 'Alt text of the updated attachment is not identical.' );
37523752
}
37533753

3754+
/**
3755+
* Tests that an edited image inherits the title, caption, description and alt text of the original attachment.
3756+
*
3757+
* @ticket 50675
3758+
* @requires function imagejpeg
3759+
*/
3760+
public function test_edit_image_copies_fields_from_original_attachment() {
3761+
$attachment = $this->create_attachment_with_original_fields();
3762+
3763+
$new_attachment_id = $this->crop_attachment_via_edit_endpoint( $attachment );
3764+
$new_attachment = get_post( $new_attachment_id );
3765+
3766+
$this->assertNotSame( $attachment, $new_attachment_id, 'A new attachment should have been created.' );
3767+
$this->assertSame( 'Original Title', $new_attachment->post_title, 'Title was not copied from the original attachment.' );
3768+
$this->assertSame( 'Original Caption', $new_attachment->post_excerpt, 'Caption was not copied from the original attachment.' );
3769+
$this->assertSame( 'Original Description', $new_attachment->post_content, 'Description was not copied from the original attachment.' );
3770+
$this->assertSame( 'Original Alt Text', get_post_meta( $new_attachment_id, '_wp_attachment_image_alt', true ), 'Alt text was not copied from the original attachment.' );
3771+
}
3772+
3773+
/**
3774+
* Tests that no alt text meta is created for an edited image when the original attachment has none.
3775+
*
3776+
* @ticket 50675
3777+
* @requires function imagejpeg
3778+
*/
3779+
public function test_edit_image_does_not_copy_empty_alt_text_from_original_attachment() {
3780+
$attachment = $this->create_attachment_with_original_fields( false );
3781+
3782+
$new_attachment_id = $this->crop_attachment_via_edit_endpoint( $attachment );
3783+
3784+
$this->assertFalse( metadata_exists( 'post', $new_attachment_id, '_wp_attachment_image_alt' ), 'Alt text meta should not exist when the original has none.' );
3785+
}
3786+
3787+
/**
3788+
* Tests that fields passed in the request take precedence over the original attachment, while omitted fields are still inherited.
3789+
*
3790+
* @ticket 50675
3791+
* @requires function imagejpeg
3792+
*/
3793+
public function test_edit_image_request_fields_override_original_attachment_fields() {
3794+
$attachment = $this->create_attachment_with_original_fields();
3795+
3796+
$new_attachment_id = $this->crop_attachment_via_edit_endpoint(
3797+
$attachment,
3798+
array(
3799+
'title' => 'New Title',
3800+
'alt_text' => 'New Alt Text',
3801+
)
3802+
);
3803+
$new_attachment = get_post( $new_attachment_id );
3804+
3805+
$this->assertSame( 'New Title', $new_attachment->post_title, 'Title from the request was not used.' );
3806+
$this->assertSame( 'New Alt Text', get_post_meta( $new_attachment_id, '_wp_attachment_image_alt', true ), 'Alt text from the request was not used.' );
3807+
$this->assertSame( 'Original Caption', $new_attachment->post_excerpt, 'Omitted caption was not copied from the original attachment.' );
3808+
$this->assertSame( 'Original Description', $new_attachment->post_content, 'Omitted description was not copied from the original attachment.' );
3809+
}
3810+
3811+
/**
3812+
* Creates an uploaded image attachment with distinctive title, caption, description and, optionally, alt text.
3813+
*
3814+
* @param bool $with_alt_text Whether to set alt text on the attachment.
3815+
* @return int Attachment ID.
3816+
*/
3817+
private function create_attachment_with_original_fields( $with_alt_text = true ) {
3818+
wp_set_current_user( self::$superadmin_id );
3819+
$attachment = self::factory()->attachment->create_upload_object( self::$test_file );
3820+
3821+
wp_update_post(
3822+
array(
3823+
'ID' => $attachment,
3824+
'post_title' => 'Original Title',
3825+
'post_excerpt' => 'Original Caption',
3826+
'post_content' => 'Original Description',
3827+
)
3828+
);
3829+
3830+
if ( $with_alt_text ) {
3831+
update_post_meta( $attachment, '_wp_attachment_image_alt', 'Original Alt Text' );
3832+
}
3833+
3834+
return $attachment;
3835+
}
3836+
3837+
/**
3838+
* Crops an attachment through the edit endpoint and returns the ID of the newly created attachment.
3839+
*
3840+
* @param int $attachment Attachment ID to edit.
3841+
* @param array $extra_params Additional request parameters.
3842+
* @return int New attachment ID.
3843+
*/
3844+
private function crop_attachment_via_edit_endpoint( $attachment, array $extra_params = array() ) {
3845+
$request = new WP_REST_Request( 'POST', "/wp/v2/media/{$attachment}/edit" );
3846+
$request->set_body_params(
3847+
array_merge(
3848+
array(
3849+
'src' => wp_get_attachment_image_url( $attachment, 'full' ),
3850+
'modifiers' => array(
3851+
array(
3852+
'type' => 'crop',
3853+
'args' => array(
3854+
'left' => 10,
3855+
'top' => 10,
3856+
'width' => 80,
3857+
'height' => 80,
3858+
),
3859+
),
3860+
),
3861+
),
3862+
$extra_params
3863+
)
3864+
);
3865+
$response = rest_do_request( $request );
3866+
3867+
$this->assertSame( 201, $response->get_status() );
3868+
3869+
return $response->get_data()['id'];
3870+
}
3871+
37543872
/**
37553873
* Tests that the image is flipped correctly vertically and horizontally.
37563874
*

0 commit comments

Comments
 (0)