From ac0ed5bdbd02f80b4f200286764bcd3afa92eb72 Mon Sep 17 00:00:00 2001 From: pbearne Date: Tue, 22 Jun 2021 18:41:43 -0400 Subject: [PATCH 1/7] added missing coverage paths --- tests/phpunit/tests/date/wpDate.php | 51 +++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/tests/phpunit/tests/date/wpDate.php b/tests/phpunit/tests/date/wpDate.php index be0ba8013502a..c8f41a20f3e0c 100644 --- a/tests/phpunit/tests/date/wpDate.php +++ b/tests/phpunit/tests/date/wpDate.php @@ -61,4 +61,55 @@ public function test_should_keep_localized_slashes() { $this->assertSame( $string, wp_date( 'F', $datetime->getTimestamp(), $utc ) ); } + + /** + * Test if no timestamp provides + * + * @ticket 53485 + */ + public function test_no_timestamp() { + + $this->assertSame( (string) strtotime( 'now' ), wp_date( 'U' ) ); + } + + /** + * Test if format is set to F weekday_abbrev + * + * @ticket 53485 + */ + public function test_format_F() { + $utc = new DateTimeZone( 'UTC' ); + $datetime = new DateTimeImmutable( '2019-10-17', $utc ); + + $this->assertSame( 'Thu', wp_date( 'D', $datetime->getTimestamp(), $utc ) ); + } + + /** + * Test if format is set to M month_abbrev + * + * @ticket 53485 + */ + public function test_format_M() { + $utc = new DateTimeZone( 'UTC' ); + $datetime = new DateTimeImmutable( '2019-10-17', $utc ); + + $this->assertSame( 'Oct', wp_date( 'M', $datetime->getTimestamp(), $utc ) ); + } + + /** + * Test if format is set to M month_abbrev + * + * @ticket 53485 + */ + public function test_wp_date_filter() { + add_filter( 'wp_date', array( $this, '_test_wp_date_filter' ), 99 ); + + $this->assertSame( 'filtered', wp_date( '') ); + + remove_filter('wp_date', array( $this, '_test_wp_date_filter' ), 99 ); + } + + public function _test_wp_date_filter(){ + return 'filtered'; + } } From 87b79e97a62ebfe091cd1cac8651ca40dc471532 Mon Sep 17 00:00:00 2001 From: pbearne Date: Wed, 23 Jun 2021 08:12:54 -0400 Subject: [PATCH 2/7] added more paths and fix white space --- tests/phpunit/tests/date/wpDate.php | 86 +++++++++++++++++++++++++++-- 1 file changed, 80 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/tests/date/wpDate.php b/tests/phpunit/tests/date/wpDate.php index c8f41a20f3e0c..5f4b13b626c70 100644 --- a/tests/phpunit/tests/date/wpDate.php +++ b/tests/phpunit/tests/date/wpDate.php @@ -73,17 +73,41 @@ public function test_no_timestamp() { } /** - * Test if format is set to F weekday_abbrev + * Test if format is set to D weekday_abbrev * * @ticket 53485 */ - public function test_format_F() { + public function test_format_D() { $utc = new DateTimeZone( 'UTC' ); $datetime = new DateTimeImmutable( '2019-10-17', $utc ); $this->assertSame( 'Thu', wp_date( 'D', $datetime->getTimestamp(), $utc ) ); } + /** + * Test if format is set to F Month + * + * @ticket 53485 + */ + public function test_format_F() { + $utc = new DateTimeZone( 'UTC' ); + $datetime = new DateTimeImmutable( '2019-10-17', $utc ); + + $this->assertSame( 'October', wp_date( 'F', $datetime->getTimestamp(), $utc ) ); + } + + /** + * Test if format is set to L weekday + * + * @ticket 53485 + */ + public function test_format_l() { + $utc = new DateTimeZone( 'UTC' ); + $datetime = new DateTimeImmutable( '2019-10-17', $utc ); + + $this->assertSame( 'Thursday', wp_date( 'l', $datetime->getTimestamp(), $utc ) ); + } + /** * Test if format is set to M month_abbrev * @@ -97,19 +121,69 @@ public function test_format_M() { } /** - * Test if format is set to M month_abbrev + * Test if format is set to a am/pm lowercase + * + * @ticket 53485 + */ + public function test_format_a() { + $utc = new DateTimeZone( 'UTC' ); + $datetime = new DateTimeImmutable( '2019-10-17', $utc ); + + $this->assertSame( 'am', wp_date( 'a', $datetime->getTimestamp(), $utc ) ); + } + + /** + * Test if format is set to A am/pm uppercase + * + * @ticket 53485 + */ + public function test_format_upper_A() { + $utc = new DateTimeZone( 'UTC' ); + $datetime = new DateTimeImmutable( '2019-10-17', $utc ); + + $this->assertSame( 'AM', wp_date( 'A', $datetime->getTimestamp(), $utc ) ); + } + + /** + * Test if format is set to A with an escaped A + * + * @ticket 53485 + */ + public function test_format_slash() { + $utc = new DateTimeZone( 'UTC' ); + $datetime = new DateTimeImmutable( '2019-10-17', $utc ); + + $this->assertSame( 'A AM', wp_date( '\\A A', $datetime->getTimestamp(), $utc ) ); + } + + + /** + * Test if format is set directly + * + * @ticket 53485 + */ + public function test_format() { + $utc = new DateTimeZone( 'UTC' ); + $datetime = new DateTimeImmutable( '2019-10-17', $utc ); + + $this->assertSame( '041', wp_date( 'B', $datetime->getTimestamp(), $utc ) ); + } + + /** + * Test wp_date Filter runs * * @ticket 53485 */ public function test_wp_date_filter() { add_filter( 'wp_date', array( $this, '_test_wp_date_filter' ), 99 ); - $this->assertSame( 'filtered', wp_date( '') ); + $this->assertSame( 'filtered', wp_date( '' ) ); - remove_filter('wp_date', array( $this, '_test_wp_date_filter' ), 99 ); + remove_filter( 'wp_date', array( $this, '_test_wp_date_filter' ), 99 ); } - public function _test_wp_date_filter(){ + public function _test_wp_date_filter() { + return 'filtered'; } } From 71ffcf0016003479d3f8ac3e703a6840dc9e1558 Mon Sep 17 00:00:00 2001 From: pbearne Date: Wed, 23 Jun 2021 09:14:20 -0400 Subject: [PATCH 3/7] replaced strtotime() to with time() --- tests/phpunit/tests/date/wpDate.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/date/wpDate.php b/tests/phpunit/tests/date/wpDate.php index 5f4b13b626c70..91557f050e0b4 100644 --- a/tests/phpunit/tests/date/wpDate.php +++ b/tests/phpunit/tests/date/wpDate.php @@ -69,7 +69,7 @@ public function test_should_keep_localized_slashes() { */ public function test_no_timestamp() { - $this->assertSame( (string) strtotime( 'now' ), wp_date( 'U' ) ); + $this->assertSame( (string) time(), wp_date( 'U' ) ); } /** From b89b7a6578b2f7ed4c2002591bdbb3b230ecdc60 Mon Sep 17 00:00:00 2001 From: costdev Date: Fri, 29 Apr 2022 06:45:58 +0100 Subject: [PATCH 4/7] Update tests. --- tests/phpunit/tests/date/wpDate.php | 144 +++++++++++++--------------- 1 file changed, 68 insertions(+), 76 deletions(-) diff --git a/tests/phpunit/tests/date/wpDate.php b/tests/phpunit/tests/date/wpDate.php index 91557f050e0b4..2241285dc39de 100644 --- a/tests/phpunit/tests/date/wpDate.php +++ b/tests/phpunit/tests/date/wpDate.php @@ -63,127 +63,119 @@ public function test_should_keep_localized_slashes() { } /** - * Test if no timestamp provides + * Tests that the date is formatted with no timestamp provided. * * @ticket 53485 */ - public function test_no_timestamp() { - - $this->assertSame( (string) time(), wp_date( 'U' ) ); + public function test_should_format_date_with_no_timestamp() { + $utc = new DateTimeZone( 'UTC' ); + $this->assertSame( (string) time(), wp_date( 'U', null, $utc ) ); } /** - * Test if format is set to D weekday_abbrev + * Tests that the date is formatted with no timezone provided. * * @ticket 53485 */ - public function test_format_D() { + public function test_should_format_date_with_no_timezone() { $utc = new DateTimeZone( 'UTC' ); $datetime = new DateTimeImmutable( '2019-10-17', $utc ); - - $this->assertSame( 'Thu', wp_date( 'D', $datetime->getTimestamp(), $utc ) ); + $this->assertSame( 'October', wp_date( 'F', $datetime->getTimestamp() ) ); } /** - * Test if format is set to F Month + * Tests that the format is set correctly. * - * @ticket 53485 - */ - public function test_format_F() { - $utc = new DateTimeZone( 'UTC' ); - $datetime = new DateTimeImmutable( '2019-10-17', $utc ); - - $this->assertSame( 'October', wp_date( 'F', $datetime->getTimestamp(), $utc ) ); - } - - /** - * Test if format is set to L weekday + * @dataProvider data_should_format_date * * @ticket 53485 - */ - public function test_format_l() { - $utc = new DateTimeZone( 'UTC' ); - $datetime = new DateTimeImmutable( '2019-10-17', $utc ); - - $this->assertSame( 'Thursday', wp_date( 'l', $datetime->getTimestamp(), $utc ) ); - } - - /** - * Test if format is set to M month_abbrev * - * @ticket 53485 + * @param string $expected The expected result. + * @param string $format The date format. */ - public function test_format_M() { + public function test_should_format_date( $expected, $format ) { $utc = new DateTimeZone( 'UTC' ); $datetime = new DateTimeImmutable( '2019-10-17', $utc ); - $this->assertSame( 'Oct', wp_date( 'M', $datetime->getTimestamp(), $utc ) ); + $this->assertSame( $expected, wp_date( $format, $datetime->getTimestamp(), $utc ) ); } /** - * Test if format is set to a am/pm lowercase + * Data provider. * - * @ticket 53485 + * @return array */ - public function test_format_a() { - $utc = new DateTimeZone( 'UTC' ); - $datetime = new DateTimeImmutable( '2019-10-17', $utc ); - - $this->assertSame( 'am', wp_date( 'a', $datetime->getTimestamp(), $utc ) ); + public function data_should_format_date() { + return array( + 'Swatch Internet Time' => array( + 'expected' => '041', + 'format' => 'B', + ), + 'Ante meridiem and Post meridiem (uppercase)' => array( + 'expected' => 'AM', + 'format' => 'A', + ), + 'Ante meridiem and Post meridiem (uppercase) and escaped "A"' => array( + 'expected' => 'A AM', + 'format' => '\\A A', + ), + 'Ante meridiem and Post meridiem (lowercase)' => array( + 'expected' => 'am', + 'format' => 'a', + ), + 'Month' => array( + 'expected' => 'October', + 'format' => 'F', + ), + 'Month (abbreviated' => array( + 'expected' => 'Oct', + 'format' => 'M', + ), + 'Weekday' => array( + 'expected' => 'Thursday', + 'format' => 'l', + ), + 'Weekday (abbreviated)' => array( + 'expected' => 'Thu', + 'format' => 'D', + ), + ); } /** - * Test if format is set to A am/pm uppercase + * Tests that the date is formatted when + * `$wp_locale->month` and `$wp_locale->weekday` are empty. * * @ticket 53485 */ - public function test_format_upper_A() { - $utc = new DateTimeZone( 'UTC' ); - $datetime = new DateTimeImmutable( '2019-10-17', $utc ); - - $this->assertSame( 'AM', wp_date( 'A', $datetime->getTimestamp(), $utc ) ); - } + public function test_should_format_date_with_empty_wp_locale_month_and_weekday() { + global $wp_locale; - /** - * Test if format is set to A with an escaped A - * - * @ticket 53485 - */ - public function test_format_slash() { $utc = new DateTimeZone( 'UTC' ); $datetime = new DateTimeImmutable( '2019-10-17', $utc ); - $this->assertSame( 'A AM', wp_date( '\\A A', $datetime->getTimestamp(), $utc ) ); - } - + $original_wp_locale = $wp_locale; + $wp_locale->month = array(); + $wp_locale->weekday = array(); + $actual = wp_date( 'F', $datetime->getTimestamp(), $utc ); + $wp_locale = $original_wp_locale; - /** - * Test if format is set directly - * - * @ticket 53485 - */ - public function test_format() { - $utc = new DateTimeZone( 'UTC' ); - $datetime = new DateTimeImmutable( '2019-10-17', $utc ); - - $this->assertSame( '041', wp_date( 'B', $datetime->getTimestamp(), $utc ) ); + $this->assertSame( 'October', $actual ); } /** - * Test wp_date Filter runs + * Tests the wp_date filter. * * @ticket 53485 */ - public function test_wp_date_filter() { - add_filter( 'wp_date', array( $this, '_test_wp_date_filter' ), 99 ); + public function test_should_apply_filters_for_wp_date() { + add_filter( + 'wp_date', + static function() { + return 'filtered'; + } + ); $this->assertSame( 'filtered', wp_date( '' ) ); - - remove_filter( 'wp_date', array( $this, '_test_wp_date_filter' ), 99 ); - } - - public function _test_wp_date_filter() { - - return 'filtered'; } } From f19a71b3e144e48052eb7dd29869286f929ce052 Mon Sep 17 00:00:00 2001 From: costdev Date: Sat, 30 Apr 2022 06:19:25 +0100 Subject: [PATCH 5/7] Use MockAction as @peterwilsoncc suggested. --- tests/phpunit/tests/date/wpDate.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/tests/phpunit/tests/date/wpDate.php b/tests/phpunit/tests/date/wpDate.php index 2241285dc39de..e54767026fd40 100644 --- a/tests/phpunit/tests/date/wpDate.php +++ b/tests/phpunit/tests/date/wpDate.php @@ -169,13 +169,10 @@ public function test_should_format_date_with_empty_wp_locale_month_and_weekday() * @ticket 53485 */ public function test_should_apply_filters_for_wp_date() { - add_filter( - 'wp_date', - static function() { - return 'filtered'; - } - ); + $ma = new MockAction(); + add_filter( 'wp_date', array( &$ma, 'filter' ) ); + wp_date( '' ); - $this->assertSame( 'filtered', wp_date( '' ) ); + $this->assertSame( 1, $ma->get_call_count() ); } } From 533b7dbf888ec0463f46c2e788965e3024bac988 Mon Sep 17 00:00:00 2001 From: costdev Date: Sun, 31 Jul 2022 05:38:48 +0100 Subject: [PATCH 6/7] Remove unnecessary backup of global `$wp_locale`. --- tests/phpunit/tests/date/wpDate.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/phpunit/tests/date/wpDate.php b/tests/phpunit/tests/date/wpDate.php index e54767026fd40..a35caff8c93d3 100644 --- a/tests/phpunit/tests/date/wpDate.php +++ b/tests/phpunit/tests/date/wpDate.php @@ -154,11 +154,9 @@ public function test_should_format_date_with_empty_wp_locale_month_and_weekday() $utc = new DateTimeZone( 'UTC' ); $datetime = new DateTimeImmutable( '2019-10-17', $utc ); - $original_wp_locale = $wp_locale; $wp_locale->month = array(); $wp_locale->weekday = array(); $actual = wp_date( 'F', $datetime->getTimestamp(), $utc ); - $wp_locale = $original_wp_locale; $this->assertSame( 'October', $actual ); } From f0631cf0e50a0165fd43c1f229790771567d9092 Mon Sep 17 00:00:00 2001 From: costdev Date: Sun, 31 Jul 2022 05:39:30 +0100 Subject: [PATCH 7/7] Swap annotation order to meet new standard. --- tests/phpunit/tests/date/wpDate.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/date/wpDate.php b/tests/phpunit/tests/date/wpDate.php index a35caff8c93d3..341e590a74684 100644 --- a/tests/phpunit/tests/date/wpDate.php +++ b/tests/phpunit/tests/date/wpDate.php @@ -86,10 +86,10 @@ public function test_should_format_date_with_no_timezone() { /** * Tests that the format is set correctly. * - * @dataProvider data_should_format_date - * * @ticket 53485 * + * @dataProvider data_should_format_date + * * @param string $expected The expected result. * @param string $format The date format. */