feedback.test 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * @file
  4. * Tests for Feedback module.
  5. */
  6. /**
  7. * Test basic Feedback functionality.
  8. */
  9. class FeedbackTestCase extends DrupalWebTestCase {
  10. protected $profile = 'testing';
  11. public static function getInfo() {
  12. return array(
  13. 'name' => 'Feedback functionality',
  14. 'description' => 'Tests basic Feedback module functionality.',
  15. 'group' => 'Feedback',
  16. );
  17. }
  18. function setUp() {
  19. parent::setUp(array('feedback', 'trigger'));
  20. $this->admin_user = $this->drupalCreateUser(array('access feedback form', 'view feedback messages', 'administer feedback'));
  21. $this->web_user = $this->drupalCreateUser(array('access feedback form'));
  22. $this->drupalLogin($this->web_user);
  23. }
  24. /**
  25. * Test a basic feedback message.
  26. */
  27. function testFeedbackMessage() {
  28. $message = $this->randomString();
  29. $edit = array(
  30. 'message' => $message,
  31. );
  32. $this->drupalPost('node', $edit, t('Send feedback'));
  33. // Verify the message was recorded.
  34. $this->drupalLogin($this->admin_user);
  35. $this->drupalGet('admin/reports/feedback');
  36. $this->assertRaw(check_plain($message), t('Message found.'));
  37. $this->assertText('node', t('Originating system path found.'));
  38. $this->assertLinkByHref(url('node', array('absolute' => TRUE)), 0, t('Originating absolute URL found.'));
  39. // Verify that we can process the message.
  40. $edit = array(
  41. 'feedback-messages[0][1]' => TRUE,
  42. );
  43. $this->drupalPost(NULL, $edit, t('Update'));
  44. $this->assertFieldByName('feedback-messages[1][1]', 1, t('Processed message found.'));
  45. }
  46. /**
  47. * Test visibility settings.
  48. */
  49. function testFeedbackVisibility() {
  50. $this->drupalLogin($this->admin_user);
  51. $this->drupalGet('user');
  52. $this->assertRaw('<span class="feedback-link">' . t('Feedback') . '</span>', t('Feedback form shown.'));
  53. $edit = array(
  54. 'feedback_excluded_paths' => 'user*',
  55. );
  56. $this->drupalPost('admin/config/user-interface/feedback', $edit, t('Save configuration'));
  57. $this->drupalGet('user');
  58. $this->assertNoRaw('<span class="feedback-link">' . t('Feedback') . '</span>', t('Feedback form not shown.'));
  59. }
  60. /**
  61. * Test feedback deletion.
  62. */
  63. function testFeedbackDelete() {
  64. $this->drupalLogin($this->admin_user);
  65. $message = $this->randomString();
  66. $edit = array(
  67. 'message' => $message,
  68. );
  69. $this->drupalPost('node', $edit, t('Send feedback'));
  70. // Verify a delete link is shown.
  71. $this->drupalGet('admin/reports/feedback');
  72. $this->assertLinkByHref('admin/reports/feedback/1/delete');
  73. // Verify deletion.
  74. $this->drupalPost('admin/reports/feedback/1/delete', array(), t('Delete'));
  75. $this->assertRaw(t('The feedback entry was deleted'), t('Feedback deletion message shown.'));
  76. $this->assertNoLinkByHref('admin/reports/feedback/1/delete');
  77. $this->assertNoRaw(check_plain($message), t('Message not found.'));
  78. }
  79. /**
  80. * Test the feedback triggers and actions.
  81. */
  82. function testFeedbackEmailAction() {
  83. $test_user = $this->drupalCreateUser(array('administer actions', 'administer feedback', 'access feedback form'));
  84. $this->drupalLogin($test_user);
  85. $this->assignFeedbackEmailAction('feedback_insert', $test_user->mail);
  86. // Insert a feedback entry.
  87. $message = $this->randomString();
  88. $edit = array(
  89. 'message' => $message,
  90. );
  91. $this->drupalPost('node', $edit, t('Send feedback'));
  92. $this->assertFeedbackEmailTokenReplacement($test_user->mail, $message);
  93. $this->assignFeedbackEmailAction('feedback_update', $test_user->mail);
  94. // Update a feedback entry.
  95. $fid = db_query("SELECT fid FROM {feedback} LIMIT 0,1")->fetchField();
  96. $entry = feedback_load($fid);
  97. feedback_save($entry);
  98. $this->assertFeedbackEmailTokenReplacement($test_user->mail, $entry->message);
  99. }
  100. /**
  101. * Assigns a system_send_email_action to the passed-in trigger.
  102. *
  103. * @param $trigger
  104. * For example, 'user_login'
  105. */
  106. function assignFeedbackEmailAction($trigger, $mail) {
  107. $form_name = "trigger_{$trigger}_assign_form";
  108. $form_html_id = strtr($form_name, '_', '-');
  109. $edit = array(
  110. 'actions_label' => $trigger . "_feedback_send_email_action",
  111. 'recipients' => $mail,
  112. 'subject' => $this->randomName(),
  113. 'message' => '!message',
  114. );
  115. $hash = drupal_hash_base64('feedback_send_email_action');
  116. $this->drupalPost("admin/config/system/actions/configure/$hash", $edit, t('Save'));
  117. $this->assertText(t('The action has been successfully saved.'));
  118. // Now we have to find out the action ID of what we created.
  119. $aid = db_query('SELECT aid FROM {actions} WHERE callback = :callback AND label = :label', array(':callback' => 'feedback_send_email_action', ':label' => $edit['actions_label']))->fetchField();
  120. $edit = array('aid' => drupal_hash_base64($aid));
  121. $this->drupalPost('admin/structure/trigger/feedback', $edit, t('Assign'), array(), array(), $form_html_id);
  122. }
  123. /**
  124. * Asserts correct token replacement for the given trigger and account.
  125. *
  126. * @param $account
  127. * The user account which triggered the action.
  128. * @param $email_depth
  129. * Number of emails to scan, starting with most recent.
  130. */
  131. function assertFeedbackEmailTokenReplacement($mail, $message, $email_depth = 1) {
  132. $this->verboseEmail($email_depth);
  133. $this->assertMailString('body', $message, $email_depth);
  134. $this->assertMail('to', $mail, t('Mail sent to correct destination'));
  135. }
  136. }