1.20.x user.test UserTimeZoneFunctionalTest::testUserTimeZone()

Tests the display of dates and time when user-configurable time zones are set.

File

modules/user/tests/user.test, line 1511
Tests for user.module.

Class

UserTimeZoneFunctionalTest
Tests for user-configurable time zones.

Code

function testUserTimeZone() {
  // Setup date/time settings for Los Angeles time.
  config('system.date')
    ->set('user_configurable_timezones', 1)
    ->set('default_timezone', 'America/Los_Angeles')
    ->save();

  // Include the timezone in the medium date format.
  $format = system_date_format_load('medium');
  $format['pattern'] = 'Y-m-d H:i T';
  system_date_format_save($format);

  // Create a user account and login.
  $web_user = $this->backdropCreateUser();
  $this->backdropLogin($web_user);

  // Create some nodes with different authored-on dates.
  // One date in PST (winter time):
  $date1 = '2007-03-09 21:00:00 -0800';
  // Two dates in PDT (summer time):
  $date2 = '2007-03-12 01:00:00 -0700';
  $date3 = '2007-03-25 21:00:00 -0700';
  $node1 = $this->backdropCreateNode(array('created' => strtotime($date1), 'type' => 'post'));
  $node2 = $this->backdropCreateNode(array('created' => strtotime($date2), 'type' => 'post'));
  $node3 = $this->backdropCreateNode(array('created' => strtotime($date3), 'type' => 'post'));

  // Confirm date format and time zone.
  $this->backdropGet("node/$node1->nid");
  $this->assertText('2007-03-09 21:00 PST', 'Date should be PST.');
  $this->backdropGet("node/$node2->nid");
  $this->assertText('2007-03-12 01:00 PDT', 'Date should be PDT.');
  $this->backdropGet("node/$node3->nid");
  $this->assertText('2007-03-25 21:00 PDT', 'Date should be PDT.');

  // Change user time zone to Santiago time.
  $edit = array();
  $edit['mail'] = $web_user->mail;
  $edit['timezone'] = 'Europe/Paris';
  $this->backdropPost("user/$web_user->uid/edit", $edit, t('Save'));
  $this->assertText(t('The changes have been saved.'), 'Time zone changed to Central Europe time.');

  // Confirm date format and time zone.
  $this->backdropGet("node/$node1->nid");
  $this->assertText('2007-03-10 06:00 CET', 'Date should be Central European Time; nine hours ahead of PST.');
  $this->backdropGet("node/$node2->nid");
  $this->assertText('2007-03-12 09:00 CET', 'Date should be Central European Time; eight hours ahead of PDT');
  $this->backdropGet("node/$node3->nid");
  $this->assertText('2007-03-26 06:00 CEST', 'Date should be Central European Summer Time; nine hours ahead of PDT.');
}