Projet

Général

Profil

Paste
Télécharger (67,4 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / flag / tests / flag.test @ b08d2851

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' => 'Flagging CRUD',
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
  var $_flag = FALSE;
220

    
221
  /**
222
   * Implements getInfo().
223
   */
224
  public static function getInfo() {
225
    return array(
226
      'name' => 'Flag admin tests',
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('full' => 0, 'teaser' => 0, 'rss' => 0, 'search_index' => 0, 'search_result' => 0);
281
    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]']);
282

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

    
285
    drupal_static_reset('flag_get_flags');
286
    $flag = flag_get_flag($edit['name']);
287
    // Load the roles array for checking it matches.
288
    $flag->fetch_roles();
289

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

    
293
    // Check each individual property of the flag and make sure it was set.
294
    foreach ($saved as $property => $value) {
295
      $this->assertEqual($flag->$property, $value, t('Flag property %property properly saved.', array('%property' => $property)));
296
    }
297

    
298
    // Check permissions.
299
    $permissions = user_role_permissions(user_roles());
300
    foreach ($saved['roles'] as $action => $rids) {
301
      foreach ($rids as $rid) {
302
        $permission_string = "$action ". $saved['name'];
303
        $this->assertTrue(isset($permissions[$rid][$permission_string]), t('Permission %perm set for flag.', array(
304
          '%perm' => $permission_string,
305
        )));
306
      }
307
    }
308

    
309
    // Edit the flag through the UI.
310
    $edit = array(
311
      'name' => drupal_strtolower($this->randomName()),
312
      'title' => $this->randomName(),
313
      'flag_short'          => 'flag 2 short [node:nid]',
314
      'flag_long'           => 'flag 2 long [node:nid]',
315
      'flag_message'        => 'flag 2 message [node:nid]',
316
      'unflag_short'        => 'unflag 2 short [node:nid]',
317
      'unflag_long'         => 'unflag 2 long [node:nid]',
318
      'unflag_message'      => 'unflag 2 message [node:nid]',
319
      'roles[flag][2]' => TRUE,
320
      'roles[unflag][2]' => TRUE,
321
      'types[article]' => TRUE,
322
      'types[page]' => FALSE,
323
      'show_in_links[full]' => TRUE,
324
      'show_in_links[teaser]' => TRUE,
325
      'show_in_links[rss]' => FALSE,
326
      'show_in_links[search_index]' => FALSE,
327
      'show_in_links[search_result]' => FALSE,
328
      'show_on_form' => TRUE,
329
      'link_type' => 'normal',
330
    );
331
    $saved = $edit;
332
    $saved['roles'] = array('flag' => array(2), 'unflag' => array(2));
333
    $saved['types'] = array('article');
334
    $saved['show_in_links'] = array('full' => TRUE, 'teaser' => TRUE, 'rss' => 0, 'search_index' => 0, 'search_result' => 0);
335
    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]']);
336

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

    
339
    drupal_static_reset('flag_get_flags');
340
    $flag = flag_get_flag($edit['name']);
341
    // Load the roles array for checking it matches.
342
    $flag->fetch_roles();
343

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

    
347
    // Check each individual property of the flag and make sure it was set.
348
    foreach ($saved as $property => $value) {
349
      $this->assertEqual($flag->$property, $value, t('Flag property %property properly saved.', array('%property' => $property)));
350
    }
351

    
352
    // Clear the user access cache so our changes to permissions are noticed.
353
    drupal_static_reset('user_access');
354
    drupal_static_reset('user_role_permissions');
355

    
356
    // Check permissions.
357
    $permissions = user_role_permissions(user_roles());
358

    
359
    foreach ($saved['roles'] as $action => $rids) {
360
      foreach ($rids as $rid) {
361
        $permission_string = "$action ". $saved['name'];
362
        $this->assertTrue(isset($permissions[$rid][$permission_string]), t('Permission %perm set for flag.', array(
363
          '%perm' => $permission_string,
364
        )));
365
      }
366
    }
367

    
368
    // Delete the flag through the UI.
369
    $this->drupalPost(FLAG_ADMIN_PATH . '/manage/' . $flag->name . '/delete', array(), t('Delete'));
370
    drupal_static_reset('flag_get_flags');
371
    $this->assertFalse(flag_get_flag($flag->name), t('Flag successfully deleted.'));
372
  }
373

    
374
}
375

    
376
/**
377
 * Access to flags using the entity forms.
378
 *
379
 * @todo: complete this test class.
380
 */
