Initial Drupal 11 with DDEV setup

This commit is contained in:
gluebox
2025-10-08 11:39:17 -04:00
commit 89ef74b305
25344 changed files with 2599172 additions and 0 deletions

View File

@ -0,0 +1,7 @@
name: 'Datetime test'
type: module
description: 'Provides default views for tests.'
package: Testing
version: VERSION
dependencies:
- drupal:views

View File

@ -0,0 +1,140 @@
langcode: und
status: true
dependencies: { }
id: test_argument_datetime
label: test_argument_datetime
module: views
description: ''
tag: ''
base_table: node_field_data
base_field: nid
display:
default:
display_options:
defaults:
fields: false
pager: false
sorts: false
arguments:
field_date_value_year:
field: field_date_value_year
id: field_date_value
table: node__field_date
plugin_id: datetime_year
fields:
id:
field: nid
id: nid
relationship: none
table: node_field_data
plugin_id: numeric
pager:
options:
offset: 0
type: none
sorts:
id:
field: nid
id: nid
order: ASC
relationship: none
table: node_field_data
plugin_id: numeric
display_plugin: default
display_title: Default
id: default
position: 0
embed_1:
display_options:
defaults:
arguments: false
arguments:
field_date_value_month:
field: field_date_value_month
id: field_date_value
table: node__field_date
plugin_id: datetime_month
display_plugin: embed
id: embed_1
display_title: ''
position: null
embed_2:
display_options:
defaults:
arguments: false
arguments:
field_date_value_day:
field: field_date_value_day
id: field_date_value
table: node__field_date
plugin_id: datetime_day
display_plugin: embed
id: embed_2
display_title: ''
position: null
embed_3:
display_options:
defaults:
arguments: false
arguments:
field_date_value_year:
field: field_date_value_year
id: field_date_value
table: node__field_date
plugin_id: datetime_year
field_date_value_month:
field: field_date_value_month
id: field_date_value
table: node__field_date
plugin_id: datetime_month
field_date_value_day:
field: field_date_value_day
id: field_date_value
table: node__field_date
plugin_id: datetime_day
display_plugin: embed
id: embed_2
display_title: ''
position: null
embed_4:
display_options:
defaults:
arguments: false
arguments:
field_date_value_day:
field: field_date_value_week
id: field_date_value
table: node__field_date
plugin_id: datetime_week
display_plugin: embed
id: embed_4
display_title: ''
position: null
embed_5:
display_options:
defaults:
arguments: false
arguments:
field_date_value_day:
field: field_date_value_full_date
id: field_date_value
table: node__field_date
plugin_id: datetime_full_date
display_plugin: embed
id: embed_5
display_title: ''
position: null
embed_6:
display_options:
defaults:
arguments: false
arguments:
field_date_value_day:
field: field_date_value_year_month
id: field_date_value
table: node__field_date
plugin_id: datetime_year_month
display_plugin: embed
id: embed_6
display_title: ''
position: null

View File

@ -0,0 +1,125 @@
langcode: und
status: true
dependencies:
module:
- datetime
- node
id: test_exposed_filter_datetime
label: test_exposed_filter_datetime
module: views
description: ''
tag: ''
base_table: node_field_data
base_field: nid
display:
default:
display_options:
access:
type: none
cache:
type: none
exposed_form:
type: basic
fields:
nid:
field: nid
id: nid
table: node_field_data
plugin_id: node
filters:
field_date_value:
id: field_date_value
table: node__field_date
field: field_date_value
relationship: none
group_type: group
admin_label: ''
operator: '='
value:
min: ''
max: ''
value: ''
type: date
group: 1
exposed: true
expose:
operator_id: field_date_value_op
label: 'field_date (field_date)'
description: ''
use_operator: true
operator: field_date_value_op
operator_limit_selection: true
operator_list:
'=': '='
'!=': '!='
identifier: field_date_value
required: false
remember: false
multiple: false
remember_roles:
authenticated: authenticated
placeholder: ''
min_placeholder: ''
max_placeholder: ''
is_grouped: false
group_info:
label: ''
description: ''
identifier: ''
optional: true
widget: select
multiple: false
remember: false
default_group: All
default_group_multiple: { }
group_items: { }
plugin_id: datetime
sorts:
id:
field: nid
id: nid
order: ASC
relationship: none
table: node_field_data
plugin_id: numeric
pager:
type: full
query:
options:
query_comment: ''
type: views_query
style:
type: default
row:
type: fields
display_extenders: { }
display_plugin: default
display_title: Default
id: default
position: 0
cache_metadata:
max-age: -1
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url
- url.query_args
- 'user.node_grants:view'
tags: { }
page_1:
display_plugin: page
id: page_1
display_title: Page
position: 1
display_options:
display_extenders: { }
path: test_exposed_filter_datetime
cache_metadata:
max-age: -1
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url
- url.query_args
- 'user.node_grants:view'
tags: { }

View File

@ -0,0 +1,55 @@
langcode: und
status: true
dependencies:
module:
- node
id: test_filter_datetime
label: test_filter_datetime
module: views
description: ''
tag: ''
base_table: node_field_data
base_field: nid
display:
default:
display_options:
access:
type: none
cache:
type: none
exposed_form:
type: basic
fields:
nid:
field: nid
id: nid
table: node_field_data
plugin_id: node
filters:
field_date_value:
id: field_date_value
table: node__field_date
field: field_date_value
plugin_id: datetime
sorts:
id:
field: nid
id: nid
order: ASC
relationship: none
table: node_field_data
plugin_id: numeric
pager:
type: full
query:
options:
query_comment: ''
type: views_query
style:
type: default
row:
type: fields
display_plugin: default
display_title: Default
id: default
position: 0

View File

@ -0,0 +1,57 @@
langcode: und
status: true
dependencies:
module:
- node
id: test_sort_datetime
label: test_sort_datetime
module: views
description: ''
tag: ''
base_table: node_field_data
base_field: nid
display:
default:
display_options:
access:
type: none
cache:
type: none
exposed_form:
type: basic
fields:
nid:
field: nid
id: nid
table: node_field_data
plugin_id: node
sorts:
field_date_value:
field: field_date_value
id: field_date_value
relationship: none
table: node__field_date
order: DESC
granularity: second
plugin_id: datetime
id:
field: nid
id: nid
order: ASC
relationship: none
table: node_field_data
plugin_id: numeric
pager:
type: full
query:
options:
query_comment: ''
type: views_query
style:
type: default
row:
type: fields
display_plugin: default
display_title: Default
id: default
position: 0

View File

