Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\datetime\Tests;
|
Chris@0
|
4
|
Chris@0
|
5 @trigger_error('\Drupal\datetime\Tests\DateTestBase is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Use \Drupal\Tests\BrowserTestBase instead. See https://www.drupal.org/node/2780063.', E_USER_DEPRECATED);
|
Chris@0
|
6
|
Chris@0
|
7 use Drupal\Core\Entity\Entity\EntityFormDisplay;
|
Chris@0
|
8 use Drupal\Core\Entity\Entity\EntityViewDisplay;
|
Chris@0
|
9 use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
|
Chris@0
|
10 use Drupal\entity_test\Entity\EntityTest;
|
Chris@0
|
11 use Drupal\field\Entity\FieldConfig;
|
Chris@0
|
12 use Drupal\field\Entity\FieldStorageConfig;
|
Chris@0
|
13 use Drupal\simpletest\WebTestBase;
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * Provides a base class for testing Datetime field functionality.
|
Chris@0
|
17 *
|
Chris@0
|
18 * @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0.
|
Chris@0
|
19 * Use \Drupal\Tests\BrowserTestBase instead.
|
Chris@0
|
20 */
|
Chris@0
|
21 abstract class DateTestBase extends WebTestBase {
|
Chris@0
|
22
|
Chris@0
|
23 /**
|
Chris@0
|
24 * Modules to enable.
|
Chris@0
|
25 *
|
Chris@0
|
26 * @var array
|
Chris@0
|
27 */
|
Chris@0
|
28 public static $modules = ['node', 'entity_test', 'datetime', 'field_ui'];
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * An array of display options to pass to entity_get_display()
|
Chris@0
|
32 *
|
Chris@0
|
33 * @var array
|
Chris@0
|
34 */
|
Chris@0
|
35 protected $displayOptions;
|
Chris@0
|
36
|
Chris@0
|
37 /**
|
Chris@0
|
38 * A field storage to use in this test class.
|
Chris@0
|
39 *
|
Chris@0
|
40 * @var \Drupal\field\Entity\FieldStorageConfig
|
Chris@0
|
41 */
|
Chris@0
|
42 protected $fieldStorage;
|
Chris@0
|
43
|
Chris@0
|
44 /**
|
Chris@0
|
45 * The field used in this test class.
|
Chris@0
|
46 *
|
Chris@0
|
47 * @var \Drupal\field\Entity\FieldConfig
|
Chris@0
|
48 */
|
Chris@0
|
49 protected $field;
|
Chris@0
|
50
|
Chris@0
|
51 /**
|
Chris@0
|
52 * The date formatter service.
|
Chris@0
|
53 *
|
Chris@0
|
54 * @var \Drupal\Core\Datetime\DateFormatterInterface
|
Chris@0
|
55 */
|
Chris@0
|
56 protected $dateFormatter;
|
Chris@0
|
57
|
Chris@0
|
58 /**
|
Chris@0
|
59 * An array of timezone extremes to test.
|
Chris@0
|
60 *
|
Chris@0
|
61 * @var string[]
|
Chris@0
|
62 */
|
Chris@0
|
63 protected static $timezones = [
|
Chris@0
|
64 // UTC-12, no DST.
|
Chris@0
|
65 'Pacific/Kwajalein',
|
Chris@0
|
66 // UTC-11, no DST
|
Chris@0
|
67 'Pacific/Midway',
|
Chris@0
|
68 // UTC-7, no DST.
|
Chris@0
|
69 'America/Phoenix',
|
Chris@0
|
70 // UTC.
|
Chris@0
|
71 'UTC',
|
Chris@0
|
72 // UTC+5:30, no DST.
|
Chris@0
|
73 'Asia/Kolkata',
|
Chris@0
|
74 // UTC+12, no DST
|
Chris@0
|
75 'Pacific/Funafuti',
|
Chris@0
|
76 // UTC+13, no DST.
|
Chris@0
|
77 'Pacific/Tongatapu',
|
Chris@0
|
78 ];
|
Chris@0
|
79
|
Chris@0
|
80 /**
|
Chris@0
|
81 * Returns the type of field to be tested.
|
Chris@0
|
82 *
|
Chris@0
|
83 * @return string
|
Chris@0
|
84 */
|
Chris@0
|
85 abstract protected function getTestFieldType();
|
Chris@0
|
86
|
Chris@0
|
87 /**
|
Chris@0
|
88 * {@inheritdoc}
|
Chris@0
|
89 */
|
Chris@0
|
90 protected function setUp() {
|
Chris@0
|
91 parent::setUp();
|
Chris@0
|
92
|
Chris@0
|
93 $web_user = $this->drupalCreateUser([
|
Chris@0
|
94 'access content',
|
Chris@0
|
95 'view test entity',
|
Chris@0
|
96 'administer entity_test content',
|
Chris@0
|
97 'administer entity_test form display',
|
Chris@0
|
98 'administer content types',
|
Chris@0
|
99 'administer node fields',
|
Chris@0
|
100 ]);
|
Chris@0
|
101 $this->drupalLogin($web_user);
|
Chris@0
|
102
|
Chris@0
|
103 // Create a field with settings to validate.
|
Chris@0
|
104 $this->createField();
|
Chris@0
|
105
|
Chris@0
|
106 $this->dateFormatter = $this->container->get('date.formatter');
|
Chris@0
|
107 }
|
Chris@0
|
108
|
Chris@0
|
109 /**
|
Chris@0
|
110 * Creates a date test field.
|
Chris@0
|
111 */
|
Chris@0
|
112 protected function createField() {
|
Chris@17
|
113 $field_name = mb_strtolower($this->randomMachineName());
|
Chris@0
|
114 $type = $this->getTestFieldType();
|
Chris@0
|
115 $widget_type = $formatter_type = $type . '_default';
|
Chris@0
|
116
|
Chris@0
|
117 $this->fieldStorage = FieldStorageConfig::create([
|
Chris@0
|
118 'field_name' => $field_name,
|
Chris@0
|
119 'entity_type' => 'entity_test',
|
Chris@0
|
120 'type' => $type,
|
Chris@0
|
121 'settings' => ['datetime_type' => DateTimeItem::DATETIME_TYPE_DATE],
|
Chris@0
|
122 ]);
|
Chris@0
|
123 $this->fieldStorage->save();
|
Chris@0
|
124 $this->field = FieldConfig::create([
|
Chris@0
|
125 'field_storage' => $this->fieldStorage,
|
Chris@0
|
126 'bundle' => 'entity_test',
|
Chris@0
|
127 'description' => 'Description for ' . $field_name,
|
Chris@0
|
128 'required' => TRUE,
|
Chris@0
|
129 ]);
|
Chris@0
|
130 $this->field->save();
|
Chris@0
|
131
|
Chris@0
|
132 EntityFormDisplay::load('entity_test.entity_test.default')
|
Chris@0
|
133 ->setComponent($field_name, ['type' => $widget_type])
|
Chris@0
|
134 ->save();
|
Chris@0
|
135
|
Chris@0
|
136 $this->displayOptions = [
|
Chris@0
|
137 'type' => $formatter_type,
|
Chris@0
|
138 'label' => 'hidden',
|
Chris@0
|
139 'settings' => ['format_type' => 'medium'] + $this->defaultSettings,
|
Chris@0
|
140 ];
|
Chris@0
|
141 EntityViewDisplay::create([
|
Chris@0
|
142 'targetEntityType' => $this->field->getTargetEntityTypeId(),
|
Chris@0
|
143 'bundle' => $this->field->getTargetBundle(),
|
Chris@0
|
144 'mode' => 'full',
|
Chris@0
|
145 'status' => TRUE,
|
Chris@0
|
146 ])->setComponent($field_name, $this->displayOptions)
|
Chris@0
|
147 ->save();
|
Chris@0
|
148 }
|
Chris@0
|
149
|
Chris@0
|
150 /**
|
Chris@0
|
151 * Renders a entity_test and sets the output in the internal browser.
|
Chris@0
|
152 *
|
Chris@0
|
153 * @param int $id
|
Chris@0
|
154 * The entity_test ID to render.
|
Chris@0
|
155 * @param string $view_mode
|
Chris@0
|
156 * (optional) The view mode to use for rendering. Defaults to 'full'.
|
Chris@0
|
157 * @param bool $reset
|
Chris@0
|
158 * (optional) Whether to reset the entity_test controller cache. Defaults to
|
Chris@0
|
159 * TRUE to simplify testing.
|
Chris@0
|
160 */
|
Chris@0
|
161 protected function renderTestEntity($id, $view_mode = 'full', $reset = TRUE) {
|
Chris@0
|
162 if ($reset) {
|
Chris@0
|
163 $this->container->get('entity_type.manager')->getStorage('entity_test')->resetCache([$id]);
|
Chris@0
|
164 }
|
Chris@0
|
165 $entity = EntityTest::load($id);
|
Chris@0
|
166 $display = EntityViewDisplay::collectRenderDisplay($entity, $view_mode);
|
Chris@0
|
167 $build = $display->build($entity);
|
Chris@0
|
168 $output = $this->container->get('renderer')->renderRoot($build);
|
Chris@0
|
169 $this->setRawContent($output);
|
Chris@0
|
170 $this->verbose($output);
|
Chris@0
|
171 }
|
Chris@0
|
172
|
Chris@0
|
173 /**
|
Chris@0
|
174 * Sets the site timezone to a given timezone.
|
Chris@0
|
175 *
|
Chris@0
|
176 * @param string $timezone
|
Chris@0
|
177 * The timezone identifier to set.
|
Chris@0
|
178 */
|
Chris@0
|
179 protected function setSiteTimezone($timezone) {
|
Chris@0
|
180 // Set an explicit site timezone, and disallow per-user timezones.
|
Chris@0
|
181 $this->config('system.date')
|
Chris@0
|
182 ->set('timezone.user.configurable', 0)
|
Chris@0
|
183 ->set('timezone.default', $timezone)
|
Chris@0
|
184 ->save();
|
Chris@0
|
185 }
|
Chris@0
|
186
|
Chris@14
|
187 /**
|
Chris@14
|
188 * Massages test date values.
|
Chris@14
|
189 *
|
Chris@14
|
190 * If a date object is generated directly by a test, then it needs to be
|
Chris@14
|
191 * adjusted to behave like the computed date from the item.
|
Chris@14
|
192 *
|
Chris@14
|
193 * @param \Drupal\Core\Datetime\DrupalDateTime $date
|
Chris@14
|
194 * A date object directly generated by the test.
|
Chris@14
|
195 */
|
Chris@14
|
196 protected function massageTestDate($date) {
|
Chris@14
|
197 if ($this->field->getSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
|
Chris@14
|
198 // Set the default time for date-only items.
|
Chris@14
|
199 $date->setDefaultDateTime();
|
Chris@14
|
200 }
|
Chris@14
|
201 }
|
Chris@14
|
202
|
Chris@0
|
203 }
|