View source
<?php
namespace Drupal\Tests\datetime\Functional;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\BrowserTestBase;
abstract class DateTestBase extends BrowserTestBase {
protected static $modules = [
'node',
'entity_test',
'datetime',
'field_ui',
];
protected $displayOptions;
protected $fieldStorage;
protected $field;
protected $dateFormatter;
protected static $timezones = [
'Pacific/Kwajalein',
'Pacific/Midway',
'America/Phoenix',
'UTC',
'Asia/Kolkata',
'Pacific/Funafuti',
'Pacific/Tongatapu',
];
protected abstract function getTestFieldType();
protected function setUp() : void {
parent::setUp();
$web_user = $this
->drupalCreateUser([
'access content',
'view test entity',
'administer entity_test content',
'administer entity_test form display',
'administer content types',
'bypass node access',
'administer node fields',
]);
$this
->drupalLogin($web_user);
$this
->createField();
$this->dateFormatter = $this->container
->get('date.formatter');
}
protected function createField() {
$field_name = mb_strtolower($this
->randomMachineName());
$field_label = Unicode::ucfirst(mb_strtolower($this
->randomMachineName()));
$type = $this
->getTestFieldType();
$widget_type = $formatter_type = $type . '_default';
$this->fieldStorage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test',
'type' => $type,
'settings' => [
'datetime_type' => DateTimeItem::DATETIME_TYPE_DATE,
],
]);
$this->fieldStorage
->save();
$this->field = FieldConfig::create([
'field_storage' => $this->fieldStorage,
'label' => $field_label,
'bundle' => 'entity_test',
'description' => 'Description for ' . $field_label,
'required' => TRUE,
]);
$this->field
->save();
EntityFormDisplay::load('entity_test.entity_test.default')
->setComponent($field_name, [
'type' => $widget_type,
])
->save();
$this->displayOptions = [
'type' => $formatter_type,
'label' => 'hidden',
'settings' => [
'format_type' => 'medium',
],
];
EntityViewDisplay::create([
'targetEntityType' => $this->field
->getTargetEntityTypeId(),
'bundle' => $this->field
->getTargetBundle(),
'mode' => 'full',
'status' => TRUE,
])
->setComponent($field_name, $this->displayOptions)
->save();
}
protected function renderTestEntity($id, $view_mode = 'full', $reset = TRUE) {
if ($reset) {
$this->container
->get('entity_type.manager')
->getStorage('entity_test')
->resetCache([
$id,
]);
}
$entity = EntityTest::load($id);
$display = EntityViewDisplay::collectRenderDisplay($entity, $view_mode);
$build = $display
->build($entity);
return (string) $this->container
->get('renderer')
->renderRoot($build);
}
protected function setSiteTimezone($timezone) {
$this
->config('system.date')
->set('timezone.user.configurable', 0)
->set('timezone.default', $timezone)
->save();
}
protected function massageTestDate($date) {
if ($this->field
->getSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
$date
->setDefaultDateTime();
}
}
}