Index: tests/content_actions.test
===================================================================
RCS file: /cvs/drupal/contributions/modules/simpletest/tests/content_actions.test,v
retrieving revision 1.1
diff -u -p -r1.1 content_actions.test
--- tests/content_actions.test	27 Dec 2007 07:37:50 -0000	1.1
+++ tests/content_actions.test	1 Jan 2008 23:04:17 -0000
@@ -15,22 +15,18 @@ class ActionsContentTest extends  Drupal
    * Setup function
    */
   function setup() {
+    // 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() {
-    global $user;
-
-    $this->drupalModuleEnable('trigger');
-
     $content_actions = array('node_publish_action', 'node_unpublish_action', 'node_make_sticky_action', 'node_make_unsticky_action', 'node_promote_action', 'node_unpromote_action');
 
-    $hash = md5('node_publish_action');
-
     $not_clean = db_result(db_query("SELECT COUNT(*) FROM {trigger_assignments} WHERE aid IN ('". implode("','", $content_actions) ."')"));
-    
+
     if ($not_clean) {
       $this->assertFalse($not_clean, t('Actions were already assigned to the trigger. Unassign all content triggers before attempting to run again.'));
       return;
@@ -38,51 +34,127 @@ class ActionsContentTest extends  Drupal
     else {
       $this->assertFalse($not_clean, t('Actions were already not assigned to the trigger.'));
     }
-
     // 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'));
+    $test_user = $this->drupalCreateUserRolePerm(array('administer actions', 'create page content', 'administer nodes', 'access administration pages'));
     $this->drupalLoginUser($test_user);
 
-    // Set action id to "publish post".
-    $edit = array('aid' => $hash);
-    $this->drupalPostRequest('admin/build/trigger/node', $edit, 'Assign');
-
-    // Create an unpublished node.
-    $node->body      = $this->randomName(32);
-    $node->title     = $this->randomName(8);
-    $node->teaser    = $node->body;
-    $node->comment   = 2;
-    $node->format    = 1;
-    $node->name      = $test_user->name;
-    $node->uid       = $test_user->uid;
-    $node->promote   = 0;
-    $node->revision  = 0;
-    $node->status    = 0;
-    $node->sticky    = 0;
-    $node->type      = 'page';
-    $node->revisions = NULL;
-    $node->taxonomy  = NULL;
-    node_save($node);
-    // Node should be published automatically.
-    $loaded_node = node_load($node->nid);
-    $this->assertTrue($loaded_node->status == 1, 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' => 'Publish post')), t('Check to make sure action can be unassigned from trigger.'));
+    foreach ($content_actions as $action) {
+    // Set action id to the md5 of the action
+      $hash = md5($action);
+      $edit = array('aid' => $hash);
+      $this->drupalPostRequest('admin/build/trigger/node', $edit, 'Assign');
+
+      // Create an unpublished node.
+      $edit = array();
+      $edit['body']      = $this->randomName(32);
+      $edit['title']     = $this->randomName(18);
+      $edit['promote']   = FALSE;
+      $edit['status']    = TRUE;
+      $edit['sticky']    = FALSE;
+
+      $this->modifyNode($action, $edit);
+      $this->drupalPostRequest('node/add/page', $edit, 'Save');
+      $node = node_load(array('title' => $edit['title']));
+
+      // Action should have been fired.
+      $this->assertTrue($this->actionWorked($action, $node), t('Check to make sure the @action action is fired.', array('@action' => $this->actionName($action))));
+
+      // 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.
+      $edit = array();
+      $edit['body']      = $this->randomName(32);
+      $edit['title']     = $this->randomName(18);
+      $edit['promote']   = FALSE;
+      $edit['status']    = TRUE;
+      $edit['sticky']    = FALSE;
+
+      $this->modifyNode($action, $edit);      
+      $this->drupalPostRequest('node/add/page', $edit, 'Save');
+      $node2 = node_load(array('title' => $edit['title']));
+
+      // Action should NOT work.
+      $this->assertFalse($this->actionWorked($action, $node2), t('Check to make sure the @action action is NOT fired.', array('@action' => $this->actionName($action))));
+
+      // Cleanup time!
+      node_delete($node->nid);
+      node_delete($node2->nid);
+    }
 
     $assigned = db_result(db_query("SELECT COUNT(*) FROM {trigger_assignments} WHERE aid IN ('". implode("','", $content_actions) ."')"));
     $this->assertFalse($assigned, t('Check to make sure unassign worked properly at the database level.'));
   }
+
+  /**
+   * Helper function: modifies a node to test an action. Sets appropriate variables so that the action can change them.
+   */
+  function modifyNode($action, &$edit) {
+    switch ($action) {
+      case 'node_unpromote_action':
+        $edit['promote'] = TRUE;
+        return;
+      case 'node_make_unsticky_action':
+        $edit['sticky'] = TRUE;
+        return;
+      case 'node_publish_action':
+        $edit['status'] = FALSE;
+        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);
+    }
+  }
 }
+?>
\ No newline at end of file
