Projet

Général

Profil

Paste
Télécharger (25,7 ko) Statistiques
| Branche: | Révision:

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

1
<?php
2

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

    
8
/**
9
 * UUID test helper trait.
10
 *
11
 * Contains methods that assist with running UUID tests.
12
 */
13
trait UUIDTestHelper {
14

    
15
  /**
16
   * Helper function that asserts a UUID.
17
   */
18
  protected function assertUuid($uuid, $message = NULL) {
19
    $this->assertTrue(uuid_is_valid($uuid), $message);
20
  }
21

    
22
}
23

    
24
/**
25
 * Base class with some helper methods.
26
 */
27
abstract class UUIDTestCase extends DrupalWebTestCase {
28

    
29
  use UUIDTestHelper;
30

    
31
}
32

    
33
/**
34
 * Tests the UUID API functions.
35
 */
36
class UUIDAPITestCase extends UUIDTestCase {
37

    
38
  /**
39
   * {@inheritdoc}
40
   */
41
  public static function getInfo() {
42
    return array(
43
      'name' => 'UUID API',
44
      'description' => 'Tests the UUID API functions.',
45
      'group' => 'UUID',
46
    );
47
  }
48

    
49
  /**
50
   * {@inheritdoc}
51
   */
52
  protected function setUp() {
53
    parent::setUp(array('uuid'));
54
  }
55

    
56
  /**
57
   * Tests uuid function calls.
58
   */
59
  public function testApiFunctions() {
60
    // This is a valid UUID, we know that.
61
    $valid_uuid = '0ab26e6b-f074-4e44-9da6-1205fa0e9761';
62
    // Test the uuid_is_valid() function.
63
    $this->assertUuid($valid_uuid, 'UUID validation works.');
64

    
65
    // The default generator is 'php'.
66
    $uuid = uuid_generate();
67
    $this->assertUuid($uuid, 'PHP generator works.');
68

    
69
    // Test the 'mysql' generator.
70
    variable_set('uuid_generator', 'mysql');
71
    drupal_static_reset('uuid_generate');
72
    $uuid = uuid_generate();
73
    $this->assertUuid($uuid, 'MySQL generator works.');
74
  }
75

    
76
  /**
77
   * Checks that schema for tables of core entities is correctly defined.
78
   */
79
  public function testSchemas() {
80
    module_load_include('install', 'uuid');
81

    
82
    $schemas = drupal_get_schema();
83
    $field_info = uuid_schema_field_definition();
84
    $key_names = array(
85
      'base table' => 'uuid',
86
      'revision table' => 'revision uuid',
87
    );
88

    
89
    foreach (uuid_get_core_entity_info() as $entity_info) {
90
      // Test the fields in "base" and "revision" tables.
91
      foreach ($key_names as $table_type => $key_name) {
92
        // Table or field is not defined in entity.
93
        if (!isset($entity_info[$table_type], $entity_info['entity keys'][$key_name])) {
94
          // Not all entities have a revisions table.
95
          continue;
96
        }
97

    
98
        $field_name = $entity_info['entity keys'][$key_name];
99
        $table_name = $entity_info[$table_type];
100

    
101
        if (!isset($schemas[$table_name])) {
102
          $this->fail(sprintf('Database schema does not have a "%s" table.', $table_name));
103
          continue;
104
        }
105

    
106
        $properties = array(
107
          'field' => array('fields', $field_info),
108
          'index' => array('indexes', array($field_name)),
109
        );
110

    
111
        // Check integrity of the field and index definition.
112
        foreach ($properties as $type => $data) {
113
          list($property, $value) = $data;
114

    
115
          $message = sprintf('Definition of the "%s" %s in the "%s" schema', $field_name, $type, $table_name);
116

    
117
          if (isset($schemas[$table_name][$property][$field_name])) {
118
            $this->assertIdentical($schemas[$table_name][$property][$field_name], $value, "$message is correct.");
119
          }
120
          else {
121
            $this->fail("$message does not exist.");
122
          }
123
        }
124
      }
125
    }
126
  }
127

    
128
}
129

    
130
/**
131
 * Tests the UUID API functions.
132
 */
