Projet

Général

Profil

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

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

1
<?php
2

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

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

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

    
20
}
21

    
22
/**
23
 * Tests the UUID API functions.
24
 */
25
class UUIDAPITestCase extends UUIDTestCase {
26

    
27
  /**
28
   * {@inheritdoc}
29
   */
30
  public static function getInfo() {
31
    return array(
32
      'name' => 'UUID API',
33
      'description' => 'Tests the UUID API functions.',
34
      'group' => 'UUID',
35
    );
36
  }
37

    
38
  /**
39
   * {@inheritdoc}
40
   */
41
  protected function setUp() {
42
    parent::setUp(array('uuid'));
43
  }
44

    
45
  /**
46
   * Tests uuid function calls.
47
   */
48
  public function testApiFunctions() {
49
    // This is a valid UUID, we know that.
50
    $valid_uuid = '0ab26e6b-f074-4e44-9da6-1205fa0e9761';
51
    // Test the uuid_is_valid() function.
52
    $this->assertUuid($valid_uuid, 'UUID validation works.');
53

    
54
    // The default generator is 'php'.
55
    $uuid = uuid_generate();
56
    $this->assertUuid($uuid, 'PHP generator works.');
57

    
58
    // Test the 'mysql' generator.
59
    variable_set('uuid_generator', 'mysql');
60
    drupal_static_reset('uuid_generate');
61
    $uuid = uuid_generate();
62
    $this->assertUuid($uuid, 'MySQL generator works.');
63
  }
64

    
65
  /**
66
   * Checks that schema for tables of core entities is correctly defined.
67
   */
68
  public function testSchemas() {
69
    module_load_include('install', 'uuid');
70

    
71
    $schemas = drupal_get_schema();
72
    $field_info = uuid_schema_field_definition();
73
    $key_names = array(
74
      'base table' => 'uuid',
75
      'revision table' => 'revision uuid',
76
    );
77

    
78
    foreach (uuid_get_core_entity_info() as $entity_info) {
79
      // Test the fields in "base" and "revision" tables.
80
      foreach ($key_names as $table_type => $key_name) {
81
        // Table or field is not defined in entity.
82
        if (!isset($entity_info[$table_type], $entity_info['entity keys'][$key_name])) {
83
          // Not all entities have a revisions table.
84
          continue;
85
        }
86

    
87
        $field_name = $entity_info['entity keys'][$key_name];
88
        $table_name = $entity_info[$table_type];
89

    
90
        if (!isset($schemas[$table_name])) {
91
          $this->fail(sprintf('Database schema does not have a "%s" table.', $table_name));
92
          continue;
93
        }
94

    
95
        $properties = array(
96
          'field' => array('fields', $field_info),
97
          'index' => array('indexes', array($field_name)),
98
        );
99

    
100
        // Check integrity of the field and index definition.
101
        foreach ($properties as $type => $data) {
102
          list($property, $value) = $data;
103

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

    
106
          if (isset($schemas[$table_name][$property][$field_name])) {
107
            $this->assertIdentical($schemas[$table_name][$property][$field_name], $value, "$message is correct.");
108
          }
109
          else {
110
            $this->fail("$message does not exist.");
111
          }
112
        }
113
      }
114
    }
115
  }
116

    
117
}
118

    
119
/**
120
 * Tests the UUID API functions.
121
 */
122
class UUIDV5TestCase extends UUIDTestCase {
123

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

    
135
  /**
136
   * {@inheritdoc}
137
   */
138
  protected function setUp() {
139
    parent::setUp(array('uuid'));
140
  }
141

    
142
  /**
143
   * Tests uuid function calls.
144
   */
145
  public function testV5Function() {
146
    // DNS namespace UUID.
147
    $dns_namespace = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
148

    
149
    // Valid DNS generation test.
150
    $uuid = uuid_generate_v5($dns_namespace, 'drupal.org');
151
    $this->assertUuid($uuid, 'UUID for drupal.org is valid.');
152
    $this->assertEqual($uuid, 'c809fd30-48df-52e3-a9f2-2cd78129b8b1', 'UUID for drupal.org is correct.');
153

    
154
    // Invalid namespace test.
155
    $invalid_namespace = '01234567-c7a9-feda-27e5-75d00dabc123';
156
    $uuid = uuid_generate_v5($invalid_namespace, 'drupal.org');
157
    $this->assertFalse($uuid, 'Invalid namespace UUID rejected.');
158
  }
159

    
160
}
161

    
162
/**
163
 * Tests the Entity API functions.
164
 */