@ -0,0 +1,136 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\datetime\Functional;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\views\Functional\ViewTestBase;
/**
* Test exposed datetime filters functionality.
*
* @group views
* @see \Drupal\datetime\Plugin\views\filter\Date
*/
class DateFilterTest extends ViewTestBase {
/**
* A user with permission to administer views.
*
* @var \Drupal\user\Entity\User
*/
protected $adminUser;
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = ['test_exposed_filter_datetime'];
/**
* {@inheritdoc}
*/
protected static $modules = [
'datetime_test',
'node',
'datetime',
'field',
'views_ui',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected function setUp($import_test_views = TRUE, $modules = ['datetime_test']): void {
parent::setUp($import_test_views, $modules);
// Add a date field to page nodes.
$node_type = NodeType::create([
'type' => 'page',
'name' => 'page',
]);
$node_type->save();
$fieldStorage = FieldStorageConfig::create([
'field_name' => 'field_date',
'entity_type' => 'node',
'type' => 'datetime',
'settings' => ['datetime_type' => DateTimeItem::DATETIME_TYPE_DATETIME],
]);
$fieldStorage->save();
$field = FieldConfig::create([
'field_storage' => $fieldStorage,
'bundle' => 'page',
'required' => TRUE,
]);
$field->save();
$this->adminUser = $this->drupalCreateUser(['administer views']);
$this->drupalLogin($this->adminUser);
$this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
}
/**
* Tests the limit of the expose operator functionality.
*/
public function testLimitExposedOperators(): void {
$edit = [];
$this->drupalGet('test_exposed_filter_datetime');
$this->assertSession()->statusCodeEquals(200);
// Ensure that invalid date format entries in the exposed filter are validated
$edit = ['edit-field-date-value-value' => 'lun 2018-04-27'];
$this->submitForm($edit, 'Apply');
$this->assertSession()->pageTextContains('Invalid date format.');
$this->assertSession()->pageTextNotContains('Exception: DateTime object not set.');
$this->assertSession()->optionExists('edit-field-date-value-op', '=');
$this->assertSession()->optionNotExists('edit-field-date-value-op', '>');
$this->assertSession()->optionNotExists('edit-field-date-value-op', '>=');
// Because there are not operators that use the min and max fields, those
// fields should not be in the exposed form.
$this->assertSession()->fieldExists('edit-field-date-value-value');
$this->assertSession()->fieldNotExists('edit-field-date-value-min');
$this->assertSession()->fieldNotExists('edit-field-date-value-max');
$edit = [];
$edit['options[operator]'] = '>';
$edit['options[expose][operator_list][]'] = ['>', '>=', 'between'];
$this->drupalGet('admin/structure/views/nojs/handler/test_exposed_filter_datetime/default/filter/field_date_value');
$this->submitForm($edit, 'Apply');
$this->drupalGet('admin/structure/views/view/test_exposed_filter_datetime/edit/default');
$this->submitForm([], 'Save');
$this->drupalGet('test_exposed_filter_datetime');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->optionNotExists('edit-field-date-value-op', '<');
$this->assertSession()->optionNotExists('edit-field-date-value-op', '<=');
$this->assertSession()->optionNotExists('edit-field-date-value-op', '=');
$this->assertSession()->optionExists('edit-field-date-value-op', '>');
$this->assertSession()->optionExists('edit-field-date-value-op', '>=');
$this->assertSession()->fieldExists('edit-field-date-value-value');
$this->assertSession()->fieldExists('edit-field-date-value-min');
$this->assertSession()->fieldExists('edit-field-date-value-max');
// Set the default to an excluded operator.
$edit = [];
$edit['options[operator]'] = '=';
$edit['options[expose][operator_list][]'] = ['<', '>'];
$this->drupalGet('admin/structure/views/nojs/handler/test_exposed_filter_datetime/default/filter/field_date_value');
$this->submitForm($edit, 'Apply');
$this->assertSession()->pageTextContains('You selected the "Is equal to" operator as the default value but is not included in the list of limited operators.');
}
}

View File

@ -0,0 +1,211 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\datetime\Functional;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\BrowserTestBase;
/**
* Provides a base class for testing Datetime field functionality.
*/
abstract class DateTestBase extends BrowserTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['node', 'entity_test', 'datetime', 'field_ui'];
/**
* An array of display options.
*
* An array of display options to pass to
* EntityDisplayRepositoryInterface::getViewDisplay()
*
* @var array
*
* @see \Drupal\Core\Entity\EntityDisplayRepositoryInterface::getViewDisplay()
*/
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;
/**
* The date formatter service.
*
* @var \Drupal\Core\Datetime\DateFormatterInterface
*/
protected $dateFormatter;
/**
* 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',
];
/**
* Returns the type of field to be tested.
*
* @return string
* The field type to be tested.
*/
abstract protected function getTestFieldType();
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$web_user = $this->drupalCreateUser([
'access content',
'view test entity',
'administer entity_test content',
'administer entity_test form display',
'administer content types',
'bypass node access',
'administer node fields',
'administer node form display',
'administer node display',
]);
$this->drupalLogin($web_user);
// Create a field with settings to validate.
$this->createField();
$this->dateFormatter = $this->container->get('date.formatter');
}
/**
* Creates a date test field.
*/
protected function createField() {
$field_name = $this->randomMachineName();
$field_label = Unicode::ucfirst($this->randomMachineName());
$type = $this->getTestFieldType();
$widget_type = $formatter_type = $type . '_default';
$this->fieldStorage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test',
'type' => $type,
'settings' => ['datetime_type' => DateTimeItem::DATETIME_TYPE_DATE],
]);
$this->fieldStorage->save();
$this->field = FieldConfig::create([
'field_storage' => $this->fieldStorage,
'label' => $field_label,
'bundle' => 'entity_test',
'description' => 'Description for ' . $field_label,
'required' => TRUE,
]);
$this->field->save();
EntityFormDisplay::load('entity_test.entity_test.default')
->setComponent($field_name, ['type' => $widget_type])
->save();
$this->displayOptions = [
'type' => $formatter_type,
'label' => 'hidden',
'settings' => ['format_type' => 'medium'],
];
EntityViewDisplay::create([
'targetEntityType' => $this->field->getTargetEntityTypeId(),
'bundle' => $this->field->getTargetBundle(),
'mode' => 'full',
'status' => TRUE,
])->setComponent($field_name, $this->displayOptions)
->save();
}
/**
* Renders an entity_test and sets the output in the internal browser.
*
* @param string|int $id
* The entity_test ID to render.
* @param string $view_mode
* (optional) The view mode to use for rendering. Defaults to 'full'.
* @param bool $reset
* (optional) Whether to reset the entity_test controller cache. Defaults to
* TRUE to simplify testing.
*
* @return string
* The rendered HTML output.
*/
protected function renderTestEntity($id, $view_mode = 'full', $reset = TRUE) {
if ($reset) {
$this->container->get('entity_type.manager')->getStorage('entity_test')->resetCache([$id]);
}
$entity = EntityTest::load($id);
$display = EntityViewDisplay::collectRenderDisplay($entity, $view_mode);
$build = $display->build($entity);
return (string) $this->container->get('renderer')->renderRoot($build);
}
/**
* 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();
}
/**
* 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();
}
}
}

View File

@ -0,0 +1,938 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\datetime\Functional;
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;
use Drupal\node\Entity\Node;
use Drupal\Core\Form\FormState;
/**
* Tests Datetime field functionality.
*
* @group datetime
*/
class DateTimeFieldTest extends DateTestBase {
/**
* The default display settings to use for the formatters.
*
* @var array
*/
protected $defaultSettings = ['timezone_override' => ''];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected function getTestFieldType(): string {
return 'datetime';
}
/**
* Tests date field functionality.
*/
public function testDateField(): void {
$field_name = $this->fieldStorage->getName();
$display_repository = \Drupal::service('entity_display.repository');
// Loop through defined timezones to test that date-only fields work at the
// extremes.
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');
$this->assertSession()->fieldValueEquals("{$field_name}[0][value][date]", '');
$this->assertSession()->elementExists('xpath', '//*[@id="edit-' . $field_name . '-wrapper"]//label[contains(@class,"js-form-required")]');
$this->assertSession()->fieldNotExists("{$field_name}[0][value][time]");
// ARIA described-by.
$this->assertSession()->elementExists('xpath', '//input[@aria-describedby="edit-' . $field_name . '-0-value--description"]');
$this->assertSession()->elementExists('xpath', '//div[@id="edit-' . $field_name . '-0-value--description"]');
// Build up a date in the UTC timezone. Note that using this will also
// 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, DateTimeItemInterface::STORAGE_TIMEZONE);
// Submit a valid date and ensure it is accepted.
$date_format = DateFormat::load('html_date')->getPattern();
$time_format = DateFormat::load('html_time')->getPattern();
$edit = [
"{$field_name}[0][value][date]" => $date->format($date_format),
];
$this->submitForm($edit, 'Save');
preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
$id = $match[1];
$this->assertSession()->pageTextContains('entity_test ' . $id . ' has been created.');
$this->assertSession()->responseContains($date->format($date_format));
$this->assertSession()->responseNotContains($date->format($time_format));
// Verify the date doesn't change if using a timezone that is UTC+12 when
// the entity is edited through the form.
$entity = EntityTest::load($id);
$this->assertEquals('2012-12-31', $entity->{$field_name}->value);
$this->drupalGet('entity_test/manage/' . $id . '/edit');
$this->submitForm([], 'Save');
$this->drupalGet('entity_test/manage/' . $id . '/edit');
$this->submitForm([], 'Save');
$this->drupalGet('entity_test/manage/' . $id . '/edit');
$this->submitForm([], 'Save');
$entity = EntityTest::load($id);
$this->assertEquals('2012-12-31', $entity->{$field_name}->value);
// Reset display options since these get changed below.
$this->displayOptions = [
'type' => 'datetime_default',
'label' => 'hidden',
'settings' => ['format_type' => 'medium'] + $this->defaultSettings,
];
// Verify that the date is output according to the formatter settings.
$options = [
'format_type' => ['short', 'medium', 'long'],
];
// Formats that display a time component for date-only fields will display
// the default time, so that is applied before calculating the expected
// value.
$this->massageTestDate($date);
foreach ($options as $setting => $values) {
foreach ($values as $new_value) {
// Update the entity display settings.
$this->displayOptions['settings'] = [$setting => $new_value] + $this->defaultSettings;
$this->container->get('entity_display.repository')
->getViewDisplay($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
->setComponent($field_name, $this->displayOptions)
->save();
$this->renderTestEntity($id);
switch ($setting) {
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.
/** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
$date_formatter = $this->container->get('date.formatter');
$expected = $date_formatter->format($date->getTimestamp(), $new_value, '', DateTimeItemInterface::STORAGE_TIMEZONE);
$expected_iso = $date_formatter->format($date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', DateTimeItemInterface::STORAGE_TIMEZONE);
$output = $this->renderTestEntity($id);
$expected_markup = '<time datetime="' . $expected_iso . '">' . $expected . '</time>';
$this->assertStringContainsString($expected_markup, $output, "Formatted date field using $new_value format displayed as $expected with $expected_iso attribute in $timezone.");
break;
}
}
}
// Verify that the plain formatter works.
$this->displayOptions['type'] = 'datetime_plain';
$this->displayOptions['settings'] = $this->defaultSettings;
$display_repository
->getViewDisplay($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
->setComponent($field_name, $this->displayOptions)
->save();
$expected = $date->format(DateTimeItemInterface::DATE_STORAGE_FORMAT);
$output = $this->renderTestEntity($id);
$this->assertStringContainsString($expected, $output, "Formatted date field using plain format displayed as $expected in $timezone.");
// Verify that the 'datetime_custom' formatter works.
$this->displayOptions['type'] = 'datetime_custom';
$this->displayOptions['settings'] = ['date_format' => 'm/d/Y'] + $this->defaultSettings;
$display_repository
->getViewDisplay($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
->setComponent($field_name, $this->displayOptions)
->save();
$expected = $date->format($this->displayOptions['settings']['date_format']);
$output = $this->renderTestEntity($id);
$this->assertStringContainsString($expected, $output, "Formatted date field using datetime_custom format displayed as $expected in $timezone.");
// Test that allowed markup in custom format is preserved and XSS is
// removed.
$this->displayOptions['settings']['date_format'] = '\\<\\s\\t\\r\\o\\n\\g\\>m/d/Y\\<\\/\\s\\t\\r\\o\\n\\g\\>\\<\\s\\c\\r\\i\\p\\t\\>\\a\\l\\e\\r\\t\\(\\S\\t\\r\\i\\n\\g\\.\\f\\r\\o\\m\\C\\h\\a\\r\\C\\o\\d\\e\\(\\8\\8\\,\\8\\3\\,\\8\\3\\)\\)\\<\\/\\s\\c\\r\\i\\p\\t\\>';
$display_repository->getViewDisplay($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
->setComponent($field_name, $this->displayOptions)
->save();
$expected = '<strong>' . $date->format('m/d/Y') . '</strong>alert(String.fromCharCode(88,83,83))';
$output = $this->renderTestEntity($id);
$this->assertStringContainsString($expected, $output, "Formatted date field using daterange_custom format displayed as $expected in $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 timezone.
$timestamp = \Drupal::time()->getRequestTime() - 87654321;
$entity = EntityTest::load($id);
$field_name = $this->fieldStorage->getName();
$date = DrupalDateTime::createFromTimestamp($timestamp, 'UTC');
$entity->{$field_name}->value = $date->format($date_format);
$entity->save();
$this->displayOptions['type'] = 'datetime_time_ago';
$this->displayOptions['settings'] = [
'future_format' => '@interval in the future',
'past_format' => '@interval in the past',
'granularity' => 3,
];
$display_repository->getViewDisplay($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
->setComponent($field_name, $this->displayOptions)
->save();
$expected = str_replace(
'@interval',
$this->dateFormatter->formatTimeDiffSince(
$timestamp,
['granularity' => $this->displayOptions['settings']['granularity']]),
$this->displayOptions['settings']['past_format']
);
$output = $this->renderTestEntity($id);
$this->assertStringContainsString($expected, $output, "Formatted date field using datetime_time_ago format displayed as $expected in $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 timezone.
$timestamp = \Drupal::time()->getRequestTime() + 87654321;
$entity = EntityTest::load($id);
$field_name = $this->fieldStorage->getName();
$date = DrupalDateTime::createFromTimestamp($timestamp, 'UTC');
$entity->{$field_name}->value = $date->format($date_format);
$entity->save();
$display_repository->getViewDisplay($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
->setComponent($field_name, $this->displayOptions)
->save();
$expected = str_replace(
'@interval',
$this->dateFormatter->formatTimeDiffUntil(
$timestamp,
['granularity' => $this->displayOptions['settings']['granularity']]),
$this->displayOptions['settings']['future_format']
);
$output = $this->renderTestEntity($id);
$this->assertStringContainsString($expected, $output, "Formatted date field using datetime_time_ago format displayed as $expected in $timezone.");
}
}
/**
* Tests date and time field.
*/
public function testDatetimeField(): void {
$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();
// Display creation form.
$this->drupalGet('entity_test/add');
$this->assertSession()->fieldValueEquals("{$field_name}[0][value][date]", '');
$this->assertSession()->fieldValueEquals("{$field_name}[0][value][time]", '');
$this->assertSession()->elementTextContains('xpath', '//fieldset[@id="edit-' . $field_name . '-0"]/legend', $field_label);
$this->assertSession()->elementExists('xpath', '//fieldset[@aria-describedby="edit-' . $field_name . '-0--description"]');
$this->assertSession()->elementExists('xpath', '//div[@id="edit-' . $field_name . '-0--description"]');
// Build up a date in the UTC timezone.
$value = '2012-12-31 00:00:00';
$date = new DrupalDateTime($value, 'UTC');
// Update the timezone to the system default.
$date->setTimezone(timezone_open(date_default_timezone_get()));
// Submit a valid date and ensure it is accepted.
$date_format = DateFormat::load('html_date')->getPattern();
$time_format = DateFormat::load('html_time')->getPattern();
$edit = [
"{$field_name}[0][value][date]" => $date->format($date_format),
"{$field_name}[0][value][time]" => $date->format($time_format),
];
$this->submitForm($edit, 'Save');
preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
$id = $match[1];
$this->assertSession()->pageTextContains('entity_test ' . $id . ' has been created.');
$this->assertSession()->responseContains($date->format($date_format));
$this->assertSession()->responseContains($date->format($time_format));
/** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
$display_repository = \Drupal::service('entity_display.repository');
// Verify that the date is output according to the formatter settings.
$options = [
'format_type' => ['short', 'medium', 'long'],
];
foreach ($options as $setting => $values) {
foreach ($values as $new_value) {
// Update the entity display settings.
$this->displayOptions['settings'] = [$setting => $new_value] + $this->defaultSettings;
$display_repository->getViewDisplay($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
->setComponent($field_name, $this->displayOptions)
->save();
$this->renderTestEntity($id);
switch ($setting) {
case 'format_type':
// Verify that a date is displayed.
$date_formatter = $this->container->get('date.formatter');
$expected = $date_formatter->format($date->getTimestamp(), $new_value);
$expected_iso = $date_formatter->format($date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', 'UTC');
$output = $this->renderTestEntity($id);
$expected_markup = '<time datetime="' . $expected_iso . '">' . $expected . '</time>';
$this->assertStringContainsString($expected_markup, $output, "Formatted date field using $new_value format displayed as $expected with $expected_iso attribute.");
break;
}
}
}
// Verify that the plain formatter works.
$this->displayOptions['type'] = 'datetime_plain';
$this->displayOptions['settings'] = $this->defaultSettings;
$display_repository
->getViewDisplay($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
->setComponent($field_name, $this->displayOptions)
->save();
$expected = $date->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
$output = $this->renderTestEntity($id);
$this->assertStringContainsString($expected, $output, "Formatted date field using plain format displayed as $expected.");
// Verify that the 'datetime_custom' formatter works.
$this->displayOptions['type'] = 'datetime_custom';
$this->displayOptions['settings'] = ['date_format' => 'm/d/Y g:i:s A'] + $this->defaultSettings;
$display_repository->getViewDisplay($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
->setComponent($field_name, $this->displayOptions)
->save();
$expected = $date->format($this->displayOptions['settings']['date_format']);
$output = $this->renderTestEntity($id);
$this->assertStringContainsString($expected, $output, "Formatted date field using datetime_custom format displayed as $expected.");
// Verify that the 'timezone_override' setting works.
$this->displayOptions['type'] = 'datetime_custom';
$this->displayOptions['settings'] = ['date_format' => 'm/d/Y g:i:s A', 'timezone_override' => 'America/New_York'] + $this->defaultSettings;
$display_repository->getViewDisplay($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
->setComponent($field_name, $this->displayOptions)
->save();
$expected = $date->format($this->displayOptions['settings']['date_format'], ['timezone' => 'America/New_York']);
$output = $this->renderTestEntity($id);
$this->assertStringContainsString($expected, $output, "Formatted date field using datetime_custom format displayed as $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 timezone.
$timestamp = \Drupal::time()->getRequestTime() - 87654321;
$entity = EntityTest::load($id);
$field_name = $this->fieldStorage->getName();
$date = DrupalDateTime::createFromTimestamp($timestamp, 'UTC');
$entity->{$field_name}->value = $date->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
$entity->save();
$this->displayOptions['type'] = 'datetime_time_ago';
$this->displayOptions['settings'] = [
'future_format' => '@interval from now',
'past_format' => '@interval earlier',
'granularity' => 3,
];
$display_repository->getViewDisplay($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
->setComponent($field_name, $this->displayOptions)
->save();
$expected = str_replace(
'@interval',
$this->dateFormatter->formatTimeDiffSince(
$timestamp,
['granularity' => $this->displayOptions['settings']['granularity']]),
$this->displayOptions['settings']['past_format']
);
$output = $this->renderTestEntity($id);
$this->assertStringContainsString($expected, $output, "Formatted date field using datetime_time_ago format displayed as $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 timezone.
$timestamp = \Drupal::time()->getRequestTime() + 87654321;
$entity = EntityTest::load($id);
$field_name = $this->fieldStorage->getName();
$date = DrupalDateTime::createFromTimestamp($timestamp, 'UTC');
$entity->{$field_name}->value = $date->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
$entity->save();
$display_repository
->getViewDisplay($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
->setComponent($field_name, $this->displayOptions)
->save();
$expected = str_replace(
'@interval',
$this->dateFormatter->formatTimeDiffUntil(
$timestamp,
['granularity' => $this->displayOptions['settings']['granularity']]),
$this->displayOptions['settings']['future_format']
);
$output = $this->renderTestEntity($id);
$this->assertStringContainsString($expected, $output, "Formatted date field using datetime_time_ago format displayed as $expected.");
// Test the required field validation error message.
$entity = EntityTest::create(['name' => 'test datetime required message']);
$form = \Drupal::entityTypeManager()->getFormObject('entity_test', 'default')->setEntity($entity);
$form_state = new FormState();
\Drupal::formBuilder()->submitForm($form, $form_state);
$errors = $form_state->getErrors();
$expected_error_message = "The <em class=\"placeholder\">$field_label</em> date is required.";
$actual_error_message = $errors["{$field_name}][0][value"]->__toString();
$this->assertEquals($expected_error_message, $actual_error_message);
}
/**
* Tests Date List Widget functionality.
*/
public function testDatelistWidget(): void {
$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');
$this->fieldStorage->save();
/** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
$display_repository = \Drupal::service('entity_display.repository');
// Change the widget to a datelist widget.
$display_repository->getFormDisplay($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle())
->setComponent($field_name, [
'type' => 'datetime_datelist',
'settings' => [
'date_order' => 'YMD',
],
])
->save();
\Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
// Display creation form.
$this->drupalGet('entity_test/add');
$this->assertSession()->elementTextContains('xpath', '//fieldset[@id="edit-' . $field_name . '-0"]/legend', $field_label);
$this->assertSession()->elementExists('xpath', '//fieldset[@aria-describedby="edit-' . $field_name . '-0--description"]');
$this->assertSession()->elementExists('xpath', '//div[@id="edit-' . $field_name . '-0--description"]');
// Assert that Hour and Minute Elements do not appear on Date Only
$this->assertSession()->elementNotExists('xpath', "//*[@id=\"edit-$field_name-0-value-hour\"]");
$this->assertSession()->elementNotExists('xpath', "//*[@id=\"edit-$field_name-0-value-minute\"]");
// Go to the form display page to assert that increment option does not
// appear on Date Only.
$fieldEditUrl = 'entity_test/structure/entity_test/form-display';
$this->drupalGet($fieldEditUrl);
// Click on the widget settings button to open the widget settings form.
$this->submitForm([], $field_name . "_settings_edit");
$xpathIncr = "//select[starts-with(@id, \"edit-fields-$field_name-settings-edit-form-settings-increment\")]";
$this->assertSession()->elementNotExists('xpath', $xpathIncr);
// Change the field to a datetime field.
$this->fieldStorage->setSetting('datetime_type', 'datetime');
$this->fieldStorage->save();
// Change the widget to a datelist widget.
$display_repository->getFormDisplay($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle())
->setComponent($field_name, [
'type' => 'datetime_datelist',
'settings' => [
'increment' => 1,
'date_order' => 'YMD',
'time_type' => '12',
],
])
->save();
\Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
// Go to the form display page to assert that increment option does appear
// on Date Time.
$fieldEditUrl = 'entity_test/structure/entity_test/form-display';
$this->drupalGet($fieldEditUrl);
// Click on the widget settings button to open the widget settings form.
$this->submitForm([], $field_name . "_settings_edit");
$this->assertSession()->elementExists('xpath', $xpathIncr);
// Display creation form.
$this->drupalGet('entity_test/add');
// Year element.
$this->assertSession()->elementExists('xpath', "//*[@id=\"edit-$field_name-0-value-year\"]");
$this->assertTrue($this->assertSession()->optionExists("edit-$field_name-0-value-year", '')->isSelected());
$this->assertSession()->optionExists("edit-$field_name-0-value-year", 'Year');
// Month element.
$this->assertSession()->elementExists('xpath', "//*[@id=\"edit-$field_name-0-value-month\"]");
$this->assertTrue($this->assertSession()->optionExists("edit-$field_name-0-value-month", '')->isSelected());
$this->assertSession()->optionExists("edit-$field_name-0-value-month", 'Month');
// Day element.
$this->assertSession()->elementExists('xpath', "//*[@id=\"edit-$field_name-0-value-day\"]");
$this->assertTrue($this->assertSession()->optionExists("edit-$field_name-0-value-day", '')->isSelected());
$this->assertSession()->optionExists("edit-$field_name-0-value-day", 'Day');
// Hour element.
$this->assertSession()->elementExists('xpath', "//*[@id=\"edit-$field_name-0-value-hour\"]");
$this->assertTrue($this->assertSession()->optionExists("edit-$field_name-0-value-hour", '')->isSelected());
$this->assertSession()->optionExists("edit-$field_name-0-value-hour", 'Hour');
// Minute element.
$this->assertSession()->elementExists('xpath', "//*[@id=\"edit-$field_name-0-value-minute\"]");
$this->assertTrue($this->assertSession()->optionExists("edit-$field_name-0-value-minute", '')->isSelected());
$this->assertSession()->optionExists("edit-$field_name-0-value-minute", 'Minute');
// No Second element.
$this->assertSession()->elementNotExists('xpath', "//*[@id=\"edit-$field_name-0-value-second\"]");
// AMPM element.
$this->assertSession()->elementExists('xpath', "//*[@id=\"edit-$field_name-0-value-ampm\"]");
$this->assertTrue($this->assertSession()->optionExists("edit-$field_name-0-value-ampm", '')->isSelected());
$this->assertSession()->optionExists("edit-$field_name-0-value-ampm", 'AM/PM');
// Submit a valid date and ensure it is accepted.
$date_value = ['year' => 2012, 'month' => 12, 'day' => 31, 'hour' => 5, 'minute' => 15];
$edit = [];
// Add the ampm indicator since we are testing 12 hour time.
$date_value['ampm'] = 'am';
foreach ($date_value as $part => $value) {
$edit["{$field_name}[0][value][$part]"] = $value;
}
$this->submitForm($edit, 'Save');
preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
$id = $match[1];
$this->assertSession()->pageTextContains('entity_test ' . $id . ' has been created.');
$this->assertTrue($this->assertSession()->optionExists("edit-$field_name-0-value-year", '2012')->isSelected());
$this->assertTrue($this->assertSession()->optionExists("edit-$field_name-0-value-month", '12')->isSelected());
$this->assertTrue($this->assertSession()->optionExists("edit-$field_name-0-value-day", '31')->isSelected());
$this->assertTrue($this->assertSession()->optionExists("edit-$field_name-0-value-hour", '5')->isSelected());
$this->assertTrue($this->assertSession()->optionExists("edit-$field_name-0-value-minute", '15')->isSelected());
$this->assertTrue($this->assertSession()->optionExists("edit-$field_name-0-value-ampm", 'am')->isSelected());
// Test the widget using increment other than 1 and 24 hour mode.
$display_repository->getFormDisplay($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle())
->setComponent($field_name, [
'type' => 'datetime_datelist',
'settings' => [
'increment' => 15,
'date_order' => 'YMD',
'time_type' => '24',
],
])
->save();
\Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
// Display creation form.
$this->drupalGet('entity_test/add');
// Other elements are unaffected by the changed settings.
$this->assertSession()->elementExists('xpath', "//*[@id=\"edit-$field_name-0-value-hour\"]");
$this->assertTrue($this->assertSession()->optionExists("edit-$field_name-0-value-hour", '')->isSelected());
$this->assertSession()->elementNotExists('xpath', "//*[@id=\"edit-$field_name-0-value-ampm\"]");
// Submit a valid date and ensure it is accepted.
$date_value = ['year' => 2012, 'month' => 12, 'day' => 31, 'hour' => 17, 'minute' => 15];
$edit = [];
foreach ($date_value as $part => $value) {
$edit["{$field_name}[0][value][$part]"] = $value;
}
$this->submitForm($edit, 'Save');
preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
$id = $match[1];
$this->assertSession()->pageTextContains('entity_test ' . $id . ' has been created.');
$this->assertTrue($this->assertSession()->optionExists("edit-$field_name-0-value-year", '2012')->isSelected());
$this->assertTrue($this->assertSession()->optionExists("edit-$field_name-0-value-month", '12')->isSelected());
$this->assertTrue($this->assertSession()->optionExists("edit-$field_name-0-value-day", '31')->isSelected());
$this->assertTrue($this->assertSession()->optionExists("edit-$field_name-0-value-hour", '17')->isSelected());
$this->assertTrue($this->assertSession()->optionExists("edit-$field_name-0-value-minute", '15')->isSelected());
// Test the widget for partial completion of fields.
$display_repository->getFormDisplay($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle())
->setComponent($field_name, [
'type' => 'datetime_datelist',
'settings' => [
'increment' => 1,
'date_order' => 'YMD',
'time_type' => '24',
],
])
->save();
\Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
// Test the widget for validation notifications.
foreach ($this->datelistDataProvider($field_label) as $data) {
[$date_value, $expected] = $data;
// Display creation form.
$this->drupalGet('entity_test/add');
// Submit a partial date and ensure and error message is provided.
$edit = [];
foreach ($date_value as $part => $value) {
$edit["{$field_name}[0][value][$part]"] = $value;
}
$this->submitForm($edit, 'Save');
$this->assertSession()->statusCodeEquals(200);
foreach ($expected as $expected_text) {
$this->assertSession()->pageTextContains($expected_text);
}
}
// Test the widget for complete input with zeros as part of selections.
$this->drupalGet('entity_test/add');
$date_value = ['year' => 2012, 'month' => '12', 'day' => '31', 'hour' => '0', 'minute' => '0'];
$edit = [];
foreach ($date_value as $part => $value) {
$edit["{$field_name}[0][value][$part]"] = $value;
}
$this->submitForm($edit, 'Save');
$this->assertSession()->statusCodeEquals(200);
preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
$id = $match[1];
$this->assertSession()->pageTextContains('entity_test ' . $id . ' has been created.');
// Test the widget to ensure zeros are not deselected on validation.
$this->drupalGet('entity_test/add');
$date_value = ['year' => 2012, 'month' => '12', 'day' => '31', 'hour' => '', 'minute' => '0'];
$edit = [];
foreach ($date_value as $part => $value) {
$edit["{$field_name}[0][value][$part]"] = $value;
}
$this->submitForm($edit, 'Save');
$this->assertSession()->statusCodeEquals(200);
$this->assertTrue($this->assertSession()->optionExists("edit-$field_name-0-value-minute", '0')->isSelected());
}
/**
* 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($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' => ''],
[
"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' => ''],
[
"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' => ''],
[
"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' => ''],
[
"The $field_label date is incomplete.",
'A value must be selected for minute.',
],
],
];
}
/**
* Tests default value functionality.
*/
public function testDefaultValue(): void {
// Create a test content type.
$this->drupalCreateContentType(['type' => 'date_content']);
// Create a field storage with settings to validate.
$field_name = $this->randomMachineName();
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'node',
'type' => 'datetime',
'settings' => ['datetime_type' => 'date'],
]);
$field_storage->save();
$field = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'date_content',
]);
$field->save();
// Loop through defined timezones to test that date-only defaults work at
// the extremes.
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 = [
'set_default_value' => '1',
'default_value_input[default_date_type]' => 'now',
];
$this->drupalGet('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name);
$this->submitForm($field_edit, 'Save settings');
// Check that default value is selected in default value form.
$this->drupalGet('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name);
$this->assertTrue($this->assertSession()->optionExists('edit-default-value-input-default-date-type', 'now')->isSelected());
// Check that the relative default value is empty.
$this->assertSession()->fieldValueEquals('default_value_input[default_date]', '');
// Check if default_date has been stored successfully.
$config_entity = $this->config('field.field.node.date_content.' . $field_name)
->get();
$this->assertEquals(['default_date_type' => 'now', 'default_date' => 'now'], $config_entity['default_value'][0], 'Default value has been stored successfully');
// Clear field cache in order to avoid stale cache values.
\Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
// Create a new node to check that datetime field default value is today.
$new_node = Node::create(['type' => 'date_content']);
$expected_date = new DrupalDateTime('now', date_default_timezone_get());
$this->assertEquals($expected_date->format(DateTimeItemInterface::DATE_STORAGE_FORMAT), $new_node->get($field_name)->offsetGet(0)->value);
// Set an invalid relative default_value to test validation.
$field_edit = [
'set_default_value' => '1',
'default_value_input[default_date_type]' => 'relative',
'default_value_input[default_date]' => 'invalid date',
];
$this->drupalGet('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name);
$this->submitForm($field_edit, 'Save settings');
$this->assertSession()->pageTextContains('The relative date value entered is invalid.');
// Set a relative default_value.
$field_edit = [
'set_default_value' => '1',
'default_value_input[default_date_type]' => 'relative',
'default_value_input[default_date]' => '+90 days',
];
$this->drupalGet('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name);
$this->submitForm($field_edit, 'Save settings');
// Check that default value is selected in default value form.
$this->drupalGet('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name);
$this->assertTrue($this->assertSession()->optionExists('edit-default-value-input-default-date-type', 'relative')->isSelected());
// Check that the relative default value is displayed.
$this->assertSession()->fieldValueEquals('default_value_input[default_date]', '+90 days');
// Check if default_date has been stored successfully.
$config_entity = $this->config('field.field.node.date_content.' . $field_name)
->get();
$this->assertEquals(['default_date_type' => 'relative', 'default_date' => '+90 days'], $config_entity['default_value'][0], 'Default value has been stored successfully');
// Clear field cache in order to avoid stale cache values.
\Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
// Create a new node to check that datetime field default value is +90
// days.
$new_node = Node::create(['type' => 'date_content']);
$expected_date = new DrupalDateTime('+90 days', date_default_timezone_get());
$this->assertEquals($expected_date->format(DateTimeItemInterface::DATE_STORAGE_FORMAT), $new_node->get($field_name)->offsetGet(0)->value);
// Remove default value.
$field_edit = [
'set_default_value' => '1',
'default_value_input[default_date_type]' => '',
];
$this->drupalGet('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name);
$this->submitForm($field_edit, 'Save settings');
// Check that default value is selected in default value form.
$this->drupalGet('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name);
$this->assertTrue($this->assertSession()->optionExists('edit-default-value-input-default-date-type', '')->isSelected());
// Check that the relative default value is empty.
$this->assertSession()->fieldValueEquals('default_value_input[default_date]', '');
// Check if default_date has been stored successfully.
$config_entity = $this->config('field.field.node.date_content.' . $field_name)
->get();
$this->assertEmpty($config_entity['default_value'], 'Empty default value has been stored successfully');
// Clear field cache in order to avoid stale cache values.
\Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
// Create a new node to check that datetime field default value is not
// set.
$new_node = Node::create(['type' => 'date_content']);
$this->assertNull($new_node->get($field_name)->value, 'Default value is not set');
}
}
/**
* Tests that invalid values are caught and marked as invalid.
*/
public function testInvalidField(): void {
// Change the field to a datetime field.
$this->fieldStorage->setSetting('datetime_type', 'datetime');
$this->fieldStorage->save();
$field_name = $this->fieldStorage->getName();
// Display creation form.
$this->drupalGet('entity_test/add');
$this->assertSession()->fieldValueEquals("{$field_name}[0][value][date]", '');
$this->assertSession()->fieldValueEquals("{$field_name}[0][value][time]", '');
// Submit invalid dates and ensure they is not accepted.
$date_value = '';
$edit = [
"{$field_name}[0][value][date]" => $date_value,
"{$field_name}[0][value][time]" => '12:00:00',
];
$this->submitForm($edit, 'Save');
$this->assertSession()->pageTextContains('date is invalid');
// Invalid year value.
$date_value = 'aaaa-12-01';
$edit = [
"{$field_name}[0][value][date]" => $date_value,
"{$field_name}[0][value][time]" => '00:00:00',
];
$this->submitForm($edit, 'Save');
$this->assertSession()->pageTextContains('date is invalid');
// Invalid month value.
$date_value = '2012-75-01';
$edit = [
"{$field_name}[0][value][date]" => $date_value,
"{$field_name}[0][value][time]" => '00:00:00',
];
$this->submitForm($edit, 'Save');
$this->assertSession()->pageTextContains('date is invalid');
// Invalid day value.
$date_value = '2012-12-99';
$edit = [
"{$field_name}[0][value][date]" => $date_value,
"{$field_name}[0][value][time]" => '00:00:00',
];
$this->submitForm($edit, 'Save');
$this->assertSession()->pageTextContains('date is invalid');
// Invalid time value.
$date_value = '2012-12-01';
$time_value = '';
$edit = [
"{$field_name}[0][value][date]" => $date_value,
"{$field_name}[0][value][time]" => $time_value,
];
$this->submitForm($edit, 'Save');
$this->assertSession()->pageTextContains('date is invalid');
// Invalid hour value.
$date_value = '2012-12-01';
$time_value = '49:00:00';
$edit = [
"{$field_name}[0][value][date]" => $date_value,
"{$field_name}[0][value][time]" => $time_value,
];
$this->submitForm($edit, 'Save');
$this->assertSession()->pageTextContains('date is invalid');
// Invalid minutes value.
$date_value = '2012-12-01';
$time_value = '12:99:00';
$edit = [
"{$field_name}[0][value][date]" => $date_value,
"{$field_name}[0][value][time]" => $time_value,
];
$this->submitForm($edit, 'Save');
$this->assertSession()->pageTextContains('date is invalid');
// Invalid seconds value.
$date_value = '2012-12-01';
$time_value = '12:15:99';
$edit = [
"{$field_name}[0][value][date]" => $date_value,
"{$field_name}[0][value][time]" => $time_value,
];
$this->submitForm($edit, 'Save');
$this->assertSession()->pageTextContains('date is invalid');
}
/**
* Tests that 'Date' field storage setting form is disabled if field has data.
*/
public function testDateStorageSettings(): void {
// Create a test content type.
$this->drupalCreateContentType(['type' => 'date_content']);
// Create a field storage with settings to validate.
$field_name = $this->randomMachineName();
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'node',
'type' => 'datetime',
'settings' => [
'datetime_type' => 'date',
],
]);
$field_storage->save();
$field = FieldConfig::create([
'field_storage' => $field_storage,
'field_name' => $field_name,
'bundle' => 'date_content',
]);
$field->save();
\Drupal::service('entity_display.repository')
->getFormDisplay('node', 'date_content')
->setComponent($field_name, [
'type' => 'datetime_default',
])
->save();
$edit = [
'title[0][value]' => $this->randomString(),
'body[0][value]' => $this->randomString(),
$field_name . '[0][value][date]' => '2016-04-01',
];
$this->drupalGet('node/add/date_content');
$this->submitForm($edit, 'Save');
$this->drupalGet('admin/structure/types/manage/date_content/fields/node.date_content.' . $field_name);
$this->assertSession()->elementsCount('xpath', "//*[@name='field_storage[subform][settings][datetime_type]' and contains(@disabled, 'disabled')]", 1);
}
}

View File

@ -0,0 +1,129 @@
<?php
declare(strict_types=1);
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 field formatter display options.
*
* @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}
*/
protected static $modules = ['datetime', 'entity_test', 'field_ui'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected function setUp(): void {
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(): void {
$this->drupalGet('entity_test/structure/entity_test/display');
$edit = [
'fields[field_datetime][region]' => 'content',
'fields[field_datetime][type]' => 'datetime_time_ago',
];
$this->submitForm($edit, 'Save');
$this->submitForm([], '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]' => 1,
];
$this->submitForm($edit, 'Update');
$this->submitForm([], 'Save');
$this->assertSession()->pageTextContains('ends in 1 year');
$this->assertSession()->pageTextContains('started 1 year ago');
}
}

View File

@ -0,0 +1,107 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\datetime\Functional;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
/**
* Tests Datetime widgets functionality.
*
* @group datetime
*/
class DateTimeWidgetTest extends DateTestBase {
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* The default display settings to use for the formatters.
*
* @var array
*/
protected $defaultSettings = ['timezone_override' => ''];
/**
* {@inheritdoc}
*/
protected function getTestFieldType(): string {
return 'datetime';
}
/**
* Test default value functionality.
*/
public function testDateOnlyDefaultValue(): void {
// Create a test content type.
$this->drupalCreateContentType(['type' => 'date_only_content']);
// Create a field storage with settings to validate.
$field_storage = FieldStorageConfig::create([
'field_name' => 'field_date_only',
'entity_type' => 'node',
'type' => 'datetime',
'settings' => ['datetime_type' => 'date'],
]);
$field_storage->save();
$field = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'date_only_content',
]);
$field->save();
$edit = [
'fields[field_date_only][region]' => 'content',
'fields[field_date_only][type]' => 'datetime_default',
];
$this->drupalGet('admin/structure/types/manage/date_only_content/form-display');
$this->submitForm($edit, 'Save');
$this->drupalGet('admin/structure/types/manage/date_only_content/display');
$this->submitForm($edit, 'Save');
// Set now as default_value.
$edit = [
'set_default_value' => '1',
'default_value_input[default_date_type]' => 'now',
];
$this->drupalGet('admin/structure/types/manage/date_only_content/fields/node.date_only_content.field_date_only');
$this->submitForm($edit, 'Save settings');
// Check that default value is selected in default value form.
$this->drupalGet('admin/structure/types/manage/date_only_content/fields/node.date_only_content.field_date_only');
$option_field = $this->assertSession()->optionExists('edit-default-value-input-default-date-type', 'now');
$this->assertTrue($option_field->hasAttribute('selected'));
$this->assertSession()->fieldValueEquals('default_value_input[default_date]', '');
// Loop through defined timezones to test that date-only defaults work at
// the extremes.
foreach (static::$timezones as $timezone) {
$this->setSiteTimezone($timezone);
$this->assertEquals($timezone, $this->config('system.date')->get('timezone.default'), 'Time zone set to ' . $timezone);
// The time of the request is determined very early on in the request so
// use the current time prior to making a request.
$request_time = $this->container->get('datetime.time')->getCurrentTime();
$this->drupalGet('node/add/date_only_content');
$today = $this->dateFormatter->format($request_time, 'html_date', NULL, $timezone);
$this->assertSession()->fieldValueEquals('field_date_only[0][value][date]', $today);
$edit = [
'title[0][value]' => $timezone,
];
$this->submitForm($edit, 'Save');
$this->assertSession()->pageTextContains('date_only_content ' . $timezone . ' has been created');
$node = $this->drupalGetNodeByTitle($timezone);
$today_storage = $this->dateFormatter->format($request_time, 'html_date', NULL, $timezone);
$this->assertEquals($today_storage, $node->field_date_only->value);
}
}
}

View File

@ -0,0 +1,161 @@
<?php
declare(strict_types=1);
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';
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Datetime test field name.
*
* @var string
*/
protected static $fieldName = 'field_date_only';
/**
* {@inheritdoc}
*/
protected static $modules = ['datetime', 'entity_test'];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
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): void {
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 = "The specified date \"$value\" is not in an accepted format: \"Y-m-d\" (date-only).";
$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 = "The specified date \"$value\" is not in an accepted format: \"Y-m-d\" (date-only).";
$this->assertResourceErrorResponse(422, $message, $response);
}
}
}

View File

@ -0,0 +1,171 @@
<?php
declare(strict_types=1);
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';
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Datetime test field name.
*
* @var string
*/
protected static $fieldName = 'field_datetime';
/**
* {@inheritdoc}
*/
protected static $modules = ['datetime', 'entity_test'];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
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' => '2017-03-02T07:02:00+11:00',
],
],
];
}
/**
* {@inheritdoc}
*/
protected function getNormalizedPostEntity() {
return parent::getNormalizedPostEntity() + [
static::$fieldName => [
[
'value' => static::$dateString . '+00:00',
],
],
];
}
/**
* {@inheritdoc}
*/
protected function assertNormalizationEdgeCases($method, Url $url, array $request_options): void {
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 = "The specified date \"$value\" is not in an accepted format: \"Y-m-d\\TH:i:sP\" (RFC 3339), \"Y-m-d\\TH:i:sO\" (ISO 8601).";
$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 = "The specified date \"$value\" is not in an accepted format: \"Y-m-d\\TH:i:sP\" (RFC 3339), \"Y-m-d\\TH:i:sO\" (ISO 8601).";
$this->assertResourceErrorResponse(422, $message, $response);
// DX: 422 when date value is invalid.
$normalization = $this->getNormalizedPostEntity();
$value = '2017-13-55T20:02:00+00: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 = "The specified date \"$value\" is not in an accepted format: \"Y-m-d\\TH:i:sP\" (RFC 3339), \"Y-m-d\\TH:i:sO\" (ISO 8601).";
$this->assertResourceErrorResponse(422, $message, $response);
}
}
}