133
class UUIDV5TestCase extends UUIDTestCase {
134

    
135
  /**
136
   * {@inheritdoc}
137
   */
138
  public static function getInfo() {
139
    return array(
140
      'name' => 'UUID v5',
141
      'description' => 'Tests the UUID v5 function.',
142
      'group' => 'UUID',
143
    );
144
  }
145

    
146
  /**
147
   * {@inheritdoc}
148
   */
149
  protected function setUp() {
150
    parent::setUp(array('uuid'));
151
  }
152

    
153
  /**
154
   * Tests uuid function calls.
155
   */
156
  public function testV5Function() {
157
    // DNS namespace UUID.
158
    $dns_namespace = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
159

    
160
    // Valid DNS generation test.
161
    $uuid = uuid_generate_v5($dns_namespace, 'drupal.org');
162
    $this->assertUuid($uuid, 'UUID for drupal.org is valid.');
163
    $this->assertEqual($uuid, 'c809fd30-48df-52e3-a9f2-2cd78129b8b1', 'UUID for drupal.org is correct.');
164

    
165
    // Invalid namespace test.
166
    $invalid_namespace = '01234567-c7a9-feda-27e5-75d00dabc123';
167
    $uuid = uuid_generate_v5($invalid_namespace, 'drupal.org');
168
    $this->assertFalse($uuid, 'Invalid namespace UUID rejected.');
169
  }
170

    
171
}
172

    
173
/**
174
 * Tests the Entity API functions.
175
 */
176
class UUIDEntityTestCase extends UUIDTestCase {
177

    
178
  /**
179
   * {@inheritdoc}
180
   */
181
  public static function getInfo() {
182
    return array(
183
      'name' => 'Entity API functions',
184
      'description' => 'Tests the Entity API functions.',
185
      'group' => 'UUID',
186
    );
187
  }
188

    
189
  /**
190
   * {@inheritdoc}
191
   */
192
  protected function setUp() {
193
    parent::setUp(array('uuid'));
194
  }
195

    
196
  /**
197
   * Tests Entity API's UUID functions.
198
   */
199
  public function testEntityApiFunctions() {
200
    // Create some entities that we will work with.
201
    $user = $this->drupalCreateUser();
202
    $node = $this->drupalCreateNode(array('title' => 'original title', 'uid' => $user->uid));
203

    
204
    // Test entity_get_id_by_uuid().
205
    $nids = entity_get_id_by_uuid('node', array($node->uuid), FALSE);
206
    $this->assertTrue(in_array($node->nid, $nids), 'Lookup of entity ID works.');
207
    $vids = entity_get_id_by_uuid('node', array($node->vuuid), TRUE);
208
    $this->assertTrue(in_array($node->vid, $vids), 'Lookup of entity revision ID works.');
209

    
210
    // Test entity_get_uuid_by_id().
211
    $uuids = entity_get_uuid_by_id('node', array($node->nid), FALSE);
212
    $this->assertTrue(in_array($node->uuid, $uuids), 'Lookup of entity UUID works.');
213
    $vuuids = entity_get_uuid_by_id('node', array($node->vid), TRUE);
214
    $this->assertTrue(in_array($node->vuuid, $vuuids), 'Lookup of entity revision UUID works.');
215
  }
216

    
217
}
218

    
219
/**
220
 * Tests the User implementation.
221
 */
