| 1 | <?php |
| 2 | /** |
| 3 | * @group functions.php |
| 4 | * @group current_time |
| 5 | */ |
| 6 | |
| 7 | /** |
| 8 | * Retrieve the current time based on specified type. |
| 9 | * |
| 10 | * The 'mysql' type will return the time in the format for MySQL DATETIME field. |
| 11 | * The 'timestamp' type will return the current timestamp. |
| 12 | * Other strings will be interpreted as PHP date formats (e.g. 'Y-m-d'). |
| 13 | * |
| 14 | * If $gmt is set to either '1' or 'true', then both types will use GMT time. |
| 15 | * if $gmt is false, the output is adjusted with the GMT offset in the WordPress option. |
| 16 | * |
| 17 | * @since 1.0.0 |
| 18 | * |
| 19 | * @param string $type Type of time to retrieve. Accepts 'mysql', 'timestamp', or PHP date |
| 20 | * format string (e.g. 'Y-m-d'). |
| 21 | * @param int|bool $gmt Optional. Whether to use GMT timezone. Default false. |
| 22 | * |
| 23 | * @return int|string Integer if $type is 'timestamp', string otherwise. |
| 24 | */ |
| 25 | class Tests_Functions_current_time extends WP_UnitTestCase { |
| 26 | |
| 27 | function test_current_time_null_parms() { |
| 28 | $this->assertEquals( '', current_time( null ), 'Null passed' ); |
| 29 | } |
| 30 | |
| 31 | function test_current_time_pass_random_String() { |
| 32 | $this->assertEquals( date( 'Random_String_passed' ), current_time( 'Random_String_passed' ), 'Random_String passed' ); |
| 33 | } |
| 34 | |
| 35 | function test_current_time_pass_year() { |
| 36 | $this->assertEquals( date( 'Y-m-d' ), current_time( 'Y-m-d' ), 'Y-m-d passed' ); |
| 37 | } |
| 38 | |
| 39 | function test_current_time_pass_full_format() { |
| 40 | $this->assertEquals( date( 'F j, Y, g:i a' ), current_time( 'F j, Y, g:i a' ), 'F j, Y, g:i a passed' ); |
| 41 | } |
| 42 | |
| 43 | function test_current_time_pass_year_gmt() { |
| 44 | $this->assertEquals( date( 'Y-m-d' ), current_time( 'Y-m-d', true ), 'Y-m-d, true passed' ); |
| 45 | } |
| 46 | |
| 47 | function test_current_time_pass_year_1() { |
| 48 | $this->assertEquals( date( 'Y-m-d' ), current_time( 'Y-m-d', 1 ), 'Y-m-d, true passed' ); |
| 49 | } |
| 50 | |
| 51 | function test_current_time_pass_year_not_gmt() { |
| 52 | $this->assertEquals( date( 'Y-m-d' ), current_time( 'Y-m-d', false ), 'Y-m-d, false passed' ); |
| 53 | } |
| 54 | |
| 55 | function test_current_time_pass_year_not_gmt_offset() { |
| 56 | update_option( 'gmt_offset', 6 ); |
| 57 | $this->assertEquals( date( 'F j, Y, g:i a', time() + ( 6 * HOUR_IN_SECONDS ) ), current_time( 'F j, Y, g:i a', false ), 'Y-m-d, false passed' ); |
| 58 | } |
| 59 | |
| 60 | function test_current_time_pass_timestamp_not_gmt_offset() { |
| 61 | update_option( 'gmt_offset', 6 ); |
| 62 | $this->assertEquals( time() + ( 6 * HOUR_IN_SECONDS ), current_time( 'timestamp', false ), 'timestamp, false passed' ); |
| 63 | } |
| 64 | |
| 65 | function test_current_time_pass_timestamp_gmt_offset() { |
| 66 | update_option( 'gmt_offset', 6 ); |
| 67 | $this->assertEquals( time(), current_time( 'timestamp', true ), 'timestamp, true passed' ); |
| 68 | } |
| 69 | |
| 70 | function test_current_time_pass_timestamp_not_gmt() { |
| 71 | update_option( 'gmt_offset', 6 ); |
| 72 | $this->assertEquals( time() + ( 6 * HOUR_IN_SECONDS ), current_time( 'timestamp', false ), 'timestamp, false passed' ); |
| 73 | } |
| 74 | |
| 75 | function test_current_time_pass_timestamp_gmt() { |
| 76 | update_option( 'gmt_offset', 6 ); |
| 77 | $this->assertEquals( time(), current_time( 'timestamp', true ), 'timestamp, true passed' ); |
| 78 | } |
| 79 | |
| 80 | function test_current_time_pass_mysql_not_gmt_offset() { |
| 81 | update_option( 'gmt_offset', 6 ); |
| 82 | $this->assertEquals( gmdate( 'Y-m-d H:i:s', ( time() + ( 6 * HOUR_IN_SECONDS ) ) ), current_time( 'mysql', false ), 'mysql, false passed' ); |
| 83 | } |
| 84 | |
| 85 | function test_current_time_pass_mysql_gmt_offset() { |
| 86 | update_option( 'gmt_offset', 6 ); |
| 87 | $this->assertEquals( gmdate( 'Y-m-d H:i:s', ( time() ) ), current_time( 'mysql', true ), 'mysql, true passed' ); |
| 88 | } |
| 89 | |
| 90 | function test_current_time_pass_mysql_not_gmt() { |
| 91 | update_option( 'gmt_offset', 6 ); |
| 92 | $this->assertEquals( gmdate( 'Y-m-d H:i:s', ( time() + ( 6 * HOUR_IN_SECONDS ) ) ), current_time( 'mysql', false ), 'mysql, false passed' ); |
| 93 | } |
| 94 | |
| 95 | function test_current_time_pass_mysql_gmt() { |
| 96 | update_option( 'gmt_offset', 6 ); |
| 97 | $this->assertEquals( gmdate( 'Y-m-d H:i:s' ), current_time( 'mysql', true ), 'mysql, true passed' ); |
| 98 | } |
| 99 | } |