/** * @since 4.6.x */ public function test_create_new_blank_datetime() { //if timezone is empty string then the setUp didn't work correctly. For the purpose of this test //we want a high positive timezone, so let's force that if necessary if (get_option('timezone_string') != 'Australia/Sydney') { update_option('timezone_string', 'Australia/Sydney'); EEM_Datetime::reset(); EEM_Datetime::instance(); } EE_Registry::instance()->load_helper('DTT_Helper'); //make sure now is in the timezone we want to test with. $now = new Datetime('@' . (time() + DAY_IN_SECONDS * 30)); $now->setTimeZone(new DateTimeZone(EEH_DTT_Helper::get_timezone())); $now->setTime('8', '0', '0'); $now->setTimeZone(new DateTimeZone('America/Toronto')); //get the default datetime $default_date = EEM_Datetime::instance()->create_new_blank_datetime(); $default_date = reset($default_date); //assert instance $this->assertInstanceOf('EE_Datetime', $default_date); //set its timezone to match our expected timezone $default_date->set_timezone('America/Toronto'); $actual = $default_date->get_DateTime_object('DTT_EVT_start'); $this->assertInstanceOf('DateTime', $actual); //assert timezones are the same $this->assertEquals($now->getTimezone(), $actual->getTimeZone()); //assert that we have the correct values on the date... we'll do each part separately to verify. $this->assertEquals($now->format('Y'), $actual->format('Y')); $this->assertEquals($now->format('m'), $actual->format('m')); $this->assertEquals($now->format('d'), $actual->format('d')); $this->assertEquals($now->format('H'), $actual->format('H')); $this->assertEquals($now->format('i'), $actual->format('i')); }
/** * @since 4.6.12+ */ public function test_get_i18n_datetime() { //setup a datetime object with some known values for testing with. $original_timezone = get_option('timezone_string'); update_option('timezone_string', 'America/Toronto'); $dateTimeZone = new DateTimeZone('America/Toronto'); $currentTime = new DateTime("now", $dateTimeZone); $futureTime = clone $currentTime; $futureTime->add(new DateInterval('P2D')); /** @type EE_Datetime $datetime */ $datetime = $this->factory->datetime->create(array('DTT_EVT_start' => $currentTime->format('Y-m-d H:i:s'), 'DTT_EVT_end' => $futureTime->format('Y-m-d H:i:s'), 'formats' => array('Y-m-d', 'H:i:s'))); $this->assertInstanceOf('EE_Datetime', $datetime); //test get_i18n_datetime $this->assertEquals($currentTime->format('Y-m-d H:i:s'), $datetime->get_i18n_datetime('DTT_EVT_start')); $this->assertEquals($futureTime->format('Y-m-d H:i:s'), $datetime->get_i18n_datetime('DTT_EVT_end')); $id = $datetime->ID(); //test when retrieved from the database. EEM_Datetime::reset(); $dbDatetime = EEM_Datetime::instance()->get_one_by_ID($id); //set formats to match expected $dbDatetime->set_date_format('Y-m-d'); $dbDatetime->set_time_format('H:i:s'); $this->assertEquals($currentTime->format('Y-m-d H:i:s'), $dbDatetime->get_i18n_datetime('DTT_EVT_start')); $this->assertEquals($futureTime->format('Y-m-d H:i:s'), $dbDatetime->get_i18n_datetime('DTT_EVT_end')); //restore timezone update_option('timezone_string', $original_timezone); }