t('Actions content'), 'desc' => t('Perform various tests with content actions.') , 'group' => 'Actions'); } /** * Setup function */ function setup() { global $user; // Enable trigger module. Should automatically disable at the end of test if not previously enabled. $this->drupalModuleEnable('trigger'); } /** * Various tests, all in one function to assure they happen in the right order. */ function testActionsContent() { $this->setup(); // Test 1: Assign an action to a trigger, then pull the trigger, and make sure the actions fire. (Wow, those metaphors work out nicely). $test_user = $this->drupalCreateUserRolePerm(array('administer actions', 'create page content')); $this->drupalLoginUser($test_user); foreach (array('node_publish_action', 'node_unpublish_action', 'node_make_sticky_action', 'node_make_unsticky_action', 'node_promote_action', 'node_unpromote_action') as $action) { // Set action id to "publish post". $hash = md5($action); $edit = array('aid' => $hash); $this->drupalPostRequest('admin/build/trigger/node', $edit, 'Assign'); // Create an unpublished node. $node->body = $this->randomName(32 + $i); $node->title = $this->randomName(8 + $i); $node->teaser = $node->body; $node->comment = 2; $node->created = time(); $node->date = format_date($node->created, 'custom', 'Y-m-d H:i:s O'); $node->format = 1; $node->moderate = 0; $node->name = $test_user->name; $node->uid = $test_user->uid; $node->promote = 0; $node->revision = 0; $node->status = 1; $node->sticky = 0; $node->type = 'page'; $node->revisions = NULL; $node->changed = $node->created; $node->taxonomy = NULL; $this->modifyNode($action, $node); node_save($node); // Node should be published automatically. $this->assertTrue($this->actionWorked($action, $node), t('Check to make sure the node is automatically published')); // Leave action assigned for next test // Test 2: There should be an error when the action is assigned to the trigger twice. $edit = array('aid' => $hash); $this->drupalPostRequest('admin/build/trigger/node', $edit, 'Assign'); $this->assertWantedRaw(t('The action you choose is already assigned to that trigger.'), t('Check to make sure an error occurs when assigning an action to a trigger twice.')); // Test 3: The action should be able to be unassigned from a trigger. // This effectively cleans up as well. $this->drupalPostRequest('admin/build/trigger/unassign/nodeapi/presave/'. $hash, array(), 'Unassign'); $this->assertWantedRaw(t('Action %action has been unassigned.', array('%action' => $this->actionName($action))), t('Check to make sure action can be unassigned from trigger.')); // Test 3 followup: Make sure it's REALLY been unassigned. $node2->body = $this->randomName(32 + $i); $node2->title = $this->randomName(8 + $i); $node2->teaser = $node->body; $node2->comment = 2; $node2->created = time(); $node2->date = format_date($node->created, 'custom', 'Y-m-d H:i:s O'); $node2->format = 1; $node2->moderate = 0; $node2->name = $test_user->name; $node2->uid = $test_user->uid; $node2->promote = 0; $node2->revision = 0; $node2->status = 1; $node2->sticky = 0; $node2->type = 'page'; $node2->revisions = NULL; $node2->changed = $node->created; $node2->taxonomy = NULL; $this->modifyNode($action, $node2); node_save($node2); // Action should NOT work. $this->assertTrue(!$this->actionWorked($action, $node2), t('Check to make sure the node is NOT automatically published')); // Cleanup time! node_delete($node->nid); node_delete($node2->nid); unset($node); unset($node2); } } /** * Helper function: modifies a node to test an action. Sets appropriate variables so that the action can change them. */ function modifyNode($action, &$node) { switch ($action) { case 'node_unpromote_action': $node->promote = 1; return; case 'node_make_unsticky_action': $node->sticky = 1; return; case 'node_publish_action': $node->status = 0; return; default: return; } } /** * Helper function: Returns the name of a given action's function. */ function actionName($action) { switch ($action) { case 'node_publish_action': return t('Publish post'); case 'node_unpublish_action': return t('Unpublish post'); case 'node_make_sticky_action': return t('Make post sticky'); case 'node_make_unsticky_action': return t('Make post unsticky'); case 'node_promote_action': return t('Promote post to front page'); case 'node_unpromote_action': return t('Remove post from front page'); } } /** * Helper function: Determine whether the action fired or not. */ function actionWorked($action, $node) { switch ($action) { case 'node_publish_action': return ($node->status == 1); case 'node_unpublish_action': return ($node->status == 0); case 'node_make_sticky_action': return ($node->sticky == 1); case 'node_make_unsticky_action': return ($node->sticky == 0); case 'node_promote_action': return ($node->promote == 1); case 'node_unpromote_action': return ($node->promote == 0); } } } ?>