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