| 1 |
<?php
|
| 2 |
// $Id: trigger.test,v 1.20 2009/10/13 05:37:46 webchick Exp $
|
| 3 |
|
| 4 |
class TriggerContentTestCase extends DrupalWebTestCase {
|
| 5 |
var $_cleanup_roles = array();
|
| 6 |
var $_cleanup_users = array();
|
| 7 |
|
| 8 |
public static function getInfo() {
|
| 9 |
return array(
|
| 10 |
'name' => 'Trigger content (node) actions',
|
| 11 |
'description' => 'Perform various tests with content actions.' ,
|
| 12 |
'group' => 'Trigger',
|
| 13 |
);
|
| 14 |
}
|
| 15 |
|
| 16 |
function setUp() {
|
| 17 |
parent::setUp('trigger');
|
| 18 |
}
|
| 19 |
|
| 20 |
/**
|
| 21 |
* Various tests, all in one function to assure they happen in the right order.
|
| 22 |
*/
|
| 23 |
function testActionsContent() {
|
| 24 |
global $user;
|
| 25 |
$content_actions = array('node_publish_action', 'node_unpublish_action', 'node_make_sticky_action', 'node_make_unsticky_action', 'node_promote_action', 'node_unpromote_action');
|
| 26 |
|
| 27 |
foreach ($content_actions as $action) {
|
| 28 |
$hash = md5($action);
|
| 29 |
$info = $this->actionInfo($action);
|
| 30 |
|
| 31 |
// Test 1: Assign an action to a trigger, then pull the trigger, and make sure the actions fire.
|
| 32 |
$test_user = $this->drupalCreateUser(array('administer actions'));
|
| 33 |
$this->drupalLogin($test_user);
|
| 34 |
$edit = array('aid' => $hash);
|
| 35 |
$this->drupalPost('admin/structure/trigger/node', $edit, t('Assign'));
|
| 36 |
// Create an unpublished node.
|
| 37 |
$web_user = $this->drupalCreateUser(array('create page content', 'access content', 'administer nodes'));
|
| 38 |
$this->drupalLogin($web_user);
|
| 39 |
$edit = array();
|
| 40 |
$langcode = FIELD_LANGUAGE_NONE;
|
| 41 |
$edit["title[$langcode][0][value]"] = '!SimpleTest test node! ' . $this->randomName(10);
|
| 42 |
$edit["body[$langcode][0][value]"] = '!SimpleTest test body! ' . $this->randomName(32) . ' ' . $this->randomName(32);
|
| 43 |
$edit[$info['property']] = !$info['expected'];
|
| 44 |
$this->drupalPost('node/add/page', $edit, t('Save'));
|
| 45 |
// Make sure the text we want appears.
|
| 46 |
$this->assertRaw(t('!post %title has been created.', array('!post' => 'Page', '%title' => $edit["title[$langcode][0][value]"])), t('Make sure the page has actually been created'));
|
| 47 |
// Action should have been fired.
|
| 48 |
$loaded_node = $this->drupalGetNodeByTitle($edit["title[$langcode][0][value]"]);;
|
| 49 |
$this->assertTrue($loaded_node->$info['property'] == $info['expected'], t('Make sure the @action action fired.', array('@action' => $info['name'])));
|
| 50 |
// Leave action assigned for next test
|
| 51 |
|
| 52 |
// Test 2: There should be an error when the action is assigned to the trigger twice.
|
| 53 |
$test_user = $this->drupalCreateUser(array('administer actions'));
|
| 54 |
$this->drupalLogin($test_user);
|
| 55 |
$edit = array('aid' => $hash);
|
| 56 |
$this->drupalPost('admin/structure/trigger/node', $edit, t('Assign'));
|
| 57 |
$edit = array('aid' => $hash);
|
| 58 |
$this->drupalPost('admin/structure/trigger/node', $edit, t('Assign'));
|
| 59 |
$this->assertRaw(t('The action you chose is already assigned to that trigger.'), t('Check to make sure an error occurs when assigning an action to a trigger twice.'));
|
| 60 |
|
| 61 |
// Test 3: The action should be able to be unassigned from a trigger.
|
| 62 |
$this->drupalPost('admin/structure/trigger/unassign/node/node_presave/' . $hash, array(), t('Unassign'));
|
| 63 |
$this->assertRaw(t('Action %action has been unassigned.', array('%action' => ucfirst($info['name']))), t('Check to make sure the @action action can be unassigned from the trigger.', array('@action' => $info['name'])));
|
| 64 |
$assigned = db_query("SELECT COUNT(*) FROM {trigger_assignments} WHERE aid IN (:keys)", array(':keys' => $content_actions))->fetchField();
|
| 65 |
$this->assertFalse($assigned, t('Check to make sure unassign worked properly at the database level.'));
|
| 66 |
}
|
| 67 |
}
|
| 68 |
|
| 69 |
/**
|
| 70 |
* Helper function for testActionsContent(): returns some info about each of the content actions.
|
| 71 |
*
|
| 72 |
* @param $action
|
| 73 |
* The name of the action to return info about.
|
| 74 |
* @return
|
| 75 |
* An associative array of info about the action.
|
| 76 |
*/
|
| 77 |
function actionInfo($action) {
|
| 78 |
$info = array(
|
| 79 |
'node_publish_action' => array(
|
| 80 |
'property' => 'status',
|
| 81 |
'expected' => 1,
|
| 82 |
'name' => t('publish content'),
|
| 83 |
),
|
| 84 |
'node_unpublish_action' => array(
|
| 85 |
'property' => 'status',
|
| 86 |
'expected' => 0,
|
| 87 |
'name' => t('unpublish content'),
|
| 88 |
),
|
| 89 |
'node_make_sticky_action' => array(
|
| 90 |
'property' => 'sticky',
|
| 91 |
'expected' => 1,
|
| 92 |
'name' => t('make content sticky'),
|
| 93 |
),
|
| 94 |
'node_make_unsticky_action' => array(
|
| 95 |
'property' => 'sticky',
|
| 96 |
'expected' => 0,
|
| 97 |
'name' => t('make content unsticky'),
|
| 98 |
),
|
| 99 |
'node_promote_action' => array(
|
| 100 |
'property' => 'promote',
|
| 101 |
'expected' => 1,
|
| 102 |
'name' => t('promote content to front page'),
|
| 103 |
),
|
| 104 |
'node_unpromote_action' => array(
|
| 105 |
'property' => 'promote',
|
| 106 |
'expected' => 0,
|
| 107 |
'name' => t('remove content from front page'),
|
| 108 |
),
|
| 109 |
);
|
| 110 |
return $info[$action];
|
| 111 |
}
|
| 112 |
}
|
| 113 |
|
| 114 |
/**
|
| 115 |
* Test cron trigger.
|
| 116 |
*/
|
| 117 |
class TriggerCronTestCase extends DrupalWebTestCase {
|
| 118 |
public static function getInfo() {
|
| 119 |
return array(
|
| 120 |
'name' => 'Trigger cron (system) actions',
|
| 121 |
'description' => 'Perform various tests with cron trigger.' ,
|
| 122 |
'group' => 'Trigger',
|
| 123 |
);
|
| 124 |
}
|
| 125 |
|
| 126 |
function setUp() {
|
| 127 |
parent::setUp('trigger', 'trigger_test');
|
| 128 |
}
|
| 129 |
|
| 130 |
/**
|
| 131 |
* Test assigning multiple actions to the cron trigger.
|
| 132 |
*
|
| 133 |
* This test ensures that both simple and multiple complex actions
|
| 134 |
* succeed properly. This is done in the cron trigger test because
|
| 135 |
* cron allows passing multiple actions in at once.
|
| 136 |
*/
|
| 137 |
function testActionsCron() {
|
| 138 |
// Create an administrative user.
|
| 139 |
$test_user = $this->drupalCreateUser(array('administer actions'));
|
| 140 |
$this->drupalLogin($test_user);
|
| 141 |
|
| 142 |
// Assign a non-configurable action to the cron run trigger.
|
| 143 |
$edit = array('aid' => md5('trigger_test_system_cron_action'));
|
| 144 |
$this->drupalPost('admin/structure/trigger/system', $edit, t('Assign'));
|
| 145 |
|
| 146 |
// Assign a configurable action to the cron trigger.
|
| 147 |
$hash = md5('trigger_test_system_cron_conf_action');
|
| 148 |
$action_label = $this->randomName();
|
| 149 |
$edit = array(
|
| 150 |
'actions_label' => $action_label,
|
| 151 |
'subject' => $action_label,
|
| 152 |
);
|
| 153 |
$this->drupalPost('admin/config/system/actions/configure/' . $hash, $edit, t('Save'));
|
| 154 |
$aid = db_query('SELECT aid FROM {actions} WHERE callback = :callback', array(':callback' => 'trigger_test_system_cron_conf_action'))->fetchField();
|
| 155 |
// $aid is likely 3 but if we add more uses for the sequences table in
|
| 156 |
// core it might break, so it is easier to get the value from the database.
|
| 157 |
$edit = array('aid' => md5($aid));
|
| 158 |
$this->drupalPost('admin/structure/trigger/system', $edit, t('Assign'));
|
| 159 |
|
| 160 |
// Add a second configurable action to the cron trigger.
|
| 161 |
$action_label = $this->randomName();
|
| 162 |
$edit = array(
|
| 163 |
'actions_label' => $action_label,
|
| 164 |
'subject' => $action_label,
|
| 165 |
);
|
| 166 |
$this->drupalPost('admin/config/system/actions/configure/' . $hash, $edit, t('Save'));
|
| 167 |
$edit = array('aid' => md5($aid + 1));
|
| 168 |
$this->drupalPost('admin/structure/trigger/system', $edit, t('Assign'));
|
| 169 |
|
| 170 |
// Force a cron run.
|
| 171 |
$this->cronRun();
|
| 172 |
|
| 173 |
// Make sure the non-configurable action has fired.
|
| 174 |
$action_run = variable_get('trigger_test_system_cron_action', FALSE);
|
| 175 |
$this->assertTrue($action_run, t('Check that the cron run triggered the test action.'));
|
| 176 |
|
| 177 |
// Make sure that both configurable actions have fired.
|
| 178 |
$action_run = variable_get('trigger_test_system_cron_conf_action', 0) == 2;
|
| 179 |
$this->assertTrue($action_run, t('Check that the cron run triggered both complex actions.'));
|
| 180 |
}
|
| 181 |
}
|
| 182 |
|
| 183 |
/**
|
| 184 |
* Test other triggers.
|
| 185 |
*/
|
| 186 |
class TriggerOtherTestCase extends DrupalWebTestCase {
|
| 187 |
var $_cleanup_roles = array();
|
| 188 |
var $_cleanup_users = array();
|
| 189 |
|
| 190 |
public static function getInfo() {
|
| 191 |
return array(
|
| 192 |
'name' => 'Trigger other actions',
|
| 193 |
'description' => 'Test triggering of user, comment, taxonomy actions.' ,
|
| 194 |
'group' => 'Trigger',
|
| 195 |
);
|
| 196 |
}
|
| 197 |
|
| 198 |
function setUp() {
|
| 199 |
parent::setUp('trigger', 'trigger_test');
|
| 200 |
}
|
| 201 |
|
| 202 |
/**
|
| 203 |
* Test triggering on user create.
|
| 204 |
*/
|
| 205 |
function testActionsUser() {
|
| 206 |
// Assign an action to the create user trigger.
|
| 207 |
$test_user = $this->drupalCreateUser(array('administer actions'));
|
| 208 |
$this->drupalLogin($test_user);
|
| 209 |
$action_id = 'trigger_test_generic_action';
|
| 210 |
$hash = md5($action_id);
|
| 211 |
$edit = array('aid' => $hash);
|
| 212 |
$this->drupalPost('admin/structure/trigger/user', $edit, t('Assign'));
|
| 213 |
|
| 214 |
// Set action variable to FALSE.
|
| 215 |
variable_set( $action_id, FALSE );
|
| 216 |
|
| 217 |
// Create an unblocked user
|
| 218 |
$web_user = $this->drupalCreateUser(array('administer users'));
|
| 219 |
$this->drupalLogin($web_user);
|
| 220 |
$name = $this->randomName();
|
| 221 |
$pass = user_password();
|
| 222 |
$edit = array();
|
| 223 |
$edit['name'] = $name;
|
| 224 |
$edit['mail'] = $name . '@example.com';
|
| 225 |
$edit['pass[pass1]'] = $pass;
|
| 226 |
$edit['pass[pass2]'] = $pass;
|
| 227 |
$edit['status'] = 1;
|
| 228 |
$this->drupalPost('admin/people/create', $edit, t('Create new account'));
|
| 229 |
|
| 230 |
// Verify that the action variable has been set.
|
| 231 |
$this->assertTrue(variable_get($action_id, FALSE), t('Check that creating a user triggered the test action.'));
|
| 232 |
|
| 233 |
// Reset the action variable.
|
| 234 |
variable_set( $action_id, FALSE );
|
| 235 |
}
|
| 236 |
|
| 237 |
/**
|
| 238 |
* Test triggering on comment save.
|
| 239 |
*/
|
| 240 |
function testActionsComment() {
|
| 241 |
// Assign an action to the comment save trigger.
|
| 242 |
$test_user = $this->drupalCreateUser(array('administer actions'));
|
| 243 |
$this->drupalLogin($test_user);
|
| 244 |
$action_id = 'trigger_test_generic_action';
|
| 245 |
$hash = md5($action_id);
|
| 246 |
$edit = array('aid' => $hash);
|
| 247 |
$this->drupalPost('admin/structure/trigger/comment', $edit, t('Assign'));
|
| 248 |
|
| 249 |
// Set action variable to FALSE.
|
| 250 |
variable_set( $action_id, FALSE );
|
| 251 |
|
| 252 |
// Create a node and add a comment to it.
|
| 253 |
$web_user = $this->drupalCreateUser(array('create article content', 'access content', 'post comments without approval', 'post comments'));
|
| 254 |
$this->drupalLogin($web_user);
|
| 255 |
$node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
|
| 256 |
$edit = array();
|
| 257 |
$edit['subject'] = $this->randomName(10);
|
| 258 |
$edit['comment'] = $this->randomName(10) . ' ' . $this->randomName(10);
|
| 259 |
$this->drupalGet('comment/reply/' . $node->nid);
|
| 260 |
$this->drupalPost(NULL, $edit, t('Save'));
|
| 261 |
|
| 262 |
// Verify that the action variable has been set.
|
| 263 |
$this->assertTrue(variable_get($action_id, FALSE), t('Check that creating a comment triggered the action.'));
|
| 264 |
}
|
| 265 |
|
| 266 |
/**
|
| 267 |
* Test triggering on taxonomy new term.
|
| 268 |
*/
|
| 269 |
function testActionsTaxonomy() {
|
| 270 |
// Assign an action to the taxonomy term save trigger.
|
| 271 |
$test_user = $this->drupalCreateUser(array('administer actions'));
|
| 272 |
$this->drupalLogin($test_user);
|
| 273 |
$action_id = 'trigger_test_generic_action';
|
| 274 |
$hash = md5($action_id);
|
| 275 |
$edit = array('aid' => $hash);
|
| 276 |
$this->drupalPost('admin/structure/trigger/taxonomy', $edit, t('Assign'));
|
| 277 |
|
| 278 |
// Set action variable to FALSE.
|
| 279 |
variable_set( $action_id, FALSE );
|
| 280 |
|
| 281 |
// Create a taxonomy vocabulary and add a term to it.
|
| 282 |
|
| 283 |
// Create a vocabulary.
|
| 284 |
$vocabulary = new stdClass();
|
| 285 |
$vocabulary->name = $this->randomName();
|
| 286 |
$vocabulary->description = $this->randomName();
|
| 287 |
$vocabulary->machine_name = drupal_strtolower($this->randomName());
|
| 288 |
$vocabulary->help = '';
|
| 289 |
$vocabulary->nodes = array('article' => 'article');
|
| 290 |
$vocabulary->weight = mt_rand(0, 10);
|
| 291 |
taxonomy_vocabulary_save($vocabulary);
|
| 292 |
|
| 293 |
$term = new stdClass();
|
| 294 |
$term->name = $this->randomName();
|
| 295 |
$term->vid = $vocabulary->vid;
|
| 296 |
taxonomy_term_save($term);
|
| 297 |
|
| 298 |
// Verify that the action variable has been set.
|
| 299 |
$this->assertTrue(variable_get($action_id, FALSE), t('Check that creating a taxonomy term triggered the action.'));
|
| 300 |
}
|
| 301 |
}
|