222
class UUIDUserTestCase extends UUIDTestCase {
223

    
224
  /**
225
   * {@inheritdoc}
226
   */
227
  public static function getInfo() {
228
    return array(
229
      'name' => 'User implementation',
230
      'description' => 'Tests the User implementation.',
231
      'group' => 'UUID',
232
    );
233
  }
234

    
235
  /**
236
   * {@inheritdoc}
237
   */
238
  protected function setUp() {
239
    $modules = array('uuid');
240

    
241
    // Some tests depends on the optional Entity API module.
242
    if (module_exists('entity')) {
243
      $modules[] = 'entity';
244
    }
245

    
246
    parent::setUp($modules);
247
  }
248

    
249
  /**
250
   * Test CRUD on users with UUID functions.
251
   */
252
  public function testUserCrud() {
253
    $user = $this->drupalCreateUser();
254
    $this->assertUuid($user->uuid, 'User UUID was generated.');
255

    
256
    // Test updating user.
257
    $user_test = clone $user;
258
    user_save($user_test, array('name' => 'new name'));
259
    $user_test = user_load($user->uid, TRUE);
260
    $this->assertEqual($user_test->uuid, $user->uuid, 'User UUID was intact after update.');
261

    
262
    // Test entity_uuid_load().
263
    $users = entity_uuid_load('user', array($user->uuid), array(), TRUE);
264
    $user_test = reset($users);
265
    $this->assertEqual($user_test->uid, $user->uid, 'User was correctly loaded with UUID.');
266

    
267
    // The following tests depends on the optional Entity API module.
268
    if (module_exists('entity')) {
269
      // Test entity_uuid_save() for users.
270
      $user_test = clone $user;
271
      $user_test->uid = rand();
272
      $user_test->name = 'new name';
273
      entity_uuid_save('user', $user_test);
274
      $user_test = user_load($user->uid, TRUE);
275
      $this->assertEqual($user_test->name, 'new name', 'Saving user with UUID mapped to correct user.');
276
      $this->assertEqual($user_test->uuid, $user->uuid, 'User UUID was intact after saving with UUID.');
277

    
278
      // Test entity_uuid_delete() for users.
279
      entity_uuid_delete('user', $user->uuid);
280
      $user_test = user_load($user->uid);
281
      $this->assertFalse($user_test, 'Deleting user with UUID worked.');
282
    }
283
  }
284

    
285
}
286

    
287
/**
288
 * Tests the Node implementation.
289
 */
