3 namespace Drupal\Tests\datetime\Functional;
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;
15 * Provides a base class for testing Datetime field functionality.
17 abstract class DateTestBase extends BrowserTestBase {
24 public static $modules = ['node', 'entity_test', 'datetime', 'field_ui'];
27 * An array of display options to pass to entity_get_display()
31 protected $displayOptions;
34 * A field storage to use in this test class.
36 * @var \Drupal\field\Entity\FieldStorageConfig
38 protected $fieldStorage;
41 * The field used in this test class.
43 * @var \Drupal\field\Entity\FieldConfig
48 * The date formatter service.
50 * @var \Drupal\Core\Datetime\DateFormatterInterface
52 protected $dateFormatter;
55 * An array of timezone extremes to test.
59 protected static $timezones = [
77 * Returns the type of field to be tested.
81 abstract protected function getTestFieldType();
86 protected function setUp() {
89 $web_user = $this->drupalCreateUser([
92 'administer entity_test content',
93 'administer entity_test form display',
94 'administer content types',
95 'administer node fields',
97 $this->drupalLogin($web_user);
99 // Create a field with settings to validate.
100 $this->createField();
102 $this->dateFormatter = $this->container->get('date.formatter');
106 * Creates a date test field.
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';
114 $this->fieldStorage = FieldStorageConfig::create([
115 'field_name' => $field_name,
116 'entity_type' => 'entity_test',
118 'settings' => ['datetime_type' => DateTimeItem::DATETIME_TYPE_DATE],
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,
128 $this->field->save();
130 EntityFormDisplay::load('entity_test.entity_test.default')
131 ->setComponent($field_name, ['type' => $widget_type])
134 $this->displayOptions = [
135 'type' => $formatter_type,
137 'settings' => ['format_type' => 'medium'] + $this->defaultSettings,
139 EntityViewDisplay::create([
140 'targetEntityType' => $this->field->getTargetEntityTypeId(),
141 'bundle' => $this->field->getTargetBundle(),
144 ])->setComponent($field_name, $this->displayOptions)
149 * Renders a entity_test and sets the output in the internal browser.
152 * The entity_test ID to render.
153 * @param string $view_mode
154 * (optional) The view mode to use for rendering. Defaults to 'full'.
156 * (optional) Whether to reset the entity_test controller cache. Defaults to
157 * TRUE to simplify testing.
160 * The rendered HTML output.
162 protected function renderTestEntity($id, $view_mode = 'full', $reset = TRUE) {
164 $this->container->get('entity_type.manager')->getStorage('entity_test')->resetCache([$id]);
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);
173 * Sets the site timezone to a given timezone.
175 * @param string $timezone
176 * The timezone identifier to set.
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)