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
This commit is contained in:
Greg Ziółkowski
2021-05-21 06:28:59 +00:00
parent 5a2c3706ee
commit e7dbf1d5f1

View File

@@ -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( '<!-- wp:example {"ok":true} -->a<!-- wp:example /-->b<!-- /wp:example -->' );
$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( '<!-- wp:example {"ok":true} -->a<!-- wp:example /-->b<!-- /wp:example -->' );
$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( '<!-- wp:example {"ok":true} -->a<!-- wp:example /-->b<!-- /wp:example -->' );
$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( '<!-- wp:example {"ok":true} -->a<!-- wp:example /-->b<!-- /wp:example -->' );
$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( '<!-- wp:example {"ok":true} -->a<!-- wp:example /-->b<!-- /wp:example -->' );
$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,
)
);
}
}