Make WordPress Core


Ignore:
Timestamp:
11/08/2016 04:28:47 AM (9 years ago)
Author:
rmccue
Message:

REST API: Change attachment caption & description to objects.

Just like excerpt and content for regular posts, these have transformations applied that can make the content significantly different from the raw value.

Props jnylen0.
Fixes #38679.

File:
1 edited

Legend:

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

    r39126 r39154  
    461461    public function test_create_item() {
    462462        wp_set_current_user( self::$author_id );
     463
    463464        $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
    464465        $request->set_header( 'Content-Type', 'image/jpeg' );
    465466        $request->set_header( 'Content-Disposition', 'attachment; filename=canola.jpg' );
     467        $request->set_param( 'title', 'My title is very cool' );
     468        $request->set_param( 'caption', 'This is a better caption.' );
     469        $request->set_param( 'description', 'Without a description, my attachment is descriptionless.' );
     470        $request->set_param( 'alt_text', 'Alt text is stored outside post schema.' );
     471
    466472        $request->set_body( file_get_contents( $this->test_file ) );
    467473        $response = $this->server->dispatch( $request );
    468474        $data = $response->get_data();
     475
    469476        $this->assertEquals( 201, $response->get_status() );
    470477        $this->assertEquals( 'image', $data['media_type'] );
     478
     479        $attachment = get_post( $data['id'] );
     480        $this->assertEquals( 'My title is very cool', $data['title']['raw'] );
     481        $this->assertEquals( 'My title is very cool', $attachment->post_title );
     482        $this->assertEquals( 'This is a better caption.', $data['caption']['raw'] );
     483        $this->assertEquals( 'This is a better caption.', $attachment->post_excerpt );
     484        $this->assertEquals( 'Without a description, my attachment is descriptionless.', $data['description']['raw'] );
     485        $this->assertEquals( 'Without a description, my attachment is descriptionless.', $attachment->post_content );
     486        $this->assertEquals( 'Alt text is stored outside post schema.', $data['alt_text'] );
     487        $this->assertEquals( 'Alt text is stored outside post schema.', get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ) );
    471488    }
    472489
     
    651668        $this->assertEquals( 'My title is very cool', $data['title']['raw'] );
    652669        $this->assertEquals( 'My title is very cool', $attachment->post_title );
    653         $this->assertEquals( 'This is a better caption.', $data['caption'] );
     670        $this->assertEquals( 'This is a better caption.', $data['caption']['raw'] );
    654671        $this->assertEquals( 'This is a better caption.', $attachment->post_excerpt );
    655         $this->assertEquals( 'Without a description, my attachment is descriptionless.', $data['description'] );
     672        $this->assertEquals( 'Without a description, my attachment is descriptionless.', $data['description']['raw'] );
    656673        $this->assertEquals( 'Without a description, my attachment is descriptionless.', $attachment->post_content );
    657674        $this->assertEquals( 'Alt text is stored outside post schema.', $data['alt_text'] );
     
    776793        $this->assertArrayHasKey( 'alt_text', $properties );
    777794        $this->assertArrayHasKey( 'caption', $properties );
     795        $this->assertArrayHasKey( 'raw', $properties['caption']['properties'] );
     796        $this->assertArrayHasKey( 'rendered', $properties['caption']['properties'] );
    778797        $this->assertArrayHasKey( 'description', $properties );
     798        $this->assertArrayHasKey( 'raw', $properties['description']['properties'] );
     799        $this->assertArrayHasKey( 'rendered', $properties['description']['properties'] );
    779800        $this->assertArrayHasKey( 'comment_status', $properties );
    780801        $this->assertArrayHasKey( 'date', $properties );
     
    795816        $this->assertArrayHasKey( 'source_url', $properties );
    796817        $this->assertArrayHasKey( 'title', $properties );
     818        $this->assertArrayHasKey( 'raw', $properties['title']['properties'] );
     819        $this->assertArrayHasKey( 'rendered', $properties['title']['properties'] );
    797820        $this->assertArrayHasKey( 'type', $properties );
    798821    }
     
    892915        parent::check_post_data( $attachment, $data, $context, $links );
    893916
     917        $this->assertArrayNotHasKey( 'content', $data );
     918        $this->assertArrayNotHasKey( 'excerpt', $data );
     919
    894920        $this->assertEquals( get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ), $data['alt_text'] );
    895         $this->assertEquals( $attachment->post_excerpt, $data['caption'] );
    896         $this->assertEquals( $attachment->post_content, $data['description'] );
     921        if ( 'edit' === $context ) {
     922            $this->assertEquals( $attachment->post_excerpt, $data['caption']['raw'] );
     923            $this->assertEquals( $attachment->post_content, $data['description']['raw'] );
     924        } else {
     925            $this->assertFalse( isset( $data['caption']['raw'] ) );
     926            $this->assertFalse( isset( $data['description']['raw'] ) );
     927        }
    897928        $this->assertTrue( isset( $data['media_details'] ) );
    898929
Note: See TracChangeset for help on using the changeset viewer.