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