290
class UUIDNodeTestCase extends UUIDTestCase {
291

    
292
  /**
293
   * {@inheritdoc}
294
   */
295
  public static function getInfo() {
296
    return array(
297
      'name' => 'Node implementation',
298
      'description' => 'Tests the Node implementation.',
299
      'group' => 'UUID',
300
    );
301
  }
302

    
303
  /**
304
   * {@inheritdoc}
305
   */
306
  protected function setUp() {
307
    $modules = array('uuid');
308

    
309
    // Some tests depends on the optional Entity API module.
310
    if (module_exists('entity')) {
311
      $modules[] = 'entity';
312
    }
313

    
314
    parent::setUp($modules);
315
  }
316

    
317
  /**
318
   * Tests CRUD on nodes with UUID functions.
319
   *
320
   * @todo
321
   *   Break out into multiple test methods to loosen coupling between tests.
322
   */
323
  public function testNodeCrud() {
324
    // Create some entities that we will work with.
325
    $user = $this->drupalCreateUser();
326
    $node = $this->drupalCreateNode(array('title' => 'original title', 'uid' => $user->uid));
327

    
328
    $this->assertUuid($node->uuid, 'Node UUID was generated.');
329
    $this->assertUuid($node->vuuid, 'Node revision UUID was generated.');
330

    
331
    // Test node update, without creating new revision.
332
    $node_test = clone $node;
333
    $node_test->title = 'original title';
334
    $node_test->revision = FALSE;
335
    node_save($node_test);
336
    $node_test = node_load($node->nid, FALSE, TRUE);
337
    $this->assertEqual($node_test->uuid, $node->uuid, 'Node UUID was intact after update, when not creating new revision.');
338
    $this->assertEqual($node_test->vuuid, $node->vuuid, 'Node revision UUID was intact after updating, when not creating new revision.');
339
    // Save the original revision IDs that we will test with later.
340
    $vid_old = $node_test->vid;
341
    $vuuid_old = $node_test->vuuid;
342
    $uuid_old = $node_test->uuid;
343

    
344
    // Test node update, with new revision.
345
    $node_test = clone $node;
346
    $node_test->title = 'newer title';
347
    $node_test->revision = TRUE;
348
    node_save($node_test);
349
    $node_test = node_load($node->nid, FALSE, TRUE);
350
    $this->assertEqual($node_test->uuid, $node->uuid, 'Node UUID was intact after updating, when creating new revision.');
351
    $this->assertNotEqual($node_test->vuuid, $node->vuuid, 'A new node revision UUID was generated, when creating new revision.');
352
    $this->assertUuid($node_test->vuuid, 'The new node revision UUID was valid.');
353

    
354
    // Test entity_uuid_load().
355
    // Save some variables that we will test against.
356
    $nid_test = $node_test->nid;
357
    $vid_test = $node_test->vid;
358
    $uid_test = $user->uuid;
359
    $uuid_test = $node_test->uuid;
360
    $vuuid_test = $node_test->vuuid;
361
    $nodes = entity_uuid_load('node', array($node->uuid), array(), TRUE);
362
    $node_test = reset($nodes);
363
    $this->assertEqual($node_test->nid, $nid_test, 'Node ID was correct when loading with UUID.');
364
    $this->assertEqual($node_test->vid, $vid_test, 'Node revision ID was correct when loading with UUID.');
365
    $this->assertEqual($node_test->uid, $uid_test, "Node author ID was transformed to UUID when loaded with UUID.");
366
    $this->assertEqual($node_test->uuid, $uuid_test, 'Node UUID was correct when loading with UUID.');
367
    $this->assertEqual($node_test->vuuid, $vuuid_test, 'Node revision UUID was correct when loading with UUID.');
368

    
369
    // Test entity_uuid_load() with conditions.
370
    // Load the previous revision UUID that we saved earlier.
371
    $nodes = entity_uuid_load('node', array($uuid_test), array('vuuid' => $vuuid_old));
372
    $node_test = reset($nodes);
373
    $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.');
374
    $this->assertEqual($node_test->vuuid, $vuuid_old, 'Correct revision UUID was loaded when loading a universal entity with a revision UUID condition.');
375
    $this->assertEqual($node_test->vid, $vid_old, 'Correct revision ID was loaded when loading a universal entity with a revision UUID condition.');
376
    $this->assertEqual($node_test->title, 'original title', 'Correct title was loaded when loading a universal entity with a revision UUID condition.');
377

    
378
    // The following tests depends on the optional Entity API module.
379
    if (module_exists('entity')) {
380
      // Reload the node again because we have created new revisions above.
381
      $node = node_load($node->nid, FALSE, TRUE);
382
      // Test entity_uuid_save() for nodes.
383
      $nodes = entity_uuid_load('node', array($node->uuid), array(), TRUE);
384
      $node_test = reset($nodes);
385
      $node_test->nid = rand();
386
      $node_test->vid = rand();
387
      $node_test->title = 'new title';
388
      $node_test->revision = FALSE;
389
      entity_uuid_save('node', $node_test);
390
      $node_test = node_load($node->nid, FALSE, TRUE);
391
      $this->assertEqual($node_test->title, 'new title', 'Saving node with UUID mapped to correct node, when not creating new revision.');
392
      $this->assertEqual($node_test->uuid, $node->uuid, 'Node UUID was intact after saving with UUID, when not creating new revision.');
393
      $this->assertEqual($node_test->vuuid, $node->vuuid, 'Node revison UUID was intact after saving with UUID, when not creating new revision.');
394
      $this->assertEqual($node_test->uid, $node->uid, "Node property 'uid' was intact after saving with UUID, when not creating new revision.");
395

    
396
      // Test the same thing again, but now triggering a new revision.
397
      $nodes = entity_uuid_load('node', array($node->uuid), array(), TRUE);
398
      $node_test = reset($nodes);
399
      $node_test->nid = rand();
400
      $node_test->vid = rand();
401
      $node_test->title = 'newer title';
402
      $node_test->revision = TRUE;
403
      entity_uuid_save('node', $node_test);
404
      $node_test = node_load($node->nid, FALSE, TRUE);
405
      $this->assertEqual($node_test->title, 'newer title', 'Saving node with UUID mapped to correct node, when creating new revision.');
406
      $this->assertEqual($node_test->uuid, $node->uuid, 'Node UUID was intact after saving with UUID, when creating new revision.');
407
      $this->assertNotEqual($node_test->vuuid, $node->vuuid, 'A new node revison UUID was generated after saving with UUID, when creating new revision.');
408
      $this->assertUuid($node_test->vuuid, 'New node revision UUID was valid.');
409
      $this->assertEqual($node_test->uid, $node->uid, "Node property 'uid' was intact after saving with UUID, when creating new revision.");
410

    
411
      // Test the same thing again, but now triggering a new revision from a
412
      // remote environment.
413
      // TODO: Move this test to the uuid_services module.
414
      $nodes = entity_uuid_load('node', array($node->uuid), array(), TRUE);
415
      $node_test = reset($nodes);
416
      // Store the current local revision ID to test with later.
417
      $vid_old1 = $node_test->vid;
418
      // Simulate this node coming from a remote environment by generating
419
      // IDs that won't match. Only the UUID match at this point.
420
      $node_test->uuid_services = TRUE;
421
      $vuuid_test = uuid_generate();
422
      $node_test->nid = $nid_test;
423
      $node_test->vid = $vid_test;
424
      $node_test->vuuid = $vuuid_test;
425
      $node_test->revision = TRUE;
426
      entity_uuid_save('node', $node_test);
427
      $node_test = node_load($node->nid, FALSE, TRUE);
428
      $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');
429
      $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.');
430

    
431
      // Test the same thing again from a remote environment, but now with the
432
      // same vuuid as once previosuly. This should not trigger a new revision.
433
      // This covers the case of "dupe deployments" where a client might push a
434
      // node several times.
435
      // TODO: Move this test to the uuid_services module.
436
      $nodes = entity_uuid_load('node', array($node->uuid), array(), TRUE);
437
      $node_test = reset($nodes);
438
      // Store the current local revision ID to test with later.
439
      $vid_old2 = $node_test->vid;
440
      // Simulate this node coming from a remote environment by generating
441
      // IDs that won't match.
442
      $node_test->uuid_services = TRUE;
443
      $node_test->nid = $nid_test;
444
      $node_test->vid = $vid_test;
445
      $node_test->vuuid = $vuuid_test;
446
      $node_test->revision = TRUE;
447
      entity_uuid_save('node', $node_test);
448
      $node_test = node_load($node->nid, FALSE, TRUE);
449
      $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.');
450
      $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.');
451

    
452
      // Test the same this again, but now with an old revision.
453
      $nodes = entity_uuid_load('node', array($uuid_old), array('vuuid' => $vuuid_old), TRUE);
454
      $node_test = reset($nodes);
455
      // Simulate this node coming from a remote environment by generating
456
      // IDs that won't match.
457
      $node_test->uuid_services = TRUE;
458
      $node_test->nid = rand();
459
      $node_test->vid = rand();
460
      $node_test->revision = TRUE;
461
      $node_test->title = 'newest title';
462
      entity_uuid_save('node', $node_test);
463
      $node_test = node_load($node->nid, $vid_old, TRUE);
464
      $this->assertEqual($node_test->title, 'newest title', 'The revision was updated, when updating old revision with existing revision UUID from remote site.');
465
      $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.');
466

    
467
      // Setting the node options variable should also trigger a new revision.
468
      $nodes = entity_uuid_load('node', array($node->uuid), array(), TRUE);
469
      $node_test = reset($nodes);
470
      variable_set('node_options_' . $node_test->type, array('revision'));
471
      entity_uuid_save('node', $node_test);
472
      $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.');
473

    
474
      // Test entity_uuid_delete() for nodes.
475
      entity_uuid_delete('node', $node->uuid);
476
      $node_test = node_load($node->nid);
477
      $this->assertFalse($node_test, 'Deleting node with UUID worked.');
478
    }
479
  }
480

    
481
}
482

    
483
/**
484
 * Tests the Comment implementation.
485
 *
486
 * @todo
487
 *   Contribute patch to CommentHelperCase::setUp() to make it extendable.
488
 */
