From 5eef2bbb2a476fc3f94f7d7c2d89ff01b9b74f82 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 12 Dec 2023 12:19:38 +0000 Subject: [PATCH] Tests: Add unit tests for `wp_checkdate()`. Follow-up to [21922]. Props pbearne, ironprogrammer, antonvlasenko, SergeyBiryukov. Fixes #59825. git-svn-id: https://develop.svn.wordpress.org/trunk@57184 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/date/wpCheckdate.php | 67 ++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 tests/phpunit/tests/date/wpCheckdate.php diff --git a/tests/phpunit/tests/date/wpCheckdate.php b/tests/phpunit/tests/date/wpCheckdate.php new file mode 100644 index 0000000000..204489200c --- /dev/null +++ b/tests/phpunit/tests/date/wpCheckdate.php @@ -0,0 +1,67 @@ +assertSame( $expected, wp_checkdate( $month, $day, $year, $source_date ) ); + } + + /** + * Data provider for test_wp_checkdate(). + * + * @return array + */ + public function data_wp_checkdate() { + return array( + 'integers' => array( 1, 1, 1, '1-1-1', true ), + 'strings' => array( '1', '1', '1', '1-1-1', true ), + 'arbitrary source_date' => array( 1, 1, 1, 'arbitrary source_date', true ), // source_date is only used by the filter. + 'valid day' => array( 2, 29, 2024, '2/29/2024', true ), // 2024 is a leap year. + 'invalid day' => array( 2, 29, 2023, '2/29/2023', false ), // 2023 is not a leap year. + 'invalid month' => array( 99, 1, 1, '1-1-1', false ), // Month must be between 1 and 12. + 'invalid year' => array( 1, 1, 0, '1-1-0', false ), // Year must be between 1 and 32767. + ); + } + + /** + * Checks that the filter overrides the return value. + */ + public function test_wp_checkdate_filter() { + add_filter( + 'wp_checkdate', + static function ( $is_valid_date, $source_date ) { + if ( '2/29/2023' === $source_date ) { + // Date is invalid, but return true anyway. + return true; + } + + return $is_valid_date; + }, + 10, + 2 + ); + + // Test with an invalid date that the filter will return as valid. + $this->assertTrue( wp_checkdate( '2', '29', '2023', '2/29/2023' ) ); + } +}