381
class FlagAccessFormTestCase extends FlagTestCaseBase {
382

    
383
  /**
384
   * Implements getInfo().
385
   */
386
  public static function getInfo() {
387
    return array(
388
      'name' => 'Flag access: entity forms',
389
      'description' => 'Access to flag and unflag entities via entity forms.',
390
      'group' => 'Flag',
391
    );
392
  }
393

    
394
  /**
395
   * Implements setUp().
396
   */
397
  function setUp() {
398
    parent::setUp('flag');
399
  }
400

    
401
  /**
402
   * Test scenarios with no access to a global flag.
403
   */
404
  function testFlagAccessGlobalNone() {
405
    // Create a global flag on article nodes.
406
    $flag_data = array(
407
      'entity_type' => 'node',
408
      'name' => 'global_flag',
409
      'title' => 'Global Flag',
410
      'global' => 1,
411
      'types' => array(
412
        0 => 'article',
413
      ),
414
      'flag_short' => 'Flag this item',
415
      'flag_long' => '',
416
      'flag_message' => '',
417
      'unflag_short' => 'Unflag this item',
418
      'unflag_long' => '',
419
      'unflag_message' => '',
420
      'unflag_denied_text' => 'You may not unflag this item',
421
      'link_type' => 'normal',
422
      'weight' => 0,
423
      // Show the flag on the form.
424
      'show_on_form' => 1,
425
      'access_author' => '',
426
      'show_contextual_link' => 0,
427
      'show_in_links' => array(
428
        'full' => 1,
429
        'teaser' => 1,
430
      ),
431
      'i18n' => 0,
432
      'api_version' => 3,
433
    );
434
    $flag = $this->createFlag($flag_data);
435

    
436
    // Create test user who can't us this flag, but can create nodes.
437
    $no_flag_user = $this->drupalCreateUser(array('create article content'));
438
    $this->drupalLogin($no_flag_user);
439

    
440
    $this->drupalGet('node/add/article');
441

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

    
445
    // Have the user create a node.
446
    $edit = array(
447
      'title' => 'node 1',
448
    );
449
    $this->drupalPost('node/add/article', $edit, t('Save'));
450

    
451
    $node = $this->drupalGetNodeByTitle($edit["title"]);
452

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

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

    
459
    // Create another new node.
460
    $edit = array(
461
      'title' => 'node 2',
462
    );
463
    $this->drupalPost('node/add/article', $edit, t('Save'));
464

    
465
    $node = $this->drupalGetNodeByTitle($edit["title"]);
466

    
467
    // Check the new node has been flagged, despite the user not having access
468
    // to the flag.
469
    $this->assertTrue($flag->is_flagged($node->nid), t('New node is flagged.'));
470
  }
471

    
472
}
473

    
474
/**
475
 * Tokens we provide on generic entities.
476
 */
477
class FlagEntityTokensTestCase extends FlagTestCaseBase {
478

    
479
  /**
480
   * Implements getInfo().
481
   */
482
  public static function getInfo() {
483
    return array(
484
      'name' => 'Flag: Entity tokens',
485
      'description' => 'Tokens for flag count on entities.',
486
      'group' => 'Flag',
487
    );
488
  }
489

    
490
  /**
491
   * Implements setUp().
492
   */
493
  function setUp() {
494
    // Our entity tokens require token module.
495
    parent::setUp('flag', 'token');
496
  }
497

    
498
  /**
499
   * Test tokens on nodes.
500
   */
501
  function testNodeFlagToken() {
502
    // Create a flag on article nodes.
503
    $flag_data = array(
504
      'entity_type' => 'node',
505
      'name' => 'node_flag',
506
      'title' => 'Node Flag',
507
      'global' => 0,
508
      'types' => array(
509
        0 => 'article',
510
      ),
511
      'flag_short' => 'Flag this item',
512
      'flag_long' => '',
513
      'flag_message' => '',
514
      'unflag_short' => 'Unflag this item',
515
      'unflag_long' => '',
516
      'unflag_message' => '',
517
      'unflag_denied_text' => 'You may not unflag this item',
518
      'link_type' => 'normal',
519
      'weight' => 0,
520
      // Show the flag on the form.
521
      'show_on_form' => 1,
522
      'access_author' => '',
523
      'show_contextual_link' => 0,
524
      'show_in_links' => array(
525
        'full' => 1,
526
        'teaser' => 1,
527
      ),
528
      'i18n' => 0,
529
      'api_version' => 3,
530
    );
531
    $flag = $this->createFlag($flag_data);
532

    
533
    // Create a node to flag.
534
    $node = (object) array(
535
      'type' => 'article',
536
      'title' => $this->randomName(),
537
    );
538
    node_save($node);
539

    
540
    // Flag it by several users.
541
    $flag_user_1 = $this->drupalCreateUser(array('flag node_flag',));
542

    
543
    // Flag the node as the user.
544
    $flag = flag_get_flag('node_flag');
545
    $flag->flag('flag', $node->nid, $flag_user_1);
546

    
547
    $flag_user_2 = $this->drupalCreateUser(array('flag node_flag',));
548

    
549
    // Flag the node as the user.
550
    $flag->flag('flag', $node->nid, $flag_user_2);
551

    
552
    $text = '[node:flag-node-flag-count]';
553

    
554
    $replaced_text = token_replace($text, array('node' => $node));
555

    
556
    $this->assertEqual($replaced_text, 2, "The flag count token for the node is correct.");
557
  }
558

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

    
596
    $vocabulary = taxonomy_vocabulary_load(1);
597

    
598
    // Create a term to flag.
599
    $term = (object) array(
600
      'name' => $this->randomName(),
601
      'vid' => 1,
602
    );
603
    taxonomy_term_save($term);
604

    
605
    // Flag it by several users.
606
    $flag_user_1 = $this->drupalCreateUser(array('flag term_flag',));
607

    
608
    // Flag the term as the user.
609
    $flag = flag_get_flag('term_flag');
610
    $flag->flag('flag', $term->tid, $flag_user_1);
611

    
612
    $flag_user_2 = $this->drupalCreateUser(array('flag term_flag',));
613

    
614
    // Flag the term as the user.
615
    $flag = flag_get_flag('term_flag');
616
    $flag->flag('flag', $term->tid, $flag_user_2);
617

    
618
    $text = '[term:flag-term-flag-count]';
619

    
620
    $replaced_text = token_replace($text, array('term' => $term));
621

    
622
    debug($replaced_text);
623

    
624
    $this->assertEqual($replaced_text, 2, "The flag count token for the term is correct.");
625
  }
