3 namespace Drupal\FunctionalTests\Datetime;
5 use Drupal\Core\Datetime\DrupalDateTime;
6 use Drupal\Core\Datetime\Entity\DateFormat;
7 use Drupal\Core\Entity\Entity\EntityFormDisplay;
8 use Drupal\Core\Entity\Entity\EntityViewDisplay;
9 use Drupal\field\Entity\FieldConfig;
10 use Drupal\field\Entity\FieldStorageConfig;
11 use Drupal\Tests\BrowserTestBase;
14 * Tests the functionality of Timestamp core field UI.
18 class TimestampTest extends BrowserTestBase {
21 * An array of display options to pass to entity_get_display().
25 protected $displayOptions;
28 * A field storage to use in this test class.
30 * @var \Drupal\field\Entity\FieldStorageConfig
32 protected $fieldStorage;
35 * The field used in this test class.
37 * @var \Drupal\field\Entity\FieldConfig
44 public static $modules = ['node', 'entity_test', 'field_ui'];
49 protected function setUp() {
52 $web_user = $this->drupalCreateUser([
55 'administer entity_test content',
56 'administer entity_test form display',
57 'administer content types',
58 'administer node fields',
61 $this->drupalLogin($web_user);
62 $field_name = 'field_timestamp';
64 $widget_type = 'datetime_timestamp';
65 $formatter_type = 'timestamp';
67 $this->fieldStorage = FieldStorageConfig::create([
68 'field_name' => $field_name,
69 'entity_type' => 'entity_test',
72 $this->fieldStorage->save();
73 $this->field = FieldConfig::create([
74 'field_storage' => $this->fieldStorage,
75 'bundle' => 'entity_test',
80 EntityFormDisplay::load('entity_test.entity_test.default')
81 ->setComponent($field_name, ['type' => $widget_type])
84 $this->displayOptions = [
85 'type' => $formatter_type,
89 EntityViewDisplay::create([
90 'targetEntityType' => $this->field->getTargetEntityTypeId(),
91 'bundle' => $this->field->getTargetBundle(),
94 ])->setComponent($field_name, $this->displayOptions)
99 * Tests the "datetime_timestamp" widget.
101 public function testWidget() {
102 // Build up a date in the UTC timezone.
103 $value = '2012-12-31 00:00:00';
104 $date = new DrupalDateTime($value, 'UTC');
106 // Update the timezone to the system default.
107 $date->setTimezone(timezone_open(drupal_get_user_timezone()));
109 // Display creation form.
110 $this->drupalGet('entity_test/add');
112 // Make sure the "datetime_timestamp" widget is on the page.
113 $fields = $this->xpath('//div[contains(@class, "field--widget-datetime-timestamp") and @id="edit-field-timestamp-wrapper"]');
114 $this->assertEquals(1, count($fields));
116 // Look for the widget elements and make sure they are empty.
117 $this->assertSession()->fieldExists('field_timestamp[0][value][date]');
118 $this->assertSession()->fieldValueEquals('field_timestamp[0][value][date]', '');
119 $this->assertSession()->fieldExists('field_timestamp[0][value][time]');
120 $this->assertSession()->fieldValueEquals('field_timestamp[0][value][time]', '');
123 $date_format = DateFormat::load('html_date')->getPattern();
124 $time_format = DateFormat::load('html_time')->getPattern();
127 'field_timestamp[0][value][date]' => $date->format($date_format),
128 'field_timestamp[0][value][time]' => $date->format($time_format),
130 $this->drupalPostForm(NULL, $edit, 'Save');
132 // Make sure the submitted date is set as the default in the widget.
133 $this->assertSession()->fieldExists('field_timestamp[0][value][date]');
134 $this->assertSession()->fieldValueEquals('field_timestamp[0][value][date]', $date->format($date_format));
135 $this->assertSession()->fieldExists('field_timestamp[0][value][time]');
136 $this->assertSession()->fieldValueEquals('field_timestamp[0][value][time]', $date->format($time_format));
138 // Make sure the entity was saved.
139 preg_match('|entity_test/manage/(\d+)|', $this->getSession()->getCurrentUrl(), $match);
141 $this->assertSession()->pageTextContains(sprintf('entity_test %s has been created.', $id));
143 // Make sure the timestamp is output properly with the default formatter.
144 $medium = DateFormat::load('medium')->getPattern();
145 $this->drupalGet('entity_test/' . $id);
146 $this->assertSession()->pageTextContains($date->format($medium));