date_field.test 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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', 'administer fields')
  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_handling) ? $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. * Creates a date field from an array of settings values.
  140. *
  141. * All values have defaults, only need to specify values that need to be
  142. * different.
  143. */
  144. protected function createMultiDateField($values = array()) {
  145. extract($values);
  146. $field_name = !empty($field_name) ? $field_name : 'field_test';
  147. $entity_type = !empty($entity_type) ? $entity_type : 'node';
  148. $bundle = !empty($bundle) ? $bundle : 'story';
  149. $label = !empty($label) ? $label : 'Test';
  150. $field_type = !empty($field_type) ? $field_type : 'datetime';
  151. $repeat = !empty($repeat) ? $repeat : 0;
  152. $todate = !empty($todate) ? $todate : 'optional';
  153. $widget_type = !empty($widget_type) ? $widget_type : 'date_select';
  154. $this->verbose(!empty($tz_handling));
  155. $tz_handling = !empty($tz_handling) ? $tz_handling : 'site';
  156. $granularity = !empty($granularity) ? $granularity : array('year', 'month', 'day', 'hour', 'minute');
  157. $year_range = !empty($year_range) ? $year_range : '2010:+1';
  158. $input_format = !empty($input_format) ? $input_format : date_default_format($widget_type);
  159. $input_format_custom = !empty($input_format_custom) ? $input_format_custom : '';
  160. $text_parts = !empty($text_parts) ? $text_parts : array();
  161. $increment = !empty($increment) ? $increment : 15;
  162. $default_value = !empty($default_value) ? $default_value : 'now';
  163. $default_value2 = !empty($default_value2) ? $default_value2 : 'blank';
  164. $default_format = !empty($default_format) ? $default_format : 'long';
  165. $cache_enabled = !empty($cache_enabled);
  166. $cache_count = !empty($cache_count) ? $cache_count : 4;
  167. $cardinality = !empty($cardinality) ? $cardinality : 1;
  168. $field = array(
  169. 'field_name' => $field_name,
  170. 'type' => $field_type,
  171. 'cardinality' => $cardinality,
  172. 'settings' => array(
  173. 'granularity' => $granularity,
  174. 'tz_handling' => $tz_handling,
  175. 'timezone_db' => date_get_timezone_db($tz_handling),
  176. 'repeat' => $repeat,
  177. 'todate' => $todate,
  178. 'cache_enabled' => $cache_enabled,
  179. 'cache_count' => $cache_count,
  180. ),
  181. );
  182. $instance = array(
  183. 'entity_type' => $entity_type,
  184. 'field_name' => $field_name,
  185. 'label' => $label,
  186. 'bundle' => $bundle,
  187. // Move the date right below the title.
  188. 'weight' => -4,
  189. 'widget' => array(
  190. 'type' => $widget_type,
  191. // Increment for minutes and seconds, can be 1, 5, 10, 15, or 30.
  192. 'settings' => array(
  193. 'increment' => $increment,
  194. // The number of years to go back and forward in drop-down year
  195. // selectors.
  196. 'year_range' => $year_range,
  197. 'input_format' => $input_format,
  198. 'input_format_custom' => $input_format_custom,
  199. 'text_parts' => $text_parts,
  200. 'label_position' => 'above',
  201. 'repeat_collapsed' => 0,
  202. ),
  203. 'weight' => -4,
  204. ),
  205. 'settings' => array(
  206. 'default_value' => $default_value,
  207. 'default_value2' => $default_value2,
  208. ),
  209. );
  210. $instance['display'] = array(
  211. 'default' => array(
  212. 'label' => 'above',
  213. 'type' => 'date_default',
  214. 'settings' => array(
  215. 'format_type' => $default_format,
  216. 'show_repeat_rule' => 'show',
  217. 'multiple_number' => '',
  218. 'multiple_from' => '',
  219. 'multiple_to' => '',
  220. 'fromto' => 'both',
  221. ),
  222. 'module' => 'date',
  223. 'weight' => 0 ,
  224. ),
  225. 'teaser' => array(
  226. 'label' => 'above',
  227. 'type' => 'date_default',
  228. 'weight' => 0,
  229. 'settings' => array(
  230. 'format_type' => $default_format,
  231. 'show_repeat_rule' => 'show',
  232. 'multiple_number' => '',
  233. 'multiple_from' => '',
  234. 'multiple_to' => '',
  235. 'fromto' => 'both',
  236. ),
  237. 'module' => 'date',
  238. ),
  239. );
  240. $field = field_create_field($field);
  241. $instance = field_create_instance($instance);
  242. field_info_cache_clear(TRUE);
  243. field_cache_clear(TRUE);
  244. // Look at how the field got configured.
  245. $this->drupalGet("admin/structure/types/manage/$bundle/fields/$field_name");
  246. $this->drupalGet("admin/structure/types/manage/$bundle/display");
  247. }
  248. /**
  249. * @todo.
  250. */
  251. protected function deleteDateField($label, $bundle = 'story', $bundle_name = 'Story') {
  252. $this->drupalGet("admin/structure/types/manage/$bundle/fields");
  253. $this->clickLink('delete');
  254. $this->drupalPost(NULL, NULL, t('Delete'));
  255. $this->assertText("The field $label has been deleted from the $bundle_name content type.", 'Removed date field.');
  256. }
  257. }
  258. class DateFieldTestCase extends DateFieldBasic {
  259. /**
  260. * @todo.
  261. */
  262. public static function getInfo() {
  263. return array(
  264. 'name' => 'Date Field',
  265. 'description' => 'Test date field settings and Start/End date interaction.',
  266. 'group' => 'Date',
  267. );
  268. }
  269. /**
  270. * @todo.
  271. */
  272. public function testField() {
  273. // Create a date fields with simple values.
  274. foreach (array('date', 'datestamp', 'datetime') as $field_type) {
  275. foreach (array('date_select', 'date_popup', 'date_text') as $widget_type) {
  276. $field_name = "field_test_$widget_type";
  277. $label = 'Test';
  278. $options = array(
  279. 'label' => $label,
  280. 'widget_type' => $widget_type,
  281. 'field_name' => $field_name,
  282. 'field_type' => $field_type,
  283. 'input_format' => 'm/d/Y - H:i',
  284. );
  285. $this->createDateField($options);
  286. $this->dateForm($field_name, $field_type, $widget_type);
  287. $this->deleteDateField($label);
  288. }
  289. }
  290. }
  291. /**
  292. * @todo.
  293. */
  294. public function dateForm($field_name, $field_type, $widget_type, $todate = TRUE) {
  295. // Tests that date field functions properly.
  296. $edit = array();
  297. $edit['title'] = $this->randomName(8);
  298. if ($widget_type == 'date_select') {
  299. $edit[$field_name . '[und][0][value][year]'] = '2010';
  300. $edit[$field_name . '[und][0][value][month]'] = '10';
  301. $edit[$field_name . '[und][0][value][day]'] = '7';
  302. $edit[$field_name . '[und][0][value][hour]'] = '10';
  303. $edit[$field_name . '[und][0][value][minute]'] = '30';
  304. if ($todate) {
  305. $edit[$field_name . '[und][0][show_todate]'] = '1';
  306. $edit[$field_name . '[und][0][value2][year]'] = '2010';
  307. $edit[$field_name . '[und][0][value2][month]'] = '10';
  308. $edit[$field_name . '[und][0][value2][day]'] = '7';
  309. $edit[$field_name . '[und][0][value2][hour]'] = '11';
  310. $edit[$field_name . '[und][0][value2][minute]'] = '30';
  311. }
  312. }
  313. elseif ($widget_type == 'date_text') {
  314. $edit[$field_name . '[und][0][value][date]'] = '10/07/2010 - 10:30';
  315. if ($todate) {
  316. $edit[$field_name . '[und][0][show_todate]'] = '1';
  317. $edit[$field_name . '[und][0][value2][date]'] = '10/07/2010 - 11:30';
  318. }
  319. }
  320. elseif ($widget_type == 'date_popup') {
  321. $edit[$field_name . '[und][0][value][date]'] = '10/07/2010';
  322. $edit[$field_name . '[und][0][value][time]'] = '10:30';
  323. if ($todate) {
  324. $edit[$field_name . '[und][0][show_todate]'] = '1';
  325. $edit[$field_name . '[und][0][value2][date]'] = '10/07/2010';
  326. $edit[$field_name . '[und][0][value2][time]'] = '11:30';
  327. }
  328. }
  329. // Test that the date is displayed correctly using both the 'short' and
  330. // 'long' date types.
  331. //
  332. // For the short type, save an explicit format and assert that is the one
  333. // which is displayed.
  334. variable_set('date_format_short', 'l, m/d/Y - H:i:s');
  335. $instance = field_info_instance('node', $field_name, 'story');
  336. $instance['display']['default']['settings']['format_type'] = 'short';
  337. field_update_instance($instance);
  338. $this->drupalPost('node/add/story', $edit, t('Save'));
  339. $this->assertText($edit['title'], "Node has been created");
  340. $should_be = $todate ? 'Thursday, 10/07/2010 - 10:30 to 11:30' : 'Thursday, 10/07/2010 - 10:30';
  341. $this->assertText($should_be, "Found the correct date for a $field_type field using the $widget_type widget displayed using the short date format.");
  342. // For the long format, do not save anything, and assert that the displayed
  343. // date uses the expected default value of this format provided by Drupal
  344. // core ('l, F j, Y - H:i').
  345. $instance = field_info_instance('node', $field_name, 'story');
  346. $instance['display']['default']['settings']['format_type'] = 'long';
  347. field_update_instance($instance);
  348. $this->drupalPost('node/add/story', $edit, t('Save'));
  349. $this->assertText($edit['title'], "Node has been created");
  350. $should_be = $todate ? 'Thursday, October 7, 2010 - 10:30 to 11:30' : 'Thursday, October 7, 2010 - 10:30';
  351. $this->assertText($should_be, "Found the correct date for a $field_type field using the $widget_type widget displayed using the long date format.");
  352. }
  353. }