View File

@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\datetime\Functional;
use Drupal\Tests\system\Functional\Module\GenericModuleTestBase;
/**
* Generic module test for datetime.
*
* @group datetime
*/
class GenericTest extends GenericModuleTestBase {}

View File

@ -0,0 +1,267 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\datetime\Functional\Views;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\views\Functional\ViewTestBase;
use Drupal\views\Tests\ViewTestData;
/**
* Tests Views filters for datetime fields.
*
* @group datetime
*/
class FilterDateTest extends ViewTestBase {
/**
* Name of the field.
*
* Note, this is used in the default test view.
*
* @var string
*/
protected $fieldName = 'field_date';
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Nodes to test.
*
* @var \Drupal\node\NodeInterface[]
*/
protected $nodes = [];
/**
* Dates of test nodes in date storage format.
*
* @var string[]
*/
protected $dates;
/**
* {@inheritdoc}
*/
protected static $modules = [
'datetime',
'datetime_test',
'node',
'views_ui',
];
/**
* {@inheritdoc}
*/
public static $testViews = ['test_filter_datetime'];
/**
* {@inheritdoc}
*
* Create nodes with relative dates of yesterday, today, and tomorrow.
*/
protected function setUp($import_test_views = TRUE, $modules = ['views_test_config']): void {
parent::setUp($import_test_views, $modules);
$now = \Drupal::time()->getRequestTime();
$admin_user = $this->drupalCreateUser(['administer views']);
$this->drupalLogin($admin_user);
$this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
// Add a date field to page nodes.
$fieldStorage = FieldStorageConfig::create([
'field_name' => $this->fieldName,
'entity_type' => 'node',
'type' => 'datetime',
'settings' => ['datetime_type' => DateTimeItem::DATETIME_TYPE_DATETIME],
]);
$fieldStorage->save();
$field = FieldConfig::create([
'field_storage' => $fieldStorage,
'bundle' => 'page',
'required' => TRUE,
]);
$field->save();
// Create some nodes.
$this->dates = [
// Tomorrow.
DrupalDateTime::createFromTimestamp($now + 86400, DateTimeItemInterface::STORAGE_TIMEZONE)->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
// Today.
DrupalDateTime::createFromTimestamp($now, DateTimeItemInterface::STORAGE_TIMEZONE)->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
// Yesterday.
DrupalDateTime::createFromTimestamp($now - 86400, DateTimeItemInterface::STORAGE_TIMEZONE)->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
];
$this->nodes = [];
foreach ($this->dates as $date) {
$this->nodes[] = $this->drupalCreateNode([
$this->fieldName => [
'value' => $date,
],
]);
}
// Add a node where the date field is empty.
$this->nodes[] = $this->drupalCreateNode();
// Views needs to be aware of the new field.
$this->container->get('views.views_data')->clear();
// Load test views.
ViewTestData::createTestViews(static::class, ['datetime_test']);
}
/**
* Tests exposed grouped filters.
*/
public function testExposedGroupedFilters(): void {
$filter_identifier = $this->fieldName . '_value';
$this->drupalGet('admin/structure/views/nojs/handler/test_filter_datetime/default/filter/' . $filter_identifier);
$this->submitForm([], 'Expose filter');
$this->submitForm([], 'Grouped filters');
// Create groups with different amount of expected values.
$edit = [];
// No values are required.
$edit['options[group_info][group_items][1][title]'] = 'empty';
$edit['options[group_info][group_items][1][operator]'] = 'empty';
$edit['options[group_info][group_items][2][title]'] = 'not empty';
$edit['options[group_info][group_items][2][operator]'] = 'not empty';
// One value is required.
$edit['options[group_info][group_items][3][title]'] = 'less than';
$edit['options[group_info][group_items][3][operator]'] = '<';
$edit['options[group_info][group_items][3][value][value]'] = $this->dates[0];
// Two values are required (min and max).
$this->submitForm($edit, 'Add another item');
$edit['options[group_info][group_items][4][title]'] = 'between';
$edit['options[group_info][group_items][4][operator]'] = 'between';
$edit['options[group_info][group_items][4][value][type]'] = 'offset';
$edit['options[group_info][group_items][4][value][min]'] = '-2 hours';
$edit['options[group_info][group_items][4][value][max]'] = '+2 hours';
$this->submitForm($edit, 'Apply');
// Test that the exposed filter works as expected.
$path = 'test_filter_datetime-path';
$this->drupalGet('admin/structure/views/view/test_filter_datetime/edit');
$this->submitForm([], 'Add Page');
$this->drupalGet('admin/structure/views/nojs/display/test_filter_datetime/page_1/path');
$this->submitForm(['path' => $path], 'Apply');
$this->submitForm([], 'Save');
$this->drupalGet($path);
// Filter the Preview by 'empty'.
$this->getSession()->getPage()->findField($filter_identifier)->selectOption('1');
$this->getSession()->getPage()->pressButton('Apply');
$this->assertIds([4]);
// Filter the Preview by 'not empty'.
$this->getSession()->getPage()->findField($filter_identifier)->selectOption('2');
$this->getSession()->getPage()->pressButton('Apply');
$this->assertIds([1, 2, 3]);
// Filter the Preview by 'less than'.
$this->getSession()->getPage()->findField($filter_identifier)->selectOption('3');
$this->getSession()->getPage()->pressButton('Apply');
$this->assertIds([2, 3]);
// Filter the Preview by 'between'.
$this->getSession()->getPage()->findField($filter_identifier)->selectOption('4');
$this->getSession()->getPage()->pressButton('Apply');
$this->assertIds([2]);
// Change the identifier for grouped exposed filter.
$this->drupalGet('admin/structure/views/nojs/handler/test_filter_datetime/default/filter/' . $filter_identifier);
$filter_identifier = 'date';
$edit['options[group_info][identifier]'] = $filter_identifier;
$this->submitForm($edit, 'Apply');
$this->submitForm([], 'Save');
// Filter results again using a new filter identifier.
$this->drupalGet($path);
$this->getSession()->getPage()->findField($filter_identifier)->selectOption('2');
$this->getSession()->getPage()->pressButton('Apply');
$this->assertIds([1, 2, 3]);
}
/**
* Ensures that a given list of items appear on the view result.
*
* @param array $expected_ids
* An array of IDs.
*/
protected function assertIds(array $expected_ids = []): void {
// First verify the count.
$elements = $this->cssSelect('.views-row .field-content');
$this->assertCount(count($expected_ids), $elements);
$actual_ids = [];
foreach ($elements as $element) {
$actual_ids[] = (int) $element->getText();
}
$this->assertEquals($expected_ids, $actual_ids);
}
/**
* Tests exposed date filters with a pager.
*/
public function testExposedFilterWithPager(): void {
// Expose the empty and not empty operators in a grouped filter.
$this->drupalGet('admin/structure/views/nojs/handler/test_filter_datetime/default/filter/' . $this->fieldName . '_value');
$this->submitForm([], 'Expose filter');
$edit = [];
$edit['options[operator]'] = '>';
$this->submitForm($edit, 'Apply');
// Expose the view and set the pager to 2 items.
$path = 'test_filter_datetime-path';
$this->drupalGet('admin/structure/views/view/test_filter_datetime/edit');
$this->submitForm([], 'Add Page');
$this->drupalGet('admin/structure/views/nojs/display/test_filter_datetime/page_1/path');
$this->submitForm(['path' => $path], 'Apply');
$this->drupalGet('admin/structure/views/nojs/display/test_filter_datetime/default/pager_options');
$this->submitForm(['pager_options[items_per_page]' => 2], 'Apply');
$this->submitForm([], 'Save');
// Assert the page without filters.
$this->drupalGet($path);
$results = $this->cssSelect('.views-row');
$this->assertCount(2, $results);
$this->assertSession()->pageTextContains('Next');
// Assert the page with filter in the future, one results without pager.
$page = $this->getSession()->getPage();
$now = \Drupal::time()->getRequestTime();
$page->fillField($this->fieldName . '_value', DrupalDateTime::createFromTimestamp($now + 1)->format('Y-m-d H:i:s'));
$page->pressButton('Apply');
$results = $this->cssSelect('.views-row');
$this->assertCount(1, $results);
$this->assertSession()->pageTextNotContains('Next');
// Assert the page with filter in the past, 3 results with pager.
$page->fillField($this->fieldName . '_value', DrupalDateTime::createFromTimestamp($now - 1000000)->format('Y-m-d H:i:s'));
$this->getSession()->getPage()->pressButton('Apply');
$results = $this->cssSelect('.views-row');
$this->assertCount(2, $results);
$this->assertSession()->pageTextContains('Next');
$page->clickLink('2');
$results = $this->cssSelect('.views-row');
$this->assertCount(1, $results);
}
}

