From f03979ccf135ab86bee187cc21d0d3fcdb6874d0 Mon Sep 17 00:00:00 2001 From: xjm <xjm@65776.no-reply.drupal.org> Date: Fri, 2 Sep 2016 14:23:40 -0500 Subject: [PATCH] Issue #2786583 by claudiu.cristea, jhedstrom, effulgentsia: Create a base class for DateTimeFieldTest and DateRangeFieldTest and move common code into it --- .../datetime/src/Tests/DateTestBase.php | 182 ++++++++++++++++++ .../datetime/src/Tests/DateTimeFieldTest.php | 149 +------------- .../src/Tests/DateRangeFieldTest.php | 149 +------------- 3 files changed, 196 insertions(+), 284 deletions(-) create mode 100644 core/modules/datetime/src/Tests/DateTestBase.php diff --git a/core/modules/datetime/src/Tests/DateTestBase.php b/core/modules/datetime/src/Tests/DateTestBase.php new file mode 100644 index 000000000000..0f1277e92210 --- /dev/null +++ b/core/modules/datetime/src/Tests/DateTestBase.php @@ -0,0 +1,182 @@ +<?php + +namespace Drupal\datetime\Tests; + +use Drupal\Component\Utility\Unicode; +use Drupal\Core\Entity\Entity\EntityFormDisplay; +use Drupal\Core\Entity\Entity\EntityViewDisplay; +use Drupal\datetime_range\Plugin\Field\FieldType\DateRangeItem; +use Drupal\entity_test\Entity\EntityTest; +use Drupal\field\Entity\FieldConfig; +use Drupal\field\Entity\FieldStorageConfig; +use Drupal\simpletest\WebTestBase; + +/** + * Provides a base class for testing Datetime field functionality. + */ +abstract class DateTestBase extends WebTestBase { + + /** + * 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' => DateRangeItem::DATETIME_TYPE_DATE], + ]); + $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', + '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. + */ + 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); + $output = $this->container->get('renderer')->renderRoot($build); + $this->setRawContent($output); + $this->verbose($output); + } + + /** + * 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(); + } + +} diff --git a/core/modules/datetime/src/Tests/DateTimeFieldTest.php b/core/modules/datetime/src/Tests/DateTimeFieldTest.php index 64576f38d01d..c729b232a3b9 100644 --- a/core/modules/datetime/src/Tests/DateTimeFieldTest.php +++ b/core/modules/datetime/src/Tests/DateTimeFieldTest.php @@ -6,125 +6,30 @@ use Drupal\Component\Utility\Unicode; use Drupal\Core\Datetime\DrupalDateTime; use Drupal\Core\Datetime\Entity\DateFormat; -use Drupal\Core\Entity\Entity\EntityViewDisplay; use Drupal\entity_test\Entity\EntityTest; use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; use Drupal\node\Entity\Node; -use Drupal\simpletest\WebTestBase; /** * Tests Datetime field functionality. * * @group datetime */ -class DateTimeFieldTest extends WebTestBase { - - /** - * Modules to enable. - * - * @var array - */ - public static $modules = array('node', 'entity_test', 'datetime', 'field_ui'); +class DateTimeFieldTest extends DateTestBase { /** * The default display settings to use for the formatters. - */ - protected $defaultSettings; - - /** - * 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; - - /** - * 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', - ]; + protected $defaultSettings = ['timezone_override' => '']; /** * {@inheritdoc} */ - protected function setUp() { - parent::setUp(); - - $web_user = $this->drupalCreateUser(array( - '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. - $field_name = Unicode::strtolower($this->randomMachineName()); - $this->fieldStorage = FieldStorageConfig::create(array( - 'field_name' => $field_name, - 'entity_type' => 'entity_test', - 'type' => 'datetime', - 'settings' => array('datetime_type' => 'date'), - )); - $this->fieldStorage->save(); - $this->field = FieldConfig::create([ - 'field_storage' => $this->fieldStorage, - 'bundle' => 'entity_test', - 'required' => TRUE, - ]); - $this->field->save(); - - entity_get_form_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'default') - ->setComponent($field_name, array( - 'type' => 'datetime_default', - )) - ->save(); - - $this->defaultSettings = array( - 'timezone_override' => '', - ); - - $this->displayOptions = array( - 'type' => 'datetime_default', - 'label' => 'hidden', - 'settings' => array('format_type' => 'medium') + $this->defaultSettings, - ); - entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full') - ->setComponent($field_name, $this->displayOptions) - ->save(); + protected function getTestFieldType() { + return 'datetime'; } /** @@ -256,7 +161,7 @@ function testDateField() { ->setComponent($field_name, $this->displayOptions) ->save(); $expected = SafeMarkup::format($this->displayOptions['settings']['past_format'], [ - '@interval' => \Drupal::service('date.formatter')->formatTimeDiffSince($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]) + '@interval' => $this->dateFormatter->formatTimeDiffSince($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]) ]); $this->renderTestEntity($id); $this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', array('%expected' => $expected))); @@ -277,7 +182,7 @@ function testDateField() { ->setComponent($field_name, $this->displayOptions) ->save(); $expected = SafeMarkup::format($this->displayOptions['settings']['future_format'], [ - '@interval' => \Drupal::service('date.formatter')->formatTimeDiffUntil($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]) + '@interval' => $this->dateFormatter->formatTimeDiffUntil($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]) ]); $this->renderTestEntity($id); $this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', array('%expected' => $expected))); @@ -397,7 +302,7 @@ function testDatetimeField() { ->setComponent($field_name, $this->displayOptions) ->save(); $expected = SafeMarkup::format($this->displayOptions['settings']['past_format'], [ - '@interval' => \Drupal::service('date.formatter')->formatTimeDiffSince($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]) + '@interval' => $this->dateFormatter->formatTimeDiffSince($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]) ]); $this->renderTestEntity($id); $this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', array('%expected' => $expected))); @@ -418,7 +323,7 @@ function testDatetimeField() { ->setComponent($field_name, $this->displayOptions) ->save(); $expected = SafeMarkup::format($this->displayOptions['settings']['future_format'], [ - '@interval' => \Drupal::service('date.formatter')->formatTimeDiffUntil($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]) + '@interval' => $this->dateFormatter->formatTimeDiffUntil($timestamp, ['granularity' => $this->displayOptions['settings']['granularity']]) ]); $this->renderTestEntity($id); $this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', array('%expected' => $expected))); @@ -894,42 +799,4 @@ public function testDateStorageSettings() { $this->assertText('There is data for this field in the database. The field settings can no longer be changed.'); } - /** - * 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. - */ - protected function renderTestEntity($id, $view_mode = 'full', $reset = TRUE) { - if ($reset) { - \Drupal::entityManager()->getStorage('entity_test')->resetCache(array($id)); - } - $entity = EntityTest::load($id); - $display = EntityViewDisplay::collectRenderDisplay($entity, $view_mode); - $build = $display->build($entity); - $output = \Drupal::service('renderer')->renderRoot($build); - $this->setRawContent($output); - $this->verbose($output); - } - - /** - * 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) - // A timezone with an offset greater than UTC+12 is used. - ->set('timezone.default', $timezone) - ->save(); - } - } diff --git a/core/modules/datetime_range/src/Tests/DateRangeFieldTest.php b/core/modules/datetime_range/src/Tests/DateRangeFieldTest.php index 078a67e88160..2afb5f5ec9eb 100644 --- a/core/modules/datetime_range/src/Tests/DateRangeFieldTest.php +++ b/core/modules/datetime_range/src/Tests/DateRangeFieldTest.php @@ -6,138 +6,39 @@ use Drupal\Component\Utility\Unicode; use Drupal\Core\Datetime\DrupalDateTime; use Drupal\Core\Datetime\Entity\DateFormat; -use Drupal\Core\Entity\Entity\EntityViewDisplay; +use Drupal\datetime\Tests\DateTestBase; use Drupal\datetime_range\Plugin\Field\FieldType\DateRangeItem; use Drupal\entity_test\Entity\EntityTest; use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; use Drupal\node\Entity\Node; -use Drupal\simpletest\WebTestBase; /** * Tests Daterange field functionality. * * @group datetime */ -class DateRangeFieldTest extends WebTestBase { +class DateRangeFieldTest extends DateTestBase { /** * Modules to enable. * * @var array */ - public static $modules = ['node', 'entity_test', 'datetime', 'datetime_range', 'field_ui']; + public static $modules = ['datetime_range']; /** * The default display settings to use for the formatters. * * @var array */ - protected $defaultSettings; - - /** - * 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; - - /** - * 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', - ]; - - /** - * The date formatter service. - * - * @var \Drupal\Core\Datetime\DateFormatterInterface - */ - protected $dateFormatter; + protected $defaultSettings = ['timezone_override' => '', 'separator' => '-']; /** * {@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. - $field_name = Unicode::strtolower($this->randomMachineName()); - $this->fieldStorage = FieldStorageConfig::create([ - 'field_name' => $field_name, - 'entity_type' => 'entity_test', - 'type' => 'daterange', - 'settings' => ['datetime_type' => DateRangeItem::DATETIME_TYPE_DATE], - ]); - $this->fieldStorage->save(); - $this->field = FieldConfig::create([ - 'field_storage' => $this->fieldStorage, - 'bundle' => 'entity_test', - 'required' => TRUE, - ]); - $this->field->save(); - - entity_get_form_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'default') - ->setComponent($field_name, [ - 'type' => 'daterange_default', - ]) - ->save(); - - $this->defaultSettings = [ - 'separator' => '-', - 'timezone_override' => '', - ]; - - $this->displayOptions = [ - 'type' => 'daterange_default', - 'label' => 'hidden', - 'settings' => ['format_type' => 'medium'] + $this->defaultSettings, - ]; - entity_get_display($this->field->getTargetEntityTypeId(), $this->field->getTargetBundle(), 'full') - ->setComponent($field_name, $this->displayOptions) - ->save(); - - $this->dateFormatter = \Drupal::service('date.formatter'); + protected function getTestFieldType() { + return 'daterange'; } /** @@ -1391,42 +1292,4 @@ public function testDateStorageSettings() { $this->assertText('There is data for this field in the database. The field settings can no longer be changed.'); } - /** - * 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. - */ - protected function renderTestEntity($id, $view_mode = 'full', $reset = TRUE) { - if ($reset) { - \Drupal::service('entity_type.manager')->getStorage('entity_test')->resetCache([$id]); - } - $entity = EntityTest::load($id); - $display = EntityViewDisplay::collectRenderDisplay($entity, $view_mode); - $build = $display->build($entity); - $output = \Drupal::service('renderer')->renderRoot($build); - $this->setRawContent($output); - $this->verbose($output); - } - - /** - * 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) - // A timezone with an offset greater than UTC+12 is used. - ->set('timezone.default', $timezone) - ->save(); - } - } -- GitLab