489
class UUIDCommentTestCase extends CommentHelperCase {
490

    
491
  use UUIDTestHelper;
492

    
493
  /**
494
   * {@inheritdoc}
495
   */
496
  public static function getInfo() {
497
    return array(
498
      'name' => 'Comment implementation',
499
      'description' => 'Tests the Comment implementation.',
500
      'group' => 'UUID',
501
    );
502
  }
503

    
504
  /**
505
   * Test CRUD on comments with UUID functions.
506
   */
507
  public function testCommentCrud() {
508
    // This is sub optimal, but due to how CommentHelperCase::setUp() is
509
    // constructed we are enforced to do this. So unfortunately this test
510
    // depends on 'entity' module for now.
511
    module_enable(array('uuid', 'entity'));
512
    $user = $this->drupalCreateUser();
513
    $this->drupalLogin($user);
514
    $node = $this->drupalCreateNode();
515
    $return = $this->postComment($node, 'Lorem ipsum');
516

    
517
    $comment = comment_load($return->id);
518
    $this->assertUuid($comment->uuid, 'Comment UUID was generated.');
519

    
520
    // Test updating comment.
521
    $comment_test = clone $comment;
522
    $comment_test->subject = 'new subject';
523
    comment_save($comment_test);
524
    $comment_test = comment_load($comment->cid);
525
    $this->assertEqual($comment_test->uuid, $comment->uuid, 'Comment UUID was intact after update.');
526

    
527
    // Test entity_uuid_load().
528
    $comments = entity_uuid_load('comment', array($comment->uuid), array(), TRUE);
529
    $comment_test = reset($comments);
530
    $this->assertEqual($comment_test->cid, $return->id, 'Comment was correctly loaded with UUID.');
531
    $this->assertEqual($comment_test->uid, $user->uuid, "Comment property 'uid' was transformed to UUID when loaded with UUID.");
532
    $this->assertEqual($comment_test->nid, $node->uuid, "Comment property 'nid' was transformed to UUID when loaded with UUID.");
533

    
534
    // The following tests depends on the optional Entity API module.
535
    if (module_exists('entity')) {
536
      // Test entity_uuid_save() for comments.
537
      $comments = entity_uuid_load('comment', array($comment->uuid), array(), TRUE);
538
      $comment_test = reset($comments);
539
      $comment_test->cid = rand();
540
      $comment_test->subject = 'newer subject';
541
      entity_uuid_save('comment', $comment_test);
542
      $comment_test = comment_load($comment->cid);
543
      $this->assertEqual($comment_test->subject, 'newer subject', 'Saving comment with UUID mapped to correct comment.');
544
      $this->assertEqual($comment_test->uuid, $comment->uuid, 'Comment UUID was intact after saving with UUID.');
545
      $this->assertEqual($comment_test->uid, $user->uid, "Comment property 'uid' was after saving with UUID.");
546
      $this->assertEqual($comment_test->nid, $node->nid, "Comment property 'nid' was after saving with UUID.");
547

    
548
      // Test entity_uuid_delete() for comments.
549
      entity_uuid_delete('comment', $comment->uuid);
550
      $comment_test = comment_load($comment->cid);
551
      $this->assertFalse($comment_test, 'Deleting comment with UUID worked.');
552
    }
553
  }
554

    
555
}
556

    
557
/**
558
 * Tests the Taxonomy implementation.
559
 */
