Update Composer, update everything
This commit is contained in:
parent
ea3e94409f
commit
dda5c284b6
19527 changed files with 1135420 additions and 351004 deletions
|
@ -0,0 +1,32 @@
|
|||
views.argument.datetime:
|
||||
type: views.argument.date
|
||||
|
||||
views.argument.datetime_day:
|
||||
type: views.argument.datetime
|
||||
|
||||
views.argument.datetime_full_date:
|
||||
type: views.argument.datetime
|
||||
|
||||
views.argument.datetime_month:
|
||||
type: views.argument.datetime
|
||||
|
||||
views.argument.datetime_week:
|
||||
type: views.argument.datetime
|
||||
|
||||
views.argument.datetime_year:
|
||||
type: views.argument.datetime
|
||||
|
||||
views.argument.datetime_year_month:
|
||||
type: views.argument.datetime
|
||||
|
||||
views.filter.datetime:
|
||||
type: views.filter.date
|
||||
|
||||
views.filter_value.datetime:
|
||||
type: views.filter_value.date
|
||||
|
||||
views.sort.datetime:
|
||||
type: views.sort.date
|
||||
|
||||
views.sort_expose.datetime:
|
||||
type: views.sort_expose.date
|
|
@ -5,4 +5,4 @@ package: Field types
|
|||
version: VERSION
|
||||
core: 8.x
|
||||
dependencies:
|
||||
- field
|
||||
- drupal:field
|
||||
|
|
|
@ -9,16 +9,34 @@ use Drupal\Core\Routing\RouteMatchInterface;
|
|||
|
||||
/**
|
||||
* Defines the timezone that dates should be stored in.
|
||||
*
|
||||
* @deprecated in Drupal 8.5.x and will be removed before Drupal 9.0.x. Use
|
||||
* \Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface::STORAGE_TIMEZONE
|
||||
* instead.
|
||||
*
|
||||
* @see https://www.drupal.org/node/2912980
|
||||
*/
|
||||
const DATETIME_STORAGE_TIMEZONE = 'UTC';
|
||||
|
||||
/**
|
||||
* Defines the format that date and time should be stored in.
|
||||
*
|
||||
* @deprecated in Drupal 8.5.x and will be removed before Drupal 9.0.x. Use
|
||||
* \Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface::DATETIME_STORAGE_FORMAT
|
||||
* instead.
|
||||
*
|
||||
* @see https://www.drupal.org/node/2912980
|
||||
*/
|
||||
const DATETIME_DATETIME_STORAGE_FORMAT = 'Y-m-d\TH:i:s';
|
||||
|
||||
/**
|
||||
* Defines the format that dates should be stored in.
|
||||
*
|
||||
* @deprecated in Drupal 8.5.x and will be removed before Drupal 9.0.x. Use
|
||||
* \Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface::DATE_STORAGE_FORMAT
|
||||
* instead.
|
||||
*
|
||||
* @see https://www.drupal.org/node/2912980
|
||||
*/
|
||||
const DATETIME_DATE_STORAGE_FORMAT = 'Y-m-d';
|
||||
|
||||
|
@ -50,7 +68,20 @@ function datetime_help($route_name, RouteMatchInterface $route_match) {
|
|||
* same value for in both the local timezone and UTC.
|
||||
*
|
||||
* @param $date
|
||||
*
|
||||
* @deprecated in Drupal 8.5.0 and will be removed before Drupal 9.0.0. Use
|
||||
* \Drupal\Component\Datetime\DateTimePlus::setDefaultDateTime() or
|
||||
* \Drupal\Core\Datetime\DrupalDateTime::setDefaultDateTime() instead.
|
||||
*
|
||||
* @see https://www.drupal.org/node/2880931
|
||||
*/
|
||||
function datetime_date_default_time($date) {
|
||||
@trigger_error('datetime_date_default_time() is deprecated in Drupal 8.5.0 and will be removed before Drupal 9.0.0. Use \Drupal\Component\Datetime\DateTimePlus::setDefaultDateTime() or \Drupal\Core\Datetime\DrupalDateTime::setDefaultDateTime() instead. See https://www.drupal.org/node/2880931.', E_USER_DEPRECATED);
|
||||
|
||||
// For maximum BC before this method is removed, we do not use the
|
||||
// recommendation from the deprecation method. Instead, we call the setTime()
|
||||
// method directly. This allows the method to continue to work with
|
||||
// \DateTime, DateTimePlus, and DrupalDateTime objects (and classes that
|
||||
// may derive from them).
|
||||
$date->setTime(12, 0, 0);
|
||||
}
|
||||
|
|
|
@ -11,18 +11,42 @@ use Drupal\field\FieldStorageConfigInterface;
|
|||
* Implements hook_field_views_data().
|
||||
*/
|
||||
function datetime_field_views_data(FieldStorageConfigInterface $field_storage) {
|
||||
return datetime_type_field_views_data_helper($field_storage, [], $field_storage->getMainPropertyName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides Views integration for any datetime-based fields.
|
||||
*
|
||||
* Overrides the default Views data for datetime-based fields, adding datetime
|
||||
* views plugins. Modules defining new datetime-based fields may use this
|
||||
* function to simplify Views integration.
|
||||
*
|
||||
* @param \Drupal\field\FieldStorageConfigInterface $field_storage
|
||||
* The field storage config entity.
|
||||
* @param array $data
|
||||
* Field view data or views_field_default_views_data($field_storage) if empty.
|
||||
* @param string $column_name
|
||||
* The schema column name with the datetime value.
|
||||
*
|
||||
* @return array
|
||||
* The array of field views data with the datetime plugin.
|
||||
*
|
||||
* @see datetime_field_views_data()
|
||||
* @see datetime_range_field_views_data()
|
||||
*/
|
||||
function datetime_type_field_views_data_helper(FieldStorageConfigInterface $field_storage, array $data, $column_name) {
|
||||
// @todo This code only covers configurable fields, handle base table fields
|
||||
// in https://www.drupal.org/node/2489476.
|
||||
$data = views_field_default_views_data($field_storage);
|
||||
$data = empty($data) ? views_field_default_views_data($field_storage) : $data;
|
||||
foreach ($data as $table_name => $table_data) {
|
||||
// Set the 'datetime' filter type.
|
||||
$data[$table_name][$field_storage->getName() . '_value']['filter']['id'] = 'datetime';
|
||||
$data[$table_name][$field_storage->getName() . '_' . $column_name]['filter']['id'] = 'datetime';
|
||||
|
||||
// Set the 'datetime' argument type.
|
||||
$data[$table_name][$field_storage->getName() . '_value']['argument']['id'] = 'datetime';
|
||||
$data[$table_name][$field_storage->getName() . '_' . $column_name]['argument']['id'] = 'datetime';
|
||||
|
||||
// Create year, month, and day arguments.
|
||||
$group = $data[$table_name][$field_storage->getName() . '_value']['group'];
|
||||
$group = $data[$table_name][$field_storage->getName() . '_' . $column_name]['group'];
|
||||
$arguments = [
|
||||
// Argument type => help text.
|
||||
'year' => t('Date in the form of YYYY.'),
|
||||
|
@ -33,19 +57,26 @@ function datetime_field_views_data(FieldStorageConfigInterface $field_storage) {
|
|||
'full_date' => t('Date in the form of CCYYMMDD.'),
|
||||
];
|
||||
foreach ($arguments as $argument_type => $help_text) {
|
||||
$data[$table_name][$field_storage->getName() . '_value_' . $argument_type] = [
|
||||
'title' => $field_storage->getLabel() . ' (' . $argument_type . ')',
|
||||
$column_name_text = $column_name === $field_storage->getMainPropertyName() ? '' : ':' . $column_name;
|
||||
$data[$table_name][$field_storage->getName() . '_' . $column_name . '_' . $argument_type] = [
|
||||
'title' => t('@label@column (@argument)', [
|
||||
'@label' => $field_storage->getLabel(),
|
||||
'@column' => $column_name_text,
|
||||
'@argument' => $argument_type,
|
||||
]),
|
||||
'help' => $help_text,
|
||||
'argument' => [
|
||||
'field' => $field_storage->getName() . '_value',
|
||||
'field' => $field_storage->getName() . '_' . $column_name,
|
||||
'id' => 'datetime_' . $argument_type,
|
||||
'entity_type' => $field_storage->getTargetEntityTypeId(),
|
||||
'field_name' => $field_storage->getName(),
|
||||
],
|
||||
'group' => $group,
|
||||
];
|
||||
}
|
||||
|
||||
// Set the 'datetime' sort handler.
|
||||
$data[$table_name][$field_storage->getName() . '_value']['sort']['id'] = 'datetime';
|
||||
$data[$table_name][$field_storage->getName() . '_' . $column_name]['sort']['id'] = 'datetime';
|
||||
}
|
||||
|
||||
return $data;
|
||||
|
|
|
@ -7,6 +7,7 @@ use Drupal\Core\Datetime\DrupalDateTime;
|
|||
use Drupal\Core\TypedData\DataDefinitionInterface;
|
||||
use Drupal\Core\TypedData\TypedDataInterface;
|
||||
use Drupal\Core\TypedData\TypedData;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
|
||||
|
||||
/**
|
||||
* A computed property for dates of date time field items.
|
||||
|
@ -36,7 +37,7 @@ class DateTimeComputed extends TypedData {
|
|||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getValue($langcode = NULL) {
|
||||
public function getValue() {
|
||||
if ($this->date !== NULL) {
|
||||
return $this->date;
|
||||
}
|
||||
|
@ -46,9 +47,9 @@ class DateTimeComputed extends TypedData {
|
|||
$value = $item->{($this->definition->getSetting('date source'))};
|
||||
|
||||
$datetime_type = $item->getFieldDefinition()->getSetting('datetime_type');
|
||||
$storage_format = $datetime_type === DateTimeItem::DATETIME_TYPE_DATE ? DATETIME_DATE_STORAGE_FORMAT : DATETIME_DATETIME_STORAGE_FORMAT;
|
||||
$storage_format = $datetime_type === DateTimeItem::DATETIME_TYPE_DATE ? DateTimeItemInterface::DATE_STORAGE_FORMAT : DateTimeItemInterface::DATETIME_STORAGE_FORMAT;
|
||||
try {
|
||||
$date = DrupalDateTime::createFromFormat($storage_format, $value, DATETIME_STORAGE_TIMEZONE);
|
||||
$date = DrupalDateTime::createFromFormat($storage_format, $value, DateTimeItemInterface::STORAGE_TIMEZONE);
|
||||
if ($date instanceof DrupalDateTime && !$date->hasErrors()) {
|
||||
$this->date = $date;
|
||||
// If the format did not include an explicit time portion, then the
|
||||
|
@ -56,12 +57,10 @@ class DateTimeComputed extends TypedData {
|
|||
// set the time to 12:00:00 UTC for date-only fields. This is used so
|
||||
// that the local date portion is the same, across nearly all time
|
||||
// zones.
|
||||
// @see datetime_date_default_time()
|
||||
// @see http://php.net/manual/en/datetime.createfromformat.php
|
||||
// @todo Update comment and/or code per the chosen solution in
|
||||
// https://www.drupal.org/node/2830094
|
||||
// @see \Drupal\Component\Datetime\DateTimePlus::setDefaultDateTime()
|
||||
// @see http://php.net/manual/datetime.createfromformat.php
|
||||
if ($datetime_type === DateTimeItem::DATETIME_TYPE_DATE) {
|
||||
$this->date->setTime(12, 0, 0);
|
||||
$this->date->setDefaultDateTime();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ namespace Drupal\datetime\Plugin\Field\FieldFormatter;
|
|||
use Drupal\Core\Datetime\DrupalDateTime;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'Custom' formatter for 'datetime' fields.
|
||||
|
@ -24,7 +25,7 @@ class DateTimeCustomFormatter extends DateTimeFormatterBase {
|
|||
*/
|
||||
public static function defaultSettings() {
|
||||
return [
|
||||
'date_format' => DATETIME_DATETIME_STORAGE_FORMAT,
|
||||
'date_format' => DateTimeItemInterface::DATETIME_STORAGE_FORMAT,
|
||||
] + parent::defaultSettings();
|
||||
}
|
||||
|
||||
|
@ -32,30 +33,18 @@ class DateTimeCustomFormatter extends DateTimeFormatterBase {
|
|||
* {@inheritdoc}
|
||||
*/
|
||||
public function viewElements(FieldItemListInterface $items, $langcode) {
|
||||
// @todo Evaluate removing this method in
|
||||
// https://www.drupal.org/node/2793143 to determine if the behavior and
|
||||
// markup in the base class implementation can be used instead.
|
||||
$elements = [];
|
||||
|
||||
foreach ($items as $delta => $item) {
|
||||
$output = '';
|
||||
if (!empty($item->date)) {
|
||||
/** @var \Drupal\Core\Datetime\DrupalDateTime $date */
|
||||
$date = $item->date;
|
||||
|
||||
if ($this->getFieldSetting('datetime_type') == 'date') {
|
||||
// A date without time will pick up the current time, use the default.
|
||||
datetime_date_default_time($date);
|
||||
}
|
||||
$this->setTimeZone($date);
|
||||
|
||||
$output = $this->formatDate($date);
|
||||
$elements[$delta] = $this->buildDate($date);
|
||||
}
|
||||
$elements[$delta] = [
|
||||
'#markup' => $output,
|
||||
'#cache' => [
|
||||
'contexts' => [
|
||||
'timezone',
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
return $elements;
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
namespace Drupal\datetime\Plugin\Field\FieldFormatter;
|
||||
|
||||
use Drupal\Core\Datetime\DrupalDateTime;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
|
||||
/**
|
||||
|
@ -28,59 +27,6 @@ class DateTimeDefaultFormatter extends DateTimeFormatterBase {
|
|||
] + parent::defaultSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function viewElements(FieldItemListInterface $items, $langcode) {
|
||||
$elements = [];
|
||||
|
||||
foreach ($items as $delta => $item) {
|
||||
$output = '';
|
||||
$iso_date = '';
|
||||
|
||||
if ($item->date) {
|
||||
/** @var \Drupal\Core\Datetime\DrupalDateTime $date */
|
||||
$date = $item->date;
|
||||
|
||||
if ($this->getFieldSetting('datetime_type') == 'date') {
|
||||
// A date without time will pick up the current time, use the default.
|
||||
datetime_date_default_time($date);
|
||||
}
|
||||
|
||||
// Create the ISO date in Universal Time.
|
||||
$iso_date = $date->format("Y-m-d\TH:i:s") . 'Z';
|
||||
|
||||
$this->setTimeZone($date);
|
||||
|
||||
$output = $this->formatDate($date);
|
||||
}
|
||||
|
||||
// Display the date using theme datetime.
|
||||
$elements[$delta] = [
|
||||
'#cache' => [
|
||||
'contexts' => [
|
||||
'timezone',
|
||||
],
|
||||
],
|
||||
'#theme' => 'time',
|
||||
'#text' => $output,
|
||||
'#html' => FALSE,
|
||||
'#attributes' => [
|
||||
'datetime' => $iso_date,
|
||||
],
|
||||
];
|
||||
if (!empty($item->_attributes)) {
|
||||
$elements[$delta]['#attributes'] += $item->_attributes;
|
||||
// Unset field item attributes since they have been included in the
|
||||
// formatter output and should not be rendered in the field template.
|
||||
unset($item->_attributes);
|
||||
}
|
||||
}
|
||||
|
||||
return $elements;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
@ -122,7 +68,7 @@ class DateTimeDefaultFormatter extends DateTimeFormatterBase {
|
|||
$summary = parent::settingsSummary();
|
||||
|
||||
$date = new DrupalDateTime();
|
||||
$summary[] = t('Format: @display', ['@display' => $this->formatDate($date, $this->getFormatSettings())]);
|
||||
$summary[] = t('Format: @display', ['@display' => $this->formatDate($date)]);
|
||||
|
||||
return $summary;
|
||||
}
|
||||
|
|
|
@ -6,13 +6,14 @@ use Drupal\Core\Datetime\DateFormatterInterface;
|
|||
use Drupal\Core\Datetime\DrupalDateTime;
|
||||
use Drupal\Core\Entity\EntityStorageInterface;
|
||||
use Drupal\Core\Field\FieldDefinitionInterface;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Core\Field\FormatterBase;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
|
||||
/**
|
||||
* Base class for 'DateTime Field formatter' plugin implementations.
|
||||
*/
|
||||
|
@ -97,7 +98,7 @@ abstract class DateTimeFormatterBase extends FormatterBase implements ContainerF
|
|||
'#type' => 'select',
|
||||
'#title' => $this->t('Time zone override'),
|
||||
'#description' => $this->t('The time zone selected here will always be used'),
|
||||
'#options' => system_time_zones(TRUE),
|
||||
'#options' => system_time_zones(TRUE, TRUE),
|
||||
'#default_value' => $this->getSetting('timezone_override'),
|
||||
];
|
||||
|
||||
|
@ -117,6 +118,30 @@ abstract class DateTimeFormatterBase extends FormatterBase implements ContainerF
|
|||
return $summary;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function viewElements(FieldItemListInterface $items, $langcode) {
|
||||
$elements = [];
|
||||
|
||||
foreach ($items as $delta => $item) {
|
||||
if ($item->date) {
|
||||
/** @var \Drupal\Core\Datetime\DrupalDateTime $date */
|
||||
$date = $item->date;
|
||||
$elements[$delta] = $this->buildDateWithIsoAttribute($date);
|
||||
|
||||
if (!empty($item->_attributes)) {
|
||||
$elements[$delta]['#attributes'] += $item->_attributes;
|
||||
// Unset field item attributes since they have been included in the
|
||||
// formatter output and should not be rendered in the field template.
|
||||
unset($item->_attributes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $elements;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a formatted date value as a string.
|
||||
*
|
||||
|
@ -143,7 +168,7 @@ abstract class DateTimeFormatterBase extends FormatterBase implements ContainerF
|
|||
protected function setTimeZone(DrupalDateTime $date) {
|
||||
if ($this->getFieldSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
|
||||
// A date without time has no timezone conversion.
|
||||
$timezone = DATETIME_STORAGE_TIMEZONE;
|
||||
$timezone = DateTimeItemInterface::STORAGE_TIMEZONE;
|
||||
}
|
||||
else {
|
||||
$timezone = drupal_get_user_timezone();
|
||||
|
@ -167,4 +192,60 @@ abstract class DateTimeFormatterBase extends FormatterBase implements ContainerF
|
|||
return $settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a render array from a date object.
|
||||
*
|
||||
* @param \Drupal\Core\Datetime\DrupalDateTime $date
|
||||
* A date object.
|
||||
*
|
||||
* @return array
|
||||
* A render array.
|
||||
*/
|
||||
protected function buildDate(DrupalDateTime $date) {
|
||||
$this->setTimeZone($date);
|
||||
|
||||
$build = [
|
||||
'#markup' => $this->formatDate($date),
|
||||
'#cache' => [
|
||||
'contexts' => [
|
||||
'timezone',
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
return $build;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a render array from a date object with ISO date attribute.
|
||||
*
|
||||
* @param \Drupal\Core\Datetime\DrupalDateTime $date
|
||||
* A date object.
|
||||
*
|
||||
* @return array
|
||||
* A render array.
|
||||
*/
|
||||
protected function buildDateWithIsoAttribute(DrupalDateTime $date) {
|
||||
// Create the ISO date in Universal Time.
|
||||
$iso_date = $date->format("Y-m-d\TH:i:s") . 'Z';
|
||||
|
||||
$this->setTimeZone($date);
|
||||
|
||||
$build = [
|
||||
'#theme' => 'time',
|
||||
'#text' => $this->formatDate($date),
|
||||
'#html' => FALSE,
|
||||
'#attributes' => [
|
||||
'datetime' => $iso_date,
|
||||
],
|
||||
'#cache' => [
|
||||
'contexts' => [
|
||||
'timezone',
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
return $build;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ namespace Drupal\datetime\Plugin\Field\FieldFormatter;
|
|||
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'Plain' formatter for 'datetime' fields.
|
||||
|
@ -25,27 +26,12 @@ class DateTimePlainFormatter extends DateTimeFormatterBase {
|
|||
$elements = [];
|
||||
|
||||
foreach ($items as $delta => $item) {
|
||||
$output = '';
|
||||
if (!empty($item->date)) {
|
||||
/** @var \Drupal\Core\Datetime\DrupalDateTime $date */
|
||||
$date = $item->date;
|
||||
|
||||
if ($this->getFieldSetting('datetime_type') == 'date') {
|
||||
// A date without time will pick up the current time, use the default.
|
||||
datetime_date_default_time($date);
|
||||
}
|
||||
$this->setTimeZone($date);
|
||||
|
||||
$output = $this->formatDate($date);
|
||||
$elements[$delta] = $this->buildDate($date);
|
||||
}
|
||||
$elements[$delta] = [
|
||||
'#cache' => [
|
||||
'contexts' => [
|
||||
'timezone',
|
||||
],
|
||||
],
|
||||
'#markup' => $output,
|
||||
];
|
||||
}
|
||||
|
||||
return $elements;
|
||||
|
@ -55,7 +41,7 @@ class DateTimePlainFormatter extends DateTimeFormatterBase {
|
|||
* {@inheritdoc}
|
||||
*/
|
||||
protected function formatDate($date) {
|
||||
$format = $this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE ? DATETIME_DATE_STORAGE_FORMAT : DATETIME_DATETIME_STORAGE_FORMAT;
|
||||
$format = $this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE ? DateTimeItemInterface::DATE_STORAGE_FORMAT : DateTimeItemInterface::DATETIME_STORAGE_FORMAT;
|
||||
$timezone = $this->getSetting('timezone_override');
|
||||
return $this->dateFormatter->format($date->getTimestamp(), 'custom', $format, $timezone != '' ? $timezone : NULL);
|
||||
}
|
||||
|
|
|
@ -2,17 +2,9 @@
|
|||
|
||||
namespace Drupal\datetime\Plugin\Field\FieldFormatter;
|
||||
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\Core\Cache\CacheableMetadata;
|
||||
use Drupal\Core\Datetime\DateFormatterInterface;
|
||||
use Drupal\Core\Datetime\DrupalDateTime;
|
||||
use Drupal\Core\Field\FieldDefinitionInterface;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Core\Field\FormatterBase;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Drupal\Core\Field\Plugin\Field\FieldFormatter\TimestampAgoFormatter;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'Time ago' formatter for 'datetime' fields.
|
||||
|
@ -25,80 +17,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
|
|||
* }
|
||||
* )
|
||||
*/
|
||||
class DateTimeTimeAgoFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
|
||||
|
||||
/**
|
||||
* The date formatter service.
|
||||
*
|
||||
* @var \Drupal\Core\Datetime\DateFormatterInterface
|
||||
*/
|
||||
protected $dateFormatter;
|
||||
|
||||
/**
|
||||
* The current Request object.
|
||||
*
|
||||
* @var \Symfony\Component\HttpFoundation\Request
|
||||
*/
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* Constructs a DateTimeTimeAgoFormatter object.
|
||||
*
|
||||
* @param string $plugin_id
|
||||
* The plugin_id for the formatter.
|
||||
* @param mixed $plugin_definition
|
||||
* The plugin implementation definition.
|
||||
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
|
||||
* The definition of the field to which the formatter is associated.
|
||||
* @param array $settings
|
||||
* The formatter settings.
|
||||
* @param string $label
|
||||
* The formatter label display setting.
|
||||
* @param string $view_mode
|
||||
* The view mode.
|
||||
* @param array $third_party_settings
|
||||
* Third party settings.
|
||||
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
|
||||
* The date formatter service.
|
||||
* @param \Symfony\Component\HttpFoundation\Request $request
|
||||
* The current request.
|
||||
*/
|
||||
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, DateFormatterInterface $date_formatter, Request $request) {
|
||||
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
|
||||
|
||||
$this->dateFormatter = $date_formatter;
|
||||
$this->request = $request;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function defaultSettings() {
|
||||
$settings = [
|
||||
'future_format' => '@interval hence',
|
||||
'past_format' => '@interval ago',
|
||||
'granularity' => 2,
|
||||
] + parent::defaultSettings();
|
||||
|
||||
return $settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
|
||||
return new static(
|
||||
$plugin_id,
|
||||
$plugin_definition,
|
||||
$configuration['field_definition'],
|
||||
$configuration['settings'],
|
||||
$configuration['label'],
|
||||
$configuration['view_mode'],
|
||||
$configuration['third_party_settings'],
|
||||
$container->get('date.formatter'),
|
||||
$container->get('request_stack')->getCurrentRequest()
|
||||
);
|
||||
}
|
||||
class DateTimeTimeAgoFormatter extends TimestampAgoFormatter {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
|
@ -110,10 +29,6 @@ class DateTimeTimeAgoFormatter extends FormatterBase implements ContainerFactory
|
|||
$date = $item->date;
|
||||
$output = [];
|
||||
if (!empty($item->date)) {
|
||||
if ($this->getFieldSetting('datetime_type') == 'date') {
|
||||
// A date without time will pick up the current time, use the default.
|
||||
datetime_date_default_time($date);
|
||||
}
|
||||
$output = $this->formatDate($date);
|
||||
}
|
||||
$elements[$delta] = $output;
|
||||
|
@ -122,50 +37,6 @@ class DateTimeTimeAgoFormatter extends FormatterBase implements ContainerFactory
|
|||
return $elements;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function settingsForm(array $form, FormStateInterface $form_state) {
|
||||
$form = parent::settingsForm($form, $form_state);
|
||||
|
||||
$form['future_format'] = [
|
||||
'#type' => 'textfield',
|
||||
'#title' => $this->t('Future format'),
|
||||
'#default_value' => $this->getSetting('future_format'),
|
||||
'#description' => $this->t('Use <em>@interval</em> where you want the formatted interval text to appear.'),
|
||||
];
|
||||
|
||||
$form['past_format'] = [
|
||||
'#type' => 'textfield',
|
||||
'#title' => $this->t('Past format'),
|
||||
'#default_value' => $this->getSetting('past_format'),
|
||||
'#description' => $this->t('Use <em>@interval</em> where you want the formatted interval text to appear.'),
|
||||
];
|
||||
|
||||
$form['granularity'] = [
|
||||
'#type' => 'number',
|
||||
'#title' => $this->t('Granularity'),
|
||||
'#default_value' => $this->getSetting('granularity'),
|
||||
'#description' => $this->t('How many time units should be shown in the formatted output.'),
|
||||
];
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function settingsSummary() {
|
||||
$summary = parent::settingsSummary();
|
||||
|
||||
$future_date = new DrupalDateTime('1 year 1 month 1 week 1 day 1 hour 1 minute');
|
||||
$past_date = new DrupalDateTime('-1 year -1 month -1 week -1 day -1 hour -1 minute');
|
||||
$summary[] = t('Future date: %display', ['%display' => $this->formatDate($future_date)]);
|
||||
$summary[] = t('Past date: %display', ['%display' => $this->formatDate($past_date)]);
|
||||
|
||||
return $summary;
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a date/time as a time interval.
|
||||
*
|
||||
|
@ -176,27 +47,7 @@ class DateTimeTimeAgoFormatter extends FormatterBase implements ContainerFactory
|
|||
* The formatted date/time string using the past or future format setting.
|
||||
*/
|
||||
protected function formatDate(DrupalDateTime $date) {
|
||||
$granularity = $this->getSetting('granularity');
|
||||
$timestamp = $date->getTimestamp();
|
||||
$options = [
|
||||
'granularity' => $granularity,
|
||||
'return_as_object' => TRUE,
|
||||
];
|
||||
|
||||
if ($this->request->server->get('REQUEST_TIME') > $timestamp) {
|
||||
$result = $this->dateFormatter->formatTimeDiffSince($timestamp, $options);
|
||||
$build = [
|
||||
'#markup' => SafeMarkup::format($this->getSetting('past_format'), ['@interval' => $result->getString()]),
|
||||
];
|
||||
}
|
||||
else {
|
||||
$result = $this->dateFormatter->formatTimeDiffUntil($timestamp, $options);
|
||||
$build = [
|
||||
'#markup' => SafeMarkup::format($this->getSetting('future_format'), ['@interval' => $result->getString()]),
|
||||
];
|
||||
}
|
||||
CacheableMetadata::createFromObject($result)->applyTo($build);
|
||||
return $build;
|
||||
return parent::formatTimestamp($date->getTimestamp());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -51,9 +51,9 @@ class DateTimeFieldItemList extends FieldItemList {
|
|||
'#states' => [
|
||||
'visible' => [
|
||||
':input[id="edit-default-value-input-default-date-type"]' => ['value' => static::DEFAULT_VALUE_CUSTOM],
|
||||
]
|
||||
]
|
||||
]
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
return $element;
|
||||
|
@ -96,13 +96,13 @@ class DateTimeFieldItemList extends FieldItemList {
|
|||
// A default date only value should be in the format used for date
|
||||
// storage but in the user's local timezone.
|
||||
$date = new DrupalDateTime($default_value[0]['default_date'], drupal_get_user_timezone());
|
||||
$format = DATETIME_DATE_STORAGE_FORMAT;
|
||||
$format = DateTimeItemInterface::DATE_STORAGE_FORMAT;
|
||||
}
|
||||
else {
|
||||
// A default date+time value should be in the format and timezone used
|
||||
// for date storage.
|
||||
$date = new DrupalDateTime($default_value[0]['default_date'], DATETIME_STORAGE_TIMEZONE);
|
||||
$format = DATETIME_DATETIME_STORAGE_FORMAT;
|
||||
$date = new DrupalDateTime($default_value[0]['default_date'], DateTimeItemInterface::STORAGE_TIMEZONE);
|
||||
$format = DateTimeItemInterface::DATETIME_STORAGE_FORMAT;
|
||||
}
|
||||
$value = $date->format($format);
|
||||
// We only provide a default value for the first item, as do all fields.
|
||||
|
@ -112,7 +112,7 @@ class DateTimeFieldItemList extends FieldItemList {
|
|||
[
|
||||
'value' => $value,
|
||||
'date' => $date,
|
||||
]
|
||||
],
|
||||
];
|
||||
}
|
||||
return $default_value;
|
||||
|
|
|
@ -17,10 +17,11 @@ use Drupal\Core\Field\FieldItemBase;
|
|||
* description = @Translation("Create and store date values."),
|
||||
* default_widget = "datetime_default",
|
||||
* default_formatter = "datetime_default",
|
||||
* list_class = "\Drupal\datetime\Plugin\Field\FieldType\DateTimeFieldItemList"
|
||||
* list_class = "\Drupal\datetime\Plugin\Field\FieldType\DateTimeFieldItemList",
|
||||
* constraints = {"DateTimeFormat" = {}}
|
||||
* )
|
||||
*/
|
||||
class DateTimeItem extends FieldItemBase {
|
||||
class DateTimeItem extends FieldItemBase implements DateTimeItemInterface {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
|
@ -108,10 +109,10 @@ class DateTimeItem extends FieldItemBase {
|
|||
// type.
|
||||
$timestamp = REQUEST_TIME - mt_rand(0, 86400 * 365);
|
||||
if ($type == DateTimeItem::DATETIME_TYPE_DATE) {
|
||||
$values['value'] = gmdate(DATETIME_DATE_STORAGE_FORMAT, $timestamp);
|
||||
$values['value'] = gmdate(static::DATE_STORAGE_FORMAT, $timestamp);
|
||||
}
|
||||
else {
|
||||
$values['value'] = gmdate(DATETIME_DATETIME_STORAGE_FORMAT, $timestamp);
|
||||
$values['value'] = gmdate(static::DATETIME_STORAGE_FORMAT, $timestamp);
|
||||
}
|
||||
return $values;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\datetime\Plugin\Field\FieldType;
|
||||
|
||||
/**
|
||||
* Interface definition for Datetime items.
|
||||
*/
|
||||
interface DateTimeItemInterface {
|
||||
|
||||
/**
|
||||
* Defines the timezone that dates should be stored in.
|
||||
*/
|
||||
const STORAGE_TIMEZONE = 'UTC';
|
||||
|
||||
/**
|
||||
* Defines the format that date and time should be stored in.
|
||||
*/
|
||||
const DATETIME_STORAGE_FORMAT = 'Y-m-d\TH:i:s';
|
||||
|
||||
/**
|
||||
* Defines the format that dates should be stored in.
|
||||
*/
|
||||
const DATE_STORAGE_FORMAT = 'Y-m-d';
|
||||
|
||||
}
|
|
@ -7,6 +7,7 @@ use Drupal\Core\Field\FieldItemListInterface;
|
|||
use Drupal\Core\Field\WidgetBase;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
|
||||
|
||||
/**
|
||||
* Base class for the 'datetime_*' widgets.
|
||||
|
@ -28,20 +29,15 @@ class DateTimeWidgetBase extends WidgetBase {
|
|||
if ($this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE) {
|
||||
// A date-only field should have no timezone conversion performed, so
|
||||
// use the same timezone as for storage.
|
||||
$element['value']['#date_timezone'] = DATETIME_STORAGE_TIMEZONE;
|
||||
$element['value']['#date_timezone'] = DateTimeItemInterface::STORAGE_TIMEZONE;
|
||||
}
|
||||
|
||||
if ($items[$delta]->date) {
|
||||
$date = $items[$delta]->date;
|
||||
// The date was created and verified during field_load(), so it is safe to
|
||||
// use without further inspection.
|
||||
if ($this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE) {
|
||||
// A date without time will pick up the current time, use the default
|
||||
// time.
|
||||
datetime_date_default_time($date);
|
||||
}
|
||||
$date->setTimezone(new \DateTimeZone($element['value']['#date_timezone']));
|
||||
$element['value']['#default_value'] = $date;
|
||||
$element['value']['#default_value'] = $this->createDefaultValue($date, $element['value']['#date_timezone']);
|
||||
}
|
||||
|
||||
return $element;
|
||||
|
@ -59,22 +55,43 @@ class DateTimeWidgetBase extends WidgetBase {
|
|||
$date = $item['value'];
|
||||
switch ($this->getFieldSetting('datetime_type')) {
|
||||
case DateTimeItem::DATETIME_TYPE_DATE:
|
||||
// If this is a date-only field, set it to the default time so the
|
||||
// timezone conversion can be reversed.
|
||||
datetime_date_default_time($date);
|
||||
$format = DATETIME_DATE_STORAGE_FORMAT;
|
||||
$format = DateTimeItemInterface::DATE_STORAGE_FORMAT;
|
||||
break;
|
||||
|
||||
default:
|
||||
$format = DATETIME_DATETIME_STORAGE_FORMAT;
|
||||
$format = DateTimeItemInterface::DATETIME_STORAGE_FORMAT;
|
||||
break;
|
||||
}
|
||||
// Adjust the date for storage.
|
||||
$date->setTimezone(new \DateTimezone(DATETIME_STORAGE_TIMEZONE));
|
||||
$date->setTimezone(new \DateTimezone(DateTimeItemInterface::STORAGE_TIMEZONE));
|
||||
$item['value'] = $date->format($format);
|
||||
}
|
||||
}
|
||||
return $values;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a date object for use as a default value.
|
||||
*
|
||||
* This will take a default value, apply the proper timezone for display in
|
||||
* a widget, and set the default time for date-only fields.
|
||||
*
|
||||
* @param \Drupal\Core\Datetime\DrupalDateTime $date
|
||||
* The UTC default date.
|
||||
* @param string $timezone
|
||||
* The timezone to apply.
|
||||
*
|
||||
* @return \Drupal\Core\Datetime\DrupalDateTime
|
||||
* A date object for use as a default value in a field widget.
|
||||
*/
|
||||
protected function createDefaultValue($date, $timezone) {
|
||||
// The date was created and verified during field_load(), so it is safe to
|
||||
// use without further inspection.
|
||||
if ($this->getFieldSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
|
||||
$date->setDefaultDateTime();
|
||||
}
|
||||
$date->setTimezone(new \DateTimeZone($timezone));
|
||||
return $date;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\datetime\Plugin\Validation\Constraint;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
|
||||
/**
|
||||
* Validation constraint for DateTime items to ensure the format is correct.
|
||||
*
|
||||
* @Constraint(
|
||||
* id = "DateTimeFormat",
|
||||
* label = @Translation("Datetime format valid for datetime type.", context = "Validation"),
|
||||
* )
|
||||
*/
|
||||
class DateTimeFormatConstraint extends Constraint {
|
||||
|
||||
/**
|
||||
* Message for when the value isn't a string.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $badType = "The datetime value must be a string.";
|
||||
|
||||
/**
|
||||
* Message for when the value isn't in the proper format.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $badFormat = "The datetime value '@value' is invalid for the format '@format'";
|
||||
|
||||
/**
|
||||
* Message for when the value did not parse properly.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $badValue = "The datetime value '@value' did not parse properly for the format '@format'";
|
||||
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\datetime\Plugin\Validation\Constraint;
|
||||
|
||||
use Drupal\Component\Datetime\DateTimePlus;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
|
||||
/**
|
||||
* Constraint validator for DateTime items to ensure the format is correct.
|
||||
*/
|
||||
class DateTimeFormatConstraintValidator extends ConstraintValidator {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($item, Constraint $constraint) {
|
||||
/* @var $item \Drupal\datetime\Plugin\Field\FieldType\DateTimeItem */
|
||||
if (isset($item)) {
|
||||
$value = $item->getValue()['value'];
|
||||
if (!is_string($value)) {
|
||||
$this->context->addViolation($constraint->badType);
|
||||
}
|
||||
else {
|
||||
$datetime_type = $item->getFieldDefinition()->getSetting('datetime_type');
|
||||
$format = $datetime_type === DateTimeItem::DATETIME_TYPE_DATE ? DateTimeItemInterface::DATE_STORAGE_FORMAT : DateTimeItemInterface::DATETIME_STORAGE_FORMAT;
|
||||
$date = NULL;
|
||||
try {
|
||||
$date = DateTimePlus::createFromFormat($format, $value, new \DateTimeZone(DateTimeItemInterface::STORAGE_TIMEZONE));
|
||||
}
|
||||
catch (\InvalidArgumentException $e) {
|
||||
$this->context->addViolation($constraint->badFormat, [
|
||||
'@value' => $value,
|
||||
'@format' => $format,
|
||||
]);
|
||||
return;
|
||||
}
|
||||
catch (\UnexpectedValueException $e) {
|
||||
$this->context->addViolation($constraint->badValue, [
|
||||
'@value' => $value,
|
||||
'@format' => $format,
|
||||
]);
|
||||
return;
|
||||
}
|
||||
if ($date === NULL || $date->hasErrors()) {
|
||||
$this->context->addViolation($constraint->badFormat, [
|
||||
'@value' => $value,
|
||||
'@format' => $format,
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,81 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\datetime\Plugin\migrate\field;
|
||||
|
||||
use Drupal\migrate\Plugin\MigrationInterface;
|
||||
use Drupal\migrate\MigrateException;
|
||||
use Drupal\migrate_drupal\Plugin\migrate\field\FieldPluginBase;
|
||||
|
||||
/**
|
||||
* @MigrateField(
|
||||
* id = "datetime",
|
||||
* type_map = {
|
||||
* "date" = "datetime",
|
||||
* "datestamp" = "timestamp",
|
||||
* "datetime" = "datetime",
|
||||
* },
|
||||
* core = {6,7},
|
||||
* source_module = "date",
|
||||
* destination_module = "datetime"
|
||||
* )
|
||||
*/
|
||||
class DateField extends FieldPluginBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFieldFormatterMap() {
|
||||
return [
|
||||
'date_default' => 'datetime_default',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFieldWidgetMap() {
|
||||
return [
|
||||
'date' => 'datetime_default',
|
||||
'datetime' => 'datetime_default',
|
||||
'datestamp' => 'datetime_timestamp',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function defineValueProcessPipeline(MigrationInterface $migration, $field_name, $data) {
|
||||
switch ($data['type']) {
|
||||
case 'date':
|
||||
$from_format = 'Y-m-d\TH:i:s';
|
||||
$to_format = 'Y-m-d\TH:i:s';
|
||||
break;
|
||||
case 'datestamp':
|
||||
$from_format = 'U';
|
||||
$to_format = 'U';
|
||||
break;
|
||||
case 'datetime':
|
||||
$from_format = 'Y-m-d H:i:s';
|
||||
$to_format = 'Y-m-d\TH:i:s';
|
||||
break;
|
||||
default:
|
||||
throw new MigrateException(sprintf('Field %s of type %s is an unknown date field type.', $field_name, var_export($data['type'], TRUE)));
|
||||
}
|
||||
$process = [
|
||||
'value' => [
|
||||
'plugin' => 'format_date',
|
||||
'from_format' => $from_format,
|
||||
'to_format' => $to_format,
|
||||
'source' => 'value',
|
||||
],
|
||||
];
|
||||
|
||||
$process = [
|
||||
'plugin' => 'sub_process',
|
||||
'source' => $field_name,
|
||||
'process' => $process,
|
||||
];
|
||||
$migration->mergeProcessOfProperty($field_name, $process);
|
||||
}
|
||||
|
||||
}
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
namespace Drupal\datetime\Plugin\migrate\field\d6;
|
||||
|
||||
@trigger_error('DateField is deprecated in Drupal 8.4.x and will be removed before Drupal 9.0.x. Use \Drupal\datetime\Plugin\migrate\field\DateField instead.', E_USER_DEPRECATED);
|
||||
|
||||
use Drupal\migrate\Plugin\MigrationInterface;
|
||||
use Drupal\migrate\MigrateException;
|
||||
use Drupal\migrate_drupal\Plugin\migrate\field\FieldPluginBase;
|
||||
|
@ -14,8 +16,13 @@ use Drupal\migrate_drupal\Plugin\migrate\field\FieldPluginBase;
|
|||
* "datestamp" = "timestamp",
|
||||
* "datetime" = "datetime",
|
||||
* },
|
||||
* core = {6}
|
||||
* core = {6},
|
||||
* source_module = "date",
|
||||
* destination_module = "datetime"
|
||||
* )
|
||||
*
|
||||
* @deprecated in Drupal 8.4.x, to be removed before Drupal 9.0.x. Use
|
||||
* \Drupal\datetime\Plugin\migrate\field\DateField instead.
|
||||
*/
|
||||
class DateField extends FieldPluginBase {
|
||||
|
||||
|
@ -33,16 +40,7 @@ class DateField extends FieldPluginBase {
|
|||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFieldFormatterMap() {
|
||||
// See d6_field_formatter_settings.yml and
|
||||
// FieldPluginBase::processFieldFormatter().
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function processFieldValues(MigrationInterface $migration, $field_name, $data) {
|
||||
public function defineValueProcessPipeline(MigrationInterface $migration, $field_name, $data) {
|
||||
switch ($data['type']) {
|
||||
case 'date':
|
||||
$from_format = 'Y-m-d\TH:i:s';
|
||||
|
@ -69,7 +67,7 @@ class DateField extends FieldPluginBase {
|
|||
];
|
||||
|
||||
$process = [
|
||||
'plugin' => 'iterator',
|
||||
'plugin' => 'sub_process',
|
||||
'source' => $field_name,
|
||||
'process' => $process,
|
||||
];
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\datetime\Plugin\views\Argument;
|
||||
namespace Drupal\datetime\Plugin\views\argument;
|
||||
|
||||
use Drupal\Core\Routing\RouteMatchInterface;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
|
||||
use Drupal\views\FieldAPIHandlerTrait;
|
||||
use Drupal\views\Plugin\views\argument\Date as NumericDate;
|
||||
|
||||
/**
|
||||
|
@ -22,12 +25,36 @@ use Drupal\views\Plugin\views\argument\Date as NumericDate;
|
|||
*/
|
||||
class Date extends NumericDate {
|
||||
|
||||
use FieldAPIHandlerTrait;
|
||||
|
||||
/**
|
||||
* Determines if the timezone offset is calculated.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $calculateOffset = TRUE;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match) {
|
||||
parent::__construct($configuration, $plugin_id, $plugin_definition, $route_match);
|
||||
|
||||
$definition = $this->getFieldStorageDefinition();
|
||||
if ($definition->getSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
|
||||
// Timezone offset calculation is not applicable to dates that are stored
|
||||
// as date-only.
|
||||
$this->calculateOffset = FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDateField() {
|
||||
// Return the real field, since it is already in string format.
|
||||
return "$this->tableAlias.$this->realField";
|
||||
// Use string date storage/formatting since datetime fields are stored as
|
||||
// strings rather than UNIX timestamps.
|
||||
return $this->query->getDateField("$this->tableAlias.$this->realField", TRUE, $this->calculateOffset);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -2,9 +2,11 @@
|
|||
|
||||
namespace Drupal\datetime\Plugin\views\filter;
|
||||
|
||||
use Drupal\Component\Datetime\DateTimePlus;
|
||||
use Drupal\Core\Datetime\DateFormatterInterface;
|
||||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
|
||||
use Drupal\views\FieldAPIHandlerTrait;
|
||||
use Drupal\views\Plugin\views\filter\Date as NumericDate;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
@ -38,10 +40,17 @@ class Date extends NumericDate implements ContainerFactoryPluginInterface {
|
|||
*
|
||||
* @see \Drupal\views\Plugin\views\query\Sql::getDateFormat()
|
||||
*/
|
||||
protected $dateFormat = DATETIME_DATETIME_STORAGE_FORMAT;
|
||||
protected $dateFormat = DateTimeItemInterface::DATETIME_STORAGE_FORMAT;
|
||||
|
||||
/**
|
||||
* The request stack used to determin current time.
|
||||
* Determines if the timezone offset is calculated.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $calculateOffset = TRUE;
|
||||
|
||||
/**
|
||||
* The request stack used to determine current time.
|
||||
*
|
||||
* @var \Symfony\Component\HttpFoundation\RequestStack
|
||||
*/
|
||||
|
@ -66,10 +75,13 @@ class Date extends NumericDate implements ContainerFactoryPluginInterface {
|
|||
$this->dateFormatter = $date_formatter;
|
||||
$this->requestStack = $request_stack;
|
||||
|
||||
// Date format depends on field storage format.
|
||||
$definition = $this->getFieldStorageDefinition();
|
||||
if ($definition->getSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
|
||||
$this->dateFormat = DATETIME_DATE_STORAGE_FORMAT;
|
||||
// Date format depends on field storage format.
|
||||
$this->dateFormat = DateTimeItemInterface::DATE_STORAGE_FORMAT;
|
||||
// Timezone offset calculation is not applicable to dates that are stored
|
||||
// as date-only.
|
||||
$this->calculateOffset = FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -90,21 +102,23 @@ class Date extends NumericDate implements ContainerFactoryPluginInterface {
|
|||
* Override parent method, which deals with dates as integers.
|
||||
*/
|
||||
protected function opBetween($field) {
|
||||
$origin = ($this->value['type'] == 'offset') ? $this->requestStack->getCurrentRequest()->server->get('REQUEST_TIME') : 0;
|
||||
$a = intval(strtotime($this->value['min'], $origin));
|
||||
$b = intval(strtotime($this->value['max'], $origin));
|
||||
|
||||
// Formatting will vary on date storage.
|
||||
$timezone = $this->getTimezone();
|
||||
$origin_offset = $this->getOffset($this->value['min'], $timezone);
|
||||
|
||||
// Although both 'min' and 'max' values are required, default empty 'min'
|
||||
// value as UNIX timestamp 0.
|
||||
$min = (!empty($this->value['min'])) ? $this->value['min'] : '@0';
|
||||
|
||||
// Convert to ISO format and format for query. UTC timezone is used since
|
||||
// dates are stored in UTC.
|
||||
$a = $this->query->getDateFormat("'" . $this->dateFormatter->format($a, 'custom', DATETIME_DATETIME_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE) . "'", $this->dateFormat, TRUE);
|
||||
$b = $this->query->getDateFormat("'" . $this->dateFormatter->format($b, 'custom', DATETIME_DATETIME_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE) . "'", $this->dateFormat, TRUE);
|
||||
$a = new DateTimePlus($min, new \DateTimeZone($timezone));
|
||||
$a = $this->query->getDateFormat($this->query->getDateField("'" . $this->dateFormatter->format($a->getTimestamp() + $origin_offset, 'custom', DateTimeItemInterface::DATETIME_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE) . "'", TRUE, $this->calculateOffset), $this->dateFormat, TRUE);
|
||||
$b = new DateTimePlus($this->value['max'], new \DateTimeZone($timezone));
|
||||
$b = $this->query->getDateFormat($this->query->getDateField("'" . $this->dateFormatter->format($b->getTimestamp() + $origin_offset, 'custom', DateTimeItemInterface::DATETIME_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE) . "'", TRUE, $this->calculateOffset), $this->dateFormat, TRUE);
|
||||
|
||||
// This is safe because we are manually scrubbing the values.
|
||||
$operator = strtoupper($this->operator);
|
||||
$field = $this->query->getDateFormat($field, $this->dateFormat, TRUE);
|
||||
$field = $this->query->getDateFormat($this->query->getDateField($field, TRUE, $this->calculateOffset), $this->dateFormat, TRUE);
|
||||
$this->query->addWhereExpression($this->options['group'], "$field $operator $a AND $b");
|
||||
}
|
||||
|
||||
|
@ -112,15 +126,57 @@ class Date extends NumericDate implements ContainerFactoryPluginInterface {
|
|||
* Override parent method, which deals with dates as integers.
|
||||
*/
|
||||
protected function opSimple($field) {
|
||||
$origin = (!empty($this->value['type']) && $this->value['type'] == 'offset') ? $this->requestStack->getCurrentRequest()->server->get('REQUEST_TIME') : 0;
|
||||
$value = intval(strtotime($this->value['value'], $origin));
|
||||
$timezone = $this->getTimezone();
|
||||
$origin_offset = $this->getOffset($this->value['value'], $timezone);
|
||||
|
||||
// Convert to ISO. UTC is used since dates are stored in UTC.
|
||||
$value = $this->query->getDateFormat("'" . $this->dateFormatter->format($value, 'custom', DATETIME_DATETIME_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE) . "'", $this->dateFormat, TRUE);
|
||||
// Convert to ISO. UTC timezone is used since dates are stored in UTC.
|
||||
$value = new DateTimePlus($this->value['value'], new \DateTimeZone($timezone));
|
||||
$value = $this->query->getDateFormat($this->query->getDateField("'" . $this->dateFormatter->format($value->getTimestamp() + $origin_offset, 'custom', DateTimeItemInterface::DATETIME_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE) . "'", TRUE, $this->calculateOffset), $this->dateFormat, TRUE);
|
||||
|
||||
// This is safe because we are manually scrubbing the value.
|
||||
$field = $this->query->getDateFormat($field, $this->dateFormat, TRUE);
|
||||
$field = $this->query->getDateFormat($this->query->getDateField($field, TRUE, $this->calculateOffset), $this->dateFormat, TRUE);
|
||||
$this->query->addWhereExpression($this->options['group'], "$field $this->operator $value");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the proper time zone to use in computations.
|
||||
*
|
||||
* Date-only fields do not have a time zone associated with them, so the
|
||||
* filter input needs to use UTC for reference. Otherwise, use the time zone
|
||||
* for the current user.
|
||||
*
|
||||
* @return string
|
||||
* The time zone name.
|
||||
*/
|
||||
protected function getTimezone() {
|
||||
return $this->dateFormat === DateTimeItemInterface::DATE_STORAGE_FORMAT
|
||||
? DateTimeItemInterface::STORAGE_TIMEZONE
|
||||
: drupal_get_user_timezone();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the proper offset from UTC to use in computations.
|
||||
*
|
||||
* @param string $time
|
||||
* A date/time string compatible with \DateTime. It is used as the
|
||||
* reference for computing the offset, which can vary based on the time
|
||||
* zone rules.
|
||||
* @param string $timezone
|
||||
* The time zone that $time is in.
|
||||
*
|
||||
* @return int
|
||||
* The computed offset in seconds.
|
||||
*/
|
||||
protected function getOffset($time, $timezone) {
|
||||
// Date-only fields do not have a time zone or offset from UTC associated
|
||||
// with them. For relative (i.e. 'offset') comparisons, we need to compute
|
||||
// the user's offset from UTC for use in the query.
|
||||
$origin_offset = 0;
|
||||
if ($this->dateFormat === DateTimeItemInterface::DATE_STORAGE_FORMAT && $this->value['type'] === 'offset') {
|
||||
$origin_offset = $origin_offset + timezone_offset_get(new \DateTimeZone(drupal_get_user_timezone()), new \DateTime($time, new \DateTimeZone($timezone)));
|
||||
}
|
||||
|
||||
return $origin_offset;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
namespace Drupal\datetime\Plugin\views\sort;
|
||||
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
|
||||
use Drupal\views\FieldAPIHandlerTrait;
|
||||
use Drupal\views\Plugin\views\sort\Date as NumericDate;
|
||||
|
||||
/**
|
||||
|
@ -14,12 +16,38 @@ use Drupal\views\Plugin\views\sort\Date as NumericDate;
|
|||
*/
|
||||
class Date extends NumericDate {
|
||||
|
||||
use FieldAPIHandlerTrait;
|
||||
|
||||
/**
|
||||
* Determines if the timezone offset is calculated.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $calculateOffset = TRUE;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
|
||||
parent::__construct($configuration, $plugin_id, $plugin_definition);
|
||||
|
||||
$definition = $this->getFieldStorageDefinition();
|
||||
if ($definition->getSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
|
||||
// Timezone offset calculation is not applicable to dates that are stored
|
||||
// as date-only.
|
||||
$this->calculateOffset = FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* Override to account for dates stored as strings.
|
||||
*/
|
||||
public function getDateField() {
|
||||
// Return the real field, since it is already in string format.
|
||||
return "$this->tableAlias.$this->realField";
|
||||
// Use string date storage/formatting since datetime fields are stored as
|
||||
// strings rather than UNIX timestamps.
|
||||
return $this->query->getDateField("$this->tableAlias.$this->realField", TRUE, $this->calculateOffset);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -4,7 +4,6 @@ namespace Drupal\datetime\Tests;
|
|||
|
||||
@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);
|
||||
|
||||
use Drupal\Component\Utility\Unicode;
|
||||
use Drupal\Core\Entity\Entity\EntityFormDisplay;
|
||||
use Drupal\Core\Entity\Entity\EntityViewDisplay;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
|
||||
|
@ -111,7 +110,7 @@ abstract class DateTestBase extends WebTestBase {
|
|||
* Creates a date test field.
|
||||
*/
|
||||
protected function createField() {
|
||||
$field_name = Unicode::strtolower($this->randomMachineName());
|
||||
$field_name = mb_strtolower($this->randomMachineName());
|
||||
$type = $this->getTestFieldType();
|
||||
$widget_type = $formatter_type = $type . '_default';
|
||||
|
||||
|
@ -185,4 +184,20 @@ abstract class DateTestBase extends WebTestBase {
|
|||
->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Massages test date values.
|
||||
*
|
||||
* If a date object is generated directly by a test, then it needs to be
|
||||
* adjusted to behave like the computed date from the item.
|
||||
*
|
||||
* @param \Drupal\Core\Datetime\DrupalDateTime $date
|
||||
* A date object directly generated by the test.
|
||||
*/
|
||||
protected function massageTestDate($date) {
|
||||
if ($this->field->getSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
|
||||
// Set the default time for date-only items.
|
||||
$date->setDefaultDateTime();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
namespace Drupal\datetime\Tests\Views;
|
||||
|
||||
@trigger_error('\Drupal\datetime\Tests\Views\DateTimeHandlerTestBase is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Instead, use \Drupal\Tests\BrowserTestBase', E_USER_DEPRECATED);
|
||||
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\node\Entity\NodeType;
|
||||
|
@ -11,6 +13,9 @@ use Drupal\field\Entity\FieldStorageConfig;
|
|||
|
||||
/**
|
||||
* Base class for testing datetime handlers.
|
||||
*
|
||||
* @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0.
|
||||
* Use \Drupal\Tests\BrowserTestBase.
|
||||
*/
|
||||
abstract class DateTimeHandlerTestBase extends HandlerTestBase {
|
||||
|
||||
|
@ -38,13 +43,13 @@ abstract class DateTimeHandlerTestBase extends HandlerTestBase {
|
|||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
protected function setUp($import_test_views = TRUE) {
|
||||
parent::setUp($import_test_views);
|
||||
|
||||
// Add a date field to page nodes.
|
||||
$node_type = NodeType::create([
|
||||
'type' => 'page',
|
||||
'name' => 'page'
|
||||
'name' => 'page',
|
||||
]);
|
||||
$node_type->save();
|
||||
$fieldStorage = FieldStorageConfig::create([
|
||||
|
|
|
@ -1,110 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\datetime\Tests\Views;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
use Drupal\views\Views;
|
||||
|
||||
/**
|
||||
* Tests date-only fields.
|
||||
*
|
||||
* @group datetime
|
||||
*/
|
||||
class FilterDateTest extends DateTimeHandlerTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $testViews = ['test_filter_datetime'];
|
||||
|
||||
/**
|
||||
* For offset tests, set to the current time.
|
||||
*/
|
||||
protected static $date;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* Create nodes with relative dates of yesterday, today, and tomorrow.
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Set to 'today'.
|
||||
static::$date = REQUEST_TIME;
|
||||
|
||||
// Change field storage to date-only.
|
||||
$storage = FieldStorageConfig::load('node.' . static::$field_name);
|
||||
$storage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
|
||||
$storage->save();
|
||||
|
||||
$dates = [
|
||||
// Tomorrow.
|
||||
\Drupal::service('date.formatter')->format(static::$date + 86400, 'custom', DATETIME_DATE_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE),
|
||||
// Today.
|
||||
\Drupal::service('date.formatter')->format(static::$date, 'custom', DATETIME_DATE_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE),
|
||||
// Yesterday.
|
||||
\Drupal::service('date.formatter')->format(static::$date - 86400, 'custom', DATETIME_DATE_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE),
|
||||
];
|
||||
|
||||
foreach ($dates as $date) {
|
||||
$this->nodes[] = $this->drupalCreateNode([
|
||||
'field_date' => [
|
||||
'value' => $date,
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test offsets with date-only fields.
|
||||
*/
|
||||
public function testDateOffsets() {
|
||||
$view = Views::getView('test_filter_datetime');
|
||||
$field = static::$field_name . '_value';
|
||||
|
||||
// Test simple operations.
|
||||
$view->initHandlers();
|
||||
|
||||
// A greater than or equal to 'now', should return the 'today' and
|
||||
// the 'tomorrow' node.
|
||||
$view->filter[$field]->operator = '>=';
|
||||
$view->filter[$field]->value['type'] = 'offset';
|
||||
$view->filter[$field]->value['value'] = 'now';
|
||||
$view->setDisplay('default');
|
||||
$this->executeView($view);
|
||||
$expected_result = [
|
||||
['nid' => $this->nodes[0]->id()],
|
||||
['nid' => $this->nodes[1]->id()],
|
||||
];
|
||||
$this->assertIdenticalResultset($view, $expected_result, $this->map);
|
||||
$view->destroy();
|
||||
|
||||
// Only dates in the past.
|
||||
$view->initHandlers();
|
||||
$view->filter[$field]->operator = '<';
|
||||
$view->filter[$field]->value['type'] = 'offset';
|
||||
$view->filter[$field]->value['value'] = 'now';
|
||||
$view->setDisplay('default');
|
||||
$this->executeView($view);
|
||||
$expected_result = [
|
||||
['nid' => $this->nodes[2]->id()],
|
||||
];
|
||||
$this->assertIdenticalResultset($view, $expected_result, $this->map);
|
||||
$view->destroy();
|
||||
|
||||
// Test offset for between operator. Only the 'tomorrow' node should appear.
|
||||
$view->initHandlers();
|
||||
$view->filter[$field]->operator = 'between';
|
||||
$view->filter[$field]->value['type'] = 'offset';
|
||||
$view->filter[$field]->value['max'] = '+2 days';
|
||||
$view->filter[$field]->value['min'] = '+1 day';
|
||||
$view->setDisplay('default');
|
||||
$this->executeView($view);
|
||||
$expected_result = [
|
||||
['nid' => $this->nodes[0]->id()],
|
||||
];
|
||||
$this->assertIdenticalResultset($view, $expected_result, $this->map);
|
||||
}
|
||||
|
||||
}
|
|
@ -5,4 +5,4 @@ package: Testing
|
|||
version: VERSION
|
||||
core: 8.x
|
||||
dependencies:
|
||||
- views
|
||||
- drupal:views
|
||||
|
|
|
@ -33,6 +33,7 @@ display:
|
|||
relationship: none
|
||||
table: node__field_date
|
||||
order: DESC
|
||||
granularity: second
|
||||
plugin_id: datetime
|
||||
id:
|
||||
field: nid
|
||||
|
|
|
@ -106,7 +106,8 @@ abstract class DateTestBase extends BrowserTestBase {
|
|||
* Creates a date test field.
|
||||
*/
|
||||
protected function createField() {
|
||||
$field_name = Unicode::strtolower($this->randomMachineName());
|
||||
$field_name = mb_strtolower($this->randomMachineName());
|
||||
$field_label = Unicode::ucfirst(mb_strtolower($this->randomMachineName()));
|
||||
$type = $this->getTestFieldType();
|
||||
$widget_type = $formatter_type = $type . '_default';
|
||||
|
||||
|
@ -119,8 +120,9 @@ abstract class DateTestBase extends BrowserTestBase {
|
|||
$this->fieldStorage->save();
|
||||
$this->field = FieldConfig::create([
|
||||
'field_storage' => $this->fieldStorage,
|
||||
'label' => $field_label,
|
||||
'bundle' => 'entity_test',
|
||||
'description' => 'Description for ' . $field_name,
|
||||
'description' => 'Description for ' . $field_label,
|
||||
'required' => TRUE,
|
||||
]);
|
||||
$this->field->save();
|
||||
|
@ -181,4 +183,20 @@ abstract class DateTestBase extends BrowserTestBase {
|
|||
->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Massages test date values.
|
||||
*
|
||||
* If a date object is generated directly by a test, then it needs to be
|
||||
* adjusted to behave like the computed date from the item.
|
||||
*
|
||||
* @param \Drupal\Core\Datetime\DrupalDateTime $date
|
||||
* A date object directly generated by the test.
|
||||
*/
|
||||
protected function massageTestDate($date) {
|
||||
if ($this->field->getSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
|
||||
// Set the default time for date-only items.
|
||||
$date->setDefaultDateTime();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -3,10 +3,9 @@
|
|||
namespace Drupal\Tests\datetime\Functional;
|
||||
|
||||
use Drupal\Component\Render\FormattableMarkup;
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\Component\Utility\Unicode;
|
||||
use Drupal\Core\Datetime\DrupalDateTime;
|
||||
use Drupal\Core\Datetime\Entity\DateFormat;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
|
||||
use Drupal\entity_test\Entity\EntityTest;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
|
@ -44,6 +43,7 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
foreach (static::$timezones as $timezone) {
|
||||
|
||||
$this->setSiteTimezone($timezone);
|
||||
$this->assertEquals($timezone, $this->config('system.date')->get('timezone.default'), 'Time zone set to ' . $timezone);
|
||||
|
||||
// Display creation form.
|
||||
$this->drupalGet('entity_test/add');
|
||||
|
@ -57,7 +57,7 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
// mimic the user in a different timezone simply entering '2012-12-31' via
|
||||
// the UI.
|
||||
$value = '2012-12-31 00:00:00';
|
||||
$date = new DrupalDateTime($value, DATETIME_STORAGE_TIMEZONE);
|
||||
$date = new DrupalDateTime($value, DateTimeItemInterface::STORAGE_TIMEZONE);
|
||||
|
||||
// Submit a valid date and ensure it is accepted.
|
||||
$date_format = DateFormat::load('html_date')->getPattern();
|
||||
|
@ -99,7 +99,7 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
// Formats that display a time component for date-only fields will display
|
||||
// the default time, so that is applied before calculating the expected
|
||||
// value.
|
||||
datetime_date_default_time($date);
|
||||
$this->massageTestDate($date);
|
||||
foreach ($options as $setting => $values) {
|
||||
foreach ($values as $new_value) {
|
||||
// Update the entity display settings.
|
||||
|
@ -113,11 +113,16 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
case 'format_type':
|
||||
// Verify that a date is displayed. Since this is a date-only
|
||||
// field, it is expected to display the time as 00:00:00.
|
||||
$expected = format_date($date->getTimestamp(), $new_value, '', DATETIME_STORAGE_TIMEZONE);
|
||||
$expected_iso = format_date($date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', DATETIME_STORAGE_TIMEZONE);
|
||||
$expected = format_date($date->getTimestamp(), $new_value, '', DateTimeItemInterface::STORAGE_TIMEZONE);
|
||||
$expected_iso = format_date($date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', DateTimeItemInterface::STORAGE_TIMEZONE);
|
||||
$output = $this->renderTestEntity($id);
|
||||
$expected_markup = '<time datetime="' . $expected_iso . '" class="datetime">' . $expected . '</time>';
|
||||
$this->assertContains($expected_markup, $output, SafeMarkup::format('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', ['%value' => $new_value, '%expected' => $expected, '%expected_iso' => $expected_iso]));
|
||||
$this->assertContains($expected_markup, $output, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute in %timezone.', [
|
||||
'%value' => $new_value,
|
||||
'%expected' => $expected,
|
||||
'%expected_iso' => $expected_iso,
|
||||
'%timezone' => $timezone,
|
||||
]));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -129,9 +134,12 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
|
||||
->setComponent($field_name, $this->displayOptions)
|
||||
->save();
|
||||
$expected = $date->format(DATETIME_DATE_STORAGE_FORMAT);
|
||||
$expected = $date->format(DateTimeItemInterface::DATE_STORAGE_FORMAT);
|
||||
$output = $this->renderTestEntity($id);
|
||||
$this->assertContains($expected, $output, SafeMarkup::format('Formatted date field using plain format displayed as %expected.', ['%expected' => $expected]));
|
||||
$this->assertContains($expected, $output, new FormattableMarkup('Formatted date field using plain format displayed as %expected in %timezone.', [
|
||||
'%expected' => $expected,
|
||||
'%timezone' => $timezone,
|
||||
]));
|
||||
|
||||
// Verify that the 'datetime_custom' formatter works.
|
||||
$this->displayOptions['type'] = 'datetime_custom';
|
||||
|
@ -141,7 +149,10 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
->save();
|
||||
$expected = $date->format($this->displayOptions['settings']['date_format']);
|
||||
$output = $this->renderTestEntity($id);
|
||||
$this->assertContains($expected, $output, SafeMarkup::format('Formatted date field using datetime_custom format displayed as %expected.', ['%expected' => $expected]));
|
||||
$this->assertContains($expected, $output, new FormattableMarkup('Formatted date field using datetime_custom format displayed as %expected in %timezone.', [
|
||||
'%expected' => $expected,
|
||||
'%timezone' => $timezone,
|
||||
]));
|
||||
|
||||
// Test that allowed markup in custom format is preserved and XSS is
|
||||
// removed.
|
||||
|
@ -151,13 +162,16 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
->save();
|
||||
$expected = '<strong>' . $date->format('m/d/Y') . '</strong>alert(String.fromCharCode(88,83,83))';
|
||||
$output = $this->renderTestEntity($id);
|
||||
$this->assertContains($expected, $output, new FormattableMarkup('Formatted date field using daterange_custom format displayed as %expected.', ['%expected' => $expected]));
|
||||
$this->assertContains($expected, $output, new FormattableMarkup('Formatted date field using daterange_custom format displayed as %expected in %timezone.', [
|
||||
'%expected' => $expected,
|
||||
'%timezone' => $timezone,
|
||||
]));
|
||||
|
||||
// Verify that the 'datetime_time_ago' formatter works for intervals in the
|
||||
// past. First update the test entity so that the date difference always
|
||||
// has the same interval. Since the database always stores UTC, and the
|
||||
// interval will use this, force the test date to use UTC and not the local
|
||||
// or user timezome.
|
||||
// or user timezone.
|
||||
$timestamp = REQUEST_TIME - 87654321;
|
||||
$entity = EntityTest::load($id);
|
||||
$field_name = $this->fieldStorage->getName();
|
||||
|
@ -174,17 +188,20 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
|
||||
->setComponent($field_name, $this->displayOptions)
|
||||
->save();
|
||||
$expected = SafeMarkup::format($this->displayOptions['settings']['past_format'], [
|
||||
'@interval' => $this->dateFormatter->formatTimeDiffSince($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']])
|
||||
$expected = new FormattableMarkup($this->displayOptions['settings']['past_format'], [
|
||||
'@interval' => $this->dateFormatter->formatTimeDiffSince($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]),
|
||||
]);
|
||||
$output = $this->renderTestEntity($id);
|
||||
$this->assertContains((string) $expected, $output, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', ['%expected' => $expected]));
|
||||
$this->assertContains((string) $expected, $output, new FormattableMarkup('Formatted date field using datetime_time_ago format displayed as %expected in %timezone.', [
|
||||
'%expected' => $expected,
|
||||
'%timezone' => $timezone,
|
||||
]));
|
||||
|
||||
// Verify that the 'datetime_time_ago' formatter works for intervals in the
|
||||
// future. First update the test entity so that the date difference always
|
||||
// has the same interval. Since the database always stores UTC, and the
|
||||
// interval will use this, force the test date to use UTC and not the local
|
||||
// or user timezome.
|
||||
// or user timezone.
|
||||
$timestamp = REQUEST_TIME + 87654321;
|
||||
$entity = EntityTest::load($id);
|
||||
$field_name = $this->fieldStorage->getName();
|
||||
|
@ -195,11 +212,14 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
|
||||
->setComponent($field_name, $this->displayOptions)
|
||||
->save();
|
||||
$expected = SafeMarkup::format($this->displayOptions['settings']['future_format'], [
|
||||
'@interval' => $this->dateFormatter->formatTimeDiffUntil($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']])
|
||||
$expected = new FormattableMarkup($this->displayOptions['settings']['future_format'], [
|
||||
'@interval' => $this->dateFormatter->formatTimeDiffUntil($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]),
|
||||
]);
|
||||
$output = $this->renderTestEntity($id);
|
||||
$this->assertContains((string) $expected, $output, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', ['%expected' => $expected]));
|
||||
$this->assertContains((string) $expected, $output, new FormattableMarkup('Formatted date field using datetime_time_ago format displayed as %expected in %timezone.', [
|
||||
'%expected' => $expected,
|
||||
'%timezone' => $timezone,
|
||||
]));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -208,6 +228,7 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
*/
|
||||
public function testDatetimeField() {
|
||||
$field_name = $this->fieldStorage->getName();
|
||||
$field_label = $this->field->label();
|
||||
// Change the field to a datetime field.
|
||||
$this->fieldStorage->setSetting('datetime_type', 'datetime');
|
||||
$this->fieldStorage->save();
|
||||
|
@ -216,7 +237,7 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
$this->drupalGet('entity_test/add');
|
||||
$this->assertFieldByName("{$field_name}[0][value][date]", '', 'Date element found.');
|
||||
$this->assertFieldByName("{$field_name}[0][value][time]", '', 'Time element found.');
|
||||
$this->assertFieldByXPath('//fieldset[@id="edit-' . $field_name . '-0"]/legend', $field_name, 'Fieldset and label found');
|
||||
$this->assertFieldByXPath('//fieldset[@id="edit-' . $field_name . '-0"]/legend', $field_label, 'Fieldset and label found');
|
||||
$this->assertFieldByXPath('//fieldset[@aria-describedby="edit-' . $field_name . '-0--description"]', NULL, 'ARIA described-by found');
|
||||
$this->assertFieldByXPath('//div[@id="edit-' . $field_name . '-0--description"]', NULL, 'ARIA description found');
|
||||
|
||||
|
@ -262,7 +283,7 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
$expected_iso = format_date($date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', 'UTC');
|
||||
$output = $this->renderTestEntity($id);
|
||||
$expected_markup = '<time datetime="' . $expected_iso . '" class="datetime">' . $expected . '</time>';
|
||||
$this->assertContains($expected_markup, $output, SafeMarkup::format('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', ['%value' => $new_value, '%expected' => $expected, '%expected_iso' => $expected_iso]));
|
||||
$this->assertContains($expected_markup, $output, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', ['%value' => $new_value, '%expected' => $expected, '%expected_iso' => $expected_iso]));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -274,9 +295,9 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
|
||||
->setComponent($field_name, $this->displayOptions)
|
||||
->save();
|
||||
$expected = $date->format(DATETIME_DATETIME_STORAGE_FORMAT);
|
||||
$expected = $date->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
|
||||
$output = $this->renderTestEntity($id);
|
||||
$this->assertContains($expected, $output, SafeMarkup::format('Formatted date field using plain format displayed as %expected.', ['%expected' => $expected]));
|
||||
$this->assertContains($expected, $output, new FormattableMarkup('Formatted date field using plain format displayed as %expected.', ['%expected' => $expected]));
|
||||
|
||||
// Verify that the 'datetime_custom' formatter works.
|
||||
$this->displayOptions['type'] = 'datetime_custom';
|
||||
|
@ -286,7 +307,7 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
->save();
|
||||
$expected = $date->format($this->displayOptions['settings']['date_format']);
|
||||
$output = $this->renderTestEntity($id);
|
||||
$this->assertContains($expected, $output, SafeMarkup::format('Formatted date field using datetime_custom format displayed as %expected.', ['%expected' => $expected]));
|
||||
$this->assertContains($expected, $output, new FormattableMarkup('Formatted date field using datetime_custom format displayed as %expected.', ['%expected' => $expected]));
|
||||
|
||||
// Verify that the 'timezone_override' setting works.
|
||||
$this->displayOptions['type'] = 'datetime_custom';
|
||||
|
@ -296,18 +317,18 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
->save();
|
||||
$expected = $date->format($this->displayOptions['settings']['date_format'], ['timezone' => 'America/New_York']);
|
||||
$output = $this->renderTestEntity($id);
|
||||
$this->assertContains($expected, $output, SafeMarkup::format('Formatted date field using datetime_custom format displayed as %expected.', ['%expected' => $expected]));
|
||||
$this->assertContains($expected, $output, new FormattableMarkup('Formatted date field using datetime_custom format displayed as %expected.', ['%expected' => $expected]));
|
||||
|
||||
// Verify that the 'datetime_time_ago' formatter works for intervals in the
|
||||
// past. First update the test entity so that the date difference always
|
||||
// has the same interval. Since the database always stores UTC, and the
|
||||
// interval will use this, force the test date to use UTC and not the local
|
||||
// or user timezome.
|
||||
// or user timezone.
|
||||
$timestamp = REQUEST_TIME - 87654321;
|
||||
$entity = EntityTest::load($id);
|
||||
$field_name = $this->fieldStorage->getName();
|
||||
$date = DrupalDateTime::createFromTimestamp($timestamp, 'UTC');
|
||||
$entity->{$field_name}->value = $date->format(DATETIME_DATETIME_STORAGE_FORMAT);
|
||||
$entity->{$field_name}->value = $date->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
|
||||
$entity->save();
|
||||
|
||||
$this->displayOptions['type'] = 'datetime_time_ago';
|
||||
|
@ -319,32 +340,32 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
|
||||
->setComponent($field_name, $this->displayOptions)
|
||||
->save();
|
||||
$expected = SafeMarkup::format($this->displayOptions['settings']['past_format'], [
|
||||
'@interval' => $this->dateFormatter->formatTimeDiffSince($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']])
|
||||
$expected = new FormattableMarkup($this->displayOptions['settings']['past_format'], [
|
||||
'@interval' => $this->dateFormatter->formatTimeDiffSince($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]),
|
||||
]);
|
||||
$output = $this->renderTestEntity($id);
|
||||
$this->assertContains((string) $expected, $output, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', ['%expected' => $expected]));
|
||||
$this->assertContains((string) $expected, $output, new FormattableMarkup('Formatted date field using datetime_time_ago format displayed as %expected.', ['%expected' => $expected]));
|
||||
|
||||
// Verify that the 'datetime_time_ago' formatter works for intervals in the
|
||||
// future. First update the test entity so that the date difference always
|
||||
// has the same interval. Since the database always stores UTC, and the
|
||||
// interval will use this, force the test date to use UTC and not the local
|
||||
// or user timezome.
|
||||
// or user timezone.
|
||||
$timestamp = REQUEST_TIME + 87654321;
|
||||
$entity = EntityTest::load($id);
|
||||
$field_name = $this->fieldStorage->getName();
|
||||
$date = DrupalDateTime::createFromTimestamp($timestamp, 'UTC');
|
||||
$entity->{$field_name}->value = $date->format(DATETIME_DATETIME_STORAGE_FORMAT);
|
||||
$entity->{$field_name}->value = $date->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
|
||||
$entity->save();
|
||||
|
||||
entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
|
||||
->setComponent($field_name, $this->displayOptions)
|
||||
->save();
|
||||
$expected = SafeMarkup::format($this->displayOptions['settings']['future_format'], [
|
||||
'@interval' => $this->dateFormatter->formatTimeDiffUntil($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']])
|
||||
$expected = new FormattableMarkup($this->displayOptions['settings']['future_format'], [
|
||||
'@interval' => $this->dateFormatter->formatTimeDiffUntil($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]),
|
||||
]);
|
||||
$output = $this->renderTestEntity($id);
|
||||
$this->assertContains((string) $expected, $output, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', ['%expected' => $expected]));
|
||||
$this->assertContains((string) $expected, $output, new FormattableMarkup('Formatted date field using datetime_time_ago format displayed as %expected.', ['%expected' => $expected]));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -352,6 +373,7 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
*/
|
||||
public function testDatelistWidget() {
|
||||
$field_name = $this->fieldStorage->getName();
|
||||
$field_label = $this->field->label();
|
||||
|
||||
// Ensure field is set to a date only field.
|
||||
$this->fieldStorage->setSetting('datetime_type', 'date');
|
||||
|
@ -370,7 +392,7 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
|
||||
// Display creation form.
|
||||
$this->drupalGet('entity_test/add');
|
||||
$this->assertFieldByXPath('//fieldset[@id="edit-' . $field_name . '-0"]/legend', $field_name, 'Fieldset and label found');
|
||||
$this->assertFieldByXPath('//fieldset[@id="edit-' . $field_name . '-0"]/legend', $field_label, 'Fieldset and label found');
|
||||
$this->assertFieldByXPath('//fieldset[@aria-describedby="edit-' . $field_name . '-0--description"]', NULL, 'ARIA described-by found');
|
||||
$this->assertFieldByXPath('//div[@id="edit-' . $field_name . '-0--description"]', NULL, 'ARIA description found');
|
||||
|
||||
|
@ -511,7 +533,7 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
\Drupal::entityManager()->clearCachedFieldDefinitions();
|
||||
|
||||
// Test the widget for validation notifications.
|
||||
foreach ($this->datelistDataProvider() as $data) {
|
||||
foreach ($this->datelistDataProvider($field_label) as $data) {
|
||||
list($date_value, $expected) = $data;
|
||||
|
||||
// Display creation form.
|
||||
|
@ -562,33 +584,57 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
/**
|
||||
* The data provider for testing the validation of the datelist widget.
|
||||
*
|
||||
* @param string $field_label
|
||||
* The label of the field being tested.
|
||||
*
|
||||
* @return array
|
||||
* An array of datelist input permutations to test.
|
||||
*/
|
||||
protected function datelistDataProvider() {
|
||||
protected function datelistDataProvider($field_label) {
|
||||
return [
|
||||
// Nothing selected.
|
||||
[
|
||||
['year' => '', 'month' => '', 'day' => '', 'hour' => '', 'minute' => ''],
|
||||
["The $field_label date is required."],
|
||||
],
|
||||
// Year only selected, validation error on Month, Day, Hour, Minute.
|
||||
[['year' => 2012, 'month' => '', 'day' => '', 'hour' => '', 'minute' => ''], [
|
||||
'A value must be selected for month.',
|
||||
'A value must be selected for day.',
|
||||
'A value must be selected for hour.',
|
||||
'A value must be selected for minute.',
|
||||
]],
|
||||
[
|
||||
['year' => 2012, 'month' => '', 'day' => '', 'hour' => '', 'minute' => ''],
|
||||
[
|
||||
"The $field_label date is incomplete.",
|
||||
'A value must be selected for month.',
|
||||
'A value must be selected for day.',
|
||||
'A value must be selected for hour.',
|
||||
'A value must be selected for minute.',
|
||||
],
|
||||
],
|
||||
// Year and Month selected, validation error on Day, Hour, Minute.
|
||||
[['year' => 2012, 'month' => '12', 'day' => '', 'hour' => '', 'minute' => ''], [
|
||||
'A value must be selected for day.',
|
||||
'A value must be selected for hour.',
|
||||
'A value must be selected for minute.',
|
||||
]],
|
||||
[
|
||||
['year' => 2012, 'month' => '12', 'day' => '', 'hour' => '', 'minute' => ''],
|
||||
[
|
||||
"The $field_label date is incomplete.",
|
||||
'A value must be selected for day.',
|
||||
'A value must be selected for hour.',
|
||||
'A value must be selected for minute.',
|
||||
],
|
||||
],
|
||||
// Year, Month and Day selected, validation error on Hour, Minute.
|
||||
[['year' => 2012, 'month' => '12', 'day' => '31', 'hour' => '', 'minute' => ''], [
|
||||
'A value must be selected for hour.',
|
||||
'A value must be selected for minute.',
|
||||
]],
|
||||
[
|
||||
['year' => 2012, 'month' => '12', 'day' => '31', 'hour' => '', 'minute' => ''],
|
||||
[
|
||||
"The $field_label date is incomplete.",
|
||||
'A value must be selected for hour.',
|
||||
'A value must be selected for minute.',
|
||||
],
|
||||
],
|
||||
// Year, Month, Day and Hour selected, validation error on Minute only.
|
||||
[['year' => 2012, 'month' => '12', 'day' => '31', 'hour' => '0', 'minute' => ''], [
|
||||
'A value must be selected for minute.',
|
||||
]],
|
||||
[
|
||||
['year' => 2012, 'month' => '12', 'day' => '31', 'hour' => '0', 'minute' => ''],
|
||||
[
|
||||
"The $field_label date is incomplete.",
|
||||
'A value must be selected for minute.',
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -600,7 +646,7 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
$this->drupalCreateContentType(['type' => 'date_content']);
|
||||
|
||||
// Create a field storage with settings to validate.
|
||||
$field_name = Unicode::strtolower($this->randomMachineName());
|
||||
$field_name = mb_strtolower($this->randomMachineName());
|
||||
$field_storage = FieldStorageConfig::create([
|
||||
'field_name' => $field_name,
|
||||
'entity_type' => 'node',
|
||||
|
@ -620,6 +666,7 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
foreach (static::$timezones as $timezone) {
|
||||
|
||||
$this->setSiteTimezone($timezone);
|
||||
$this->assertEquals($timezone, $this->config('system.date')->get('timezone.default'), 'Time zone set to ' . $timezone);
|
||||
|
||||
// Set now as default_value.
|
||||
$field_edit = [
|
||||
|
@ -647,7 +694,7 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
$new_node = Node::create(['type' => 'date_content']);
|
||||
$expected_date = new DrupalDateTime('now', drupal_get_user_timezone());
|
||||
$this->assertEqual($new_node->get($field_name)
|
||||
->offsetGet(0)->value, $expected_date->format(DATETIME_DATE_STORAGE_FORMAT));
|
||||
->offsetGet(0)->value, $expected_date->format(DateTimeItemInterface::DATE_STORAGE_FORMAT));
|
||||
|
||||
// Set an invalid relative default_value to test validation.
|
||||
$field_edit = [
|
||||
|
@ -686,7 +733,7 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
$new_node = Node::create(['type' => 'date_content']);
|
||||
$expected_date = new DrupalDateTime('+90 days', drupal_get_user_timezone());
|
||||
$this->assertEqual($new_node->get($field_name)
|
||||
->offsetGet(0)->value, $expected_date->format(DATETIME_DATE_STORAGE_FORMAT));
|
||||
->offsetGet(0)->value, $expected_date->format(DateTimeItemInterface::DATE_STORAGE_FORMAT));
|
||||
|
||||
// Remove default value.
|
||||
$field_edit = [
|
||||
|
@ -806,7 +853,7 @@ class DateTimeFieldTest extends DateTestBase {
|
|||
$this->drupalCreateContentType(['type' => 'date_content']);
|
||||
|
||||
// Create a field storage with settings to validate.
|
||||
$field_name = Unicode::strtolower($this->randomMachineName());
|
||||
$field_name = mb_strtolower($this->randomMachineName());
|
||||
$field_storage = FieldStorageConfig::create([
|
||||
'field_name' => $field_name,
|
||||
'entity_type' => 'node',
|
||||
|
|
|
@ -0,0 +1,122 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\datetime\Functional;
|
||||
|
||||
use Drupal\Core\Entity\Entity\EntityFormDisplay;
|
||||
use Drupal\Core\Entity\Entity\EntityViewDisplay;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
use Drupal\Tests\BrowserTestBase;
|
||||
|
||||
/**
|
||||
* Tests the functionality of DateTimeTimeAgoFormatter field formatter.
|
||||
*
|
||||
* @group field
|
||||
*/
|
||||
class DateTimeTimeAgoFormatterTest extends BrowserTestBase {
|
||||
|
||||
/**
|
||||
* An array of display options to pass to entity_get_display().
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $displayOptions;
|
||||
|
||||
/**
|
||||
* A field storage to use in this test class.
|
||||
*
|
||||
* @var \Drupal\field\Entity\FieldStorageConfig
|
||||
*/
|
||||
protected $fieldStorage;
|
||||
|
||||
/**
|
||||
* The field used in this test class.
|
||||
*
|
||||
* @var \Drupal\field\Entity\FieldConfig
|
||||
*/
|
||||
protected $field;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['datetime', 'entity_test', 'field_ui'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$web_user = $this->drupalCreateUser([
|
||||
'access administration pages',
|
||||
'view test entity',
|
||||
'administer entity_test content',
|
||||
'administer entity_test fields',
|
||||
'administer entity_test display',
|
||||
'administer entity_test form display',
|
||||
'view the administration theme',
|
||||
]);
|
||||
$this->drupalLogin($web_user);
|
||||
|
||||
$field_name = 'field_datetime';
|
||||
$type = 'datetime';
|
||||
$widget_type = 'datetime_default';
|
||||
$formatter_type = 'datetime_time_ago';
|
||||
|
||||
$this->fieldStorage = FieldStorageConfig::create([
|
||||
'field_name' => $field_name,
|
||||
'entity_type' => 'entity_test',
|
||||
'type' => $type,
|
||||
]);
|
||||
$this->fieldStorage->save();
|
||||
$this->field = FieldConfig::create([
|
||||
'field_storage' => $this->fieldStorage,
|
||||
'bundle' => 'entity_test',
|
||||
'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',
|
||||
];
|
||||
|
||||
EntityViewDisplay::create([
|
||||
'targetEntityType' => $this->field->getTargetEntityTypeId(),
|
||||
'bundle' => $this->field->getTargetBundle(),
|
||||
'mode' => 'full',
|
||||
'status' => TRUE,
|
||||
])->setComponent($field_name, $this->displayOptions)
|
||||
->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the formatter settings.
|
||||
*/
|
||||
public function testSettings() {
|
||||
$this->drupalGet('entity_test/structure/entity_test/display');
|
||||
|
||||
$edit = [
|
||||
'fields[field_datetime][region]' => 'content',
|
||||
'fields[field_datetime][type]' => 'datetime_time_ago',
|
||||
];
|
||||
$this->drupalPostForm(NULL, $edit, t('Save'));
|
||||
|
||||
$this->drupalPostForm(NULL, [], 'field_datetime_settings_edit');
|
||||
$edit = [
|
||||
'fields[field_datetime][settings_edit_form][settings][future_format]' => 'ends in @interval',
|
||||
'fields[field_datetime][settings_edit_form][settings][past_format]' => 'started @interval ago',
|
||||
'fields[field_datetime][settings_edit_form][settings][granularity]' => 3,
|
||||
];
|
||||
$this->drupalPostForm(NULL, $edit, 'Update');
|
||||
$this->drupalPostForm(NULL, [], 'Save');
|
||||
|
||||
$this->assertSession()->pageTextContains('ends in 1 year 1 month 1 week');
|
||||
$this->assertSession()->pageTextContains('started 1 year 1 month 1 week ago');
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,154 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\datetime\Functional\EntityResource\EntityTest;
|
||||
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\entity_test\Entity\EntityTest;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
use Drupal\Tests\entity_test\Functional\Rest\EntityTestResourceTestBase;
|
||||
use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
|
||||
use GuzzleHttp\RequestOptions;
|
||||
|
||||
/**
|
||||
* Tests the datetime field constraint with 'date' items.
|
||||
*
|
||||
* @group datetime
|
||||
*/
|
||||
class EntityTestDateonlyTest extends EntityTestResourceTestBase {
|
||||
|
||||
use AnonResourceTestTrait;
|
||||
|
||||
/**
|
||||
* The ISO date string to use throughout the test.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected static $dateString = '2017-03-01';
|
||||
|
||||
/**
|
||||
* Datetime test field name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected static $fieldName = 'field_dateonly';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['datetime', 'entity_test'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Add datetime field.
|
||||
FieldStorageConfig::create([
|
||||
'field_name' => static::$fieldName,
|
||||
'type' => 'datetime',
|
||||
'entity_type' => static::$entityTypeId,
|
||||
'settings' => ['datetime_type' => DateTimeItem::DATETIME_TYPE_DATE],
|
||||
])
|
||||
->save();
|
||||
|
||||
FieldConfig::create([
|
||||
'field_name' => static::$fieldName,
|
||||
'entity_type' => static::$entityTypeId,
|
||||
'bundle' => $this->entity->bundle(),
|
||||
'settings' => ['default_value' => static::$dateString],
|
||||
])
|
||||
->save();
|
||||
|
||||
// Reload entity so that it has the new field.
|
||||
$this->entity = $this->entityStorage->load($this->entity->id());
|
||||
$this->entity->set(static::$fieldName, ['value' => static::$dateString]);
|
||||
$this->entity->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function createEntity() {
|
||||
$entity_test = EntityTest::create([
|
||||
'name' => 'Llama',
|
||||
'type' => static::$entityTypeId,
|
||||
static::$fieldName => static::$dateString,
|
||||
]);
|
||||
$entity_test->setOwnerId(0);
|
||||
$entity_test->save();
|
||||
|
||||
return $entity_test;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getExpectedNormalizedEntity() {
|
||||
return parent::getExpectedNormalizedEntity() + [
|
||||
static::$fieldName => [
|
||||
[
|
||||
'value' => $this->entity->get(static::$fieldName)->value,
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getNormalizedPostEntity() {
|
||||
return parent::getNormalizedPostEntity() + [
|
||||
static::$fieldName => [
|
||||
[
|
||||
'value' => static::$dateString,
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function assertNormalizationEdgeCases($method, Url $url, array $request_options) {
|
||||
parent::assertNormalizationEdgeCases($method, $url, $request_options);
|
||||
|
||||
if ($this->entity->getEntityType()->hasKey('bundle')) {
|
||||
$fieldName = static::$fieldName;
|
||||
|
||||
// DX: 422 when date type is incorrect.
|
||||
$normalization = $this->getNormalizedPostEntity();
|
||||
$normalization[static::$fieldName][0]['value'] = [
|
||||
'2017', '03', '01',
|
||||
];
|
||||
|
||||
$request_options[RequestOptions::BODY] = $this->serializer->encode($normalization, static::$format);
|
||||
$response = $this->request($method, $url, $request_options);
|
||||
$message = "Unprocessable Entity: validation failed.\n{$fieldName}.0: The datetime value must be a string.\n{$fieldName}.0.value: This value should be of the correct primitive type.\n";
|
||||
$this->assertResourceErrorResponse(422, $message, $response);
|
||||
|
||||
// DX: 422 when date format is incorrect.
|
||||
$normalization = $this->getNormalizedPostEntity();
|
||||
$value = '2017-03-01T01:02:03';
|
||||
$normalization[static::$fieldName][0]['value'] = $value;
|
||||
|
||||
$request_options[RequestOptions::BODY] = $this->serializer->encode($normalization, static::$format);
|
||||
$response = $this->request($method, $url, $request_options);
|
||||
$message = "Unprocessable Entity: validation failed.\n{$fieldName}.0: The datetime value '{$value}' is invalid for the format 'Y-m-d'\n";
|
||||
$this->assertResourceErrorResponse(422, $message, $response);
|
||||
|
||||
// DX: 422 when value is not a valid date.
|
||||
$normalization = $this->getNormalizedPostEntity();
|
||||
$value = '2017-13-55';
|
||||
$normalization[static::$fieldName][0]['value'] = $value;
|
||||
|
||||
$request_options[RequestOptions::BODY] = $this->serializer->encode($normalization, static::$format);
|
||||
$response = $this->request($method, $url, $request_options);
|
||||
$message = "Unprocessable Entity: validation failed.\n{$fieldName}.0: The datetime value '{$value}' did not parse properly for the format 'Y-m-d'\n{$fieldName}.0.value: This value should be of the correct primitive type.\n";
|
||||
$this->assertResourceErrorResponse(422, $message, $response);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,154 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\datetime\Functional\EntityResource\EntityTest;
|
||||
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\entity_test\Entity\EntityTest;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
use Drupal\Tests\entity_test\Functional\Rest\EntityTestResourceTestBase;
|
||||
use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
|
||||
use GuzzleHttp\RequestOptions;
|
||||
|
||||
/**
|
||||
* Tests the datetime field constraint with 'datetime' items.
|
||||
*
|
||||
* @group datetime
|
||||
*/
|
||||
class EntityTestDatetimeTest extends EntityTestResourceTestBase {
|
||||
|
||||
use AnonResourceTestTrait;
|
||||
|
||||
/**
|
||||
* The ISO date string to use throughout the test.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected static $dateString = '2017-03-01T20:02:00';
|
||||
|
||||
/**
|
||||
* Datetime test field name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected static $fieldName = 'field_datetime';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['datetime', 'entity_test'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// Add datetime field.
|
||||
FieldStorageConfig::create([
|
||||
'field_name' => static::$fieldName,
|
||||
'type' => 'datetime',
|
||||
'entity_type' => static::$entityTypeId,
|
||||
'settings' => ['datetime_type' => DateTimeItem::DATETIME_TYPE_DATETIME],
|
||||
])
|
||||
->save();
|
||||
|
||||
FieldConfig::create([
|
||||
'field_name' => static::$fieldName,
|
||||
'entity_type' => static::$entityTypeId,
|
||||
'bundle' => $this->entity->bundle(),
|
||||
'settings' => ['default_value' => static::$dateString],
|
||||
])
|
||||
->save();
|
||||
|
||||
// Reload entity so that it has the new field.
|
||||
$this->entity = $this->entityStorage->load($this->entity->id());
|
||||
$this->entity->set(static::$fieldName, ['value' => static::$dateString]);
|
||||
$this->entity->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function createEntity() {
|
||||
$entity_test = EntityTest::create([
|
||||
'name' => 'Llama',
|
||||
'type' => static::$entityTypeId,
|
||||
static::$fieldName => static::$dateString,
|
||||
]);
|
||||
$entity_test->setOwnerId(0);
|
||||
$entity_test->save();
|
||||
|
||||
return $entity_test;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getExpectedNormalizedEntity() {
|
||||
return parent::getExpectedNormalizedEntity() + [
|
||||
static::$fieldName => [
|
||||
[
|
||||
'value' => $this->entity->get(static::$fieldName)->value,
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getNormalizedPostEntity() {
|
||||
return parent::getNormalizedPostEntity() + [
|
||||
static::$fieldName => [
|
||||
[
|
||||
'value' => static::$dateString,
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function assertNormalizationEdgeCases($method, Url $url, array $request_options) {
|
||||
parent::assertNormalizationEdgeCases($method, $url, $request_options);
|
||||
|
||||
if ($this->entity->getEntityType()->hasKey('bundle')) {
|
||||
$fieldName = static::$fieldName;
|
||||
|
||||
// DX: 422 when date type is incorrect.
|
||||
$normalization = $this->getNormalizedPostEntity();
|
||||
$normalization[static::$fieldName][0]['value'] = [
|
||||
'2017', '03', '01', '21', '53', '00',
|
||||
];
|
||||
|
||||
$request_options[RequestOptions::BODY] = $this->serializer->encode($normalization, static::$format);
|
||||
$response = $this->request($method, $url, $request_options);
|
||||
$message = "Unprocessable Entity: validation failed.\n{$fieldName}.0: The datetime value must be a string.\n{$fieldName}.0.value: This value should be of the correct primitive type.\n";
|
||||
$this->assertResourceErrorResponse(422, $message, $response);
|
||||
|
||||
// DX: 422 when date format is incorrect.
|
||||
$normalization = $this->getNormalizedPostEntity();
|
||||
$value = '2017-03-01';
|
||||
$normalization[static::$fieldName][0]['value'] = $value;
|
||||
|
||||
$request_options[RequestOptions::BODY] = $this->serializer->encode($normalization, static::$format);
|
||||
$response = $this->request($method, $url, $request_options);
|
||||
$message = "Unprocessable Entity: validation failed.\n{$fieldName}.0: The datetime value '{$value}' is invalid for the format 'Y-m-d\\TH:i:s'\n";
|
||||
$this->assertResourceErrorResponse(422, $message, $response);
|
||||
|
||||
// DX: 422 when date format is incorrect.
|
||||
$normalization = $this->getNormalizedPostEntity();
|
||||
$value = '2017-13-55T20:02:00';
|
||||
$normalization[static::$fieldName][0]['value'] = $value;
|
||||
|
||||
$request_options[RequestOptions::BODY] = $this->serializer->encode($normalization, static::$format);
|
||||
$response = $this->request($method, $url, $request_options);
|
||||
$message = "Unprocessable Entity: validation failed.\n{$fieldName}.0: The datetime value '{$value}' did not parse properly for the format 'Y-m-d\\TH:i:s'\n{$fieldName}.0.value: This value should be of the correct primitive type.\n";
|
||||
$this->assertResourceErrorResponse(422, $message, $response);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -5,6 +5,7 @@ namespace Drupal\Tests\datetime\Kernel;
|
|||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Core\Field\FieldItemInterface;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
|
||||
use Drupal\entity_test\Entity\EntityTest;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
|
||||
|
@ -80,16 +81,19 @@ class DateTimeItemTest extends FieldKernelTestBase {
|
|||
$this->assertTrue($entity->field_datetime[0] instanceof FieldItemInterface, 'Field item implements interface.');
|
||||
$this->assertEqual($entity->field_datetime->value, $value);
|
||||
$this->assertEqual($entity->field_datetime[0]->value, $value);
|
||||
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
|
||||
|
||||
// Verify changing the date value.
|
||||
$new_value = '2016-11-04T00:21:00';
|
||||
$entity->field_datetime->value = $new_value;
|
||||
$this->assertEqual($entity->field_datetime->value, $new_value);
|
||||
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
|
||||
|
||||
// Read changed entity and assert changed values.
|
||||
$this->entityValidateAndSave($entity);
|
||||
$entity = EntityTest::load($id);
|
||||
$this->assertEqual($entity->field_datetime->value, $new_value);
|
||||
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
|
||||
|
||||
// Test the generateSampleValue() method.
|
||||
$entity = EntityTest::create();
|
||||
|
@ -118,20 +122,35 @@ class DateTimeItemTest extends FieldKernelTestBase {
|
|||
$this->assertTrue($entity->field_datetime[0] instanceof FieldItemInterface, 'Field item implements interface.');
|
||||
$this->assertEqual($entity->field_datetime->value, $value);
|
||||
$this->assertEqual($entity->field_datetime[0]->value, $value);
|
||||
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
|
||||
$this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
|
||||
$entity->field_datetime->date->setDefaultDateTime();
|
||||
$this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
|
||||
|
||||
// Verify changing the date value.
|
||||
$new_value = '2016-11-04';
|
||||
$entity->field_datetime->value = $new_value;
|
||||
$this->assertEqual($entity->field_datetime->value, $new_value);
|
||||
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
|
||||
$this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
|
||||
$entity->field_datetime->date->setDefaultDateTime();
|
||||
$this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
|
||||
|
||||
// Read changed entity and assert changed values.
|
||||
$this->entityValidateAndSave($entity);
|
||||
$entity = EntityTest::load($id);
|
||||
$this->assertEqual($entity->field_datetime->value, $new_value);
|
||||
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
|
||||
$this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
|
||||
$entity->field_datetime->date->setDefaultDateTime();
|
||||
$this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
|
||||
|
||||
// Test the generateSampleValue() method.
|
||||
$entity = EntityTest::create();
|
||||
$entity->field_datetime->generateSampleItems();
|
||||
$this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
|
||||
$entity->field_datetime->date->setDefaultDateTime();
|
||||
$this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
|
||||
$this->entityValidateAndSave($entity);
|
||||
}
|
||||
|
||||
|
@ -152,6 +171,7 @@ class DateTimeItemTest extends FieldKernelTestBase {
|
|||
$id = $entity->id();
|
||||
$entity = EntityTest::load($id);
|
||||
$this->assertEqual($entity->field_datetime[0]->value, $value, 'DateTimeItem::setValue() works with string value.');
|
||||
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
|
||||
|
||||
// Test DateTimeItem::setValue() using property array.
|
||||
$entity = EntityTest::create();
|
||||
|
@ -162,6 +182,7 @@ class DateTimeItemTest extends FieldKernelTestBase {
|
|||
$id = $entity->id();
|
||||
$entity = EntityTest::load($id);
|
||||
$this->assertEqual($entity->field_datetime[0]->value, $value, 'DateTimeItem::setValue() works with array value.');
|
||||
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
|
||||
|
||||
// Test a date-only field.
|
||||
$this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
|
||||
|
@ -176,6 +197,7 @@ class DateTimeItemTest extends FieldKernelTestBase {
|
|||
$id = $entity->id();
|
||||
$entity = EntityTest::load($id);
|
||||
$this->assertEqual($entity->field_datetime[0]->value, $value, 'DateTimeItem::setValue() works with string value.');
|
||||
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
|
||||
|
||||
// Test DateTimeItem::setValue() using property array.
|
||||
$entity = EntityTest::create();
|
||||
|
@ -186,6 +208,7 @@ class DateTimeItemTest extends FieldKernelTestBase {
|
|||
$id = $entity->id();
|
||||
$entity = EntityTest::load($id);
|
||||
$this->assertEqual($entity->field_datetime[0]->value, $value, 'DateTimeItem::setValue() works with array value.');
|
||||
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -205,6 +228,7 @@ class DateTimeItemTest extends FieldKernelTestBase {
|
|||
$id = $entity->id();
|
||||
$entity = EntityTest::load($id);
|
||||
$this->assertEqual($entity->field_datetime[0]->value, $value, '"Value" property can be set directly.');
|
||||
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
|
||||
|
||||
// Test Date::setValue() with a date-only field.
|
||||
// Test a date+time field.
|
||||
|
@ -219,6 +243,107 @@ class DateTimeItemTest extends FieldKernelTestBase {
|
|||
$id = $entity->id();
|
||||
$entity = EntityTest::load($id);
|
||||
$this->assertEqual($entity->field_datetime[0]->value, $value, '"Value" property can be set directly.');
|
||||
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the constraint validations for fields with date and time.
|
||||
*
|
||||
* @dataProvider datetimeValidationProvider
|
||||
*/
|
||||
public function testDatetimeValidation($value) {
|
||||
$this->setExpectedException(\PHPUnit_Framework_AssertionFailedError::class);
|
||||
|
||||
$this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATETIME);
|
||||
$this->fieldStorage->save();
|
||||
$entity = EntityTest::create();
|
||||
|
||||
$entity->set('field_datetime', $value);
|
||||
$this->entityValidateAndSave($entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provider for testDatetimeValidation().
|
||||
*/
|
||||
public function datetimeValidationProvider() {
|
||||
return [
|
||||
// Valid ISO 8601 dates, but unsupported by DateTimeItem.
|
||||
['2014-01-01T20:00:00Z'],
|
||||
['2014-01-01T20:00:00+04:00'],
|
||||
['2014-01-01T20:00:00+0400'],
|
||||
['2014-01-01T20:00:00+04'],
|
||||
['2014-01-01T20:00:00.123'],
|
||||
['2014-01-01T200000'],
|
||||
['2014-01-01T2000'],
|
||||
['2014-01-01T20'],
|
||||
['20140101T20:00:00'],
|
||||
['2014-01T20:00:00'],
|
||||
['2014-001T20:00:00'],
|
||||
['2014001T20:00:00'],
|
||||
// Valid date strings, but unsupported by DateTimeItem.
|
||||
['2016-11-03 20:52:00'],
|
||||
['Thu, 03 Nov 2014 20:52:00 -0400'],
|
||||
['Thursday, November 3, 2016 - 20:52'],
|
||||
['Thu, 11/03/2016 - 20:52'],
|
||||
['11/03/2016 - 20:52'],
|
||||
// Invalid date strings.
|
||||
['YYYY-01-01T20:00:00'],
|
||||
['2014-MM-01T20:00:00'],
|
||||
['2014-01-DDT20:00:00'],
|
||||
['2014-01-01Thh:00:00'],
|
||||
['2014-01-01T20:mm:00'],
|
||||
['2014-01-01T20:00:ss'],
|
||||
// Invalid dates.
|
||||
['2014-13-13T20:00:00'],
|
||||
['2014-01-55T20:00:00'],
|
||||
['2014-01-01T25:00:00'],
|
||||
['2014-01-01T00:70:00'],
|
||||
['2014-01-01T00:00:70'],
|
||||
// Proper format for different field setting.
|
||||
['2014-01-01'],
|
||||
// Wrong input type.
|
||||
[['2014', '01', '01', '00', '00', '00']],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the constraint validations for fields with date only.
|
||||
*
|
||||
* @dataProvider dateonlyValidationProvider
|
||||
*/
|
||||
public function testDateonlyValidation($value) {
|
||||
$this->setExpectedException(\PHPUnit_Framework_AssertionFailedError::class);
|
||||
|
||||
$this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
|
||||
$this->fieldStorage->save();
|
||||
$entity = EntityTest::create();
|
||||
|
||||
$entity->set('field_datetime', $value);
|
||||
$this->entityValidateAndSave($entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provider for testDatetimeValidation().
|
||||
*/
|
||||
public function dateonlyValidationProvider() {
|
||||
return [
|
||||
// Valid date strings, but unsupported by DateTimeItem.
|
||||
['Thu, 03 Nov 2014'],
|
||||
['Thursday, November 3, 2016'],
|
||||
['Thu, 11/03/2016'],
|
||||
['11/03/2016'],
|
||||
// Invalid date strings.
|
||||
['YYYY-01-01'],
|
||||
['2014-MM-01'],
|
||||
['2014-01-DD'],
|
||||
// Invalid dates.
|
||||
['2014-13-01'],
|
||||
['2014-01-55'],
|
||||
// Proper format for different field setting.
|
||||
['2014-01-01T20:00:00'],
|
||||
// Wrong input type.
|
||||
[['2014', '01', '01']],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\datetime\Tests\Views;
|
||||
namespace Drupal\Tests\datetime\Kernel\Views;
|
||||
|
||||
use Drupal\node\Entity\Node;
|
||||
use Drupal\views\Views;
|
||||
|
||||
/**
|
||||
|
@ -19,21 +20,28 @@ class ArgumentDateTimeTest extends DateTimeHandlerTestBase {
|
|||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
protected function setUp($import_test_views = TRUE) {
|
||||
parent::setUp($import_test_views);
|
||||
|
||||
// Add some basic test nodes.
|
||||
$dates = [
|
||||
'2000-10-10',
|
||||
'2001-10-10',
|
||||
'2002-01-01',
|
||||
// Add a date that is the year 2002 in UTC, but 2003 in the site's time
|
||||
// zone (Australia/Sydney).
|
||||
'2002-12-31T23:00:00',
|
||||
];
|
||||
foreach ($dates as $date) {
|
||||
$this->nodes[] = $this->drupalCreateNode([
|
||||
$node = Node::create([
|
||||
'title' => $this->randomMachineName(8),
|
||||
'type' => 'page',
|
||||
'field_date' => [
|
||||
'value' => $date,
|
||||
]
|
||||
],
|
||||
]);
|
||||
$node->save();
|
||||
$this->nodes[] = $node;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -59,6 +67,25 @@ class ArgumentDateTimeTest extends DateTimeHandlerTestBase {
|
|||
$expected[] = ['nid' => $this->nodes[2]->id()];
|
||||
$this->assertIdenticalResultset($view, $expected, $this->map);
|
||||
$view->destroy();
|
||||
|
||||
$view->setDisplay('default');
|
||||
$this->executeView($view, ['2003']);
|
||||
$expected = [];
|
||||
$expected[] = ['nid' => $this->nodes[3]->id()];
|
||||
$this->assertIdenticalResultset($view, $expected, $this->map);
|
||||
$view->destroy();
|
||||
|
||||
// Tests different system timezone with the same nodes.
|
||||
$this->setSiteTimezone('America/Vancouver');
|
||||
|
||||
$view->setDisplay('default');
|
||||
$this->executeView($view, ['2002']);
|
||||
$expected = [];
|
||||
// Only the 3rd node is returned here since UTC 2002-01-01T00:00:00 is still
|
||||
// in 2001 for this user timezone.
|
||||
$expected[] = ['nid' => $this->nodes[3]->id()];
|
||||
$this->assertIdenticalResultset($view, $expected, $this->map);
|
||||
$view->destroy();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -82,6 +109,7 @@ class ArgumentDateTimeTest extends DateTimeHandlerTestBase {
|
|||
$this->executeView($view, ['01']);
|
||||
$expected = [];
|
||||
$expected[] = ['nid' => $this->nodes[2]->id()];
|
||||
$expected[] = ['nid' => $this->nodes[3]->id()];
|
||||
$this->assertIdenticalResultset($view, $expected, $this->map);
|
||||
$view->destroy();
|
||||
}
|
||||
|
@ -107,6 +135,7 @@ class ArgumentDateTimeTest extends DateTimeHandlerTestBase {
|
|||
$this->executeView($view, ['01']);
|
||||
$expected = [];
|
||||
$expected[] = ['nid' => $this->nodes[2]->id()];
|
||||
$expected[] = ['nid' => $this->nodes[3]->id()];
|
||||
$this->assertIdenticalResultset($view, $expected, $this->map);
|
||||
$view->destroy();
|
||||
}
|
||||
|
@ -152,6 +181,7 @@ class ArgumentDateTimeTest extends DateTimeHandlerTestBase {
|
|||
$this->executeView($view, ['01']);
|
||||
$expected = [];
|
||||
$expected[] = ['nid' => $this->nodes[2]->id()];
|
||||
$expected[] = ['nid' => $this->nodes[3]->id()];
|
||||
$this->assertIdenticalResultset($view, $expected, $this->map);
|
||||
$view->destroy();
|
||||
}
|
|
@ -0,0 +1,141 @@
|
|||
<?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;
|
||||
|
||||
/**
|
||||
* Base class for testing datetime handlers.
|
||||
*/
|
||||
abstract class DateTimeHandlerTestBase extends ViewsKernelTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['datetime_test', 'node', 'datetime', 'field'];
|
||||
|
||||
/**
|
||||
* Name of the field.
|
||||
*
|
||||
* Note, this is used in the default test view.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected static $field_name = 'field_date';
|
||||
|
||||
/**
|
||||
* Type of the field.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected static $field_type = 'datetime';
|
||||
|
||||
/**
|
||||
* Nodes to test.
|
||||
*
|
||||
* @var \Drupal\node\NodeInterface[]
|
||||
*/
|
||||
protected $nodes = [];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp($import_test_views = TRUE) {
|
||||
parent::setUp($import_test_views);
|
||||
|
||||
$this->installSchema('node', 'node_access');
|
||||
$this->installEntitySchema('node');
|
||||
$this->installEntitySchema('user');
|
||||
|
||||
// Add a date field to page nodes.
|
||||
$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();
|
||||
|
||||
// Views needs to be aware of the new field.
|
||||
$this->container->get('views.views_data')->clear();
|
||||
|
||||
// Set column map.
|
||||
$this->map = [
|
||||
'nid' => 'nid',
|
||||
];
|
||||
|
||||
// Load test views.
|
||||
ViewTestData::createTestViews(get_class($this), ['datetime_test']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the site timezone to a given timezone.
|
||||
*
|
||||
* @param string $timezone
|
||||
* The timezone identifier to set.
|
||||
*/
|
||||
protected function setSiteTimezone($timezone) {
|
||||
// Set an explicit site timezone, and disallow per-user timezones.
|
||||
$this->config('system.date')
|
||||
->set('timezone.user.configurable', 0)
|
||||
->set('timezone.default', $timezone)
|
||||
->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns UTC timestamp of user's TZ 'now'.
|
||||
*
|
||||
* The date field stores date_only values without conversion, considering them
|
||||
* already as UTC. This method returns the UTC equivalent of user's 'now' as a
|
||||
* unix timestamp, so they match using Y-m-d format.
|
||||
*
|
||||
* @return int
|
||||
* Unix timestamp.
|
||||
*/
|
||||
protected function getUTCEquivalentOfUserNowAsTimestamp() {
|
||||
$user_now = new DateTimePlus('now', new \DateTimeZone(drupal_get_user_timezone()));
|
||||
$utc_equivalent = new DateTimePlus($user_now->format('Y-m-d H:i:s'), new \DateTimeZone(DateTimeItemInterface::STORAGE_TIMEZONE));
|
||||
|
||||
return $utc_equivalent->getTimestamp();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array formatted date_only values relative to timestamp.
|
||||
*
|
||||
* @param int $timestamp
|
||||
* Unix Timestamp used as 'today'.
|
||||
*
|
||||
* @return array
|
||||
* An array of DateTimeItemInterface::DATE_STORAGE_FORMAT date values. In
|
||||
* order tomorrow, today and yesterday.
|
||||
*/
|
||||
protected function getRelativeDateValuesFromTimestamp($timestamp) {
|
||||
return [
|
||||
// Tomorrow.
|
||||
\Drupal::service('date.formatter')->format($timestamp + 86400, 'custom', DateTimeItemInterface::DATE_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE),
|
||||
// Today.
|
||||
\Drupal::service('date.formatter')->format($timestamp, 'custom', DateTimeItemInterface::DATE_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE),
|
||||
// Yesterday.
|
||||
\Drupal::service('date.formatter')->format($timestamp - 86400, 'custom', DateTimeItemInterface::DATE_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE),
|
||||
];
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\datetime\Kernel\Views;
|
||||
|
||||
use Drupal\Tests\SchemaCheckTestTrait;
|
||||
use Drupal\views\Views;
|
||||
|
||||
/**
|
||||
* Tests the Drupal\datetime\Plugin\views schemas.
|
||||
*
|
||||
* @group datetime
|
||||
*/
|
||||
class DateTimeSchemaTest extends DateTimeHandlerTestBase {
|
||||
|
||||
use SchemaCheckTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $testViews = ['test_argument_datetime', 'test_filter_datetime', 'test_sort_datetime'];
|
||||
|
||||
/**
|
||||
* Test argument plugin schema.
|
||||
*/
|
||||
public function testDateTimeSchema() {
|
||||
// Test argument schema.
|
||||
$view = Views::getView('test_argument_datetime');
|
||||
$view->initHandlers();
|
||||
$view->setDisplay('default');
|
||||
$arguments = $view->displayHandlers->get('default')->getOption('arguments');
|
||||
$arguments['field_date_value_year']['date'] = 'Date';
|
||||
$view->displayHandlers->get('default')->overrideOption('arguments', $arguments);
|
||||
$view->save();
|
||||
$this->assertConfigSchemaByName('views.view.test_argument_datetime');
|
||||
|
||||
// Test filter schema.
|
||||
$view = Views::getView('test_filter_datetime');
|
||||
$view->initHandlers();
|
||||
$filters = $view->displayHandlers->get('default')->getOption('filters');
|
||||
$filters['field_date_value']['type'] = 'Date';
|
||||
$view->save();
|
||||
$this->assertConfigSchemaByName('views.view.test_filter_datetime');
|
||||
|
||||
// Test sort schema.
|
||||
$view = Views::getView('test_sort_datetime');
|
||||
$view->initHandlers();
|
||||
$sorts = $view->displayHandlers->get('default')->getOption('sorts');
|
||||
$this->assertNotEmpty($sorts['field_date_value']['granularity']);
|
||||
$this->assertConfigSchemaByName('views.view.test_sort_datetime');
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,197 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\datetime\Kernel\Views;
|
||||
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
use Drupal\node\Entity\Node;
|
||||
use Drupal\views\Views;
|
||||
|
||||
/**
|
||||
* Tests date-only fields.
|
||||
*
|
||||
* @group datetime
|
||||
*/
|
||||
class FilterDateTest extends DateTimeHandlerTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $testViews = ['test_filter_datetime'];
|
||||
|
||||
/**
|
||||
* An array of timezone extremes to test.
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
protected static $timezones = [
|
||||
// UTC-12, no DST.
|
||||
'Pacific/Kwajalein',
|
||||
// UTC-11, no DST.
|
||||
'Pacific/Midway',
|
||||
// UTC-7, no DST.
|
||||
'America/Phoenix',
|
||||
// UTC.
|
||||
'UTC',
|
||||
// UTC+5:30, no DST.
|
||||
'Asia/Kolkata',
|
||||
// UTC+12, no DST.
|
||||
'Pacific/Funafuti',
|
||||
// UTC+13, no DST.
|
||||
'Pacific/Tongatapu',
|
||||
];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* Create nodes with relative dates of yesterday, today, and tomorrow.
|
||||
*/
|
||||
protected function setUp($import_test_views = TRUE) {
|
||||
parent::setUp($import_test_views);
|
||||
|
||||
// Change field storage to date-only.
|
||||
$storage = FieldStorageConfig::load('node.' . static::$field_name);
|
||||
$storage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
|
||||
$storage->save();
|
||||
|
||||
// Retrieve tomorrow, today and yesterday dates just to create the nodes.
|
||||
$timestamp = $this->getUTCEquivalentOfUserNowAsTimestamp();
|
||||
$dates = $this->getRelativeDateValuesFromTimestamp($timestamp);
|
||||
|
||||
// Clean the nodes on setUp.
|
||||
$this->nodes = [];
|
||||
foreach ($dates as $date) {
|
||||
$node = Node::create([
|
||||
'title' => $this->randomMachineName(8),
|
||||
'type' => 'page',
|
||||
'field_date' => [
|
||||
'value' => $date,
|
||||
],
|
||||
]);
|
||||
$node->save();
|
||||
$this->nodes[] = $node;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test offsets with date-only fields.
|
||||
*/
|
||||
public function testDateOffsets() {
|
||||
$view = Views::getView('test_filter_datetime');
|
||||
$field = static::$field_name . '_value';
|
||||
|
||||
foreach (static::$timezones as $timezone) {
|
||||
|
||||
$this->setSiteTimezone($timezone);
|
||||
$timestamp = $this->getUTCEquivalentOfUserNowAsTimestamp();
|
||||
$dates = $this->getRelativeDateValuesFromTimestamp($timestamp);
|
||||
$this->updateNodesDateFieldsValues($dates);
|
||||
|
||||
// Test simple operations.
|
||||
$view->initHandlers();
|
||||
|
||||
// A greater than or equal to 'now', should return the 'today' and the
|
||||
// 'tomorrow' node.
|
||||
$view->filter[$field]->operator = '>=';
|
||||
$view->filter[$field]->value['type'] = 'offset';
|
||||
$view->filter[$field]->value['value'] = 'now';
|
||||
$view->setDisplay('default');
|
||||
$this->executeView($view);
|
||||
$expected_result = [
|
||||
['nid' => $this->nodes[0]->id()],
|
||||
['nid' => $this->nodes[1]->id()],
|
||||
];
|
||||
$this->assertIdenticalResultset($view, $expected_result, $this->map);
|
||||
$view->destroy();
|
||||
|
||||
// Only dates in the past.
|
||||
$view->initHandlers();
|
||||
$view->filter[$field]->operator = '<';
|
||||
$view->filter[$field]->value['type'] = 'offset';
|
||||
$view->filter[$field]->value['value'] = 'now';
|
||||
$view->setDisplay('default');
|
||||
$this->executeView($view);
|
||||
$expected_result = [
|
||||
['nid' => $this->nodes[2]->id()],
|
||||
];
|
||||
$this->assertIdenticalResultset($view, $expected_result, $this->map);
|
||||
$view->destroy();
|
||||
|
||||
// Test offset for between operator. Only 'tomorrow' node should appear.
|
||||
$view->initHandlers();
|
||||
$view->filter[$field]->operator = 'between';
|
||||
$view->filter[$field]->value['type'] = 'offset';
|
||||
$view->filter[$field]->value['max'] = '+2 days';
|
||||
$view->filter[$field]->value['min'] = '+1 day';
|
||||
$view->setDisplay('default');
|
||||
$this->executeView($view);
|
||||
$expected_result = [
|
||||
['nid' => $this->nodes[0]->id()],
|
||||
];
|
||||
$this->assertIdenticalResultset($view, $expected_result, $this->map);
|
||||
$view->destroy();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test date filter with date-only fields.
|
||||
*/
|
||||
public function testDateIs() {
|
||||
$view = Views::getView('test_filter_datetime');
|
||||
$field = static::$field_name . '_value';
|
||||
|
||||
foreach (static::$timezones as $timezone) {
|
||||
|
||||
$this->setSiteTimezone($timezone);
|
||||
$timestamp = $this->getUTCEquivalentOfUserNowAsTimestamp();
|
||||
$dates = $this->getRelativeDateValuesFromTimestamp($timestamp);
|
||||
$this->updateNodesDateFieldsValues($dates);
|
||||
|
||||
// Test simple operations.
|
||||
$view->initHandlers();
|
||||
|
||||
// Filtering with nodes date-only values (format: Y-m-d) to test UTC
|
||||
// conversion does NOT change the day.
|
||||
$view->filter[$field]->operator = '=';
|
||||
$view->filter[$field]->value['type'] = 'date';
|
||||
$view->filter[$field]->value['value'] = $this->nodes[2]->field_date->first()->getValue()['value'];
|
||||
$view->setDisplay('default');
|
||||
$this->executeView($view);
|
||||
$expected_result = [
|
||||
['nid' => $this->nodes[2]->id()],
|
||||
];
|
||||
$this->assertIdenticalResultset($view, $expected_result, $this->map);
|
||||
$view->destroy();
|
||||
|
||||
// Test offset for between operator. Only 'today' and 'tomorrow' nodes
|
||||
// should appear.
|
||||
$view->initHandlers();
|
||||
$view->filter[$field]->operator = 'between';
|
||||
$view->filter[$field]->value['type'] = 'date';
|
||||
$view->filter[$field]->value['max'] = $this->nodes[0]->field_date->first()->getValue()['value'];
|
||||
$view->filter[$field]->value['min'] = $this->nodes[1]->field_date->first()->getValue()['value'];
|
||||
$view->setDisplay('default');
|
||||
$this->executeView($view);
|
||||
$expected_result = [
|
||||
['nid' => $this->nodes[0]->id()],
|
||||
['nid' => $this->nodes[1]->id()],
|
||||
];
|
||||
$this->assertIdenticalResultset($view, $expected_result, $this->map);
|
||||
$view->destroy();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates tests nodes date fields values.
|
||||
*
|
||||
* @param array $dates
|
||||
* An array of DATETIME_DATE_STORAGE_FORMAT date values.
|
||||
*/
|
||||
protected function updateNodesDateFieldsValues(array $dates) {
|
||||
foreach ($dates as $index => $date) {
|
||||
$this->nodes[$index]->{static::$field_name}->value = $date;
|
||||
$this->nodes[$index]->save();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,7 +1,9 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\datetime\Tests\Views;
|
||||
namespace Drupal\Tests\datetime\Kernel\Views;
|
||||
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
|
||||
use Drupal\node\Entity\Node;
|
||||
use Drupal\views\Views;
|
||||
|
||||
/**
|
||||
|
@ -23,19 +25,24 @@ class FilterDateTimeTest extends DateTimeHandlerTestBase {
|
|||
|
||||
/**
|
||||
* Use a non-UTC timezone.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected static $timezone = 'America/Vancouver';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
protected function setUp($import_test_views = TRUE) {
|
||||
parent::setUp($import_test_views);
|
||||
|
||||
static::$date = REQUEST_TIME + 86400;
|
||||
|
||||
// Set the timezone.
|
||||
date_default_timezone_set(static::$timezone);
|
||||
$this->config('system.date')
|
||||
->set('timezone.default', static::$timezone)
|
||||
->save();
|
||||
|
||||
// Add some basic test nodes.
|
||||
$dates = [
|
||||
|
@ -44,14 +51,18 @@ class FilterDateTimeTest extends DateTimeHandlerTestBase {
|
|||
'2002-10-10T14:14:14',
|
||||
// The date storage timezone is used (this mimics the steps taken in the
|
||||
// widget: \Drupal\datetime\Plugin\Field\FieldWidget::messageFormValues().
|
||||
\Drupal::service('date.formatter')->format(static::$date, 'custom', DATETIME_DATETIME_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE),
|
||||
\Drupal::service('date.formatter')->format(static::$date, 'custom', DateTimeItemInterface::DATETIME_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE),
|
||||
];
|
||||
foreach ($dates as $date) {
|
||||
$this->nodes[] = $this->drupalCreateNode([
|
||||
$node = Node::create([
|
||||
'title' => $this->randomMachineName(8),
|
||||
'type' => 'page',
|
||||
'field_date' => [
|
||||
'value' => $date,
|
||||
]
|
||||
],
|
||||
]);
|
||||
$node->save();
|
||||
$this->nodes[] = $node;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -176,7 +187,7 @@ class FilterDateTimeTest extends DateTimeHandlerTestBase {
|
|||
$view->filter[$field]->value['max'] = '';
|
||||
// Use the date from node 3. Use the site timezone (mimics a value entered
|
||||
// through the UI).
|
||||
$view->filter[$field]->value['value'] = \Drupal::service('date.formatter')->format(static::$date, 'custom', DATETIME_DATETIME_STORAGE_FORMAT, static::$timezone);
|
||||
$view->filter[$field]->value['value'] = \Drupal::service('date.formatter')->format(static::$date, 'custom', DateTimeItemInterface::DATETIME_STORAGE_FORMAT, static::$timezone);
|
||||
$view->setDisplay('default');
|
||||
$this->executeView($view);
|
||||
$expected_result = [
|
|
@ -1,7 +1,8 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\datetime\Tests\Views;
|
||||
namespace Drupal\Tests\datetime\Kernel\Views;
|
||||
|
||||
use Drupal\node\Entity\Node;
|
||||
use Drupal\views\Views;
|
||||
|
||||
/**
|
||||
|
@ -19,8 +20,8 @@ class SortDateTimeTest extends DateTimeHandlerTestBase {
|
|||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
protected function setUp($import_test_views = TRUE) {
|
||||
parent::setUp($import_test_views);
|
||||
|
||||
// Add some basic test nodes.
|
||||
$dates = [
|
||||
|
@ -33,11 +34,15 @@ class SortDateTimeTest extends DateTimeHandlerTestBase {
|
|||
'2000-10-10T00:03:03',
|
||||
];
|
||||
foreach ($dates as $date) {
|
||||
$this->nodes[] = $this->drupalCreateNode([
|
||||
$node = Node::create([
|
||||
'title' => $this->randomMachineName(8),
|
||||
'type' => 'page',
|
||||
'field_date' => [
|
||||
'value' => $date,
|
||||
]
|
||||
],
|
||||
]);
|
||||
$node->save();
|
||||
$this->nodes[] = $node;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\datetime\Unit\Plugin\migrate\field;
|
||||
|
||||
/**
|
||||
* @group migrate
|
||||
* @group legacy
|
||||
*/
|
||||
class DateFieldLegacyTest extends DateFieldTest {
|
||||
|
||||
/**
|
||||
* @expectedDeprecation Deprecated in Drupal 8.6.0, to be removed before Drupal 9.0.0. Use defineValueProcessPipeline() instead. See https://www.drupal.org/node/2944598.
|
||||
*/
|
||||
public function testUnknownDateType($method = 'processFieldValues') {
|
||||
parent::testUnknownDateType($method);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\datetime\Unit\Plugin\migrate\field;
|
||||
|
||||
use Drupal\datetime\Plugin\migrate\field\DateField;
|
||||
use Drupal\migrate\MigrateException;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
|
||||
/**
|
||||
* @group migrate
|
||||
*/
|
||||
class DateFieldTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* Tests an Exception is thrown when the field type is not a known date type.
|
||||
*/
|
||||
public function testUnknownDateType($method = 'defineValueProcessPipeline') {
|
||||
$migration = $this->prophesize('Drupal\migrate\Plugin\MigrationInterface')->reveal();
|
||||
$plugin = new DateField([], '', []);
|
||||
|
||||
$this->setExpectedException(MigrateException::class, "Field field_date of type 'timestamp' is an unknown date field type.");
|
||||
$plugin->$method($migration, 'field_date', ['type' => 'timestamp']);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\datetime\Unit\Plugin\migrate\field\d6;
|
||||
|
||||
/**
|
||||
* @group migrate
|
||||
* @group legacy
|
||||
*/
|
||||
class DateFieldLegacyTest extends DateFieldTest {
|
||||
|
||||
/**
|
||||
* @expectedDeprecation Deprecated in Drupal 8.6.0, to be removed before Drupal 9.0.0. Use defineValueProcessPipeline() instead. See https://www.drupal.org/node/2944598.
|
||||
*/
|
||||
public function testUnknownDateType($method = 'processFieldValues') {
|
||||
parent::testUnknownDateType($method);
|
||||
}
|
||||
|
||||
}
|
|
@ -4,32 +4,39 @@ namespace Drupal\Tests\datetime\Unit\Plugin\migrate\field\d6;
|
|||
|
||||
use Drupal\datetime\Plugin\migrate\field\d6\DateField;
|
||||
use Drupal\migrate\MigrateException;
|
||||
use Drupal\migrate\Plugin\MigrationInterface;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
|
||||
/**
|
||||
* @group migrate
|
||||
* @group legacy
|
||||
*/
|
||||
class DateFieldTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* @var \Drupal\migrate_drupal\Plugin\MigrateFieldInterface
|
||||
*/
|
||||
protected $plugin;
|
||||
|
||||
/**
|
||||
* @var \Drupal\migrate\Plugin\MigrationInterface
|
||||
*/
|
||||
protected $migration;
|
||||
|
||||
/**
|
||||
* Tests an Exception is thrown when the field type is not a known date type.
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function testUnknownDateType() {
|
||||
$this->migration = $this->prophesize('Drupal\migrate\Plugin\MigrationInterface')->reveal();
|
||||
$this->plugin = new DateField([], '', []);
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->migration = $this->prophesize(MigrationInterface::class)->reveal();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests an Exception is thrown when the field type is not a known date type.
|
||||
*
|
||||
* @runInSeparateProcess
|
||||
* @expectedDeprecation DateField is deprecated in Drupal 8.4.x and will be removed before Drupal 9.0.x. Use \Drupal\datetime\Plugin\migrate\field\DateField instead.
|
||||
*/
|
||||
public function testUnknownDateType($method = 'defineValueProcessPipeline') {
|
||||
$plugin = new DateField([], '', []);
|
||||
|
||||
$this->setExpectedException(MigrateException::class, "Field field_date of type 'timestamp' is an unknown date field type.");
|
||||
$this->plugin->processFieldValues($this->migration, 'field_date', ['type' => 'timestamp']);
|
||||
$plugin->$method($this->migration, 'field_date', ['type' => 'timestamp']);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Reference in a new issue