From 8f219cdd78103f9b5159b110700009cbefe00f5a Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Wed, 15 Jun 2022 14:07:24 +0000 Subject: [PATCH] Tests: Some improvements for REST API cache priming tests: * Give the test methods more specific names and move them closer together. * Correct the `@covers` tags. Follow-up to [53499], [53504], [53506], [53507], [53508]. See #55593, #55652. git-svn-id: https://develop.svn.wordpress.org/trunk@53509 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/post/nav-menu.php | 8 +- .../tests/rest-api/rest-posts-controller.php | 91 ++++++++++--------- 2 files changed, 50 insertions(+), 49 deletions(-) diff --git a/tests/phpunit/tests/post/nav-menu.php b/tests/phpunit/tests/post/nav-menu.php index 2e38640fd2..4c2f2146f7 100644 --- a/tests/phpunit/tests/post/nav-menu.php +++ b/tests/phpunit/tests/post/nav-menu.php @@ -204,9 +204,9 @@ class Tests_Post_Nav_Menu extends WP_UnitTestCase { /** * @ticket 55620 - * @covers update_menu_item_cache + * @covers ::update_menu_item_cache */ - public function test_update_menu_item_cache_primed_posts() { + public function test_update_menu_item_cache_primes_posts() { $post_id = self::factory()->post->create(); wp_update_nav_menu_item( $this->menu_id, @@ -235,9 +235,9 @@ class Tests_Post_Nav_Menu extends WP_UnitTestCase { /** * @ticket 55620 - * @covers update_menu_item_cache + * @covers ::update_menu_item_cache */ - public function test_update_menu_item_cache_primed_terms() { + public function test_update_menu_item_cache_primes_terms() { register_taxonomy( 'wptests_tax', 'post', array( 'hierarchical' => true ) ); $term_id = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) ); wp_update_nav_menu_item( diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php index 6a7ceeece6..1f6187a3d0 100644 --- a/tests/phpunit/tests/rest-api/rest-posts-controller.php +++ b/tests/phpunit/tests/rest-api/rest-posts-controller.php @@ -1517,8 +1517,9 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te /** * @ticket 55592 * @covers WP_REST_Posts_Controller::get_items + * @covers ::update_post_thumbnail_cache */ - public function test_get_items_with_featured_media() { + public function test_get_items_primes_thumbnail_cache_for_featured_media() { $file = DIR_TESTDATA . '/images/canola.jpg'; $attachment_ids = array(); $post_ids = array(); @@ -1534,7 +1535,7 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te set_post_thumbnail( $post_ids[ $i ], $attachment_ids[ $i ] ); } - // Attachment creation warms thumbnail ids. Needs clean up for test. + // Attachment creation warms thumbnail IDs. Needs clean up for test. wp_cache_delete_multiple( $attachment_ids, 'posts' ); $filter = new MockAction(); @@ -1550,6 +1551,49 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $this->assertEqualSets( $attachment_ids, $last[1] ); } + /** + * @ticket 55593 + * @covers WP_REST_Posts_Controller::get_items + * @covers ::update_post_parent_caches + */ + public function test_get_items_primes_parent_post_caches() { + $parent_id1 = self::$post_ids[0]; + $parent_id2 = self::$post_ids[1]; + $parent_ids = array( $parent_id2, $parent_id1 ); + + $this->factory->attachment->create_object( + DIR_TESTDATA . '/images/canola.jpg', + $parent_id1, + array( + 'post_mime_type' => 'image/jpeg', + 'post_excerpt' => 'A sample caption 1', + ) + ); + + $this->factory->attachment->create_object( + DIR_TESTDATA . '/images/canola.jpg', + $parent_id2, + array( + 'post_mime_type' => 'image/jpeg', + 'post_excerpt' => 'A sample caption 2', + ) + ); + + // Attachment creation warms parent IDs. Needs clean up for test. + wp_cache_delete_multiple( $parent_ids, 'posts' ); + + $filter = new MockAction(); + add_filter( 'update_post_metadata_cache', array( $filter, 'filter' ), 10, 2 ); + + $request = new WP_REST_Request( 'GET', '/wp/v2/media' ); + rest_get_server()->dispatch( $request ); + + $args = $filter->get_args(); + $last = end( $args ); + $this->assertIsArray( $last, 'The last value is not an array' ); + $this->assertEqualSets( $parent_ids, $last[1] ); + } + public function test_get_items_pagination_headers() { $total_posts = self::$total_posts; $total_pages = (int) ceil( $total_posts / 10 ); @@ -1787,49 +1831,6 @@ class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te $this->assertSame( $formats, $data['schema']['properties']['format']['enum'] ); } - /** - * @ticket 55593 - * @covers WP_REST_Posts_Controller::get_items - * @covers update_post_parent_caches - */ - public function test_get_items_parent_ids_primed() { - $parent_id1 = self::$post_ids[0]; - $parent_id2 = self::$post_ids[1]; - $parent_ids = array( $parent_id2, $parent_id1 ); - - $this->factory->attachment->create_object( - DIR_TESTDATA . '/images/canola.jpg', - $parent_id1, - array( - 'post_mime_type' => 'image/jpeg', - 'post_excerpt' => 'A sample caption 1', - ) - ); - - $this->factory->attachment->create_object( - DIR_TESTDATA . '/images/canola.jpg', - $parent_id2, - array( - 'post_mime_type' => 'image/jpeg', - 'post_excerpt' => 'A sample caption 2', - ) - ); - - // Attachment creation warms parent ids. Needs clean up for test. - wp_cache_delete_multiple( $parent_ids, 'posts' ); - - $filter = new MockAction(); - add_filter( 'update_post_metadata_cache', array( $filter, 'filter' ), 10, 2 ); - - $request = new WP_REST_Request( 'GET', '/wp/v2/media' ); - rest_get_server()->dispatch( $request ); - - $args = $filter->get_args(); - $last = end( $args ); - $this->assertIsArray( $last, 'The last value is not an array' ); - $this->assertEqualSets( $parent_ids, $last[1] ); - } - public function test_get_item() { $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); $response = rest_get_server()->dispatch( $request );