From e14d94858f7f16d15de4286924d9ae4b7f3d1d7c Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 8 Aug 2022 14:38:08 +0000 Subject: [PATCH] Tests: Improve the test for not throwing a warning on malformed date queries. * Make it specifically about `wp_resolve_numeric_slug_conflicts()`, the function that was throwing an `Undefined array key "year"` PHP warning for malformed date requests. * Move the test under the `rewrite` component and make its name a bit more descriptive. * Check the return result of the function instead of performing a dummy assertion. * Use named array keys in the data provider for clarity. * Add missing `@covers` tag. Follow-up to [32648], [53857]. Props costdev, peterwilsoncc, 1naveengiri, mukesh27, SergeyBiryukov. See #52252, #45513. git-svn-id: https://develop.svn.wordpress.org/trunk@53861 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/query.php | 67 ----------------- .../rewrite/wpResolveNumericSlugConflicts.php | 71 +++++++++++++++++++ 2 files changed, 71 insertions(+), 67 deletions(-) create mode 100644 tests/phpunit/tests/rewrite/wpResolveNumericSlugConflicts.php diff --git a/tests/phpunit/tests/query.php b/tests/phpunit/tests/query.php index 8c9d18b520..9a8a9b3ffd 100644 --- a/tests/phpunit/tests/query.php +++ b/tests/phpunit/tests/query.php @@ -2,13 +2,6 @@ class Tests_Query extends WP_UnitTestCase { - /** - * Fixed date post ID. - * - * @var int - */ - public static $post_with_date; - public function set_up() { parent::set_up(); @@ -16,15 +9,6 @@ class Tests_Query extends WP_UnitTestCase { create_initial_taxonomies(); } - public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { - self::$post_with_date = $factory->post->create( - array( - 'post_date' => '2020-01-05 12:00:00', - 'post_name' => 'post-with-date', - ) - ); - } - /** * @ticket 24785 */ @@ -713,57 +697,6 @@ class Tests_Query extends WP_UnitTestCase { $this->assertSame( 'term1', get_query_var( 'term' ) ); } - /** - * @ticket 52252 - * @dataProvider data_malformed_date_queries - * - * @param string $permalink_structure Permalink structure. - * @param array $query_vars Querystring parameteres. - */ - public function test_malformed_date_queries( $permalink_structure, $query_vars ) { - $this->set_permalink_structure( $permalink_structure ); - $this->go_to( add_query_arg( $query_vars, home_url() ) ); - - /* - * Ticket 52252 was to prevent notices from being thrown - * if the date query is malformed. - * - * The test will automatically fail if the function triggers a notice, - * so this dummy assertion is just for accurate stats. - */ - $this->assertTrue( true ); - } - - /** - * Data provider for test_malformed_date_queries. - * - * @return array Test data. - */ - public function data_malformed_date_queries() { - return array( - '/%postname%/ with missing year' => array( - '/%postname%/', - array( - 'monthnum' => 1, - 'day' => 15, - ), - ), - '/%postname%/ with month only' => array( - '/%postname%/', - array( - 'monthnum' => 1, - ), - ), - '/%year%/%postname%/ with missing month' => array( - '/%year%/%postname%/', - array( - 'year' => 2020, - 'day' => 15, - ), - ), - ); - } - /** * @ticket 55100 */ diff --git a/tests/phpunit/tests/rewrite/wpResolveNumericSlugConflicts.php b/tests/phpunit/tests/rewrite/wpResolveNumericSlugConflicts.php new file mode 100644 index 0000000000..7a5232bc86 --- /dev/null +++ b/tests/phpunit/tests/rewrite/wpResolveNumericSlugConflicts.php @@ -0,0 +1,71 @@ +post->create( + array( + 'post_date' => '2020-01-05 12:00:00', + 'post_name' => 'post-with-date', + ) + ); + } + + /** + * @ticket 52252 + * @dataProvider data_should_not_throw_warning_for_malformed_date_queries + * + * @param string $permalink_structure Permalink structure. + * @param array $query_vars Query string parameters. + */ + public function test_should_not_throw_warning_for_malformed_date_queries( $permalink_structure, $query_vars ) { + $this->set_permalink_structure( $permalink_structure ); + + /* + * For malformed date queries, the function is unable to identify the requested post, + * and just returns the initial query vars. + */ + $this->assertSame( $query_vars, wp_resolve_numeric_slug_conflicts( $query_vars ) ); + } + + /** + * Data provider for test_should_not_throw_warning_for_malformed_date_queries(). + * + * @return array Test data. + */ + public function data_should_not_throw_warning_for_malformed_date_queries() { + return array( + '/%postname%/ with missing year' => array( + 'permalink_structure' => '/%postname%/', + 'query' => array( + 'monthnum' => 1, + 'day' => 15, + ), + ), + '/%postname%/ with month only' => array( + 'permalink_structure' => '/%postname%/', + 'query' => array( + 'monthnum' => 1, + ), + ), + '/%year%/%postname%/ with missing month' => array( + 'permalink_structure' => '/%year%/%postname%/', + 'query' => array( + 'year' => 2020, + 'day' => 15, + ), + ), + ); + } +}