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