View source
<?php
namespace Drupal\system\Tests\Datetime;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\simpletest\WebTestBase;
use Drupal\user\Entity\User;
class DrupalDateTimeTest extends WebTestBase {
public static $modules = array();
protected function setUp() {
parent::setUp();
}
public function testSystemTimezone() {
$options = array(
'query' => array(
'date' => 'Tue+Sep+17+2013+21%3A35%3A31+GMT%2B0100+(BST)#',
),
);
$response = $this
->drupalGet('system/timezone/BST/3600/1', $options);
$this
->assertEqual($response, '"Europe\\/London"', 'Timezone AJAX callback successfully identifies and responds to a long-format date.');
}
public function testDateTimezone() {
$date_string = '2007-01-31 21:00:00';
$this
->config('system.date')
->set('timezone.user.configurable', 0)
->set('timezone.default', NULL)
->save();
$system_timezone = date_default_timezone_get();
$date = new DrupalDateTime($date_string);
$timezone = $date
->getTimezone()
->getName();
$this
->assertTrue($timezone == $system_timezone, 'DrupalDateTime uses the system timezone when there is no site timezone.');
$date = new DrupalDateTime($date_string, 'America/Yellowknife');
$timezone = $date
->getTimezone()
->getName();
$this
->assertTrue($timezone == 'America/Yellowknife', 'DrupalDateTime uses the specified timezone if provided.');
$this
->config('system.date')
->set('timezone.default', 'Europe/Warsaw')
->save();
$date = new DrupalDateTime($date_string);
$timezone = $date
->getTimezone()
->getName();
$this
->assertTrue($timezone == 'Europe/Warsaw', 'DrupalDateTime uses the site timezone if provided.');
$this
->config('system.date')
->set('timezone.user.configurable', 1)
->save();
$test_user = $this
->drupalCreateUser(array());
$this
->drupalLogin($test_user);
$edit = array(
'mail' => $test_user
->getEmail(),
'timezone' => 'Asia/Manila',
);
$this
->drupalPostForm('user/' . $test_user
->id() . '/edit', $edit, t('Save'));
\Drupal::entityManager()
->getStorage('user')
->resetCache();
$this->container
->get('current_user')
->setAccount(User::load($test_user
->id()));
$date = new DrupalDateTime($date_string);
$timezone = $date
->getTimezone()
->getName();
$this
->assertTrue($timezone == 'Asia/Manila', 'DrupalDateTime uses the user timezone, if configurable timezones are used and it is set.');
}
function testTimezoneFormat() {
$date = DrupalDateTime::createFromTimestamp(87654321, 'UTC');
$this
->assertEqual($date
->format('Y/m/d H:i:s e'), '1972/10/11 12:25:21 UTC', 'Date has default UTC time zone and correct date/time.');
$this
->assertEqual($date
->format('Y/m/d H:i:s e', array(
'timezone' => 'America/New_York',
)), '1972/10/11 08:25:21 America/New_York', 'Date displayed overidden time zone and correct date/time');
$this
->assertEqual($date
->format('Y/m/d H:i:s e'), '1972/10/11 12:25:21 UTC', 'Date still has default UTC time zone and correct date/time');
}
}