560
class UUIDTaxonomyTestCase extends TaxonomyWebTestCase {
561

    
562
  use UUIDTestHelper;
563

    
564
  /**
565
   * {@inheritdoc}
566
   */
567
  public static function getInfo() {
568
    return array(
569
      'name' => 'Taxonomy implementation',
570
      'description' => 'Tests the Taxonomy implementation.',
571
      'group' => 'UUID',
572
    );
573
  }
574

    
575
  /**
576
   * {@inheritdoc}
577
   *
578
   * A lot of code here is taken from TaxonomyTermTestCase::setUp().
579
   */
580
  protected function setUp() {
581
    $modules = array('taxonomy', 'uuid');
582

    
583
    // Some tests depends on the optional Entity API module.
584
    if (module_exists('entity')) {
585
      $modules[] = 'entity';
586
    }
587

    
588
    parent::setUp($modules);
589
  }
590

    
591
  /**
592
   * Test CRUD on comments with UUID functions.
593
   */
594
  public function testTaxonomyCrud() {
595
    $perms = array(
596
      'administer taxonomy',
597
      'administer nodes',
598
      'bypass node access',
599
    );
600
    $user = $this->drupalCreateUser($perms);
601
    $this->drupalLogin($user);
602

    
603
    // Create a term by tagging a node. We'll use this node later too.
604
    $vocabulary = new stdClass();
605
    $vocabulary->vid = 1;
606
    $term = $this->createTerm($vocabulary);
607
    $this->assertUuid($term->uuid, 'Term UUID was generated.');
608

    
609
    // Test updating term.
610
    $term_test = clone $term;
611
    $term_test->name = 'new name';
612
    taxonomy_term_save($term_test);
613
    $term_test = taxonomy_term_load($term->tid);
614
    $this->assertEqual($term_test->uuid, $term->uuid, 'Term UUID was intact after update.');
615

    
616
    // Test entity_uuid_load().
617
    $terms = entity_uuid_load('taxonomy_term', array($term->uuid), array(), TRUE);
618
    $term_test = reset($terms);
619
    $this->assertEqual($term_test->tid, $term->tid, 'Term was correctly loaded with UUID.');
620

    
621
    // The following tests depends on the Entity API module.
622
    if (module_exists('entity')) {
623
      // Test entity_uuid_save() for terms.
624
      $terms = entity_uuid_load('taxonomy_term', array($term->uuid), array(), TRUE);
625
      $term_test = reset($terms);
626
      $term_test->tid = rand();
627
      $term_test->name = 'newer name';
628
      entity_uuid_save('taxonomy_term', $term_test);
629
      $term_test = taxonomy_term_load($term->tid);
630
      $this->assertEqual($term_test->name, 'newer name', 'Saving term with UUID mapped to correct term.');
631
      $this->assertEqual($term_test->uuid, $term->uuid, 'Term UUID was intact after saving with UUID.');
632

    
633
      // Test entity_uuid_delete() for nodes.
634
      entity_uuid_delete('taxonomy_term', $term->uuid);
635
      $term_test = taxonomy_term_load($term->tid);
636
      $this->assertFalse($term_test, 'Deleting term with UUID worked.');
637
    }
638
  }
639

    
640
}
641

    
642
/**
643
 * Tests for the UUID synchronization.
644
 */
