Changeset 59544
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
r59453 r59544 347 347 $args = $this->prepare_tax_query( $args, $request ); 348 348 349 if ( ! empty($request['format'] ) ) {349 if ( isset( $registered['format'], $request['format'] ) ) { 350 350 $formats = $request['format']; 351 351 /* -
trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php
r59235 r59544 5509 5509 5510 5510 /** 5511 * Test the REST API support for the standard post format.5511 * Test the REST API ignores the post format parameter for post types that do not support them. 5512 5512 * 5513 * @ticket 62646 5513 5514 * @ticket 62014 5514 5515 * 5515 5516 * @covers WP_REST_Posts_Controller::get_items 5516 5517 */ 5517 public function test_standard_post_format_ support() {5518 public function test_standard_post_format_ignored_for_post_types_that_do_not_support_them() { 5518 5519 $initial_theme_support = get_theme_support( 'post-formats' ); 5519 5520 add_theme_support( 'post-formats', array( 'aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio', 'chat' ) ); 5520 5521 5521 $post_id =self::factory()->post->create(5522 array( 5523 'post_type' => 'p ost',5522 self::factory()->post->create( 5523 array( 5524 'post_type' => 'page', 5524 5525 'post_status' => 'publish', 5525 5526 ) 5526 5527 ); 5527 set_post_format( $post_id, 'aside' ); 5528 5529 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 5530 $request->set_param( 'format', array( 'standard' ) ); 5531 $request->set_param( 'per_page', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); 5528 5529 $request = new WP_REST_Request( 'GET', '/wp/v2/pages' ); 5530 $request->set_param( 'format', 'invalid_type' ); 5532 5531 5533 5532 $response = rest_get_server()->dispatch( $request ); … … 5545 5544 } 5546 5545 5547 $this->assertCount( 3, $response->get_data(), 'The response should only include standard post formats' );5548 } 5549 5550 /** 5551 * Test the REST API support for post formats.5546 $this->assertCount( 1, $response->get_data(), 'The response should ignore the post format parameter' ); 5547 } 5548 5549 /** 5550 * Test the REST API support for the standard post format. 5552 5551 * 5553 5552 * @ticket 62014 … … 5555 5554 * @covers WP_REST_Posts_Controller::get_items 5556 5555 */ 5557 public function test_ post_format_support() {5556 public function test_standard_post_format_support() { 5558 5557 $initial_theme_support = get_theme_support( 'post-formats' ); 5559 5558 add_theme_support( 'post-formats', array( 'aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio', 'chat' ) ); … … 5568 5567 5569 5568 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 5570 $request->set_param( 'format', array( 'aside' ) ); 5571 5572 $response_aside = rest_get_server()->dispatch( $request ); 5573 5574 $request->set_param( 'format', array( 'invalid_format' ) ); 5575 $response_invalid = rest_get_server()->dispatch( $request ); 5569 $request->set_param( 'format', array( 'standard' ) ); 5570 $request->set_param( 'per_page', REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); 5571 5572 $response = rest_get_server()->dispatch( $request ); 5576 5573 5577 5574 /* … … 5587 5584 } 5588 5585 5589 $this->assertCount( 1, $response_aside->get_data(), 'Only one post is expected to be returned.' ); 5590 $this->assertErrorResponse( 'rest_invalid_param', $response_invalid, 400, 'An invalid post format should return an error' ); 5591 } 5592 5593 /** 5594 * Test the REST API support for multiple post formats. 5586 $this->assertCount( 3, $response->get_data(), 'The response should only include standard post formats' ); 5587 } 5588 5589 /** 5590 * Test the REST API support for post formats. 5595 5591 * 5596 5592 * @ticket 62014 … … 5598 5594 * @covers WP_REST_Posts_Controller::get_items 5599 5595 */ 5600 public function test_ multiple_post_format_support() {5596 public function test_post_format_support() { 5601 5597 $initial_theme_support = get_theme_support( 'post-formats' ); 5602 5598 add_theme_support( 'post-formats', array( 'aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio', 'chat' ) ); … … 5610 5606 set_post_format( $post_id, 'aside' ); 5611 5607 5612 $post_id_2 = self::factory()->post->create( 5613 array( 5614 'post_type' => 'post', 5615 'post_status' => 'publish', 5616 ) 5617 ); 5618 set_post_format( $post_id_2, 'gallery' ); 5619 5620 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 5621 $request->set_param( 'format', array( 'aside', 'gallery' ) ); 5622 5623 $response = rest_get_server()->dispatch( $request ); 5608 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 5609 $request->set_param( 'format', array( 'aside' ) ); 5610 5611 $response_aside = rest_get_server()->dispatch( $request ); 5612 5613 $request->set_param( 'format', array( 'invalid_format' ) ); 5614 $response_invalid = rest_get_server()->dispatch( $request ); 5624 5615 5625 5616 /* … … 5635 5626 } 5636 5627 5628 $this->assertCount( 1, $response_aside->get_data(), 'Only one post is expected to be returned.' ); 5629 $this->assertErrorResponse( 'rest_invalid_param', $response_invalid, 400, 'An invalid post format should return an error' ); 5630 } 5631 5632 /** 5633 * Test the REST API support for multiple post formats. 5634 * 5635 * @ticket 62014 5636 * 5637 * @covers WP_REST_Posts_Controller::get_items 5638 */ 5639 public function test_multiple_post_format_support() { 5640 $initial_theme_support = get_theme_support( 'post-formats' ); 5641 add_theme_support( 'post-formats', array( 'aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio', 'chat' ) ); 5642 5643 $post_id = self::factory()->post->create( 5644 array( 5645 'post_type' => 'post', 5646 'post_status' => 'publish', 5647 ) 5648 ); 5649 set_post_format( $post_id, 'aside' ); 5650 5651 $post_id_2 = self::factory()->post->create( 5652 array( 5653 'post_type' => 'post', 5654 'post_status' => 'publish', 5655 ) 5656 ); 5657 set_post_format( $post_id_2, 'gallery' ); 5658 5659 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); 5660 $request->set_param( 'format', array( 'aside', 'gallery' ) ); 5661 5662 $response = rest_get_server()->dispatch( $request ); 5663 5664 /* 5665 * Restore the initial post formats support. 5666 * 5667 * This needs to be done prior to the assertions to avoid unexpected 5668 * results for other tests should an assertion fail. 5669 */ 5670 if ( $initial_theme_support ) { 5671 add_theme_support( 'post-formats', $initial_theme_support[0] ); 5672 } else { 5673 remove_theme_support( 'post-formats' ); 5674 } 5675 5637 5676 $this->assertCount( 2, $response->get_data(), 'Two posts are expected to be returned' ); 5638 5677 }
Note: See TracChangeset
for help on using the changeset viewer.