trigger.test 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. <?php
  2. /**
  3. * @file
  4. * Tests for trigger.module.
  5. */
  6. /**
  7. * Provides common helper methods.
  8. */
  9. class TriggerWebTestCase extends DrupalWebTestCase {
  10. /**
  11. * Configures an advanced action.
  12. *
  13. * @param $action
  14. * The name of the action callback. For example: 'user_block_user_action'
  15. * @param $edit
  16. * The $edit array for the form to be used to configure.
  17. * Example members would be 'actions_label' (always), 'message', etc.
  18. *
  19. * @return
  20. * the aid (action id) of the configured action, or FALSE if none.
  21. */
  22. protected function configureAdvancedAction($action, $edit) {
  23. // Create an advanced action.
  24. $hash = drupal_hash_base64($action);
  25. $this->drupalPost("admin/config/system/actions/configure/$hash", $edit, t('Save'));
  26. $this->assertText(t('The action has been successfully saved.'));
  27. // Now we have to find out the action ID of what we created.
  28. return db_query('SELECT aid FROM {actions} WHERE callback = :callback AND label = :label', array(':callback' => $action, ':label' => $edit['actions_label']))->fetchField();
  29. }
  30. }
  31. /**
  32. * Provides tests for node triggers.
  33. */
  34. class TriggerContentTestCase extends TriggerWebTestCase {
  35. var $_cleanup_roles = array();
  36. var $_cleanup_users = array();
  37. public static function getInfo() {
  38. return array(
  39. 'name' => 'Trigger content (node) actions',
  40. 'description' => 'Perform various tests with content actions.',
  41. 'group' => 'Trigger',
  42. );
  43. }
  44. function setUp() {
  45. parent::setUp('trigger', 'trigger_test');
  46. }
  47. /**
  48. * Tests several content-oriented trigger issues.
  49. *
  50. * These are in one function to assure they happen in the right order.
  51. */
  52. function testActionsContent() {
  53. global $user;
  54. $content_actions = array('node_publish_action', 'node_unpublish_action', 'node_make_sticky_action', 'node_make_unsticky_action', 'node_promote_action', 'node_unpromote_action');
  55. $test_user = $this->drupalCreateUser(array('administer actions'));
  56. $web_user = $this->drupalCreateUser(array('create page content', 'access content', 'administer nodes'));
  57. foreach ($content_actions as $action) {
  58. $hash = drupal_hash_base64($action);
  59. $info = $this->actionInfo($action);
  60. // Assign an action to a trigger, then pull the trigger, and make sure
  61. // the actions fire.
  62. $this->drupalLogin($test_user);
  63. $edit = array('aid' => $hash);
  64. $this->drupalPost('admin/structure/trigger/node', $edit, t('Assign'), array(), array(), 'trigger-node-presave-assign-form');
  65. // Create an unpublished node.
  66. $this->drupalLogin($web_user);
  67. $edit = array();
  68. $langcode = LANGUAGE_NONE;
  69. $edit["title"] = '!SimpleTest test node! ' . $this->randomName(10);
  70. $edit["body[$langcode][0][value]"] = '!SimpleTest test body! ' . $this->randomName(32) . ' ' . $this->randomName(32);
  71. $edit[$info['property']] = !$info['expected'];
  72. $this->drupalPost('node/add/page', $edit, t('Save'));
  73. // Make sure the text we want appears.
  74. $this->assertRaw(t('!post %title has been created.', array('!post' => 'Basic page', '%title' => $edit["title"])), 'Make sure the Basic page has actually been created');
  75. // Action should have been fired.
  76. $loaded_node = $this->drupalGetNodeByTitle($edit["title"]);
  77. $this->assertTrue($loaded_node->$info['property'] == $info['expected'], format_string('Make sure the @action action fired.', array('@action' => $info['name'])));
  78. // Leave action assigned for next test
  79. // There should be an error when the action is assigned to the trigger
  80. // twice.
  81. $this->drupalLogin($test_user);
  82. // This action already assigned in this test.
  83. $edit = array('aid' => $hash);
  84. $this->drupalPost('admin/structure/trigger/node', $edit, t('Assign'), array(), array(), 'trigger-node-presave-assign-form');
  85. $this->assertRaw(t('The action you chose is already assigned to that trigger.'), 'Check to make sure an error occurs when assigning an action to a trigger twice.');
  86. // The action should be able to be unassigned from a trigger.
  87. $this->drupalPost('admin/structure/trigger/unassign/node/node_presave/' . $hash, array(), t('Unassign'));
  88. $this->assertRaw(t('Action %action has been unassigned.', array('%action' => ucfirst($info['name']))), format_string('Check to make sure the @action action can be unassigned from the trigger.', array('@action' => $info['name'])));
  89. $assigned = db_query("SELECT COUNT(*) FROM {trigger_assignments} WHERE aid IN (:keys)", array(':keys' => $content_actions))->fetchField();
  90. $this->assertFalse($assigned, 'Check to make sure unassign worked properly at the database level.');
  91. }
  92. }
  93. /**
  94. * Tests multiple node actions.
  95. *
  96. * Verifies that node actions are fired for each node individually, if acting
  97. * on multiple nodes.
  98. */
  99. function testActionContentMultiple() {
  100. // Assign an action to the node save/update trigger.
  101. $test_user = $this->drupalCreateUser(array('administer actions', 'administer nodes', 'create page content', 'access administration pages', 'access content overview'));
  102. $this->drupalLogin($test_user);
  103. $nodes = array();
  104. for ($index = 0; $index < 3; $index++) {
  105. $nodes[] = $this->drupalCreateNode(array('type' => 'page'));
  106. }
  107. $action_id = 'trigger_test_generic_any_action';
  108. $hash = drupal_hash_base64($action_id);
  109. $edit = array('aid' => $hash);
  110. $this->drupalPost('admin/structure/trigger/node', $edit, t('Assign'), array(), array(), 'trigger-node-update-assign-form');
  111. $edit = array(
  112. 'operation' => 'unpublish',
  113. 'nodes[' . $nodes[0]->nid . ']' => TRUE,
  114. 'nodes[' . $nodes[1]->nid . ']' => TRUE,
  115. );
  116. $this->drupalPost('admin/content', $edit, t('Update'));
  117. $count = variable_get('trigger_test_generic_any_action', 0);
  118. $this->assertTrue($count == 2, format_string('Action was triggered 2 times. Actual: %count', array('%count' => $count)));
  119. }
  120. /**
  121. * Returns some info about each of the content actions.
  122. *
  123. * This is helper function for testActionsContent().
  124. *
  125. * @param $action
  126. * The name of the action to return info about.
  127. *
  128. * @return
  129. * An associative array of info about the action.
  130. */
  131. function actionInfo($action) {
  132. $info = array(
  133. 'node_publish_action' => array(
  134. 'property' => 'status',
  135. 'expected' => 1,
  136. 'name' => t('publish content'),
  137. ),
  138. 'node_unpublish_action' => array(
  139. 'property' => 'status',
  140. 'expected' => 0,
  141. 'name' => t('unpublish content'),
  142. ),
  143. 'node_make_sticky_action' => array(
  144. 'property' => 'sticky',
  145. 'expected' => 1,
  146. 'name' => t('make content sticky'),
  147. ),
  148. 'node_make_unsticky_action' => array(
  149. 'property' => 'sticky',
  150. 'expected' => 0,
  151. 'name' => t('make content unsticky'),
  152. ),
  153. 'node_promote_action' => array(
  154. 'property' => 'promote',
  155. 'expected' => 1,
  156. 'name' => t('promote content to front page'),
  157. ),
  158. 'node_unpromote_action' => array(
  159. 'property' => 'promote',
  160. 'expected' => 0,
  161. 'name' => t('remove content from front page'),
  162. ),
  163. );
  164. return $info[$action];
  165. }
  166. }
  167. /**
  168. * Tests cron trigger.
  169. */
  170. class TriggerCronTestCase extends TriggerWebTestCase {
  171. public static function getInfo() {
  172. return array(
  173. 'name' => 'Trigger cron (system) actions',
  174. 'description' => 'Perform various tests with cron trigger.',
  175. 'group' => 'Trigger',
  176. );
  177. }
  178. function setUp() {
  179. parent::setUp('trigger', 'trigger_test');
  180. }
  181. /**
  182. * Tests assigning multiple actions to the cron trigger.
  183. *
  184. * This test ensures that both simple and multiple complex actions
  185. * succeed properly. This is done in the cron trigger test because
  186. * cron allows passing multiple actions in at once.
  187. */
  188. function testActionsCron() {
  189. // Create an administrative user.
  190. $test_user = $this->drupalCreateUser(array('administer actions'));
  191. $this->drupalLogin($test_user);
  192. // Assign a non-configurable action to the cron run trigger.
  193. $edit = array('aid' => drupal_hash_base64('trigger_test_system_cron_action'));
  194. $this->drupalPost('admin/structure/trigger/system', $edit, t('Assign'), array(), array(), 'trigger-cron-assign-form');
  195. // Assign a configurable action to the cron trigger.
  196. $action_label = $this->randomName();
  197. $edit = array(
  198. 'actions_label' => $action_label,
  199. 'subject' => $action_label,
  200. );
  201. $aid = $this->configureAdvancedAction('trigger_test_system_cron_conf_action', $edit);
  202. // $aid is likely 3 but if we add more uses for the sequences table in
  203. // core it might break, so it is easier to get the value from the database.
  204. $edit = array('aid' => drupal_hash_base64($aid));
  205. $this->drupalPost('admin/structure/trigger/system', $edit, t('Assign'), array(), array(), 'trigger-cron-assign-form');
  206. // Add a second configurable action to the cron trigger.
  207. $action_label = $this->randomName();
  208. $edit = array(
  209. 'actions_label' => $action_label,
  210. 'subject' => $action_label,
  211. );
  212. $aid = $this->configureAdvancedAction('trigger_test_system_cron_conf_action', $edit);
  213. $edit = array('aid' => drupal_hash_base64($aid));
  214. $this->drupalPost('admin/structure/trigger/system', $edit, t('Assign'), array(), array(), 'trigger-cron-assign-form');
  215. // Force a cron run.
  216. $this->cronRun();
  217. // Make sure the non-configurable action has fired.
  218. $action_run = variable_get('trigger_test_system_cron_action', FALSE);
  219. $this->assertTrue($action_run, 'Check that the cron run triggered the test action.');
  220. // Make sure that both configurable actions have fired.
  221. $action_run = variable_get('trigger_test_system_cron_conf_action', 0) == 2;
  222. $this->assertTrue($action_run, 'Check that the cron run triggered both complex actions.');
  223. }
  224. }
  225. /**
  226. * Provides a base class with trigger assignments and test comparisons.
  227. */
  228. class TriggerActionTestCase extends TriggerWebTestCase {
  229. function setUp() {
  230. parent::setUp('trigger');
  231. }
  232. /**
  233. * Creates a message with tokens.
  234. *
  235. * @param $trigger
  236. *
  237. * @return
  238. * A message with embedded tokens.
  239. */
  240. function generateMessageWithTokens($trigger) {
  241. // Note that subject is limited to 254 characters in action configuration.
  242. $message = t('Action was triggered by trigger @trigger user:name=[user:name] user:uid=[user:uid] user:mail=[user:mail] user:url=[user:url] user:edit-url=[user:edit-url] user:created=[user:created]',
  243. array('@trigger' => $trigger));
  244. return trim($message);
  245. }
  246. /**
  247. * Generates a comparison message to match the pre-token-replaced message.
  248. *
  249. * @param $trigger
  250. * Trigger, like 'user_login'.
  251. * @param $account
  252. * Associated user account.
  253. *
  254. * @return
  255. * The token-replaced equivalent message. This does not use token
  256. * functionality.
  257. *
  258. * @see generateMessageWithTokens()
  259. */
  260. function generateTokenExpandedComparison($trigger, $account) {
  261. // Note that user:last-login was omitted because it changes and can't
  262. // be properly verified.
  263. $message = t('Action was triggered by trigger @trigger user:name=@username user:uid=@uid user:mail=@mail user:url=@user_url user:edit-url=@user_edit_url user:created=@user_created',
  264. array(
  265. '@trigger' => $trigger,
  266. '@username' => $account->name,
  267. '@uid' => !empty($account->uid) ? $account->uid : t('not yet assigned'),
  268. '@mail' => $account->mail,
  269. '@user_url' => !empty($account->uid) ? url("user/$account->uid", array('absolute' => TRUE)) : t('not yet assigned'),
  270. '@user_edit_url' => !empty($account->uid) ? url("user/$account->uid/edit", array('absolute' => TRUE)) : t('not yet assigned'),
  271. '@user_created' => isset($account->created) ? format_date($account->created, 'medium') : t('not yet created'),
  272. )
  273. );
  274. return trim($message);
  275. }
  276. /**
  277. * Assigns a simple (non-configurable) action to a trigger.
  278. *
  279. * @param $trigger
  280. * The trigger to assign to, like 'user_login'.
  281. * @param $action
  282. * The simple action to be assigned, like 'comment_insert'.
  283. */
  284. function assignSimpleAction($trigger, $action) {
  285. $form_name = "trigger_{$trigger}_assign_form";
  286. $form_html_id = strtr($form_name, '_', '-');
  287. $edit = array('aid' => drupal_hash_base64($action));
  288. $trigger_type = preg_replace('/_.*/', '', $trigger);
  289. $this->drupalPost("admin/structure/trigger/$trigger_type", $edit, t('Assign'), array(), array(), $form_html_id);
  290. $actions = trigger_get_assigned_actions($trigger);
  291. $this->assertTrue(!empty($actions[$action]), format_string('Simple action @action assigned to trigger @trigger', array('@action' => $action, '@trigger' => $trigger)));
  292. }
  293. /**
  294. * Assigns a system message action to the passed-in trigger.
  295. *
  296. * @param $trigger
  297. * For example, 'user_login'
  298. */
  299. function assignSystemMessageAction($trigger) {
  300. $form_name = "trigger_{$trigger}_assign_form";
  301. $form_html_id = strtr($form_name, '_', '-');
  302. // Assign a configurable action 'System message' to the passed trigger.
  303. $action_edit = array(
  304. 'actions_label' => $trigger . "_system_message_action_" . $this->randomName(16),
  305. 'message' => $this->generateMessageWithTokens($trigger),
  306. );
  307. // Configure an advanced action that we can assign.
  308. $aid = $this->configureAdvancedAction('system_message_action', $action_edit);
  309. $edit = array('aid' => drupal_hash_base64($aid));
  310. $this->drupalPost('admin/structure/trigger/user', $edit, t('Assign'), array(), array(), $form_html_id);
  311. drupal_static_reset('trigger_get_asssigned_actions');
  312. }
  313. /**
  314. * Assigns a system_send_email_action to the passed-in trigger.
  315. *
  316. * @param $trigger
  317. * For example, 'user_login'
  318. */
  319. function assignSystemEmailAction($trigger) {
  320. $form_name = "trigger_{$trigger}_assign_form";
  321. $form_html_id = strtr($form_name, '_', '-');
  322. $message = $this->generateMessageWithTokens($trigger);
  323. // Assign a configurable action 'System message' to the passed trigger.
  324. $action_edit = array(
  325. // 'actions_label' => $trigger . "_system_send_message_action_" . $this->randomName(16),
  326. 'actions_label' => $trigger . "_system_send_email_action",
  327. 'recipient' => '[user:mail]',
  328. 'subject' => $message,
  329. 'message' => $message,
  330. );
  331. // Configure an advanced action that we can assign.
  332. $aid = $this->configureAdvancedAction('system_send_email_action', $action_edit);
  333. $edit = array('aid' => drupal_hash_base64($aid));
  334. $this->drupalPost('admin/structure/trigger/user', $edit, t('Assign'), array(), array(), $form_html_id);
  335. drupal_static_reset('trigger_get_assigned_actions');
  336. }
  337. /**
  338. * Asserts correct token replacement in both system message and email.
  339. *
  340. * @param $trigger
  341. * A trigger like 'user_login'.
  342. * @param $account
  343. * The user account which triggered the action.
  344. * @param $email_depth
  345. * Number of emails to scan, starting with most recent.
  346. */
  347. function assertSystemMessageAndEmailTokenReplacement($trigger, $account, $email_depth = 1) {
  348. $this->assertSystemMessageTokenReplacement($trigger, $account);
  349. $this->assertSystemEmailTokenReplacement($trigger, $account, $email_depth);
  350. }
  351. /**
  352. * Asserts correct token replacement for the given trigger and account.
  353. *
  354. * @param $trigger
  355. * A trigger like 'user_login'.
  356. * @param $account
  357. * The user account which triggered the action.
  358. */
  359. function assertSystemMessageTokenReplacement($trigger, $account) {
  360. $expected = $this->generateTokenExpandedComparison($trigger, $account);
  361. $this->assertText($expected,
  362. format_string('Expected system message to contain token-replaced text "@expected" found in configured system message action', array('@expected' => $expected )) );
  363. }
  364. /**
  365. * Asserts correct token replacement for the given trigger and account.
  366. *
  367. * @param $trigger
  368. * A trigger like 'user_login'.
  369. * @param $account
  370. * The user account which triggered the action.
  371. * @param $email_depth
  372. * Number of emails to scan, starting with most recent.
  373. */
  374. function assertSystemEmailTokenReplacement($trigger, $account, $email_depth = 1) {
  375. $this->verboseEmail($email_depth);
  376. $expected = $this->generateTokenExpandedComparison($trigger, $account);
  377. $this->assertMailString('subject', $expected, $email_depth);
  378. $this->assertMailString('body', $expected, $email_depth);
  379. $this->assertMail('to', $account->mail, 'Mail sent to correct destination');
  380. }
  381. }
  382. /**
  383. * Tests token substitution in trigger actions.
  384. *
  385. * This tests nearly every permutation of user triggers with system actions
  386. * and checks the token replacement.
  387. */
  388. class TriggerUserTokenTestCase extends TriggerActionTestCase {
  389. public static function getInfo() {
  390. return array(
  391. 'name' => 'Test user triggers',
  392. 'description' => 'Test user triggers and system actions with token replacement.',
  393. 'group' => 'Trigger',
  394. );
  395. }
  396. /**
  397. * Tests a variety of token replacements in actions.
  398. */
  399. function testUserTriggerTokenReplacement() {
  400. $test_user = $this->drupalCreateUser(array('administer actions', 'administer users', 'change own username', 'access user profiles'));
  401. $this->drupalLogin($test_user);
  402. $triggers = array('user_login', 'user_insert', 'user_update', 'user_delete', 'user_logout', 'user_view');
  403. foreach ($triggers as $trigger) {
  404. $this->assignSystemMessageAction($trigger);
  405. $this->assignSystemEmailAction($trigger);
  406. }
  407. $this->drupalLogout();
  408. $this->assertSystemEmailTokenReplacement('user_logout', $test_user);
  409. $this->drupalLogin($test_user);
  410. $this->assertSystemMessageAndEmailTokenReplacement('user_login', $test_user, 2);
  411. $this->assertSystemMessageAndEmailTokenReplacement('user_view', $test_user, 2);
  412. $this->drupalPost("user/{$test_user->uid}/edit", array('name' => $test_user->name . '_changed'), t('Save'));
  413. $test_user->name .= '_changed'; // Since we just changed it.
  414. $this->assertSystemMessageAndEmailTokenReplacement('user_update', $test_user, 2);
  415. $this->drupalGet('user');
  416. $this->assertSystemMessageAndEmailTokenReplacement('user_view', $test_user);
  417. $new_user = $this->drupalCreateUser(array('administer actions', 'administer users', 'cancel account', 'access administration pages'));
  418. $this->assertSystemEmailTokenReplacement('user_insert', $new_user);
  419. $this->drupalLogin($new_user);
  420. $user_to_delete = $this->drupalCreateUser(array('access content'));
  421. variable_set('user_cancel_method', 'user_cancel_delete');
  422. $this->drupalPost("user/{$user_to_delete->uid}/cancel", array(), t('Cancel account'));
  423. $this->assertSystemMessageAndEmailTokenReplacement('user_delete', $user_to_delete);
  424. }
  425. }
  426. /**
  427. * Tests token substitution in trigger actions.
  428. *
  429. * This tests nearly every permutation of user triggers with system actions
  430. * and checks the token replacement.
  431. */
  432. class TriggerUserActionTestCase extends TriggerActionTestCase {
  433. public static function getInfo() {
  434. return array(
  435. 'name' => 'Test user actions',
  436. 'description' => 'Test user actions.',
  437. 'group' => 'Trigger',
  438. );
  439. }
  440. /**
  441. * Tests user action assignment and execution.
  442. */
  443. function testUserActionAssignmentExecution() {
  444. $test_user = $this->drupalCreateUser(array('administer actions', 'create article content', 'access comments', 'administer comments', 'skip comment approval', 'edit own comments'));
  445. $this->drupalLogin($test_user);
  446. $triggers = array('comment_presave', 'comment_insert', 'comment_update');
  447. // system_block_ip_action is difficult to test without ruining the test.
  448. $actions = array('user_block_user_action');
  449. foreach ($triggers as $trigger) {
  450. foreach ($actions as $action) {
  451. $this->assignSimpleAction($trigger, $action);
  452. }
  453. }
  454. $node = $this->drupalCreateNode(array('type' => 'article'));
  455. $this->drupalPost("node/{$node->nid}", array('comment_body[und][0][value]' => t("my comment"), 'subject' => t("my comment subject")), t('Save'));
  456. // Posting a comment should have blocked this user.
  457. $account = user_load($test_user->uid, TRUE);
  458. $this->assertTrue($account->status == 0, 'Account is blocked');
  459. $comment_author_uid = $account->uid;
  460. // Now rehabilitate the comment author so it can be be blocked again when
  461. // the comment is updated.
  462. user_save($account, array('status' => TRUE));
  463. $test_user = $this->drupalCreateUser(array('administer actions', 'create article content', 'access comments', 'administer comments', 'skip comment approval', 'edit own comments'));
  464. $this->drupalLogin($test_user);
  465. // Our original comment will have been comment 1.
  466. $this->drupalPost("comment/1/edit", array('comment_body[und][0][value]' => t("my comment, updated"), 'subject' => t("my comment subject")), t('Save'));
  467. $comment_author_account = user_load($comment_author_uid, TRUE);
  468. $this->assertTrue($comment_author_account->status == 0, format_string('Comment author account (uid=@uid) is blocked after update to comment', array('@uid' => $comment_author_uid)));
  469. // Verify that the comment was updated.
  470. $test_user = $this->drupalCreateUser(array('administer actions', 'create article content', 'access comments', 'administer comments', 'skip comment approval', 'edit own comments'));
  471. $this->drupalLogin($test_user);
  472. $this->drupalGet("node/$node->nid");
  473. $this->assertText(t("my comment, updated"));
  474. $this->verboseEmail();
  475. }
  476. }
  477. /**
  478. * Tests other triggers.
  479. */
  480. class TriggerOtherTestCase extends TriggerWebTestCase {
  481. var $_cleanup_roles = array();
  482. var $_cleanup_users = array();
  483. public static function getInfo() {
  484. return array(
  485. 'name' => 'Trigger other actions',
  486. 'description' => 'Test triggering of user, comment, taxonomy actions.',
  487. 'group' => 'Trigger',
  488. );
  489. }
  490. function setUp() {
  491. parent::setUp('trigger', 'trigger_test', 'contact');
  492. }
  493. /**
  494. * Tests triggering on user create and user login.
  495. */
  496. function testActionsUser() {
  497. // Assign an action to the create user trigger.
  498. $test_user = $this->drupalCreateUser(array('administer actions'));
  499. $this->drupalLogin($test_user);
  500. $action_id = 'trigger_test_generic_action';
  501. $hash = drupal_hash_base64($action_id);
  502. $edit = array('aid' => $hash);
  503. $this->drupalPost('admin/structure/trigger/user', $edit, t('Assign'), array(), array(), 'trigger-user-insert-assign-form');
  504. // Set action variable to FALSE.
  505. variable_set($action_id, FALSE);
  506. // Create an unblocked user
  507. $web_user = $this->drupalCreateUser(array('administer users'));
  508. $this->drupalLogin($web_user);
  509. $name = $this->randomName();
  510. $pass = user_password();
  511. $edit = array();
  512. $edit['name'] = $name;
  513. $edit['mail'] = $name . '@example.com';
  514. $edit['pass[pass1]'] = $pass;
  515. $edit['pass[pass2]'] = $pass;
  516. $edit['status'] = 1;
  517. $this->drupalPost('admin/people/create', $edit, t('Create new account'));
  518. // Verify that the action variable has been set.
  519. $this->assertTrue(variable_get($action_id, FALSE), 'Check that creating a user triggered the test action.');
  520. // Reset the action variable.
  521. variable_set($action_id, FALSE);
  522. $this->drupalLogin($test_user);
  523. // Assign a configurable action 'System message' to the user_login trigger.
  524. $action_edit = array(
  525. 'actions_label' => $this->randomName(16),
  526. 'message' => t("You have logged in:") . $this->randomName(16),
  527. );
  528. // Configure an advanced action that we can assign.
  529. $aid = $this->configureAdvancedAction('system_message_action', $action_edit);
  530. $edit = array('aid' => drupal_hash_base64($aid));
  531. $this->drupalPost('admin/structure/trigger/user', $edit, t('Assign'), array(), array(), 'trigger-user-login-assign-form');
  532. // Verify that the action has been assigned to the correct hook.
  533. $actions = trigger_get_assigned_actions('user_login');
  534. $this->assertEqual(1, count($actions), 'One Action assigned to the hook');
  535. $this->assertEqual($actions[$aid]['label'], $action_edit['actions_label'], 'Correct action label found.');
  536. // User should get the configured message at login.
  537. $contact_user = $this->drupalCreateUser(array('access site-wide contact form'));;
  538. $this->drupalLogin($contact_user);
  539. $this->assertText($action_edit['message']);
  540. }
  541. /**
  542. * Tests triggering on comment save.
  543. */
  544. function testActionsComment() {
  545. // Assign an action to the comment save trigger.
  546. $test_user = $this->drupalCreateUser(array('administer actions'));
  547. $this->drupalLogin($test_user);
  548. $action_id = 'trigger_test_generic_action';
  549. $hash = drupal_hash_base64($action_id);
  550. $edit = array('aid' => $hash);
  551. $this->drupalPost('admin/structure/trigger/comment', $edit, t('Assign'), array(), array(), 'trigger-comment-insert-assign-form');
  552. // Set action variable to FALSE.
  553. variable_set($action_id, FALSE);
  554. // Create a node and add a comment to it.
  555. $web_user = $this->drupalCreateUser(array('create article content', 'access content', 'skip comment approval', 'post comments'));
  556. $this->drupalLogin($web_user);
  557. $node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
  558. $edit = array();
  559. $edit['subject'] = $this->randomName(10);
  560. $edit['comment_body[' . LANGUAGE_NONE . '][0][value]'] = $this->randomName(10) . ' ' . $this->randomName(10);
  561. $this->drupalGet('comment/reply/' . $node->nid);
  562. $this->drupalPost(NULL, $edit, t('Save'));
  563. // Verify that the action variable has been set.
  564. $this->assertTrue(variable_get($action_id, FALSE), 'Check that creating a comment triggered the action.');
  565. }
  566. /**
  567. * Tests triggering on taxonomy new term.
  568. */
  569. function testActionsTaxonomy() {
  570. // Assign an action to the taxonomy term save trigger.
  571. $test_user = $this->drupalCreateUser(array('administer actions'));
  572. $this->drupalLogin($test_user);
  573. $action_id = 'trigger_test_generic_action';
  574. $hash = drupal_hash_base64($action_id);
  575. $edit = array('aid' => $hash);
  576. $this->drupalPost('admin/structure/trigger/taxonomy', $edit, t('Assign'), array(), array(), 'trigger-taxonomy-term-insert-assign-form');
  577. // Set action variable to FALSE.
  578. variable_set($action_id, FALSE);
  579. // Create a taxonomy vocabulary and add a term to it.
  580. // Create a vocabulary.
  581. $vocabulary = new stdClass();
  582. $vocabulary->name = $this->randomName();
  583. $vocabulary->description = $this->randomName();
  584. $vocabulary->machine_name = drupal_strtolower($this->randomName());
  585. $vocabulary->help = '';
  586. $vocabulary->nodes = array('article' => 'article');
  587. $vocabulary->weight = mt_rand(0, 10);
  588. taxonomy_vocabulary_save($vocabulary);
  589. $term = new stdClass();
  590. $term->name = $this->randomName();
  591. $term->vid = $vocabulary->vid;
  592. taxonomy_term_save($term);
  593. // Verify that the action variable has been set.
  594. $this->assertTrue(variable_get($action_id, FALSE), 'Check that creating a taxonomy term triggered the action.');
  595. }
  596. }
  597. /**
  598. * Tests that orphaned actions are properly handled.
  599. */
  600. class TriggerOrphanedActionsTestCase extends DrupalWebTestCase {
  601. public static function getInfo() {
  602. return array(
  603. 'name' => 'Trigger orphaned actions',
  604. 'description' => 'Test triggering an action that has since been removed.',
  605. 'group' => 'Trigger',
  606. );
  607. }
  608. function setUp() {
  609. parent::setUp('trigger', 'trigger_test');
  610. }
  611. /**
  612. * Tests logic around orphaned actions.
  613. */
  614. function testActionsOrphaned() {
  615. $action = 'trigger_test_generic_any_action';
  616. $hash = drupal_hash_base64($action);
  617. // Assign an action from a disable-able module to a trigger, then pull the
  618. // trigger, and make sure the actions fire.
  619. $test_user = $this->drupalCreateUser(array('administer actions'));
  620. $this->drupalLogin($test_user);
  621. $edit = array('aid' => $hash);
  622. $this->drupalPost('admin/structure/trigger/node', $edit, t('Assign'), array(), array(), 'trigger-node-presave-assign-form');
  623. // Create an unpublished node.
  624. $web_user = $this->drupalCreateUser(array('create page content', 'edit own page content', 'access content', 'administer nodes'));
  625. $this->drupalLogin($web_user);
  626. $edit = array();
  627. $langcode = LANGUAGE_NONE;
  628. $edit["title"] = '!SimpleTest test node! ' . $this->randomName(10);
  629. $edit["body[$langcode][0][value]"] = '!SimpleTest test body! ' . $this->randomName(32) . ' ' . $this->randomName(32);
  630. $this->drupalPost('node/add/page', $edit, t('Save'));
  631. $this->assertRaw(t('!post %title has been created.', array('!post' => 'Basic page', '%title' => $edit["title"])), 'Make sure the Basic page has actually been created');
  632. // Action should have been fired.
  633. $this->assertTrue(variable_get('trigger_test_generic_any_action', FALSE), 'Trigger test action successfully fired.');
  634. // Disable the module that provides the action and make sure the trigger
  635. // doesn't white screen.
  636. module_disable(array('trigger_test'));
  637. $loaded_node = $this->drupalGetNodeByTitle($edit["title"]);
  638. $edit["body[$langcode][0][value]"] = '!SimpleTest test body! ' . $this->randomName(32) . ' ' . $this->randomName(32);
  639. $this->drupalPost("node/$loaded_node->nid/edit", $edit, t('Save'));
  640. // If the node body was updated successfully we have dealt with the
  641. // unavailable action.
  642. $this->assertRaw(t('!post %title has been updated.', array('!post' => 'Basic page', '%title' => $edit["title"])), 'Make sure the Basic page can be updated with the missing trigger function.');
  643. }
  644. }
  645. /**
  646. * Tests the unassigning of triggers.
  647. */
  648. class TriggerUnassignTestCase extends DrupalWebTestCase {
  649. public static function getInfo() {
  650. return array(
  651. 'name' => 'Trigger unassigning',
  652. 'description' => 'Tests the unassigning of triggers.',
  653. 'group' => 'Trigger',
  654. );
  655. }
  656. function setUp() {
  657. parent::setUp('trigger', 'trigger_test');
  658. $web_user = $this->drupalCreateUser(array('administer actions'));
  659. $this->drupalLogin($web_user);
  660. }
  661. /**
  662. * Tests an attempt to unassign triggers when none are assigned.
  663. */
  664. function testUnassignAccessDenied() {
  665. $this->drupalGet('admin/structure/trigger/unassign');
  666. $this->assertResponse(403, 'If there are no actions available, return access denied.');
  667. }
  668. }