View File

@ -0,0 +1,109 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\datetime\Kernel;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\KernelTests\KernelTestBase;
/**
* Tests serializing a form with an injected datetime instance.
*
* @group datetime
*/
class DateTimeFormInjectionTest extends KernelTestBase implements FormInterface {
use DependencySerializationTrait;
/**
* A Dblog logger instance.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* {@inheritdoc}
*/
protected static $modules = ['system', 'datetime'];
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'datetime_test_injection_form';
}
/**
* Process callback.
*
* @param array $element
* Form element.
*
* @return array
* Processed element.
*/
public function process($element) {
return $element;
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['datelist_element'] = [
'#title' => 'datelist test',
'#type' => 'datelist',
'#default_value' => new DrupalDateTime('2000-01-01 00:00:00'),
'#date_part_order' => [
'month',
'day',
'year',
'hour',
'minute', 'ampm',
],
'#date_text_parts' => ['year'],
'#date_year_range' => '2010:2020',
'#date_increment' => 15,
];
$form['#process'][] = [$this, 'process'];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state): void {
$this->assertTrue(TRUE);
$form_state->setRebuild();
}
/**
* Tests custom string injection serialization.
*/
public function testDatetimeSerialization(): void {
$form_state = new FormState();
$form_state->setRequestMethod('POST');
$form_state->setCached();
$form_builder = $this->container->get('form_builder');
$form_id = $form_builder->getFormId($this, $form_state);
$form = $form_builder->retrieveForm($form_id, $form_state);
$form_builder->prepareForm($form_id, $form, $form_state);
// Set up $form_state so that the form is properly submitted.
$form_state->setUserInput(['form_id' => $form_id]);
$form_state->setProgrammed();
$form_state->setSubmitted();
$form_builder->processForm($form_id, $form, $form_state);
}
}