645
class UUIDSyncTestCase extends UUIDTestCase {
646

    
647
  /**
648
   * {@inheritdoc}
649
   */
650
  public static function getInfo() {
651
    return array(
652
      'name' => 'UUID sync',
653
      'description' => 'Tests the UUID synchronization.',
654
      'group' => 'UUID',
655
    );
656
  }
657

    
658
  /**
659
   * Helper function that asserts that a database table column exists.
660
   *
661
   * @todo
662
   *   There are something weird around this assertion.
663
   */
664
  protected function assertTableColumn($table, $column, $message) {
665
    $this->assertTrue(db_field_exists($table, $column), $message);
666
  }
667

    
668
  /**
669
   * Tests creating UUIDs for entities that don't have them.
670
   */
671
  public function testSync() {
672
    // These entities will not have UUID from the start, since the UUID module
673
    // isn't installed yet.
674
    $user = $this->drupalCreateUser();
675
    $node = $this->drupalCreateNode();
676

    
677
    $this->assertTrue(!isset($node->uuid), "Node has no UUID before installation of UUID module.");
678
    $this->assertTrue(!isset($node->vuuid), "Node has no revision UUID before installation of UUID module.");
679
    $this->assertTrue(!isset($user->uuid), "User has no UUID before installation of UUID module.");
680

    
681
    // Now enable the UUID module.
682
    module_enable(array('uuid'), TRUE);
683
    drupal_flush_all_caches();
684
    drupal_static_reset();
685

    
686
    // Check that the UUID column was generated for {node}.
687
    $this->assertTableColumn('node', 'uuid', 'UUID column was generated for the node table.');
688
    $this->assertTableColumn('node_revision', 'vuuid', 'Revision UUID column was generated for the node_revision table.');
689
    $this->assertTableColumn('users', 'uuid', 'UUID column was generated for the user table.');
690

    
691
    // Login with a user and click the sync button.
692
    $web_user = $this->drupalCreateUser(array('administer uuid'));
693
    $this->drupalLogin($web_user);
694
    $this->drupalPost('admin/config/system/uuid', array(), t('Create missing UUIDs'));
695

    
696
    // Test if UUID was generated for nodes.
697
    $node_test = node_load($node->nid, FALSE, TRUE);
698
    $this->assertUuid($node_test->uuid, 'Node UUID was generated when clicking the sync button.');
699
    $this->assertUuid($node_test->vuuid, 'Node revision UUID was generated when clicking the sync button.');
700

    
701
    // Test if UUID was generated for users.
702
    $user_test = user_load($user->uid, TRUE);
703
    $this->assertUuid($user_test->uuid, 'User UUID was generated when clicking the sync button.');
704
  }
705

    
706
}