Update Composer, update everything

This commit is contained in:
Oliver Davies 2018-11-23 12:29:20 +00:00
parent ea3e94409f
commit dda5c284b6
19527 changed files with 1135420 additions and 351004 deletions

View file

@ -5,4 +5,4 @@ package: Testing
version: VERSION
core: 8.x
dependencies:
- views
- drupal:views

View file

@ -33,6 +33,7 @@ display:
relationship: none
table: node__field_date
order: DESC
granularity: second
plugin_id: datetime
id:
field: nid

View file

@ -106,7 +106,8 @@ abstract class DateTestBase extends BrowserTestBase {
* Creates a date test field.
*/
protected function createField() {
$field_name = Unicode::strtolower($this->randomMachineName());
$field_name = mb_strtolower($this->randomMachineName());
$field_label = Unicode::ucfirst(mb_strtolower($this->randomMachineName()));
$type = $this->getTestFieldType();
$widget_type = $formatter_type = $type . '_default';
@ -119,8 +120,9 @@ abstract class DateTestBase extends BrowserTestBase {
$this->fieldStorage->save();
$this->field = FieldConfig::create([
'field_storage' => $this->fieldStorage,
'label' => $field_label,
'bundle' => 'entity_test',
'description' => 'Description for ' . $field_name,
'description' => 'Description for ' . $field_label,
'required' => TRUE,
]);
$this->field->save();
@ -181,4 +183,20 @@ abstract class DateTestBase extends BrowserTestBase {
->save();
}
/**
* Massages test date values.
*
* If a date object is generated directly by a test, then it needs to be
* adjusted to behave like the computed date from the item.
*
* @param \Drupal\Core\Datetime\DrupalDateTime $date
* A date object directly generated by the test.
*/
protected function massageTestDate($date) {
if ($this->field->getSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
// Set the default time for date-only items.
$date->setDefaultDateTime();
}
}
}

View file

@ -3,10 +3,9 @@
namespace Drupal\Tests\datetime\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Datetime\Entity\DateFormat;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
@ -44,6 +43,7 @@ class DateTimeFieldTest extends DateTestBase {
foreach (static::$timezones as $timezone) {
$this->setSiteTimezone($timezone);
$this->assertEquals($timezone, $this->config('system.date')->get('timezone.default'), 'Time zone set to ' . $timezone);
// Display creation form.
$this->drupalGet('entity_test/add');
@ -57,7 +57,7 @@ class DateTimeFieldTest extends DateTestBase {
// mimic the user in a different timezone simply entering '2012-12-31' via
// the UI.
$value = '2012-12-31 00:00:00';
$date = new DrupalDateTime($value, DATETIME_STORAGE_TIMEZONE);
$date = new DrupalDateTime($value, DateTimeItemInterface::STORAGE_TIMEZONE);
// Submit a valid date and ensure it is accepted.
$date_format = DateFormat::load('html_date')->getPattern();
@ -99,7 +99,7 @@ class DateTimeFieldTest extends DateTestBase {
// Formats that display a time component for date-only fields will display
// the default time, so that is applied before calculating the expected
// value.
datetime_date_default_time($date);
$this->massageTestDate($date);
foreach ($options as $setting => $values) {
foreach ($values as $new_value) {
// Update the entity display settings.
@ -113,11 +113,16 @@ class DateTimeFieldTest extends DateTestBase {
case 'format_type':
// Verify that a date is displayed. Since this is a date-only
// field, it is expected to display the time as 00:00:00.
$expected = format_date($date->getTimestamp(), $new_value, '', DATETIME_STORAGE_TIMEZONE);
$expected_iso = format_date($date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', DATETIME_STORAGE_TIMEZONE);
$expected = format_date($date->getTimestamp(), $new_value, '', DateTimeItemInterface::STORAGE_TIMEZONE);
$expected_iso = format_date($date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', DateTimeItemInterface::STORAGE_TIMEZONE);
$output = $this->renderTestEntity($id);
$expected_markup = '<time datetime="' . $expected_iso . '" class="datetime">' . $expected . '</time>';
$this->assertContains($expected_markup, $output, SafeMarkup::format('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', ['%value' => $new_value, '%expected' => $expected, '%expected_iso' => $expected_iso]));
$this->assertContains($expected_markup, $output, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute in %timezone.', [
'%value' => $new_value,
'%expected' => $expected,
'%expected_iso' => $expected_iso,
'%timezone' => $timezone,
]));
break;
}
}
@ -129,9 +134,12 @@ class DateTimeFieldTest extends DateTestBase {
entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
->setComponent($field_name, $this->displayOptions)
->save();
$expected = $date->format(DATETIME_DATE_STORAGE_FORMAT);
$expected = $date->format(DateTimeItemInterface::DATE_STORAGE_FORMAT);
$output = $this->renderTestEntity($id);
$this->assertContains($expected, $output, SafeMarkup::format('Formatted date field using plain format displayed as %expected.', ['%expected' => $expected]));
$this->assertContains($expected, $output, new FormattableMarkup('Formatted date field using plain format displayed as %expected in %timezone.', [
'%expected' => $expected,
'%timezone' => $timezone,
]));
// Verify that the 'datetime_custom' formatter works.
$this->displayOptions['type'] = 'datetime_custom';
@ -141,7 +149,10 @@ class DateTimeFieldTest extends DateTestBase {
->save();
$expected = $date->format($this->displayOptions['settings']['date_format']);
$output = $this->renderTestEntity($id);
$this->assertContains($expected, $output, SafeMarkup::format('Formatted date field using datetime_custom format displayed as %expected.', ['%expected' => $expected]));
$this->assertContains($expected, $output, new FormattableMarkup('Formatted date field using datetime_custom format displayed as %expected in %timezone.', [
'%expected' => $expected,
'%timezone' => $timezone,
]));
// Test that allowed markup in custom format is preserved and XSS is
// removed.
@ -151,13 +162,16 @@ class DateTimeFieldTest extends DateTestBase {
->save();
$expected = '<strong>' . $date->format('m/d/Y') . '</strong>alert(String.fromCharCode(88,83,83))';
$output = $this->renderTestEntity($id);
$this->assertContains($expected, $output, new FormattableMarkup('Formatted date field using daterange_custom format displayed as %expected.', ['%expected' => $expected]));
$this->assertContains($expected, $output, new FormattableMarkup('Formatted date field using daterange_custom format displayed as %expected in %timezone.', [
'%expected' => $expected,
'%timezone' => $timezone,
]));
// Verify that the 'datetime_time_ago' formatter works for intervals in the
// past. First update the test entity so that the date difference always
// has the same interval. Since the database always stores UTC, and the
// interval will use this, force the test date to use UTC and not the local
// or user timezome.
// or user timezone.
$timestamp = REQUEST_TIME - 87654321;
$entity = EntityTest::load($id);
$field_name = $this->fieldStorage->getName();
@ -174,17 +188,20 @@ class DateTimeFieldTest extends DateTestBase {
entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
->setComponent($field_name, $this->displayOptions)
->save();
$expected = SafeMarkup::format($this->displayOptions['settings']['past_format'], [
'@interval' => $this->dateFormatter->formatTimeDiffSince($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']])
$expected = new FormattableMarkup($this->displayOptions['settings']['past_format'], [
'@interval' => $this->dateFormatter->formatTimeDiffSince($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]),
]);
$output = $this->renderTestEntity($id);
$this->assertContains((string) $expected, $output, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', ['%expected' => $expected]));
$this->assertContains((string) $expected, $output, new FormattableMarkup('Formatted date field using datetime_time_ago format displayed as %expected in %timezone.', [
'%expected' => $expected,
'%timezone' => $timezone,
]));
// Verify that the 'datetime_time_ago' formatter works for intervals in the
// future. First update the test entity so that the date difference always
// has the same interval. Since the database always stores UTC, and the
// interval will use this, force the test date to use UTC and not the local
// or user timezome.
// or user timezone.
$timestamp = REQUEST_TIME + 87654321;
$entity = EntityTest::load($id);
$field_name = $this->fieldStorage->getName();
@ -195,11 +212,14 @@ class DateTimeFieldTest extends DateTestBase {
entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
->setComponent($field_name, $this->displayOptions)
->save();
$expected = SafeMarkup::format($this->displayOptions['settings']['future_format'], [
'@interval' => $this->dateFormatter->formatTimeDiffUntil($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']])
$expected = new FormattableMarkup($this->displayOptions['settings']['future_format'], [
'@interval' => $this->dateFormatter->formatTimeDiffUntil($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]),
]);
$output = $this->renderTestEntity($id);
$this->assertContains((string) $expected, $output, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', ['%expected' => $expected]));
$this->assertContains((string) $expected, $output, new FormattableMarkup('Formatted date field using datetime_time_ago format displayed as %expected in %timezone.', [
'%expected' => $expected,
'%timezone' => $timezone,
]));
}
}
@ -208,6 +228,7 @@ class DateTimeFieldTest extends DateTestBase {
*/
public function testDatetimeField() {
$field_name = $this->fieldStorage->getName();
$field_label = $this->field->label();
// Change the field to a datetime field.
$this->fieldStorage->setSetting('datetime_type', 'datetime');
$this->fieldStorage->save();
@ -216,7 +237,7 @@ class DateTimeFieldTest extends DateTestBase {
$this->drupalGet('entity_test/add');
$this->assertFieldByName("{$field_name}[0][value][date]", '', 'Date element found.');
$this->assertFieldByName("{$field_name}[0][value][time]", '', 'Time element found.');
$this->assertFieldByXPath('//fieldset[@id="edit-' . $field_name . '-0"]/legend', $field_name, 'Fieldset and label found');
$this->assertFieldByXPath('//fieldset[@id="edit-' . $field_name . '-0"]/legend', $field_label, 'Fieldset and label found');
$this->assertFieldByXPath('//fieldset[@aria-describedby="edit-' . $field_name . '-0--description"]', NULL, 'ARIA described-by found');
$this->assertFieldByXPath('//div[@id="edit-' . $field_name . '-0--description"]', NULL, 'ARIA description found');
@ -262,7 +283,7 @@ class DateTimeFieldTest extends DateTestBase {
$expected_iso = format_date($date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', 'UTC');
$output = $this->renderTestEntity($id);
$expected_markup = '<time datetime="' . $expected_iso . '" class="datetime">' . $expected . '</time>';
$this->assertContains($expected_markup, $output, SafeMarkup::format('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', ['%value' => $new_value, '%expected' => $expected, '%expected_iso' => $expected_iso]));
$this->assertContains($expected_markup, $output, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', ['%value' => $new_value, '%expected' => $expected, '%expected_iso' => $expected_iso]));
break;
}
}
@ -274,9 +295,9 @@ class DateTimeFieldTest extends DateTestBase {
entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
->setComponent($field_name, $this->displayOptions)
->save();
$expected = $date->format(DATETIME_DATETIME_STORAGE_FORMAT);
$expected = $date->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
$output = $this->renderTestEntity($id);
$this->assertContains($expected, $output, SafeMarkup::format('Formatted date field using plain format displayed as %expected.', ['%expected' => $expected]));
$this->assertContains($expected, $output, new FormattableMarkup('Formatted date field using plain format displayed as %expected.', ['%expected' => $expected]));
// Verify that the 'datetime_custom' formatter works.
$this->displayOptions['type'] = 'datetime_custom';
@ -286,7 +307,7 @@ class DateTimeFieldTest extends DateTestBase {
->save();
$expected = $date->format($this->displayOptions['settings']['date_format']);
$output = $this->renderTestEntity($id);
$this->assertContains($expected, $output, SafeMarkup::format('Formatted date field using datetime_custom format displayed as %expected.', ['%expected' => $expected]));
$this->assertContains($expected, $output, new FormattableMarkup('Formatted date field using datetime_custom format displayed as %expected.', ['%expected' => $expected]));
// Verify that the 'timezone_override' setting works.
$this->displayOptions['type'] = 'datetime_custom';
@ -296,18 +317,18 @@ class DateTimeFieldTest extends DateTestBase {
->save();
$expected = $date->format($this->displayOptions['settings']['date_format'], ['timezone' => 'America/New_York']);
$output = $this->renderTestEntity($id);
$this->assertContains($expected, $output, SafeMarkup::format('Formatted date field using datetime_custom format displayed as %expected.', ['%expected' => $expected]));
$this->assertContains($expected, $output, new FormattableMarkup('Formatted date field using datetime_custom format displayed as %expected.', ['%expected' => $expected]));
// Verify that the 'datetime_time_ago' formatter works for intervals in the
// past. First update the test entity so that the date difference always
// has the same interval. Since the database always stores UTC, and the
// interval will use this, force the test date to use UTC and not the local
// or user timezome.
// or user timezone.
$timestamp = REQUEST_TIME - 87654321;
$entity = EntityTest::load($id);
$field_name = $this->fieldStorage->getName();
$date = DrupalDateTime::createFromTimestamp($timestamp, 'UTC');
$entity->{$field_name}->value = $date->format(DATETIME_DATETIME_STORAGE_FORMAT);
$entity->{$field_name}->value = $date->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
$entity->save();
$this->displayOptions['type'] = 'datetime_time_ago';
@ -319,32 +340,32 @@ class DateTimeFieldTest extends DateTestBase {
entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
->setComponent($field_name, $this->displayOptions)
->save();
$expected = SafeMarkup::format($this->displayOptions['settings']['past_format'], [
'@interval' => $this->dateFormatter->formatTimeDiffSince($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']])
$expected = new FormattableMarkup($this->displayOptions['settings']['past_format'], [
'@interval' => $this->dateFormatter->formatTimeDiffSince($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]),
]);
$output = $this->renderTestEntity($id);
$this->assertContains((string) $expected, $output, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', ['%expected' => $expected]));
$this->assertContains((string) $expected, $output, new FormattableMarkup('Formatted date field using datetime_time_ago format displayed as %expected.', ['%expected' => $expected]));
// Verify that the 'datetime_time_ago' formatter works for intervals in the
// future. First update the test entity so that the date difference always
// has the same interval. Since the database always stores UTC, and the
// interval will use this, force the test date to use UTC and not the local
// or user timezome.
// or user timezone.
$timestamp = REQUEST_TIME + 87654321;
$entity = EntityTest::load($id);
$field_name = $this->fieldStorage->getName();
$date = DrupalDateTime::createFromTimestamp($timestamp, 'UTC');
$entity->{$field_name}->value = $date->format(DATETIME_DATETIME_STORAGE_FORMAT);
$entity->{$field_name}->value = $date->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
$entity->save();
entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full')
->setComponent($field_name, $this->displayOptions)
->save();
$expected = SafeMarkup::format($this->displayOptions['settings']['future_format'], [
'@interval' => $this->dateFormatter->formatTimeDiffUntil($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']])
$expected = new FormattableMarkup($this->displayOptions['settings']['future_format'], [
'@interval' => $this->dateFormatter->formatTimeDiffUntil($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]),
]);
$output = $this->renderTestEntity($id);
$this->assertContains((string) $expected, $output, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', ['%expected' => $expected]));
$this->assertContains((string) $expected, $output, new FormattableMarkup('Formatted date field using datetime_time_ago format displayed as %expected.', ['%expected' => $expected]));
}
/**
@ -352,6 +373,7 @@ class DateTimeFieldTest extends DateTestBase {
*/
public function testDatelistWidget() {
$field_name = $this->fieldStorage->getName();
$field_label = $this->field->label();
// Ensure field is set to a date only field.
$this->fieldStorage->setSetting('datetime_type', 'date');
@ -370,7 +392,7 @@ class DateTimeFieldTest extends DateTestBase {
// Display creation form.
$this->drupalGet('entity_test/add');
$this->assertFieldByXPath('//fieldset[@id="edit-' . $field_name . '-0"]/legend', $field_name, 'Fieldset and label found');
$this->assertFieldByXPath('//fieldset[@id="edit-' . $field_name . '-0"]/legend', $field_label, 'Fieldset and label found');
$this->assertFieldByXPath('//fieldset[@aria-describedby="edit-' . $field_name . '-0--description"]', NULL, 'ARIA described-by found');
$this->assertFieldByXPath('//div[@id="edit-' . $field_name . '-0--description"]', NULL, 'ARIA description found');
@ -511,7 +533,7 @@ class DateTimeFieldTest extends DateTestBase {
\Drupal::entityManager()->clearCachedFieldDefinitions();
// Test the widget for validation notifications.
foreach ($this->datelistDataProvider() as $data) {
foreach ($this->datelistDataProvider($field_label) as $data) {
list($date_value, $expected) = $data;
// Display creation form.
@ -562,33 +584,57 @@ class DateTimeFieldTest extends DateTestBase {
/**
* The data provider for testing the validation of the datelist widget.
*
* @param string $field_label
* The label of the field being tested.
*
* @return array
* An array of datelist input permutations to test.
*/
protected function datelistDataProvider() {
protected function datelistDataProvider($field_label) {
return [
// Nothing selected.
[
['year' => '', 'month' => '', 'day' => '', 'hour' => '', 'minute' => ''],
["The $field_label date is required."],
],
// Year only selected, validation error on Month, Day, Hour, Minute.
[['year' => 2012, 'month' => '', 'day' => '', 'hour' => '', 'minute' => ''], [
'A value must be selected for month.',
'A value must be selected for day.',
'A value must be selected for hour.',
'A value must be selected for minute.',
]],
[
['year' => 2012, 'month' => '', 'day' => '', 'hour' => '', 'minute' => ''],
[
"The $field_label date is incomplete.",
'A value must be selected for month.',
'A value must be selected for day.',
'A value must be selected for hour.',
'A value must be selected for minute.',
],
],
// Year and Month selected, validation error on Day, Hour, Minute.
[['year' => 2012, 'month' => '12', 'day' => '', 'hour' => '', 'minute' => ''], [
'A value must be selected for day.',
'A value must be selected for hour.',
'A value must be selected for minute.',
]],
[
['year' => 2012, 'month' => '12', 'day' => '', 'hour' => '', 'minute' => ''],
[
"The $field_label date is incomplete.",
'A value must be selected for day.',
'A value must be selected for hour.',
'A value must be selected for minute.',
],
],
// Year, Month and Day selected, validation error on Hour, Minute.
[['year' => 2012, 'month' => '12', 'day' => '31', 'hour' => '', 'minute' => ''], [
'A value must be selected for hour.',
'A value must be selected for minute.',
]],
[
['year' => 2012, 'month' => '12', 'day' => '31', 'hour' => '', 'minute' => ''],
[
"The $field_label date is incomplete.",
'A value must be selected for hour.',
'A value must be selected for minute.',
],
],
// Year, Month, Day and Hour selected, validation error on Minute only.
[['year' => 2012, 'month' => '12', 'day' => '31', 'hour' => '0', 'minute' => ''], [
'A value must be selected for minute.',
]],
[
['year' => 2012, 'month' => '12', 'day' => '31', 'hour' => '0', 'minute' => ''],
[
"The $field_label date is incomplete.",
'A value must be selected for minute.',
],
],
];
}
@ -600,7 +646,7 @@ class DateTimeFieldTest extends DateTestBase {
$this->drupalCreateContentType(['type' => 'date_content']);
// Create a field storage with settings to validate.
$field_name = Unicode::strtolower($this->randomMachineName());
$field_name = mb_strtolower($this->randomMachineName());
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'node',
@ -620,6 +666,7 @@ class DateTimeFieldTest extends DateTestBase {
foreach (static::$timezones as $timezone) {
$this->setSiteTimezone($timezone);
$this->assertEquals($timezone, $this->config('system.date')->get('timezone.default'), 'Time zone set to ' . $timezone);
// Set now as default_value.
$field_edit = [
@ -647,7 +694,7 @@ class DateTimeFieldTest extends DateTestBase {
$new_node = Node::create(['type' => 'date_content']);
$expected_date = new DrupalDateTime('now', drupal_get_user_timezone());
$this->assertEqual($new_node->get($field_name)
->offsetGet(0)->value, $expected_date->format(DATETIME_DATE_STORAGE_FORMAT));
->offsetGet(0)->value, $expected_date->format(DateTimeItemInterface::DATE_STORAGE_FORMAT));
// Set an invalid relative default_value to test validation.
$field_edit = [
@ -686,7 +733,7 @@ class DateTimeFieldTest extends DateTestBase {
$new_node = Node::create(['type' => 'date_content']);
$expected_date = new DrupalDateTime('+90 days', drupal_get_user_timezone());
$this->assertEqual($new_node->get($field_name)
->offsetGet(0)->value, $expected_date->format(DATETIME_DATE_STORAGE_FORMAT));
->offsetGet(0)->value, $expected_date->format(DateTimeItemInterface::DATE_STORAGE_FORMAT));
// Remove default value.
$field_edit = [
@ -806,7 +853,7 @@ class DateTimeFieldTest extends DateTestBase {
$this->drupalCreateContentType(['type' => 'date_content']);
// Create a field storage with settings to validate.
$field_name = Unicode::strtolower($this->randomMachineName());
$field_name = mb_strtolower($this->randomMachineName());
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'node',

View file

@ -0,0 +1,122 @@
<?php
namespace Drupal\Tests\datetime\Functional;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\BrowserTestBase;
/**
* Tests the functionality of DateTimeTimeAgoFormatter field formatter.
*
* @group field
*/
class DateTimeTimeAgoFormatterTest extends BrowserTestBase {
/**
* An array of display options to pass to entity_get_display().
*
* @var array
*/
protected $displayOptions;
/**
* A field storage to use in this test class.
*
* @var \Drupal\field\Entity\FieldStorageConfig
*/
protected $fieldStorage;
/**
* The field used in this test class.
*
* @var \Drupal\field\Entity\FieldConfig
*/
protected $field;
/**
* {@inheritdoc}
*/
public static $modules = ['datetime', 'entity_test', 'field_ui'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$web_user = $this->drupalCreateUser([
'access administration pages',
'view test entity',
'administer entity_test content',
'administer entity_test fields',
'administer entity_test display',
'administer entity_test form display',
'view the administration theme',
]);
$this->drupalLogin($web_user);
$field_name = 'field_datetime';
$type = 'datetime';
$widget_type = 'datetime_default';
$formatter_type = 'datetime_time_ago';
$this->fieldStorage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test',
'type' => $type,
]);
$this->fieldStorage->save();
$this->field = FieldConfig::create([
'field_storage' => $this->fieldStorage,
'bundle' => 'entity_test',
'required' => TRUE,
]);
$this->field->save();
EntityFormDisplay::load('entity_test.entity_test.default')
->setComponent($field_name, ['type' => $widget_type])
->save();
$this->displayOptions = [
'type' => $formatter_type,
'label' => 'hidden',
];
EntityViewDisplay::create([
'targetEntityType' => $this->field->getTargetEntityTypeId(),
'bundle' => $this->field->getTargetBundle(),
'mode' => 'full',
'status' => TRUE,
])->setComponent($field_name, $this->displayOptions)
->save();
}
/**
* Tests the formatter settings.
*/
public function testSettings() {
$this->drupalGet('entity_test/structure/entity_test/display');
$edit = [
'fields[field_datetime][region]' => 'content',
'fields[field_datetime][type]' => 'datetime_time_ago',
];
$this->drupalPostForm(NULL, $edit, t('Save'));
$this->drupalPostForm(NULL, [], 'field_datetime_settings_edit');
$edit = [
'fields[field_datetime][settings_edit_form][settings][future_format]' => 'ends in @interval',
'fields[field_datetime][settings_edit_form][settings][past_format]' => 'started @interval ago',
'fields[field_datetime][settings_edit_form][settings][granularity]' => 3,
];
$this->drupalPostForm(NULL, $edit, 'Update');
$this->drupalPostForm(NULL, [], 'Save');
$this->assertSession()->pageTextContains('ends in 1 year 1 month 1 week');
$this->assertSession()->pageTextContains('started 1 year 1 month 1 week ago');
}
}

View file

@ -0,0 +1,154 @@
<?php
namespace Drupal\Tests\datetime\Functional\EntityResource\EntityTest;
use Drupal\Core\Url;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\entity_test\Functional\Rest\EntityTestResourceTestBase;
use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
use GuzzleHttp\RequestOptions;
/**
* Tests the datetime field constraint with 'date' items.
*
* @group datetime
*/
class EntityTestDateonlyTest extends EntityTestResourceTestBase {
use AnonResourceTestTrait;
/**
* The ISO date string to use throughout the test.
*
* @var string
*/
protected static $dateString = '2017-03-01';
/**
* Datetime test field name.
*
* @var string
*/
protected static $fieldName = 'field_dateonly';
/**
* {@inheritdoc}
*/
public static $modules = ['datetime', 'entity_test'];
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
// Add datetime field.
FieldStorageConfig::create([
'field_name' => static::$fieldName,
'type' => 'datetime',
'entity_type' => static::$entityTypeId,
'settings' => ['datetime_type' => DateTimeItem::DATETIME_TYPE_DATE],
])
->save();
FieldConfig::create([
'field_name' => static::$fieldName,
'entity_type' => static::$entityTypeId,
'bundle' => $this->entity->bundle(),
'settings' => ['default_value' => static::$dateString],
])
->save();
// Reload entity so that it has the new field.
$this->entity = $this->entityStorage->load($this->entity->id());
$this->entity->set(static::$fieldName, ['value' => static::$dateString]);
$this->entity->save();
}
/**
* {@inheritdoc}
*/
protected function createEntity() {
$entity_test = EntityTest::create([
'name' => 'Llama',
'type' => static::$entityTypeId,
static::$fieldName => static::$dateString,
]);
$entity_test->setOwnerId(0);
$entity_test->save();
return $entity_test;
}
/**
* {@inheritdoc}
*/
protected function getExpectedNormalizedEntity() {
return parent::getExpectedNormalizedEntity() + [
static::$fieldName => [
[
'value' => $this->entity->get(static::$fieldName)->value,
],
],
];
}
/**
* {@inheritdoc}
*/
protected function getNormalizedPostEntity() {
return parent::getNormalizedPostEntity() + [
static::$fieldName => [
[
'value' => static::$dateString,
],
],
];
}
/**
* {@inheritdoc}
*/
protected function assertNormalizationEdgeCases($method, Url $url, array $request_options) {
parent::assertNormalizationEdgeCases($method, $url, $request_options);
if ($this->entity->getEntityType()->hasKey('bundle')) {
$fieldName = static::$fieldName;
// DX: 422 when date type is incorrect.
$normalization = $this->getNormalizedPostEntity();
$normalization[static::$fieldName][0]['value'] = [
'2017', '03', '01',
];
$request_options[RequestOptions::BODY] = $this->serializer->encode($normalization, static::$format);
$response = $this->request($method, $url, $request_options);
$message = "Unprocessable Entity: validation failed.\n{$fieldName}.0: The datetime value must be a string.\n{$fieldName}.0.value: This value should be of the correct primitive type.\n";
$this->assertResourceErrorResponse(422, $message, $response);
// DX: 422 when date format is incorrect.
$normalization = $this->getNormalizedPostEntity();
$value = '2017-03-01T01:02:03';
$normalization[static::$fieldName][0]['value'] = $value;
$request_options[RequestOptions::BODY] = $this->serializer->encode($normalization, static::$format);
$response = $this->request($method, $url, $request_options);
$message = "Unprocessable Entity: validation failed.\n{$fieldName}.0: The datetime value '{$value}' is invalid for the format 'Y-m-d'\n";
$this->assertResourceErrorResponse(422, $message, $response);
// DX: 422 when value is not a valid date.
$normalization = $this->getNormalizedPostEntity();
$value = '2017-13-55';
$normalization[static::$fieldName][0]['value'] = $value;
$request_options[RequestOptions::BODY] = $this->serializer->encode($normalization, static::$format);
$response = $this->request($method, $url, $request_options);
$message = "Unprocessable Entity: validation failed.\n{$fieldName}.0: The datetime value '{$value}' did not parse properly for the format 'Y-m-d'\n{$fieldName}.0.value: This value should be of the correct primitive type.\n";
$this->assertResourceErrorResponse(422, $message, $response);
}
}
}

View file

@ -0,0 +1,154 @@
<?php
namespace Drupal\Tests\datetime\Functional\EntityResource\EntityTest;
use Drupal\Core\Url;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\entity_test\Functional\Rest\EntityTestResourceTestBase;
use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
use GuzzleHttp\RequestOptions;
/**
* Tests the datetime field constraint with 'datetime' items.
*
* @group datetime
*/
class EntityTestDatetimeTest extends EntityTestResourceTestBase {
use AnonResourceTestTrait;
/**
* The ISO date string to use throughout the test.
*
* @var string
*/
protected static $dateString = '2017-03-01T20:02:00';
/**
* Datetime test field name.
*
* @var string
*/
protected static $fieldName = 'field_datetime';
/**
* {@inheritdoc}
*/
public static $modules = ['datetime', 'entity_test'];
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
// Add datetime field.
FieldStorageConfig::create([
'field_name' => static::$fieldName,
'type' => 'datetime',
'entity_type' => static::$entityTypeId,
'settings' => ['datetime_type' => DateTimeItem::DATETIME_TYPE_DATETIME],
])
->save();
FieldConfig::create([
'field_name' => static::$fieldName,
'entity_type' => static::$entityTypeId,
'bundle' => $this->entity->bundle(),
'settings' => ['default_value' => static::$dateString],
])
->save();
// Reload entity so that it has the new field.
$this->entity = $this->entityStorage->load($this->entity->id());
$this->entity->set(static::$fieldName, ['value' => static::$dateString]);
$this->entity->save();
}
/**
* {@inheritdoc}
*/
protected function createEntity() {
$entity_test = EntityTest::create([
'name' => 'Llama',
'type' => static::$entityTypeId,
static::$fieldName => static::$dateString,
]);
$entity_test->setOwnerId(0);
$entity_test->save();
return $entity_test;
}
/**
* {@inheritdoc}
*/
protected function getExpectedNormalizedEntity() {
return parent::getExpectedNormalizedEntity() + [
static::$fieldName => [
[
'value' => $this->entity->get(static::$fieldName)->value,
],
],
];
}
/**
* {@inheritdoc}
*/
protected function getNormalizedPostEntity() {
return parent::getNormalizedPostEntity() + [
static::$fieldName => [
[
'value' => static::$dateString,
],
],
];
}
/**
* {@inheritdoc}
*/
protected function assertNormalizationEdgeCases($method, Url $url, array $request_options) {
parent::assertNormalizationEdgeCases($method, $url, $request_options);
if ($this->entity->getEntityType()->hasKey('bundle')) {
$fieldName = static::$fieldName;
// DX: 422 when date type is incorrect.
$normalization = $this->getNormalizedPostEntity();
$normalization[static::$fieldName][0]['value'] = [
'2017', '03', '01', '21', '53', '00',
];
$request_options[RequestOptions::BODY] = $this->serializer->encode($normalization, static::$format);
$response = $this->request($method, $url, $request_options);
$message = "Unprocessable Entity: validation failed.\n{$fieldName}.0: The datetime value must be a string.\n{$fieldName}.0.value: This value should be of the correct primitive type.\n";
$this->assertResourceErrorResponse(422, $message, $response);
// DX: 422 when date format is incorrect.
$normalization = $this->getNormalizedPostEntity();
$value = '2017-03-01';
$normalization[static::$fieldName][0]['value'] = $value;
$request_options[RequestOptions::BODY] = $this->serializer->encode($normalization, static::$format);
$response = $this->request($method, $url, $request_options);
$message = "Unprocessable Entity: validation failed.\n{$fieldName}.0: The datetime value '{$value}' is invalid for the format 'Y-m-d\\TH:i:s'\n";
$this->assertResourceErrorResponse(422, $message, $response);
// DX: 422 when date format is incorrect.
$normalization = $this->getNormalizedPostEntity();
$value = '2017-13-55T20:02:00';
$normalization[static::$fieldName][0]['value'] = $value;
$request_options[RequestOptions::BODY] = $this->serializer->encode($normalization, static::$format);
$response = $this->request($method, $url, $request_options);
$message = "Unprocessable Entity: validation failed.\n{$fieldName}.0: The datetime value '{$value}' did not parse properly for the format 'Y-m-d\\TH:i:s'\n{$fieldName}.0.value: This value should be of the correct primitive type.\n";
$this->assertResourceErrorResponse(422, $message, $response);
}
}
}

View file

@ -5,6 +5,7 @@ namespace Drupal\Tests\datetime\Kernel;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
@ -80,16 +81,19 @@ class DateTimeItemTest extends FieldKernelTestBase {
$this->assertTrue($entity->field_datetime[0] instanceof FieldItemInterface, 'Field item implements interface.');
$this->assertEqual($entity->field_datetime->value, $value);
$this->assertEqual($entity->field_datetime[0]->value, $value);
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
// Verify changing the date value.
$new_value = '2016-11-04T00:21:00';
$entity->field_datetime->value = $new_value;
$this->assertEqual($entity->field_datetime->value, $new_value);
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
// Read changed entity and assert changed values.
$this->entityValidateAndSave($entity);
$entity = EntityTest::load($id);
$this->assertEqual($entity->field_datetime->value, $new_value);
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
// Test the generateSampleValue() method.
$entity = EntityTest::create();
@ -118,20 +122,35 @@ class DateTimeItemTest extends FieldKernelTestBase {
$this->assertTrue($entity->field_datetime[0] instanceof FieldItemInterface, 'Field item implements interface.');
$this->assertEqual($entity->field_datetime->value, $value);
$this->assertEqual($entity->field_datetime[0]->value, $value);
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
$this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
$entity->field_datetime->date->setDefaultDateTime();
$this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
// Verify changing the date value.
$new_value = '2016-11-04';
$entity->field_datetime->value = $new_value;
$this->assertEqual($entity->field_datetime->value, $new_value);
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
$this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
$entity->field_datetime->date->setDefaultDateTime();
$this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
// Read changed entity and assert changed values.
$this->entityValidateAndSave($entity);
$entity = EntityTest::load($id);
$this->assertEqual($entity->field_datetime->value, $new_value);
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
$this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
$entity->field_datetime->date->setDefaultDateTime();
$this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
// Test the generateSampleValue() method.
$entity = EntityTest::create();
$entity->field_datetime->generateSampleItems();
$this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
$entity->field_datetime->date->setDefaultDateTime();
$this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));
$this->entityValidateAndSave($entity);
}
@ -152,6 +171,7 @@ class DateTimeItemTest extends FieldKernelTestBase {
$id = $entity->id();
$entity = EntityTest::load($id);
$this->assertEqual($entity->field_datetime[0]->value, $value, 'DateTimeItem::setValue() works with string value.');
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
// Test DateTimeItem::setValue() using property array.
$entity = EntityTest::create();
@ -162,6 +182,7 @@ class DateTimeItemTest extends FieldKernelTestBase {
$id = $entity->id();
$entity = EntityTest::load($id);
$this->assertEqual($entity->field_datetime[0]->value, $value, 'DateTimeItem::setValue() works with array value.');
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
// Test a date-only field.
$this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
@ -176,6 +197,7 @@ class DateTimeItemTest extends FieldKernelTestBase {
$id = $entity->id();
$entity = EntityTest::load($id);
$this->assertEqual($entity->field_datetime[0]->value, $value, 'DateTimeItem::setValue() works with string value.');
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
// Test DateTimeItem::setValue() using property array.
$entity = EntityTest::create();
@ -186,6 +208,7 @@ class DateTimeItemTest extends FieldKernelTestBase {
$id = $entity->id();
$entity = EntityTest::load($id);
$this->assertEqual($entity->field_datetime[0]->value, $value, 'DateTimeItem::setValue() works with array value.');
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
}
/**
@ -205,6 +228,7 @@ class DateTimeItemTest extends FieldKernelTestBase {
$id = $entity->id();
$entity = EntityTest::load($id);
$this->assertEqual($entity->field_datetime[0]->value, $value, '"Value" property can be set directly.');
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
// Test Date::setValue() with a date-only field.
// Test a date+time field.
@ -219,6 +243,107 @@ class DateTimeItemTest extends FieldKernelTestBase {
$id = $entity->id();
$entity = EntityTest::load($id);
$this->assertEqual($entity->field_datetime[0]->value, $value, '"Value" property can be set directly.');
$this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());
}
/**
* Tests the constraint validations for fields with date and time.
*
* @dataProvider datetimeValidationProvider
*/
public function testDatetimeValidation($value) {
$this->setExpectedException(\PHPUnit_Framework_AssertionFailedError::class);
$this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATETIME);
$this->fieldStorage->save();
$entity = EntityTest::create();
$entity->set('field_datetime', $value);
$this->entityValidateAndSave($entity);
}
/**
* Provider for testDatetimeValidation().
*/
public function datetimeValidationProvider() {
return [
// Valid ISO 8601 dates, but unsupported by DateTimeItem.
['2014-01-01T20:00:00Z'],
['2014-01-01T20:00:00+04:00'],
['2014-01-01T20:00:00+0400'],
['2014-01-01T20:00:00+04'],
['2014-01-01T20:00:00.123'],
['2014-01-01T200000'],
['2014-01-01T2000'],
['2014-01-01T20'],
['20140101T20:00:00'],
['2014-01T20:00:00'],
['2014-001T20:00:00'],
['2014001T20:00:00'],
// Valid date strings, but unsupported by DateTimeItem.
['2016-11-03 20:52:00'],
['Thu, 03 Nov 2014 20:52:00 -0400'],
['Thursday, November 3, 2016 - 20:52'],
['Thu, 11/03/2016 - 20:52'],
['11/03/2016 - 20:52'],
// Invalid date strings.
['YYYY-01-01T20:00:00'],
['2014-MM-01T20:00:00'],
['2014-01-DDT20:00:00'],
['2014-01-01Thh:00:00'],
['2014-01-01T20:mm:00'],
['2014-01-01T20:00:ss'],
// Invalid dates.
['2014-13-13T20:00:00'],
['2014-01-55T20:00:00'],
['2014-01-01T25:00:00'],
['2014-01-01T00:70:00'],
['2014-01-01T00:00:70'],
// Proper format for different field setting.
['2014-01-01'],
// Wrong input type.
[['2014', '01', '01', '00', '00', '00']],
];
}
/**
* Tests the constraint validations for fields with date only.
*
* @dataProvider dateonlyValidationProvider
*/
public function testDateonlyValidation($value) {
$this->setExpectedException(\PHPUnit_Framework_AssertionFailedError::class);
$this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
$this->fieldStorage->save();
$entity = EntityTest::create();
$entity->set('field_datetime', $value);
$this->entityValidateAndSave($entity);
}
/**
* Provider for testDatetimeValidation().
*/
public function dateonlyValidationProvider() {
return [
// Valid date strings, but unsupported by DateTimeItem.
['Thu, 03 Nov 2014'],
['Thursday, November 3, 2016'],
['Thu, 11/03/2016'],
['11/03/2016'],
// Invalid date strings.
['YYYY-01-01'],
['2014-MM-01'],
['2014-01-DD'],
// Invalid dates.
['2014-13-01'],
['2014-01-55'],
// Proper format for different field setting.
['2014-01-01T20:00:00'],
// Wrong input type.
[['2014', '01', '01']],
];
}
}

View file

@ -0,0 +1,233 @@
<?php
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) {
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;
}
}
/**
* Test year argument.
*
* @see \Drupal\datetime\Plugin\views\argument\YearDate
*/
public function testDatetimeArgumentYear() {
$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();
}
/**
* Test month argument.
*
* @see \Drupal\datetime\Plugin\views\argument\MonthDate
*/
public function testDatetimeArgumentMonth() {
$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();
}
/**
* Test day argument.
*
* @see \Drupal\datetime\Plugin\views\argument\DayDate
*/
public function testDatetimeArgumentDay() {
$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();
}
/**
* Test year, month, and day arguments combined.
*/
public function testDatetimeArgumentAll() {
$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();
}
/**
* Test week WW argument.
*/
public function testDatetimeArgumentWeek() {
$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();
}
/**
* Test full_date CCYYMMDD argument.
*/
public function testDatetimeArgumentFullDate() {
$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();
}
/**
* Test year_month CCYYMM argument.
*/
public function testDatetimeArgumentYearMonth() {
$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,141 @@
<?php
namespace Drupal\Tests\datetime\Kernel\Views;
use Drupal\Component\Datetime\DateTimePlus;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\views\Tests\ViewTestData;
use Drupal\field\Entity\FieldStorageConfig;
/**
* Base class for testing datetime handlers.
*/
abstract class DateTimeHandlerTestBase extends ViewsKernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['datetime_test', 'node', 'datetime', 'field'];
/**
* Name of the field.
*
* Note, this is used in the default test view.
*
* @var string
*/
protected static $field_name = 'field_date';
/**
* Type of the field.
*
* @var string
*/
protected static $field_type = 'datetime';
/**
* Nodes to test.
*
* @var \Drupal\node\NodeInterface[]
*/
protected $nodes = [];
/**
* {@inheritdoc}
*/
protected function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);
$this->installSchema('node', 'node_access');
$this->installEntitySchema('node');
$this->installEntitySchema('user');
// Add a date field to page nodes.
$node_type = NodeType::create([
'type' => 'page',
'name' => 'page',
]);
$node_type->save();
$fieldStorage = FieldStorageConfig::create([
'field_name' => static::$field_name,
'entity_type' => 'node',
'type' => static::$field_type,
'settings' => ['datetime_type' => DateTimeItem::DATETIME_TYPE_DATETIME],
]);
$fieldStorage->save();
$field = FieldConfig::create([
'field_storage' => $fieldStorage,
'bundle' => 'page',
'required' => TRUE,
]);
$field->save();
// Views needs to be aware of the new field.
$this->container->get('views.views_data')->clear();
// Set column map.
$this->map = [
'nid' => 'nid',
];
// Load test views.
ViewTestData::createTestViews(get_class($this), ['datetime_test']);
}
/**
* Sets the site timezone to a given timezone.
*
* @param string $timezone
* The timezone identifier to set.
*/
protected function setSiteTimezone($timezone) {
// Set an explicit site timezone, and disallow per-user timezones.
$this->config('system.date')
->set('timezone.user.configurable', 0)
->set('timezone.default', $timezone)
->save();
}
/**
* Returns UTC timestamp of user's TZ 'now'.
*
* The date field stores date_only values without conversion, considering them
* already as UTC. This method returns the UTC equivalent of user's 'now' as a
* unix timestamp, so they match using Y-m-d format.
*
* @return int
* Unix timestamp.
*/
protected function getUTCEquivalentOfUserNowAsTimestamp() {
$user_now = new DateTimePlus('now', new \DateTimeZone(drupal_get_user_timezone()));
$utc_equivalent = new DateTimePlus($user_now->format('Y-m-d H:i:s'), new \DateTimeZone(DateTimeItemInterface::STORAGE_TIMEZONE));
return $utc_equivalent->getTimestamp();
}
/**
* Returns an array formatted date_only values relative to timestamp.
*
* @param int $timestamp
* Unix Timestamp used as 'today'.
*
* @return array
* An array of DateTimeItemInterface::DATE_STORAGE_FORMAT date values. In
* order tomorrow, today and yesterday.
*/
protected function getRelativeDateValuesFromTimestamp($timestamp) {
return [
// Tomorrow.
\Drupal::service('date.formatter')->format($timestamp + 86400, 'custom', DateTimeItemInterface::DATE_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE),
// Today.
\Drupal::service('date.formatter')->format($timestamp, 'custom', DateTimeItemInterface::DATE_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE),
// Yesterday.
\Drupal::service('date.formatter')->format($timestamp - 86400, 'custom', DateTimeItemInterface::DATE_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE),
];
}
}

View file

@ -0,0 +1,52 @@
<?php
namespace Drupal\Tests\datetime\Kernel\Views;
use Drupal\Tests\SchemaCheckTestTrait;
use Drupal\views\Views;
/**
* Tests the Drupal\datetime\Plugin\views schemas.
*
* @group datetime
*/
class DateTimeSchemaTest extends DateTimeHandlerTestBase {
use SchemaCheckTestTrait;
/**
* {@inheritdoc}
*/
public static $testViews = ['test_argument_datetime', 'test_filter_datetime', 'test_sort_datetime'];
/**
* Test argument plugin schema.
*/
public function testDateTimeSchema() {
// Test argument schema.
$view = Views::getView('test_argument_datetime');
$view->initHandlers();
$view->setDisplay('default');
$arguments = $view->displayHandlers->get('default')->getOption('arguments');
$arguments['field_date_value_year']['date'] = 'Date';
$view->displayHandlers->get('default')->overrideOption('arguments', $arguments);
$view->save();
$this->assertConfigSchemaByName('views.view.test_argument_datetime');
// Test filter schema.
$view = Views::getView('test_filter_datetime');
$view->initHandlers();
$filters = $view->displayHandlers->get('default')->getOption('filters');
$filters['field_date_value']['type'] = 'Date';
$view->save();
$this->assertConfigSchemaByName('views.view.test_filter_datetime');
// Test sort schema.
$view = Views::getView('test_sort_datetime');
$view->initHandlers();
$sorts = $view->displayHandlers->get('default')->getOption('sorts');
$this->assertNotEmpty($sorts['field_date_value']['granularity']);
$this->assertConfigSchemaByName('views.view.test_sort_datetime');
}
}

View file

@ -0,0 +1,197 @@
<?php
namespace Drupal\Tests\datetime\Kernel\Views;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\node\Entity\Node;
use Drupal\views\Views;
/**
* Tests date-only fields.
*
* @group datetime
*/
class FilterDateTest extends DateTimeHandlerTestBase {
/**
* {@inheritdoc}
*/
public static $testViews = ['test_filter_datetime'];
/**
* An array of timezone extremes to test.
*
* @var string[]
*/
protected static $timezones = [
// UTC-12, no DST.
'Pacific/Kwajalein',
// UTC-11, no DST.
'Pacific/Midway',
// UTC-7, no DST.
'America/Phoenix',
// UTC.
'UTC',
// UTC+5:30, no DST.
'Asia/Kolkata',
// UTC+12, no DST.
'Pacific/Funafuti',
// UTC+13, no DST.
'Pacific/Tongatapu',
];
/**
* {@inheritdoc}
*
* Create nodes with relative dates of yesterday, today, and tomorrow.
*/
protected function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);
// Change field storage to date-only.
$storage = FieldStorageConfig::load('node.' . static::$field_name);
$storage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
$storage->save();
// Retrieve tomorrow, today and yesterday dates just to create the nodes.
$timestamp = $this->getUTCEquivalentOfUserNowAsTimestamp();
$dates = $this->getRelativeDateValuesFromTimestamp($timestamp);
// Clean the nodes on setUp.
$this->nodes = [];
foreach ($dates as $date) {
$node = Node::create([
'title' => $this->randomMachineName(8),
'type' => 'page',
'field_date' => [
'value' => $date,
],
]);
$node->save();
$this->nodes[] = $node;
}
}
/**
* Test offsets with date-only fields.
*/
public function testDateOffsets() {
$view = Views::getView('test_filter_datetime');
$field = static::$field_name . '_value';
foreach (static::$timezones as $timezone) {
$this->setSiteTimezone($timezone);
$timestamp = $this->getUTCEquivalentOfUserNowAsTimestamp();
$dates = $this->getRelativeDateValuesFromTimestamp($timestamp);
$this->updateNodesDateFieldsValues($dates);
// Test simple operations.
$view->initHandlers();
// A greater than or equal to 'now', should return the 'today' and the
// 'tomorrow' node.
$view->filter[$field]->operator = '>=';
$view->filter[$field]->value['type'] = 'offset';
$view->filter[$field]->value['value'] = 'now';
$view->setDisplay('default');
$this->executeView($view);
$expected_result = [
['nid' => $this->nodes[0]->id()],
['nid' => $this->nodes[1]->id()],
];
$this->assertIdenticalResultset($view, $expected_result, $this->map);
$view->destroy();
// Only dates in the past.
$view->initHandlers();
$view->filter[$field]->operator = '<';
$view->filter[$field]->value['type'] = 'offset';
$view->filter[$field]->value['value'] = 'now';
$view->setDisplay('default');
$this->executeView($view);
$expected_result = [
['nid' => $this->nodes[2]->id()],
];
$this->assertIdenticalResultset($view, $expected_result, $this->map);
$view->destroy();
// Test offset for between operator. Only 'tomorrow' node should appear.
$view->initHandlers();
$view->filter[$field]->operator = 'between';
$view->filter[$field]->value['type'] = 'offset';
$view->filter[$field]->value['max'] = '+2 days';
$view->filter[$field]->value['min'] = '+1 day';
$view->setDisplay('default');
$this->executeView($view);
$expected_result = [
['nid' => $this->nodes[0]->id()],
];
$this->assertIdenticalResultset($view, $expected_result, $this->map);
$view->destroy();
}
}
/**
* Test date filter with date-only fields.
*/
public function testDateIs() {
$view = Views::getView('test_filter_datetime');
$field = static::$field_name . '_value';
foreach (static::$timezones as $timezone) {
$this->setSiteTimezone($timezone);
$timestamp = $this->getUTCEquivalentOfUserNowAsTimestamp();
$dates = $this->getRelativeDateValuesFromTimestamp($timestamp);
$this->updateNodesDateFieldsValues($dates);
// Test simple operations.
$view->initHandlers();
// Filtering with nodes date-only values (format: Y-m-d) to test UTC
// conversion does NOT change the day.
$view->filter[$field]->operator = '=';
$view->filter[$field]->value['type'] = 'date';
$view->filter[$field]->value['value'] = $this->nodes[2]->field_date->first()->getValue()['value'];
$view->setDisplay('default');
$this->executeView($view);
$expected_result = [
['nid' => $this->nodes[2]->id()],
];
$this->assertIdenticalResultset($view, $expected_result, $this->map);
$view->destroy();
// Test offset for between operator. Only 'today' and 'tomorrow' nodes
// should appear.
$view->initHandlers();
$view->filter[$field]->operator = 'between';
$view->filter[$field]->value['type'] = 'date';
$view->filter[$field]->value['max'] = $this->nodes[0]->field_date->first()->getValue()['value'];
$view->filter[$field]->value['min'] = $this->nodes[1]->field_date->first()->getValue()['value'];
$view->setDisplay('default');
$this->executeView($view);
$expected_result = [
['nid' => $this->nodes[0]->id()],
['nid' => $this->nodes[1]->id()],
];
$this->assertIdenticalResultset($view, $expected_result, $this->map);
$view->destroy();
}
}
/**
* Updates tests nodes date fields values.
*
* @param array $dates
* An array of DATETIME_DATE_STORAGE_FORMAT date values.
*/
protected function updateNodesDateFieldsValues(array $dates) {
foreach ($dates as $index => $date) {
$this->nodes[$index]->{static::$field_name}->value = $date;
$this->nodes[$index]->save();
}
}
}

View file

@ -0,0 +1,201 @@
<?php
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.
*/
protected static $date;
/**
* Use a non-UTC timezone.
*
* @var string
*/
protected static $timezone = 'America/Vancouver';
/**
* {@inheritdoc}
*/
protected function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);
static::$date = REQUEST_TIME + 86400;
// Set the timezone.
date_default_timezone_set(static::$timezone);
$this->config('system.date')
->set('timezone.default', static::$timezone)
->save();
// Add some basic test nodes.
$dates = [
'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;
}
}
/**
* Test filter operations.
*/
public function testDatetimeFilter() {
$this->_testOffset();
$this->_testBetween();
$this->_testExact();
}
/**
* Test offset operations.
*/
protected function _testOffset() {
$view = Views::getView('test_filter_datetime');
$field = static::$field_name . '_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);
}
/**
* Test between operations.
*/
protected function _testBetween() {
$view = Views::getView('test_filter_datetime');
$field = static::$field_name . '_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);
}
/**
* Test exact date matching.
*/
protected function _testExact() {
$view = Views::getView('test_filter_datetime');
$field = static::$field_name . '_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,130 @@
<?php
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) {
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() {
$field = static::$field_name . '_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::$field_name . '_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,18 @@
<?php
namespace Drupal\Tests\datetime\Unit\Plugin\migrate\field;
/**
* @group migrate
* @group legacy
*/
class DateFieldLegacyTest extends DateFieldTest {
/**
* @expectedDeprecation Deprecated in Drupal 8.6.0, to be removed before Drupal 9.0.0. Use defineValueProcessPipeline() instead. See https://www.drupal.org/node/2944598.
*/
public function testUnknownDateType($method = 'processFieldValues') {
parent::testUnknownDateType($method);
}
}

View file

@ -0,0 +1,25 @@
<?php
namespace Drupal\Tests\datetime\Unit\Plugin\migrate\field;
use Drupal\datetime\Plugin\migrate\field\DateField;
use Drupal\migrate\MigrateException;
use Drupal\Tests\UnitTestCase;
/**
* @group migrate
*/
class DateFieldTest extends UnitTestCase {
/**
* Tests an Exception is thrown when the field type is not a known date type.
*/
public function testUnknownDateType($method = 'defineValueProcessPipeline') {
$migration = $this->prophesize('Drupal\migrate\Plugin\MigrationInterface')->reveal();
$plugin = new DateField([], '', []);
$this->setExpectedException(MigrateException::class, "Field field_date of type 'timestamp' is an unknown date field type.");
$plugin->$method($migration, 'field_date', ['type' => 'timestamp']);
}
}

View file

@ -0,0 +1,18 @@
<?php
namespace Drupal\Tests\datetime\Unit\Plugin\migrate\field\d6;
/**
* @group migrate
* @group legacy
*/
class DateFieldLegacyTest extends DateFieldTest {
/**
* @expectedDeprecation Deprecated in Drupal 8.6.0, to be removed before Drupal 9.0.0. Use defineValueProcessPipeline() instead. See https://www.drupal.org/node/2944598.
*/
public function testUnknownDateType($method = 'processFieldValues') {
parent::testUnknownDateType($method);
}
}

View file

@ -4,32 +4,39 @@ namespace Drupal\Tests\datetime\Unit\Plugin\migrate\field\d6;
use Drupal\datetime\Plugin\migrate\field\d6\DateField;
use Drupal\migrate\MigrateException;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\Tests\UnitTestCase;
/**
* @group migrate
* @group legacy
*/
class DateFieldTest extends UnitTestCase {
/**
* @var \Drupal\migrate_drupal\Plugin\MigrateFieldInterface
*/
protected $plugin;
/**
* @var \Drupal\migrate\Plugin\MigrationInterface
*/
protected $migration;
/**
* Tests an Exception is thrown when the field type is not a known date type.
* {@inheritdoc}
*/
public function testUnknownDateType() {
$this->migration = $this->prophesize('Drupal\migrate\Plugin\MigrationInterface')->reveal();
$this->plugin = new DateField([], '', []);
protected function setUp() {
parent::setUp();
$this->migration = $this->prophesize(MigrationInterface::class)->reveal();
}
/**
* Tests an Exception is thrown when the field type is not a known date type.
*
* @runInSeparateProcess
* @expectedDeprecation DateField is deprecated in Drupal 8.4.x and will be removed before Drupal 9.0.x. Use \Drupal\datetime\Plugin\migrate\field\DateField instead.
*/
public function testUnknownDateType($method = 'defineValueProcessPipeline') {
$plugin = new DateField([], '', []);
$this->setExpectedException(MigrateException::class, "Field field_date of type 'timestamp' is an unknown date field type.");
$this->plugin->processFieldValues($this->migration, 'field_date', ['type' => 'timestamp']);
$plugin->$method($this->migration, 'field_date', ['type' => 'timestamp']);
}
}