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\Component\Utility\Unicode;
8 use Drupal\Core\Entity\Entity\EntityFormDisplay;
9 use Drupal\Core\Entity\Entity\EntityViewDisplay;
10 use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
11 use Drupal\entity_test\Entity\EntityTest;
12 use Drupal\field\Entity\FieldConfig;
13 use Drupal\field\Entity\FieldStorageConfig;
14 use Drupal\simpletest\WebTestBase;
17 * Provides a base class for testing Datetime field functionality.
19 * @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0.
20 * Use \Drupal\Tests\BrowserTestBase instead.
22 abstract class DateTestBase extends WebTestBase {
29 public static $modules = ['node', 'entity_test', 'datetime', 'field_ui'];
32 * An array of display options to pass to entity_get_display()
36 protected $displayOptions;
39 * A field storage to use in this test class.
41 * @var \Drupal\field\Entity\FieldStorageConfig
43 protected $fieldStorage;
46 * The field used in this test class.
48 * @var \Drupal\field\Entity\FieldConfig
53 * The date formatter service.
55 * @var \Drupal\Core\Datetime\DateFormatterInterface
57 protected $dateFormatter;
60 * An array of timezone extremes to test.
64 protected static $timezones = [
82 * Returns the type of field to be tested.
86 abstract protected function getTestFieldType();
91 protected function setUp() {
94 $web_user = $this->drupalCreateUser([
97 'administer entity_test content',
98 'administer entity_test form display',
99 'administer content types',
100 'administer node fields',
102 $this->drupalLogin($web_user);
104 // Create a field with settings to validate.
105 $this->createField();
107 $this->dateFormatter = $this->container->get('date.formatter');
111 * Creates a date test field.
113 protected function createField() {
114 $field_name = Unicode::strtolower($this->randomMachineName());
115 $type = $this->getTestFieldType();
116 $widget_type = $formatter_type = $type . '_default';
118 $this->fieldStorage = FieldStorageConfig::create([
119 'field_name' => $field_name,
120 'entity_type' => 'entity_test',
122 'settings' => ['datetime_type' => DateTimeItem::DATETIME_TYPE_DATE],
124 $this->fieldStorage->save();
125 $this->field = FieldConfig::create([
126 'field_storage' => $this->fieldStorage,
127 'bundle' => 'entity_test',
128 'description' => 'Description for ' . $field_name,
131 $this->field->save();
133 EntityFormDisplay::load('entity_test.entity_test.default')
134 ->setComponent($field_name, ['type' => $widget_type])
137 $this->displayOptions = [
138 'type' => $formatter_type,
140 'settings' => ['format_type' => 'medium'] + $this->defaultSettings,
142 EntityViewDisplay::create([
143 'targetEntityType' => $this->field->getTargetEntityTypeId(),
144 'bundle' => $this->field->getTargetBundle(),
147 ])->setComponent($field_name, $this->displayOptions)
152 * Renders a entity_test and sets the output in the internal browser.
155 * The entity_test ID to render.
156 * @param string $view_mode
157 * (optional) The view mode to use for rendering. Defaults to 'full'.
159 * (optional) Whether to reset the entity_test controller cache. Defaults to
160 * TRUE to simplify testing.
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 $output = $this->container->get('renderer')->renderRoot($build);
170 $this->setRawContent($output);
171 $this->verbose($output);
175 * Sets the site timezone to a given timezone.
177 * @param string $timezone
178 * The timezone identifier to set.
180 protected function setSiteTimezone($timezone) {
181 // Set an explicit site timezone, and disallow per-user timezones.
182 $this->config('system.date')
183 ->set('timezone.user.configurable', 0)
184 ->set('timezone.default', $timezone)