Chris@14: config('system.date') Chris@14: ->set('timezone.user.configurable', 1) Chris@14: ->set('timezone.default', 'America/Los_Angeles') Chris@14: ->save(); Chris@14: Chris@14: // Load the 'medium' date format, which is the default for node creation Chris@14: // time, and override it. Since we are testing time zones with Daylight Chris@14: // Saving Time, and need to future proof against changes to the zoneinfo Chris@14: // database, we choose the 'I' format placeholder instead of a Chris@14: // human-readable zone name. With 'I', a 1 means the date is in DST, and 0 Chris@14: // if not. Chris@14: DateFormat::load('medium') Chris@14: ->setPattern('Y-m-d H:i I') Chris@14: ->save(); Chris@14: Chris@14: // Create a user account and login. Chris@14: $web_user = $this->drupalCreateUser(); Chris@14: $this->drupalLogin($web_user); Chris@14: Chris@14: // Create some nodes with different authored-on dates. Chris@14: // Two dates in PST (winter time): Chris@14: $date1 = '2007-03-09 21:00:00 -0800'; Chris@14: $date2 = '2007-03-11 01:00:00 -0800'; Chris@14: // One date in PDT (summer time): Chris@14: $date3 = '2007-03-20 21:00:00 -0700'; Chris@14: $this->drupalCreateContentType(['type' => 'article']); Chris@14: $node1 = $this->drupalCreateNode(['created' => strtotime($date1), 'type' => 'article']); Chris@14: $node2 = $this->drupalCreateNode(['created' => strtotime($date2), 'type' => 'article']); Chris@14: $node3 = $this->drupalCreateNode(['created' => strtotime($date3), 'type' => 'article']); Chris@14: Chris@14: // Confirm date format and time zone. Chris@14: $this->drupalGet('node/' . $node1->id()); Chris@14: $this->assertText('2007-03-09 21:00 0', 'Date should be PST.'); Chris@14: $this->drupalGet('node/' . $node2->id()); Chris@14: $this->assertText('2007-03-11 01:00 0', 'Date should be PST.'); Chris@14: $this->drupalGet('node/' . $node3->id()); Chris@14: $this->assertText('2007-03-20 21:00 1', 'Date should be PDT.'); Chris@14: Chris@14: // Change user time zone to Santiago time. Chris@14: $edit = []; Chris@14: $edit['mail'] = $web_user->getEmail(); Chris@14: $edit['timezone'] = 'America/Santiago'; Chris@14: $this->drupalPostForm("user/" . $web_user->id() . "/edit", $edit, t('Save')); Chris@14: $this->assertText(t('The changes have been saved.'), 'Time zone changed to Santiago time.'); Chris@14: Chris@14: // Confirm date format and time zone. Chris@14: $this->drupalGet('node/' . $node1->id()); Chris@14: $this->assertText('2007-03-10 02:00 1', 'Date should be Chile summer time; five hours ahead of PST.'); Chris@14: $this->drupalGet('node/' . $node2->id()); Chris@14: $this->assertText('2007-03-11 05:00 0', 'Date should be Chile time; four hours ahead of PST'); Chris@14: $this->drupalGet('node/' . $node3->id()); Chris@14: $this->assertText('2007-03-21 00:00 0', 'Date should be Chile time; three hours ahead of PDT.'); Chris@14: Chris@14: // Ensure that anonymous users also use the default timezone. Chris@14: $this->drupalLogout(); Chris@14: $this->drupalGet('node/' . $node1->id()); Chris@14: $this->assertText('2007-03-09 21:00 0', 'Date should be PST.'); Chris@14: $this->drupalGet('node/' . $node2->id()); Chris@14: $this->assertText('2007-03-11 01:00 0', 'Date should be PST.'); Chris@14: $this->drupalGet('node/' . $node3->id()); Chris@14: $this->assertText('2007-03-20 21:00 1', 'Date should be PDT.'); Chris@14: Chris@14: // Format a date without accessing the current user at all and Chris@14: // ensure that it uses the default timezone. Chris@14: $this->drupalGet('/system-test/date'); Chris@14: $this->assertText('2016-01-13 08:29 0', 'Date should be PST.'); Chris@14: } Chris@14: Chris@14: }