626

    
627
}
628

    
629
/**
630
 * Access to flags using the basic flag link.
631
 */
632
class FlagAccessLinkTestCase extends FlagTestCaseBase {
633

    
634
  /**
635
   * Implements getInfo().
636
   */
637
  public static function getInfo() {
638
    return array(
639
      'name' => 'Flag access tests',
640
      'description' => 'Access to flag and unflag entities using the basic link.',
641
      'group' => 'Flag',
642
    );
643
  }
644

    
645
  /**
646
   * Implements setUp().
647
   */
648
  function setUp() {
649
    parent::setUp('flag');
650

    
651
    // Create a test flag on article nodes.
652
    $flag_data = array(
653
      'entity_type' => 'node',
654
      'name' => 'test_flag',
655
      'title' => 'Test Flag',
656
      'global' => 0,
657
      'types' => array(
658
        0 => 'article',
659
      ),
660
      'flag_short' => 'Flag this item',
661
      'flag_long' => '',
662
      'flag_message' => '',
663
      'unflag_short' => 'Unflag this item',
664
      'unflag_long' => '',
665
      'unflag_message' => '',
666
      'unflag_denied_text' => 'You may not unflag this item',
667
      // Use the normal link type as it involves no intermediary page loads.
668
      'link_type' => 'normal',
669
      'weight' => 0,
670
      'show_on_form' => 0,
671
      'access_author' => '',
672
      'show_contextual_link' => 0,
673
      'show_in_links' => array(
674
        'full' => 1,
675
        'teaser' => 1,
676
      ),
677
      'i18n' => 0,
678
      'api_version' => 3,
679
    );
680
    $flag = $this->createFlag($flag_data);
681

    
682
    // Create an article node that various users will try to flag.
683
    $title = $this->randomName(8);
684
    $node = array(
685
      'title' => $title,
686
      'body' => array(LANGUAGE_NONE => array(array('value' => $this->randomName(32)))),
687
      'uid' => 1,
688
      'type' => 'article',
689
      'is_new' => TRUE,
690
    );
691
    $node = node_submit((object) $node);
692
    node_save($node);
693
    $this->nid = $node->nid;
694
  }
695

    
696
  /**
697
   * Test that a user without flag access can't see the flag.
698
   */
699
  function testFlagAccessNone() {
700
    // Create test user who can't flag at all.
701
    $no_flag_user = $this->drupalCreateUser(array());
702
    $this->drupalLogin($no_flag_user);
703

    
704
    // Look at our node.
705
    $this->drupalGet('node/' . $this->nid);
706

    
707
    $this->assertNoLink('Flag this item', 0, 'The flag link does not appear on the page');
708
  }
709

    
710
  /**
711
   * Test that a user with only flag access can flag but not unflag.
712
   */
713
  function testFlagAccessFlagOnly() {
714
    // Create test user who can flag but not unflag.
715
    $flag_user = $this->drupalCreateUser(array('flag test_flag',));
716
    $this->drupalLogin($flag_user);
717

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

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

    
723
    // Click the link to flag the node.
724
    $this->clickLink(t('Flag this item'));
725

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

    
729
  /**
730
   * Test that a user with flag access can flag and unflag.
731
   */
732
  function testFlagAccessFlagUnflag() {
733
    // Create test user who can flag and unflag.
734
    $flag_unflag_user = $this->drupalCreateUser(array('flag test_flag', 'unflag test_flag'));
735
    $this->drupalLogin($flag_unflag_user);
736

    
737
    // Look at our node.
738
    $this->drupalGet('node/' . $this->nid);
739

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

    
742
    // Click the link to flag the node.
743
    $this->clickLink(t('Flag this item'));
744

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

    
747
    // Click the link to unflag the node.
748
    $this->clickLink(t('Unflag this item'));
749

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

    
753
}
754

    
755
/**
756
 * Test the 'confirm form' link type.
757
 */
758
class FlagLinkTypeConfirmTestCase extends DrupalWebTestCase {
759

    
760
  /**
761
   * Implements getInfo().
762
   */
763
  public static function getInfo() {
764
    return array(
765
      'name' => 'Flag confirm link tests',
766
      'description' => 'Flag confirm form link type.',
767
      'group' => 'Flag',
768
    );
769
  }
770

    
771
  /**
772
   * Implements setUp().
773
   */
774
  function setUp() {
775
    parent::setUp('flag');
776

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

    
813
    // Reset permissions so that permissions for this flag are available.
814
    $this->checkPermissions(array(), TRUE);
815

    
816
    // Create test user who can flag and unflag.
817
    $this->flag_unflag_user = $this->drupalCreateUser(array('flag test_flag', 'unflag test_flag'));
818
    $this->drupalLogin($this->flag_unflag_user);
819

    
820
    // Create an article node to flag and unflag.
821
    $title = $this->randomName(8);
822
    $node = array(
823
      'title' => $title,
824
      'body' => array(LANGUAGE_NONE => array(array('value' => $this->randomName(32)))),
825
      'uid' => 1,
826
      'type' => 'article',
827
      'is_new' => TRUE,
828
    );
829
    $node = node_submit((object) $node);
830
    node_save($node);
831
    $this->nid = $node->nid;
832
  }
833

    
834
  /**
835
   * Test usage of the flag confirm form.
836
   */
837
  function testFlag() {
838
    // Look at our node.
839
    $this->drupalGet('node/' . $this->nid);
840

    
841
    $this->assertLink($this->flag_data['flag_short'], 0, 'The flag link appears on the page');
842

    
843
    // Click the link to flag the node.
844
    $this->clickLink($this->flag_data['flag_short']);
845

    
846
    $this->assertUrl('flag/confirm/flag/test_flag/' . $this->nid, array(
847
      'query' => array(
848
        'destination' => 'node/' . $this->nid,
849
      ),
850
    ), 'On confirm flagging form page.');
851

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

    
854
    // Click the button to confirm the flagging.
855
    $this->drupalPost(NULL, array(), $this->flag_data['flag_short']);
856

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

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

    
864
    // Click the link to unflag the node.
865
    $this->clickLink($this->flag_data['unflag_short']);
866

    
867
    $this->assertUrl('flag/confirm/unflag/test_flag/' . $this->nid, array(
868
      'query' => array(
869
        'destination' => 'node/' . $this->nid,
870
      ),
871
    ), t('On confirm unflagging form page.'));
872

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

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

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

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

    
885
}
886

    
887
/**
888
 * Verifies the implementation of hook_flag_access().
889
 */
890
class FlagHookFlagAccessTestCase extends FlagTestCaseBase {
891

    
892
  /**
893
   * Implements getInfo().
894
   */
895
  public static function getInfo() {
896
    return array(
897
      'name' => 'Flag hook_flag_access() tests',
898
      'description' => 'Checks the ability of modules to use hook_flag_access().',
899
      'group' => 'Flag',
900
    );
901
  }
902

    
903
  /**
904
   * Implements setUp().
905
   */
906
  function setUp() {
907
    parent::setUp('flag');
908

    
909
    $success = module_enable(array('flagaccesstest'), FALSE);
910

    
911
    // Create a test flag on article nodes.
912
    $flag_data = array(
913
      'entity_type' => 'node',
914
      'name' => 'test_flag',
915
      'title' => 'Test Flag',
916
      'global' => 0,
917
      'types' => array(
918
        0 => 'article',
919
      ),
920
      'flag_short' => 'Flag this item',
921
      'flag_long' => '',
922
      'flag_message' => '',
923
      'unflag_short' => 'Unflag this item',
924
      'unflag_long' => '',
925
      'unflag_message' => '',
926
      'unflag_denied_text' => 'You may not unflag this item',
927
      // Use the normal link type as it involves no intermediary page loads.
928
      'link_type' => 'normal',
929
      'weight' => 0,
930
      'show_on_form' => 0,
931
      'access_author' => '',
932
      'show_contextual_link' => 0,
933
      'show_in_links' => array(
934
        'full' => 1,
935
        'teaser' => 1,
936
      ),
937
      'i18n' => 0,
938
      'api_version' => 3,
939
    );
940
    $flag = $this->createFlag($flag_data);
941

    
942
    // Create an article node that various users will try to flag.
943
    $title = $this->randomName(8);
944
    $node = array(
945
      'title' => $title,
946
      'body' => array(LANGUAGE_NONE => array(array('value' => $this->randomName(32)))),
947
      'uid' => 1,
948
      'type' => 'article',
949
      'is_new' => TRUE,
950
    );
951
    $node = node_submit((object) $node);
952
    node_save($node);
953
    $this->nid = $node->nid;
954
  }
955

    
956
  /**
957
   * Verifies that the user sees the flag if a module returns NULL (Ignore).
958
   */
959
  function testFlagAccessIgnore() {
960
    variable_set('FlagHookFlagAccessTestCaseMode', 'ignore');
961
    $flag_user = $this->drupalCreateUser(array('flag test_flag', 'unflag test_flag'));
962
    $this->drupalLogin($flag_user);
963

    
964
    // Look at our node.
965
    $this->drupalGet('node/' . $this->nid);
966

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

    
969
    // Click the link to flag the node.
970
    $this->clickLink(t('Flag this item'));
971

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

    
974
    // Click the link to unflag the node.
975
    $this->clickLink(t('Unflag this item'));
976

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

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

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

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

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

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

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

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

    
1004
  /**
1005
   * Verifies that the user sees the flag if a module returns TRUE (Allow) to override default access check.
1006
   */
1007
  function testFlagAccessAllowOverride() {
1008
    variable_set('FlagHookFlagAccessTestCaseMode', 'allow');
1009
    $flag_user = $this->drupalCreateUser(array());
1010
    $this->drupalLogin($flag_user);
1011

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

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

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

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

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

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

    
1028
  /**
1029
   * Verifies that the user does not see the flag if a module returns FALSE (Deny).
1030
   */
1031
  function testFlagAccessDeny() {
1032
    variable_set('FlagHookFlagAccessTestCaseMode', 'deny');
1033
    $flag_user = $this->drupalCreateUser(array('flag test_flag', 'unflag test_flag'));
1034
    $this->drupalLogin($flag_user);
1035

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

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

    
1042
}
1043

    
1044
/**
1045
 * Test use of EntityFieldQueries with flagging entities.
1046
 */
1047
class FlagEntityFieldQueryTestCase extends FlagTestCaseBase {
1048

    
1049
  /**
1050
   * Implements getInfo().
1051
   */
1052
  public static function getInfo() {
1053
     return array(
1054
      'name' => 'Flagging Entity Field Query Extension',
1055
      'description' => 'Entity Field Query for flagging entities.',
1056
      'group' => 'Flag',
1057
    );
1058
  }
1059

    
1060
  /**
1061
   * Implements setUp().
1062
   */
1063
  function setUp() {
1064
    parent::setUp('flag');
1065

    
1066
    $flag_data = array(
1067
      'entity_type' => 'node',
1068
      'name' => 'test_flag_1',
1069
      'title' => 'Test Flag',
1070
      'global' => 0,
1071
      'types' => array(
1072
        0 => 'article',
1073
      ),
1074
      'flag_short' => 'Flag this item',
1075
      'flag_long' => '',
1076
      'flag_message' => '',
1077
      'unflag_short' => 'Unflag this item',
1078
      'unflag_long' => '',
1079
      'unflag_message' => '',
1080
      'unflag_denied_text' => 'You may not unflag this item',
1081
      // Use the normal link type as it involves no intermediary page loads.
1082
      'link_type' => 'normal',
1083
      'weight' => 0,
1084
      'show_on_form' => 0,
1085
      'access_author' => '',
1086
      'show_contextual_link' => 0,
1087
      'show_in_links' => array(
1088
        'full' => 1,
1089
        'teaser' => 1,
1090
      ),
1091
      'i18n' => 0,
1092
      'api_version' => 3,
1093
    );
1094

    
1095
    $this->flag1 = $this->createFlag($flag_data);
1096
    $flag_data['name'] = 'test_flag_2';
1097
    $this->flag2 = $this->createFlag($flag_data);
1098
    $flag_data['name'] = 'test_flag_3';
1099
    $this->flag3 = $this->createFlag($flag_data);
1100

    
1101
    // Create test user who can flag and unflag.
1102
    $this->flag_unflag_user = $this->drupalCreateUser(array('flag test_flag_1', 'unflag test_flag_1', 'flag test_flag_2', 'unflag test_flag_2'));
1103
    $this->drupalLogin($this->flag_unflag_user);
1104

    
1105
  }
1106

    
1107
  /**
1108
   * Test use of EntityFieldQuery with flagging entities.
1109
   */
1110
  function testEntityFieldQuery() {
1111
     $node_settings = array(
1112
      'title' => $this->randomName(),
1113
      'body' => array(LANGUAGE_NONE => array(array('value' => $this->randomName(32)))),
1114
      'uid' => 1,
1115
      'type' => 'article',
1116
      'is_new' => TRUE,
1117
    );
1118
    $node = $this->drupalCreateNode($node_settings);
1119

    
1120
    flag('flag', 'test_flag_1', $node->nid, $this->flag_unflag_user);
1121
    flag('flag', 'test_flag_2', $node->nid, $this->flag_unflag_user);
1122

    
1123
    $query = new EntityFieldQuery();
1124
    $query->entityCondition('entity_type', 'flagging')
1125
      ->entityCondition('bundle', 'test_flag_1');
1126

    
1127
    $flagged = $query->execute();
1128
    $this->assertEqual(count($flagged['flagging']), 1);
1129

    
1130
    $query = new EntityFieldQuery();
1131
    $query->entityCondition('entity_type', 'flagging')
1132
      ->entityCondition('bundle', 'test%', 'like');
1133
    $flagged = $query->execute();
1134
    $this->assertEqual(count($flagged['flagging']), 2);
1135

    
1136
    $query = new EntityFieldQuery();
1137
    $query->entityCondition('entity_type', 'flagging')
1138
      ->entityCondition('bundle', array('test_flag_1', 'test_flag_2'), 'IN');
1139
    $this->assertEqual(count($flagged['flagging']), 2);
1140
  }
1141

    
1142
}
1143

    
1144
/**
1145
 * Verifies the invocation of hooks when performing flagging and unflagging.
1146
 */
1147
class FlagHookInvocationsTestCase extends FlagTestCaseBase {
1148

    
1149
  /**
1150
   * Implements getInfo().
1151
   */
1152
  public static function getInfo() {
1153
     return array(
1154
      'name' => 'Flag hook invocations',
1155
      'description' => 'Invocation of flag and entity hooks and rules during flagging and unflagging.',
1156
      'group' => 'Flag',
1157
    );
1158
  }
1159

    
1160
  /**
1161
   * Implements setUp().
1162
   */
1163
  function setUp() {
1164
    parent::setUp('flag', 'rules', 'flag_hook_test');
1165

    
1166
    // Note the test module contains our test flag.
1167

    
1168
    // Create test user who can flag and unflag.
1169
    $this->flag_unflag_user = $this->drupalCreateUser(array('flag flag_hook_test_flag', 'unflag flag_hook_test_flag'));
1170
    $this->drupalLogin($this->flag_unflag_user);
1171
  }
1172

    
1173
  /**
1174
   * Test invocation of hooks and their data during flagging and unflagging.
1175
   *
1176
   * For each operation (flagging, re-flagging, unflagging) we test:
1177
   *  - the order in which Flag hooks, entity hooks, and rules are invoked.
1178
   *  - the parameters each hook receives
1179
   *  - the data that a hook implementation obtains when it calls the Flag data
1180
   *    API functions.
1181
   */
1182
  function testHookInvocation() {
1183
    // Create an article node that we try to create a flagging entity for.
1184
    $title = $this->randomName(8);
1185
    $node = array(
1186
      'title' => $title,
1187
      'body' => array(LANGUAGE_NONE => array(array('value' => $this->randomName(32)))),
1188
      'uid' => 1,
1189
      'type' => 'article',
1190
      'is_new' => TRUE,
1191
    );
1192
    $node = node_submit((object) $node);
1193
    node_save($node);
1194

    
1195
    // Initialize a tracking variable. The test module will update this when
1196
    // its hooks are invoked.
1197
    variable_set('flag_hook_test_hook_tracking', array());
1198

    
1199
    // Flag the node as the user.
1200
    $flag = flag_get_flag('flag_hook_test_flag');
1201
    $flag->flag('flag', $node->nid, $this->flag_unflag_user);
1202

    
1203
    // Get the variable the test module sets the hook order into.
1204
    $hook_data_variable = variable_get('flag_hook_test_hook_tracking', array());
1205
    //debug($hook_data_variable['hook_entity_presave']);
1206
    //debug($hook_data_variable['hook_entity_insert']);
1207

    
1208
    $expected_hook_order = array(
1209
      'hook_entity_presave',
1210
      'hook_entity_insert',
1211
      'hook_flag_flag',
1212
      'rules_event',
1213
    );
1214

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

    
1218
    // Check the parameters received by hook_entity_presave().
1219
    // Param 0: $flagging.
1220
    // There is a bug with what is passed to entity hooks; for now, just check
1221
    // the things that do get passed.
1222
    // See https://drupal.org/node/2196055.
1223
    $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.");
1224
    //$this->assertEqual($hook_data_variable['hook_entity_insert']['parameters'][0]->fid, $flag->fid);
1225
    //$this->assertEqual($hook_data_variable['hook_entity_insert']['parameters'][0]->entity_type, 'node');
1226
    $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.");
1227
    $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.");
1228
    // Param 1: $entity_type.
1229
    $this->assertEqual($hook_data_variable['hook_entity_presave']['parameters'][1], 'flagging', "hook_entity_presave() is passed the correct entity type.");
1230

    
1231
    // Check the API data available to hook_entity_presave().
1232
    //debug($hook_data_variable['hook_entity_presave']['api_calls']);
1233
    $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.");
1234
    $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.");
1235
    // The flag counts have not yet been increased.
1236
    $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.");
1237
    $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.");
1238
    $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.");
1239
    $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.");
1240

    
1241
    // Check the parameters received by hook_entity_insert().
1242
    // Param 0: $flagging.
1243
    // See https://drupal.org/node/2196055.
1244
    $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.");
1245
    //$this->assertEqual($hook_data_variable['hook_entity_insert']['parameters'][0]->fid, $flag->fid);
1246
    //$this->assertEqual($hook_data_variable['hook_entity_insert']['parameters'][0]->entity_type, 'node');
1247
    $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.");
1248
    $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.");
1249
    $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.");
1250
    // Param 1: $entity_type.
1251
    $this->assertEqual($hook_data_variable['hook_entity_insert']['parameters'][1], 'flagging', "hook_entity_insert() is passed the correct entity type.");
1252

    
1253
    // Check the API data available to hook_entity_insert().
1254
    //debug($hook_data_variable['hook_entity_insert']['api_calls']);
1255
    $flag_get_entity_flags = $hook_data_variable['hook_entity_insert']['api_calls']['flag_get_entity_flags'];
1256
    $flag_get_entity_flags_uids = array_keys($flag_get_entity_flags);
1257
    $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()");
1258
    $flag_get_entity_flags_flagging = $flag_get_entity_flags[$this->flag_unflag_user->uid];
1259
    $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()");
1260
    $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()");
1261
    $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()");
1262

    
1263
    $flag_get_user_flags = $hook_data_variable['hook_entity_insert']['api_calls']['flag_get_user_flags'];
1264
    $flag_get_user_flags_flagging = $flag_get_user_flags[$flag->name];
1265
    $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()");
1266
    $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()");
1267
    $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()");
1268

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

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

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

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

    
1282
    // Check the parameters received by hook_flag_flag().
1283
    // Param 0: $flag.
1284
    $this->assertEqual($hook_data_variable['hook_flag_flag']['parameters'][0], $flag, "The flag object is passed to hook_flag_flag().");
1285
    // Param 1: $entity_id.
1286
    $this->assertEqual($hook_data_variable['hook_flag_flag']['parameters'][1], $node->nid, "The entity ID is passed to hook_flag_flag().");
1287
    // Param 2: $account.
1288
    $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().");
1289
    // Param 3: $flagging.
1290
    // See https://drupal.org/node/2196055.
1291
    $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.");
1292
    //$this->assertEqual($hook_data_variable['hook_entity_insert']['parameters'][3]->fid, $flag->fid);
1293
    //$this->assertEqual($hook_data_variable['hook_entity_insert']['parameters'][3]->entity_type, 'node');
1294
    $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.");
1295
    $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.");
1296

    
1297
    // Check the API data available to hook_flag_flag().
1298
    //debug($hook_data_variable['hook_flag_flag']['api_calls']);
1299
    $flag_get_entity_flags = $hook_data_variable['hook_flag_flag']['api_calls']['flag_get_entity_flags'];
1300
    $flag_get_entity_flags_uids = array_keys($flag_get_entity_flags);
1301
    $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()");
1302
    $flag_get_entity_flags_flagging = $flag_get_entity_flags[$this->flag_unflag_user->uid];
1303
    $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()");
1304
    $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()");
1305
    $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()");
1306

    
1307
    $flag_get_user_flags = $hook_data_variable['hook_flag_flag']['api_calls']['flag_get_user_flags'];
1308
    $flag_get_user_flags_flagging = $flag_get_user_flags[$flag->name];
1309
    $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()");
1310
    $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()");
1311
    $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()");
1312

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

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

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

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

    
1326
    // Clear the tracking variable.
1327
    variable_set('flag_hook_test_hook_tracking', array());
1328

    
1329
    // Get the Flagging, and re-flag the node.
1330
    // This does nothing in our case, but is the API for updating a Flagging
1331
    // entity with changes to its Field API fields.
1332
    // Query the database to get the Flagging ID rather than Flag API so we
1333
    // don't interefere with any caches.
1334
    $query = db_select('flagging', 'f');
1335
    $query->addField('f', 'flagging_id');
1336
    $query->condition('fid', $flag->fid)
1337
      ->condition('entity_id', $node->nid);
1338
    $flagging_id = $query
1339
      ->execute()
1340
      ->fetchField();
1341
    $flagging = flagging_load($flagging_id);
1342

    
1343
    // Re-flag the node passing in the flagging entity.
1344
    $flag->flag('flag', $node->nid, $this->flag_unflag_user, FALSE, $flagging);
1345

    
1346
    // Get the variable the test module sets the hook order into.
1347
    $hook_data_variable = variable_get('flag_hook_test_hook_tracking', array());
1348
    //debug($hook_data_variable);
1349

    
1350
    $expected_hook_order = array(
1351
      'hook_entity_presave',
1352
      'hook_entity_update',
1353
      // Note that hook_flag() and the rule are not invoked, as this is not a
1354
      // new act of flagging.
1355
    );
1356

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

    
1360
    // Check the parameters received by hook_entity_presave().
1361
    // Param 0: $flagging.
1362
    // There is a bug with what is passed to entity hooks; for now, just check
1363
    // the things that do get passed.
1364
    // See https://drupal.org/node/2196055.
1365
    $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.");
1366
    //$this->assertEqual($hook_data_variable['hook_entity_insert']['parameters'][0]->fid, $flag->fid);
1367
    //$this->assertEqual($hook_data_variable['hook_entity_insert']['parameters'][0]->entity_type, 'node');
1368
    $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.");
1369
    $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.");
1370
    // Param 1: $entity_type.
1371
    $this->assertEqual($hook_data_variable['hook_entity_presave']['parameters'][1], 'flagging', "hook_entity_presave() is passed the correct entity type.");
1372

    
1373
    // Check the API data available to hook_entity_presave().
1374
    //debug($hook_data_variable['hook_entity_presave']['api_calls']);
1375
    $flag_get_entity_flags = $hook_data_variable['hook_entity_presave']['api_calls']['flag_get_entity_flags'];
1376
    $flag_get_entity_flags_uids = array_keys($flag_get_entity_flags);
1377
    $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()");
1378
    $flag_get_entity_flags_flagging = $flag_get_entity_flags[$this->flag_unflag_user->uid];
1379
    $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()");
1380
    $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()");
1381
    $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()");
1382

    
1383
    $flag_get_user_flags = $hook_data_variable['hook_entity_presave']['api_calls']['flag_get_user_flags'];
1384
    $flag_get_user_flags_flagging = $flag_get_user_flags[$flag->name];
1385
    $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()");
1386
    $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()");
1387
    $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()");
1388

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

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

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

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

    
1402
    // Check the parameters received by hook_entity_update().
1403
    // Param 0: $flagging.
1404
    // See https://drupal.org/node/2196055.
1405
    $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.");
1406
    //$this->assertEqual($hook_data_variable['hook_entity_update']['parameters'][0]->fid, $flag->fid);
1407
    //$this->assertEqual($hook_data_variable['hook_entity_update']['parameters'][0]->entity_type, 'node');
1408
    $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.");
1409
    $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.");
1410
    $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.");
1411
    // Param 1: $entity_type.
1412
    $this->assertEqual($hook_data_variable['hook_entity_update']['parameters'][1], 'flagging', "hook_entity_update() is passed the correct entity type.");
1413

    
1414
    // Check the API data available to hook_entity_update().
1415
    //debug($hook_data_variable['hook_entity_update']['api_calls']);
1416
    $flag_get_entity_flags = $hook_data_variable['hook_entity_update']['api_calls']['flag_get_entity_flags'];
1417
    $flag_get_entity_flags_uids = array_keys($flag_get_entity_flags);
1418
    $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()");
1419
    $flag_get_entity_flags_flagging = $flag_get_entity_flags[$this->flag_unflag_user->uid];
1420
    $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()");
1421
    $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()");
1422
    $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()");
1423

    
1424
    $flag_get_user_flags = $hook_data_variable['hook_entity_update']['api_calls']['flag_get_user_flags'];
1425
    $flag_get_user_flags_flagging = $flag_get_user_flags[$flag->name];
1426
    $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()");
1427
    $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()");
1428
    $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()");
1429

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

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

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

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

    
1443
    // Clear the tracking variable.
1444
    variable_set('flag_hook_test_hook_tracking', array());
1445

    
1446
    // Unflag the node as the user.
1447
    $flag->flag('unflag', $node->nid, $this->flag_unflag_user);
1448

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

    
1453
    $expected_hook_order = array(
1454
      'hook_flag_unflag',
1455
      'rules_event',
1456
      'hook_entity_delete',
1457
    );
1458

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

    
1462
    // Check the parameters received by hook_flag_unflag().
1463
    // Param 0: $flag.
1464
    $this->assertEqual($hook_data_variable['hook_flag_unflag']['parameters'][0], $flag, "The flag object is passed to hook_flag_unflag().");
1465
    // Param 1: $entity_id.
1466
    $this->assertEqual($hook_data_variable['hook_flag_unflag']['parameters'][1], $node->nid, "The entity ID is passed to hook_flag_unflag().");
1467
    // Param 2: $account.
1468
    $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().");
1469
    // Param 3: $flagging.
1470
    // See https://drupal.org/node/2196055.
1471
    $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.");
1472
    //$this->assertEqual($hook_data_variable['hook_entity_insert']['parameters'][3]->fid, $flag->fid);
1473
    //$this->assertEqual($hook_data_variable['hook_entity_insert']['parameters'][3]->entity_type, 'node');
1474
    $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.");
1475
    $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.");
1476

    
1477
    // Check the API data available to hook_flag_unflag().
1478
    //debug($hook_data_variable['hook_flag_unflag']['api_calls']);
1479
    // The unflagging is not yet done, so flag_get_entity_flags() will find the
1480
    // flagging data.
1481
    $flag_get_entity_flags = $hook_data_variable['hook_flag_unflag']['api_calls']['flag_get_entity_flags'];
1482
    $flag_get_entity_flags_uids = array_keys($flag_get_entity_flags);
1483
    $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()");
1484
    $flag_get_entity_flags_flagging = $flag_get_entity_flags[$this->flag_unflag_user->uid];
1485
    $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.");
1486
    $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()");
1487
    $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()");
1488

    
1489
    $flag_get_user_flags = $hook_data_variable['hook_flag_unflag']['api_calls']['flag_get_user_flags'];
1490
    $flag_get_user_flags_flagging = $flag_get_user_flags[$flag->name];
1491
    $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()");
1492
    $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()");
1493
    $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()");
1494

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

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

    
1502
    // flag_get_entity_flag_counts() queries the {flagging} table, so is not
1503
    // updated yet.
1504
    $flag_get_entity_flag_counts = $hook_data_variable['hook_flag_unflag']['api_calls']['flag_get_entity_flag_counts'];
1505
    $this->assertEqual($flag_get_entity_flag_counts, 1, "hook_flag_unflag() gets a correct flag count from flag_get_entity_flag_counts().");
1506

    
1507
    // flag_get_user_flag_counts() queries the {flagging} table, so is not
1508
    // updated yet.
1509
    $flag_get_user_flag_counts = $hook_data_variable['hook_flag_unflag']['api_calls']['flag_get_user_flag_counts'];
1510
    $this->assertEqual($flag_get_user_flag_counts, 1, "hook_flag_unflag() gets a correct flag count from flag_get_user_flag_counts().");
1511

    
1512
    // Check the parameters received by hook_entity_delete().
1513
    // Param 0: $flagging.
1514
    // See https://drupal.org/node/2196055.
1515
    $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.");
1516
    //$this->assertEqual($hook_data_variable['hook_entity_delete']['parameters'][0]->fid, $flag->fid);
1517
    //$this->assertEqual($hook_data_variable['hook_entity_delete']['parameters'][0]->entity_type, 'node');
1518
    $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.");
1519
    $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.");
1520
    $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.");
1521
    // Param 1: $entity_type.
1522
    $this->assertEqual($hook_data_variable['hook_entity_delete']['parameters'][1], 'flagging', "hook_entity_delete() is passed the correct entity type.");
1523

    
1524
    // Check the API data available to hook_entity_delete().
1525
    //debug($hook_data_variable['hook_entity_delete']['api_calls']);
1526
    // The unflagging is not yet done, so hook_entity_delete() will find the
1527
    // flagging data.
1528
    $flag_get_entity_flags = $hook_data_variable['hook_entity_delete']['api_calls']['flag_get_entity_flags'];
1529
    $flag_get_entity_flags_uids = array_keys($flag_get_entity_flags);
1530
    $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()");
1531
    $flag_get_entity_flags_flagging = $flag_get_entity_flags[$this->flag_unflag_user->uid];
1532
    $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()");
1533
    $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()");
1534
    $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()");
1535

    
1536
    $flag_get_user_flags = $hook_data_variable['hook_entity_delete']['api_calls']['flag_get_user_flags'];
1537
    $flag_get_user_flags_flagging = $flag_get_user_flags[$flag->name];
1538
    $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()");
1539
    $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()");
1540
    $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()");
1541

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

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

    
1549
    // flag_get_entity_flag_counts() queries the {flagging} table, so is not
1550
    // updated yet.
1551
    $flag_get_entity_flag_counts = $hook_data_variable['hook_entity_delete']['api_calls']['flag_get_entity_flag_counts'];
1552
    $this->assertEqual($flag_get_entity_flag_counts, 1, "hook_entity_delete() gets a correct flag count from flag_get_entity_flag_counts().");
1553

    
1554
    // flag_get_user_flag_counts() queries the {flagging} table, so is not
1555
    // updated yet.
1556
    $flag_get_user_flag_counts = $hook_data_variable['hook_entity_delete']['api_calls']['flag_get_user_flag_counts'];
1557
    $this->assertEqual($flag_get_user_flag_counts, 1, "hook_entity_delete() gets a correct flag count from flag_get_user_flag_counts().");
1558
  }
1559

    
1560
}