date_field.test 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /**
  3. * @file
  4. * Basic functions for Date tests.
  5. */
  6. abstract class DateFieldBasic extends DrupalWebTestCase {
  7. protected $privileged_user;
  8. /**
  9. * @todo.
  10. */
  11. protected function setUp() {
  12. // Load the date_api module.
  13. parent::setUp('field', 'field_ui', 'date_api', 'date', 'date_popup', 'date_tools');
  14. // Create and log in our privileged user.
  15. $this->privileged_user = $this->drupalCreateUser(
  16. array('administer content types', 'administer nodes', 'bypass node access', 'administer date tools')
  17. );
  18. $this->drupalLogin($this->privileged_user);
  19. variable_set('date_popup_timepicker', 'none');
  20. module_load_include('inc', 'node', 'content_types');
  21. module_load_include('inc', 'node', 'node.pages');
  22. module_load_include('inc', 'field', 'field.crud');
  23. module_load_include('inc', 'date', 'date_admin');
  24. $edit = array();
  25. $edit['name'] = 'Story';
  26. $edit['type'] = 'story';
  27. $this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
  28. $this->assertText('The content type Story has been added.', 'Content type added.');
  29. }
  30. /**
  31. * Creates a date field from an array of settings values.
  32. *
  33. * All values have defaults, only need to specify values that need to be
  34. * different.
  35. */
  36. protected function createDateField($values = array()) {
  37. extract($values);
  38. $field_name = !empty($field_name) ? $field_name : 'field_test';
  39. $entity_type = !empty($entity_type) ? $entity_type : 'node';
  40. $bundle = !empty($bundle) ? $bundle : 'story';
  41. $label = !empty($label) ? $label : 'Test';
  42. $field_type = !empty($field_type) ? $field_type : 'datetime';
  43. $repeat = !empty($repeat) ? $repeat : 0;
  44. $todate = !empty($todate) ? $todate : 'optional';
  45. $widget_type = !empty($widget_type) ? $widget_type : 'date_select';
  46. $tz_handling = !empty($tz_handing) ? $tz_handling : 'site';
  47. $granularity = !empty($granularity) ? $granularity : array('year', 'month', 'day', 'hour', 'minute');
  48. $year_range = !empty($year_range) ? $year_range : '2010:+1';
  49. $input_format = !empty($input_format) ? $input_format : date_default_format($widget_type);
  50. $input_format_custom = !empty($input_format_custom) ? $input_format_custom : '';
  51. $text_parts = !empty($text_parts) ? $text_parts : array();
  52. $increment = !empty($increment) ? $increment : 15;
  53. $default_value = !empty($default_value) ? $default_value : 'now';
  54. $default_value2 = !empty($default_value2) ? $default_value2 : 'blank';
  55. $default_format = !empty($default_format) ? $default_format : 'long';
  56. $cache_enabled = !empty($cache_enabled);
  57. $cache_count = !empty($cache_count) ? $cache_count : 4;
  58. $field = array(
  59. 'field_name' => $field_name,
  60. 'type' => $field_type,
  61. 'cardinality' => !empty($repeat) ? FIELD_CARDINALITY_UNLIMITED : 1,
  62. 'settings' => array(
  63. 'granularity' => $granularity,
  64. 'tz_handling' => $tz_handling,
  65. 'timezone_db' => date_get_timezone_db($tz_handling),
  66. 'repeat' => $repeat,
  67. 'todate' => $todate,
  68. 'cache_enabled' => $cache_enabled,
  69. 'cache_count' => $cache_count,
  70. ),
  71. );
  72. $instance = array(
  73. 'entity_type' => $entity_type,
  74. 'field_name' => $field_name,
  75. 'label' => $label,
  76. 'bundle' => $bundle,
  77. // Move the date right below the title.
  78. 'weight' => -4,
  79. 'widget' => array(
  80. 'type' => $widget_type,
  81. // Increment for minutes and seconds, can be 1, 5, 10, 15, or 30.
  82. 'settings' => array(
  83. 'increment' => $increment,
  84. // The number of years to go back and forward in drop-down year
  85. // selectors.
  86. 'year_range' => $year_range,
  87. 'input_format' => $input_format,
  88. 'input_format_custom' => $input_format_custom,
  89. 'text_parts' => $text_parts,
  90. 'label_position' => 'above',
  91. 'repeat_collapsed' => 0,
  92. ),
  93. 'weight' => -4,
  94. ),
  95. 'settings' => array(
  96. 'default_value' => $default_value,
  97. 'default_value2' => $default_value2,
  98. ),
  99. );
  100. $instance['display'] = array(
  101. 'default' => array(
  102. 'label' => 'above',
  103. 'type' => 'date_default',
  104. 'settings' => array(
  105. 'format_type' => $default_format,
  106. 'show_repeat_rule' => 'show',
  107. 'multiple_number' => '',
  108. 'multiple_from' => '',
  109. 'multiple_to' => '',
  110. 'fromto' => 'both',
  111. ),
  112. 'module' => 'date',
  113. 'weight' => 0 ,
  114. ),
  115. 'teaser' => array(
  116. 'label' => 'above',
  117. 'type' => 'date_default',
  118. 'weight' => 0,
  119. 'settings' => array(
  120. 'format_type' => $default_format,
  121. 'show_repeat_rule' => 'show',
  122. 'multiple_number' => '',
  123. 'multiple_from' => '',
  124. 'multiple_to' => '',
  125. 'fromto' => 'both',
  126. ),
  127. 'module' => 'date',
  128. ),
  129. );
  130. $field = field_create_field($field);
  131. $instance = field_create_instance($instance);
  132. field_info_cache_clear(TRUE);
  133. field_cache_clear(TRUE);
  134. // Look at how the field got configured.
  135. $this->drupalGet("admin/structure/types/manage/$bundle/fields/$field_name");
  136. $this->drupalGet("admin/structure/types/manage/$bundle/display");
  137. }
  138. /**
  139. * @todo.
  140. */
  141. protected function deleteDateField($label, $bundle = 'story', $bundle_name = 'Story') {
  142. $this->drupalGet("admin/structure/types/manage/$bundle/fields");
  143. $this->clickLink('delete');
  144. $this->drupalPost(NULL, NULL, t('Delete'));
  145. $this->assertText("The field $label has been deleted from the $bundle_name content type.", 'Removed date field.');
  146. }
  147. }
  148. class DateFieldTestCase extends DateFieldBasic {
  149. /**
  150. * @todo.
  151. */
  152. public static function getInfo() {
  153. return array(
  154. 'name' => 'Date Field',
  155. 'description' => 'Test date field settings and Start/End date interaction.',
  156. 'group' => 'Date',
  157. );
  158. }
  159. /**
  160. * @todo.
  161. */
  162. public function testField() {
  163. // Create a date fields with simple values.
  164. foreach (array('date', 'datestamp', 'datetime') as $field_type) {
  165. foreach (array('date_select', 'date_popup', 'date_text') as $widget_type) {
  166. $field_name = "field_test_$widget_type";
  167. $label = 'Test';
  168. $options = array(
  169. 'label' => $label,
  170. 'widget_type' => $widget_type,
  171. 'field_name' => $field_name,
  172. 'field_type' => $field_type,
  173. 'input_format' => 'm/d/Y - H:i',
  174. );
  175. $this->createDateField($options);
  176. $this->dateForm($field_name, $field_type, $widget_type);
  177. $this->deleteDateField($label);
  178. }
  179. }
  180. }
  181. /**
  182. * @todo.
  183. */
  184. public function dateForm($field_name, $field_type, $widget_type, $todate = TRUE) {
  185. // Tests that date field functions properly.
  186. $edit = array();
  187. $edit['title'] = $this->randomName(8);
  188. if ($widget_type == 'date_select') {
  189. $edit[$field_name . '[und][0][value][year]'] = '2010';
  190. $edit[$field_name . '[und][0][value][month]'] = '10';
  191. $edit[$field_name . '[und][0][value][day]'] = '7';
  192. $edit[$field_name . '[und][0][value][hour]'] = '10';
  193. $edit[$field_name . '[und][0][value][minute]'] = '30';
  194. if ($todate) {
  195. $edit[$field_name . '[und][0][show_todate]'] = '1';
  196. $edit[$field_name . '[und][0][value2][year]'] = '2010';
  197. $edit[$field_name . '[und][0][value2][month]'] = '10';
  198. $edit[$field_name . '[und][0][value2][day]'] = '7';
  199. $edit[$field_name . '[und][0][value2][hour]'] = '11';
  200. $edit[$field_name . '[und][0][value2][minute]'] = '30';
  201. }
  202. }
  203. elseif ($widget_type == 'date_text') {
  204. $edit[$field_name . '[und][0][value][date]'] = '10/07/2010 - 10:30';
  205. if ($todate) {
  206. $edit[$field_name . '[und][0][show_todate]'] = '1';
  207. $edit[$field_name . '[und][0][value2][date]'] = '10/07/2010 - 11:30';
  208. }
  209. }
  210. elseif ($widget_type == 'date_popup') {
  211. $edit[$field_name . '[und][0][value][date]'] = '10/07/2010';
  212. $edit[$field_name . '[und][0][value][time]'] = '10:30';
  213. if ($todate) {
  214. $edit[$field_name . '[und][0][show_todate]'] = '1';
  215. $edit[$field_name . '[und][0][value2][date]'] = '10/07/2010';
  216. $edit[$field_name . '[und][0][value2][time]'] = '11:30';
  217. }
  218. }
  219. // Test that the date is displayed correctly using both the 'short' and
  220. // 'long' date types.
  221. //
  222. // For the short type, save an explicit format and assert that is the one
  223. // which is displayed.
  224. variable_set('date_format_short', 'l, m/d/Y - H:i:s');
  225. $instance = field_info_instance('node', $field_name, 'story');
  226. $instance['display']['default']['settings']['format_type'] = 'short';
  227. field_update_instance($instance);
  228. $this->drupalPost('node/add/story', $edit, t('Save'));
  229. $this->assertText($edit['title'], "Node has been created");
  230. $should_be = $todate ? 'Thursday, 10/07/2010 - 10:30 to 11:30' : 'Thursday, 10/07/2010 - 10:30';
  231. $this->assertText($should_be, "Found the correct date for a $field_type field using the $widget_type widget displayed using the short date format.");
  232. // For the long format, do not save anything, and assert that the displayed
  233. // date uses the expected default value of this format provided by Drupal
  234. // core ('l, F j, Y - H:i').
  235. $instance = field_info_instance('node', $field_name, 'story');
  236. $instance['display']['default']['settings']['format_type'] = 'long';
  237. field_update_instance($instance);
  238. $this->drupalPost('node/add/story', $edit, t('Save'));
  239. $this->assertText($edit['title'], "Node has been created");
  240. $should_be = $todate ? 'Thursday, October 7, 2010 - 10:30 to 11:30' : 'Thursday, October 7, 2010 - 10:30';
  241. $this->assertText($should_be, "Found the correct date for a $field_type field using the $widget_type widget displayed using the long date format.");
  242. }
  243. }