165
class UUIDEntityTestCase extends UUIDTestCase {
166

    
167
  /**
168
   * {@inheritdoc}
169
   */
170
  public static function getInfo() {
171
    return array(
172
      'name' => 'Entity API functions',
173
      'description' => 'Tests the Entity API functions.',
174
      'group' => 'UUID',
175
    );
176
  }
177

    
178
  /**
179
   * {@inheritdoc}
180
   */
181
  protected function setUp() {
182
    parent::setUp(array('uuid'));
183
  }
184

    
185
  /**
186
   * Tests Entity API's UUID functions.
187
   */
188
  public function testEntityApiFunctions() {
189
    // Create some entities that we will work with.
190
    $user = $this->drupalCreateUser();
191
    $node = $this->drupalCreateNode(array('title' => 'original title', 'uid' => $user->uid));
192

    
193
    // Test entity_get_id_by_uuid().
194
    $nids = entity_get_id_by_uuid('node', array($node->uuid), FALSE);
195
    $this->assertTrue(in_array($node->nid, $nids), 'Lookup of entity ID works.');
196
    $vids = entity_get_id_by_uuid('node', array($node->vuuid), TRUE);
197
    $this->assertTrue(in_array($node->vid, $vids), 'Lookup of entity revision ID works.');
198

    
199
    // Test entity_get_uuid_by_id().
200
    $uuids = entity_get_uuid_by_id('node', array($node->nid), FALSE);
201
    $this->assertTrue(in_array($node->uuid, $uuids), 'Lookup of entity UUID works.');
202
    $vuuids = entity_get_uuid_by_id('node', array($node->vid), TRUE);
203
    $this->assertTrue(in_array($node->vuuid, $vuuids), 'Lookup of entity revision UUID works.');
204
  }
205

    
206
}
207

    
208
/**
209
 * Tests the User implementation.
210
 */
211
class UUIDUserTestCase extends UUIDTestCase {
212

    
213
  /**
214
   * {@inheritdoc}
215
   */
216
  public static function getInfo() {
217
    return array(
218
      'name' => 'User implementation',
219
      'description' => 'Tests the User implementation.',
220
      'group' => 'UUID',
221
    );
222
  }
223

    
224
  /**
225
   * {@inheritdoc}
226
   */
227
  protected function setUp() {
228
    $modules = array('uuid');
229

    
230
    // Some tests depends on the optional Entity API module.
231
    if (module_exists('entity')) {
232
      $modules[] = 'entity';
233
    }
234

    
235
    parent::setUp($modules);
236
  }
237

    
238
  /**
239
   * Test CRUD on users with UUID functions.
240
   */
241
  public function testUserCrud() {
242
    $user = $this->drupalCreateUser();
243
    $this->assertUuid($user->uuid, 'User UUID was generated.');
244

    
245
    // Test updating user.
246
    $user_test = clone $user;
247
    user_save($user_test, array('name' => 'new name'));
248
    $user_test = user_load($user->uid, TRUE);
249
    $this->assertEqual($user_test->uuid, $user->uuid, 'User UUID was intact after update.');
250

    
251
    // Test entity_uuid_load().
252
    $users = entity_uuid_load('user', array($user->uuid), array(), TRUE);
253
    $user_test = reset($users);
254
    $this->assertEqual($user_test->uid, $user->uid, 'User was correctly loaded with UUID.');
255

    
256
    // The following tests depends on the optional Entity API module.
257
    if (module_exists('entity')) {
258
      // Test entity_uuid_save() for users.
259
      $user_test = clone $user;
260
      $user_test->uid = rand();
261
      $user_test->name = 'new name';
262
      entity_uuid_save('user', $user_test);
263
      $user_test = user_load($user->uid, TRUE);
264
      $this->assertEqual($user_test->name, 'new name', 'Saving user with UUID mapped to correct user.');
265
      $this->assertEqual($user_test->uuid, $user->uuid, 'User UUID was intact after saving with UUID.');
266

    
267
      // Test entity_uuid_delete() for users.
268
      entity_uuid_delete('user', $user->uuid);
269
      $user_test = user_load($user->uid);
270
      $this->assertFalse($user_test, 'Deleting user with UUID worked.');
271
    }
272
  }
273

    
274
}
275

    
276
/**
277
 * Tests the Node implementation.
278
 */