View File

@ -0,0 +1,356 @@
<?php
declare(strict_types=1);
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;
use Drupal\field\Entity\FieldStorageConfig;
use PHPUnit\Framework\AssertionFailedError;
/**
* Tests the new entity API for the date field type.
*
* @group datetime
*/
class DateTimeItemTest extends FieldKernelTestBase {
/**
* 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}
*/
protected static $modules = ['datetime'];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Create a field with settings to validate.
$this->fieldStorage = FieldStorageConfig::create([
'field_name' => 'field_datetime',
'type' => 'datetime',
'entity_type' => 'entity_test',
'settings' => ['datetime_type' => DateTimeItem::DATETIME_TYPE_DATETIME],
]);
$this->fieldStorage->save();
$this->field = FieldConfig::create([
'field_storage' => $this->fieldStorage,
'bundle' => 'entity_test',
'settings' => [
'default_value' => 'blank',
],
]);
$this->field->save();
}
/**
* Tests using entity fields of the datetime field type.
*/
public function testDateTime(): void {
$this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATETIME);
$this->fieldStorage->save();
// Verify entity creation.
$entity = EntityTest::create();
$value = '2014-01-01T20:00:00';
$entity->field_datetime = $value;
$entity->name->value = $this->randomMachineName();
$this->entityValidateAndSave($entity);
// Verify entity has been created properly.
$id = $entity->id();
$entity = EntityTest::load($id);
$this->assertInstanceOf(FieldItemListInterface::class, $entity->field_datetime);
$this->assertInstanceOf(FieldItemInterface::class, $entity->field_datetime[0]);
$this->assertEquals($value, $entity->field_datetime->value);
$this->assertEquals($value, $entity->field_datetime[0]->value);
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime[0]->getProperties()['value']->getDateTime()->getTimeZone()->getName());
$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->assertEquals($new_value, $entity->field_datetime->value);
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime[0]->getProperties()['value']->getDateTime()->getTimeZone()->getName());
$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->assertEquals($new_value, $entity->field_datetime->value);
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime[0]->getProperties()['value']->getDateTime()->getTimeZone()->getName());
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
// Test the generateSampleValue() method.
$entity = EntityTest::create();
$entity->field_datetime->generateSampleItems();
$this->entityValidateAndSave($entity);
}
/**
* Tests using entity fields of the date field type.
*/
public function testDateOnly(): void {
$this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
$this->fieldStorage->save();
// Verify entity creation.
$entity = EntityTest::create();
$value = '2014-01-01';
$entity->field_datetime = $value;
$entity->name->value = $this->randomMachineName();
$this->entityValidateAndSave($entity);
// Verify entity has been created properly.
$id = $entity->id();
$entity = EntityTest::load($id);
$this->assertInstanceOf(FieldItemListInterface::class, $entity->field_datetime);
$this->assertInstanceOf(FieldItemInterface::class, $entity->field_datetime[0]);
$this->assertEquals($value, $entity->field_datetime->value);
$this->assertEquals($value, $entity->field_datetime[0]->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->assertEquals($new_value, $entity->field_datetime->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->assertEquals($new_value, $entity->field_datetime->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);
}
/**
* Tests DateTimeItem::setValue().
*/
public function testSetValue(): void {
// Test a date+time field.
$this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATETIME);
$this->fieldStorage->save();
// Test DateTimeItem::setValue() using string.
$entity = EntityTest::create();
$value = '2014-01-01T20:00:00';
$entity->get('field_datetime')->set(0, $value);
$this->entityValidateAndSave($entity);
// Load the entity and ensure the field was saved correctly.
$id = $entity->id();
$entity = EntityTest::load($id);
$this->assertEquals($value, $entity->field_datetime[0]->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();
$value = '2014-01-01T20:00:00';
$entity->set('field_datetime', $value);
$this->entityValidateAndSave($entity);
// Load the entity and ensure the field was saved correctly.
$id = $entity->id();
$entity = EntityTest::load($id);
$this->assertEquals($value, $entity->field_datetime[0]->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);
$this->fieldStorage->save();
// Test DateTimeItem::setValue() using string.
$entity = EntityTest::create();
$value = '2014-01-01';
$entity->get('field_datetime')->set(0, $value);
$this->entityValidateAndSave($entity);
// Load the entity and ensure the field was saved correctly.
$id = $entity->id();
$entity = EntityTest::load($id);
$this->assertEquals($value, $entity->field_datetime[0]->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();
$value = '2014-01-01';
$entity->set('field_datetime', $value);
$this->entityValidateAndSave($entity);
// Load the entity and ensure the field was saved correctly.
$id = $entity->id();
$entity = EntityTest::load($id);
$this->assertEquals($value, $entity->field_datetime[0]->value, 'DateTimeItem::setValue() works with array value.');
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
}
/**
* Tests setting the value of the DateTimeItem directly.
*/
public function testSetValueProperty(): void {
// Test Date::setValue() with a date+time field.
// Test a date+time field.
$this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATETIME);
$this->fieldStorage->save();
$entity = EntityTest::create();
$value = '2014-01-01T20:00:00';
$entity->set('field_datetime', $value);
$this->entityValidateAndSave($entity);
// Load the entity and ensure the field was saved correctly.
$id = $entity->id();
$entity = EntityTest::load($id);
$this->assertEquals($value, $entity->field_datetime[0]->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.
$this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
$this->fieldStorage->save();
$entity = EntityTest::create();
$value = '2014-01-01';
$entity->set('field_datetime', $value);
$this->entityValidateAndSave($entity);
// Load the entity and ensure the field was saved correctly.
$id = $entity->id();
$entity = EntityTest::load($id);
$this->assertEquals($value, $entity->field_datetime[0]->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): void {
$this->expectException(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 static 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): void {
$this->expectException(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 static 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']],
];
}
}

View File

@ -0,0 +1,235 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\datetime\Kernel\Views;
use Drupal\node\Entity\Node;
use Drupal\views\Views;
/**
* Tests the Drupal\datetime\Plugin\views\filter\Date handler.
*
* @group datetime
*/
class ArgumentDateTimeTest extends DateTimeHandlerTestBase {
/**
* {@inheritdoc}
*/
public static $testViews = ['test_argument_datetime'];
/**
* {@inheritdoc}
*/
protected function setUp($import_test_views = TRUE): void {
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) {
$node = Node::create([
'title' => $this->randomMachineName(8),
'type' => 'page',
'field_date' => [
'value' => $date,
],
]);
$node->save();
$this->nodes[] = $node;
}
}
/**
* Tests year argument.
*
* @see \Drupal\datetime\Plugin\views\argument\YearDate
*/
public function testDatetimeArgumentYear(): void {
$view = Views::getView('test_argument_datetime');
// The 'default' display has the 'year' argument.
$view->setDisplay('default');
$this->executeView($view, ['2000']);
$expected = [];
$expected[] = ['nid' => $this->nodes[0]->id()];
$this->assertIdenticalResultset($view, $expected, $this->map);
$view->destroy();
$view->setDisplay('default');
$this->executeView($view, ['2002']);
$expected = [];
$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();
}
/**
* Tests month argument.
*
* @see \Drupal\datetime\Plugin\views\argument\MonthDate
*/
public function testDatetimeArgumentMonth(): void {
$view = Views::getView('test_argument_datetime');
// The 'embed_1' display has the 'month' argument.
$view->setDisplay('embed_1');
$this->executeView($view, ['10']);
$expected = [];
$expected[] = ['nid' => $this->nodes[0]->id()];
$expected[] = ['nid' => $this->nodes[1]->id()];
$this->assertIdenticalResultset($view, $expected, $this->map);
$view->destroy();
$view->setDisplay('embed_1');
$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();
}
/**
* Tests day argument.
*
* @see \Drupal\datetime\Plugin\views\argument\DayDate
*/
public function testDatetimeArgumentDay(): void {
$view = Views::getView('test_argument_datetime');
// The 'embed_2' display has the 'day' argument.
$view->setDisplay('embed_2');
$this->executeView($view, ['10']);
$expected = [];
$expected[] = ['nid' => $this->nodes[0]->id()];
$expected[] = ['nid' => $this->nodes[1]->id()];
$this->assertIdenticalResultset($view, $expected, $this->map);
$view->destroy();
$view->setDisplay('embed_2');
$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();
}
/**
* Tests year, month, and day arguments combined.
*/
public function testDatetimeArgumentAll(): void {
$view = Views::getView('test_argument_datetime');
// The 'embed_3' display has year, month, and day arguments.
$view->setDisplay('embed_3');
$this->executeView($view, ['2000', '10', '10']);
$expected = [];
$expected[] = ['nid' => $this->nodes[0]->id()];
$this->assertIdenticalResultset($view, $expected, $this->map);
$view->destroy();
$view->setDisplay('embed_3');
$this->executeView($view, ['2002', '01', '01']);
$expected = [];
$expected[] = ['nid' => $this->nodes[2]->id()];
$this->assertIdenticalResultset($view, $expected, $this->map);
$view->destroy();
}
/**
* Tests week WW argument.
*/
public function testDatetimeArgumentWeek(): void {
$view = Views::getView('test_argument_datetime');
// The 'embed_4' display has WW argument.
$view->setDisplay('embed_4');
$this->executeView($view, ['41']);
$expected = [];
$expected[] = ['nid' => $this->nodes[0]->id()];
$expected[] = ['nid' => $this->nodes[1]->id()];
$this->assertIdenticalResultset($view, $expected, $this->map);
$view->destroy();
$view->setDisplay('embed_4');
$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();
}
/**
* Tests full_date CCYYMMDD argument.
*/
public function testDatetimeArgumentFullDate(): void {
$view = Views::getView('test_argument_datetime');
// The 'embed_5' display has CCYYMMDD argument.
$view->setDisplay('embed_5');
$this->executeView($view, ['20001010']);
$expected = [];
$expected[] = ['nid' => $this->nodes[0]->id()];
$this->assertIdenticalResultset($view, $expected, $this->map);
$view->destroy();
$view->setDisplay('embed_5');
$this->executeView($view, ['20020101']);
$expected = [];
$expected[] = ['nid' => $this->nodes[2]->id()];
$this->assertIdenticalResultset($view, $expected, $this->map);
$view->destroy();
}
/**
* Tests year_month CCYYMM argument.
*/
public function testDatetimeArgumentYearMonth(): void {
$view = Views::getView('test_argument_datetime');
// The 'embed_6' display has CCYYMM argument.
$view->setDisplay('embed_6');
$this->executeView($view, ['200010']);
$expected = [];
$expected[] = ['nid' => $this->nodes[0]->id()];
$this->assertIdenticalResultset($view, $expected, $this->map);
$view->destroy();
$view->setDisplay('embed_6');
$this->executeView($view, ['200201']);
$expected = [];
$expected[] = ['nid' => $this->nodes[2]->id()];
$this->assertIdenticalResultset($view, $expected, $this->map);
$view->destroy();
}
}

View File

@ -0,0 +1,150 @@
<?php
declare(strict_types=1);
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}
*/
protected static $modules = ['datetime_test', 'node', 'datetime', 'field'];
/**
* Name of the field.
*
* Note, this is used in the default test view.
*
* @var string
*/
protected static $fieldName = 'field_date';
/**
* Type of the field.
*
* @var string
*/
protected static $fieldType = 'datetime';
/**
* Nodes to test.
*
* @var \Drupal\node\NodeInterface[]
*/
protected $nodes = [];
/**
* Column map.
*
* @var string[]
*/
protected array $map;
/**
* {@inheritdoc}
*/
protected function setUp($import_test_views = TRUE): void {
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::$fieldName,
'entity_type' => 'node',
'type' => static::$fieldType,
'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(static::class, ['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(): int {
$user_now = new DateTimePlus('now', new \DateTimeZone(date_default_timezone_get()));
$utc_equivalent = new DateTimePlus($user_now->format('Y-m-d H:i:s'), new \DateTimeZone(DateTimeItemInterface::STORAGE_TIMEZONE));
return $utc_equivalent->getTimestamp();
}
/**
* 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),
];
}
}

View File

@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
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'];
/**
* Tests argument plugin schema.
*/
public function testDateTimeSchema(): void {
// 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->displayHandlers->get('default')->overrideOption('filters', $filters);
$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');
}
}

View File

@ -0,0 +1,232 @@
<?php
declare(strict_types=1);
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): void {
parent::setUp($import_test_views);
// Change field storage to date-only.
$storage = FieldStorageConfig::load('node.' . static::$fieldName);
$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;
}
// Add a node where the date field is empty.
$node = Node::create([
'title' => $this->randomMachineName(8),
'type' => 'page',
'field_date' => [],
]);
$node->save();
$this->nodes[] = $node;
}
/**
* Tests offsets with date-only fields.
*/
public function testDateOffsets(): void {
$view = Views::getView('test_filter_datetime');
$field = static::$fieldName . '_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 the empty operator.
$view->initHandlers();
$view->filter[$field]->operator = 'empty';
$view->setDisplay('default');
$this->executeView($view);
$expected_result = [
['nid' => $this->nodes[3]->id()],
];
$this->assertIdenticalResultset($view, $expected_result, $this->map);
$view->destroy();
// Test the not empty operator.
$view->initHandlers();
$view->filter[$field]->operator = 'not empty';
$view->setDisplay('default');
$this->executeView($view);
$expected_result = [
['nid' => $this->nodes[0]->id()],
['nid' => $this->nodes[1]->id()],
['nid' => $this->nodes[2]->id()],
];
$this->assertIdenticalResultset($view, $expected_result, $this->map);
$view->destroy();
}
}
/**
* Tests date filter with date-only fields.
*/
public function testDateIs(): void {
$view = Views::getView('test_filter_datetime');
$field = static::$fieldName . '_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): void {
foreach ($dates as $index => $date) {
$this->nodes[$index]->{static::$fieldName}->value = $date;
$this->nodes[$index]->save();
}
}
}

