date_popup_authored_format.test 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * @file
  4. * Simpletest case for Date Popup Authored.
  5. *
  6. * Verify Date Popup Authored functionality.
  7. */
  8. /**
  9. * Functionality tests for Date Popup Authored.
  10. */
  11. class DatePopupAuthoredTestCase extends PageEditTestCase {
  12. public static function getInfo() {
  13. return array(
  14. 'name' => 'Page editing with Date Popup Authored',
  15. 'description' => 'Verify Date Popup Authored does not interfere with page editing.',
  16. 'group' => 'Date Popup Authored',
  17. );
  18. }
  19. function setUp() {
  20. // Enable the module.
  21. DrupalWebTestCase::setUp('date_popup_authored');
  22. // Create users for test cases.
  23. $this->web_user = $this->drupalCreateUser(array('edit own page content', 'create page content'));
  24. $this->admin_user = $this->drupalCreateUser(array('bypass node access', 'administer nodes'));
  25. }
  26. /**
  27. * Check changing node authored on fields.
  28. */
  29. function testPageAuthoredOnEdit() {
  30. $this->drupalLogin($this->admin_user);
  31. // Create node to edit.
  32. $langcode = LANGUAGE_NONE;
  33. $body_key = "body[$langcode][0][value]";
  34. $edit = array();
  35. $edit['title'] = $this->randomName(8);
  36. $edit[$body_key] = $this->randomName(16);
  37. $this->drupalPost('node/add/page', $edit, t('Save'));
  38. $node = $this->drupalGetNodeByTitle($edit['title']);
  39. $node->date = format_date($node->created, 'custom', 'Y-m-d H:i:s O');
  40. // Check that the Authored On field's format does not mangle the saved date.
  41. $this->checkAuthoredOnWithFormat('M d, Y', $node);
  42. $this->checkAuthoredOnWithFormat('M, Y', $node);
  43. $this->checkAuthoredOnWithFormat('M d, Y H:i', $node);
  44. $this->checkAuthoredOnWithFormat('m/d/Y - H:i:s', $node);
  45. $this->checkAuthoredOnWithFormat('d M Y - g:i A', $node);
  46. }
  47. /**
  48. * Check to see if Date Popup Authored keeps the Authored On field intact.
  49. *
  50. * Date Popup cleverly handles how to replace regular text fields: if the date
  51. * format has both a date and time component, it creates one textfield for the
  52. * date and one for the time.
  53. *
  54. * Because of this, the node date needs to be split into date and time parts
  55. * so it can be compared with the date popup on the node submission form.
  56. *
  57. * @param $format
  58. * The date format to test.
  59. * @param $node
  60. * The node object to test the date format with.
  61. * @param $timezone
  62. * Optionally, a timezone to test with.
  63. */
  64. function checkAuthoredOnWithFormat($format, $node, $timezone = '') {
  65. debug('Format: ' . $format);
  66. $timezone = !empty($timezone) ? $timezone : date_default_timezone_object();
  67. // Extract the date and time format parts
  68. $granularity = date_format_order($format);
  69. $date_format = date_limit_format($format, array_intersect($granularity, array('month', 'day', 'year')));
  70. $time_format = date_popup_format_to_popup_time(date_limit_format($format, array_intersect($granularity, array('hour', 'minute', 'second'))));
  71. // Generate a DateObject object for the node date.
  72. $node_date = DateObject::createFromFormat('Y-m-d H:i:s O', $node->date, $timezone);
  73. debug($node_date->format($date_format), 'Node date');
  74. debug($node_date->format($time_format), 'Node time');
  75. // Extract the date and time parts as seen on the node submission form
  76. $default_format = variable_get('date_popup_authored_format_page', variable_get('date_format_short', 'm/d/Y - H:i'));
  77. variable_set('date_popup_authored_format_page', $format);
  78. $this->drupalGet('node/' . $node->nid . '/edit');
  79. $elements = $this->xpath("//input[starts-with(@name, 'date[')]");
  80. $submitted_date_string = '';
  81. foreach ($elements as $element) {
  82. if ((string) $element['name'] === 'date[date]') {
  83. debug((string) $element['value'], 'Submitted date');
  84. $this->assertIdentical($node_date->format($date_format), (string) $element['value'], 'The node date and submission form have identical dates.');
  85. }
  86. elseif ((string) $element['name'] === 'date[time]') {
  87. debug((string) $element['value'], 'Submitted time');
  88. $this->assertIdentical($node_date->format($time_format), (string) $element['value'], 'The node date and submission form have identical times.');
  89. }
  90. }
  91. // Reset format back to default
  92. variable_set('date_popup_authored_format_page', $format);
  93. }
  94. /**
  95. * Tests form field validation.
  96. */
  97. function testFieldValidation() {
  98. // Define some test cases.
  99. $test_cases = array(
  100. array(
  101. 'description' => 'a valid date field and a missing time field',
  102. 'date' => '02/07/2014',
  103. 'time' => '',
  104. 'valid' => FALSE,
  105. ),
  106. array(
  107. 'description' => 'a valid date field and a valid time field',
  108. 'date' => '02/07/2014',
  109. 'time' => '12:00',
  110. 'valid' => TRUE,
  111. ),
  112. );
  113. // Log in as administrator.
  114. $this->drupalLogin($this->admin_user);
  115. // Test the test cases.
  116. foreach ($test_cases as $test_case) {
  117. $edit = array(
  118. 'title' => $this->randomString(),
  119. 'date[date]' => $test_case['date'],
  120. 'date[time]' => $test_case['time'],
  121. );
  122. $this->drupalPost('node/add/page', $edit, t('Save'));
  123. $error_messages = $this->xpath('//div[contains(@class, "error")]');
  124. $message = format_string('When submitting a node with @description the form validation correctly @result.', array(
  125. '@description' => $test_case['description'],
  126. '@result' => $test_case['valid'] ? 'succeeds' : 'fails',
  127. ));
  128. $this->assertEqual(empty($error_messages), $test_case['valid'], $message);
  129. }
  130. }
  131. /**
  132. * Tests variable cleanup after a content type is removed.
  133. */
  134. function testVariableCleanupAfterNodeTypeRemoval() {
  135. $node_type_name = strtolower($this->randomName(8) . '_test');
  136. $node_type = $this->drupalCreateContentType(array('name' => $node_type_name, 'type' => $node_type_name));
  137. variable_set('date_popup_authored_enabled_' . $node_type_name, 'foo');
  138. variable_set('date_popup_authored_format_' . $node_type_name, 'foo');
  139. variable_set('date_popup_authored_year_range_' . $node_type_name, 'foo');
  140. node_type_delete($node_type_name);
  141. $this->assertNull(variable_get('date_popup_authored_enabled_' . $node_type_name));
  142. $this->assertNull(variable_get('date_popup_authored_format_' . $node_type_name));
  143. $this->assertNull(variable_get('date_popup_authored_year_range_' . $node_type_name));
  144. }
  145. /**
  146. * Tests variable cleanup after Date Popup Authored is uninstalled.
  147. */
  148. function testVariableCleanupAfterUninstall() {
  149. variable_set('date_popup_authored_enabled_page', 'foo');
  150. variable_set('date_popup_authored_format_page', 'foo');
  151. variable_set('date_popup_authored_year_range_page', 'foo');
  152. module_disable(array('date_popup_authored'));
  153. drupal_uninstall_modules(array('date_popup_authored'));
  154. $this->assertNull(variable_get('date_popup_authored_enabled_page'));
  155. $this->assertNull(variable_get('date_popup_authored_format_page'));
  156. $this->assertNull(variable_get('date_popup_authored_year_range_page'));
  157. }
  158. }