Issue #2780063 by mpdonadio, Jo Fitzgerald, GoZ, boaloysius, klausi, Lendude: Convert web tests to browser tests for datetime and datetime_range modules

8.4.x
Alex Pott 2017-04-03 15:25:27 +01:00
parent c2ad4bcddc
commit 5e8c04df41
4 changed files with 311 additions and 110 deletions

View File

@ -2,6 +2,8 @@
namespace Drupal\datetime\Tests; namespace Drupal\datetime\Tests;
@trigger_error('\Drupal\datetime\Tests\DateTestBase is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Use \Drupal\Tests\BrowserTestBase instead. See https://www.drupal.org/node/2780063.', E_USER_DEPRECATED);
use Drupal\Component\Utility\Unicode; use Drupal\Component\Utility\Unicode;
use Drupal\Core\Entity\Entity\EntityFormDisplay; use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay; use Drupal\Core\Entity\Entity\EntityViewDisplay;
@ -13,6 +15,9 @@ use Drupal\simpletest\WebTestBase;
/** /**
* Provides a base class for testing Datetime field functionality. * Provides a base class for testing Datetime field functionality.
*
* @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0.
* Use \Drupal\Tests\BrowserTestBase instead.
*/ */
abstract class DateTestBase extends WebTestBase { abstract class DateTestBase extends WebTestBase {

View File

@ -0,0 +1,184 @@
<?php
namespace Drupal\Tests\datetime\Functional;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\BrowserTestBase;
/**
* Provides a base class for testing Datetime field functionality.
*/
abstract class DateTestBase extends BrowserTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['node', 'entity_test', 'datetime', 'field_ui'];
/**
* 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;
/**
* 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
*/
abstract protected function getTestFieldType();
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$web_user = $this->drupalCreateUser([
'access content',
'view test entity',
'administer entity_test content',
'administer entity_test form display',
'administer content types',
'administer node fields',
]);
$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 = Unicode::strtolower($this->randomMachineName());
$type = $this->getTestFieldType();
$widget_type = $formatter_type = $type . '_default';
$this->fieldStorage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test',
'type' => $type,
'settings' => ['datetime_type' => DateTimeItem::DATETIME_TYPE_DATE],
]);
$this->fieldStorage->save();
$this->field = FieldConfig::create([
'field_storage' => $this->fieldStorage,
'bundle' => 'entity_test',
'description' => 'Description for ' . $field_name,
'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'] + $this->defaultSettings,
];
EntityViewDisplay::create([
'targetEntityType' => $this->field->getTargetEntityTypeId(),
'bundle' => $this->field->getTargetBundle(),
'mode' => 'full',
'status' => TRUE,
])->setComponent($field_name, $this->displayOptions)
->save();
}
/**
* Renders a entity_test and sets the output in the internal browser.
*
* @param 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();
}
}

View File

@ -1,6 +1,6 @@
<?php <?php
namespace Drupal\datetime\Tests; namespace Drupal\Tests\datetime\Functional;
use Drupal\Component\Render\FormattableMarkup; use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\SafeMarkup; use Drupal\Component\Utility\SafeMarkup;
@ -67,7 +67,7 @@ class DateTimeFieldTest extends DateTestBase {
"{$field_name}[0][value][date]" => $date->format($date_format), "{$field_name}[0][value][date]" => $date->format($date_format),
]; ];
$this->drupalPostForm(NULL, $edit, t('Save')); $this->drupalPostForm(NULL, $edit, t('Save'));
preg_match('|entity_test/manage/(\d+)|', $this->url, $match); preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
$id = $match[1]; $id = $match[1];
$this->assertText(t('entity_test @id has been created.', ['@id' => $id])); $this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
$this->assertRaw($date->format($date_format)); $this->assertRaw($date->format($date_format));
@ -115,8 +115,9 @@ class DateTimeFieldTest extends DateTestBase {
// field, it is expected to display the time as 00:00:00. // field, it is expected to display the time as 00:00:00.
$expected = format_date($date->getTimestamp(), $new_value, '', DATETIME_STORAGE_TIMEZONE); $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_iso = format_date($date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', DATETIME_STORAGE_TIMEZONE);
$this->renderTestEntity($id); $output = $this->renderTestEntity($id);
$this->assertFieldByXPath('//time[@datetime="' . $expected_iso . '"]', $expected, SafeMarkup::format('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', ['%value' => $new_value, '%expected' => $expected, '%expected_iso' => $expected_iso])); $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]));
break; break;
} }
} }
@ -129,8 +130,8 @@ class DateTimeFieldTest extends DateTestBase {
->setComponent($field_name, $this->displayOptions) ->setComponent($field_name, $this->displayOptions)
->save(); ->save();
$expected = $date->format(DATETIME_DATE_STORAGE_FORMAT); $expected = $date->format(DATETIME_DATE_STORAGE_FORMAT);
$this->renderTestEntity($id); $output = $this->renderTestEntity($id);
$this->assertText($expected, SafeMarkup::format('Formatted date field using plain format displayed as %expected.', ['%expected' => $expected])); $this->assertContains($expected, $output, SafeMarkup::format('Formatted date field using plain format displayed as %expected.', ['%expected' => $expected]));
// Verify that the 'datetime_custom' formatter works. // Verify that the 'datetime_custom' formatter works.
$this->displayOptions['type'] = 'datetime_custom'; $this->displayOptions['type'] = 'datetime_custom';
@ -139,8 +140,8 @@ class DateTimeFieldTest extends DateTestBase {
->setComponent($field_name, $this->displayOptions) ->setComponent($field_name, $this->displayOptions)
->save(); ->save();
$expected = $date->format($this->displayOptions['settings']['date_format']); $expected = $date->format($this->displayOptions['settings']['date_format']);
$this->renderTestEntity($id); $output = $this->renderTestEntity($id);
$this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_custom format displayed as %expected.', ['%expected' => $expected])); $this->assertContains($expected, $output, SafeMarkup::format('Formatted date field using datetime_custom format displayed as %expected.', ['%expected' => $expected]));
// Test that allowed markup in custom format is preserved and XSS is // Test that allowed markup in custom format is preserved and XSS is
// removed. // removed.
@ -149,8 +150,8 @@ class DateTimeFieldTest extends DateTestBase {
->setComponent($field_name, $this->displayOptions) ->setComponent($field_name, $this->displayOptions)
->save(); ->save();
$expected = '<strong>' . $date->format('m/d/Y') . '</strong>alert(String.fromCharCode(88,83,83))'; $expected = '<strong>' . $date->format('m/d/Y') . '</strong>alert(String.fromCharCode(88,83,83))';
$this->renderTestEntity($id); $output = $this->renderTestEntity($id);
$this->assertRaw($expected, 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.', ['%expected' => $expected]));
// Verify that the 'datetime_time_ago' formatter works for intervals in the // Verify that the 'datetime_time_ago' formatter works for intervals in the
// past. First update the test entity so that the date difference always // past. First update the test entity so that the date difference always
@ -176,8 +177,8 @@ class DateTimeFieldTest extends DateTestBase {
$expected = SafeMarkup::format($this->displayOptions['settings']['past_format'], [ $expected = SafeMarkup::format($this->displayOptions['settings']['past_format'], [
'@interval' => $this->dateFormatter->formatTimeDiffSince($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]) '@interval' => $this->dateFormatter->formatTimeDiffSince($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']])
]); ]);
$this->renderTestEntity($id); $output = $this->renderTestEntity($id);
$this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', ['%expected' => $expected])); $this->assertContains((string) $expected, $output, SafeMarkup::format('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 // Verify that the 'datetime_time_ago' formatter works for intervals in the
// future. First update the test entity so that the date difference always // future. First update the test entity so that the date difference always
@ -197,8 +198,8 @@ class DateTimeFieldTest extends DateTestBase {
$expected = SafeMarkup::format($this->displayOptions['settings']['future_format'], [ $expected = SafeMarkup::format($this->displayOptions['settings']['future_format'], [
'@interval' => $this->dateFormatter->formatTimeDiffUntil($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]) '@interval' => $this->dateFormatter->formatTimeDiffUntil($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']])
]); ]);
$this->renderTestEntity($id); $output = $this->renderTestEntity($id);
$this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', ['%expected' => $expected])); $this->assertContains((string) $expected, $output, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', ['%expected' => $expected]));
} }
} }
@ -215,7 +216,7 @@ class DateTimeFieldTest extends DateTestBase {
$this->drupalGet('entity_test/add'); $this->drupalGet('entity_test/add');
$this->assertFieldByName("{$field_name}[0][value][date]", '', 'Date element found.'); $this->assertFieldByName("{$field_name}[0][value][date]", '', 'Date element found.');
$this->assertFieldByName("{$field_name}[0][value][time]", '', 'Time element found.'); $this->assertFieldByName("{$field_name}[0][value][time]", '', 'Time element found.');
$this->assertFieldByXPath('//fieldset[@id="edit-' . $field_name . '-0"]/legend//text()', $field_name, 'Fieldset and label found'); $this->assertFieldByXPath('//fieldset[@id="edit-' . $field_name . '-0"]/legend', $field_name, 'Fieldset and label found');
$this->assertFieldByXPath('//fieldset[@aria-describedby="edit-' . $field_name . '-0--description"]', NULL, 'ARIA described-by 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'); $this->assertFieldByXPath('//div[@id="edit-' . $field_name . '-0--description"]', NULL, 'ARIA description found');
@ -235,7 +236,7 @@ class DateTimeFieldTest extends DateTestBase {
"{$field_name}[0][value][time]" => $date->format($time_format), "{$field_name}[0][value][time]" => $date->format($time_format),
]; ];
$this->drupalPostForm(NULL, $edit, t('Save')); $this->drupalPostForm(NULL, $edit, t('Save'));
preg_match('|entity_test/manage/(\d+)|', $this->url, $match); preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
$id = $match[1]; $id = $match[1];
$this->assertText(t('entity_test @id has been created.', ['@id' => $id])); $this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
$this->assertRaw($date->format($date_format)); $this->assertRaw($date->format($date_format));
@ -259,8 +260,9 @@ class DateTimeFieldTest extends DateTestBase {
// Verify that a date is displayed. // Verify that a date is displayed.
$expected = format_date($date->getTimestamp(), $new_value); $expected = format_date($date->getTimestamp(), $new_value);
$expected_iso = format_date($date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', 'UTC'); $expected_iso = format_date($date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', 'UTC');
$this->renderTestEntity($id); $output = $this->renderTestEntity($id);
$this->assertFieldByXPath('//time[@datetime="' . $expected_iso . '"]', $expected, SafeMarkup::format('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', ['%value' => $new_value, '%expected' => $expected, '%expected_iso' => $expected_iso])); $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]));
break; break;
} }
} }
@ -273,8 +275,8 @@ class DateTimeFieldTest extends DateTestBase {
->setComponent($field_name, $this->displayOptions) ->setComponent($field_name, $this->displayOptions)
->save(); ->save();
$expected = $date->format(DATETIME_DATETIME_STORAGE_FORMAT); $expected = $date->format(DATETIME_DATETIME_STORAGE_FORMAT);
$this->renderTestEntity($id); $output = $this->renderTestEntity($id);
$this->assertText($expected, SafeMarkup::format('Formatted date field using plain format displayed as %expected.', ['%expected' => $expected])); $this->assertContains($expected, $output, SafeMarkup::format('Formatted date field using plain format displayed as %expected.', ['%expected' => $expected]));
// Verify that the 'datetime_custom' formatter works. // Verify that the 'datetime_custom' formatter works.
$this->displayOptions['type'] = 'datetime_custom'; $this->displayOptions['type'] = 'datetime_custom';
@ -283,8 +285,8 @@ class DateTimeFieldTest extends DateTestBase {
->setComponent($field_name, $this->displayOptions) ->setComponent($field_name, $this->displayOptions)
->save(); ->save();
$expected = $date->format($this->displayOptions['settings']['date_format']); $expected = $date->format($this->displayOptions['settings']['date_format']);
$this->renderTestEntity($id); $output = $this->renderTestEntity($id);
$this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_custom format displayed as %expected.', ['%expected' => $expected])); $this->assertContains($expected, $output, SafeMarkup::format('Formatted date field using datetime_custom format displayed as %expected.', ['%expected' => $expected]));
// Verify that the 'timezone_override' setting works. // Verify that the 'timezone_override' setting works.
$this->displayOptions['type'] = 'datetime_custom'; $this->displayOptions['type'] = 'datetime_custom';
@ -293,8 +295,8 @@ class DateTimeFieldTest extends DateTestBase {
->setComponent($field_name, $this->displayOptions) ->setComponent($field_name, $this->displayOptions)
->save(); ->save();
$expected = $date->format($this->displayOptions['settings']['date_format'], ['timezone' => 'America/New_York']); $expected = $date->format($this->displayOptions['settings']['date_format'], ['timezone' => 'America/New_York']);
$this->renderTestEntity($id); $output = $this->renderTestEntity($id);
$this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_custom format displayed as %expected.', ['%expected' => $expected])); $this->assertContains($expected, $output, SafeMarkup::format('Formatted date field using datetime_custom format displayed as %expected.', ['%expected' => $expected]));
// Verify that the 'datetime_time_ago' formatter works for intervals in the // Verify that the 'datetime_time_ago' formatter works for intervals in the
// past. First update the test entity so that the date difference always // past. First update the test entity so that the date difference always
@ -320,8 +322,8 @@ class DateTimeFieldTest extends DateTestBase {
$expected = SafeMarkup::format($this->displayOptions['settings']['past_format'], [ $expected = SafeMarkup::format($this->displayOptions['settings']['past_format'], [
'@interval' => $this->dateFormatter->formatTimeDiffSince($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]) '@interval' => $this->dateFormatter->formatTimeDiffSince($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']])
]); ]);
$this->renderTestEntity($id); $output = $this->renderTestEntity($id);
$this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', ['%expected' => $expected])); $this->assertContains((string) $expected, $output, SafeMarkup::format('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 // Verify that the 'datetime_time_ago' formatter works for intervals in the
// future. First update the test entity so that the date difference always // future. First update the test entity so that the date difference always
@ -341,8 +343,8 @@ class DateTimeFieldTest extends DateTestBase {
$expected = SafeMarkup::format($this->displayOptions['settings']['future_format'], [ $expected = SafeMarkup::format($this->displayOptions['settings']['future_format'], [
'@interval' => $this->dateFormatter->formatTimeDiffUntil($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]) '@interval' => $this->dateFormatter->formatTimeDiffUntil($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']])
]); ]);
$this->renderTestEntity($id); $output = $this->renderTestEntity($id);
$this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', ['%expected' => $expected])); $this->assertContains((string) $expected, $output, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', ['%expected' => $expected]));
} }
/** /**
@ -368,7 +370,7 @@ class DateTimeFieldTest extends DateTestBase {
// Display creation form. // Display creation form.
$this->drupalGet('entity_test/add'); $this->drupalGet('entity_test/add');
$this->assertFieldByXPath('//fieldset[@id="edit-' . $field_name . '-0"]/legend//text()', $field_name, 'Fieldset and label found'); $this->assertFieldByXPath('//fieldset[@id="edit-' . $field_name . '-0"]/legend', $field_name, 'Fieldset and label found');
$this->assertFieldByXPath('//fieldset[@aria-describedby="edit-' . $field_name . '-0--description"]', NULL, 'ARIA described-by 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'); $this->assertFieldByXPath('//div[@id="edit-' . $field_name . '-0--description"]', NULL, 'ARIA description found');
@ -381,7 +383,7 @@ class DateTimeFieldTest extends DateTestBase {
$this->drupalGet($fieldEditUrl); $this->drupalGet($fieldEditUrl);
// Click on the widget settings button to open the widget settings form. // Click on the widget settings button to open the widget settings form.
$this->drupalPostAjaxForm(NULL, [], $field_name . "_settings_edit"); $this->drupalPostForm(NULL, [], $field_name . "_settings_edit");
$xpathIncr = "//select[starts-with(@id, \"edit-fields-$field_name-settings-edit-form-settings-increment\")]"; $xpathIncr = "//select[starts-with(@id, \"edit-fields-$field_name-settings-edit-form-settings-increment\")]";
$this->assertNoFieldByXPath($xpathIncr, NULL, 'Increment element not found for Date Only.'); $this->assertNoFieldByXPath($xpathIncr, NULL, 'Increment element not found for Date Only.');
@ -407,7 +409,7 @@ class DateTimeFieldTest extends DateTestBase {
$this->drupalGet($fieldEditUrl); $this->drupalGet($fieldEditUrl);
// Click on the widget settings button to open the widget settings form. // Click on the widget settings button to open the widget settings form.
$this->drupalPostAjaxForm(NULL, [], $field_name . "_settings_edit"); $this->drupalPostForm(NULL, [], $field_name . "_settings_edit");
$this->assertFieldByXPath($xpathIncr, NULL, 'Increment element found for Date and time.'); $this->assertFieldByXPath($xpathIncr, NULL, 'Increment element found for Date and time.');
// Display creation form. // Display creation form.
@ -444,7 +446,7 @@ class DateTimeFieldTest extends DateTestBase {
} }
$this->drupalPostForm(NULL, $edit, t('Save')); $this->drupalPostForm(NULL, $edit, t('Save'));
preg_match('|entity_test/manage/(\d+)|', $this->url, $match); preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
$id = $match[1]; $id = $match[1];
$this->assertText(t('entity_test @id has been created.', ['@id' => $id])); $this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
@ -485,7 +487,7 @@ class DateTimeFieldTest extends DateTestBase {
} }
$this->drupalPostForm(NULL, $edit, t('Save')); $this->drupalPostForm(NULL, $edit, t('Save'));
preg_match('|entity_test/manage/(\d+)|', $this->url, $match); preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
$id = $match[1]; $id = $match[1];
$this->assertText(t('entity_test @id has been created.', ['@id' => $id])); $this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
@ -539,7 +541,7 @@ class DateTimeFieldTest extends DateTestBase {
$this->drupalPostForm(NULL, $edit, t('Save')); $this->drupalPostForm(NULL, $edit, t('Save'));
$this->assertResponse(200); $this->assertResponse(200);
preg_match('|entity_test/manage/(\d+)|', $this->url, $match); preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
$id = $match[1]; $id = $match[1];
$this->assertText(t('entity_test @id has been created.', ['@id' => $id])); $this->assertText(t('entity_test @id has been created.', ['@id' => $id]));

View File

@ -1,12 +1,12 @@
<?php <?php
namespace Drupal\datetime_range\Tests; namespace Drupal\Tests\datetime_range\Functional;
use Drupal\Component\Render\FormattableMarkup; use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\Unicode; use Drupal\Component\Utility\Unicode;
use Drupal\Core\Datetime\DrupalDateTime; use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Datetime\Entity\DateFormat; use Drupal\Core\Datetime\Entity\DateFormat;
use Drupal\datetime\Tests\DateTestBase; use Drupal\Tests\datetime\Functional\DateTestBase;
use Drupal\datetime_range\Plugin\Field\FieldType\DateRangeItem; use Drupal\datetime_range\Plugin\Field\FieldType\DateRangeItem;
use Drupal\entity_test\Entity\EntityTest; use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldConfig;
@ -64,7 +64,7 @@ class DateRangeFieldTest extends DateTestBase {
$this->assertFieldByXPath('//*[@id="edit-' . $field_name . '-wrapper"]//label[contains(@class, "js-form-required")]', TRUE, 'Required markup found'); $this->assertFieldByXPath('//*[@id="edit-' . $field_name . '-wrapper"]//label[contains(@class, "js-form-required")]', TRUE, 'Required markup found');
$this->assertNoFieldByName("{$field_name}[0][value][time]", '', 'Start time element not found.'); $this->assertNoFieldByName("{$field_name}[0][value][time]", '', 'Start time element not found.');
$this->assertNoFieldByName("{$field_name}[0][end_value][time]", '', 'End time element not found.'); $this->assertNoFieldByName("{$field_name}[0][end_value][time]", '', 'End time element not found.');
$this->assertFieldByXPath('//fieldset[@id="edit-' . $field_name . '-0"]/legend//text()', $field_name, 'Fieldset and label found'); $this->assertFieldByXPath('//fieldset[@id="edit-' . $field_name . '-0"]/legend', $field_name, 'Fieldset and label found');
$this->assertFieldByXPath('//fieldset[@aria-describedby="edit-' . $field_name . '-0--description"]', NULL, 'ARIA described-by 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'); $this->assertFieldByXPath('//div[@id="edit-' . $field_name . '-0--description"]', NULL, 'ARIA description found');
@ -83,7 +83,7 @@ class DateRangeFieldTest extends DateTestBase {
"{$field_name}[0][end_value][date]" => $end_date->format($date_format), "{$field_name}[0][end_value][date]" => $end_date->format($date_format),
]; ];
$this->drupalPostForm(NULL, $edit, t('Save')); $this->drupalPostForm(NULL, $edit, t('Save'));
preg_match('|entity_test/manage/(\d+)|', $this->url, $match); preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
$id = $match[1]; $id = $match[1];
$this->assertText(t('entity_test @id has been created.', ['@id' => $id])); $this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
$this->assertRaw($start_date->format($date_format)); $this->assertRaw($start_date->format($date_format));
@ -128,20 +128,22 @@ class DateRangeFieldTest extends DateTestBase {
$start_expected = $this->dateFormatter->format($start_date->getTimestamp(), 'long', '', DATETIME_STORAGE_TIMEZONE); $start_expected = $this->dateFormatter->format($start_date->getTimestamp(), 'long', '', DATETIME_STORAGE_TIMEZONE);
$start_expected_iso = $this->dateFormatter->format($start_date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', DATETIME_STORAGE_TIMEZONE); $start_expected_iso = $this->dateFormatter->format($start_date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', DATETIME_STORAGE_TIMEZONE);
$start_expected_markup = '<time datetime="' . $start_expected_iso . '" class="datetime">' . $start_expected . '</time>';
$end_expected = $this->dateFormatter->format($end_date->getTimestamp(), 'long', '', DATETIME_STORAGE_TIMEZONE); $end_expected = $this->dateFormatter->format($end_date->getTimestamp(), 'long', '', DATETIME_STORAGE_TIMEZONE);
$end_expected_iso = $this->dateFormatter->format($end_date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', DATETIME_STORAGE_TIMEZONE); $end_expected_iso = $this->dateFormatter->format($end_date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', DATETIME_STORAGE_TIMEZONE);
$this->renderTestEntity($id); $end_expected_markup = '<time datetime="' . $end_expected_iso . '" class="datetime">' . $end_expected . '</time>';
$this->assertFieldByXPath('//time[@datetime="' . $start_expected_iso . '"]', $start_expected, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', [ $output = $this->renderTestEntity($id);
$this->assertContains($start_expected_markup, $output, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', [
'%value' => 'long', '%value' => 'long',
'%expected' => $start_expected, '%expected' => $start_expected,
'%expected_iso' => $start_expected_iso, '%expected_iso' => $start_expected_iso,
])); ]));
$this->assertFieldByXPath('//time[@datetime="' . $end_expected_iso . '"]', $end_expected, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', [ $this->assertContains($end_expected_markup, $output, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', [
'%value' => 'long', '%value' => 'long',
'%expected' => $end_expected, '%expected' => $end_expected,
'%expected_iso' => $end_expected_iso, '%expected_iso' => $end_expected_iso,
])); ]));
$this->assertText(' THESEPARATOR ', 'Found proper separator'); $this->assertContains(' THESEPARATOR ', $output, 'Found proper separator');
// Verify that hook_entity_prepare_view can add attributes. // Verify that hook_entity_prepare_view can add attributes.
// @see entity_test_entity_prepare_view() // @see entity_test_entity_prepare_view()
@ -155,8 +157,8 @@ class DateRangeFieldTest extends DateTestBase {
->setComponent($field_name, $this->displayOptions) ->setComponent($field_name, $this->displayOptions)
->save(); ->save();
$expected = $start_date->format(DATETIME_DATE_STORAGE_FORMAT) . ' - ' . $end_date->format(DATETIME_DATE_STORAGE_FORMAT); $expected = $start_date->format(DATETIME_DATE_STORAGE_FORMAT) . ' - ' . $end_date->format(DATETIME_DATE_STORAGE_FORMAT);
$this->renderTestEntity($id); $output = $this->renderTestEntity($id);
$this->assertText($expected, new FormattableMarkup('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 custom formatter works. // Verify that the custom formatter works.
$this->displayOptions['type'] = 'daterange_custom'; $this->displayOptions['type'] = 'daterange_custom';
@ -165,8 +167,8 @@ class DateRangeFieldTest extends DateTestBase {
->setComponent($field_name, $this->displayOptions) ->setComponent($field_name, $this->displayOptions)
->save(); ->save();
$expected = $start_date->format($this->displayOptions['settings']['date_format']) . ' - ' . $end_date->format($this->displayOptions['settings']['date_format']); $expected = $start_date->format($this->displayOptions['settings']['date_format']) . ' - ' . $end_date->format($this->displayOptions['settings']['date_format']);
$this->renderTestEntity($id); $output = $this->renderTestEntity($id);
$this->assertText($expected, 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.', ['%expected' => $expected]));
// Test that allowed markup in custom format is preserved and XSS is // Test that allowed markup in custom format is preserved and XSS is
// removed. // removed.
@ -175,8 +177,8 @@ class DateRangeFieldTest extends DateTestBase {
->setComponent($field_name, $this->displayOptions) ->setComponent($field_name, $this->displayOptions)
->save(); ->save();
$expected = '<strong>' . $start_date->format('m/d/Y') . '</strong>alert(String.fromCharCode(88,83,83)) - <strong>' . $end_date->format('m/d/Y') . '</strong>alert(String.fromCharCode(88,83,83))'; $expected = '<strong>' . $start_date->format('m/d/Y') . '</strong>alert(String.fromCharCode(88,83,83)) - <strong>' . $end_date->format('m/d/Y') . '</strong>alert(String.fromCharCode(88,83,83))';
$this->renderTestEntity($id); $output = $this->renderTestEntity($id);
$this->assertRaw($expected, 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.', ['%expected' => $expected]));
// Test formatters when start date and end date are the same // Test formatters when start date and end date are the same
$this->drupalGet('entity_test/add'); $this->drupalGet('entity_test/add');
@ -192,7 +194,7 @@ class DateRangeFieldTest extends DateTestBase {
]; ];
$this->drupalPostForm(NULL, $edit, t('Save')); $this->drupalPostForm(NULL, $edit, t('Save'));
preg_match('|entity_test/manage/(\d+)|', $this->url, $match); preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
$id = $match[1]; $id = $match[1];
$this->assertText(t('entity_test @id has been created.', ['@id' => $id])); $this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
@ -213,13 +215,14 @@ class DateRangeFieldTest extends DateTestBase {
$start_expected = $this->dateFormatter->format($start_date->getTimestamp(), 'long', '', DATETIME_STORAGE_TIMEZONE); $start_expected = $this->dateFormatter->format($start_date->getTimestamp(), 'long', '', DATETIME_STORAGE_TIMEZONE);
$start_expected_iso = $this->dateFormatter->format($start_date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', DATETIME_STORAGE_TIMEZONE); $start_expected_iso = $this->dateFormatter->format($start_date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', DATETIME_STORAGE_TIMEZONE);
$this->renderTestEntity($id); $start_expected_markup = '<time datetime="' . $start_expected_iso . '" class="datetime">' . $start_expected . '</time>';
$this->assertFieldByXPath('//time[@datetime="' . $start_expected_iso . '"]', $start_expected, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', [ $output = $this->renderTestEntity($id);
$this->assertContains($start_expected_markup, $output, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', [
'%value' => 'long', '%value' => 'long',
'%expected' => $start_expected, '%expected' => $start_expected,
'%expected_iso' => $start_expected_iso, '%expected_iso' => $start_expected_iso,
])); ]));
$this->assertNoText(' THESEPARATOR ', 'Separator not found on page'); $this->assertNotContains(' THESEPARATOR ', $output, 'Separator not found on page');
// Verify that hook_entity_prepare_view can add attributes. // Verify that hook_entity_prepare_view can add attributes.
// @see entity_test_entity_prepare_view() // @see entity_test_entity_prepare_view()
@ -232,9 +235,9 @@ class DateRangeFieldTest extends DateTestBase {
->setComponent($field_name, $this->displayOptions) ->setComponent($field_name, $this->displayOptions)
->save(); ->save();
$expected = $start_date->format(DATETIME_DATE_STORAGE_FORMAT); $expected = $start_date->format(DATETIME_DATE_STORAGE_FORMAT);
$this->renderTestEntity($id); $output = $this->renderTestEntity($id);
$this->assertText($expected, new FormattableMarkup('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]));
$this->assertNoText(' THESEPARATOR ', 'Separator not found on page'); $this->assertNotContains(' THESEPARATOR ', $output, 'Separator not found on page');
$this->displayOptions['type'] = 'daterange_custom'; $this->displayOptions['type'] = 'daterange_custom';
$this->displayOptions['settings'] = ['date_format' => 'm/d/Y'] + $this->defaultSettings; $this->displayOptions['settings'] = ['date_format' => 'm/d/Y'] + $this->defaultSettings;
@ -242,9 +245,9 @@ class DateRangeFieldTest extends DateTestBase {
->setComponent($field_name, $this->displayOptions) ->setComponent($field_name, $this->displayOptions)
->save(); ->save();
$expected = $start_date->format($this->displayOptions['settings']['date_format']); $expected = $start_date->format($this->displayOptions['settings']['date_format']);
$this->renderTestEntity($id); $output = $this->renderTestEntity($id);
$this->assertText($expected, 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.', ['%expected' => $expected]));
$this->assertNoText(' THESEPARATOR ', 'Separator not found on page'); $this->assertNotContains(' THESEPARATOR ', $output, 'Separator not found on page');
} }
} }
@ -264,7 +267,7 @@ class DateRangeFieldTest extends DateTestBase {
$this->assertFieldByName("{$field_name}[0][value][time]", '', 'Start time element found.'); $this->assertFieldByName("{$field_name}[0][value][time]", '', 'Start time element found.');
$this->assertFieldByName("{$field_name}[0][end_value][date]", '', 'End date element found.'); $this->assertFieldByName("{$field_name}[0][end_value][date]", '', 'End date element found.');
$this->assertFieldByName("{$field_name}[0][end_value][time]", '', 'End time element found.'); $this->assertFieldByName("{$field_name}[0][end_value][time]", '', 'End time element found.');
$this->assertFieldByXPath('//fieldset[@id="edit-' . $field_name . '-0"]/legend//text()', $field_name, 'Fieldset and label found'); $this->assertFieldByXPath('//fieldset[@id="edit-' . $field_name . '-0"]/legend', $field_name, 'Fieldset and label found');
$this->assertFieldByXPath('//fieldset[@aria-describedby="edit-' . $field_name . '-0--description"]', NULL, 'ARIA described-by 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'); $this->assertFieldByXPath('//div[@id="edit-' . $field_name . '-0--description"]', NULL, 'ARIA description found');
@ -289,7 +292,7 @@ class DateRangeFieldTest extends DateTestBase {
"{$field_name}[0][end_value][time]" => $end_date->format($time_format), "{$field_name}[0][end_value][time]" => $end_date->format($time_format),
]; ];
$this->drupalPostForm(NULL, $edit, t('Save')); $this->drupalPostForm(NULL, $edit, t('Save'));
preg_match('|entity_test/manage/(\d+)|', $this->url, $match); preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
$id = $match[1]; $id = $match[1];
$this->assertText(t('entity_test @id has been created.', ['@id' => $id])); $this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
$this->assertRaw($start_date->format($date_format)); $this->assertRaw($start_date->format($date_format));
@ -308,12 +311,14 @@ class DateRangeFieldTest extends DateTestBase {
$start_expected = $this->dateFormatter->format($start_date->getTimestamp(), 'long'); $start_expected = $this->dateFormatter->format($start_date->getTimestamp(), 'long');
$start_expected_iso = $this->dateFormatter->format($start_date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', 'UTC'); $start_expected_iso = $this->dateFormatter->format($start_date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', 'UTC');
$start_expected_markup = '<time datetime="' . $start_expected_iso . '" class="datetime">' . $start_expected . '</time>';
$end_expected = $this->dateFormatter->format($end_date->getTimestamp(), 'long'); $end_expected = $this->dateFormatter->format($end_date->getTimestamp(), 'long');
$end_expected_iso = $this->dateFormatter->format($end_date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', 'UTC'); $end_expected_iso = $this->dateFormatter->format($end_date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', 'UTC');
$this->renderTestEntity($id); $end_expected_markup = '<time datetime="' . $end_expected_iso . '" class="datetime">' . $end_expected . '</time>';
$this->assertFieldByXPath('//time[@datetime="' . $start_expected_iso . '"]', $start_expected, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', ['%value' => 'long', '%expected' => $start_expected, '%expected_iso' => $start_expected_iso])); $output = $this->renderTestEntity($id);
$this->assertFieldByXPath('//time[@datetime="' . $end_expected_iso . '"]', $end_expected, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', ['%value' => 'long', '%expected' => $end_expected, '%expected_iso' => $end_expected_iso])); $this->assertContains($start_expected_markup, $output, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', ['%value' => 'long', '%expected' => $start_expected, '%expected_iso' => $start_expected_iso]));
$this->assertText(' THESEPARATOR ', 'Found proper separator'); $this->assertContains($end_expected_markup, $output, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', ['%value' => 'long', '%expected' => $end_expected, '%expected_iso' => $end_expected_iso]));
$this->assertContains(' THESEPARATOR ', $output, 'Found proper separator');
// Verify that hook_entity_prepare_view can add attributes. // Verify that hook_entity_prepare_view can add attributes.
// @see entity_test_entity_prepare_view() // @see entity_test_entity_prepare_view()
@ -327,8 +332,8 @@ class DateRangeFieldTest extends DateTestBase {
->setComponent($field_name, $this->displayOptions) ->setComponent($field_name, $this->displayOptions)
->save(); ->save();
$expected = $start_date->format(DATETIME_DATETIME_STORAGE_FORMAT) . ' - ' . $end_date->format(DATETIME_DATETIME_STORAGE_FORMAT); $expected = $start_date->format(DATETIME_DATETIME_STORAGE_FORMAT) . ' - ' . $end_date->format(DATETIME_DATETIME_STORAGE_FORMAT);
$this->renderTestEntity($id); $output = $this->renderTestEntity($id);
$this->assertText($expected, new FormattableMarkup('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. // Verify that the 'datetime_custom' formatter works.
$this->displayOptions['type'] = 'daterange_custom'; $this->displayOptions['type'] = 'daterange_custom';
@ -337,8 +342,8 @@ class DateRangeFieldTest extends DateTestBase {
->setComponent($field_name, $this->displayOptions) ->setComponent($field_name, $this->displayOptions)
->save(); ->save();
$expected = $start_date->format($this->displayOptions['settings']['date_format']) . ' - ' . $end_date->format($this->displayOptions['settings']['date_format']); $expected = $start_date->format($this->displayOptions['settings']['date_format']) . ' - ' . $end_date->format($this->displayOptions['settings']['date_format']);
$this->renderTestEntity($id); $output = $this->renderTestEntity($id);
$this->assertText($expected, 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.', ['%expected' => $expected]));
// Verify that the 'timezone_override' setting works. // Verify that the 'timezone_override' setting works.
$this->displayOptions['type'] = 'daterange_custom'; $this->displayOptions['type'] = 'daterange_custom';
@ -348,8 +353,8 @@ class DateRangeFieldTest extends DateTestBase {
->save(); ->save();
$expected = $start_date->format($this->displayOptions['settings']['date_format'], ['timezone' => 'America/New_York']); $expected = $start_date->format($this->displayOptions['settings']['date_format'], ['timezone' => 'America/New_York']);
$expected .= ' - ' . $end_date->format($this->displayOptions['settings']['date_format'], ['timezone' => 'America/New_York']); $expected .= ' - ' . $end_date->format($this->displayOptions['settings']['date_format'], ['timezone' => 'America/New_York']);
$this->renderTestEntity($id); $output = $this->renderTestEntity($id);
$this->assertText($expected, 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.', ['%expected' => $expected]));
// Test formatters when start date and end date are the same // Test formatters when start date and end date are the same
$this->drupalGet('entity_test/add'); $this->drupalGet('entity_test/add');
@ -368,7 +373,7 @@ class DateRangeFieldTest extends DateTestBase {
]; ];
$this->drupalPostForm(NULL, $edit, t('Save')); $this->drupalPostForm(NULL, $edit, t('Save'));
preg_match('|entity_test/manage/(\d+)|', $this->url, $match); preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
$id = $match[1]; $id = $match[1];
$this->assertText(t('entity_test @id has been created.', ['@id' => $id])); $this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
@ -387,9 +392,10 @@ class DateRangeFieldTest extends DateTestBase {
$start_expected = $this->dateFormatter->format($start_date->getTimestamp(), 'long'); $start_expected = $this->dateFormatter->format($start_date->getTimestamp(), 'long');
$start_expected_iso = $this->dateFormatter->format($start_date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', 'UTC'); $start_expected_iso = $this->dateFormatter->format($start_date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', 'UTC');
$this->renderTestEntity($id); $start_expected_markup = '<time datetime="' . $start_expected_iso . '" class="datetime">' . $start_expected . '</time>';
$this->assertFieldByXPath('//time[@datetime="' . $start_expected_iso . '"]', $start_expected, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', ['%value' => 'long', '%expected' => $start_expected, '%expected_iso' => $start_expected_iso])); $output = $this->renderTestEntity($id);
$this->assertNoText(' THESEPARATOR ', 'Separator not found on page'); $this->assertContains($start_expected_markup, $output, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', ['%value' => 'long', '%expected' => $start_expected, '%expected_iso' => $start_expected_iso]));
$this->assertNotContains(' THESEPARATOR ', $output, 'Separator not found on page');
// Verify that hook_entity_prepare_view can add attributes. // Verify that hook_entity_prepare_view can add attributes.
// @see entity_test_entity_prepare_view() // @see entity_test_entity_prepare_view()
@ -402,9 +408,9 @@ class DateRangeFieldTest extends DateTestBase {
->setComponent($field_name, $this->displayOptions) ->setComponent($field_name, $this->displayOptions)
->save(); ->save();
$expected = $start_date->format(DATETIME_DATETIME_STORAGE_FORMAT); $expected = $start_date->format(DATETIME_DATETIME_STORAGE_FORMAT);
$this->renderTestEntity($id); $output = $this->renderTestEntity($id);
$this->assertText($expected, new FormattableMarkup('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]));
$this->assertNoText(' THESEPARATOR ', 'Separator not found on page'); $this->assertNotContains(' THESEPARATOR ', $output, 'Separator not found on page');
$this->displayOptions['type'] = 'daterange_custom'; $this->displayOptions['type'] = 'daterange_custom';
$this->displayOptions['settings'] = ['date_format' => 'm/d/Y g:i:s A'] + $this->defaultSettings; $this->displayOptions['settings'] = ['date_format' => 'm/d/Y g:i:s A'] + $this->defaultSettings;
@ -412,9 +418,9 @@ class DateRangeFieldTest extends DateTestBase {
->setComponent($field_name, $this->displayOptions) ->setComponent($field_name, $this->displayOptions)
->save(); ->save();
$expected = $start_date->format($this->displayOptions['settings']['date_format']); $expected = $start_date->format($this->displayOptions['settings']['date_format']);
$this->renderTestEntity($id); $output = $this->renderTestEntity($id);
$this->assertText($expected, 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.', ['%expected' => $expected]));
$this->assertNoText(' THESEPARATOR ', 'Separator not found on page'); $this->assertNotContains(' THESEPARATOR ', $output, 'Separator not found on page');
} }
/** /**
@ -434,7 +440,7 @@ class DateRangeFieldTest extends DateTestBase {
$this->assertFieldByXPath('//*[@id="edit-' . $field_name . '-wrapper"]//label[contains(@class, "js-form-required")]', TRUE, 'Required markup found'); $this->assertFieldByXPath('//*[@id="edit-' . $field_name . '-wrapper"]//label[contains(@class, "js-form-required")]', TRUE, 'Required markup found');
$this->assertNoFieldByName("{$field_name}[0][value][time]", '', 'Start time element not found.'); $this->assertNoFieldByName("{$field_name}[0][value][time]", '', 'Start time element not found.');
$this->assertNoFieldByName("{$field_name}[0][end_value][time]", '', 'End time element not found.'); $this->assertNoFieldByName("{$field_name}[0][end_value][time]", '', 'End time element not found.');
$this->assertFieldByXPath('//fieldset[@id="edit-' . $field_name . '-0"]/legend//text()', $field_name, 'Fieldset and label found'); $this->assertFieldByXPath('//fieldset[@id="edit-' . $field_name . '-0"]/legend', $field_name, 'Fieldset and label found');
$this->assertFieldByXPath('//fieldset[@aria-describedby="edit-' . $field_name . '-0--description"]', NULL, 'ARIA described-by 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'); $this->assertFieldByXPath('//div[@id="edit-' . $field_name . '-0--description"]', NULL, 'ARIA description found');
@ -453,7 +459,7 @@ class DateRangeFieldTest extends DateTestBase {
"{$field_name}[0][end_value][date]" => $end_date->format($date_format), "{$field_name}[0][end_value][date]" => $end_date->format($date_format),
]; ];
$this->drupalPostForm(NULL, $edit, t('Save')); $this->drupalPostForm(NULL, $edit, t('Save'));
preg_match('|entity_test/manage/(\d+)|', $this->url, $match); preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
$id = $match[1]; $id = $match[1];
$this->assertText(t('entity_test @id has been created.', ['@id' => $id])); $this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
$this->assertRaw($start_date->format($date_format)); $this->assertRaw($start_date->format($date_format));
@ -472,12 +478,14 @@ class DateRangeFieldTest extends DateTestBase {
$start_expected = $this->dateFormatter->format($start_date->getTimestamp(), 'long'); $start_expected = $this->dateFormatter->format($start_date->getTimestamp(), 'long');
$start_expected_iso = $this->dateFormatter->format($start_date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', 'UTC'); $start_expected_iso = $this->dateFormatter->format($start_date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', 'UTC');
$start_expected_markup = '<time datetime="' . $start_expected_iso . '" class="datetime">' . $start_expected . '</time>';
$end_expected = $this->dateFormatter->format($end_date->getTimestamp(), 'long'); $end_expected = $this->dateFormatter->format($end_date->getTimestamp(), 'long');
$end_expected_iso = $this->dateFormatter->format($end_date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', 'UTC'); $end_expected_iso = $this->dateFormatter->format($end_date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', 'UTC');
$this->renderTestEntity($id); $end_expected_markup = '<time datetime="' . $end_expected_iso . '" class="datetime">' . $end_expected . '</time>';
$this->assertFieldByXPath('//time[@datetime="' . $start_expected_iso . '"]', $start_expected, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', ['%value' => 'long', '%expected' => $start_expected, '%expected_iso' => $start_expected_iso])); $output = $this->renderTestEntity($id);
$this->assertFieldByXPath('//time[@datetime="' . $end_expected_iso . '"]', $end_expected, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', ['%value' => 'long', '%expected' => $end_expected, '%expected_iso' => $end_expected_iso])); $this->assertContains($start_expected_markup, $output, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', ['%value' => 'long', '%expected' => $start_expected, '%expected_iso' => $start_expected_iso]));
$this->assertText(' THESEPARATOR ', 'Found proper separator'); $this->assertContains($end_expected_markup, $output, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', ['%value' => 'long', '%expected' => $end_expected, '%expected_iso' => $end_expected_iso]));
$this->assertContains(' THESEPARATOR ', $output, 'Found proper separator');
// Verify that hook_entity_prepare_view can add attributes. // Verify that hook_entity_prepare_view can add attributes.
// @see entity_test_entity_prepare_view() // @see entity_test_entity_prepare_view()
@ -491,8 +499,8 @@ class DateRangeFieldTest extends DateTestBase {
->setComponent($field_name, $this->displayOptions) ->setComponent($field_name, $this->displayOptions)
->save(); ->save();
$expected = $start_date->format(DATETIME_DATETIME_STORAGE_FORMAT) . ' - ' . $end_date->format(DATETIME_DATETIME_STORAGE_FORMAT); $expected = $start_date->format(DATETIME_DATETIME_STORAGE_FORMAT) . ' - ' . $end_date->format(DATETIME_DATETIME_STORAGE_FORMAT);
$this->renderTestEntity($id); $output = $this->renderTestEntity($id);
$this->assertText($expected, new FormattableMarkup('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 custom formatter works. // Verify that the custom formatter works.
$this->displayOptions['type'] = 'daterange_custom'; $this->displayOptions['type'] = 'daterange_custom';
@ -501,8 +509,8 @@ class DateRangeFieldTest extends DateTestBase {
->setComponent($field_name, $this->displayOptions) ->setComponent($field_name, $this->displayOptions)
->save(); ->save();
$expected = $start_date->format($this->displayOptions['settings']['date_format']) . ' - ' . $end_date->format($this->displayOptions['settings']['date_format']); $expected = $start_date->format($this->displayOptions['settings']['date_format']) . ' - ' . $end_date->format($this->displayOptions['settings']['date_format']);
$this->renderTestEntity($id); $output = $this->renderTestEntity($id);
$this->assertText($expected, 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.', ['%expected' => $expected]));
// Verify that the 'timezone_override' setting works. // Verify that the 'timezone_override' setting works.
$this->displayOptions['type'] = 'daterange_custom'; $this->displayOptions['type'] = 'daterange_custom';
@ -512,8 +520,8 @@ class DateRangeFieldTest extends DateTestBase {
->save(); ->save();
$expected = $start_date->format($this->displayOptions['settings']['date_format'], ['timezone' => 'America/New_York']); $expected = $start_date->format($this->displayOptions['settings']['date_format'], ['timezone' => 'America/New_York']);
$expected .= ' - ' . $end_date->format($this->displayOptions['settings']['date_format'], ['timezone' => 'America/New_York']); $expected .= ' - ' . $end_date->format($this->displayOptions['settings']['date_format'], ['timezone' => 'America/New_York']);
$this->renderTestEntity($id); $output = $this->renderTestEntity($id);
$this->assertText($expected, 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.', ['%expected' => $expected]));
// Test formatters when start date and end date are the same // Test formatters when start date and end date are the same
$this->drupalGet('entity_test/add'); $this->drupalGet('entity_test/add');
@ -531,7 +539,7 @@ class DateRangeFieldTest extends DateTestBase {
"{$field_name}[0][end_value][date]" => $start_date->format($date_format), "{$field_name}[0][end_value][date]" => $start_date->format($date_format),
]; ];
$this->drupalPostForm(NULL, $edit, t('Save')); $this->drupalPostForm(NULL, $edit, t('Save'));
preg_match('|entity_test/manage/(\d+)|', $this->url, $match); preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
$id = $match[1]; $id = $match[1];
$this->assertText(t('entity_test @id has been created.', ['@id' => $id])); $this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
@ -550,12 +558,14 @@ class DateRangeFieldTest extends DateTestBase {
$start_expected = $this->dateFormatter->format($start_date->getTimestamp(), 'long'); $start_expected = $this->dateFormatter->format($start_date->getTimestamp(), 'long');
$start_expected_iso = $this->dateFormatter->format($start_date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', 'UTC'); $start_expected_iso = $this->dateFormatter->format($start_date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', 'UTC');
$start_expected_markup = '<time datetime="' . $start_expected_iso . '" class="datetime">' . $start_expected . '</time>';
$end_expected = $this->dateFormatter->format($end_date->getTimestamp(), 'long'); $end_expected = $this->dateFormatter->format($end_date->getTimestamp(), 'long');
$end_expected_iso = $this->dateFormatter->format($end_date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', 'UTC'); $end_expected_iso = $this->dateFormatter->format($end_date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', 'UTC');
$this->renderTestEntity($id); $end_expected_markup = '<time datetime="' . $end_expected_iso . '" class="datetime">' . $end_expected . '</time>';
$this->assertFieldByXPath('//time[@datetime="' . $start_expected_iso . '"]', $start_expected, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', ['%value' => 'long', '%expected' => $start_expected, '%expected_iso' => $start_expected_iso])); $output = $this->renderTestEntity($id);
$this->assertFieldByXPath('//time[@datetime="' . $end_expected_iso . '"]', $end_expected, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', ['%value' => 'long', '%expected' => $end_expected, '%expected_iso' => $end_expected_iso])); $this->assertContains($start_expected_markup, $output, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', ['%value' => 'long', '%expected' => $start_expected, '%expected_iso' => $start_expected_iso]));
$this->assertText(' THESEPARATOR ', 'Found proper separator'); $this->assertContains($end_expected_markup, $output, new FormattableMarkup('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', ['%value' => 'long', '%expected' => $end_expected, '%expected_iso' => $end_expected_iso]));
$this->assertContains(' THESEPARATOR ', $output, 'Found proper separator');
// Verify that hook_entity_prepare_view can add attributes. // Verify that hook_entity_prepare_view can add attributes.
// @see entity_test_entity_prepare_view() // @see entity_test_entity_prepare_view()
@ -567,9 +577,9 @@ class DateRangeFieldTest extends DateTestBase {
->setComponent($field_name, $this->displayOptions) ->setComponent($field_name, $this->displayOptions)
->save(); ->save();
$expected = $start_date->format(DATETIME_DATETIME_STORAGE_FORMAT) . ' THESEPARATOR ' . $end_date->format(DATETIME_DATETIME_STORAGE_FORMAT); $expected = $start_date->format(DATETIME_DATETIME_STORAGE_FORMAT) . ' THESEPARATOR ' . $end_date->format(DATETIME_DATETIME_STORAGE_FORMAT);
$this->renderTestEntity($id); $output = $this->renderTestEntity($id);
$this->assertText($expected, new FormattableMarkup('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]));
$this->assertText(' THESEPARATOR ', 'Found proper separator'); $this->assertContains(' THESEPARATOR ', $output, 'Found proper separator');
$this->displayOptions['type'] = 'daterange_custom'; $this->displayOptions['type'] = 'daterange_custom';
$this->displayOptions['settings']['date_format'] = 'm/d/Y'; $this->displayOptions['settings']['date_format'] = 'm/d/Y';
@ -577,9 +587,9 @@ class DateRangeFieldTest extends DateTestBase {
->setComponent($field_name, $this->displayOptions) ->setComponent($field_name, $this->displayOptions)
->save(); ->save();
$expected = $start_date->format($this->displayOptions['settings']['date_format']) . ' THESEPARATOR ' . $end_date->format($this->displayOptions['settings']['date_format']); $expected = $start_date->format($this->displayOptions['settings']['date_format']) . ' THESEPARATOR ' . $end_date->format($this->displayOptions['settings']['date_format']);
$this->renderTestEntity($id); $output = $this->renderTestEntity($id);
$this->assertText($expected, 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.', ['%expected' => $expected]));
$this->assertText(' THESEPARATOR ', 'Found proper separator'); $this->assertContains(' THESEPARATOR ', $output, 'Found proper separator');
} }
@ -606,7 +616,7 @@ class DateRangeFieldTest extends DateTestBase {
// Display creation form. // Display creation form.
$this->drupalGet('entity_test/add'); $this->drupalGet('entity_test/add');
$this->assertFieldByXPath('//fieldset[@id="edit-' . $field_name . '-0"]/legend//text()', $field_name, 'Fieldset and label found'); $this->assertFieldByXPath('//fieldset[@id="edit-' . $field_name . '-0"]/legend', $field_name, 'Fieldset and label found');
$this->assertFieldByXPath('//fieldset[@aria-describedby="edit-' . $field_name . '-0--description"]', NULL, 'ARIA described-by 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'); $this->assertFieldByXPath('//div[@id="edit-' . $field_name . '-0--description"]', NULL, 'ARIA description found');
@ -622,7 +632,7 @@ class DateRangeFieldTest extends DateTestBase {
$this->drupalGet($fieldEditUrl); $this->drupalGet($fieldEditUrl);
// Click on the widget settings button to open the widget settings form. // Click on the widget settings button to open the widget settings form.
$this->drupalPostAjaxForm(NULL, [], $field_name . "_settings_edit"); $this->drupalPostForm(NULL, [], $field_name . "_settings_edit");
$xpathIncr = "//select[starts-with(@id, \"edit-fields-$field_name-settings-edit-form-settings-increment\")]"; $xpathIncr = "//select[starts-with(@id, \"edit-fields-$field_name-settings-edit-form-settings-increment\")]";
$this->assertNoFieldByXPath($xpathIncr, NULL, 'Increment element not found for Date Only.'); $this->assertNoFieldByXPath($xpathIncr, NULL, 'Increment element not found for Date Only.');
@ -656,7 +666,7 @@ class DateRangeFieldTest extends DateTestBase {
$this->drupalGet($fieldEditUrl); $this->drupalGet($fieldEditUrl);
// Click on the widget settings button to open the widget settings form. // Click on the widget settings button to open the widget settings form.
$this->drupalPostAjaxForm(NULL, [], $field_name . "_settings_edit"); $this->drupalPostForm(NULL, [], $field_name . "_settings_edit");
$xpathIncr = "//select[starts-with(@id, \"edit-fields-$field_name-settings-edit-form-settings-increment\")]"; $xpathIncr = "//select[starts-with(@id, \"edit-fields-$field_name-settings-edit-form-settings-increment\")]";
$this->assertNoFieldByXPath($xpathIncr, NULL, 'Increment element not found for Date Only.'); $this->assertNoFieldByXPath($xpathIncr, NULL, 'Increment element not found for Date Only.');
@ -683,7 +693,7 @@ class DateRangeFieldTest extends DateTestBase {
$this->drupalGet($fieldEditUrl); $this->drupalGet($fieldEditUrl);
// Click on the widget settings button to open the widget settings form. // Click on the widget settings button to open the widget settings form.
$this->drupalPostAjaxForm(NULL, [], $field_name . "_settings_edit"); $this->drupalPostForm(NULL, [], $field_name . "_settings_edit");
$this->assertFieldByXPath($xpathIncr, NULL, 'Increment element found for Date and time.'); $this->assertFieldByXPath($xpathIncr, NULL, 'Increment element found for Date and time.');
// Display creation form. // Display creation form.
@ -712,7 +722,7 @@ class DateRangeFieldTest extends DateTestBase {
} }
$this->drupalPostForm(NULL, $edit, t('Save')); $this->drupalPostForm(NULL, $edit, t('Save'));
preg_match('|entity_test/manage/(\d+)|', $this->url, $match); preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
$id = $match[1]; $id = $match[1];
$this->assertText(t('entity_test @id has been created.', ['@id' => $id])); $this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
@ -767,7 +777,7 @@ class DateRangeFieldTest extends DateTestBase {
} }
$this->drupalPostForm(NULL, $edit, t('Save')); $this->drupalPostForm(NULL, $edit, t('Save'));
preg_match('|entity_test/manage/(\d+)|', $this->url, $match); preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
$id = $match[1]; $id = $match[1];
$this->assertText(t('entity_test @id has been created.', ['@id' => $id])); $this->assertText(t('entity_test @id has been created.', ['@id' => $id]));
@ -834,7 +844,7 @@ class DateRangeFieldTest extends DateTestBase {
$this->drupalPostForm(NULL, $edit, t('Save')); $this->drupalPostForm(NULL, $edit, t('Save'));
$this->assertResponse(200); $this->assertResponse(200);
preg_match('|entity_test/manage/(\d+)|', $this->url, $match); preg_match('|entity_test/manage/(\d+)|', $this->getUrl(), $match);
$id = $match[1]; $id = $match[1];
$this->assertText(t('entity_test @id has been created.', ['@id' => $id])); $this->assertText(t('entity_test @id has been created.', ['@id' => $id]));