3 namespace Drupal\datetime\Tests;
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);
7 use Drupal\Core\Entity\Entity\EntityFormDisplay;
8 use Drupal\Core\Entity\Entity\EntityViewDisplay;
9 use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
10 use Drupal\entity_test\Entity\EntityTest;
11 use Drupal\field\Entity\FieldConfig;
12 use Drupal\field\Entity\FieldStorageConfig;
13 use Drupal\simpletest\WebTestBase;
16 * Provides a base class for testing Datetime field functionality.
18 * @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0.
19 * Use \Drupal\Tests\BrowserTestBase instead.
21 abstract class DateTestBase extends WebTestBase {
28 public static $modules = ['node', 'entity_test', 'datetime', 'field_ui'];
31 * An array of display options to pass to entity_get_display()
35 protected $displayOptions;
38 * A field storage to use in this test class.
40 * @var \Drupal\field\Entity\FieldStorageConfig
42 protected $fieldStorage;
45 * The field used in this test class.
47 * @var \Drupal\field\Entity\FieldConfig
52 * The date formatter service.
54 * @var \Drupal\Core\Datetime\DateFormatterInterface
56 protected $dateFormatter;
59 * An array of timezone extremes to test.
63 protected static $timezones = [
81 * Returns the type of field to be tested.
85 abstract protected function getTestFieldType();
90 protected function setUp() {
93 $web_user = $this->drupalCreateUser([
96 'administer entity_test content',
97 'administer entity_test form display',
98 'administer content types',
99 'administer node fields',
101 $this->drupalLogin($web_user);
103 // Create a field with settings to validate.
104 $this->createField();
106 $this->dateFormatter = $this->container->get('date.formatter');
110 * Creates a date test field.
112 protected function createField() {
113 $field_name = mb_strtolower($this->randomMachineName());
114 $type = $this->getTestFieldType();
115 $widget_type = $formatter_type = $type . '_default';
117 $this->fieldStorage = FieldStorageConfig::create([
118 'field_name' => $field_name,
119 'entity_type' => 'entity_test',
121 'settings' => ['datetime_type' => DateTimeItem::DATETIME_TYPE_DATE],
123 $this->fieldStorage->save();
124 $this->field = FieldConfig::create([
125 'field_storage' => $this->fieldStorage,
126 'bundle' => 'entity_test',
127 'description' => 'Description for ' . $field_name,
130 $this->field->save();
132 EntityFormDisplay::load('entity_test.entity_test.default')
133 ->setComponent($field_name, ['type' => $widget_type])
136 $this->displayOptions = [
137 'type' => $formatter_type,
139 'settings' => ['format_type' => 'medium'] + $this->defaultSettings,
141 EntityViewDisplay::create([
142 'targetEntityType' => $this->field->getTargetEntityTypeId(),
143 'bundle' => $this->field->getTargetBundle(),
146 ])->setComponent($field_name, $this->displayOptions)
151 * Renders a entity_test and sets the output in the internal browser.
154 * The entity_test ID to render.
155 * @param string $view_mode
156 * (optional) The view mode to use for rendering. Defaults to 'full'.
158 * (optional) Whether to reset the entity_test controller cache. Defaults to
159 * TRUE to simplify testing.
161 protected function renderTestEntity($id, $view_mode = 'full', $reset = TRUE) {
163 $this->container->get('entity_type.manager')->getStorage('entity_test')->resetCache([$id]);
165 $entity = EntityTest::load($id);
166 $display = EntityViewDisplay::collectRenderDisplay($entity, $view_mode);
167 $build = $display->build($entity);
168 $output = $this->container->get('renderer')->renderRoot($build);
169 $this->setRawContent($output);
170 $this->verbose($output);
174 * Sets the site timezone to a given timezone.
176 * @param string $timezone
177 * The timezone identifier to set.
179 protected function setSiteTimezone($timezone) {
180 // Set an explicit site timezone, and disallow per-user timezones.
181 $this->config('system.date')
182 ->set('timezone.user.configurable', 0)
183 ->set('timezone.default', $timezone)
188 * Massages test date values.
190 * If a date object is generated directly by a test, then it needs to be
191 * adjusted to behave like the computed date from the item.
193 * @param \Drupal\Core\Datetime\DrupalDateTime $date
194 * A date object directly generated by the test.
196 protected function massageTestDate($date) {
197 if ($this->field->getSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
198 // Set the default time for date-only items.
199 $date->setDefaultDateTime();