summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorxjm2017-03-04 19:52:45 (GMT)
committerxjm2017-03-04 19:52:45 (GMT)
commitcd176e1d2438e925950fd2d2e256fb8d6c57980f (patch)
tree79a38c0d17596c158b1a12b7befff0774b62d0eb
parentfa39f06433eebd7f2b52bc48a33a7fba05d6f150 (diff)
Issue #2802663 by mpdonadio, jibran, Berdir, jhedstrom, amateescu: ChangedItem, CreatedItem, and TimestampItem fields have an implicit dependency on datetime module
-rw-r--r--core/lib/Drupal/Core/Field/Plugin/Field/FieldType/ChangedItem.php2
-rw-r--r--core/lib/Drupal/Core/Field/Plugin/Field/FieldType/CreatedItem.php2
-rw-r--r--core/lib/Drupal/Core/Field/Plugin/Field/FieldType/TimestampItem.php2
-rw-r--r--core/modules/system/system.post_update.php18
-rw-r--r--core/tests/Drupal/FunctionalTests/Datetime/TimestampTest.php149
5 files changed, 170 insertions, 3 deletions
diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/ChangedItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/ChangedItem.php
index 9f2096f..9c53665 100644
--- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/ChangedItem.php
+++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/ChangedItem.php
@@ -13,7 +13,7 @@ namespace Drupal\Core\Field\Plugin\Field\FieldType;
* label = @Translation("Last changed"),
* description = @Translation("An entity field containing a UNIX timestamp of when the entity has been last updated."),
* no_ui = TRUE,
- * default_widget = "datetime_default",
+ * default_widget = "datetime_timestamp",
* default_formatter = "timestamp",
* list_class = "\Drupal\Core\Field\ChangedFieldItemList"
* )
diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/CreatedItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/CreatedItem.php
index 228c8e6..da2e2a7 100644
--- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/CreatedItem.php
+++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/CreatedItem.php
@@ -10,7 +10,7 @@ namespace Drupal\Core\Field\Plugin\Field\FieldType;
* label = @Translation("Created"),
* description = @Translation("An entity field containing a UNIX timestamp of when the entity has been created."),
* no_ui = TRUE,
- * default_widget = "datetime_default",
+ * default_widget = "datetime_timestamp",
* default_formatter = "timestamp"
* )
*/
diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/TimestampItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/TimestampItem.php
index f48e4f0..8c3ffd2 100644
--- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/TimestampItem.php
+++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/TimestampItem.php
@@ -13,7 +13,7 @@ use Drupal\Core\TypedData\DataDefinition;
* id = "timestamp",
* label = @Translation("Timestamp"),
* description = @Translation("An entity field containing a UNIX timestamp value."),
- * default_widget = "datetime_default",
+ * default_widget = "datetime_timestamp",
* default_formatter = "timestamp",
* constraints = {
* "ComplexData" = {
diff --git a/core/modules/system/system.post_update.php b/core/modules/system/system.post_update.php
index 01f304b..e123f91 100644
--- a/core/modules/system/system.post_update.php
+++ b/core/modules/system/system.post_update.php
@@ -65,3 +65,21 @@ function system_post_update_add_region_to_entity_displays() {
function system_post_update_hashes_clear_cache() {
// Empty post-update hook.
}
+
+/**
+ * @addtogroup updates-8.3.x
+ * @{
+ */
+
+/**
+ * Force plugin definitions to be cleared.
+ *
+ * @see https://www.drupal.org/node/2802663
+ */
+function system_post_update_timestamp_plugins() {
+ // Empty post-update hook.
+}
+
+/**
+ * @} End of "addtogroup uupdates-8.3.x".
+ */
diff --git a/core/tests/Drupal/FunctionalTests/Datetime/TimestampTest.php b/core/tests/Drupal/FunctionalTests/Datetime/TimestampTest.php
new file mode 100644
index 0000000..8faf8c6
--- /dev/null
+++ b/core/tests/Drupal/FunctionalTests/Datetime/TimestampTest.php
@@ -0,0 +1,149 @@
+<?php
+
+namespace Drupal\FunctionalTests\Datetime;
+
+use Drupal\Core\Datetime\DrupalDateTime;
+use Drupal\Core\Datetime\Entity\DateFormat;
+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 Timestamp core field UI.
+ *
+ * @group field
+ */
+class TimestampTest 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 = ['node', 'entity_test', 'field_ui'];
+
+ /**
+ * {@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);
+ $field_name = 'field_timestamp';
+ $type = 'timestamp';
+ $widget_type = 'datetime_timestamp';
+ $formatter_type = 'timestamp';
+
+ $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 "datetime_timestamp" widget.
+ */
+ public function testWidget() {
+ // Build up a date in the UTC timezone.
+ $value = '2012-12-31 00:00:00';
+ $date = new DrupalDateTime($value, 'UTC');
+
+ // Update the timezone to the system default.
+ $date->setTimezone(timezone_open(drupal_get_user_timezone()));
+
+ // Display creation form.
+ $this->drupalGet('entity_test/add');
+
+ // Make sure the "datetime_timestamp" widget is on the page.
+ $fields = $this->xpath('//div[contains(@class, "field--widget-datetime-timestamp") and @id="edit-field-timestamp-wrapper"]');
+ $this->assertEquals(1, count($fields));
+
+ // Look for the widget elements and make sure they are empty.
+ $this->assertSession()->fieldExists('field_timestamp[0][value][date]');
+ $this->assertSession()->fieldValueEquals('field_timestamp[0][value][date]', '');
+ $this->assertSession()->fieldExists('field_timestamp[0][value][time]');
+ $this->assertSession()->fieldValueEquals('field_timestamp[0][value][time]', '');
+
+ // Submit the date.
+ $date_format = DateFormat::load('html_date')->getPattern();
+ $time_format = DateFormat::load('html_time')->getPattern();
+
+ $edit = [
+ 'field_timestamp[0][value][date]' => $date->format($date_format),
+ 'field_timestamp[0][value][time]' => $date->format($time_format),
+ ];
+ $this->drupalPostForm(NULL, $edit, 'Save');
+
+ // Make sure the submitted date is set as the default in the widget.
+ $this->assertSession()->fieldExists('field_timestamp[0][value][date]');
+ $this->assertSession()->fieldValueEquals('field_timestamp[0][value][date]', $date->format($date_format));
+ $this->assertSession()->fieldExists('field_timestamp[0][value][time]');
+ $this->assertSession()->fieldValueEquals('field_timestamp[0][value][time]', $date->format($time_format));
+
+ // Make sure the entity was saved.
+ preg_match('|entity_test/manage/(\d+)|', $this->getSession()->getCurrentUrl(), $match);
+ $id = $match[1];
+ $this->assertSession()->pageTextContains(sprintf('entity_test %s has been created.', $id));
+
+ // Make sure the timestamp is output properly with the default formatter.
+ $medium = DateFormat::load('medium')->getPattern();
+ $this->drupalGet('entity_test/' . $id);
+ $this->assertSession()->pageTextContains($date->format($medium));
+ }
+
+}