PageRenderTime 50ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/system/src/Tests/System/DateTimeTest.php

http://github.com/drupal/drupal
PHP | 227 lines | 159 code | 33 blank | 35 comment | 0 complexity | 472c51d53daae2740cfefb1ee6e0eeda MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. namespace Drupal\system\Tests\System;
  3. use Drupal\Core\Datetime\Entity\DateFormat;
  4. use Drupal\Core\Url;
  5. use Drupal\simpletest\WebTestBase;
  6. /**
  7. * Configure date and time settings. Test date formatting and time zone
  8. * handling, including daylight saving time.
  9. *
  10. * @group system
  11. */
  12. class DateTimeTest extends WebTestBase {
  13. /**
  14. * Modules to enable.
  15. *
  16. * @var array
  17. */
  18. public static $modules = ['block', 'node', 'language', 'field', 'field_ui', 'datetime', 'options'];
  19. protected function setUp() {
  20. parent::setUp();
  21. // Create admin user and log in admin user.
  22. $this->drupalLogin ($this->drupalCreateUser(array(
  23. 'administer site configuration',
  24. 'administer content types',
  25. 'administer nodes',
  26. 'administer node fields',
  27. 'administer node form display',
  28. 'administer node display',
  29. )));
  30. $this->drupalPlaceBlock('local_actions_block');
  31. }
  32. /**
  33. * Test time zones and DST handling.
  34. */
  35. function testTimeZoneHandling() {
  36. // Setup date/time settings for Honolulu time.
  37. $config = $this->config('system.date')
  38. ->set('timezone.default', 'Pacific/Honolulu')
  39. ->set('timezone.user.configurable', 0)
  40. ->save();
  41. DateFormat::load('medium')
  42. ->setPattern('Y-m-d H:i:s O')
  43. ->save();
  44. // Create some nodes with different authored-on dates.
  45. $date1 = '2007-01-31 21:00:00 -1000';
  46. $date2 = '2007-07-31 21:00:00 -1000';
  47. $this->drupalCreateContentType(array('type' => 'article'));
  48. $node1 = $this->drupalCreateNode(array('created' => strtotime($date1), 'type' => 'article'));
  49. $node2 = $this->drupalCreateNode(array('created' => strtotime($date2), 'type' => 'article'));
  50. // Confirm date format and time zone.
  51. $this->drupalGet('node/' . $node1->id());
  52. $this->assertText('2007-01-31 21:00:00 -1000', 'Date should be identical, with GMT offset of -10 hours.');
  53. $this->drupalGet('node/' . $node2->id());
  54. $this->assertText('2007-07-31 21:00:00 -1000', 'Date should be identical, with GMT offset of -10 hours.');
  55. // Set time zone to Los Angeles time.
  56. $config->set('timezone.default', 'America/Los_Angeles')->save();
  57. \Drupal::entityManager()->getViewBuilder('node')->resetCache(array($node1, $node2));
  58. // Confirm date format and time zone.
  59. $this->drupalGet('node/' . $node1->id());
  60. $this->assertText('2007-01-31 23:00:00 -0800', 'Date should be two hours ahead, with GMT offset of -8 hours.');
  61. $this->drupalGet('node/' . $node2->id());
  62. $this->assertText('2007-08-01 00:00:00 -0700', 'Date should be three hours ahead, with GMT offset of -7 hours.');
  63. }
  64. /**
  65. * Test date format configuration.
  66. */
  67. function testDateFormatConfiguration() {
  68. // Confirm 'no custom date formats available' message appears.
  69. $this->drupalGet('admin/config/regional/date-time');
  70. // Add custom date format.
  71. $this->clickLink(t('Add format'));
  72. $date_format_id = strtolower($this->randomMachineName(8));
  73. $name = ucwords($date_format_id);
  74. $date_format = 'd.m.Y - H:i';
  75. $edit = array(
  76. 'id' => $date_format_id,
  77. 'label' => $name,
  78. 'date_format_pattern' => $date_format,
  79. );
  80. $this->drupalPostForm('admin/config/regional/date-time/formats/add', $edit, t('Add format'));
  81. $this->assertUrl(\Drupal::url('entity.date_format.collection', [], ['absolute' => TRUE]), [], 'Correct page redirection.');
  82. $this->assertText(t('Custom date format added.'), 'Date format added confirmation message appears.');
  83. $this->assertText($name, 'Custom date format appears in the date format list.');
  84. $this->assertText(t('Delete'), 'Delete link for custom date format appears.');
  85. // Edit the custom date format and re-save without editing the format.
  86. $this->drupalGet('admin/config/regional/date-time');
  87. $this->clickLink(t('Edit'));
  88. $this->drupalPostForm(NULL, NULL, t('Save format'));
  89. $this->assertUrl('admin/config/regional/date-time', array('absolute' => TRUE), 'Correct page redirection.');
  90. $this->assertText(t('Custom date format updated.'), 'Custom date format successfully updated.');
  91. // Edit custom date format.
  92. $this->drupalGet('admin/config/regional/date-time');
  93. $this->clickLink(t('Edit'));
  94. $edit = array(
  95. 'date_format_pattern' => 'Y m',
  96. );
  97. $this->drupalPostForm($this->getUrl(), $edit, t('Save format'));
  98. $this->assertUrl(\Drupal::url('entity.date_format.collection', [], ['absolute' => TRUE]), [], 'Correct page redirection.');
  99. $this->assertText(t('Custom date format updated.'), 'Custom date format successfully updated.');
  100. // Delete custom date format.
  101. $this->clickLink(t('Delete'));
  102. $this->drupalPostForm('admin/config/regional/date-time/formats/manage/' . $date_format_id . '/delete', array(), t('Delete'));
  103. $this->assertUrl(\Drupal::url('entity.date_format.collection', [], ['absolute' => TRUE]), [], 'Correct page redirection.');
  104. $this->assertRaw(t('The date format %format has been deleted.', array('%format' => $name)), 'Custom date format removed.');
  105. // Make sure the date does not exist in config.
  106. $date_format = DateFormat::load($date_format_id);
  107. $this->assertFalse($date_format);
  108. // Add a new date format with an existing format.
  109. $date_format_id = strtolower($this->randomMachineName(8));
  110. $name = ucwords($date_format_id);
  111. $date_format = 'Y';
  112. $edit = array(
  113. 'id' => $date_format_id,
  114. 'label' => $name,
  115. 'date_format_pattern' => $date_format,
  116. );
  117. $this->drupalPostForm('admin/config/regional/date-time/formats/add', $edit, t('Add format'));
  118. $this->assertUrl(\Drupal::url('entity.date_format.collection', [], ['absolute' => TRUE]), [], 'Correct page redirection.');
  119. $this->assertText(t('Custom date format added.'), 'Date format added confirmation message appears.');
  120. $this->assertText($name, 'Custom date format appears in the date format list.');
  121. $this->assertText(t('Delete'), 'Delete link for custom date format appears.');
  122. $date_format = DateFormat::create(array(
  123. 'id' => 'xss_short',
  124. 'label' => 'XSS format',
  125. 'pattern' => '\<\s\c\r\i\p\t\>\a\l\e\r\t\(\'\X\S\S\'\)\;\<\/\s\c\r\i\p\t\>',
  126. ));
  127. $date_format->save();
  128. $this->drupalGet(Url::fromRoute('entity.date_format.collection'));
  129. $this->assertEscaped("<script>alert('XSS');</script>", 'The date format was properly escaped');
  130. // Add a new date format with HTML in it.
  131. $date_format_id = strtolower($this->randomMachineName(8));
  132. $name = ucwords($date_format_id);
  133. $date_format = '& \<\e\m\>Y\<\/\e\m\>';
  134. $edit = array(
  135. 'id' => $date_format_id,
  136. 'label' => $name,
  137. 'date_format_pattern' => $date_format,
  138. );
  139. $this->drupalPostForm('admin/config/regional/date-time/formats/add', $edit, t('Add format'));
  140. $this->assertUrl(\Drupal::url('entity.date_format.collection', [], ['absolute' => TRUE]), [], 'Correct page redirection.');
  141. $this->assertText(t('Custom date format added.'), 'Date format added confirmation message appears.');
  142. $this->assertText($name, 'Custom date format appears in the date format list.');
  143. $this->assertEscaped('<em>' . date("Y") . '</em>');
  144. }
  145. /**
  146. * Test handling case with invalid data in selectors (like February, 31st).
  147. */
  148. function testEnteringDateTimeViaSelectors() {
  149. $this->drupalCreateContentType(array('type' => 'page_with_date', 'name' => 'Page with date'));
  150. $this->drupalGet('admin/structure/types/manage/page_with_date');
  151. $this->assertResponse(200, 'Content type created.');
  152. $this->drupalGet('admin/structure/types/manage/page_with_date/fields/add-field');
  153. $edit = array(
  154. 'new_storage_type' => 'datetime',
  155. 'label' => 'dt',
  156. 'field_name' => 'dt',
  157. );
  158. $this->drupalPostForm('admin/structure/types/manage/page_with_date/fields/add-field', $edit, t('Save and continue'));
  159. $this->assertText(t('These settings apply to the'), 'New datetime field created, now configuring');
  160. $this->drupalGet('admin/structure/types/manage/page_with_date/fields/node.page_with_date.field_dt/storage');
  161. $edit = array(
  162. 'settings[datetime_type]' => 'datetime',
  163. 'cardinality' => 'number',
  164. 'cardinality_number' => '1',
  165. );
  166. $this->drupalPostForm('admin/structure/types/manage/page_with_date/fields/node.page_with_date.field_dt/storage', $edit, t('Save field settings'));
  167. $this->drupalGet('admin/structure/types/manage/page_with_date/fields');
  168. $this->assertText('field_dt', 'New field is in place');
  169. $this->drupalGet('admin/structure/types/manage/page_with_date/form-display');
  170. $edit = array(
  171. 'fields[field_dt][type]' => 'datetime_datelist',
  172. );
  173. $this->drupalPostForm('admin/structure/types/manage/page_with_date/form-display', $edit, t('Save'));
  174. $this->drupalLogout();
  175. // Now log in as a regular editor.
  176. $this->drupalLogin($this->drupalCreateUser(array('create page_with_date content')));
  177. $this->drupalGet('node/add/page_with_date');
  178. $edit = array(
  179. 'title[0][value]' => 'sample doc',
  180. 'field_dt[0][value][year]' => '2016',
  181. 'field_dt[0][value][month]' => '2',
  182. 'field_dt[0][value][day]' => '31',
  183. 'field_dt[0][value][hour]' => '1',
  184. 'field_dt[0][value][minute]' => '30',
  185. );
  186. $this->drupalPostForm('node/add/page_with_date', $edit, t('Save'));
  187. $this->assertText(t('Selected combination of day and month is not valid.'), 'Inorrect date failed validation');
  188. $edit['field_dt[0][value][day]'] = '29';
  189. $this->drupalPostForm('node/add/page_with_date', $edit, t('Save'));
  190. $this->assertNoText(t('Selected combination of day and month is not valid.'), 'Correct date passed validation.');
  191. $this->drupalGet('node/1');
  192. $this->assertText(t('Mon, 02/29/2016 - 01:30'), 'Node successfully created with valid date.');
  193. }
  194. }