a234ae08f7cac197802aa0332e4adbf7da50bfcd
[yaffs-website] / web / core / modules / datetime / src / Tests / DateTestBase.php
1 <?php
2
3 namespace Drupal\datetime\Tests;
4
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);
6
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;
15
16 /**
17  * Provides a base class for testing Datetime field functionality.
18  *
19  * @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0.
20  *   Use \Drupal\Tests\BrowserTestBase instead.
21  */
22 abstract class DateTestBase extends WebTestBase {
23
24   /**
25    * Modules to enable.
26    *
27    * @var array
28    */
29   public static $modules = ['node', 'entity_test', 'datetime', 'field_ui'];
30
31   /**
32    * An array of display options to pass to entity_get_display()
33    *
34    * @var array
35    */
36   protected $displayOptions;
37
38   /**
39    * A field storage to use in this test class.
40    *
41    * @var \Drupal\field\Entity\FieldStorageConfig
42    */
43   protected $fieldStorage;
44
45   /**
46    * The field used in this test class.
47    *
48    * @var \Drupal\field\Entity\FieldConfig
49    */
50   protected $field;
51
52   /**
53    * The date formatter service.
54    *
55    * @var \Drupal\Core\Datetime\DateFormatterInterface
56    */
57   protected $dateFormatter;
58
59   /**
60    * An array of timezone extremes to test.
61    *
62    * @var string[]
63    */
64   protected static $timezones = [
65     // UTC-12, no DST.
66     'Pacific/Kwajalein',
67     // UTC-11, no DST
68     'Pacific/Midway',
69     // UTC-7, no DST.
70     'America/Phoenix',
71     // UTC.
72     'UTC',
73     // UTC+5:30, no DST.
74     'Asia/Kolkata',
75     // UTC+12, no DST
76     'Pacific/Funafuti',
77     // UTC+13, no DST.
78     'Pacific/Tongatapu',
79   ];
80
81   /**
82    * Returns the type of field to be tested.
83    *
84    * @return string
85    */
86   abstract protected function getTestFieldType();
87
88   /**
89    * {@inheritdoc}
90    */
91   protected function setUp() {
92     parent::setUp();
93
94     $web_user = $this->drupalCreateUser([
95       'access content',
96       'view test entity',
97       'administer entity_test content',
98       'administer entity_test form display',
99       'administer content types',
100       'administer node fields',
101     ]);
102     $this->drupalLogin($web_user);
103
104     // Create a field with settings to validate.
105     $this->createField();
106
107     $this->dateFormatter = $this->container->get('date.formatter');
108   }
109
110   /**
111    * Creates a date test field.
112    */
113   protected function createField() {
114     $field_name = Unicode::strtolower($this->randomMachineName());
115     $type = $this->getTestFieldType();
116     $widget_type = $formatter_type = $type . '_default';
117
118     $this->fieldStorage = FieldStorageConfig::create([
119       'field_name' => $field_name,
120       'entity_type' => 'entity_test',
121       'type' => $type,
122       'settings' => ['datetime_type' => DateTimeItem::DATETIME_TYPE_DATE],
123     ]);
124     $this->fieldStorage->save();
125     $this->field = FieldConfig::create([
126       'field_storage' => $this->fieldStorage,
127       'bundle' => 'entity_test',
128       'description' => 'Description for ' . $field_name,
129       'required' => TRUE,
130     ]);
131     $this->field->save();
132
133     EntityFormDisplay::load('entity_test.entity_test.default')
134       ->setComponent($field_name, ['type' => $widget_type])
135       ->save();
136
137     $this->displayOptions = [
138       'type' => $formatter_type,
139       'label' => 'hidden',
140       'settings' => ['format_type' => 'medium'] + $this->defaultSettings,
141     ];
142     EntityViewDisplay::create([
143       'targetEntityType' => $this->field->getTargetEntityTypeId(),
144       'bundle' => $this->field->getTargetBundle(),
145       'mode' => 'full',
146       'status' => TRUE,
147     ])->setComponent($field_name, $this->displayOptions)
148       ->save();
149   }
150
151   /**
152    * Renders a entity_test and sets the output in the internal browser.
153    *
154    * @param int $id
155    *   The entity_test ID to render.
156    * @param string $view_mode
157    *   (optional) The view mode to use for rendering. Defaults to 'full'.
158    * @param bool $reset
159    *   (optional) Whether to reset the entity_test controller cache. Defaults to
160    *   TRUE to simplify testing.
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     $output = $this->container->get('renderer')->renderRoot($build);
170     $this->setRawContent($output);
171     $this->verbose($output);
172   }
173
174   /**
175    * Sets the site timezone to a given timezone.
176    *
177    * @param string $timezone
178    *   The timezone identifier to set.
179    */
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)
185       ->save();
186   }
187
188 }