date_timezone.test 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * @file
  4. * Timezone tests.
  5. */
  6. class DateTimezoneTestCase extends DateFieldBasic {
  7. /**
  8. * @todo.
  9. */
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Timezone & Granularity',
  13. 'description' => 'Test combinations of date field timezone handling and granularity.',
  14. 'group' => 'Date',
  15. );
  16. }
  17. public function setUp() {
  18. parent::setUp();
  19. // Set the timezone explicitly. Otherwise the site's default timezone is
  20. // used, which defaults to the server timezone when installing Drupal. This
  21. // depends on the environment and is therefore uncertain.
  22. // The Australia/Sydney timezone is chosen so all tests are run using an
  23. // edge case scenario (UTC+10 and DST).
  24. variable_set('date_default_timezone', 'Australia/Sydney');
  25. }
  26. /**
  27. * @todo.
  28. */
  29. public function testTimezone() {
  30. // Create a date fields with combinations of various timezone handling and
  31. // granularity.
  32. foreach (array('date', 'datestamp', 'datetime') as $field_type) {
  33. foreach (array('site', 'none', 'date', 'user', 'utc', 'Europe/Dublin') as $tz_handling) {
  34. foreach (array('year', 'month', 'day', 'hour', 'minute', 'second') as $max_granularity) {
  35. // Skip invalid combinations.
  36. if (in_array($max_granularity, array('year', 'month', 'day')) && $tz_handling != 'none') {
  37. continue;
  38. }
  39. $field_name = "field_test";
  40. $label = 'Test';
  41. $granularity = date_granularity_array_from_precision($max_granularity);
  42. $options = array(
  43. 'label' => $label,
  44. 'widget_type' => 'date_text',
  45. 'field_name' => $field_name,
  46. 'field_type' => $field_type,
  47. 'input_format' => 'custom',
  48. 'input_format_custom' => 'm/d/Y - H:i:s',
  49. 'tz_handling' => $tz_handling,
  50. 'granularity' => $granularity,
  51. );
  52. $this->createDateField($options);
  53. $this->dateForm($field_name, $field_type, $max_granularity, $tz_handling);
  54. $this->deleteDateField($label);
  55. }
  56. }
  57. }
  58. }
  59. /**
  60. * Validates timezone handling with a multi-value date field.
  61. */
  62. public function testMultiUserTimezone() {
  63. // Create date fields with combinations of various types and granularity
  64. // using the "Date's Timezone" strategy.
  65. $field_type = 'datetime';
  66. $tz_handling = 'date';
  67. $max_granularity = 'minute';
  68. // Create date field
  69. $field_name = "field_test";
  70. $label = 'Test';
  71. $options = array(
  72. 'label' => $label,
  73. 'widget_type' => 'date_text',
  74. 'field_name' => $field_name,
  75. 'field_type' => $field_type,
  76. 'input_format' => 'custom',
  77. 'input_format_custom' => 'm/d/Y - H:i:s T',
  78. 'cardinality' => 3,
  79. 'tz_handling' => $tz_handling,
  80. );
  81. $this->createMultiDateField($options);
  82. // Submit a date field form with multiple values
  83. $this->dateMultiValueForm($field_name, $field_type, $max_granularity, $tz_handling);
  84. $this->deleteDateField($label);
  85. }
  86. /**
  87. * Tests the submission of a date field's widget form when using unlimited
  88. * cardinality
  89. */
  90. public function dateMultiValueForm($field_name, $field_type, $max_granularity, $tz_handling) {
  91. variable_set('date_format_long', 'D, m/d/Y - H:i:s T');
  92. $edit = array();
  93. $should_be = array();
  94. $edit['title'] = $this->randomName(8);
  95. $timezones = array('America/Chicago', 'America/Los_Angeles', 'America/New_York');
  96. switch ($max_granularity) {
  97. case 'hour':
  98. $edit[$field_name . '[und][0][value][date]'] = '10/07/2010 - 10:30';
  99. $edit[$field_name . '[und][0][timezone][timezone]'] = 'America/Chicago';
  100. $should_be[0] = 'Thu, 10/07/2010 - 10 CDT';
  101. $edit[$field_name . '[und][1][value][date]'] = '10/07/2010 - 10:30';
  102. $edit[$field_name . '[und][1][timezone][timezone]'] = 'America/Los_Angeles';
  103. $should_be[1] = 'Thu, 10/07/2010 - 10 PDT';
  104. $edit[$field_name . '[und][2][value][date]'] = '10/07/2010 - 10:30';
  105. $edit[$field_name . '[und][2][timezone][timezone]'] = 'America/New_York';
  106. $should_be[2] = 'Thu, 10/07/2010 - 10 EDT';
  107. break;
  108. case 'minute':
  109. $edit[$field_name . '[und][0][value][date]'] = '10/07/2010 - 10:30';
  110. $edit[$field_name . '[und][0][timezone][timezone]'] = 'America/Chicago';
  111. $should_be[0] = 'Thu, 10/07/2010 - 10:30 CDT';
  112. $edit[$field_name . '[und][1][value][date]'] = '10/07/2010 - 10:30';
  113. $edit[$field_name . '[und][1][timezone][timezone]'] = 'America/Los_Angeles';
  114. $should_be[1] = 'Thu, 10/07/2010 - 10:30 PDT';
  115. $edit[$field_name . '[und][2][value][date]'] = '10/07/2010 - 10:30';
  116. $edit[$field_name . '[und][2][timezone][timezone]'] = 'America/New_York';
  117. $should_be[2] = 'Thu, 10/07/2010 - 10:30 EDT';
  118. break;
  119. case 'second':
  120. $edit[$field_name . '[und][0][value][date]'] = '10/07/2010 - 10:30';
  121. $edit[$field_name . '[und][0][timezone][timezone]'] = 'America/Chicago';
  122. $should_be[0] = 'Thu, 10/07/2010 - 10:30:30 CDT';
  123. $edit[$field_name . '[und][1][value][date]'] = '10/07/2010 - 10:30';
  124. $edit[$field_name . '[und][1][timezone][timezone]'] = 'America/Los_Angeles';
  125. $should_be[1] = 'Thu, 10/07/2010 - 10:30:30 PDT';
  126. $edit[$field_name . '[und][2][value][date]'] = '10/07/2010 - 10:30';
  127. $edit[$field_name . '[und][2][timezone][timezone]'] = 'America/New_York';
  128. $should_be[2] = 'Thu, 10/07/2010 - 10:30:30 EDT';
  129. break;
  130. }
  131. $this->drupalPost('node/add/story', $edit, t('Save'));
  132. $this->assertText($edit['title'], "Node has been created");
  133. foreach ($should_be as $assertion) {
  134. $this->assertText($assertion, "Found the correct date for a $field_type field using $max_granularity granularity with $tz_handling timezone handling.");
  135. }
  136. // Goto the edit page and save the node again.
  137. $node = $this->drupalGetNodeByTitle($edit['title']);
  138. $this->drupalGet('node/' . $node->nid . '/edit');
  139. // Re-assert the proper date timezones.
  140. foreach ($timezones as $key => $timezone) {
  141. $this->assertOptionSelected('edit-field-test-und-' . $key . '-timezone-timezone', $timezone, "Found the correct timezone $timezone for a $field_type field using $max_granularity granularity with $tz_handling timezone handling.");
  142. }
  143. }
  144. /**
  145. * @todo.
  146. */
  147. public function dateForm($field_name, $field_type, $max_granularity, $tz_handling) {
  148. variable_set('date_format_long', 'D, m/d/Y - H:i:s');
  149. $edit = array();
  150. $edit['title'] = $this->randomName(8);
  151. $edit[$field_name . '[und][0][show_todate]'] = '1';
  152. switch ($max_granularity) {
  153. case 'year':
  154. $edit[$field_name . '[und][0][value][date]'] = '2010';
  155. $edit[$field_name . '[und][0][value2][date]'] = '2011';
  156. $should_be = '2010 to 2011';
  157. break;
  158. case 'month':
  159. $edit[$field_name . '[und][0][value][date]'] = '07/2010';
  160. $edit[$field_name . '[und][0][value2][date]'] = '08/2010';
  161. $should_be = '07/2010 to 08/2010';
  162. break;
  163. case 'day':
  164. $edit[$field_name . '[und][0][value][date]'] = '10/07/2010';
  165. $edit[$field_name . '[und][0][value2][date]'] = '10/08/2010';
  166. $should_be = 'Thu, 10/07/2010 to Fri, 10/08/2010';
  167. break;
  168. case 'hour':
  169. $edit[$field_name . '[und][0][value][date]'] = '10/07/2010 - 10';
  170. $edit[$field_name . '[und][0][value2][date]'] = '10/07/2010 - 11';
  171. if ($tz_handling == 'utc') {
  172. $should_be = 'Thu, 10/07/2010 - 21 to Thu, 10/07/2010 - 22';
  173. }
  174. else {
  175. $should_be = 'Thu, 10/07/2010 - 10 to Thu, 10/07/2010 - 11';
  176. }
  177. break;
  178. case 'minute':
  179. $edit[$field_name . '[und][0][value][date]'] = '10/07/2010 - 10:30';
  180. $edit[$field_name . '[und][0][value2][date]'] = '10/07/2010 - 11:30';
  181. if ($tz_handling == 'utc') {
  182. $should_be = 'Thu, 10/07/2010 - 21:30 to 22:30';
  183. }
  184. else {
  185. $should_be = 'Thu, 10/07/2010 - 10:30 to 11:30';
  186. }
  187. break;
  188. case 'second':
  189. $edit[$field_name . '[und][0][value][date]'] = '10/07/2010 - 10:30:30';
  190. $edit[$field_name . '[und][0][value2][date]'] = '10/07/2010 - 11:30:30';
  191. if ($tz_handling == 'utc') {
  192. $should_be = 'Thu, 10/07/2010 - 21:30:30 to 22:30:30';
  193. }
  194. else {
  195. $should_be = 'Thu, 10/07/2010 - 10:30:30 to 11:30:30';
  196. }
  197. break;
  198. }
  199. $this->drupalPost('node/add/story', $edit, t('Save'));
  200. $this->assertText($edit['title'], "Node has been created");
  201. $this->assertText($should_be, "Found the correct date for a $field_type field using $max_granularity granularity with $tz_handling timezone handling.");
  202. }
  203. }