TimestampTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace Drupal\FunctionalTests\Datetime;
  3. use Drupal\Core\Datetime\DrupalDateTime;
  4. use Drupal\Core\Datetime\Entity\DateFormat;
  5. use Drupal\Core\Entity\Entity\EntityFormDisplay;
  6. use Drupal\Core\Entity\Entity\EntityViewDisplay;
  7. use Drupal\field\Entity\FieldConfig;
  8. use Drupal\field\Entity\FieldStorageConfig;
  9. use Drupal\Tests\BrowserTestBase;
  10. /**
  11. * Tests the functionality of Timestamp core field UI.
  12. *
  13. * @group field
  14. */
  15. class TimestampTest extends BrowserTestBase {
  16. /**
  17. * An array of display options to pass to EntityDisplayRepositoryInterface::getViewDisplay().
  18. *
  19. * @var array
  20. */
  21. protected $displayOptions;
  22. /**
  23. * A field storage to use in this test class.
  24. *
  25. * @var \Drupal\field\Entity\FieldStorageConfig
  26. */
  27. protected $fieldStorage;
  28. /**
  29. * The field used in this test class.
  30. *
  31. * @var \Drupal\field\Entity\FieldConfig
  32. */
  33. protected $field;
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public static $modules = ['node', 'entity_test', 'field_ui'];
  38. /**
  39. * {@inheritdoc}
  40. */
  41. protected $defaultTheme = 'stark';
  42. /**
  43. * {@inheritdoc}
  44. */
  45. protected function setUp() {
  46. parent::setUp();
  47. $web_user = $this->drupalCreateUser([
  48. 'access content',
  49. 'view test entity',
  50. 'administer entity_test content',
  51. 'administer entity_test form display',
  52. 'administer content types',
  53. 'administer node fields',
  54. ]);
  55. $this->drupalLogin($web_user);
  56. $field_name = 'field_timestamp';
  57. $type = 'timestamp';
  58. $widget_type = 'datetime_timestamp';
  59. $formatter_type = 'timestamp';
  60. $this->fieldStorage = FieldStorageConfig::create([
  61. 'field_name' => $field_name,
  62. 'entity_type' => 'entity_test',
  63. 'type' => $type,
  64. ]);
  65. $this->fieldStorage->save();
  66. $this->field = FieldConfig::create([
  67. 'field_storage' => $this->fieldStorage,
  68. 'bundle' => 'entity_test',
  69. 'required' => TRUE,
  70. ]);
  71. $this->field->save();
  72. EntityFormDisplay::load('entity_test.entity_test.default')
  73. ->setComponent($field_name, ['type' => $widget_type])
  74. ->save();
  75. $this->displayOptions = [
  76. 'type' => $formatter_type,
  77. 'label' => 'hidden',
  78. ];
  79. EntityViewDisplay::create([
  80. 'targetEntityType' => $this->field->getTargetEntityTypeId(),
  81. 'bundle' => $this->field->getTargetBundle(),
  82. 'mode' => 'full',
  83. 'status' => TRUE,
  84. ])->setComponent($field_name, $this->displayOptions)
  85. ->save();
  86. }
  87. /**
  88. * Tests the "datetime_timestamp" widget.
  89. */
  90. public function testWidget() {
  91. // Build up a date in the UTC timezone.
  92. $value = '2012-12-31 00:00:00';
  93. $date = new DrupalDateTime($value, 'UTC');
  94. // Update the timezone to the system default.
  95. $date->setTimezone(timezone_open(date_default_timezone_get()));
  96. // Display creation form.
  97. $this->drupalGet('entity_test/add');
  98. // Make sure the "datetime_timestamp" widget is on the page.
  99. $fields = $this->xpath('//div[contains(@class, "field--widget-datetime-timestamp") and @id="edit-field-timestamp-wrapper"]');
  100. $this->assertCount(1, $fields);
  101. // Look for the widget elements and make sure they are empty.
  102. $this->assertSession()->fieldExists('field_timestamp[0][value][date]');
  103. $this->assertSession()->fieldValueEquals('field_timestamp[0][value][date]', '');
  104. $this->assertSession()->fieldExists('field_timestamp[0][value][time]');
  105. $this->assertSession()->fieldValueEquals('field_timestamp[0][value][time]', '');
  106. // Submit the date.
  107. $date_format = DateFormat::load('html_date')->getPattern();
  108. $time_format = DateFormat::load('html_time')->getPattern();
  109. $edit = [
  110. 'field_timestamp[0][value][date]' => $date->format($date_format),
  111. 'field_timestamp[0][value][time]' => $date->format($time_format),
  112. ];
  113. $this->drupalPostForm(NULL, $edit, 'Save');
  114. // Make sure the submitted date is set as the default in the widget.
  115. $this->assertSession()->fieldExists('field_timestamp[0][value][date]');
  116. $this->assertSession()->fieldValueEquals('field_timestamp[0][value][date]', $date->format($date_format));
  117. $this->assertSession()->fieldExists('field_timestamp[0][value][time]');
  118. $this->assertSession()->fieldValueEquals('field_timestamp[0][value][time]', $date->format($time_format));
  119. // Make sure the entity was saved.
  120. preg_match('|entity_test/manage/(\d+)|', $this->getSession()->getCurrentUrl(), $match);
  121. $id = $match[1];
  122. $this->assertSession()->pageTextContains(sprintf('entity_test %s has been created.', $id));
  123. // Make sure the timestamp is output properly with the default formatter.
  124. $medium = DateFormat::load('medium')->getPattern();
  125. $this->drupalGet('entity_test/' . $id);
  126. $this->assertSession()->pageTextContains($date->format($medium));
  127. }
  128. }