Make WordPress Core


Ignore:
Timestamp:
10/06/2025 11:50:00 PM (4 months ago)
Author:
ramonopoly
Message:

Attachments REST API endpoint: update attachments controller to support flip and to customize attachment fields

This commit enhances media editor capabilities pursuant to the Phase 3: Collaboration > Media Library. See https://make.wordpress.org/core/2023/07/07/media-library/

It adds the following functionality:

  • the ability to flip an image horizontally and vertically
  • the ability to send arguments to update the new image's captiondescription, and titlepost and alt_text fields.

Props ramonopoly, mukesh27, isabel_brison, andrewserong.
Fixes #64035.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r60893 r60908  
    26832683        $this->assertTrue( $result );
    26842684    }
     2685
     2686    /**
     2687     * Tests that the attachment fields caption, description, and title, post and alt_text are updated correctly.
     2688     * @ticket 64035
     2689     * @requires function imagejpeg
     2690     */
     2691    public function test_edit_image_updates_attachment_fields() {
     2692        wp_set_current_user( self::$superadmin_id );
     2693        $attachment = self::factory()->attachment->create_upload_object( self::$test_file );
     2694
     2695        // In order to test the edit endpoint editable fields, we need to create a new attachment.
     2696        $params = array(
     2697            'src'         => wp_get_attachment_image_url( $attachment, 'full' ),
     2698            'modifiers'   => array(
     2699                array(
     2700                    'type' => 'crop',
     2701                    'args' => array(
     2702                        'left'   => 10,
     2703                        'top'    => 10,
     2704                        'width'  => 80,
     2705                        'height' => 80,
     2706                    ),
     2707                ),
     2708            ),
     2709            'caption'     => 'Test Caption',
     2710            'description' => 'Test Description',
     2711            'title'       => 'Test Title',
     2712            'post'        => 1,
     2713            'alt_text'    => 'Test Alt Text',
     2714        );
     2715
     2716        $request = new WP_REST_Request( 'POST', "/wp/v2/media/{$attachment}/edit" );
     2717        $request->set_body_params( $params );
     2718        $response = rest_do_request( $request );
     2719
     2720        // The edit endpoint creates a new attachment, so we expect a 201 status.
     2721        $this->assertEquals( 201, $response->get_status() );
     2722
     2723        $data              = $response->get_data();
     2724        $new_attachment_id = $data['id'];
     2725
     2726        $updated_attachment = get_post( $new_attachment_id );
     2727
     2728        $this->assertSame( 'Test Title', $updated_attachment->post_title, 'Title of the updated attachment is not identical.' );
     2729
     2730        $this->assertSame( 'Test Caption', $updated_attachment->post_excerpt, 'Caption of the updated attachment is not identical.' );
     2731
     2732        $this->assertSame( 'Test Description', $updated_attachment->post_content, 'Description of the updated attachment is not identical.' );
     2733
     2734        $this->assertSame( 1, $updated_attachment->post_parent, 'Post parent of the updated attachment is not identical.' );
     2735
     2736        $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.' );
     2737    }
     2738
     2739    /**
     2740     * Tests that the image is flipped correctly vertically and horizontally.
     2741     *
     2742     * @ticket 64035
     2743     * @requires function imagejpeg
     2744     */
     2745    public function test_edit_image_vertical_and_horizontal_flip() {
     2746        wp_set_current_user( self::$superadmin_id );
     2747        $attachment = self::factory()->attachment->create_upload_object( self::$test_file );
     2748
     2749        $this->setup_mock_editor();
     2750        WP_Image_Editor_Mock::$edit_return['flip'] = new WP_Error();
     2751
     2752        $params = array(
     2753            'flip' => array(
     2754                'vertical'   => true,
     2755                'horizontal' => true,
     2756            ),
     2757            'src'  => wp_get_attachment_image_url( $attachment, 'full' ),
     2758        );
     2759
     2760        $request = new WP_REST_Request( 'POST', "/wp/v2/media/{$attachment}/edit" );
     2761        $request->set_body_params( $params );
     2762        $response = rest_do_request( $request );
     2763        $this->assertErrorResponse( 'rest_image_flip_failed', $response, 500 );
     2764
     2765        $this->assertCount( 1, WP_Image_Editor_Mock::$spy['flip'] );
     2766        // The controller converts the integer values to booleans: 0 !== (int) 1 = true.
     2767        $this->assertSame( array( true, true ), WP_Image_Editor_Mock::$spy['flip'][0], 'Vertical and horizontal flip of the image is not identical.' );
     2768    }
     2769
     2770    /**
     2771     * Tests that the image is flipped correctly vertically only.
     2772     *
     2773     * @ticket 64035
     2774     * @requires function imagejpeg
     2775     */
     2776    public function test_edit_image_vertical_flip_with_horizontal_false() {
     2777        wp_set_current_user( self::$superadmin_id );
     2778        $attachment = self::factory()->attachment->create_upload_object( self::$test_file );
     2779
     2780        $this->setup_mock_editor();
     2781        WP_Image_Editor_Mock::$edit_return['flip'] = new WP_Error();
     2782
     2783        $params = array(
     2784            'flip' => array(
     2785                'vertical'   => true,
     2786                'horizontal' => false,
     2787            ),
     2788            'src'  => wp_get_attachment_image_url( $attachment, 'full' ),
     2789        );
     2790
     2791        $request = new WP_REST_Request( 'POST', "/wp/v2/media/{$attachment}/edit" );
     2792        $request->set_body_params( $params );
     2793        $response = rest_do_request( $request );
     2794        $this->assertErrorResponse( 'rest_image_flip_failed', $response, 500 );
     2795
     2796        $this->assertCount( 1, WP_Image_Editor_Mock::$spy['flip'] );
     2797        // The controller converts the integer values to booleans: 0 !== (int) 1 = true.
     2798        $this->assertSame( array( true, false ), WP_Image_Editor_Mock::$spy['flip'][0], 'Vertical flip of the image is not identical.' );
     2799    }
     2800
     2801    /**
     2802     * Tests that the image is flipped correctly with only vertical flip in arguments.
     2803     *
     2804     * @ticket 64035
     2805     * @requires function imagejpeg
     2806     */
     2807    public function test_edit_image_vertical_flip_only() {
     2808        wp_set_current_user( self::$superadmin_id );
     2809        $attachment = self::factory()->attachment->create_upload_object( self::$test_file );
     2810
     2811        $this->setup_mock_editor();
     2812        WP_Image_Editor_Mock::$edit_return['flip'] = new WP_Error();
     2813
     2814        $params = array(
     2815            'flip' => array(
     2816                'vertical' => true,
     2817            ),
     2818            'src'  => wp_get_attachment_image_url( $attachment, 'full' ),
     2819        );
     2820
     2821        $request = new WP_REST_Request( 'POST', "/wp/v2/media/{$attachment}/edit" );
     2822        $request->set_body_params( $params );
     2823        $response = rest_do_request( $request );
     2824        $this->assertErrorResponse( 'rest_image_flip_failed', $response, 500 );
     2825
     2826        $this->assertCount( 1, WP_Image_Editor_Mock::$spy['flip'] );
     2827        // The controller converts the integer values to booleans: 0 !== (int) 1 = true.
     2828        $this->assertSame( array( true, false ), WP_Image_Editor_Mock::$spy['flip'][0], 'Vertical flip of the image is not identical.' );
     2829    }
    26852830}
Note: See TracChangeset for help on using the changeset viewer.