diff --git a/lib/Config/Menu.php b/lib/Config/Menu.php index c6507ab5d9..f3676c52a7 100644 --- a/lib/Config/Menu.php +++ b/lib/Config/Menu.php @@ -404,7 +404,7 @@ class Menu { $data['roles']['mailpoet_all'] = __('In any WordPress role'); $date_time = new DateTime(); - $data['current_date'] = $date_time->getCurrentDate(DateTime::INTERNAL_DATE_FORMAT); + $data['current_date'] = $date_time->getCurrentDate(DateTime::DEFAULT_DATE_FORMAT); $data['current_time'] = $date_time->getCurrentTime(); $data['schedule_time_of_day'] = $date_time->getTimeInterval( '00:00:00', @@ -496,4 +496,4 @@ class Menu { ? (int)$listing_per_page : Listing\Handler::DEFAULT_LIMIT_PER_PAGE; } -} \ No newline at end of file +} diff --git a/lib/WP/DateTime.php b/lib/WP/DateTime.php index b42bfbd6ec..2b0359f094 100644 --- a/lib/WP/DateTime.php +++ b/lib/WP/DateTime.php @@ -3,22 +3,22 @@ namespace MailPoet\WP; class DateTime { - const INTERNAL_DATE_FORMAT = 'Y-m-d'; - const INTERNAL_TIME_FORMAT = 'H:i:s'; - const INTERNAL_DATE_TIME_FORMAT = 'Y-m-d H:i:s'; + const DEFAULT_DATE_FORMAT = 'Y-m-d'; + const DEFAULT_TIME_FORMAT = 'H:i:s'; + const DEFAULT_DATE_TIME_FORMAT = 'Y-m-d H:i:s'; function __construct() { } function getTimeFormat() { $time_format = get_option('time_format'); - if (empty($time_format)) $time_format = self::INTERNAL_TIME_FORMAT; + if (empty($time_format)) $time_format = self::DEFAULT_TIME_FORMAT; return $time_format; } function getDateFormat() { $date_format = get_option('date_format'); - if (empty($date_format)) $date_format = self::INTERNAL_DATE_FORMAT; + if (empty($date_format)) $date_format = self::DEFAULT_DATE_FORMAT; return $date_format; } @@ -32,15 +32,15 @@ class DateTime { return $this->getCurrentTime($format); } - function getTime($time, $format=false) { + function formatTime($timestamp, $format=false) { if (empty($format)) $format = $this->getTimeFormat(); - return date($format, $time); + return date($format, $timestamp); } /** * Generates a list of time strings within an interval, - * formatted and mapped from INTERNAL_TIME_FORMAT to Wordpress time strings. + * formatted and mapped from DEFAULT_TIME_FORMAT to Wordpress time strings. */ function getTimeInterval( $start_time='00:00:00', @@ -49,15 +49,15 @@ class DateTime { ) { $steps = array(); - $internal_time = $start_time; - $timestamp = strtotime($internal_time); + $formatted_time = $start_time; + $timestamp = strtotime($formatted_time); for ($step = 0; $step < $total_steps; $step += 1) { - $wordpress_time = $this->getTime($timestamp); - $steps[$internal_time] = $wordpress_time; + $formatted_time = $this->formatTime($timestamp, self::DEFAULT_TIME_FORMAT); + $label_time = $this->formatTime($timestamp); + $steps[$formatted_time] = $label_time; $timestamp = strtotime($time_step, $timestamp); - $internal_time = $this->getTime($timestamp, self::INTERNAL_TIME_FORMAT); } return $steps; diff --git a/tests/_support/Helper/WordPress.php b/tests/_support/Helper/WordPress.php new file mode 100644 index 0000000000..326dddd3fe --- /dev/null +++ b/tests/_support/Helper/WordPress.php @@ -0,0 +1,35 @@ +date_time = new DateTime(); + } + + function testGetTimeFormat() { + WordPressHelper::interceptFunction('get_option', function($key) { + return 'H:i'; + }); + expect($this->date_time->getTimeFormat())->equals('H:i'); + + WordPressHelper::interceptFunction('get_option', function($key) { + return ''; + }); + expect($this->date_time->getTimeFormat())->equals('H:i:s'); + } + + function testGetDateFormat() { + WordPressHelper::interceptFunction('get_option', function($key) { + return 'm-d'; + }); + expect($this->date_time->getDateFormat())->equals('m-d'); + + WordPressHelper::interceptFunction('get_option', function($key) { + return ''; + }); + expect($this->date_time->getDateFormat())->equals('Y-m-d'); + } + + function testGetCurrentDate() { + expect($this->date_time->getCurrentDate("Y-m"))->equals(date("Y-m")); + } + + function testGetCurrentTime() { + expect($this->date_time->getCurrentTime("i:s"))->regExp('/\d\d:\d\d/'); + } + + function testFormatTime() { + $timestamp = 1234567; + $format = "H:i:s"; + expect($this->date_time->formatTime($timestamp, $format))->equals(date($format, $timestamp)); + } + + function testTimeInterval() { + $one_hour_interval = array_keys($this->date_time->getTimeInterval( + '00:00:00', + '+1 hour', + $total_steps=5 + )); + $one_hour_expected = array( + '00:00:00', '01:00:00', '02:00:00', '03:00:00', '04:00:00'); + expect($one_hour_interval)->equals($one_hour_expected); + + $quarter_hour_interval = array_keys($this->date_time->getTimeInterval( + '00:00:00', + '+15 minute', + $total_steps=5 + )); + $quarter_hour_expected = array( + '00:00:00', '00:15:00', '00:30:00', '00:45:00', '01:00:00', + ); + expect($quarter_hour_interval)->equals($quarter_hour_expected); + + $offset_start_time_interval = array_keys($this->date_time->getTimeInterval( + '03:00:00', + '+1 hour', + $total_steps=5 + )); + $offset_start_time_expected = array( + '03:00:00', '04:00:00', '05:00:00', '06:00:00', '07:00:00', + ); + expect($offset_start_time_interval)->equals($offset_start_time_expected); + } + + function _afterStep() { + Wordpress::releaseAllFunctions(); + } +}