Projet

Général

Profil

Paste
Télécharger (23,3 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / uuid / uuid.test @ e7101f36

1
<?php
2

    
3
/**
4
 * @file
5
 * Test suite for UUID module.
6
 */
7

    
8
/**
9
 * Base class with some helper methods.
10
 */
11
class UUIDTestCase extends DrupalWebTestCase {
12

    
13
  /**
14
   * {@inheritdoc}
15
   */
16
  function setUp() {
17
    parent::setUp(func_get_args());
18
  }
19

    
20
  /**
21
   * Helper function that asserts a UUID.
22
   */
23
  function assertUUID($uuid, $message = NULL) {
24
    $this->assertTrue(uuid_is_valid($uuid), $message);
25
  }
26
}
27

    
28
/**
29
 * Tests the UUID API functions.
30
 */
31
class UUIDAPITestCase extends UUIDTestCase {
32

    
33
  /**
34
   * {@inheritdoc}
35
   */
36
  public static function getInfo() {
37
    return array(
38
      'name' => 'UUID API',
39
      'description' => 'Tests the UUID API functions.',
40
      'group' => 'UUID',
41
    );
42
  }
43

    
44
  /**
45
   * {@inheritdoc}
46
   */
47
  function setUp() {
48
    parent::setUp('uuid');
49
  }
50

    
51
  /**
52
   * Tests uuid function calls.
53
   */
54
  function testAPIFunctions() {
55
    // This is a valid UUID, we know that.
56
    $valid_uuid = '0ab26e6b-f074-4e44-9da6-1205fa0e9761';
57
    // Test the uuid_is_valid() function.
58
    $this->assertUUID($valid_uuid, 'UUID validation works.');
59

    
60
    // The default generator is 'php'.
61
    $uuid = uuid_generate();
62
    $this->assertUUID($uuid, 'PHP generator works.');
63

    
64
    // Test the 'mysql' generator.
65
    variable_set('uuid_generator', 'mysql');
66
    drupal_static_reset('uuid_generate');
67
    $uuid = uuid_generate();
68
    $this->assertUUID($uuid, 'MySQL generator works.');
69
  }
70
}
71

    
72
/**
73
 * Tests the Entity API functions.
74
 */
75
class UUIDEntityTestCase extends UUIDTestCase {
76

    
77
  /**
78
   * {@inheritdoc}
79
   */
80
  public static function getInfo() {
81
    return array(
82
      'name' => 'Entity API functions',
83
      'description' => 'Tests the Entity API functions.',
84
      'group' => 'UUID',
85
    );
86
  }
87

    
88
  /**
89
   * {@inheritdoc}
90
   */
91
  function setUp() {
92
    parent::setUp('uuid');
93
  }
94

    
95
  /**
96
   * Tests Entity API's UUID functions.
97
   */
98
  function testEntityAPIFunctions() {
99
    // Create some entities that we will work with.
100
    $user = $this->drupalCreateUser();
101
    $node = $this->drupalCreateNode(array('title' => 'original title', 'uid' => $user->uid));
102

    
103
    // Test entity_get_id_by_uuid().
104
    $nids = entity_get_id_by_uuid('node', array($node->uuid), FALSE);
105
    $this->assertTrue(in_array($node->nid, $nids), 'Lookup of entity ID works.');
106
    $vids = entity_get_id_by_uuid('node', array($node->vuuid), TRUE);
107
    $this->assertTrue(in_array($node->vid, $vids), 'Lookup of entity revision ID works.');
108

    
109
    // Test entity_get_uuid_by_id().
110
    $uuids = entity_get_uuid_by_id('node', array($node->nid), FALSE);
111
    $this->assertTrue(in_array($node->uuid, $uuids), 'Lookup of entity UUID works.');
112
    $vuuids = entity_get_uuid_by_id('node', array($node->vid), TRUE);
113
    $this->assertTrue(in_array($node->vuuid, $vuuids), 'Lookup of entity revision UUID works.');
114
  }
115
}
116

    
117
/**
118
 * Tests the User implementation.
119
 */
120
class UUIDUserTestCase extends UUIDTestCase {
121

    
122
  /**
123
   * {@inheritdoc}
124
   */
125
  public static function getInfo() {
126
    return array(
127
      'name' => 'User implementation',
128
      'description' => 'Tests the User implementation.',
129
      'group' => 'UUID',
130
    );
131
  }
132

    
133
  /**
134
   * {@inheritdoc}
135
   */
136
  function setUp() {
137
    // Some tests depends on the optional Entity API module.
138
    if (module_exists('entity')) {
139
      parent::setUp('uuid', 'entity');
140
    }
141
    else {
142
      parent::setUp('uuid');
143
    }
144
  }
145

    
146
  /**
147
   * Test CRUD on users with UUID functions.
148
   */
149
  function testUserCRUD() {
150
    $user = $this->drupalCreateUser();
151
    $this->assertUUID($user->uuid, 'User UUID was generated.');
152

    
153
    // Test updating user.
154
    $user_test = clone $user;
155
    user_save($user_test, array('name' => 'new name'));
156
    $user_test = user_load($user->uid, TRUE);
157
    $this->assertEqual($user_test->uuid, $user->uuid, 'User UUID was intact after update.');
158

    
159
    // Test entity_uuid_load().
160
    $users = entity_uuid_load('user', array($user->uuid), array(), TRUE);
161
    $user_test = reset($users);
162
    $this->assertEqual($user_test->uid, $user->uid, 'User was correctly loaded with UUID.');
163

    
164
    // The following tests depends on the optional Entity API module.
165
    if (module_exists('entity')) {
166
      // Test entity_uuid_save() for users.
167
      $user_test = clone $user;
168
      $user_test->uid = rand();
169
      $user_test->name = 'new name';
170
      entity_uuid_save('user', $user_test);
171
      $user_test = user_load($user->uid, TRUE);
172
      $this->assertEqual($user_test->name, 'new name', 'Saving user with UUID mapped to correct user.');
173
      $this->assertEqual($user_test->uuid, $user->uuid, 'User UUID was intact after saving with UUID.');
174

    
175
      // Test entity_uuid_delete() for users.
176
      entity_uuid_delete('user', $user->uuid);
177
      $user_test = user_load($user->uid);
178
      $this->assertFalse($user_test, 'Deleting user with UUID worked.');
179
    }
180
  }
181
}
182

    
183
/**
184
 * Tests the Node implementation.
185
 */
186
class UUIDNodeTestCase extends UUIDTestCase {
187

    
188
  /**
189
   * {@inheritdoc}
190
   */
191
  public static function getInfo() {
192
    return array(
193
      'name' => 'Node implementation',
194
      'description' => 'Tests the Node implementation.',
195
      'group' => 'UUID',
196
    );
197
  }
198

    
199
  /**
200
   * {@inheritdoc}
201
   */
202
  function setUp() {
203
    // Some tests depends on the optional Entity API module.
204
    if (module_exists('entity')) {
205
      parent::setUp('uuid', 'entity');
206
    }
207
    else {
208
      parent::setUp('uuid');
209
    }
210
  }
211

    
212
  /**
213
   * Tests CRUD on nodes with UUID functions.
214
   *
215
   * @todo
216
   *   Break out into multiple test methods to loosen coupling between tests.
217
   */
218
  function testNodeCRUD() {
219
    // Create some entities that we will work with.
220
    $user = $this->drupalCreateUser();
221
    $node = $this->drupalCreateNode(array('title' => 'original title', 'uid' => $user->uid));
222

    
223
    $this->assertUUID($node->uuid, 'Node UUID was generated.');
224
    $this->assertUUID($node->vuuid, 'Node revision UUID was generated.');
225

    
226
    // Test node update, without creating new revision.
227
    $node_test = clone $node;
228
    $node_test->title = 'original title';
229
    $node_test->revision = FALSE;
230
    node_save($node_test);
231
    $node_test = node_load($node->nid, FALSE, TRUE);
232
    $this->assertEqual($node_test->uuid, $node->uuid, 'Node UUID was intact after update, when not creating new revision.');
233
    $this->assertEqual($node_test->vuuid, $node->vuuid, 'Node revision UUID was intact after updating, when not creating new revision.');
234
    // Save the original revision IDs that we will test with later.
235
    $vid_old = $node_test->vid;
236
    $vuuid_old = $node_test->vuuid;
237
    $uuid_old = $node_test->uuid;
238

    
239
    // Test node update, with new revision.
240
    $node_test = clone $node;
241
    $node_test->title = 'newer title';
242
    $node_test->revision = TRUE;
243
    node_save($node_test);
244
    $node_test = node_load($node->nid, FALSE, TRUE);
245
    $this->assertEqual($node_test->uuid, $node->uuid, 'Node UUID was intact after updating, when creating new revision.');
246
    $this->assertNotEqual($node_test->vuuid, $node->vuuid, 'A new node revision UUID was generated, when creating new revision.');
247
    $this->assertUUID($node_test->vuuid, 'The new node revision UUID was valid.');
248

    
249
    // Test entity_uuid_load().
250
    // Save some variables that we will test against.
251
    $nid_test = $node_test->nid;
252
    $vid_test = $node_test->vid;
253
    $uid_test = $user->uuid;
254
    $uuid_test = $node_test->uuid;
255
    $vuuid_test = $node_test->vuuid;
256
    $nodes = entity_uuid_load('node', array($node->uuid), array(), TRUE);
257
    $node_test = reset($nodes);
258
    $this->assertEqual($node_test->nid, $nid_test, 'Node ID was correct when loading with UUID.');
259
    $this->assertEqual($node_test->vid, $vid_test, 'Node revision ID was correct when loading with UUID.');
260
    $this->assertEqual($node_test->uid, $uid_test, "Node author ID was transformed to UUID when loaded with UUID.");
261
    $this->assertEqual($node_test->uuid, $uuid_test, 'Node UUID was correct when loading with UUID.');
262
    $this->assertEqual($node_test->vuuid, $vuuid_test, 'Node revision UUID was correct when loading with UUID.');
263

    
264
    // Test entity_uuid_load() with conditions.
265
    // Load the previous revision UUID that we saved earlier.
266
    $nodes = entity_uuid_load('node', array($uuid_test), array('vuuid' => $vuuid_old));
267
    $node_test = reset($nodes);
268
    $this->assertTrue((($node_test->uuid == $uuid_test) && ($node_test->nid && $node->nid)), 'The correct entity was loaded when loading a universal entity with a revision UUID condition.');
269
    $this->assertEqual($node_test->vuuid, $vuuid_old, 'Correct revision UUID was loaded when loading a universal entity with a revision UUID condition.');
270
    $this->assertEqual($node_test->vid, $vid_old, 'Correct revision ID was loaded when loading a universal entity with a revision UUID condition.');
271
    $this->assertEqual($node_test->title, 'original title', 'Correct title was loaded when loading a universal entity with a revision UUID condition.');
272

    
273
    // The following tests depends on the optional Entity API module.
274
    if (module_exists('entity')) {
275
      // Reload the node again because we have created new revisions above.
276
      $node = node_load($node->nid, FALSE, TRUE);
277
      // Test entity_uuid_save() for nodes.
278
      $nodes = entity_uuid_load('node', array($node->uuid), array(), TRUE);
279
      $node_test = reset($nodes);
280
      $node_test->nid = rand();
281
      $node_test->vid = rand();
282
      $node_test->title = 'new title';
283
      $node_test->revision = FALSE;
284
      entity_uuid_save('node', $node_test);
285
      $node_test = node_load($node->nid, FALSE, TRUE);
286
      $this->assertEqual($node_test->title, 'new title', 'Saving node with UUID mapped to correct node, when not creating new revision.');
287
      $this->assertEqual($node_test->uuid, $node->uuid, 'Node UUID was intact after saving with UUID, when not creating new revision.');
288
      $this->assertEqual($node_test->vuuid, $node->vuuid, 'Node revison UUID was intact after saving with UUID, when not creating new revision.');
289
      $this->assertEqual($node_test->uid, $node->uid, "Node property 'uid' was intact after saving with UUID, when not creating new revision.");
290

    
291
      // Test the same thing again, but now triggering a new revision.
292
      $nodes = entity_uuid_load('node', array($node->uuid), array(), TRUE);
293
      $node_test = reset($nodes);
294
      $node_test->nid = rand();
295
      $node_test->vid = rand();
296
      $node_test->title = 'newer title';
297
      $node_test->revision = TRUE;
298
      entity_uuid_save('node', $node_test);
299
      $node_test = node_load($node->nid, FALSE, TRUE);
300
      $this->assertEqual($node_test->title, 'newer title', 'Saving node with UUID mapped to correct node, when creating new revision.');
301
      $this->assertEqual($node_test->uuid, $node->uuid, 'Node UUID was intact after saving with UUID, when creating new revision.');
302
      $this->assertNotEqual($node_test->vuuid, $node->vuuid, 'A new node revison UUID was generated after saving with UUID, when creating new revision.');
303
      $this->assertUUID($node_test->vuuid, 'New node revision UUID was valid.');
304
      $this->assertEqual($node_test->uid, $node->uid, "Node property 'uid' was intact after saving with UUID, when creating new revision.");
305

    
306
      // Test the same thing again, but now triggering a new revision from a
307
      // remote environment.
308
      // TODO: Move this test to the uuid_services module.
309
      $nodes = entity_uuid_load('node', array($node->uuid), array(), TRUE);
310
      $node_test = reset($nodes);
311
      // Store the current local revision ID to test with later.
312
      $vid_old1 = $node_test->vid;
313
      $vuuid_old1 = $node_test->vuuid;
314
      // Simulate this node coming from a remote environment by generating
315
      // IDs that won't match. Only the UUID match at this point.
316
      $node_test->uuid_services = TRUE;
317
      $nid_remote = rand();
318
      $vid_remote = rand();
319
      $vuuid_test = uuid_generate();
320
      $node_test->nid = $nid_test;
321
      $node_test->vid = $vid_test;
322
      $node_test->vuuid = $vuuid_test;
323
      $node_test->revision = TRUE;
324
      entity_uuid_save('node', $node_test);
325
      $node_test = node_load($node->nid, FALSE, TRUE);
326
      $this->assertNotEqual($node_test->vid, $vid_old1, 'A new revision was created, when trying to create new revision with new revision UUID from remote site');
327
      $this->assertEqual($node_test->vuuid, $vuuid_test, 'The revison UUID was preserved after saving with UUID, when trying to create new revision with new revision UUID from remote site.');
328

    
329
      // Test the same thing again from a remote environment, but now with the
330
      // same vuuid as once previosuly. This should not trigger a new revision.
331
      // This covers the case of "dupe deployments" where a client might push a
332
      // node several times.
333
      // TODO: Move this test to the uuid_services module.
334
      $nodes = entity_uuid_load('node', array($node->uuid), array(), TRUE);
335
      $node_test = reset($nodes);
336
      // Store the current local revision ID to test with later.
337
      $vid_old2 = $node_test->vid;
338
      // Simulate this node coming from a remote environment by generating
339
      // IDs that won't match.
340
      $node_test->uuid_services = TRUE;
341
      $node_test->nid = $nid_test;
342
      $node_test->vid = $vid_test;
343
      $node_test->vuuid = $vuuid_test;
344
      $node_test->revision = TRUE;
345
      entity_uuid_save('node', $node_test);
346
      $node_test = node_load($node->nid, FALSE, TRUE);
347
      $this->assertEqual($node_test->vid, $vid_old2, 'A new revision was not created, when trying to create new revision with existing revision UUID from remote site.');
348
      $this->assertEqual($node_test->vuuid, $vuuid_test, 'The revison UUID was preserved after saving with UUID, when trying to create new revision with existing revision UUID from remote site.');
349

    
350
      // Test the same this again, but now with an old revision.
351
      $nodes = entity_uuid_load('node', array($uuid_old), array('vuuid' => $vuuid_old), TRUE);
352
      $node_test = reset($nodes);
353
      // Simulate this node coming from a remote environment by generating
354
      // IDs that won't match.
355
      $node_test->uuid_services = TRUE;
356
      $node_test->nid = rand();
357
      $node_test->vid = rand();
358
      $node_test->revision = TRUE;
359
      $node_test->title = 'newest title';
360
      entity_uuid_save('node', $node_test);
361
      $node_test = node_load($node->nid, $vid_old, TRUE);
362
      $this->assertEqual($node_test->title, 'newest title', 'The revision was updated, when updating old revision with existing revision UUID from remote site.');
363
      $this->assertEqual($node_test->vuuid, $vuuid_old, 'The revison UUID was preserved after saving with UUID, when updating old revision with existing revision UUID from remote site.');
364

    
365
      // Setting the node options variable should also trigger a new revision.
366
      $nodes = entity_uuid_load('node', array($node->uuid), array(), TRUE);
367
      $node_test = reset($nodes);
368
      variable_set('node_options_' . $node_test->type, array('revision'));
369
      entity_uuid_save('node', $node_test);
370
      $this->assertNotEqual($node_test->vuuid, $node->vuuid, 'A new node revison ID was generated after saving with UUID, when relying on the node options variable.');
371

    
372
      // Test entity_uuid_delete() for nodes.
373
      entity_uuid_delete('node', $node->uuid);
374
      $node_test = node_load($node->nid);
375
      $this->assertFalse($node_test, 'Deleting node with UUID worked.');
376
    }
377
  }
378
}
379

    
380
/**
381
 * Tests the Comment implementation.
382
 *
383
 * @todo
384
 *   Contribute patch to CommentHelperCase::setUp() to make it extendable.
385
 */
386
class UUIDCommentTestCase extends CommentHelperCase {
387

    
388
  /**
389
   * {@inheritdoc}
390
   */
391
  public static function getInfo() {
392
    return array(
393
      'name' => 'Comment implementation',
394
      'description' => 'Tests the Comment implementation.',
395
      'group' => 'UUID',
396
    );
397
  }
398

    
399
  /**
400
   * Helper function that asserts a UUID.
401
   *
402
   * We have duplicated this function from UUIDTestCase since we have to extend
403
   * CommentHelperCase instead.
404
   */
405
  function assertUUID($uuid, $message = NULL) {
406
    $this->assertTrue(uuid_is_valid($uuid), $message);
407
  }
408

    
409
  /**
410
   * Test CRUD on comments with UUID functions.
411
   */
412
  function testCommentCRUD() {
413
    // This is sub optimal, but due to how CommentHelperCase::setUp() is
414
    // constructed we are enforced to do this. So unfortunately this test
415
    // depends on 'entity' module for now.
416
    module_enable(array('uuid', 'entity'), TRUE);
417
    $user = $this->drupalCreateUser();
418
    $this->drupalLogin($user);
419
    $node = $this->drupalCreateNode();
420
    $return = $this->postComment($node, 'Lorem ipsum');
421

    
422
    $comment = comment_load($return->id);
423
    $this->assertUUID($comment->uuid, 'Comment UUID was generated.');
424

    
425
    // Test updating comment.
426
    $comment_test = clone $comment;
427
    $comment_test->subject = 'new subject';
428
    comment_save($comment_test);
429
    $comment_test = comment_load($comment->cid);
430
    $this->assertEqual($comment_test->uuid, $comment->uuid, 'Comment UUID was intact after update.');
431

    
432
    // Test entity_uuid_load().
433
    $comments = entity_uuid_load('comment', array($comment->uuid), array(), TRUE);
434
    $comment_test = reset($comments);
435
    $this->assertEqual($comment_test->cid, $return->id, 'Comment was correctly loaded with UUID.');
436
    $this->assertEqual($comment_test->uid, $user->uuid, "Comment property 'uid' was transformed to UUID when loaded with UUID.");
437
    $this->assertEqual($comment_test->nid, $node->uuid, "Comment property 'nid' was transformed to UUID when loaded with UUID.");
438

    
439
    // The following tests depends on the optional Entity API module.
440
    if (module_exists('entity')) {
441
      // Test entity_uuid_save() for comments.
442
      $comments = entity_uuid_load('comment', array($comment->uuid), array(), TRUE);
443
      $comment_test = reset($comments);
444
      $comment_test->cid = rand();
445
      $comment_test->subject = 'newer subject';
446
      entity_uuid_save('comment', $comment_test);
447
      $comment_test = comment_load($comment->cid);
448
      $this->assertEqual($comment_test->subject, 'newer subject', 'Saving comment with UUID mapped to correct comment.');
449
      $this->assertEqual($comment_test->uuid, $comment->uuid, 'Comment UUID was intact after saving with UUID.');
450
      $this->assertEqual($comment_test->uid, $user->uid, "Comment property 'uid' was after saving with UUID.");
451
      $this->assertEqual($comment_test->nid, $node->nid, "Comment property 'nid' was after saving with UUID.");
452

    
453
      // Test entity_uuid_delete() for comments.
454
      entity_uuid_delete('comment', $comment->uuid);
455
      $comment_test = comment_load($comment->cid);
456
      $this->assertFalse($comment_test, 'Deleting comment with UUID worked.');
457
    }
458
  }
459
}
460

    
461
/**
462
 * Tests the Taxonomy implementation.
463
 */
464
class UUIDTaxonomyTestCase extends TaxonomyWebTestCase {
465

    
466
  /**
467
   * {@inheritdoc}
468
   */
469
  public static function getInfo() {
470
    return array(
471
      'name' => 'Taxonomy implementation',
472
      'description' => 'Tests the Taxonomy implementation.',
473
      'group' => 'UUID',
474
    );
475
  }
476

    
477
  /**
478
   * {@inheritdoc}
479
   *
480
   * A lot of code here is taken from TaxonomyTermTestCase::setUp().
481
   */
482
  function setUp() {
483
    // Some tests depends on the optional Entity API module.
484
    if (module_exists('entity')) {
485
      parent::setUp('taxonomy', 'uuid', 'entity');
486
    }
487
    else {
488
      parent::setUp('taxonomy', 'uuid');
489
    }
490
  }
491

    
492
  /**
493
   * Helper function that asserts a UUID.
494
   *
495
   * We have duplicated this function from UUIDTestCase since we have to extend
496
   * TaxonomyWebTestCase instead.
497
   */
498
  function assertUUID($uuid, $message = NULL) {
499
    $this->assertTrue(uuid_is_valid($uuid), $message);
500
  }
501

    
502
  /**
503
   * Test CRUD on comments with UUID functions.
504
   */
505
  function testTaxonomyCRUD() {
506
    $user = $this->drupalCreateUser(array('administer taxonomy', 'administer nodes', 'bypass node access'));
507
    $this->drupalLogin($user);
508

    
509
    // Create a term by tagging a node. We'll use this node later too.
510
    $vocabulary = new stdClass;
511
    $vocabulary->vid = 1;
512
    $term = $this->createTerm($vocabulary);
513
    $this->assertUUID($term->uuid, 'Term UUID was generated.');
514

    
515
    // Test updating term.
516
    $term_test = clone $term;
517
    $term_test->name = 'new name';
518
    taxonomy_term_save($term_test);
519
    $term_test = taxonomy_term_load($term->tid);
520
    $this->assertEqual($term_test->uuid, $term->uuid, 'Term UUID was intact after update.');
521

    
522
    // Test entity_uuid_load().
523
    $terms = entity_uuid_load('taxonomy_term', array($term->uuid), array(), TRUE);
524
    $term_test = reset($terms);
525
    $this->assertEqual($term_test->tid, $term->tid, 'Term was correctly loaded with UUID.');
526

    
527
    // The following tests depends on the Entity API module.
528
    if (module_exists('entity')) {
529
      // Test entity_uuid_save() for terms.
530
      $terms = entity_uuid_load('taxonomy_term', array($term->uuid), array(), TRUE);
531
      $term_test = reset($terms);
532
      $term_test->tid = rand();
533
      $term_test->name = 'newer name';
534
      entity_uuid_save('taxonomy_term', $term_test);
535
      $term_test = taxonomy_term_load($term->tid);
536
      $this->assertEqual($term_test->name, 'newer name', 'Saving term with UUID mapped to correct term.');
537
      $this->assertEqual($term_test->uuid, $term->uuid, 'Term UUID was intact after saving with UUID.');
538

    
539
      // Test entity_uuid_delete() for nodes.
540
      entity_uuid_delete('taxonomy_term', $term->uuid);
541
      $term_test = taxonomy_term_load($term->tid);
542
      $this->assertFalse($term_test, 'Deleting term with UUID worked.');
543
    }
544
  }
545
}
546

    
547
/**
548
 * Tests for the UUID synchronization.
549
 */
550
class UUIDSyncTestCase extends UUIDTestCase {
551

    
552
  /**
553
   * {@inheritdoc}
554
   */
555
  public static function getInfo() {
556
    return array(
557
      'name' => 'UUID sync',
558
      'description' => 'Tests the UUID synchronization.',
559
      'group' => 'UUID',
560
    );
561
  }
562

    
563
  /**
564
   * Helper function that asserts that a database table column exists.
565
   *
566
   * @todo
567
   *   There are something weird around this assertion.
568
   */
569
  function assertTableColumn($table, $column, $message) {
570
    $this->assertTrue(db_field_exists($table, $column), $message);
571
  }
572

    
573
  /**
574
   * Tests creating UUIDs for entities that don't have them.
575
   */
576
  function testSync() {
577
    // These entities will not have UUID from the start, since the UUID module
578
    // isn't installed yet.
579
    $user = $this->drupalCreateUser();
580
    $node = $this->drupalCreateNode();
581

    
582
    $this->assertTrue(!isset($node->uuid), "Node has no UUID before installation of UUID module.");
583
    $this->assertTrue(!isset($node->vuuid), "Node has no revision UUID before installation of UUID module.");
584
    $this->assertTrue(!isset($user->uuid), "User has no UUID before installation of UUID module.");
585

    
586
    // Now enable the UUID module.
587
    module_enable(array('uuid'), TRUE);
588
    drupal_flush_all_caches();
589
    drupal_static_reset();
590

    
591
    // Check that the UUID column was generated for {node}.
592
    $this->assertTableColumn('node', 'uuid', 'UUID column was generated for the node table.');
593
    $this->assertTableColumn('node_revision', 'vuuid', 'Revision UUID column was generated for the node_revision table.');
594
    $this->assertTableColumn('users', 'uuid', 'UUID column was generated for the user table.');
595

    
596
    // Login with a user and click the sync button.
597
    $web_user = $this->drupalCreateUser(array('administer uuid'));
598
    $this->drupalLogin($web_user);
599
    $this->drupalPost('admin/config/system/uuid', array(), t('Create missing UUIDs'));
600

    
601
    // Test if UUID was generated for nodes.
602
    $node_test = node_load($node->nid, FALSE, TRUE);
603
    $this->assertUUID($node_test->uuid, 'Node UUID was generated when clicking the sync button.');
604
    $this->assertUUID($node_test->vuuid, 'Node revision UUID was generated when clicking the sync button.');
605

    
606
    // Test if UUID was generated for users.
607
    $user_test = user_load($user->uid, TRUE);
608
    $this->assertUUID($user_test->uuid, 'User UUID was generated when clicking the sync button.');
609
  }
610
}