View source
<?php
namespace Drupal\Tests\datetime\Kernel\Views;
use Drupal\Component\Datetime\DateTimePlus;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\views\Tests\ViewTestData;
use Drupal\field\Entity\FieldStorageConfig;
abstract class DateTimeHandlerTestBase extends ViewsKernelTestBase {
public static $modules = [
'datetime_test',
'node',
'datetime',
'field',
];
protected static $field_name = 'field_date';
protected static $field_type = 'datetime';
protected $nodes = [];
protected function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);
$this
->installSchema('node', 'node_access');
$this
->installEntitySchema('node');
$this
->installEntitySchema('user');
$node_type = NodeType::create([
'type' => 'page',
'name' => 'page',
]);
$node_type
->save();
$fieldStorage = FieldStorageConfig::create([
'field_name' => static::$field_name,
'entity_type' => 'node',
'type' => static::$field_type,
'settings' => [
'datetime_type' => DateTimeItem::DATETIME_TYPE_DATETIME,
],
]);
$fieldStorage
->save();
$field = FieldConfig::create([
'field_storage' => $fieldStorage,
'bundle' => 'page',
'required' => TRUE,
]);
$field
->save();
$this->container
->get('views.views_data')
->clear();
$this->map = [
'nid' => 'nid',
];
ViewTestData::createTestViews(get_class($this), [
'datetime_test',
]);
}
protected function setSiteTimezone($timezone) {
$this
->config('system.date')
->set('timezone.user.configurable', 0)
->set('timezone.default', $timezone)
->save();
}
protected function getUTCEquivalentOfUserNowAsTimestamp() {
$user_now = new DateTimePlus('now', new \DateTimeZone(date_default_timezone_get()));
$utc_equivalent = new DateTimePlus($user_now
->format('Y-m-d H:i:s'), new \DateTimeZone(DateTimeItemInterface::STORAGE_TIMEZONE));
return $utc_equivalent
->getTimestamp();
}
protected function getRelativeDateValuesFromTimestamp($timestamp) {
return [
\Drupal::service('date.formatter')
->format($timestamp + 86400, 'custom', DateTimeItemInterface::DATE_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE),
\Drupal::service('date.formatter')
->format($timestamp, 'custom', DateTimeItemInterface::DATE_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE),
\Drupal::service('date.formatter')
->format($timestamp - 86400, 'custom', DateTimeItemInterface::DATE_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE),
];
}
}