Project

General

Profile

Paste
Download (71 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / flag / tests / flag.test @ 4cfd8be6

1
<?php
2

    
3
/**
4
 * @file
5
 * Tests for the Flag module.
6
 */
7

    
8
/**
9
 * Base class for our tests with common methods.
10
 */
11
abstract class FlagTestCaseBase extends DrupalWebTestCase {
12

    
13
  /**
14
   * Helper to create a flag from an array of data and clear caches etc.
15
   *
16
   * @param $flag_data
17
   *  An array of flag data.
18
   *
19
   * @return
20
   *  The flag object.
21
   */
22
  function createFlag($flag_data) {
23
    $flag = flag_flag::factory_by_array($flag_data);
24
    $flag->save();
25
    // Reset our cache so our permissions show up.
26
    drupal_static_reset('flag_get_flags');
27

    
28
    // Reset permissions so that permissions for this flag are available.
29
    $this->checkPermissions(array(), TRUE);
30

    
31
    return $flag;
32
  }
33

    
34
}
35

    
36
/**
37
 * Test CRUD operations on Flagging entities.
38
 */
39
class FlagFlaggingCRUDTestCase extends FlagTestCaseBase {
40

    
41
  /**
42
   * Implements getInfo().
43
   */
44
  public static function getInfo() {
45
    return array(
46
      'name' => 'CRUD API',
47
      'description' => 'Basic CRUD operations on flagging entities.',
48
      'group' => 'Flag',
49
    );
50
  }
51

    
52
  /**
53
   * Implements setUp().
54
   */
55
  function setUp() {
56
    parent::setUp('flag');
57

    
58
    $flag_data = array(
59
      'entity_type' => 'node',
60
      'name' => 'test_flag',
61
      'title' => 'Test Flag',
62
      'global' => 0,
63
      'types' => array(
64
        0 => 'article',
65
      ),
66
      'flag_short' => 'Flag this item',
67
      'flag_long' => '',
68
      'flag_message' => '',
69
      'unflag_short' => 'Unflag this item',
70
      'unflag_long' => '',
71
      'unflag_message' => '',
72
      'unflag_denied_text' => 'You may not unflag this item',
73
      'link_type' => 'normal',
74
      'weight' => 0,
75
      'show_on_form' => 0,
76
      'access_author' => '',
77
      'show_contextual_link' => 0,
78
      'show_in_links' => array(
79
        'full' => 1,
80
        'teaser' => 1,
81
      ),
82
      'i18n' => 0,
83
      'api_version' => 3,
84
    );
85
    $this->flag = $this->createFlag($flag_data);
86

    
87
    // Create test user who can flag and unflag.
88
    $this->flag_unflag_user = $this->drupalCreateUser(array('flag test_flag', 'unflag test_flag'));
89
    $this->drupalLogin($this->flag_unflag_user);
90
  }
91

    
92
  /**
93
   * Test creation of a flagging entity with flagging_save().
94
   */
95
  function testFlaggingCreate() {
96
    // Create an article node that we try to create a flagging entity for.
97
    $title = $this->randomName(8);
98
    $node = array(
99
      'title' => $title,
100
      'body' => array(LANGUAGE_NONE => array(array('value' => $this->randomName(32)))),
101
      'uid' => 1,
102
      'type' => 'article',
103
      'is_new' => TRUE,
104
    );
105
    $node = node_submit((object) $node);
106
    node_save($node);
107

    
108
    // Create a flagging entity and save it.
109
    $flagging = array(
110
      'fid' => $this->flag->fid,
111
      'entity_type' => 'node',
112
      'entity_id' => $node->nid,
113
      'uid' => $this->flag_unflag_user->uid,
114
    );
115

    
116
    $flagging = (object) $flagging;
117
    flagging_save($flagging);
118

    
119
    // Test flagging has a flagging_id
120
    $this->assertTrue(!empty($flagging->flagging_id), 'The flagging entity has an entity id.');
121

    
122
    // Test the database record exists.
123
    $result = db_query("SELECT * FROM {flagging} WHERE fid = :fid AND entity_id = :nid AND uid = :uid", array(
124
      ':fid' => $this->flag->fid,
125
      ':nid' => $node->nid,
126
      ':uid' => $this->flag_unflag_user->uid,
127
    ));
128
    $records = $result->fetchAll();
129
    $this->assertTrue(count($records), 'The flagging record exists in the database.');
130

    
131
    // Test node is flagged.
132
    // The current user is not the same as the user logged into the internal
133
    // browser, so we have to pass the UID param explicitly.
134
    $this->assertTrue($this->flag->is_flagged($node->nid, $this->flag_unflag_user->uid), 'The node has been flagged by creating the flagging.');
135
  }
136

    
137
  /**
138
   * Test throwing of exceptions with flagging_save().
139
   */
140
  function testFlaggingCreateException() {
141
    // Create an article node that we try to create a flagging entity for.
142
    $title = $this->randomName(8);
143
    $node = array(
144
      'title' => $title,
145
      'body' => array(LANGUAGE_NONE => array(array('value' => $this->randomName(32)))),
146
      'uid' => 1,
147
      'type' => 'article',
148
      'is_new' => TRUE,
149
    );
150
    $node = node_submit((object) $node);
151
    node_save($node);
152

    
153
    // Create test user who can't use this flag.
154
    $no_flag_user = $this->drupalCreateUser(array());
155

    
156
    // Create a flagging entity with that tries to perform an flagging action
157
    // that is not permitted.
158
    $flagging = array(
159
      'fid' => $this->flag->fid,
160
      'entity_type' => 'node',
161
      'entity_id' => $node->nid,
162
      'uid' => $no_flag_user->uid,
163
    );
164

    
165
    $flagging = (object) $flagging;
166
    try {
167
      flagging_save($flagging);
168
      $this->fail(t('Expected exception has not been thrown.'));
169
    }
170
    catch (Exception $e) {
171
      $this->pass(t('Expected exception has been thrown.'));
172
    }
173
  }
174

    
175
  /**
176
   * Test creation of a flagging entity with flagging_save().
177
   */
178
  function testFlaggingUpdate() {
179
    // Create an article node that we try to create a flagging entity for.
180
    $title = $this->randomName(8);
181
    $node = array(
182
      'title' => $title,
183
      'body' => array(LANGUAGE_NONE => array(array('value' => $this->randomName(32)))),
184
      'uid' => 1,
185
      'type' => 'article',
186
      'is_new' => TRUE,
187
    );
188
    $node = node_submit((object) $node);
189
    node_save($node);
190

    
191
    // Flag the node as the user.
192
    $flag = flag_get_flag('test_flag');
193
    $flag->flag('flag', $node->nid, $this->flag_unflag_user);
194

    
195
    // Get the flagging record back from the database.
196
    $result = db_query("SELECT * FROM {flagging} WHERE fid = :fid AND entity_id = :nid AND uid = :uid", array(
197
      ':fid' => $this->flag->fid,
198
      ':nid' => $node->nid,
199
      ':uid' => $this->flag_unflag_user->uid,
200
    ));
201
    $record = $result->fetchObject();
202

    
203
    // Load the flagging entity we just created.
204
    $flagging = flagging_load($record->flagging_id);
205

    
206
    // Save it, as if we were updating field values.
207
    flagging_save($flagging);
208

    
209
    // This should have no effect: the node should still be flagged.
210
    $this->assertTrue($this->flag->is_flagged($node->nid, $this->flag_unflag_user->uid), 'The node is still flagged after updating the flagging.');
211
  }
212

    
213
}
214

    
215
/**
216
 * Test Flag admin UI.
217
 */
218
class FlagAdminTestCase extends FlagTestCaseBase {
219
  public $_flag = FALSE;
220

    
221
  /**
222
   * Implements getInfo().
223
   */
224
  public static function getInfo() {
225
    return array(
226
      'name' => 'Admin UI',
227
      'description' => 'Add, edit and delete flags.',
228
      'group' => 'Flag',
229
    );
230
  }
231

    
232
  /**
233
   * Implements setUp().
234
   */
235
  function setUp() {
236
    parent::setUp('flag');
237

    
238
    // Create and login user.
239
    $admin_user = $this->drupalCreateUser(array('access administration pages', 'administer flags'));
240
    $this->drupalLogin($admin_user);
241
  }
242

    
243
  /**
244
   * Create a flag through the UI and ensure that it is saved properly.
245
   */
246
  function testFlagAdmin() {
247
    // Add a new flag using the UI.
248
    $this->drupalGet(FLAG_ADMIN_PATH . '/add/node');
249

    
250
    // Check the form has the expected defaults.
251
    $this->assertFieldByName('flag_short', 'Flag this item', "The flag message default value shows in the form.");
252
    $this->assertFieldByName('unflag_short', 'Unflag this item', "The unflag message default value shows in the form.");
253
    $this->assertFieldByName('show_in_links[full]', 'full', "The view mode option is set to the node 'full' view mode by default.");
254
    $this->assertFieldByName('show_in_links[teaser]', 'teaser', "The view mode option is set to the node 'teaser' view mode by default.");
255

    
256
    $edit = array(
257
      'name' => drupal_strtolower($this->randomName()),
258
      'title' => $this->randomName(),
259
      'flag_short'          => 'flag short [node:nid]',
260
      'flag_long'           => 'flag long [node:nid]',
261
      'flag_message'        => 'flag message [node:nid]',
262
      'unflag_short'        => 'unflag short [node:nid]',
263
      'unflag_long'         => 'unflag long [node:nid]',
264
      'unflag_message'      => 'unflag message [node:nid]',
265
      'roles[flag][2]' => TRUE,
266
      'roles[unflag][2]' => TRUE,
267
      'types[article]' => FALSE,
268
      'types[page]' => TRUE,
269
      'show_in_links[full]' => FALSE,
270
      'show_in_links[teaser]' => FALSE,
271
      'show_in_links[rss]' => FALSE,
272
      'show_in_links[search_index]' => FALSE,
273
      'show_in_links[search_result]' => FALSE,
274
      'show_on_form' => FALSE,
275
      'link_type' => 'toggle',
276
    );
277
    $saved = $edit;
278
    $saved['roles'] = array('flag' => array(2), 'unflag' => array(2));
279
    $saved['types'] = array('page');
280
    $saved['show_in_links'] = array(
281
      'full' => 0,
282
      'teaser' => 0,
283
      'rss' => 0,
284
      'search_index' => 0,
285
      'search_result' => 0,
286
    );
287
    unset($saved['roles[flag][2]'], $saved['roles[unflag][2]'], $saved['types[article]'], $saved['types[page]'], $saved['show_in_links[full]'], $saved['show_in_links[teaser]'], $saved['show_in_links[rss]'], $saved['show_in_links[search_index]'], $saved['show_in_links[search_result]']);
288

    
289
    $this->drupalPost(FLAG_ADMIN_PATH . '/add/node', $edit, t('Save flag'));
290

    
291
    drupal_static_reset('flag_get_flags');
292
    $flag = flag_get_flag($edit['name']);
293
    // Load the roles array for checking it matches.
294
    $flag->fetch_roles();
295

    
296
    // Check that the flag object is in the database.
297
    $this->assertTrue($flag != FALSE, t('Flag object found in database'));
298

    
299
    // Check each individual property of the flag and make sure it was set.
300
    foreach ($saved as $property => $value) {
301
      $this->assertEqual($flag->$property, $value, t('Flag property %property properly saved.', array('%property' => $property)));
302
    }
303

    
304
    // Check permissions.
305
    $permissions = user_role_permissions(user_roles());
306
    foreach ($saved['roles'] as $action => $rids) {
307
      foreach ($rids as $rid) {
308
        $permission_string = "$action " . $saved['name'];
309
        $this->assertTrue(isset($permissions[$rid][$permission_string]), t('Permission %perm set for flag.', array(
310
          '%perm' => $permission_string,
311
        )));
312
      }
313
    }
314

    
315
    // Edit the flag through the UI.
316
    $edit = array(
317
      'name' => drupal_strtolower($this->randomName()),
318
      'title' => $this->randomName(),
319
      'flag_short'          => 'flag 2 short [node:nid]',
320
      'flag_long'           => 'flag 2 long [node:nid]',
321
      'flag_message'        => 'flag 2 message [node:nid]',
322
      'unflag_short'        => 'unflag 2 short [node:nid]',
323
      'unflag_long'         => 'unflag 2 long [node:nid]',
324
      'unflag_message'      => 'unflag 2 message [node:nid]',
325
      'roles[flag][2]' => TRUE,
326
      'roles[unflag][2]' => TRUE,
327
      'types[article]' => TRUE,
328
      'types[page]' => FALSE,
329
      'show_in_links[full]' => TRUE,
330
      'show_in_links[teaser]' => TRUE,
331
      'show_in_links[rss]' => FALSE,
332
      'show_in_links[search_index]' => FALSE,
333
      'show_in_links[search_result]' => FALSE,
334
      'show_on_form' => TRUE,
335
      'link_type' => 'normal',
336
    );
337
    $saved = $edit;
338
    $saved['roles'] = array('flag' => array(2), 'unflag' => array(2));
339
    $saved['types'] = array('article');
340
    $saved['show_in_links'] = array(
341
      'full' => TRUE,
342
      'teaser' => TRUE,
343
      'rss' => 0,
344
      'search_index' => 0,
345
      'search_result' => 0,
346
    );
347
    unset($saved['roles[flag][2]'], $saved['roles[unflag][2]'], $saved['types[article]'], $saved['types[page]'], $saved['show_in_links[full]'], $saved['show_in_links[teaser]'], $saved['show_in_links[rss]'], $saved['show_in_links[search_index]'], $saved['show_in_links[search_result]']);
348

    
349
    $this->drupalPost(FLAG_ADMIN_PATH . '/manage/' . $flag->name, $edit, t('Save flag'));
350

    
351
    drupal_static_reset('flag_get_flags');
352
    $flag = flag_get_flag($edit['name']);
353
    // Load the roles array for checking it matches.
354
    $flag->fetch_roles();
355

    
356
    // Check that the flag object is in the database.
357
    $this->assertTrue($flag != FALSE, t('Flag object found in database'));
358

    
359
    // Check each individual property of the flag and make sure it was set.
360
    foreach ($saved as $property => $value) {
361
      $this->assertEqual($flag->$property, $value, t('Flag property %property properly saved.', array('%property' => $property)));
362
    }
363

    
364
    // Clear the user access cache so our changes to permissions are noticed.
365
    drupal_static_reset('user_access');
366
    drupal_static_reset('user_role_permissions');
367

    
368
    // Check permissions.
369
    $permissions = user_role_permissions(user_roles());
370

    
371
    foreach ($saved['roles'] as $action => $rids) {
372
      foreach ($rids as $rid) {
373
        $permission_string = "$action " . $saved['name'];
374
        $this->assertTrue(isset($permissions[$rid][$permission_string]), t('Permission %perm set for flag.', array(
375
          '%perm' => $permission_string,
376
        )));
377
      }
378
    }
379

    
380
    // Delete the flag through the UI.
381
    $this->drupalPost(FLAG_ADMIN_PATH . '/manage/' . $flag->name . '/delete', array(), t('Delete'));
382
    drupal_static_reset('flag_get_flags');
383
    $this->assertFalse(flag_get_flag($flag->name), t('Flag successfully deleted.'));
384
  }
385

    
386
}
387

    
388
/**
389
 * Access to flags using the entity forms.
390
 *
391
 * @todo: complete this test class.
392
 */
393
class FlagAccessFormTestCase extends FlagTestCaseBase {
394

    
395
  /**
396
   * Implements getInfo().
397
   */
398
  public static function getInfo() {
399
    return array(
400
      'name' => 'Access to flags via entity forms',
401
      'description' => 'Access to flag and unflag entities via entity forms.',
402
      'group' => 'Flag',
403
    );
404
  }
405

    
406
  /**
407
   * Implements setUp().
408
   */
409
  function setUp() {
410
    parent::setUp('flag');
411
  }
412

    
413
  /**
414
   * Test scenarios with no access to a global flag.
415
   */
416
  function testFlagAccessGlobalNone() {
417
    // Create a global flag on article nodes.
418
    $flag_data = array(
419
      'entity_type' => 'node',
420
      'name' => 'global_flag',
421
      'title' => 'Global Flag',
422
      'global' => 1,
423
      'types' => array(
424
        0 => 'article',
425
      ),
426
      'flag_short' => 'Flag this item',
427
      'flag_long' => '',
428
      'flag_message' => '',
429
      'unflag_short' => 'Unflag this item',
430
      'unflag_long' => '',
431
      'unflag_message' => '',
432
      'unflag_denied_text' => 'You may not unflag this item',
433
      'link_type' => 'normal',
434
      'weight' => 0,
435
      // Show the flag on the form.
436
      'show_on_form' => 1,
437
      'access_author' => '',
438
      'show_contextual_link' => 0,
439
      'show_in_links' => array(
440
        'full' => 1,
441
        'teaser' => 1,
442
      ),
443
      'i18n' => 0,
444
      'api_version' => 3,
445
    );
446
    $flag = $this->createFlag($flag_data);
447

    
448
    // Create test user who can't us this flag, but can create nodes.
449
    $no_flag_user = $this->drupalCreateUser(array('create article content'));
450
    $this->drupalLogin($no_flag_user);
451

    
452
    $this->drupalGet('node/add/article');
453

    
454
    // Check that the flag form element cannot be seen.
455
    $this->assertNoText('Flag this item', t('Flag form element was not found.'));
456

    
457
    // Have the user create a node.
458
    $edit = array(
459
      'title' => 'node 1',
460
    );
461
    $this->drupalPost('node/add/article', $edit, t('Save'));
462

    
463
    $node = $this->drupalGetNodeByTitle($edit["title"]);
464

    
465
    // Check the new node has not been flagged.
466
    $this->assertFalse($flag->is_flagged($node->nid), t('New node is not flagged.'));
467

    
468
    // Now set the variable so that the flag is set by default on new nodes.
469
    variable_set('flag_' . $flag->name . '_default_' . 'article', 1);
470

    
471
    // Create another new node.
472
    $edit = array(
473
      'title' => 'node 2',
474
    );
475
    $this->drupalPost('node/add/article', $edit, t('Save'));
476

    
477
    $node = $this->drupalGetNodeByTitle($edit["title"]);
478

    
479
    // Check the new node has been flagged, despite the user not having access
480
    // to the flag.
481
    $this->assertTrue($flag->is_flagged($node->nid), t('New node is flagged.'));
482
  }
483

    
484
}
485

    
486
/**
487
 * Tokens we provide on generic entities.
488
 */
489
class FlagEntityTokensTestCase extends FlagTestCaseBase {
490

    
491
  /**
492
   * Implements getInfo().
493
   */
494
  public static function getInfo() {
495
    return array(
496
      'name' => 'Entity tokens',
497
      'description' => 'Tokens for flag count on entities.',
498
      'group' => 'Flag',
499
    );
500
  }
501

    
502
  /**
503
   * Implements setUp().
504
   */
505
  function setUp() {
506
    // Our entity tokens require token module.
507
    parent::setUp('flag', 'token');
508
  }
509

    
510
  /**
511
   * Test tokens on nodes.
512
   */
513
  function testNodeFlagToken() {
514
    // Create a flag on article nodes.
515
    $flag_data = array(
516
      'entity_type' => 'node',
517
      'name' => 'node_flag',
518
      'title' => 'Node Flag',
519
      'global' => 0,
520
      'types' => array(
521
        0 => 'article',
522
      ),
523
      'flag_short' => 'Flag this item',
524
      'flag_long' => '',
525
      'flag_message' => '',
526
      'unflag_short' => 'Unflag this item',
527
      'unflag_long' => '',
528
      'unflag_message' => '',
529
      'unflag_denied_text' => 'You may not unflag this item',
530
      'link_type' => 'normal',
531
      'weight' => 0,
532
      // Show the flag on the form.
533
      'show_on_form' => 1,
534
      'access_author' => '',
535
      'show_contextual_link' => 0,
536
      'show_in_links' => array(
537
        'full' => 1,
538
        'teaser' => 1,
539
      ),
540
      'i18n' => 0,
541
      'api_version' => 3,
542
    );
543
    $flag = $this->createFlag($flag_data);
544

    
545
    // Create a node to flag.
546
    $node = (object) array(
547
      'type' => 'article',
548
      'title' => $this->randomName(),
549
    );
550
    node_save($node);
551

    
552
    // Flag it by several users.
553
    $flag_user_1 = $this->drupalCreateUser(array('flag node_flag',));
554

    
555
    // Flag the node as the user.
556
    $flag = flag_get_flag('node_flag');
557
    $flag->flag('flag', $node->nid, $flag_user_1);
558

    
559
    $flag_user_2 = $this->drupalCreateUser(array('flag node_flag',));
560

    
561
    // Flag the node as the user.
562
    $flag->flag('flag', $node->nid, $flag_user_2);
563

    
564
    $text = '[node:flag-node-flag-count]';
565

    
566
    $replaced_text = token_replace($text, array('node' => $node));
567

    
568
    $this->assertEqual($replaced_text, 2, "The flag count token for the node is correct.");
569
  }
570

    
571
  /**
572
   * Test tokens on taxonomy terms.
573
   *
574
   * These are worthy of a separate test, as the token type is a special case.
575
   */
576
  function testTaxonomyTermFlagToken() {
577
    // Create a flag on tag terms.
578
    $flag_data = array(
579
      'entity_type' => 'taxonomy_term',
580
      'name' => 'term_flag',
581
      'title' => 'Term Flag',
582
      'global' => 0,
583
      'types' => array(
584
        0 => 'tags',
585
      ),
586
      'flag_short' => 'Flag this item',
587
      'flag_long' => '',
588
      'flag_message' => '',
589
      'unflag_short' => 'Unflag this item',
590
      'unflag_long' => '',
591
      'unflag_message' => '',
592
      'unflag_denied_text' => 'You may not unflag this item',
593
      'link_type' => 'normal',
594
      'weight' => 0,
595
      // Show the flag on the form.
596
      'show_on_form' => 1,
597
      'access_author' => '',
598
      'show_contextual_link' => 0,
599
      'show_in_links' => array(
600
        'full' => 1,
601
        'teaser' => 1,
602
      ),
603
      'i18n' => 0,
604
      'api_version' => 3,
605
    );
606
    $flag = $this->createFlag($flag_data);
607

    
608
    $vocabulary = taxonomy_vocabulary_load(1);
609

    
610
    // Create a term to flag.
611
    $term = (object) array(
612
      'name' => $this->randomName(),
613
      'vid' => 1,
614
    );
615
    taxonomy_term_save($term);
616

    
617
    // Flag it by several users.
618
    $flag_user_1 = $this->drupalCreateUser(array('flag term_flag',));
619

    
620
    // Flag the term as the user.
621
    $flag = flag_get_flag('term_flag');
622
    $flag->flag('flag', $term->tid, $flag_user_1);
623

    
624
    $flag_user_2 = $this->drupalCreateUser(array('flag term_flag',));
625

    
626
    // Flag the term as the user.
627
    $flag = flag_get_flag('term_flag');
628
    $flag->flag('flag', $term->tid, $flag_user_2);
629

    
630
    $text = '[term:flag-term-flag-count]';
631

    
632
    $replaced_text = token_replace($text, array('term' => $term));
633

    
634
    debug($replaced_text);
635

    
636
    $this->assertEqual($replaced_text, 2, "The flag count token for the term is correct.");
637
  }
638

    
639
}
640

    
641
/**
642
 * Access to flags using the basic flag link.
643
 */
644
class FlagAccessLinkTestCase extends FlagTestCaseBase {
645

    
646
  /**
647
   * Implements getInfo().
648
   */
649
  public static function getInfo() {
650
    return array(
651
      'name' => 'Access to flags via basic link',
652
      'description' => 'Access to flag and unflag entities using the basic link.',
653
      'group' => 'Flag',
654
    );
655
  }
656

    
657
  /**
658
   * Implements setUp().
659
   */
660
  function setUp() {
661
    parent::setUp('flag');
662

    
663
    // Create a test flag on article nodes.
664
    $flag_data = array(
665
      'entity_type' => 'node',
666
      'name' => 'test_flag',
667
      'title' => 'Test Flag',
668
      'global' => 0,
669
      'types' => array(
670
        0 => 'article',
671
      ),
672
      'flag_short' => 'Flag this item',
673
      'flag_long' => '',
674
      'flag_message' => '',
675
      'unflag_short' => 'Unflag this item',
676
      'unflag_long' => '',
677
      'unflag_message' => '',
678
      'unflag_denied_text' => 'You may not unflag this item',
679
      // Use the normal link type as it involves no intermediary page loads.
680
      'link_type' => 'normal',
681
      'weight' => 0,
682
      'show_on_form' => 0,
683
      'access_author' => '',
684
      'show_contextual_link' => 0,
685
      'show_in_links' => array(
686
        'full' => 1,
687
        'teaser' => 1,
688
      ),
689
      'i18n' => 0,
690
      'api_version' => 3,
691
    );
692
    $flag = $this->createFlag($flag_data);
693

    
694
    // Create an article node that various users will try to flag.
695
    $title = $this->randomName(8);
696
    $node = array(
697
      'title' => $title,
698
      'body' => array(LANGUAGE_NONE => array(array('value' => $this->randomName(32)))),
699
      'uid' => 1,
700
      'type' => 'article',
701
      'is_new' => TRUE,
702
    );
703
    $node = node_submit((object) $node);
704
    node_save($node);
705
    $this->nid = $node->nid;
706
  }
707

    
708
  /**
709
   * Test that a user without flag access can't see the flag.
710
   */
711
  function testFlagAccessNone() {
712
    // Create test user who can't flag at all.
713
    $no_flag_user = $this->drupalCreateUser(array());
714
    $this->drupalLogin($no_flag_user);
715

    
716
    // Look at our node.
717
    $this->drupalGet('node/' . $this->nid);
718

    
719
    $this->assertNoLink('Flag this item', 0, 'The flag link does not appear on the page');
720
  }
721

    
722
  /**
723
   * Test that a user with only flag access can flag but not unflag.
724
   */
725
  function testFlagAccessFlagOnly() {
726
    // Create test user who can flag but not unflag.
727
    $flag_user = $this->drupalCreateUser(array('flag test_flag',));
728
    $this->drupalLogin($flag_user);
729

    
730
    // Look at our node.
731
    $this->drupalGet('node/' . $this->nid);
732

    
733
    $this->assertLink('Flag this item', 0, 'The flag link appears on the page.');
734

    
735
    // Click the link to flag the node.
736
    $this->clickLink(t('Flag this item'));
737

    
738
    $this->assertText('You may not unflag this item', 0, 'The unflag denied text appears on the page after flagging.');
739
  }
740

    
741
  /**
742
   * Test that a user with flag access can flag and unflag.
743
   */
744
  function testFlagAccessFlagUnflag() {
745
    // Create test user who can flag and unflag.
746
    $flag_unflag_user = $this->drupalCreateUser(array('flag test_flag', 'unflag test_flag'));
747
    $this->drupalLogin($flag_unflag_user);
748

    
749
    // Look at our node.
750
    $this->drupalGet('node/' . $this->nid);
751

    
752
    $this->assertLink('Flag this item', 0, 'The flag link appears on the page.');
753

    
754
    // Click the link to flag the node.
755
    $this->clickLink(t('Flag this item'));
756

    
757
    $this->assertLink('Unflag this item', 0, 'The unflag link appears on the page after flagging.');
758

    
759
    // Click the link to unflag the node.
760
    $this->clickLink(t('Unflag this item'));
761

    
762
    $this->assertLink('Flag this item', 0, 'The flag link appears on the page after unflagging.');
763
  }
764

    
765
}
766

    
767
/**
768
 * Test the 'confirm form' link type.
769
 */
770
class FlagLinkTypeConfirmTestCase extends DrupalWebTestCase {
771

    
772
  /**
773
   * Implements getInfo().
774
   */
775
  public static function getInfo() {
776
    return array(
777
      'name' => 'Confirm form',
778
      'description' => 'Flag confirm form link type.',
779
      'group' => 'Flag',
780
    );
781
  }
782

    
783
  /**
784
   * Implements setUp().
785
   */
786
  function setUp() {
787
    parent::setUp('flag');
788

    
789
    // Create a test flag on article nodes.
790
    // Keep the original data so we can compare strings.
791
    $this->flag_data = array(
792
      'entity_type' => 'node',
793
      'name' => 'test_flag',
794
      'title' => 'Test Flag',
795
      'global' => 0,
796
      'types' => array(
797
        0 => 'article',
798
      ),
799
      'flag_short' => 'Flag <em>this</em> item',
800
      'flag_long' => '',
801
      'flag_message' => 'You have flagged this item.',
802
      'unflag_short' => 'Unflag this item',
803
      'unflag_long' => '',
804
      'unflag_message' => 'You have unflagged this item',
805
      'unflag_denied_text' => 'You may not unflag this item',
806
      'link_type' => 'confirm',
807
      'flag_confirmation' => 'Are you sure you want to flag this item?',
808
      'unflag_confirmation' => 'Are you sure you want to unflag this item?',
809
      'weight' => 0,
810
      'show_on_form' => 0,
811
      'access_author' => '',
812
      'show_contextual_link' => 0,
813
      'show_in_links' => array(
814
        'full' => 1,
815
        'teaser' => 1,
816
      ),
817
      'i18n' => 0,
818
      'api_version' => 3,
819
    );
820
    $this->flag = flag_flag::factory_by_array($this->flag_data);
821
    $this->flag->save();
822
    // Reset our cache so our permissions show up.
823
    drupal_static_reset('flag_get_flags');
824

    
825
    // Reset permissions so that permissions for this flag are available.
826
    $this->checkPermissions(array(), TRUE);
827

    
828
    // Create test user who can flag and unflag.
829
    $this->flag_unflag_user = $this->drupalCreateUser(array('flag test_flag', 'unflag test_flag'));
830
    $this->drupalLogin($this->flag_unflag_user);
831

    
832
    // Create an article node to flag and unflag.
833
    $title = $this->randomName(8);
834
    $node = array(
835
      'title' => $title,
836
      'body' => array(LANGUAGE_NONE => array(array('value' => $this->randomName(32)))),
837
      'uid' => 1,
838
      'type' => 'article',
839
      'is_new' => TRUE,
840
    );
841
    $node = node_submit((object) $node);
842
    node_save($node);
843
    $this->nid = $node->nid;
844
  }
845

    
846
  /**
847
   * Test usage of the flag confirm form.
848
   */
849
  function testFlag() {
850
    // Look at our node.
851
    $this->drupalGet('node/' . $this->nid);
852

    
853
    $flag_short_label = strip_tags($this->flag_data['flag_short']);
854

    
855
    $this->assertRaw($this->flag_data['flag_short'], 'The flag text, including HTML, appears on the page.');
856

    
857
    // assertLink() appears to have interesting problems dealing with an A
858
    // element which contains other tags. However, the xpath it uses appears to
859
    // be fine with being given just the portion of the link text that comes
860
    // before the interior tag.
861
    // Fortunately, there will be no other text on the page that matches 'Flag'.
862
    $this->assertLink('Flag', 0, 'The flag link appears on the page.');
863

    
864
    // Click the link to flag the node.
865
    // clickLink() has the same problem, as it uses the same xpath expression as
866
    // assertLink().
867
    $this->clickLink('Flag');
868

    
869
    $this->assertUrl('flag/confirm/flag/test_flag/' . $this->nid, array(
870
      'query' => array(
871
        'destination' => 'node/' . $this->nid,
872
      ),
873
    ), 'On confirm flagging form page.');
874

    
875
    $this->assertText($this->flag_data['flag_confirmation'], 'The flag confirmation text appears as the confirmation page title.');
876

    
877
    $this->assertPattern('@<input [^>]* value="' . $flag_short_label . '" [^>]*>@', 'The flag text, excluding HTML, is shown in the button.');
878

    
879
    // Click the button to confirm the flagging.
880
    $this->drupalPost(NULL, array(), $flag_short_label);
881

    
882
    $this->assertText($this->flag_data['flag_message'], 'The flag message appears once the item has been flagged.');
883
    $this->assertLink($this->flag_data['unflag_short'], 0, 'The unflag link appears once the item has been flagged.');
884

    
885
    // Reset the static cache before we get data from it.
886
    drupal_static_reset('flag_get_user_flags');
887
    $this->assertTrue($this->flag->is_flagged($this->nid, $this->flag_unflag_user->uid), "The node is recorded as flagged by the user.");
888

    
889
    // Click the link to unflag the node.
890
    $this->clickLink($this->flag_data['unflag_short']);
891

    
892
    $this->assertUrl('flag/confirm/unflag/test_flag/' . $this->nid, array(
893
      'query' => array(
894
        'destination' => 'node/' . $this->nid,
895
      ),
896
    ), t('On confirm unflagging form page.'));
897

    
898
    $this->assertText($this->flag_data['unflag_confirmation'], 'The unflag confirmation text appears as the confirmation page title.');
899

    
900
    // Click the button to confirm the flagging.
901
    $this->drupalPost(NULL, array(), $this->flag_data['unflag_short']);
902

    
903
    $this->assertText($this->flag_data['unflag_message'], 'The unflag message appears once the item has been flagged.');
904

    
905
    // Reset the static cache before we get data from it.
906
    drupal_static_reset('flag_get_user_flags');
907
    $this->assertFalse($this->flag->is_flagged($this->nid, $this->flag_unflag_user->uid), "The node is recorded as not flagged by the user.");
908
  }
909

    
910
}
911

    
912
/**
913
 * Verifies the implementation of hook_flag_access().
914
 */
915
class FlagHookFlagAccessTestCase extends FlagTestCaseBase {
916

    
917
  /**
918
   * Implements getInfo().
919
   */
920
  public static function getInfo() {
921
    return array(
922
      'name' => 'hook_flag_access()',
923
      'description' => 'Checks the ability of modules to use hook_flag_access().',
924
      'group' => 'Flag',
925
    );
926
  }
927

    
928
  /**
929
   * Implements setUp().
930
   */
931
  function setUp() {
932
    parent::setUp('flag');
933

    
934
    $success = module_enable(array('flagaccesstest'), FALSE);
935

    
936
    // Create a test flag on article nodes.
937
    $flag_data = array(
938
      'entity_type' => 'node',
939
      'name' => 'test_flag',
940
      'title' => 'Test Flag',
941
      'global' => 0,
942
      'types' => array(
943
        0 => 'article',
944
      ),
945
      'flag_short' => 'Flag this item',
946
      'flag_long' => '',
947
      'flag_message' => '',
948
      'unflag_short' => 'Unflag this item',
949
      'unflag_long' => '',
950
      'unflag_message' => '',
951
      'unflag_denied_text' => 'You may not unflag this item',
952
      // Use the normal link type as it involves no intermediary page loads.
953
      'link_type' => 'normal',
954
      'weight' => 0,
955
      'show_on_form' => 0,
956
      'access_author' => '',
957
      'show_contextual_link' => 0,
958
      'show_in_links' => array(
959
        'full' => 1,
960
        'teaser' => 1,
961
      ),
962
      'i18n' => 0,
963
      'api_version' => 3,
964
    );
965
    $flag = $this->createFlag($flag_data);
966

    
967
    // Create an article node that various users will try to flag.
968
    $title = $this->randomName(8);
969
    $node = array(
970
      'title' => $title,
971
      'body' => array(LANGUAGE_NONE => array(array('value' => $this->randomName(32)))),
972
      'uid' => 1,
973
      'type' => 'article',
974
      'is_new' => TRUE,
975
    );
976
    $node = node_submit((object) $node);
977
    node_save($node);
978
    $this->nid = $node->nid;
979
  }
980

    
981
  /**
982
   * Verifies that the user sees the flag if a module returns NULL (Ignore).
983
   */
984
  function testFlagAccessIgnore() {
985
    variable_set('FlagHookFlagAccessTestCaseMode', 'ignore');
986
    $flag_user = $this->drupalCreateUser(array('flag test_flag', 'unflag test_flag'));
987
    $this->drupalLogin($flag_user);
988

    
989
    // Look at our node.
990
    $this->drupalGet('node/' . $this->nid);
991

    
992
    $this->assertLink('Flag this item', 0, 'The flag link appears on the page.');
993

    
994
    // Click the link to flag the node.
995
    $this->clickLink(t('Flag this item'));
996

    
997
    $this->assertLink('Unflag this item', 0, 'The unflag link appears on the page after flagging.');
998

    
999
    // Click the link to unflag the node.
1000
    $this->clickLink(t('Unflag this item'));
1001

    
1002
    $this->assertLink('Flag this item', 0, 'The flag link appears on the page after unflagging.');
1003
  }
1004

    
1005
  /**
1006
   * Verifies that the user sees the flag if a module returns TRUE (Allow).
1007
   */
1008
  function testFlagAccessAllow() {
1009
    variable_set('FlagHookFlagAccessTestCaseMode', 'allow');
1010
    $flag_user = $this->drupalCreateUser(array('flag test_flag', 'unflag test_flag'));
1011
    $this->drupalLogin($flag_user);
1012

    
1013
    // Look at our node.
1014
    $this->drupalGet('node/' . $this->nid);
1015

    
1016
    $this->assertLink('Flag this item', 0, 'The flag link appears on the page.');
1017

    
1018
    // Click the link to flag the node.
1019
    $this->clickLink(t('Flag this item'));
1020

    
1021
    $this->assertLink('Unflag this item', 0, 'The unflag link appears on the page after flagging.');
1022

    
1023
    // Click the link to unflag the node.
1024
    $this->clickLink(t('Unflag this item'));
1025

    
1026
    $this->assertLink('Flag this item', 0, 'The flag link appears on the page after unflagging.');
1027
  }
1028

    
1029
  /**
1030
   * Verifies that the user sees the flag if a module returns TRUE (Allow) to
1031
   * override default access check.
1032
   */
1033
  function testFlagAccessAllowOverride() {
1034
    variable_set('FlagHookFlagAccessTestCaseMode', 'allow');
1035
    $flag_user = $this->drupalCreateUser(array());
1036
    $this->drupalLogin($flag_user);
1037

    
1038
    // Look at our node.
1039
    $this->drupalGet('node/' . $this->nid);
1040

    
1041
    $this->assertLink('Flag this item', 0, 'The flag link appears on the page.');
1042

    
1043
    // Click the link to flag the node.
1044
    $this->clickLink(t('Flag this item'));
1045

    
1046
    $this->assertLink('Unflag this item', 0, 'The unflag link appears on the page after flagging.');
1047

    
1048
    // Click the link to unflag the node.
1049
    $this->clickLink(t('Unflag this item'));
1050

    
1051
    $this->assertLink('Flag this item', 0, 'The flag link appears on the page after unflagging.');
1052
  }
1053

    
1054
  /**
1055
   * Verifies that the user does not see the flag if a module returns FALSE
1056
   * (Deny).
1057
   */
1058
  function testFlagAccessDeny() {
1059
    variable_set('FlagHookFlagAccessTestCaseMode', 'deny');
1060
    $flag_user = $this->drupalCreateUser(array('flag test_flag', 'unflag test_flag'));
1061
    $this->drupalLogin($flag_user);
1062

    
1063
    // Look at our node.
1064
    $this->drupalGet('node/' . $this->nid);
1065

    
1066
    $this->assertNoLink('Flag this item', 0, 'The flag link does not appear on the page.');
1067
  }
1068

    
1069
}
1070

    
1071
/**
1072
 * Test use of fields on flagging entities.
1073
 */
1074
class FlagFlaggingFieldTestCase extends FlagTestCaseBase {
1075

    
1076
  /**
1077
   * Implements getInfo().
1078
   */
1079
  public static function getInfo() {
1080
    return array(
1081
      'name' => 'Flagging fields',
1082
      'description' => 'Fields on Flagging entities.',
1083
      'group' => 'Flag',
1084
    );
1085
  }
1086

    
1087
  /**
1088
   * Implements setUp().
1089
   */
1090
  function setUp() {
1091
    parent::setUp('flag', 'flag_fields_test');
1092
  }
1093

    
1094
  function testFlaggingFields() {
1095
    $this->assertTrue(1);
1096

    
1097
    $flag_user = $this->drupalCreateUser(array(
1098
      'flag flag_fields_test_flag',
1099
      'unflag flag_fields_test_flag',
1100
    ));
1101
    $this->drupalLogin($flag_user);
1102

    
1103
    $node = $this->drupalCreateNode();
1104

    
1105
    $this->drupalGet('node/' . $node->nid);
1106

    
1107
    $this->clickLink('Flag with the test flag');
1108

    
1109
    // Try to fail the form validation by providing something non-numeric.
1110
    // This validation is only present in the widget validation: this is a core
1111
    // bug, but lets us test widget validation works correctly until it's fixed.
1112
    $edit = array(
1113
      'flag_fields_test_integer[und][0][value]' => 'banana',
1114
    );
1115
    $this->drupalPost(NULL, $edit, 'Flag with the test flag');
1116

    
1117
    $this->assertText("Only numbers are allowed in Test integer.", "Form validation correctly failed the input.");
1118

    
1119
    // Try to fail the form validation by a number that's out of bounds.
1120
    $edit = array(
1121
      'flag_fields_test_integer[und][0][value]' => 12,
1122
    );
1123
    $this->drupalPost(NULL, $edit, 'Flag with the test flag');
1124

    
1125
    $this->assertText("Test integer: the value may be no greater than 11.", "Form validation correctly failed the input.");
1126

    
1127
    $edit = array(
1128
      'flag_fields_test_integer[und][0][value]' => 6,
1129
    );
1130
    $this->drupalPost(NULL, $edit, 'Flag with the test flag');
1131

    
1132
    $this->assertUrl('node/' . $node->nid, array(), "The flagging form submission succeeded.");
1133

    
1134
    // Try to load the flagging, querying for the field value.
1135
    $query = new EntityFieldQuery();
1136
    $query->entityCondition('entity_type', 'flagging')
1137
      ->entityCondition('bundle', 'flag_fields_test_flag')
1138
      ->propertyCondition('entity_id', $node->nid)
1139
      ->fieldCondition('flag_fields_test_integer', 'value', 6);
1140

    
1141
    $result = $query->execute();
1142

    
1143
    $this->assertEqual(count($result['flagging']), 1, "The Flagging entity was found with the correct field values.");
1144
  }
1145

    
1146
}
1147

    
1148
/**
1149
 * Test use of EntityFieldQueries with flagging entities.
1150
 */
1151
class FlagEntityFieldQueryTestCase extends FlagTestCaseBase {
1152

    
1153
  /**
1154
   * Implements getInfo().
1155
   */
1156
  public static function getInfo() {
1157
    return array(
1158
      'name' => 'Entity Field Query',
1159
      'description' => 'Entity Field Query for flagging entities.',
1160
      'group' => 'Flag',
1161
    );
1162
  }
1163

    
1164
  /**
1165
   * Implements setUp().
1166
   */
1167
  function setUp() {
1168
    parent::setUp('flag');
1169

    
1170
    $flag_data = array(
1171
      'entity_type' => 'node',
1172
      'name' => 'test_flag_1',
1173
      'title' => 'Test Flag',
1174
      'global' => 0,
1175
      'types' => array(
1176
        0 => 'article',
1177
      ),
1178
      'flag_short' => 'Flag this item',
1179
      'flag_long' => '',
1180
      'flag_message' => '',
1181
      'unflag_short' => 'Unflag this item',
1182
      'unflag_long' => '',
1183
      'unflag_message' => '',
1184
      'unflag_denied_text' => 'You may not unflag this item',
1185
      // Use the normal link type as it involves no intermediary page loads.
1186
      'link_type' => 'normal',
1187
      'weight' => 0,
1188
      'show_on_form' => 0,
1189
      'access_author' => '',
1190
      'show_contextual_link' => 0,
1191
      'show_in_links' => array(
1192
        'full' => 1,
1193
        'teaser' => 1,
1194
      ),
1195
      'i18n' => 0,
1196
      'api_version' => 3,
1197
    );
1198

    
1199
    $this->flag1 = $this->createFlag($flag_data);
1200
    $flag_data['name'] = 'test_flag_2';
1201
    $this->flag2 = $this->createFlag($flag_data);
1202
    $flag_data['name'] = 'test_flag_3';
1203
    $this->flag3 = $this->createFlag($flag_data);
1204

    
1205
    // Create test user who can flag and unflag.
1206
    $this->flag_unflag_user = $this->drupalCreateUser(array(
1207
      'flag test_flag_1',
1208
      'unflag test_flag_1',
1209
      'flag test_flag_2',
1210
      'unflag test_flag_2',
1211
    ));
1212
    $this->drupalLogin($this->flag_unflag_user);
1213

    
1214
  }
1215

    
1216
  /**
1217
   * Test use of EntityFieldQuery with flagging entities.
1218
   */
1219
  function testEntityFieldQuery() {
1220
    $node_settings = array(
1221
      'title' => $this->randomName(),
1222
      'body' => array(LANGUAGE_NONE => array(array('value' => $this->randomName(32)))),
1223
      'uid' => 1,
1224
      'type' => 'article',
1225
      'is_new' => TRUE,
1226
    );
1227
    $node = $this->drupalCreateNode($node_settings);
1228

    
1229
    flag('flag', 'test_flag_1', $node->nid, $this->flag_unflag_user);
1230
    flag('flag', 'test_flag_2', $node->nid, $this->flag_unflag_user);
1231

    
1232
    $query = new EntityFieldQuery();
1233
    $query->entityCondition('entity_type', 'flagging')
1234
      ->entityCondition('bundle', 'test_flag_1');
1235

    
1236
    $flagged = $query->execute();
1237
    $this->assertEqual(count($flagged['flagging']), 1);
1238

    
1239
    $query = new EntityFieldQuery();
1240
    $query->entityCondition('entity_type', 'flagging')
1241
      ->entityCondition('bundle', 'test%', 'like');
1242
    $flagged = $query->execute();
1243
    $this->assertEqual(count($flagged['flagging']), 2);
1244

    
1245
    $query = new EntityFieldQuery();
1246
    $query->entityCondition('entity_type', 'flagging')
1247
      ->entityCondition('bundle', array('test_flag_1', 'test_flag_2'), 'IN');
1248
    $this->assertEqual(count($flagged['flagging']), 2);
1249
  }
1250

    
1251
}
1252

    
1253
/**
1254
 * Verifies the invocation of hooks when performing flagging and unflagging.
1255
 */
1256
class FlagHookInvocationsTestCase extends FlagTestCaseBase {
1257

    
1258
  /**
1259
   * Implements getInfo().
1260
   */
1261
  public static function getInfo() {
1262
    return array(
1263
      'name' => 'Hook invocations',
1264
      'description' => 'Invocation of flag and entity hooks and rules during flagging and unflagging.',
1265
      'group' => 'Flag',
1266
    );
1267
  }
1268

    
1269
  /**
1270
   * Implements setUp().
1271
   */
1272
  function setUp() {
1273
    parent::setUp('flag', 'rules', 'flag_hook_test');
1274

    
1275
    // Note the test module contains our test flag.
1276

    
1277
    // Create test user who can flag and unflag.
1278
    $this->flag_unflag_user = $this->drupalCreateUser(array('flag flag_hook_test_flag', 'unflag flag_hook_test_flag'));
1279
    $this->drupalLogin($this->flag_unflag_user);
1280
  }
1281

    
1282
  /**
1283
   * Test invocation of hooks and their data during flagging and unflagging.
1284
   *
1285
   * For each operation (flagging, re-flagging, unflagging) we test:
1286
   *  - the order in which Flag hooks, entity hooks, and rules are invoked.
1287
   *  - the parameters each hook receives
1288
   *  - the data that a hook implementation obtains when it calls the Flag data
1289
   *    API functions.
1290
   */
1291
  function testHookInvocation() {
1292
    // Create an article node that we try to create a flagging entity for.
1293
    $title = $this->randomName(8);
1294
    $node = array(
1295
      'title' => $title,
1296
      'body' => array(LANGUAGE_NONE => array(array('value' => $this->randomName(32)))),
1297
      'uid' => 1,
1298
      'type' => 'article',
1299
      'is_new' => TRUE,
1300
    );
1301
    $node = node_submit((object) $node);
1302
    node_save($node);
1303

    
1304
    // Initialize a tracking variable. The test module will update this when
1305
    // its hooks are invoked.
1306
    variable_set('flag_hook_test_hook_tracking', array());
1307

    
1308
    // Flag the node as the user.
1309
    $flag = flag_get_flag('flag_hook_test_flag');
1310
    $flag->flag('flag', $node->nid, $this->flag_unflag_user);
1311

    
1312
    // Get the variable the test module sets the hook order into.
1313
    $hook_data_variable = variable_get('flag_hook_test_hook_tracking', array());
1314
    //debug($hook_data_variable['hook_entity_presave']);
1315
    //debug($hook_data_variable['hook_entity_insert']);
1316

    
1317
    $expected_hook_order = array(
1318
      'hook_entity_presave',
1319
      'hook_entity_insert',
1320
      'hook_flag_flag',
1321
      'rules_event',
1322
    );
1323

    
1324
    // Check the hooks are invoked in the correct order.
1325
    $this->assertIdentical($expected_hook_order, array_keys($hook_data_variable), "The hooks are invoked in the correct order when flagging a node.");
1326

    
1327
    // Check the parameters received by hook_entity_presave().
1328
    // Param 0: $flagging.
1329
    $this->assertEqual($hook_data_variable['hook_entity_presave']['parameters'][0]->flag_name, $flag->name, "The Flagging entity passed to hook_entity_presave() has the expected flag name property.");
1330
    $this->assertEqual($hook_data_variable['hook_entity_presave']['parameters'][0]->fid, $flag->fid, "The Flagging entity passed to hook_entity_presave() has the expected fid property.");
1331
    $this->assertEqual($hook_data_variable['hook_entity_presave']['parameters'][0]->entity_type, 'node', "The Flagging entity passed to hook_entity_presave() has the expected entity type property.");
1332
    $this->assertEqual($hook_data_variable['hook_entity_presave']['parameters'][0]->entity_id, $node->nid, "The Flagging entity passed to hook_entity_presave() has the expected entity ID property.");
1333
    $this->assertEqual($hook_data_variable['hook_entity_presave']['parameters'][0]->uid, $this->flag_unflag_user->uid, "The Flagging entity passed to hook_entity_presave() has the expected uid property.");
1334
    // Param 1: $entity_type.
1335
    $this->assertEqual($hook_data_variable['hook_entity_presave']['parameters'][1], 'flagging', "hook_entity_presave() is passed the correct entity type.");
1336

    
1337
    // Check the API data available to hook_entity_presave().
1338
    //debug($hook_data_variable['hook_entity_presave']['api_calls']);
1339
    $this->assertEqual($hook_data_variable['hook_entity_presave']['api_calls']['flag_get_entity_flags'], array(), "hook_entity_presave() gets no data from from flag_get_entity_flags(), as the flagging has not yet taken place.");
1340
    $this->assertEqual($hook_data_variable['hook_entity_presave']['api_calls']['flag_get_user_flags'], array(), "hook_entity_presave() gets no data from from flag_get_user_flags(), as the flagging has not yet taken place.");
1341
    // The flag counts have not yet been increased.
1342
    $this->assertEqual($hook_data_variable['hook_entity_presave']['api_calls']['flag_get_counts'], array(), "hook_entity_presave() gets no data from from flag_get_counts(), as the flagging has not yet taken place.");
1343
    $this->assertEqual($hook_data_variable['hook_entity_presave']['api_calls']['flag_get_flag_counts'], 0, "hook_entity_presave() gets no data from from flag_get_flag_counts(), as the flagging has not yet taken place.");
1344
    $this->assertEqual($hook_data_variable['hook_entity_presave']['api_calls']['flag_get_entity_flag_counts'], 0, "hook_entity_presave() gets no data from from flag_get_entity_flag_counts(), as the flagging has not yet taken place.");
1345
    $this->assertEqual($hook_data_variable['hook_entity_presave']['api_calls']['flag_get_user_flag_counts'], 0, "hook_entity_presave() gets no data from from flag_get_user_flag_counts(), as the flagging has not yet taken place.");
1346

    
1347
    // Check the parameters received by hook_entity_insert().
1348
    // Param 0: $flagging.
1349
    $this->assertEqual($hook_data_variable['hook_entity_insert']['parameters'][0]->flag_name, $flag->name, "The Flagging entity passed to hook_entity_insert() has the expected flag name property.");
1350
    $this->assertEqual($hook_data_variable['hook_entity_insert']['parameters'][0]->fid, $flag->fid, "The Flagging entity passed to hook_entity_insert() has the expected fid property.");
1351
    $this->assertEqual($hook_data_variable['hook_entity_insert']['parameters'][0]->entity_type, 'node', "The Flagging entity passed to hook_entity_insert() has the expected entity type property.");
1352
    $this->assertEqual($hook_data_variable['hook_entity_insert']['parameters'][0]->entity_id, $node->nid, "The Flagging entity passed to hook_entity_insert() has the expected entity ID property.");
1353
    $this->assertEqual($hook_data_variable['hook_entity_insert']['parameters'][0]->uid, $this->flag_unflag_user->uid, "The Flagging entity passed to hook_entity_insert() has the expected uid property.");
1354
    $this->assertTrue(!empty($hook_data_variable['hook_entity_insert']['parameters'][0]->flagging_id), "The Flagging entity passed to hook_entity_insert() has the flagging ID set.");
1355
    // Param 1: $entity_type.
1356
    $this->assertEqual($hook_data_variable['hook_entity_insert']['parameters'][1], 'flagging', "hook_entity_insert() is passed the correct entity type.");
1357

    
1358
    // Check the API data available to hook_entity_insert().
1359
    //debug($hook_data_variable['hook_entity_insert']['api_calls']);
1360
    $flag_get_entity_flags = $hook_data_variable['hook_entity_insert']['api_calls']['flag_get_entity_flags'];
1361
    $flag_get_entity_flags_uids = array_keys($flag_get_entity_flags);
1362
    $this->assertEqual($flag_get_entity_flags_uids, array($this->flag_unflag_user->uid), "hook_entity_insert() gets correct data for flagging users from flag_get_entity_flags()");
1363
    $flag_get_entity_flags_flagging = $flag_get_entity_flags[$this->flag_unflag_user->uid];
1364
    $this->assertEqual($flag_get_entity_flags_flagging->fid, $flag->fid, "hook_entity_insert() gets a correct fid on the Flagging obtained from flag_get_entity_flags()");
1365
    $this->assertEqual($flag_get_entity_flags_flagging->entity_type, 'node', "hook_entity_insert() gets a correct entity type on the Flagging obtained from flag_get_entity_flags()");
1366
    $this->assertEqual($flag_get_entity_flags_flagging->entity_id, $node->nid, "hook_entity_insert() gets a correct entity ID on the Flagging obtained from flag_get_entity_flags()");
1367

    
1368
    $flag_get_user_flags = $hook_data_variable['hook_entity_insert']['api_calls']['flag_get_user_flags'];
1369
    $flag_get_user_flags_flagging = $flag_get_user_flags[$flag->name];
1370
    $this->assertEqual($flag_get_user_flags_flagging->fid, $flag->fid, "hook_entity_insert() gets a correct fid on the Flagging obtained from flag_get_user_flags()");
1371
    $this->assertEqual($flag_get_user_flags_flagging->entity_type, 'node', "hook_entity_insert() gets a correct entity type on the Flagging obtained from flag_get_user_flags()");
1372
    $this->assertEqual($flag_get_user_flags_flagging->entity_id, $node->nid, "hook_entity_insert() gets a correct entity ID on the Flagging obtained from flag_get_user_flags()");
1373

    
1374
    // The flag counts have been increased.
1375
    $flag_get_counts = $hook_data_variable['hook_entity_insert']['api_calls']['flag_get_counts'];
1376
    $this->assertEqual($flag_get_counts[$flag->name], 1, "hook_entity_insert() gets a correct flag count from flag_get_counts().");
1377

    
1378
    $flag_get_flag_counts = $hook_data_variable['hook_entity_insert']['api_calls']['flag_get_flag_counts'];
1379
    $this->assertEqual($flag_get_flag_counts, 1, "hook_entity_insert() gets a correct flag count from flag_get_flag_counts().");
1380

    
1381
    $flag_get_entity_flag_counts = $hook_data_variable['hook_entity_insert']['api_calls']['flag_get_entity_flag_counts'];
1382
    $this->assertEqual($flag_get_entity_flag_counts, 1, "hook_entity_insert() gets a correct flag count from flag_get_entity_flag_counts().");
1383

    
1384
    $flag_get_user_flag_counts = $hook_data_variable['hook_entity_insert']['api_calls']['flag_get_user_flag_counts'];
1385
    $this->assertEqual($flag_get_user_flag_counts, 1, "hook_entity_insert() gets a correct flag count from flag_get_user_flag_counts().");
1386

    
1387
    // Check the parameters received by hook_flag_flag().
1388
    // Param 0: $flag.
1389
    $this->assertEqual($hook_data_variable['hook_flag_flag']['parameters'][0], $flag, "The flag object is passed to hook_flag_flag().");
1390
    // Param 1: $entity_id.
1391
    $this->assertEqual($hook_data_variable['hook_flag_flag']['parameters'][1], $node->nid, "The entity ID is passed to hook_flag_flag().");
1392
    // Param 2: $account.
1393
    $this->assertEqual($hook_data_variable['hook_flag_flag']['parameters'][2]->uid, $this->flag_unflag_user->uid, "The user account is passed to hook_flag_flag().");
1394
    // Param 3: $flagging.
1395
    $this->assertEqual($hook_data_variable['hook_flag_flag']['parameters'][3]->flag_name, $flag->name, "The Flagging entity passed to hook_flag_flag() has the expected flag name property.");
1396
    $this->assertEqual($hook_data_variable['hook_flag_flag']['parameters'][3]->fid, $flag->fid, "The Flagging entity passed to hook_flag_flag() has the expected fid property.");
1397
    $this->assertEqual($hook_data_variable['hook_flag_flag']['parameters'][3]->entity_type, 'node', "The Flagging entity passed to hook_flag_flag() has the expected entity type property.");
1398
    $this->assertEqual($hook_data_variable['hook_flag_flag']['parameters'][3]->entity_id, $node->nid, "The Flagging entity passed to hook_flag_flag() has the expected entity ID property.");
1399
    $this->assertEqual($hook_data_variable['hook_flag_flag']['parameters'][3]->uid, $this->flag_unflag_user->uid, "The Flagging entity passed to hook_flag_flag() has the expected uid property.");
1400

    
1401
    // Check the API data available to hook_flag_flag().
1402
    //debug($hook_data_variable['hook_flag_flag']['api_calls']);
1403
    $flag_get_entity_flags = $hook_data_variable['hook_flag_flag']['api_calls']['flag_get_entity_flags'];
1404
    $flag_get_entity_flags_uids = array_keys($flag_get_entity_flags);
1405
    $this->assertEqual($flag_get_entity_flags_uids, array($this->flag_unflag_user->uid), "hook_flag_flag() gets correct data for flagging users from flag_get_entity_flags()");
1406
    $flag_get_entity_flags_flagging = $flag_get_entity_flags[$this->flag_unflag_user->uid];
1407
    $this->assertEqual($flag_get_entity_flags_flagging->fid, $flag->fid, "hook_flag_flag() gets a correct fid on the Flagging obtained from flag_get_entity_flags()");
1408
    $this->assertEqual($flag_get_entity_flags_flagging->entity_type, 'node', "hook_flag_flag() gets a correct entity type on the Flagging obtained from flag_get_entity_flags()");
1409
    $this->assertEqual($flag_get_entity_flags_flagging->entity_id, $node->nid, "hook_flag_flag() gets a correct entity ID on the Flagging obtained from flag_get_entity_flags()");
1410

    
1411
    $flag_get_user_flags = $hook_data_variable['hook_flag_flag']['api_calls']['flag_get_user_flags'];
1412
    $flag_get_user_flags_flagging = $flag_get_user_flags[$flag->name];
1413
    $this->assertEqual($flag_get_user_flags_flagging->fid, $flag->fid, "hook_flag_flag() gets a correct fid on the Flagging obtained from flag_get_user_flags()");
1414
    $this->assertEqual($flag_get_user_flags_flagging->entity_type, 'node', "hook_flag_flag() gets a correct entity type on the Flagging obtained from flag_get_user_flags()");
1415
    $this->assertEqual($flag_get_user_flags_flagging->entity_id, $node->nid, "hook_flag_flag() gets a correct entity ID on the Flagging obtained from flag_get_user_flags()");
1416

    
1417
    // The flag counts have been increased.
1418
    $flag_get_counts = $hook_data_variable['hook_flag_flag']['api_calls']['flag_get_counts'];
1419
    $this->assertEqual($flag_get_counts[$flag->name], 1, "hook_flag_flag() gets a correct flag count from flag_get_counts().");
1420

    
1421
    $flag_get_flag_counts = $hook_data_variable['hook_flag_flag']['api_calls']['flag_get_flag_counts'];
1422
    $this->assertEqual($flag_get_flag_counts, 1, "hook_flag_flag() gets a correct flag count from flag_get_flag_counts().");
1423

    
1424
    $flag_get_entity_flag_counts = $hook_data_variable['hook_flag_flag']['api_calls']['flag_get_entity_flag_counts'];
1425
    $this->assertEqual($flag_get_entity_flag_counts, 1, "hook_flag_flag() gets a correct flag count from flag_get_entity_flag_counts().");
1426

    
1427
    $flag_get_user_flag_counts = $hook_data_variable['hook_flag_flag']['api_calls']['flag_get_user_flag_counts'];
1428
    $this->assertEqual($flag_get_user_flag_counts, 1, "hook_flag_flag() gets a correct flag count from flag_get_user_flag_counts().");
1429

    
1430
    // Clear the tracking variable.
1431
    variable_set('flag_hook_test_hook_tracking', array());
1432

    
1433
    // Get the Flagging, and re-flag the node.
1434
    // This does nothing in our case, but is the API for updating a Flagging
1435
    // entity with changes to its Field API fields.
1436
    // Query the database to get the Flagging ID rather than Flag API so we
1437
    // don't interefere with any caches.
1438
    $query = db_select('flagging', 'f');
1439
    $query->addField('f', 'flagging_id');
1440
    $query->condition('fid', $flag->fid)
1441
      ->condition('entity_id', $node->nid);
1442
    $flagging_id = $query
1443
      ->execute()
1444
      ->fetchField();
1445
    $flagging = flagging_load($flagging_id);
1446

    
1447
    // Re-flag the node passing in the flagging entity.
1448
    $flag->flag('flag', $node->nid, $this->flag_unflag_user, FALSE, $flagging);
1449

    
1450
    // Get the variable the test module sets the hook order into.
1451
    $hook_data_variable = variable_get('flag_hook_test_hook_tracking', array());
1452
    //debug($hook_data_variable);
1453

    
1454
    $expected_hook_order = array(
1455
      'hook_entity_presave',
1456
      'hook_entity_update',
1457
      // Note that hook_flag() and the rule are not invoked, as this is not a
1458
      // new act of flagging.
1459
    );
1460

    
1461
    // Check the hooks are invoked in the correct order.
1462
    $this->assertIdentical($expected_hook_order, array_keys($hook_data_variable), "The hooks are invoked in the correct order when re-flagging a node.");
1463

    
1464
    // Check the parameters received by hook_entity_presave().
1465
    // Param 0: $flagging.
1466
    $this->assertEqual($hook_data_variable['hook_entity_presave']['parameters'][0]->flag_name, $flag->name, "The Flagging entity passed to hook_entity_presave() has the expected flag name property.");
1467
    //$this->assertEqual($hook_data_variable['hook_entity_insert']['parameters'][0]->fid, $flag->fid);
1468
    //$this->assertEqual($hook_data_variable['hook_entity_insert']['parameters'][0]->entity_type, 'node');
1469
    $this->assertEqual($hook_data_variable['hook_entity_presave']['parameters'][0]->entity_id, $node->nid, "The Flagging entity passed to hook_entity_presave() has the expected entity ID property.");
1470
    $this->assertEqual($hook_data_variable['hook_entity_presave']['parameters'][0]->uid, $this->flag_unflag_user->uid, "The Flagging entity passed to hook_entity_presave() has the expected uid property.");
1471
    // Param 1: $entity_type.
1472
    $this->assertEqual($hook_data_variable['hook_entity_presave']['parameters'][1], 'flagging', "hook_entity_presave() is passed the correct entity type.");
1473

    
1474
    // Check the API data available to hook_entity_presave().
1475
    //debug($hook_data_variable['hook_entity_presave']['api_calls']);
1476
    $flag_get_entity_flags = $hook_data_variable['hook_entity_presave']['api_calls']['flag_get_entity_flags'];
1477
    $flag_get_entity_flags_uids = array_keys($flag_get_entity_flags);
1478
    $this->assertEqual($flag_get_entity_flags_uids, array($this->flag_unflag_user->uid), "hook_entity_presave() gets correct data for flagging users from flag_get_entity_flags()");
1479
    $flag_get_entity_flags_flagging = $flag_get_entity_flags[$this->flag_unflag_user->uid];
1480
    $this->assertEqual($flag_get_entity_flags_flagging->fid, $flag->fid, "hook_entity_presave() gets a correct fid on the Flagging obtained from flag_get_entity_flags()");
1481
    $this->assertEqual($flag_get_entity_flags_flagging->entity_type, 'node', "hook_entity_presave() gets a correct entity type on the Flagging obtained from flag_get_entity_flags()");
1482
    $this->assertEqual($flag_get_entity_flags_flagging->entity_id, $node->nid, "hook_entity_presave() gets a correct entity ID on the Flagging obtained from flag_get_entity_flags()");
1483

    
1484
    $flag_get_user_flags = $hook_data_variable['hook_entity_presave']['api_calls']['flag_get_user_flags'];
1485
    $flag_get_user_flags_flagging = $flag_get_user_flags[$flag->name];
1486
    $this->assertEqual($flag_get_user_flags_flagging->fid, $flag->fid, "hook_entity_presave() gets a correct fid on the Flagging obtained from flag_get_user_flags()");
1487
    $this->assertEqual($flag_get_user_flags_flagging->entity_type, 'node', "hook_entity_presave() gets a correct entity type on the Flagging obtained from flag_get_user_flags()");
1488
    $this->assertEqual($flag_get_user_flags_flagging->entity_id, $node->nid, "hook_entity_presave() gets a correct entity ID on the Flagging obtained from flag_get_user_flags()");
1489

    
1490
    // The flag counts have not changed.
1491
    $flag_get_counts = $hook_data_variable['hook_entity_presave']['api_calls']['flag_get_counts'];
1492
    $this->assertEqual($flag_get_counts[$flag->name], 1, "hook_entity_presave() gets a correct flag count from flag_get_counts().");
1493

    
1494
    $flag_get_flag_counts = $hook_data_variable['hook_entity_presave']['api_calls']['flag_get_flag_counts'];
1495
    $this->assertEqual($flag_get_flag_counts, 1, "hook_entity_presave() gets a correct flag count from flag_get_flag_counts().");
1496

    
1497
    $flag_get_entity_flag_counts = $hook_data_variable['hook_entity_presave']['api_calls']['flag_get_entity_flag_counts'];
1498
    $this->assertEqual($flag_get_entity_flag_counts, 1, "hook_entity_presave() gets a correct flag count from flag_get_entity_flag_counts().");
1499

    
1500
    $flag_get_user_flag_counts = $hook_data_variable['hook_entity_presave']['api_calls']['flag_get_user_flag_counts'];
1501
    $this->assertEqual($flag_get_user_flag_counts, 1, "hook_entity_presave() gets a correct flag count from flag_get_user_flag_counts().");
1502

    
1503
    // Check the parameters received by hook_entity_update().
1504
    // Param 0: $flagging.
1505
    $this->assertEqual($hook_data_variable['hook_entity_update']['parameters'][0]->flag_name, $flag->name, "The Flagging entity passed to hook_entity_update() has the expected flag name property.");
1506
    $this->assertEqual($hook_data_variable['hook_entity_presave']['parameters'][0]->fid, $flag->fid, "The Flagging entity passed to hook_entity_presave() has the expected fid property.");
1507
    $this->assertEqual($hook_data_variable['hook_entity_presave']['parameters'][0]->entity_type, 'node', "The Flagging entity passed to hook_entity_presave() has the expected entity type property.");
1508
    $this->assertEqual($hook_data_variable['hook_entity_update']['parameters'][0]->entity_id, $node->nid, "The Flagging entity passed to hook_entity_update() has the expected entity ID property.");
1509
    $this->assertEqual($hook_data_variable['hook_entity_update']['parameters'][0]->uid, $this->flag_unflag_user->uid, "The Flagging entity passed to hook_entity_update() has the expected uid property.");
1510
    $this->assertTrue(!empty($hook_data_variable['hook_entity_update']['parameters'][0]->flagging_id), "The Flagging entity passed to hook_entity_update() has the flagging ID set.");
1511
    // Param 1: $entity_type.
1512
    $this->assertEqual($hook_data_variable['hook_entity_update']['parameters'][1], 'flagging', "hook_entity_update() is passed the correct entity type.");
1513

    
1514
    // Check the API data available to hook_entity_update().
1515
    //debug($hook_data_variable['hook_entity_update']['api_calls']);
1516
    $flag_get_entity_flags = $hook_data_variable['hook_entity_update']['api_calls']['flag_get_entity_flags'];
1517
    $flag_get_entity_flags_uids = array_keys($flag_get_entity_flags);
1518
    $this->assertEqual($flag_get_entity_flags_uids, array($this->flag_unflag_user->uid), "hook_entity_update() gets correct data for flagging users from flag_get_entity_flags()");
1519
    $flag_get_entity_flags_flagging = $flag_get_entity_flags[$this->flag_unflag_user->uid];
1520
    $this->assertEqual($flag_get_entity_flags_flagging->fid, $flag->fid, "hook_entity_update() gets a correct fid on the Flagging obtained from flag_get_entity_flags()");
1521
    $this->assertEqual($flag_get_entity_flags_flagging->entity_type, 'node', "hook_entity_update() gets a correct entity type on the Flagging obtained from flag_get_entity_flags()");
1522
    $this->assertEqual($flag_get_entity_flags_flagging->entity_id, $node->nid, "hook_entity_update() gets a correct entity ID on the Flagging obtained from flag_get_entity_flags()");
1523

    
1524
    $flag_get_user_flags = $hook_data_variable['hook_entity_update']['api_calls']['flag_get_user_flags'];
1525
    $flag_get_user_flags_flagging = $flag_get_user_flags[$flag->name];
1526
    $this->assertEqual($flag_get_user_flags_flagging->fid, $flag->fid, "hook_entity_update() gets a correct fid on the Flagging obtained from flag_get_user_flags()");
1527
    $this->assertEqual($flag_get_user_flags_flagging->entity_type, 'node', "hook_entity_update() gets a correct entity type on the Flagging obtained from flag_get_user_flags()");
1528
    $this->assertEqual($flag_get_user_flags_flagging->entity_id, $node->nid, "hook_entity_update() gets a correct entity ID on the Flagging obtained from flag_get_user_flags()");
1529

    
1530
    // The flag counts have not changed.
1531
    $flag_get_counts = $hook_data_variable['hook_entity_update']['api_calls']['flag_get_counts'];
1532
    $this->assertEqual($flag_get_counts[$flag->name], 1, "hook_entity_update() gets a correct flag count from flag_get_counts().");
1533

    
1534
    $flag_get_flag_counts = $hook_data_variable['hook_entity_update']['api_calls']['flag_get_flag_counts'];
1535
    $this->assertEqual($flag_get_flag_counts, 1, "hook_entity_update() gets a correct flag count from flag_get_flag_counts().");
1536

    
1537
    $flag_get_entity_flag_counts = $hook_data_variable['hook_entity_update']['api_calls']['flag_get_entity_flag_counts'];
1538
    $this->assertEqual($flag_get_entity_flag_counts, 1, "hook_entity_update() gets a correct flag count from flag_get_entity_flag_counts().");
1539

    
1540
    $flag_get_user_flag_counts = $hook_data_variable['hook_entity_update']['api_calls']['flag_get_user_flag_counts'];
1541
    $this->assertEqual($flag_get_user_flag_counts, 1, "hook_entity_update() gets a correct flag count from flag_get_user_flag_counts().");
1542

    
1543
    // Clear the tracking variable.
1544
    variable_set('flag_hook_test_hook_tracking', array());
1545

    
1546
    // Unflag the node as the user.
1547
    $flag->flag('unflag', $node->nid, $this->flag_unflag_user);
1548

    
1549
    // Get the variable the test module sets the hook order into.
1550
    $hook_data_variable = variable_get('flag_hook_test_hook_tracking', array());
1551
    //debug($hook_data_variable);
1552

    
1553
    $expected_hook_order = array(
1554
      'hook_flag_unflag',
1555
      'rules_event',
1556
      'hook_entity_delete',
1557
    );
1558

    
1559
    // Check the hooks are invoked in the correct order.
1560
    $this->assertIdentical($expected_hook_order, array_keys($hook_data_variable), "The hooks are invoked in the correct order when unflagging a node.");
1561

    
1562
    // Check the parameters received by hook_flag_unflag().
1563
    // Param 0: $flag.
1564
    $this->assertEqual($hook_data_variable['hook_flag_unflag']['parameters'][0], $flag, "The flag object is passed to hook_flag_unflag().");
1565
    // Param 1: $entity_id.
1566
    $this->assertEqual($hook_data_variable['hook_flag_unflag']['parameters'][1], $node->nid, "The entity ID is passed to hook_flag_unflag().");
1567
    // Param 2: $account.
1568
    $this->assertEqual($hook_data_variable['hook_flag_unflag']['parameters'][2]->uid, $this->flag_unflag_user->uid, "The user account is passed to hook_flag_unflag().");
1569
    // Param 3: $flagging.
1570
    $this->assertEqual($hook_data_variable['hook_flag_unflag']['parameters'][3]->flag_name, $flag->name, "The Flagging entity passed to hook_flag_unflag() has the expected flag name property.");
1571
    $this->assertEqual($hook_data_variable['hook_flag_unflag']['parameters'][3]->fid, $flag->fid, "The Flagging entity passed to hook_entity_presave() has the expected fid property.");
1572
    $this->assertEqual($hook_data_variable['hook_flag_unflag']['parameters'][3]->entity_type, 'node', "The Flagging entity passed to hook_entity_presave() has the expected entity type property.");
1573
    $this->assertEqual($hook_data_variable['hook_flag_unflag']['parameters'][3]->entity_id, $node->nid, "The Flagging entity passed to hook_flag_unflag() has the expected entity ID property.");
1574
    $this->assertEqual($hook_data_variable['hook_flag_unflag']['parameters'][3]->uid, $this->flag_unflag_user->uid, "The Flagging entity passed to hook_flag_unflag() has the expected uid property.");
1575

    
1576
    // Check the API data available to hook_flag_unflag().
1577
    //debug($hook_data_variable['hook_flag_unflag']['api_calls']);
1578
    // The unflagging is not yet done, so flag_get_entity_flags() will find the
1579
    // flagging data.
1580
    $flag_get_entity_flags = $hook_data_variable['hook_flag_unflag']['api_calls']['flag_get_entity_flags'];
1581
    $flag_get_entity_flags_uids = array_keys($flag_get_entity_flags);
1582
    $this->assertEqual($flag_get_entity_flags_uids, array($this->flag_unflag_user->uid), "hook_flag_unflag() gets correct data for flagging users from flag_get_entity_flags()");
1583
    $flag_get_entity_flags_flagging = $flag_get_entity_flags[$this->flag_unflag_user->uid];
1584
    $this->assertEqual($flag_get_entity_flags_flagging->fid, $flag->fid, "hook_flag_unflag() gets a correct fid on the Flagging obtained from flag_get_entity_flags(): the Flagging has not yet been deleted.");
1585
    $this->assertEqual($flag_get_entity_flags_flagging->entity_type, 'node', "hook_flag_unflag() gets a correct entity type on the Flagging obtained from flag_get_entity_flags()");
1586
    $this->assertEqual($flag_get_entity_flags_flagging->entity_id, $node->nid, "hook_flag_unflag() gets a correct entity ID on the Flagging obtained from flag_get_entity_flags()");
1587

    
1588
    $flag_get_user_flags = $hook_data_variable['hook_flag_unflag']['api_calls']['flag_get_user_flags'];
1589
    $flag_get_user_flags_flagging = $flag_get_user_flags[$flag->name];
1590
    $this->assertEqual($flag_get_user_flags_flagging->fid, $flag->fid, "hook_flag_unflag() gets a correct fid on the Flagging obtained from flag_get_user_flags()");
1591
    $this->assertEqual($flag_get_user_flags_flagging->entity_type, 'node', "hook_flag_unflag() gets a correct entity type on the Flagging obtained from flag_get_user_flags()");
1592
    $this->assertEqual($flag_get_user_flags_flagging->entity_id, $node->nid, "hook_flag_unflag() gets a correct entity ID on the Flagging obtained from flag_get_user_flags()");
1593

    
1594
    // The flag counts have already been decreased.
1595
    $flag_get_counts = $hook_data_variable['hook_flag_unflag']['api_calls']['flag_get_counts'];
1596
    $this->assertEqual($flag_get_counts, array(), "hook_flag_unflag() gets a correct flag count from flag_get_counts().");
1597

    
1598
    $flag_get_flag_counts = $hook_data_variable['hook_flag_unflag']['api_calls']['flag_get_flag_counts'];
1599
    $this->assertEqual($flag_get_flag_counts, 0, "hook_flag_unflag() gets a correct flag count from flag_get_flag_counts().");
1600

    
1601
    // flag_get_entity_flag_counts() queries the {flagging} table, so is not
1602
    // updated yet.
1603
    $flag_get_entity_flag_counts = $hook_data_variable['hook_flag_unflag']['api_calls']['flag_get_entity_flag_counts'];
1604
    $this->assertEqual($flag_get_entity_flag_counts, 1, "hook_flag_unflag() gets a correct flag count from flag_get_entity_flag_counts().");
1605

    
1606
    // flag_get_user_flag_counts() queries the {flagging} table, so is not
1607
    // updated yet.
1608
    $flag_get_user_flag_counts = $hook_data_variable['hook_flag_unflag']['api_calls']['flag_get_user_flag_counts'];
1609
    $this->assertEqual($flag_get_user_flag_counts, 1, "hook_flag_unflag() gets a correct flag count from flag_get_user_flag_counts().");
1610

    
1611
    // Check the parameters received by hook_entity_delete().
1612
    // Param 0: $flagging.
1613
    $this->assertEqual($hook_data_variable['hook_entity_delete']['parameters'][0]->flag_name, $flag->name, "The Flagging entity passed to hook_entity_delete() has the expected flag name property.");
1614
    $this->assertEqual($hook_data_variable['hook_entity_delete']['parameters'][0]->fid, $flag->fid, "The Flagging entity passed to hook_entity_presave() has the expected fid property.");
1615
    $this->assertEqual($hook_data_variable['hook_entity_delete']['parameters'][0]->entity_type, 'node', "The Flagging entity passed to hook_entity_presave() has the expected entity type property.");
1616
    $this->assertEqual($hook_data_variable['hook_entity_delete']['parameters'][0]->entity_id, $node->nid, "The Flagging entity passed to hook_entity_delete() has the expected entity ID property.");
1617
    $this->assertEqual($hook_data_variable['hook_entity_delete']['parameters'][0]->uid, $this->flag_unflag_user->uid, "The Flagging entity passed to hook_entity_delete() has the expected uid property.");
1618
    $this->assertTrue(!empty($hook_data_variable['hook_entity_delete']['parameters'][0]->flagging_id), "The Flagging entity passed to hook_entity_delete() has the flagging ID set.");
1619
    // Param 1: $entity_type.
1620
    $this->assertEqual($hook_data_variable['hook_entity_delete']['parameters'][1], 'flagging', "hook_entity_delete() is passed the correct entity type.");
1621

    
1622
    // Check the API data available to hook_entity_delete().
1623
    //debug($hook_data_variable['hook_entity_delete']['api_calls']);
1624
    // The unflagging is not yet done, so hook_entity_delete() will find the
1625
    // flagging data.
1626
    $flag_get_entity_flags = $hook_data_variable['hook_entity_delete']['api_calls']['flag_get_entity_flags'];
1627
    $flag_get_entity_flags_uids = array_keys($flag_get_entity_flags);
1628
    $this->assertEqual($flag_get_entity_flags_uids, array($this->flag_unflag_user->uid), "hook_entity_delete() gets correct data for flagging users from flag_get_entity_flags()");
1629
    $flag_get_entity_flags_flagging = $flag_get_entity_flags[$this->flag_unflag_user->uid];
1630
    $this->assertEqual($flag_get_entity_flags_flagging->fid, $flag->fid, "hook_entity_delete() gets a correct fid on the Flagging obtained from flag_get_entity_flags()");
1631
    $this->assertEqual($flag_get_entity_flags_flagging->entity_type, 'node', "hook_entity_delete() gets a correct entity type on the Flagging obtained from flag_get_entity_flags()");
1632
    $this->assertEqual($flag_get_entity_flags_flagging->entity_id, $node->nid, "hook_entity_delete() gets a correct entity ID on the Flagging obtained from flag_get_entity_flags()");
1633

    
1634
    $flag_get_user_flags = $hook_data_variable['hook_entity_delete']['api_calls']['flag_get_user_flags'];
1635
    $flag_get_user_flags_flagging = $flag_get_user_flags[$flag->name];
1636
    $this->assertEqual($flag_get_user_flags_flagging->fid, $flag->fid, "hook_entity_delete() gets a correct fid on the Flagging obtained from flag_get_user_flags()");
1637
    $this->assertEqual($flag_get_user_flags_flagging->entity_type, 'node', "hook_entity_delete() gets a correct entity type on the Flagging obtained from flag_get_user_flags()");
1638
    $this->assertEqual($flag_get_user_flags_flagging->entity_id, $node->nid, "hook_entity_delete() gets a correct entity ID on the Flagging obtained from flag_get_user_flags()");
1639

    
1640
    // The flag counts have already been decreased.
1641
    $flag_get_counts = $hook_data_variable['hook_entity_delete']['api_calls']['flag_get_counts'];
1642
    $this->assertEqual($flag_get_counts, array(), "hook_entity_delete() gets a correct flag count from flag_get_counts().");
1643

    
1644
    $flag_get_flag_counts = $hook_data_variable['hook_entity_delete']['api_calls']['flag_get_flag_counts'];
1645
    $this->assertEqual($flag_get_flag_counts, 0, "hook_entity_delete() gets a correct flag count from flag_get_flag_counts().");
1646

    
1647
    // flag_get_entity_flag_counts() queries the {flagging} table, so is not
1648
    // updated yet.
1649
    $flag_get_entity_flag_counts = $hook_data_variable['hook_entity_delete']['api_calls']['flag_get_entity_flag_counts'];
1650
    $this->assertEqual($flag_get_entity_flag_counts, 1, "hook_entity_delete() gets a correct flag count from flag_get_entity_flag_counts().");
1651

    
1652
    // flag_get_user_flag_counts() queries the {flagging} table, so is not
1653
    // updated yet.
1654
    $flag_get_user_flag_counts = $hook_data_variable['hook_entity_delete']['api_calls']['flag_get_user_flag_counts'];
1655
    $this->assertEqual($flag_get_user_flag_counts, 1, "hook_entity_delete() gets a correct flag count from flag_get_user_flag_counts().");
1656
  }
1657

    
1658
}