View source
<?php
namespace Drupal\Tests\system\Functional\System;
use Drupal\Core\Datetime\Entity\DateFormat;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
class DateTimeTest extends BrowserTestBase {
protected static $modules = [
'block',
'node',
'language',
'field',
'field_ui',
'datetime',
'options',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$this
->drupalLogin($this
->drupalCreateUser([
'administer site configuration',
'administer content types',
'administer nodes',
'administer node fields',
'administer node form display',
'administer node display',
]));
$this
->drupalPlaceBlock('local_actions_block');
}
public function testTimeZoneHandling() {
$config = $this
->config('system.date')
->set('timezone.default', 'Pacific/Honolulu')
->set('timezone.user.configurable', 0)
->save();
DateFormat::load('medium')
->setPattern('Y-m-d H:i:s O')
->save();
$date1 = '2007-01-31 21:00:00 -1000';
$date2 = '2007-07-31 21:00:00 -1000';
$this
->drupalCreateContentType([
'type' => 'article',
]);
$node1 = $this
->drupalCreateNode([
'created' => strtotime($date1),
'type' => 'article',
]);
$node2 = $this
->drupalCreateNode([
'created' => strtotime($date2),
'type' => 'article',
]);
$this
->drupalGet('node/' . $node1
->id());
$this
->assertSession()
->pageTextContains('2007-01-31 21:00:00 -1000');
$this
->drupalGet('node/' . $node2
->id());
$this
->assertSession()
->pageTextContains('2007-07-31 21:00:00 -1000');
$config
->set('timezone.default', 'America/Los_Angeles')
->save();
\Drupal::entityTypeManager()
->getViewBuilder('node')
->resetCache([
$node1,
$node2,
]);
$this
->drupalGet('node/' . $node1
->id());
$this
->assertSession()
->pageTextContains('2007-01-31 23:00:00 -0800');
$this
->drupalGet('node/' . $node2
->id());
$this
->assertSession()
->pageTextContains('2007-08-01 00:00:00 -0700');
}
public function testDateFormatConfiguration() {
$this
->drupalGet('admin/config/regional/date-time');
$this
->clickLink('Add format');
$date_format_id = strtolower($this
->randomMachineName(8));
$name = ucwords($date_format_id);
$date_format = 'd.m.Y - H:i';
$edit = [
'id' => $date_format_id,
'label' => $name,
'date_format_pattern' => $date_format,
];
$this
->drupalGet('admin/config/regional/date-time/formats/add');
$this
->submitForm($edit, 'Add format');
$this
->assertSession()
->addressEquals(Url::fromRoute('entity.date_format.collection'));
$this
->assertSession()
->pageTextContains('Custom date format added.');
$this
->assertSession()
->pageTextContains($name);
$this
->assertSession()
->pageTextContains('Delete');
$this
->drupalGet('admin/config/regional/date-time');
$this
->clickLink('Edit');
$this
->submitForm([], 'Save format');
$this
->assertSession()
->addressEquals(Url::fromRoute('entity.date_format.collection'));
$this
->assertSession()
->pageTextContains('Custom date format updated.');
$this
->drupalGet('admin/config/regional/date-time');
$this
->clickLink('Edit');
$edit = [
'date_format_pattern' => 'Y m',
];
$this
->drupalGet($this
->getUrl());
$this
->submitForm($edit, 'Save format');
$this
->assertSession()
->addressEquals(Url::fromRoute('entity.date_format.collection'));
$this
->assertSession()
->pageTextContains('Custom date format updated.');
$this
->clickLink('Delete');
$this
->drupalGet('admin/config/regional/date-time/formats/manage/' . $date_format_id . '/delete');
$this
->submitForm([], 'Delete');
$this
->assertSession()
->addressEquals(Url::fromRoute('entity.date_format.collection'));
$this
->assertSession()
->pageTextContains("The date format {$name} has been deleted.");
$date_format = DateFormat::load($date_format_id);
$this
->assertNull($date_format);
$date_format_id = strtolower($this
->randomMachineName(8));
$name = ucwords($date_format_id);
$date_format = 'Y';
$edit = [
'id' => $date_format_id,
'label' => $name,
'date_format_pattern' => $date_format,
];
$this
->drupalGet('admin/config/regional/date-time/formats/add');
$this
->submitForm($edit, 'Add format');
$this
->assertSession()
->addressEquals(Url::fromRoute('entity.date_format.collection'));
$this
->assertSession()
->pageTextContains('Custom date format added.');
$this
->assertSession()
->pageTextContains($name);
$this
->assertSession()
->pageTextContains('Delete');
$date_format = DateFormat::create([
'id' => 'xss_short',
'label' => 'XSS format',
'pattern' => '\\<\\s\\c\\r\\i\\p\\t\\>\\a\\l\\e\\r\\t\\(\'\\X\\S\\S\'\\)\\;\\<\\/\\s\\c\\r\\i\\p\\t\\>',
]);
$date_format
->save();
$this
->drupalGet(Url::fromRoute('entity.date_format.collection'));
$this
->assertSession()
->assertEscaped("<script>alert('XSS');</script>");
$date_format_id = strtolower($this
->randomMachineName(8));
$name = ucwords($date_format_id);
$date_format = '& \\<\\e\\m\\>Y\\<\\/\\e\\m\\>';
$edit = [
'id' => $date_format_id,
'label' => $name,
'date_format_pattern' => $date_format,
];
$this
->drupalGet('admin/config/regional/date-time/formats/add');
$this
->submitForm($edit, 'Add format');
$this
->assertSession()
->addressEquals(Url::fromRoute('entity.date_format.collection'));
$this
->assertSession()
->pageTextContains('Custom date format added.');
$this
->assertSession()
->pageTextContains($name);
$this
->assertSession()
->assertEscaped('<em>' . date("Y") . '</em>');
}
public function testEnteringDateTimeViaSelectors() {
$this
->drupalCreateContentType([
'type' => 'page_with_date',
'name' => 'Page with date',
]);
$this
->drupalGet('admin/structure/types/manage/page_with_date');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet('admin/structure/types/manage/page_with_date/fields/add-field');
$edit = [
'new_storage_type' => 'datetime',
'label' => 'dt',
'field_name' => 'dt',
];
$this
->drupalGet('admin/structure/types/manage/page_with_date/fields/add-field');
$this
->submitForm($edit, 'Save and continue');
$this
->assertSession()
->pageTextContains('These settings apply to the');
$this
->drupalGet('admin/structure/types/manage/page_with_date/fields/node.page_with_date.field_dt/storage');
$edit = [
'settings[datetime_type]' => 'datetime',
'cardinality' => 'number',
'cardinality_number' => '1',
];
$this
->drupalGet('admin/structure/types/manage/page_with_date/fields/node.page_with_date.field_dt/storage');
$this
->submitForm($edit, 'Save field settings');
$this
->drupalGet('admin/structure/types/manage/page_with_date/fields');
$this
->assertSession()
->pageTextContains('field_dt');
$this
->drupalGet('admin/structure/types/manage/page_with_date/form-display');
$edit = [
'fields[field_dt][type]' => 'datetime_datelist',
'fields[field_dt][region]' => 'content',
];
$this
->drupalGet('admin/structure/types/manage/page_with_date/form-display');
$this
->submitForm($edit, 'Save');
$this
->drupalLogout();
$this
->drupalLogin($this
->drupalCreateUser([
'create page_with_date content',
]));
$this
->drupalGet('node/add/page_with_date');
$edit = [
'title[0][value]' => 'sample doc',
'field_dt[0][value][year]' => '2016',
'field_dt[0][value][month]' => '2',
'field_dt[0][value][day]' => '31',
'field_dt[0][value][hour]' => '1',
'field_dt[0][value][minute]' => '30',
];
$this
->drupalGet('node/add/page_with_date');
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('Selected combination of day and month is not valid.');
$edit['field_dt[0][value][day]'] = '29';
$this
->drupalGet('node/add/page_with_date');
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextNotContains('Selected combination of day and month is not valid.');
$this
->drupalGet('node/1');
$this
->assertSession()
->pageTextContains('Mon, 02/29/2016 - 01:30');
}
}