summaryrefslogtreecommitdiff
path: root/modules/trigger/trigger.test
blob: fce5d460e93a2ec43b42effc9efc611a7fe9e85e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
// $Id$

class TriggerContentTestCase extends DrupalWebTestCase {
  var $_cleanup_roles = array();
  var $_cleanup_users = array();

  public static function getInfo() {
    return array(
      'name' => 'Trigger content (node) actions',
      'description' => 'Perform various tests with content actions.' ,
      'group' => 'Trigger',
    );
  }

  function setUp() {
    parent::setUp('trigger');
  }

  /**
   * Various tests, all in one function to assure they happen in the right order.
   */
  function testActionsContent() {
    global $user;
    $content_actions = array('node_publish_action', 'node_unpublish_action', 'node_make_sticky_action', 'node_make_unsticky_action', 'node_promote_action', 'node_unpromote_action');

    foreach ($content_actions as $action) {
      $hash = md5($action);
      $info = $this->actionInfo($action);

      // Test 1: Assign an action to a trigger, then pull the trigger, and make sure the actions fire.
      $test_user = $this->drupalCreateUser(array('administer actions'));
      $this->drupalLogin($test_user);
      $edit = array('aid' => $hash);
      $this->drupalPost('admin/structure/trigger/node', $edit, t('Assign'));
      // Create an unpublished node.
      $web_user = $this->drupalCreateUser(array('create page content', 'access content', 'administer nodes'));
      $this->drupalLogin($web_user);
      $edit = array();
      $langcode = LANGUAGE_NONE;
      $edit["title"] = '!SimpleTest test node! ' . $this->randomName(10);
      $edit["body[$langcode][0][value]"] = '!SimpleTest test body! ' . $this->randomName(32) . ' ' . $this->randomName(32);
      $edit[$info['property']] = !$info['expected'];
      $this->drupalPost('node/add/page', $edit, t('Save'));
      // Make sure the text we want appears.
      $this->assertRaw(t('!post %title has been created.', array('!post' => 'Basic page', '%title' => $edit["title"])), t('Make sure the Basic page has actually been created'));
      // Action should have been fired.
      $loaded_node = $this->drupalGetNodeByTitle($edit["title"]);
      $this->assertTrue($loaded_node->$info['property'] == $info['expected'], t('Make sure the @action action fired.', array('@action' => $info['name'])));
      // Leave action assigned for next test

      // Test 2: There should be an error when the action is assigned to the trigger twice.
      $test_user = $this->drupalCreateUser(array('administer actions'));
      $this->drupalLogin($test_user);
      $edit = array('aid' => $hash);
      $this->drupalPost('admin/structure/trigger/node', $edit, t('Assign'));
      $edit = array('aid' => $hash);
      $this->drupalPost('admin/structure/trigger/node', $edit, t('Assign'));
      $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.'));

      // Test 3: The action should be able to be unassigned from a trigger.
      $this->drupalPost('admin/structure/trigger/unassign/node/node_presave/' . $hash, array(), t('Unassign'));
      $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'])));
      $assigned = db_query("SELECT COUNT(*) FROM {trigger_assignments} WHERE aid IN (:keys)", array(':keys' => $content_actions))->fetchField();
      $this->assertFalse($assigned, t('Check to make sure unassign worked properly at the database level.'));
    }
  }

  /**
   * Helper function for testActionsContent(): returns some info about each of the content actions.
   *
   * @param $action
   *   The name of the action to return info about.
   * @return
   *   An associative array of info about the action.
   */
  function actionInfo($action) {
    $info = array(
      'node_publish_action' => array(
        'property' => 'status',
        'expected' => 1,
        'name' => t('publish content'),
      ),
      'node_unpublish_action' => array(
        'property' => 'status',
        'expected' => 0,
        'name' => t('unpublish content'),
      ),
      'node_make_sticky_action' => array(
        'property' => 'sticky',
        'expected' => 1,
        'name' => t('make content sticky'),
      ),
      'node_make_unsticky_action' => array(
        'property' => 'sticky',
        'expected' => 0,
        'name' => t('make content unsticky'),
      ),
      'node_promote_action' => array(
        'property' => 'promote',
        'expected' => 1,
        'name' => t('promote content to front page'),
      ),
      'node_unpromote_action' => array(
        'property' => 'promote',
        'expected' => 0,
        'name' => t('remove content from front page'),
      ),
    );
    return $info[$action];
  }
}

/**
 * Test cron trigger.
 */
