8faf8c6c915e047554b1c12a145cb15a850dec9f
[yaffs-website] / Drupal / FunctionalTests / Datetime / TimestampTest.php
1 <?php
2
3 namespace Drupal\FunctionalTests\Datetime;
4
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;
12
13 /**
14  * Tests the functionality of Timestamp core field UI.
15  *
16  * @group field
17  */
18 class TimestampTest extends BrowserTestBase {
19
20   /**
21    * An array of display options to pass to entity_get_display().
22    *
23    * @var array
24    */
25   protected $displayOptions;
26
27   /**
28    * A field storage to use in this test class.
29    *
30    * @var \Drupal\field\Entity\FieldStorageConfig
31    */
32   protected $fieldStorage;
33
34   /**
35    * The field used in this test class.
36    *
37    * @var \Drupal\field\Entity\FieldConfig
38    */
39   protected $field;
40
41   /**
42    * {@inheritdoc}
43    */
44   public static $modules = ['node', 'entity_test', 'field_ui'];
45
46   /**
47    * {@inheritdoc}
48    */
49   protected function setUp() {
50     parent::setUp();
51
52     $web_user = $this->drupalCreateUser([
53       'access content',
54       'view test entity',
55       'administer entity_test content',
56       'administer entity_test form display',
57       'administer content types',
58       'administer node fields',
59     ]);
60
61     $this->drupalLogin($web_user);
62     $field_name = 'field_timestamp';
63     $type = 'timestamp';
64     $widget_type = 'datetime_timestamp';
65     $formatter_type = 'timestamp';
66
67     $this->fieldStorage = FieldStorageConfig::create([
68       'field_name' => $field_name,
69       'entity_type' => 'entity_test',
70       'type' => $type,
71     ]);
72     $this->fieldStorage->save();
73     $this->field = FieldConfig::create([
74       'field_storage' => $this->fieldStorage,
75       'bundle' => 'entity_test',
76       'required' => TRUE,
77     ]);
78     $this->field->save();
79
80     EntityFormDisplay::load('entity_test.entity_test.default')
81       ->setComponent($field_name, ['type' => $widget_type])
82       ->save();
83
84     $this->displayOptions = [
85       'type' => $formatter_type,
86       'label' => 'hidden',
87     ];
88
89     EntityViewDisplay::create([
90       'targetEntityType' => $this->field->getTargetEntityTypeId(),
91       'bundle' => $this->field->getTargetBundle(),
92       'mode' => 'full',
93       'status' => TRUE,
94     ])->setComponent($field_name, $this->displayOptions)
95       ->save();
96   }
97
98   /**
99    * Tests the "datetime_timestamp" widget.
100    */
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');
105
106     // Update the timezone to the system default.
107     $date->setTimezone(timezone_open(drupal_get_user_timezone()));
108
109     // Display creation form.
110     $this->drupalGet('entity_test/add');
111
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));
115
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]', '');
121
122     // Submit the date.
123     $date_format = DateFormat::load('html_date')->getPattern();
124     $time_format = DateFormat::load('html_time')->getPattern();
125
126     $edit = [
127       'field_timestamp[0][value][date]' => $date->format($date_format),
128       'field_timestamp[0][value][time]' => $date->format($time_format),
129     ];
130     $this->drupalPostForm(NULL, $edit, 'Save');
131
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));
137
138     // Make sure the entity was saved.
139     preg_match('|entity_test/manage/(\d+)|', $this->getSession()->getCurrentUrl(), $match);
140     $id = $match[1];
141     $this->assertSession()->pageTextContains(sprintf('entity_test %s has been created.', $id));
142
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));
147   }
148
149 }