wordpress-develop/tests/phpunit/tests/query/invalidQueries.php
Sergey Biryukov ddb409edca Build/Test Tools: Implement use of the void solution.
> PHPUnit 8.0.0 introduced a `void` return type declaration to the "fixture" methods – `setUpBeforeClass()`, `setUp()`, `tearDown()` and `tearDownAfterClass()`. As the `void` return type was not introduced until PHP 7.1, this makes it more difficult to create cross-version compatible tests when using fixtures, due to signature mismatches.
>
> The `Yoast\PHPUnitPolyfills\TestCases\TestCase` overcomes the signature mismatch by having two versions. The correct one will be loaded depending on the PHPUnit version being used.
>
> When using this TestCase, if an individual test, or another TestCase which extends this TestCase, needs to overload any of the "fixture" methods, it should do so by using a snake_case variant of the original fixture method name, i.e. `set_up_before_class()`, `set_up()`, `assert_pre_conditions()`, `assert_post_conditions()`, `tear_down()`, and `tear_down_after_class()`.
>
> The snake_case methods will automatically be called by PHPUnit.
>
> > IMPORTANT: The snake_case methods should not call the PHPUnit parent, i.e. do not use `parent::setUp()` from within an overloaded `set_up()` method. If necessary, DO call `parent::set_up()`.

Reference: https://github.com/Yoast/PHPUnit-Polyfills#testcases

This commit renames all declared fixture methods, and calls to parent versions of those fixture methods, from camelCase to snake_case.

Follow-up to [51559-51567].

Props jrf, hellofromTonya, johnbillion, netweb, dd32, pputzer, SergeyBiryukov.
See #46149.

git-svn-id: https://develop.svn.wordpress.org/trunk@51568 602fd350-edb4-49c9-b593-d223f7449a82
2021-08-07 10:29:41 +00:00

167 lines
3.9 KiB
PHP

<?php
/**
* @group query
*/
class Tests_Query_InvalidQueries extends WP_UnitTestCase {
/**
* Store last query generated by WP_Query.
*
* @var string
*/
public static $last_posts_request;
/**
* Author for creating posts.
*
* @var int
*/
public static $author_id;
/**
* Shared fixture page IDs.
*
* @var int[]
*/
public static $page_ids;
/**
* Shared fixture post IDs.
*
* @var int[]
*/
public static $post_ids;
/**
* Generate shared fixtures.
*
* @param WP_UnitTest_Factory $factory Test suite factory.
*/
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$author_id = $factory->user->create();
foreach ( array( 'publish', 'private' ) as $status ) {
self::$page_ids[ $status ] = $factory->post->create(
array(
'post_type' => 'page',
'post_status' => $status,
'post_author' => self::$author_id,
)
);
self::$post_ids[ $status ] = $factory->post->create(
array(
'post_status' => $status,
'post_author' => self::$author_id,
)
);
}
}
/**
* Set up prior to each test.
*/
public function set_up() {
parent::set_up();
// Clean up variable before each test.
self::$last_posts_request = '';
// Store last query for tests.
add_filter( 'posts_request', array( $this, '_set_last_posts_request' ) );
}
/**
* Filter to store last SQL query generated by WP_Query.
*
* @param string $request Generated SQL query.
* @return string Unmodified SQL query.
*/
public function _set_last_posts_request( $request ) {
self::$last_posts_request = $request;
return $request;
}
/**
* Test WP Query with an invalid post type.
*
* @ticket 48556
*/
public function test_unregistered_post_type_wp_query() {
global $wpdb;
$query = new WP_Query( array( 'post_type' => 'unregistered_cpt' ) );
$posts = $query->get_posts();
$this->assertStringContainsString( "{$wpdb->posts}.post_type = 'unregistered_cpt'", self::$last_posts_request );
$this->assertStringContainsString( "{$wpdb->posts}.post_status = 'publish'", self::$last_posts_request );
$this->assertCount( 0, $posts );
}
/**
* Test WP Query with an invalid post type in a mutiple post type query.
*
* @ticket 48556
*/
public function test_unregistered_post_type_wp_query_multiple_post_types() {
global $wpdb;
$query = new WP_Query(
array(
'post_type' => array( 'unregistered_cpt', 'page' ),
)
);
$posts = $query->get_posts();
$this->assertStringContainsString( "{$wpdb->posts}.post_type = 'unregistered_cpt'", self::$last_posts_request );
$this->assertCount( 1, $posts, 'the valid `page` post type should still return one post' );
}
/**
* Test WP Query with an invalid post type specified in the URL.
*
* @ticket 48556
*/
public function test_unregistered_post_type_goto() {
global $wpdb, $wp_query;
$this->go_to( home_url( '?post_type=unregistered_cpt' ) );
$this->assertStringContainsString( "{$wpdb->posts}.post_type = 'unregistered_cpt'", self::$last_posts_request );
$this->assertStringContainsString( "{$wpdb->posts}.post_status = 'publish'", self::$last_posts_request );
// $wp_query recovers to the post type "post" and is expected to return one.
$this->assertCount( 1, $wp_query->get_posts() );
}
/**
* Ensure deprecated static parameter has no effect on queries.
*/
public function test_deprecated_parameters_have_no_effect_on_page() {
$query = new WP_Query(
array(
'static' => 'a',
'post_type' => 'page',
)
);
$posts = $query->get_posts();
// Only the published page should be returned.
$this->assertCount( 1, $posts );
}
/**
* Ensure deprecated static parameter has no effect on queries.
*/
public function test_deprecated_parameters_have_no_effect_on_post() {
$query = new WP_Query(
array(
'static' => 'a',
)
);
$posts = $query->get_posts();
// Only the published post should be returned.
$this->assertCount( 1, $posts );
}
}