279
class UUIDNodeTestCase extends UUIDTestCase {
280

    
281
  /**
282
   * {@inheritdoc}
283
   */
284
  public static function getInfo() {
285
    return array(
286
      'name' => 'Node implementation',
287
      'description' => 'Tests the Node implementation.',
288
      'group' => 'UUID',
289
    );
290
  }
291

    
292
  /**
293
   * {@inheritdoc}
294
   */
295
  protected function setUp() {
296
    $modules = array('uuid');
297

    
298
    // Some tests depends on the optional Entity API module.
299
    if (module_exists('entity')) {
300
      $modules[] = 'entity';
301
    }
302

    
303
    parent::setUp($modules);
304
  }
305

    
306
  /**
307
   * Tests CRUD on nodes with UUID functions.
308
   *
309
   * @todo
310
   *   Break out into multiple test methods to loosen coupling between tests.
311
   */
312
  public function testNodeCrud() {
313
    // Create some entities that we will work with.
314
    $user = $this->drupalCreateUser();
315
    $node = $this->drupalCreateNode(array('title' => 'original title', 'uid' => $user->uid));
316

    
317
    $this->assertUuid($node->uuid, 'Node UUID was generated.');
318
    $this->assertUuid($node->vuuid, 'Node revision UUID was generated.');
319

    
320
    // Test node update, without creating new revision.
321
    $node_test = clone $node;
322
    $node_test->title = 'original title';
323
    $node_test->revision = FALSE;
324
    node_save($node_test);
325
    $node_test = node_load($node->nid, FALSE, TRUE);
326
    $this->assertEqual($node_test->uuid, $node->uuid, 'Node UUID was intact after update, when not creating new revision.');
327
    $this->assertEqual($node_test->vuuid, $node->vuuid, 'Node revision UUID was intact after updating, when not creating new revision.');
328
    // Save the original revision IDs that we will test with later.
329
    $vid_old = $node_test->vid;
330
    $vuuid_old = $node_test->vuuid;
331
    $uuid_old = $node_test->uuid;
332

    
333
    // Test node update, with new revision.
334
    $node_test = clone $node;
335
    $node_test->title = 'newer title';
336
    $node_test->revision = TRUE;
337
    node_save($node_test);
338
    $node_test = node_load($node->nid, FALSE, TRUE);
339
    $this->assertEqual($node_test->uuid, $node->uuid, 'Node UUID was intact after updating, when creating new revision.');
340
    $this->assertNotEqual($node_test->vuuid, $node->vuuid, 'A new node revision UUID was generated, when creating new revision.');
341
    $this->assertUuid($node_test->vuuid, 'The new node revision UUID was valid.');
342

    
343
    // Test entity_uuid_load().
344
    // Save some variables that we will test against.
345
    $nid_test = $node_test->nid;
346
    $vid_test = $node_test->vid;
347
    $uid_test = $user->uuid;
348
    $uuid_test = $node_test->uuid;
349
    $vuuid_test = $node_test->vuuid;
350
    $nodes = entity_uuid_load('node', array($node->uuid), array(), TRUE);
351
    $node_test = reset($nodes);
352
    $this->assertEqual($node_test->nid, $nid_test, 'Node ID was correct when loading with UUID.');
353
    $this->assertEqual($node_test->vid, $vid_test, 'Node revision ID was correct when loading with UUID.');
354
    $this->assertEqual($node_test->uid, $uid_test, "Node author ID was transformed to UUID when loaded with UUID.");
355
    $this->assertEqual($node_test->uuid, $uuid_test, 'Node UUID was correct when loading with UUID.');
356
    $this->assertEqual($node_test->vuuid, $vuuid_test, 'Node revision UUID was correct when loading with UUID.');
357

    
358
    // Test entity_uuid_load() with conditions.
359
    // Load the previous revision UUID that we saved earlier.
360
    $nodes = entity_uuid_load('node', array($uuid_test), array('vuuid' => $vuuid_old));
361
    $node_test = reset($nodes);
362
    $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.');
363
    $this->assertEqual($node_test->vuuid, $vuuid_old, 'Correct revision UUID was loaded when loading a universal entity with a revision UUID condition.');
364
    $this->assertEqual($node_test->vid, $vid_old, 'Correct revision ID was loaded when loading a universal entity with a revision UUID condition.');
365
    $this->assertEqual($node_test->title, 'original title', 'Correct title was loaded when loading a universal entity with a revision UUID condition.');
366

    
367
    // The following tests depends on the optional Entity API module.
368
    if (module_exists('entity')) {
369
      // Reload the node again because we have created new revisions above.
370
      $node = node_load($node->nid, FALSE, TRUE);
371
      // Test entity_uuid_save() for nodes.
372
      $nodes = entity_uuid_load('node', array($node->uuid), array(), TRUE);
373
      $node_test = reset($nodes);
374
      $node_test->nid = rand();
375
      $node_test->vid = rand();
376
      $node_test->title = 'new title';
377
      $node_test->revision = FALSE;
378
      entity_uuid_save('node', $node_test);
379
      $node_test = node_load($node->nid, FALSE, TRUE);
380
      $this->assertEqual($node_test->title, 'new title', 'Saving node with UUID mapped to correct node, when not creating new revision.');
381
      $this->assertEqual($node_test->uuid, $node->uuid, 'Node UUID was intact after saving with UUID, when not creating new revision.');
382
      $this->assertEqual($node_test->vuuid, $node->vuuid, 'Node revison UUID was intact after saving with UUID, when not creating new revision.');
383
      $this->assertEqual($node_test->uid, $node->uid, "Node property 'uid' was intact after saving with UUID, when not creating new revision.");
384

    
385
      // Test the same thing again, but now triggering a new revision.
386
      $nodes = entity_uuid_load('node', array($node->uuid), array(), TRUE);
387
      $node_test = reset($nodes);
388
      $node_test->nid = rand();
389
      $node_test->vid = rand();
390
      $node_test->title = 'newer title';
391
      $node_test->revision = TRUE;
392
      entity_uuid_save('node', $node_test);
393
      $node_test = node_load($node->nid, FALSE, TRUE);
394
      $this->assertEqual($node_test->title, 'newer title', 'Saving node with UUID mapped to correct node, when creating new revision.');
395
      $this->assertEqual($node_test->uuid, $node->uuid, 'Node UUID was intact after saving with UUID, when creating new revision.');
396
      $this->assertNotEqual($node_test->vuuid, $node->vuuid, 'A new node revison UUID was generated after saving with UUID, when creating new revision.');
397
      $this->assertUuid($node_test->vuuid, 'New node revision UUID was valid.');
398
      $this->assertEqual($node_test->uid, $node->uid, "Node property 'uid' was intact after saving with UUID, when creating new revision.");
399

    
400
      // Test the same thing again, but now triggering a new revision from a
401
      // remote environment.
402
      // TODO: Move this test to the uuid_services module.
403
      $nodes = entity_uuid_load('node', array($node->uuid), array(), TRUE);
404
      $node_test = reset($nodes);
405
      // Store the current local revision ID to test with later.
406
      $vid_old1 = $node_test->vid;
407
      // Simulate this node coming from a remote environment by generating
408
      // IDs that won't match. Only the UUID match at this point.
409
      $node_test->uuid_services = TRUE;
410
      $vuuid_test = uuid_generate();
411
      $node_test->nid = $nid_test;
412
      $node_test->vid = $vid_test;
413
      $node_test->vuuid = $vuuid_test;
414
      $node_test->revision = TRUE;
415
      entity_uuid_save('node', $node_test);
416
      $node_test = node_load($node->nid, FALSE, TRUE);
417
      $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');
418
      $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.');
419

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

    
441
      // Test the same this again, but now with an old revision.
442
      $nodes = entity_uuid_load('node', array($uuid_old), array('vuuid' => $vuuid_old), TRUE);
443
      $node_test = reset($nodes);
444
      // Simulate this node coming from a remote environment by generating
445
      // IDs that won't match.
446
      $node_test->uuid_services = TRUE;
447
      $node_test->nid = rand();
448
      $node_test->vid = rand();
449
      $node_test->revision = TRUE;
450
      $node_test->title = 'newest title';
451
      entity_uuid_save('node', $node_test);
452
      $node_test = node_load($node->nid, $vid_old, TRUE);
453
      $this->assertEqual($node_test->title, 'newest title', 'The revision was updated, when updating old revision with existing revision UUID from remote site.');
454
      $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.');
455

    
456
      // Setting the node options variable should also trigger a new revision.
457
      $nodes = entity_uuid_load('node', array($node->uuid), array(), TRUE);
458
      $node_test = reset($nodes);
459
      variable_set('node_options_' . $node_test->type, array('revision'));
460
      entity_uuid_save('node', $node_test);
461
      $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.');
462

    
463
      // Test entity_uuid_delete() for nodes.
464
      entity_uuid_delete('node', $node->uuid);
465
      $node_test = node_load($node->nid);
466
      $this->assertFalse($node_test, 'Deleting node with UUID worked.');
467
    }
468
  }
469

    
470
}
471

    
472
/**
473
 * Tests the Comment implementation.
474
 *
475
 * @todo
476
 *   Contribute patch to CommentHelperCase::setUp() to make it extendable.
477
 */