View File

@ -0,0 +1,205 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\datetime\Kernel\Views;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\node\Entity\Node;
use Drupal\views\Views;
/**
* Tests the Drupal\datetime\Plugin\views\filter\Date handler.
*
* @group datetime
*/
class FilterDateTimeTest extends DateTimeHandlerTestBase {
/**
* {@inheritdoc}
*/
public static $testViews = ['test_filter_datetime'];
/**
* For offset tests, set a date 1 day in the future.
*
* @var int
*/
protected static $date;
/**
* Use a non-UTC timezone.
*
* @var string
*/
protected static $timezone = 'America/Vancouver';
/**
* {@inheritdoc}
*/
protected function setUp($import_test_views = TRUE): void {
parent::setUp($import_test_views);
static::$date = \Drupal::time()->getRequestTime() + 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 = [
'2000-10-10T00:01:30',
'2001-10-10T12:12:12',
'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', DateTimeItemInterface::DATETIME_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE),
];
foreach ($dates as $date) {
$node = Node::create([
'title' => $this->randomMachineName(8),
'type' => 'page',
'field_date' => [
'value' => $date,
],
]);
$node->save();
$this->nodes[] = $node;
}
}
/**
* Tests filter operations.
*/
public function testDatetimeFilter(): void {
$this->_testOffset();
$this->_testBetween();
$this->_testExact();
}
/**
* Tests offset operations.
*/
protected function _testOffset(): void {
$view = Views::getView('test_filter_datetime');
$field = static::$fieldName . '_value';
// Test simple operations.
$view->initHandlers();
$view->filter[$field]->operator = '>';
$view->filter[$field]->value['type'] = 'offset';
$view->filter[$field]->value['value'] = '+1 hour';
$view->setDisplay('default');
$this->executeView($view);
$expected_result = [
['nid' => $this->nodes[3]->id()],
];
$this->assertIdenticalResultset($view, $expected_result, $this->map);
$view->destroy();
// Test offset for between operator.
$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 hour';
$view->setDisplay('default');
$this->executeView($view);
$expected_result = [
['nid' => $this->nodes[3]->id()],
];
$this->assertIdenticalResultset($view, $expected_result, $this->map);
}
/**
* Tests between operations.
*/
protected function _testBetween(): void {
$view = Views::getView('test_filter_datetime');
$field = static::$fieldName . '_value';
// Test between with min and max.
$view->initHandlers();
$view->filter[$field]->operator = 'between';
$view->filter[$field]->value['min'] = '2001-01-01';
$view->filter[$field]->value['max'] = '2002-01-01';
$view->setDisplay('default');
$this->executeView($view);
$expected_result = [
['nid' => $this->nodes[1]->id()],
];
$this->assertIdenticalResultset($view, $expected_result, $this->map);
$view->destroy();
// Test between with just max.
$view->initHandlers();
$view->filter[$field]->operator = 'between';
$view->filter[$field]->value['max'] = '2002-01-01';
$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();
// Test not between with min and max.
$view->initHandlers();
$view->filter[$field]->operator = 'not between';
$view->filter[$field]->value['min'] = '2001-01-01';
// Set maximum date to date of node 1 to test range borders.
$view->filter[$field]->value['max'] = '2001-10-10T12:12:12';
$view->setDisplay('default');
$this->executeView($view);
$expected_result = [
['nid' => $this->nodes[0]->id()],
['nid' => $this->nodes[2]->id()],
['nid' => $this->nodes[3]->id()],
];
$this->assertIdenticalResultset($view, $expected_result, $this->map);
$view->destroy();
// Test not between with just max.
$view->initHandlers();
$view->filter[$field]->operator = 'not between';
$view->filter[$field]->value['max'] = '2001-01-01';
$view->setDisplay('default');
$this->executeView($view);
$expected_result = [
['nid' => $this->nodes[1]->id()],
['nid' => $this->nodes[2]->id()],
['nid' => $this->nodes[3]->id()],
];
$this->assertIdenticalResultset($view, $expected_result, $this->map);
}
/**
* Tests exact date matching.
*/
protected function _testExact(): void {
$view = Views::getView('test_filter_datetime');
$field = static::$fieldName . '_value';
// Test between with min and max.
$view->initHandlers();
$view->filter[$field]->operator = '=';
$view->filter[$field]->value['min'] = '';
$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', DateTimeItemInterface::DATETIME_STORAGE_FORMAT, static::$timezone);
$view->setDisplay('default');
$this->executeView($view);
$expected_result = [
['nid' => $this->nodes[3]->id()],
];
$this->assertIdenticalResultset($view, $expected_result, $this->map);
$view->destroy();
}
}

