From 561287c861216c9912dde1f4a459508b3eb21818 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Fri, 24 Mar 2023 17:10:44 +0000 Subject: [PATCH] Tests: Split the tests from `post/template.php` into individual test classes. This aims to bring some consistency to the location of post template function tests, as well as to make the tests more discoverable and easier to expand. Includes: * Adding `@covers` tags. * Renaming `get_post_parent()` and `has_post_parent()` tests to match the names of the functions. Follow-up to [28398], [31522], [34654], [34950], [50127], [50396], [54717], [54726], [55590]. See #57841. git-svn-id: https://develop.svn.wordpress.org/trunk@55591 602fd350-edb4-49c9-b593-d223f7449a82 --- .../tests/post/getPageTemplateSlug.php | 75 +++++ tests/phpunit/tests/post/getPostParent.php | 72 +++++ tests/phpunit/tests/post/nav-menu.php | 46 ++- tests/phpunit/tests/post/template.php | 304 ------------------ tests/phpunit/tests/post/wpLinkPages.php | 136 ++++++++ 5 files changed, 328 insertions(+), 305 deletions(-) create mode 100644 tests/phpunit/tests/post/getPageTemplateSlug.php create mode 100644 tests/phpunit/tests/post/getPostParent.php delete mode 100644 tests/phpunit/tests/post/template.php create mode 100644 tests/phpunit/tests/post/wpLinkPages.php diff --git a/tests/phpunit/tests/post/getPageTemplateSlug.php b/tests/phpunit/tests/post/getPageTemplateSlug.php new file mode 100644 index 0000000000..5a735350f5 --- /dev/null +++ b/tests/phpunit/tests/post/getPageTemplateSlug.php @@ -0,0 +1,75 @@ +post->create( + array( + 'post_type' => 'page', + ) + ); + + $this->assertSame( '', get_page_template_slug( $page_id ) ); + + update_post_meta( $page_id, '_wp_page_template', 'default' ); + $this->assertSame( '', get_page_template_slug( $page_id ) ); + + update_post_meta( $page_id, '_wp_page_template', 'example.php' ); + $this->assertSame( 'example.php', get_page_template_slug( $page_id ) ); + } + + /** + * @ticket 31389 + */ + public function test_get_page_template_slug_from_loop() { + $page_id = self::factory()->post->create( + array( + 'post_type' => 'page', + ) + ); + + update_post_meta( $page_id, '_wp_page_template', 'example.php' ); + $this->go_to( get_permalink( $page_id ) ); + + $this->assertSame( 'example.php', get_page_template_slug() ); + } + + /** + * @ticket 31389 + * @ticket 18375 + */ + public function test_get_page_template_slug_non_page() { + $post_id = self::factory()->post->create(); + + $this->assertSame( '', get_page_template_slug( $post_id ) ); + + update_post_meta( $post_id, '_wp_page_template', 'default' ); + + $this->assertSame( '', get_page_template_slug( $post_id ) ); + + update_post_meta( $post_id, '_wp_page_template', 'example.php' ); + $this->assertSame( 'example.php', get_page_template_slug( $post_id ) ); + } + + /** + * @ticket 18375 + */ + public function test_get_page_template_slug_non_page_from_loop() { + $post_id = self::factory()->post->create(); + + update_post_meta( $post_id, '_wp_page_template', 'example.php' ); + + $this->go_to( get_permalink( $post_id ) ); + + $this->assertSame( 'example.php', get_page_template_slug() ); + } +} diff --git a/tests/phpunit/tests/post/getPostParent.php b/tests/phpunit/tests/post/getPostParent.php new file mode 100644 index 0000000000..e66b458513 --- /dev/null +++ b/tests/phpunit/tests/post/getPostParent.php @@ -0,0 +1,72 @@ + 'publish', + 'post_type' => 'page', + ); + + // Insert two initial posts. + $parent_id = self::factory()->post->create( $post ); + $child_id = self::factory()->post->create( $post ); + + // Test if the function returns null by default. + $parent = get_post_parent( $child_id ); + $this->assertNull( $parent ); + + // Update child post with a parent. + wp_update_post( + array( + 'ID' => $child_id, + 'post_parent' => $parent_id, + ) + ); + + // Test if the function returns the parent object. + $parent = get_post_parent( $child_id ); + $this->assertNotNull( $parent ); + $this->assertSame( $parent_id, $parent->ID ); + } + + /** + * @ticket 33045 + */ + public function test_has_post_parent() { + $post = array( + 'post_status' => 'publish', + 'post_type' => 'page', + ); + + // Insert two initial posts. + $parent_id = self::factory()->post->create( $post ); + $child_id = self::factory()->post->create( $post ); + + // Test if the function returns false by default. + $parent = has_post_parent( $child_id ); + $this->assertFalse( $parent ); + + // Update child post with a parent. + wp_update_post( + array( + 'ID' => $child_id, + 'post_parent' => $parent_id, + ) + ); + + // Test if the function returns true for a child post. + $parent = has_post_parent( $child_id ); + $this->assertTrue( $parent ); + } +} diff --git a/tests/phpunit/tests/post/nav-menu.php b/tests/phpunit/tests/post/nav-menu.php index d02a9b1767..918b76e8e1 100644 --- a/tests/phpunit/tests/post/nav-menu.php +++ b/tests/phpunit/tests/post/nav-menu.php @@ -15,6 +15,51 @@ class Tests_Post_Nav_Menu extends WP_UnitTestCase { $this->menu_id = wp_create_nav_menu( 'foo' ); } + /** + * @ticket 11095 + * @ticket 33974 + */ + public function test_wp_page_menu_wp_nav_menu_fallback() { + $pages = self::factory()->post->create_many( 3, array( 'post_type' => 'page' ) ); + + // No menus + wp_nav_menu() falls back to wp_page_menu(). + $menu = wp_nav_menu( array( 'echo' => false ) ); + + // After falling back, the 'before' argument should be set and output as '