From 34dcf862dea9ba4aa677e5923700b2363d022d9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Zi=C3=83=C2=B3=C3=85=E2=80=9Akowski?= Date: Wed, 13 Apr 2022 07:47:15 +0000 Subject: [PATCH] Tests: Add unit tests for rendering in Comment Template block Follow-up [53138]. Props darerodz. See #55505. git-svn-id: https://develop.svn.wordpress.org/trunk@53172 602fd350-edb4-49c9-b593-d223f7449a82 --- .../tests/blocks/renderCommentTemplate.php | 148 ++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 tests/phpunit/tests/blocks/renderCommentTemplate.php diff --git a/tests/phpunit/tests/blocks/renderCommentTemplate.php b/tests/phpunit/tests/blocks/renderCommentTemplate.php new file mode 100644 index 0000000000..a278dbfdd0 --- /dev/null +++ b/tests/phpunit/tests/blocks/renderCommentTemplate.php @@ -0,0 +1,148 @@ +post->create_and_get( + array( + 'post_type' => 'dogs', + 'post_status' => 'publish', + 'post_name' => 'metaldog', + 'post_title' => 'Metal Dog', + 'post_content' => 'Metal Dog content', + 'post_excerpt' => 'Metal Dog', + ) + ); + + self::$comment_ids = self::factory()->comment->create_post_comments( + self::$custom_post->ID, + 1, + array( + 'comment_author' => 'Test', + 'comment_author_email' => 'test@example.org', + 'comment_content' => 'Hello world', + ) + ); + } + + /** + * @ticket 55505 + * @covers ::build_comment_query_vars_from_block + */ + function test_build_comment_query_vars_from_block_with_context() { + $parsed_blocks = parse_blocks( + '' + ); + + $block = new WP_Block( + $parsed_blocks[0], + array( + 'postId' => self::$custom_post->ID, + ) + ); + + $this->assertEquals( + array( + 'orderby' => 'comment_date_gmt', + 'order' => 'ASC', + 'status' => 'approve', + 'no_found_rows' => false, + 'update_comment_meta_cache' => false, + 'post_id' => self::$custom_post->ID, + 'hierarchical' => 'threaded', + 'number' => 5, + 'paged' => 1, + ), + build_comment_query_vars_from_block( $block ) + ); + } + + /** + * @ticket 55505 + * @covers ::build_comment_query_vars_from_block + */ + function test_build_comment_query_vars_from_block_no_context() { + $parsed_blocks = parse_blocks( + '' + ); + + $block = new WP_Block( $parsed_blocks[0] ); + + $this->assertEquals( + array( + 'orderby' => 'comment_date_gmt', + 'order' => 'ASC', + 'status' => 'approve', + 'no_found_rows' => false, + 'update_comment_meta_cache' => false, + 'hierarchical' => 'threaded', + 'number' => 5, + 'paged' => 1, + ), + build_comment_query_vars_from_block( $block ) + ); + } + + /** + * Test that both "Older Comments" and "Newer Comments" are displayed in the correct order + * inside the Comment Query Loop when we enable pagination on Discussion Settings. + * In order to do that, it should exist a query var 'cpage' set with the $comment_args['paged'] value. + * + * @ticket 55505 + * @covers ::build_comment_query_vars_from_block + */ + function test_build_comment_query_vars_from_block_sets_cpage_var() { + + // This could be any number, we set a fixed one instead of a random for better performance. + $comment_query_max_num_pages = 5; + // We subtract 1 because we created 1 comment at the beginning. + $post_comments_numbers = ( self::$per_page * $comment_query_max_num_pages ) - 1; + self::factory()->comment->create_post_comments( + self::$custom_post->ID, + $post_comments_numbers, + array( + 'comment_author' => 'Test', + 'comment_author_email' => 'test@example.org', + 'comment_content' => 'Hello world', + ) + ); + $parsed_blocks = parse_blocks( + '' + ); + + $block = new WP_Block( + $parsed_blocks[0], + array( + 'postId' => self::$custom_post->ID, + 'comments/inherit' => true, + ) + ); + $actual = build_comment_query_vars_from_block( $block ); + $this->assertSame( $comment_query_max_num_pages, $actual['paged'] ); + $this->assertSame( $comment_query_max_num_pages, get_query_var( 'cpage' ) ); + } +}