View File

@ -0,0 +1,132 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\datetime\Kernel\Views;
use Drupal\node\Entity\Node;
use Drupal\views\Views;
/**
* Tests for core Drupal\datetime\Plugin\views\sort\Date handler.
*
* @group datetime
*/
class SortDateTimeTest extends DateTimeHandlerTestBase {
/**
* {@inheritdoc}
*/
public static $testViews = ['test_sort_datetime'];
/**
* {@inheritdoc}
*/
protected function setUp($import_test_views = TRUE): void {
parent::setUp($import_test_views);
// Add some basic test nodes.
$dates = [
'2014-10-10T00:03:00',
'2000-10-10T00:01:00',
'2000-10-10T00:02:00',
'2000-10-10T00:03:00',
'2000-10-10T00:03:02',
'2000-10-10T00:03:01',
'2000-10-10T00:03:03',
];
foreach ($dates as $date) {
$node = Node::create([
'title' => $this->randomMachineName(8),
'type' => 'page',
'field_date' => [
'value' => $date,
],
]);
$node->save();
$this->nodes[] = $node;
}
}
/**
* Tests the datetime sort handler.
*/
public function testDateTimeSort(): void {
$field = static::$fieldName . '_value';
$view = Views::getView('test_sort_datetime');
// Set granularity to 'minute', and the secondary node ID order should
// define the order of nodes with the same minute.
$view->initHandlers();
$view->sort[$field]->options['granularity'] = 'minute';
$view->setDisplay('default');
$this->executeView($view);
$expected_result = [
['nid' => $this->nodes[0]->id()],
['nid' => $this->nodes[3]->id()],
['nid' => $this->nodes[4]->id()],
['nid' => $this->nodes[5]->id()],
['nid' => $this->nodes[6]->id()],
['nid' => $this->nodes[2]->id()],
['nid' => $this->nodes[1]->id()],
];
$this->assertIdenticalResultset($view, $expected_result, $this->map);
$view->destroy();
// Check ASC.
$view->initHandlers();
$field = static::$fieldName . '_value';
$view->sort[$field]->options['order'] = 'ASC';
$view->setDisplay('default');
$this->executeView($view);
$expected_result = [
['nid' => $this->nodes[1]->id()],
['nid' => $this->nodes[2]->id()],
['nid' => $this->nodes[3]->id()],
['nid' => $this->nodes[5]->id()],
['nid' => $this->nodes[4]->id()],
['nid' => $this->nodes[6]->id()],
['nid' => $this->nodes[0]->id()],
];
$this->assertIdenticalResultset($view, $expected_result, $this->map);
$view->destroy();
// Change granularity to 'year', and the secondary node ID order should
// define the order of nodes with the same year.
$view->initHandlers();
$view->sort[$field]->options['granularity'] = 'year';
$view->sort[$field]->options['order'] = 'DESC';
$view->setDisplay('default');
$this->executeView($view);
$expected_result = [
['nid' => $this->nodes[0]->id()],
['nid' => $this->nodes[1]->id()],
['nid' => $this->nodes[2]->id()],
['nid' => $this->nodes[3]->id()],
['nid' => $this->nodes[4]->id()],
['nid' => $this->nodes[5]->id()],
['nid' => $this->nodes[6]->id()],
];
$this->assertIdenticalResultset($view, $expected_result, $this->map);
$view->destroy();
// Change granularity to 'second'.
$view->initHandlers();
$view->sort[$field]->options['granularity'] = 'second';
$view->sort[$field]->options['order'] = 'DESC';
$view->setDisplay('default');
$this->executeView($view);
$expected_result = [
['nid' => $this->nodes[0]->id()],
['nid' => $this->nodes[6]->id()],
['nid' => $this->nodes[4]->id()],
['nid' => $this->nodes[5]->id()],
['nid' => $this->nodes[3]->id()],
['nid' => $this->nodes[2]->id()],
['nid' => $this->nodes[1]->id()],
];
$this->assertIdenticalResultset($view, $expected_result, $this->map);
$view->destroy();
}
}