class TriggerCronTestCase extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Trigger cron (system) actions',
      'description' => 'Perform various tests with cron trigger.' ,
      'group' => 'Trigger',
    );
  }

  function setUp() {
    parent::setUp('trigger', 'trigger_test');
  }

  /**
   * Test assigning multiple actions to the cron trigger.
   *
   * This test ensures that both simple and multiple complex actions
   * succeed properly. This is done in the cron trigger test because
   * cron allows passing multiple actions in at once.
   */
  function testActionsCron() {
    // Create an administrative user.
    $test_user = $this->drupalCreateUser(array('administer actions'));
    $this->drupalLogin($test_user);

    // Assign a non-configurable action to the cron run trigger.
    $edit = array('aid' => md5('trigger_test_system_cron_action'));
    $this->drupalPost('admin/structure/trigger/system', $edit, t('Assign'));

    // Assign a configurable action to the cron trigger.
    $hash = md5('trigger_test_system_cron_conf_action');
    $action_label = $this->randomName();
    $edit = array(
      'actions_label' => $action_label,
      'subject' => $action_label,
    );
    $this->drupalPost('admin/config/system/actions/configure/' . $hash, $edit, t('Save'));
    $aid = db_query('SELECT aid FROM {actions} WHERE callback = :callback', array(':callback' => 'trigger_test_system_cron_conf_action'))->fetchField();
    // $aid is likely 3 but if we add more uses for the sequences table in
    // core it might break, so it is easier to get the value from the database.
    $edit = array('aid' => md5($aid));
    $this->drupalPost('admin/structure/trigger/system', $edit, t('Assign'));

    // Add a second configurable action to the cron trigger.
    $action_label = $this->randomName();
    $edit = array(
      'actions_label' => $action_label,
      'subject' => $action_label,
    );
    $this->drupalPost('admin/config/system/actions/configure/' . $hash, $edit, t('Save'));
    $edit = array('aid' => md5($aid + 1));
    $this->drupalPost('admin/structure/trigger/system', $edit, t('Assign'));

    // Force a cron run.
    $this->cronRun();

    // Make sure the non-configurable action has fired.
    $action_run = variable_get('trigger_test_system_cron_action', FALSE);
    $this->assertTrue($action_run, t('Check that the cron run triggered the test action.'));

    // Make sure that both configurable actions have fired.
    $action_run = variable_get('trigger_test_system_cron_conf_action', 0) == 2;
    $this->assertTrue($action_run, t('Check that the cron run triggered both complex actions.'));
  }
}

/**
 * Test other triggers.
 */
class TriggerOtherTestCase extends DrupalWebTestCase {
  var $_cleanup_roles = array();
  var $_cleanup_users = array();

  public static function getInfo() {
    return array(
      'name' => 'Trigger other actions',
      'description' => 'Test triggering of user, comment, taxonomy actions.' ,
      'group' => 'Trigger',
    );
  }

  function setUp() {
    parent::setUp('trigger', 'trigger_test');
  }

  /**
   * Test triggering on user create.
   */
  function testActionsUser() {
    // Assign an action to the create user trigger.
    $test_user = $this->drupalCreateUser(array('administer actions'));
    $this->drupalLogin($test_user);
    $action_id = 'trigger_test_generic_action';
    $hash = md5($action_id);
    $edit = array('aid' => $hash);
    $this->drupalPost('admin/structure/trigger/user', $edit, t('Assign'));

    // Set action variable to FALSE.
    variable_set( $action_id, FALSE );

    // Create an unblocked user
    $web_user = $this->drupalCreateUser(array('administer users'));
    $this->drupalLogin($web_user);
    $name = $this->randomName();
    $pass = user_password();
    $edit = array();
    $edit['name'] = $name;
    $edit['mail'] = $name . '@example.com';
    $edit['pass[pass1]'] = $pass;
    $edit['pass[pass2]'] = $pass;
    $edit['status'] = 1;
    $this->drupalPost('admin/people/create', $edit, t('Create new account'));

    // Verify that the action variable has been set.
    $this->assertTrue(variable_get($action_id, FALSE), t('Check that creating a user triggered the test action.'));

    // Reset the action variable.
    variable_set( $action_id, FALSE );
  }

  /**
   * Test triggering on comment save.
   */
  function testActionsComment() {
    // Assign an action to the comment save trigger.
    $test_user = $this->drupalCreateUser(array('administer actions'));
    $this->drupalLogin($test_user);
    $action_id = 'trigger_test_generic_action';
    $hash = md5($action_id);
    $edit = array('aid' => $hash);
    $this->drupalPost('admin/structure/trigger/comment', $edit, t('Assign'));

    // Set action variable to FALSE.
    variable_set( $action_id, FALSE );

    // Create a node and add a comment to it.
    $web_user = $this->drupalCreateUser(array('create article content', 'access content', 'post comments without approval', 'post comments'));
    $this->drupalLogin($web_user);
    $node = $this->drupalCreateNode(array('type' => 'article', 'promote' => 1));
    $edit = array();
    $edit['subject'] = $this->randomName(10);
    $edit['comment_body[' . LANGUAGE_NONE . '][0][value]'] = $this->randomName(10) . ' ' . $this->randomName(10);
    $this->drupalGet('comment/reply/' . $node->nid);
    $this->drupalPost(NULL, $edit, t('Save'));

    // Verify that the action variable has been set.
    $this->assertTrue(variable_get($action_id, FALSE), t('Check that creating a comment triggered the action.'));
  }

  /**
   * Test triggering on taxonomy new term.
   */
  function testActionsTaxonomy() {
    // Assign an action to the taxonomy term save trigger.
    $test_user = $this->drupalCreateUser(array('administer actions'));
    $this->drupalLogin($test_user);
    $action_id = 'trigger_test_generic_action';
    $hash = md5($action_id);
    $edit = array('aid' => $hash);
    $this->drupalPost('admin/structure/trigger/taxonomy', $edit, t('Assign'));

    // Set action variable to FALSE.
    variable_set( $action_id, FALSE );

    // Create a taxonomy vocabulary and add a term to it.

    // Create a vocabulary.
    $vocabulary = new stdClass();
    $vocabulary->name = $this->randomName();
    $vocabulary->description = $this->randomName();
    $vocabulary->machine_name = drupal_strtolower($this->randomName());
    $vocabulary->help = '';
    $vocabulary->nodes = array('article' => 'article');
    $vocabulary->weight = mt_rand(0, 10);
    taxonomy_vocabulary_save($vocabulary);

    $term = new stdClass();
    $term->name = $this->randomName();
    $term->vid = $vocabulary->vid;
    taxonomy_term_save($term);

    // Verify that the action variable has been set.
    $this->assertTrue(variable_get($action_id, FALSE), t('Check that creating a taxonomy term triggered the action.'));
  }
}