478
class UUIDCommentTestCase extends CommentHelperCase {
479

    
480
  /**
481
   * {@inheritdoc}
482
   */
483
  public static function getInfo() {
484
    return array(
485
      'name' => 'Comment implementation',
486
      'description' => 'Tests the Comment implementation.',
487
      'group' => 'UUID',
488
    );
489
  }
490

    
491
  /**
492
   * Helper function that asserts a UUID.
493
   */
494
  protected function assertUuid($uuid, $message = NULL) {
495
    $this->assertTrue(uuid_is_valid($uuid), $message);
496
  }
497

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

    
511
    $comment = comment_load($return->id);
512
    $this->assertUuid($comment->uuid, 'Comment UUID was generated.');
513

    
514
    // Test updating comment.
515
    $comment_test = clone $comment;
516
    $comment_test->subject = 'new subject';
517
    comment_save($comment_test);
518
    $comment_test = comment_load($comment->cid);
519
    $this->assertEqual($comment_test->uuid, $comment->uuid, 'Comment UUID was intact after update.');
520

    
521
    // Test entity_uuid_load().
522
    $comments = entity_uuid_load('comment', array($comment->uuid), array(), TRUE);
523
    $comment_test = reset($comments);
524
    $this->assertEqual($comment_test->cid, $return->id, 'Comment was correctly loaded with UUID.');
525
    $this->assertEqual($comment_test->uid, $user->uuid, "Comment property 'uid' was transformed to UUID when loaded with UUID.");
526
    $this->assertEqual($comment_test->nid, $node->uuid, "Comment property 'nid' was transformed to UUID when loaded with UUID.");
527

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

    
542
      // Test entity_uuid_delete() for comments.
543
      entity_uuid_delete('comment', $comment->uuid);
544
      $comment_test = comment_load($comment->cid);
545
      $this->assertFalse($comment_test, 'Deleting comment with UUID worked.');
546
    }