View File

@ -0,0 +1,147 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\datetime\Unit\Plugin\migrate\field;
use Drupal\datetime\Plugin\migrate\field\DateField;
use Drupal\migrate\MigrateException;
use Drupal\Tests\UnitTestCase;
// cspell:ignore todate
/**
* Provides unit tests for the DateField Plugin.
*
* @coversDefaultClass \Drupal\datetime\Plugin\migrate\field\DateField
*
* @group migrate
*/
class DateFieldTest extends UnitTestCase {
/**
* Tests defineValueProcessPipeline.
*
* @covers ::defineValueProcessPipeline
*
* @dataProvider providerTestDefineValueProcessPipeline
*/
public function testDefineValueProcessPipeline($data, $from_format, $to_format): void {
$migration = $this->createMock('Drupal\migrate\Plugin\MigrationInterface');
$pipeline = [
'plugin' => 'sub_process',
'source' => 'field_date',
'process' => [
'value' => [
'plugin' => 'format_date',
'from_format' => $from_format,
'to_format' => $to_format,
'source' => 'value',
],
],
];
// If there is a todate then add a process for the end value.
if (isset($data['field_definition']['data'])) {
$tmp = is_string($data['field_definition']['data']) ? unserialize($data['field_definition']['data']) : '';
$todate = $tmp['settings']['todate'] ?? NULL;
if (!empty($todate)) {
$pipeline['process']['end_value'] = [
'plugin' => 'format_date',
'from_format' => $from_format,
'to_format' => $to_format,
'source' => 'value2',
];
}
}
$migration->expects($this->once())
->method('mergeProcessOfProperty')
->with('field_date', $pipeline)
->willReturn($migration);
$plugin = new DateField([], '', []);
$plugin->defineValueProcessPipeline($migration, 'field_date', $data);
}
/**
* Provides data for testDefineValueProcessPipeline().
*/
public static function providerTestDefineValueProcessPipeline() {
return [
[['type' => 'date'], 'Y-m-d\TH:i:s', 'Y-m-d\TH:i:s'],
[['type' => 'datestamp'], 'U', 'U'],
[['type' => 'datetime'], 'Y-m-d H:i:s', 'Y-m-d\TH:i:s'],
[
[
'type' => 'datetime',
'field_definition' => [
'data' => serialize([
'settings' => [
'granularity' => [
'hour' => 0,
'minute' => 0,
'second' => 0,
],
],
]),
],
],
'Y-m-d H:i:s',
'Y-m-d',
],
[
[
'type' => 'date',
'field_definition' => [
'data' => serialize([
'settings' => [
'granularity' => [
0 => 'year',
1 => 'month',
],
'todate' => '',
],
]),
],
],
'Y-m-d\TH:i:s',
'Y-m-d',
],
'datetime with a todate' => [
[
'type' => 'datetime',
'field_definition' => [
'data' => serialize([
'settings' => [
'granularity' => [
'hour' => 0,
'minute' => 0,
'second' => 0,
],
'todate' => 'optional',
],
]),
],
],
'Y-m-d H:i:s',
'Y-m-d',
],
];
}
/**
* Tests invalid date types throw an exception.
*
* @covers ::defineValueProcessPipeline
*/
public function testDefineValueProcessPipelineException(): void {
$migration = $this->createMock('Drupal\migrate\Plugin\MigrationInterface');
$plugin = new DateField([], '', []);
$this->expectException(MigrateException::class);
$plugin->defineValueProcessPipeline($migration, 'field_date', ['type' => 'test']);
}
}