actions.test 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. class ActionsConfigurationTestCase extends DrupalWebTestCase {
  3. public static function getInfo() {
  4. return array(
  5. 'name' => 'Actions configuration',
  6. 'description' => 'Tests complex actions configuration by adding, editing, and deleting a complex action.',
  7. 'group' => 'Actions',
  8. );
  9. }
  10. /**
  11. * Test the configuration of advanced actions through the administration
  12. * interface.
  13. */
  14. function testActionConfiguration() {
  15. // Create a user with permission to view the actions administration pages.
  16. $user = $this->drupalCreateUser(array('administer actions'));
  17. $this->drupalLogin($user);
  18. // Make a POST request to admin/config/system/actions/manage.
  19. $edit = array();
  20. $edit['action'] = drupal_hash_base64('system_goto_action');
  21. $this->drupalPost('admin/config/system/actions/manage', $edit, t('Create'));
  22. // Make a POST request to the individual action configuration page.
  23. $edit = array();
  24. $action_label = $this->randomName();
  25. $edit['actions_label'] = $action_label;
  26. $edit['url'] = 'admin';
  27. $this->drupalPost('admin/config/system/actions/configure/' . drupal_hash_base64('system_goto_action'), $edit, t('Save'));
  28. // Make sure that the new complex action was saved properly.
  29. $this->assertText(t('The action has been successfully saved.'), t("Make sure we get a confirmation that we've successfully saved the complex action."));
  30. $this->assertText($action_label, t("Make sure the action label appears on the configuration page after we've saved the complex action."));
  31. // Make another POST request to the action edit page.
  32. $this->clickLink(t('configure'));
  33. preg_match('|admin/config/system/actions/configure/(\d+)|', $this->getUrl(), $matches);
  34. $aid = $matches[1];
  35. $edit = array();
  36. $new_action_label = $this->randomName();
  37. $edit['actions_label'] = $new_action_label;
  38. $edit['url'] = 'admin';
  39. $this->drupalPost(NULL, $edit, t('Save'));
  40. // Make sure that the action updated properly.
  41. $this->assertText(t('The action has been successfully saved.'), t("Make sure we get a confirmation that we've successfully updated the complex action."));
  42. $this->assertNoText($action_label, t("Make sure the old action label does NOT appear on the configuration page after we've updated the complex action."));
  43. $this->assertText($new_action_label, t("Make sure the action label appears on the configuration page after we've updated the complex action."));
  44. // Make sure that deletions work properly.
  45. $this->clickLink(t('delete'));
  46. $edit = array();
  47. $this->drupalPost("admin/config/system/actions/delete/$aid", $edit, t('Delete'));
  48. // Make sure that the action was actually deleted.
  49. $this->assertRaw(t('Action %action was deleted', array('%action' => $new_action_label)), t('Make sure that we get a delete confirmation message.'));
  50. $this->drupalGet('admin/config/system/actions/manage');
  51. $this->assertNoText($new_action_label, t("Make sure the action label does not appear on the overview page after we've deleted the action."));
  52. $exists = db_query('SELECT aid FROM {actions} WHERE callback = :callback', array(':callback' => 'drupal_goto_action'))->fetchField();
  53. $this->assertFalse($exists, t('Make sure the action is gone from the database after being deleted.'));
  54. }
  55. }
  56. /**
  57. * Test actions executing in a potential loop, and make sure they abort properly.
  58. */
  59. class ActionLoopTestCase extends DrupalWebTestCase {
  60. public static function getInfo() {
  61. return array(
  62. 'name' => 'Actions executing in a potentially infinite loop',
  63. 'description' => 'Tests actions executing in a loop, and makes sure they abort properly.',
  64. 'group' => 'Actions',
  65. );
  66. }
  67. function setUp() {
  68. parent::setUp('dblog', 'trigger', 'actions_loop_test');
  69. }
  70. /**
  71. * Set up a loop with 3 - 12 recursions, and see if it aborts properly.
  72. */
  73. function testActionLoop() {
  74. $user = $this->drupalCreateUser(array('administer actions'));
  75. $this->drupalLogin($user);
  76. $hash = drupal_hash_base64('actions_loop_test_log');
  77. $edit = array('aid' => $hash);
  78. $this->drupalPost('admin/structure/trigger/actions_loop_test', $edit, t('Assign'));
  79. // Delete any existing watchdog messages to clear the plethora of
  80. // "Action added" messages from when Drupal was installed.
  81. db_delete('watchdog')->execute();
  82. // To prevent this test from failing when xdebug is enabled, the maximum
  83. // recursion level should be kept low enough to prevent the xdebug
  84. // infinite recursion protection mechanism from aborting the request.
  85. // See http://drupal.org/node/587634.
  86. variable_set('actions_max_stack', 7);
  87. $this->triggerActions();
  88. }
  89. /**
  90. * Create an infinite loop by causing a watchdog message to be set,
  91. * which causes the actions to be triggered again, up to actions_max_stack
  92. * times.
  93. */
  94. protected function triggerActions() {
  95. $this->drupalGet('<front>', array('query' => array('trigger_actions_on_watchdog' => TRUE)));
  96. $expected = array();
  97. $expected[] = 'Triggering action loop';
  98. for ($i = 1; $i <= variable_get('actions_max_stack', 35); $i++) {
  99. $expected[] = "Test log #$i";
  100. }
  101. $expected[] = 'Stack overflow: too many calls to actions_do(). Aborting to prevent infinite recursion.';
  102. $result = db_query("SELECT message FROM {watchdog} WHERE type = 'actions_loop_test' OR type = 'actions' ORDER BY wid");
  103. $loop_started = FALSE;
  104. foreach ($result as $row) {
  105. $expected_message = array_shift($expected);
  106. $this->assertEqual($row->message, $expected_message, t('Expected message %expected, got %message.', array('%expected' => $expected_message, '%message' => $row->message)));
  107. }
  108. $this->assertTrue(empty($expected), t('All expected messages found.'));
  109. }
  110. }