TimestampTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 entity_get_display().
  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 function setUp() {
  42. parent::setUp();
  43. $web_user = $this->drupalCreateUser([
  44. 'access content',
  45. 'view test entity',
  46. 'administer entity_test content',
  47. 'administer entity_test form display',
  48. 'administer content types',
  49. 'administer node fields',
  50. ]);
  51. $this->drupalLogin($web_user);
  52. $field_name = 'field_timestamp';
  53. $type = 'timestamp';
  54. $widget_type = 'datetime_timestamp';
  55. $formatter_type = 'timestamp';
  56. $this->fieldStorage = FieldStorageConfig::create([
  57. 'field_name' => $field_name,
  58. 'entity_type' => 'entity_test',
  59. 'type' => $type,
  60. ]);
  61. $this->fieldStorage->save();
  62. $this->field = FieldConfig::create([
  63. 'field_storage' => $this->fieldStorage,
  64. 'bundle' => 'entity_test',
  65. 'required' => TRUE,
  66. ]);
  67. $this->field->save();
  68. EntityFormDisplay::load('entity_test.entity_test.default')
  69. ->setComponent($field_name, ['type' => $widget_type])
  70. ->save();
  71. $this->displayOptions = [
  72. 'type' => $formatter_type,
  73. 'label' => 'hidden',
  74. ];
  75. EntityViewDisplay::create([
  76. 'targetEntityType' => $this->field->getTargetEntityTypeId(),
  77. 'bundle' => $this->field->getTargetBundle(),
  78. 'mode' => 'full',
  79. 'status' => TRUE,
  80. ])->setComponent($field_name, $this->displayOptions)
  81. ->save();
  82. }
  83. /**
  84. * Tests the "datetime_timestamp" widget.
  85. */
  86. public function testWidget() {
  87. // Build up a date in the UTC timezone.
  88. $value = '2012-12-31 00:00:00';
  89. $date = new DrupalDateTime($value, 'UTC');
  90. // Update the timezone to the system default.
  91. $date->setTimezone(timezone_open(drupal_get_user_timezone()));
  92. // Display creation form.
  93. $this->drupalGet('entity_test/add');
  94. // Make sure the "datetime_timestamp" widget is on the page.
  95. $fields = $this->xpath('//div[contains(@class, "field--widget-datetime-timestamp") and @id="edit-field-timestamp-wrapper"]');
  96. $this->assertEquals(1, count($fields));
  97. // Look for the widget elements and make sure they are empty.
  98. $this->assertSession()->fieldExists('field_timestamp[0][value][date]');
  99. $this->assertSession()->fieldValueEquals('field_timestamp[0][value][date]', '');
  100. $this->assertSession()->fieldExists('field_timestamp[0][value][time]');
  101. $this->assertSession()->fieldValueEquals('field_timestamp[0][value][time]', '');
  102. // Submit the date.
  103. $date_format = DateFormat::load('html_date')->getPattern();
  104. $time_format = DateFormat::load('html_time')->getPattern();
  105. $edit = [
  106. 'field_timestamp[0][value][date]' => $date->format($date_format),
  107. 'field_timestamp[0][value][time]' => $date->format($time_format),
  108. ];
  109. $this->drupalPostForm(NULL, $edit, 'Save');
  110. // Make sure the submitted date is set as the default in the widget.
  111. $this->assertSession()->fieldExists('field_timestamp[0][value][date]');
  112. $this->assertSession()->fieldValueEquals('field_timestamp[0][value][date]', $date->format($date_format));
  113. $this->assertSession()->fieldExists('field_timestamp[0][value][time]');
  114. $this->assertSession()->fieldValueEquals('field_timestamp[0][value][time]', $date->format($time_format));
  115. // Make sure the entity was saved.
  116. preg_match('|entity_test/manage/(\d+)|', $this->getSession()->getCurrentUrl(), $match);
  117. $id = $match[1];
  118. $this->assertSession()->pageTextContains(sprintf('entity_test %s has been created.', $id));
  119. // Make sure the timestamp is output properly with the default formatter.
  120. $medium = DateFormat::load('medium')->getPattern();
  121. $this->drupalGet('entity_test/' . $id);
  122. $this->assertSession()->pageTextContains($date->format($medium));
  123. }
  124. }