547
  }
548

    
549
}
550

    
551
/**
552
 * Tests the Taxonomy implementation.
553
 */
554
class UUIDTaxonomyTestCase extends TaxonomyWebTestCase {
555

    
556
  /**
557
   * {@inheritdoc}
558
   */
559
  public static function getInfo() {
560
    return array(
561
      'name' => 'Taxonomy implementation',
562
      'description' => 'Tests the Taxonomy implementation.',
563
      'group' => 'UUID',
564
    );
565
  }
566

    
567
  /**
568
   * {@inheritdoc}
569
   *
570
   * A lot of code here is taken from TaxonomyTermTestCase::setUp().
571
   */
572
  protected function setUp() {
573
    $modules = array('taxonomy', 'uuid');
574

    
575
    // Some tests depends on the optional Entity API module.
576
    if (module_exists('entity')) {
577
      $modules[] = 'entity';
578
    }
579

    
580
    parent::setUp($modules);
581
  }
582

    
583
  /**
584
   * Helper function that asserts a UUID.
585
   */
586
  protected function assertUuid($uuid, $message = NULL) {
587
    $this->assertTrue(uuid_is_valid($uuid), $message);
588
  }
589

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

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

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

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

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

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

    
639
}
640

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

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

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

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

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

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

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

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

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

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

    
705
}