date_validation.test 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * @file
  4. * Date validation tests.
  5. */
  6. class DateValidationTestCase extends DateFieldBasic {
  7. /**
  8. * @todo.
  9. */
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Date Validation',
  13. 'description' => 'Test date validation.',
  14. 'group' => 'Date',
  15. );
  16. }
  17. /**
  18. * @todo.
  19. */
  20. public function testValidation() {
  21. // Attempts to create text date field stored as a date with default settings
  22. // (from input which is not valid).
  23. foreach (array('date', 'datestamp', 'datetime') as $field_type) {
  24. foreach (array('date_select', 'date_popup', 'date_text') as $widget_type) {
  25. $field_name = 'field_test';
  26. $label = 'Test';
  27. $options = array(
  28. 'label' => $label,
  29. 'field_name' => $field_name,
  30. 'field_type' => $field_type,
  31. 'widget_type' => $widget_type,
  32. 'input_format' => 'm/d/Y - H:i',
  33. );
  34. $this->createDateField($options);
  35. // Malformed date test won't work on date_select, which won't allow
  36. // invalid input.
  37. if ($widget_type != 'date_select') {
  38. $this->malFormedDate($field_name, $field_type, $widget_type);
  39. }
  40. $this->wrongGranularity($field_name, $field_type, $widget_type);
  41. $this->deleteDateField($label);
  42. }
  43. }
  44. }
  45. /**
  46. * @todo.
  47. */
  48. function malFormedDate($field_name, $field_type, $widget_type) {
  49. // Tests that date field filters improper dates.
  50. $edit = array();
  51. $edit['title'] = $this->randomName(8);
  52. $edit['body[und][0][value]'] = $this->randomName(16);
  53. if ($widget_type == 'date_select') {
  54. $edit[$field_name . '[und][0][value][year]'] = '2011';
  55. $edit[$field_name . '[und][0][value][month]'] = '15';
  56. $edit[$field_name . '[und][0][value][day]'] = '49';
  57. $edit[$field_name . '[und][0][value][hour]'] = '10';
  58. $edit[$field_name . '[und][0][value][minute]'] = '30';
  59. }
  60. elseif ($widget_type == 'date_text') {
  61. $edit[$field_name . '[und][0][value][date]'] = '15/49/2011 - 10:30';
  62. }
  63. elseif ($widget_type == 'date_popup') {
  64. $edit[$field_name . '[und][0][value][date]'] = '15/49/2011';
  65. $edit[$field_name . '[und][0][value][time]'] = '10:30';
  66. }
  67. $this->drupalPost('node/add/story', $edit, t('Save'));
  68. $should_not_be = $edit['title'] . "has been created";
  69. $this->assertNoText($should_not_be, "Correctly blocked creation of node with invalid month and day for a $field_type field using the $widget_type widget.");
  70. $this->assertText('The month is invalid.', "Correctly blocked invalid month for a $field_type field using the $widget_type widget.");
  71. $this->assertText('The day is invalid.', "Correctly blocked invalid day for a $field_type field using the $widget_type widget.");
  72. // Test two-digit entry for year where 4-digit is expected.
  73. if ($widget_type == 'date_select') {
  74. $edit[$field_name . '[und][0][value][year]'] = '11';
  75. $edit[$field_name . '[und][0][value][month]'] = '12';
  76. $edit[$field_name . '[und][0][value][day]'] = '10';
  77. $edit[$field_name . '[und][0][value][hour]'] = '10';
  78. $edit[$field_name . '[und][0][value][minute]'] = '30';
  79. }
  80. elseif ($widget_type == 'date_text') {
  81. $edit[$field_name . '[und][0][value][date]'] = '12/10/11 - 10:30';
  82. }
  83. elseif ($widget_type == 'date_popup') {
  84. $edit[$field_name . '[und][0][value][date]'] = '12/10/11';
  85. $edit[$field_name . '[und][0][value][time]'] = '10:30';
  86. }
  87. $this->drupalPost('node/add/story', $edit, t('Save'));
  88. $should_not_be = $edit['title'] . " has been created";
  89. $this->assertNoText($should_not_be, "Correctly blocked creation of node with invalid year for a $field_type field using the $widget_type widget.");
  90. $should_be = 'The year is invalid. Please check that entry includes four digits.';
  91. $this->assertText($should_be, "Correctly blocked two digit year for a $field_type field using the $widget_type widget.");
  92. // Test invalid hour/minute entry for time.
  93. if ($widget_type == 'date_select') {
  94. $edit[$field_name . '[und][0][value][year]'] = '2011';
  95. $edit[$field_name . '[und][0][value][month]'] = '12';
  96. $edit[$field_name . '[und][0][value][day]'] = '10';
  97. $edit[$field_name . '[und][0][value][hour]'] = '29';
  98. $edit[$field_name . '[und][0][value][minute]'] = '95';
  99. }
  100. elseif ($widget_type == 'date_text') {
  101. $edit[$field_name . '[und][0][value][date]'] = '12/10/2011 - 29:95';
  102. }
  103. elseif ($widget_type == 'date_popup') {
  104. $edit[$field_name . '[und][0][value][date]'] = '12/10/2011';
  105. $edit[$field_name . '[und][0][value][time]'] = '29:95';
  106. }
  107. $this->drupalPost('node/add/story', $edit, t('Save'));
  108. $should_not_be = $edit['title'] . " has been created";
  109. $this->assertNoText($should_not_be, "Correctly blocked creation of node with invalid time for a $field_type field using the $widget_type widget.");
  110. $should_be = 'The hour is invalid.';
  111. $this->assertText($should_be, "Correctly blocked invalid hour for a $field_type field using the $widget_type widget.");
  112. $should_be = 'The minute is invalid.';
  113. $this->assertText($should_be, "Correctly blocked invalid minute for a $field_type field using the $widget_type widget.");
  114. }
  115. /**
  116. * @todo.
  117. */
  118. public function wrongGranularity($field_name, $field_type, $widget_type) {
  119. // Create a node with incorrect granularity -- missing time.
  120. $edit = array();
  121. $edit['title'] = $this->randomName(8);
  122. $edit['body[und][0][value]'] = $this->randomName(16);
  123. if ($widget_type == 'date_select') {
  124. $edit[$field_name . '[und][0][value][year]'] = '2011';
  125. $edit[$field_name . '[und][0][value][month]'] = '12';
  126. $edit[$field_name . '[und][0][value][day]'] = '10';
  127. $edit[$field_name . '[und][0][value][hour]'] = '';
  128. $edit[$field_name . '[und][0][value][minute]'] = '';
  129. }
  130. elseif ($widget_type == 'date_text') {
  131. $edit[$field_name . '[und][0][value][date]'] = '12/10/2011';
  132. }
  133. elseif ($widget_type == 'date_popup') {
  134. $edit[$field_name . '[und][0][value][date]'] = '12/10/2011';
  135. $edit[$field_name . '[und][0][value][time]'] = '';
  136. }
  137. $this->drupalPost('node/add/story', $edit, t('Save'));
  138. $should_not_be = $edit['title'] . " has been created";
  139. $this->assertNoText($should_not_be, "Correctly blocked creation of node with missing time for a $field_type field using the $widget_type widget.");
  140. $this->assertText('invalid', "Marked form with missing time as invalid for a $field_type field using the $widget_type widget.");
  141. }
  142. }