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
This commit is contained in:
Sergey Biryukov
2022-08-08 14:38:08 +00:00
parent 374052e29a
commit e14d94858f
2 changed files with 71 additions and 67 deletions
-67
View File
@@ -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
*/
@@ -0,0 +1,71 @@
<?php
/**
* @group rewrite
* @covers ::wp_resolve_numeric_slug_conflicts
*/
class Tests_Rewrite_wpResolveNumericSlugConflicts extends WP_UnitTestCase {
/**
* Fixed date post ID.
*
* @var int
*/
public static $post_with_date;
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 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,
),
),
);
}
}