Make WordPress Core

Changeset 53810


Ignore:
Timestamp:
08/01/2022 10:48:42 PM (4 years ago)
Author:
peterwilsoncc
Message:

Date/Time: Increase test coverage of wp_date().

Props costdev, pbearne, rarst.
Fixes #53485.
See #55652.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/date/wpDate.php

    r51568 r53810  
    6262                $this->assertSame( $string, wp_date( 'F', $datetime->getTimestamp(), $utc ) );
    6363        }
     64
     65        /**
     66         * Tests that the date is formatted with no timestamp provided.
     67         *
     68         * @ticket 53485
     69         */
     70        public function test_should_format_date_with_no_timestamp() {
     71                $utc = new DateTimeZone( 'UTC' );
     72                $this->assertSame( (string) time(), wp_date( 'U', null, $utc ) );
     73        }
     74
     75        /**
     76         * Tests that the date is formatted with no timezone provided.
     77         *
     78         * @ticket 53485
     79         */
     80        public function test_should_format_date_with_no_timezone() {
     81                $utc      = new DateTimeZone( 'UTC' );
     82                $datetime = new DateTimeImmutable( '2019-10-17', $utc );
     83                $this->assertSame( 'October', wp_date( 'F', $datetime->getTimestamp() ) );
     84        }
     85
     86        /**
     87         * Tests that the format is set correctly.
     88         *
     89         * @ticket 53485
     90         *
     91         * @dataProvider data_should_format_date
     92         *
     93         * @param string $expected The expected result.
     94         * @param string $format   The date format.
     95         */
     96        public function test_should_format_date( $expected, $format ) {
     97                $utc      = new DateTimeZone( 'UTC' );
     98                $datetime = new DateTimeImmutable( '2019-10-17', $utc );
     99
     100                $this->assertSame( $expected, wp_date( $format, $datetime->getTimestamp(), $utc ) );
     101        }
     102
     103        /**
     104         * Data provider.
     105         *
     106         * @return array
     107         */
     108        public function data_should_format_date() {
     109                return array(
     110                        'Swatch Internet Time'                        => array(
     111                                'expected' => '041',
     112                                'format'   => 'B',
     113                        ),
     114                        'Ante meridiem and Post meridiem (uppercase)' => array(
     115                                'expected' => 'AM',
     116                                'format'   => 'A',
     117                        ),
     118                        'Ante meridiem and Post meridiem (uppercase) and escaped "A"' => array(
     119                                'expected' => 'A AM',
     120                                'format'   => '\\A A',
     121                        ),
     122                        'Ante meridiem and Post meridiem (lowercase)' => array(
     123                                'expected' => 'am',
     124                                'format'   => 'a',
     125                        ),
     126                        'Month'                                       => array(
     127                                'expected' => 'October',
     128                                'format'   => 'F',
     129                        ),
     130                        'Month (abbreviated'                          => array(
     131                                'expected' => 'Oct',
     132                                'format'   => 'M',
     133                        ),
     134                        'Weekday'                                     => array(
     135                                'expected' => 'Thursday',
     136                                'format'   => 'l',
     137                        ),
     138                        'Weekday (abbreviated)'                       => array(
     139                                'expected' => 'Thu',
     140                                'format'   => 'D',
     141                        ),
     142                );
     143        }
     144
     145        /**
     146         * Tests that the date is formatted when
     147         * `$wp_locale->month` and `$wp_locale->weekday` are empty.
     148         *
     149         * @ticket 53485
     150         */
     151        public function test_should_format_date_with_empty_wp_locale_month_and_weekday() {
     152                global $wp_locale;
     153
     154                $utc      = new DateTimeZone( 'UTC' );
     155                $datetime = new DateTimeImmutable( '2019-10-17', $utc );
     156
     157                $wp_locale->month   = array();
     158                $wp_locale->weekday = array();
     159                $actual             = wp_date( 'F', $datetime->getTimestamp(), $utc );
     160
     161                $this->assertSame( 'October', $actual );
     162        }
     163
     164        /**
     165         * Tests the wp_date filter.
     166         *
     167         * @ticket 53485
     168         */
     169        public function test_should_apply_filters_for_wp_date() {
     170                $ma = new MockAction();
     171                add_filter( 'wp_date', array( &$ma, 'filter' ) );
     172                wp_date( '' );
     173
     174                $this->assertSame( 1, $ma->get_call_count() );
     175        }
    64176}
Note: See TracChangeset for help on using the changeset viewer.