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