From e7dbf1d5f14b23b0ed25d725afff5d912eb32197 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Zi=C3=83=C2=B3=C3=85=E2=80=9Akowski?= Date: Fri, 21 May 2021 06:28:59 +0000 Subject: [PATCH] Editor: Add missing unit tests for `construct_wp_query_args` Follow-up for [50929]. Props ntsekouras, jorbin. Fixes #53240. See #52991. git-svn-id: https://develop.svn.wordpress.org/trunk@50944 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/blocks/block.php | 154 +++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) diff --git a/tests/phpunit/tests/blocks/block.php b/tests/phpunit/tests/blocks/block.php index 7a036cb638..f7f6a2c490 100644 --- a/tests/phpunit/tests/blocks/block.php +++ b/tests/phpunit/tests/blocks/block.php @@ -413,4 +413,158 @@ class WP_Block_Test extends WP_UnitTestCase { $this->assertSame( 'abc', $block->render() ); } + /** + * @ticket 52991 + */ + public function test_build_query_vars_from_query_block() { + $this->registry->register( + 'core/example', + array( 'uses_context' => array( 'query' ) ) + ); + + $parsed_blocks = parse_blocks( 'ab' ); + $parsed_block = $parsed_blocks[0]; + $context = array( + 'query' => array( + 'postType' => 'page', + 'exclude' => array( 1, 2 ), + 'categoryIds' => array( 56 ), + 'orderBy' => 'title', + 'tagIds' => array( 3, 11, 10 ), + ), + ); + $block = new WP_Block( $parsed_block, $context, $this->registry ); + $query = construct_wp_query_args( $block, 1 ); + + $this->assertSame( + $query, + array( + 'post_type' => 'page', + 'order' => 'DESC', + 'orderby' => 'title', + 'post__not_in' => array( 1, 2 ), + 'category__in' => array( 56 ), + 'tag__in' => array( 3, 11, 10 ), + ) + ); + } + + /** + * @ticket 52991 + */ + public function test_build_query_vars_from_query_block_no_context() { + $this->registry->register( 'core/example', array() ); + + $parsed_blocks = parse_blocks( 'ab' ); + $parsed_block = $parsed_blocks[0]; + $block_no_context = new WP_Block( $parsed_block, array(), $this->registry ); + $query = construct_wp_query_args( $block_no_context, 1 ); + + $this->assertSame( + $query, + array( + 'post_type' => 'post', + 'order' => 'DESC', + 'orderby' => 'date', + 'post__not_in' => array(), + ) + ); + } + + /** + * @ticket 52991 + */ + public function test_build_query_vars_from_query_block_first_page() { + $this->registry->register( + 'core/example', + array( 'uses_context' => array( 'query' ) ) + ); + + $parsed_blocks = parse_blocks( 'ab' ); + $parsed_block = $parsed_blocks[0]; + $context = array( + 'query' => array( + 'perPage' => 2, + 'offset' => 0, + ), + ); + $block = new WP_Block( $parsed_block, $context, $this->registry ); + $query = construct_wp_query_args( $block, 1 ); + + $this->assertSame( + $query, + array( + 'post_type' => 'post', + 'order' => 'DESC', + 'orderby' => 'date', + 'post__not_in' => array(), + 'offset' => 0, + 'posts_per_page' => 2, + ) + ); + } + + /** + * @ticket 52991 + */ + public function test_build_query_vars_from_query_block_page_no_offset() { + $this->registry->register( + 'core/example', + array( 'uses_context' => array( 'query' ) ) + ); + + $parsed_blocks = parse_blocks( 'ab' ); + $parsed_block = $parsed_blocks[0]; + $context = array( + 'query' => array( + 'perPage' => 5, + 'offset' => 0, + ), + ); + $block = new WP_Block( $parsed_block, $context, $this->registry ); + $query = construct_wp_query_args( $block, 3 ); + $this->assertSame( + $query, + array( + 'post_type' => 'post', + 'order' => 'DESC', + 'orderby' => 'date', + 'post__not_in' => array(), + 'offset' => 10, + 'posts_per_page' => 5, + ) + ); + } + + /** + * @ticket 52991 + */ + public function test_build_query_vars_from_query_block_page_with_offset() { + $this->registry->register( + 'core/example', + array( 'uses_context' => array( 'query' ) ) + ); + + $parsed_blocks = parse_blocks( 'ab' ); + $parsed_block = $parsed_blocks[0]; + $context = array( + 'query' => array( + 'perPage' => 5, + 'offset' => 2, + ), + ); + $block = new WP_Block( $parsed_block, $context, $this->registry ); + $query = construct_wp_query_args( $block, 3 ); + $this->assertSame( + $query, + array( + 'post_type' => 'post', + 'order' => 'DESC', + 'orderby' => 'date', + 'post__not_in' => array(), + 'offset' => 12, + 'posts_per_page' => 5, + ) + ); + } }