-
Notifications
You must be signed in to change notification settings - Fork 1
/
uuid.test
705 lines (607 loc) · 25.9 KB
/
uuid.test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
<?php
/**
* @file
* Test suite for UUID module.
*/
/**
* Base class with some helper methods.
*/
abstract class UUIDTestCase extends DrupalWebTestCase {
/**
* Helper function that asserts a UUID.
*/
protected function assertUuid($uuid, $message = NULL) {
$this->assertTrue(uuid_is_valid($uuid), $message);
}
}
/**
* Tests the UUID API functions.
*/
class UUIDAPITestCase extends UUIDTestCase {
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => 'UUID API',
'description' => 'Tests the UUID API functions.',
'group' => 'UUID',
);
}
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp(array('uuid'));
}
/**
* Tests uuid function calls.
*/
public function testApiFunctions() {
// This is a valid UUID, we know that.
$valid_uuid = '0ab26e6b-f074-4e44-9da6-1205fa0e9761';
// Test the uuid_is_valid() function.
$this->assertUuid($valid_uuid, 'UUID validation works.');
// The default generator is 'php'.
$uuid = uuid_generate();
$this->assertUuid($uuid, 'PHP generator works.');
// Test the 'mysql' generator.
variable_set('uuid_generator', 'mysql');
drupal_static_reset('uuid_generate');
$uuid = uuid_generate();
$this->assertUuid($uuid, 'MySQL generator works.');
}
/**
* Checks that schema for tables of core entities is correctly defined.
*/
public function testSchemas() {
module_load_include('install', 'uuid');
$schemas = drupal_get_schema();
$field_info = uuid_schema_field_definition();
$key_names = array(
'base table' => 'uuid',
'revision table' => 'revision uuid',
);
foreach (uuid_get_core_entity_info() as $entity_info) {
// Test the fields in "base" and "revision" tables.
foreach ($key_names as $table_type => $key_name) {
// Table or field is not defined in entity.
if (!isset($entity_info[$table_type], $entity_info['entity keys'][$key_name])) {
// Not all entities have a revisions table.
continue;
}
$field_name = $entity_info['entity keys'][$key_name];
$table_name = $entity_info[$table_type];
if (!isset($schemas[$table_name])) {
$this->fail(sprintf('Database schema does not have a "%s" table.', $table_name));
continue;
}
$properties = array(
'field' => array('fields', $field_info),
'index' => array('indexes', array($field_name)),
);
// Check integrity of the field and index definition.
foreach ($properties as $type => $data) {
list($property, $value) = $data;
$message = sprintf('Definition of the "%s" %s in the "%s" schema', $field_name, $type, $table_name);
if (isset($schemas[$table_name][$property][$field_name])) {
$this->assertIdentical($schemas[$table_name][$property][$field_name], $value, "$message is correct.");
}
else {
$this->fail("$message does not exist.");
}
}
}
}
}
}
/**
* Tests the UUID API functions.
*/
class UUIDV5TestCase extends UUIDTestCase {
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => 'UUID v5',
'description' => 'Tests the UUID v5 function.',
'group' => 'UUID',
);
}
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp(array('uuid'));
}
/**
* Tests uuid function calls.
*/
public function testV5Function() {
// DNS namespace UUID.
$dns_namespace = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
// Valid DNS generation test.
$uuid = uuid_generate_v5($dns_namespace, 'drupal.org');
$this->assertUuid($uuid, 'UUID for drupal.org is valid.');
$this->assertEqual($uuid, 'c809fd30-48df-52e3-a9f2-2cd78129b8b1', 'UUID for drupal.org is correct.');
// Invalid namespace test.
$invalid_namespace = '01234567-c7a9-feda-27e5-75d00dabc123';
$uuid = uuid_generate_v5($invalid_namespace, 'drupal.org');
$this->assertFalse($uuid, 'Invalid namespace UUID rejected.');
}
}
/**
* Tests the Entity API functions.
*/
class UUIDEntityTestCase extends UUIDTestCase {
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => 'Entity API functions',
'description' => 'Tests the Entity API functions.',
'group' => 'UUID',
);
}
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp(array('uuid'));
}
/**
* Tests Entity API's UUID functions.
*/
public function testEntityApiFunctions() {
// Create some entities that we will work with.
$user = $this->drupalCreateUser();
$node = $this->drupalCreateNode(array('title' => 'original title', 'uid' => $user->uid));
// Test entity_get_id_by_uuid().
$nids = entity_get_id_by_uuid('node', array($node->uuid), FALSE);
$this->assertTrue(in_array($node->nid, $nids), 'Lookup of entity ID works.');
$vids = entity_get_id_by_uuid('node', array($node->vuuid), TRUE);
$this->assertTrue(in_array($node->vid, $vids), 'Lookup of entity revision ID works.');
// Test entity_get_uuid_by_id().
$uuids = entity_get_uuid_by_id('node', array($node->nid), FALSE);
$this->assertTrue(in_array($node->uuid, $uuids), 'Lookup of entity UUID works.');
$vuuids = entity_get_uuid_by_id('node', array($node->vid), TRUE);
$this->assertTrue(in_array($node->vuuid, $vuuids), 'Lookup of entity revision UUID works.');
}
}
/**
* Tests the User implementation.
*/
class UUIDUserTestCase extends UUIDTestCase {
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => 'User implementation',
'description' => 'Tests the User implementation.',
'group' => 'UUID',
);
}
/**
* {@inheritdoc}
*/
protected function setUp() {
$modules = array('uuid');
// Some tests depends on the optional Entity API module.
if (module_exists('entity')) {
$modules[] = 'entity';
}
parent::setUp($modules);
}
/**
* Test CRUD on users with UUID functions.
*/
public function testUserCrud() {
$user = $this->drupalCreateUser();
$this->assertUuid($user->uuid, 'User UUID was generated.');
// Test updating user.
$user_test = clone $user;
user_save($user_test, array('name' => 'new name'));
$user_test = user_load($user->uid, TRUE);
$this->assertEqual($user_test->uuid, $user->uuid, 'User UUID was intact after update.');
// Test entity_uuid_load().
$users = entity_uuid_load('user', array($user->uuid), array(), TRUE);
$user_test = reset($users);
$this->assertEqual($user_test->uid, $user->uid, 'User was correctly loaded with UUID.');
// The following tests depends on the optional Entity API module.
if (module_exists('entity')) {
// Test entity_uuid_save() for users.
$user_test = clone $user;
$user_test->uid = rand();
$user_test->name = 'new name';
entity_uuid_save('user', $user_test);
$user_test = user_load($user->uid, TRUE);
$this->assertEqual($user_test->name, 'new name', 'Saving user with UUID mapped to correct user.');
$this->assertEqual($user_test->uuid, $user->uuid, 'User UUID was intact after saving with UUID.');
// Test entity_uuid_delete() for users.
entity_uuid_delete('user', $user->uuid);
$user_test = user_load($user->uid);
$this->assertFalse($user_test, 'Deleting user with UUID worked.');
}
}
}
/**
* Tests the Node implementation.
*/
class UUIDNodeTestCase extends UUIDTestCase {
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => 'Node implementation',
'description' => 'Tests the Node implementation.',
'group' => 'UUID',
);
}
/**
* {@inheritdoc}
*/
protected function setUp() {
$modules = array('uuid');
// Some tests depends on the optional Entity API module.
if (module_exists('entity')) {
$modules[] = 'entity';
}
parent::setUp($modules);
}
/**
* Tests CRUD on nodes with UUID functions.
*
* @todo
* Break out into multiple test methods to loosen coupling between tests.
*/
public function testNodeCrud() {
// Create some entities that we will work with.
$user = $this->drupalCreateUser();
$node = $this->drupalCreateNode(array('title' => 'original title', 'uid' => $user->uid));
$this->assertUuid($node->uuid, 'Node UUID was generated.');
$this->assertUuid($node->vuuid, 'Node revision UUID was generated.');
// Test node update, without creating new revision.
$node_test = clone $node;
$node_test->title = 'original title';
$node_test->revision = FALSE;
node_save($node_test);
$node_test = node_load($node->nid, FALSE, TRUE);
$this->assertEqual($node_test->uuid, $node->uuid, 'Node UUID was intact after update, when not creating new revision.');
$this->assertEqual($node_test->vuuid, $node->vuuid, 'Node revision UUID was intact after updating, when not creating new revision.');
// Save the original revision IDs that we will test with later.
$vid_old = $node_test->vid;
$vuuid_old = $node_test->vuuid;
$uuid_old = $node_test->uuid;
// Test node update, with new revision.
$node_test = clone $node;
$node_test->title = 'newer title';
$node_test->revision = TRUE;
node_save($node_test);
$node_test = node_load($node->nid, FALSE, TRUE);
$this->assertEqual($node_test->uuid, $node->uuid, 'Node UUID was intact after updating, when creating new revision.');
$this->assertNotEqual($node_test->vuuid, $node->vuuid, 'A new node revision UUID was generated, when creating new revision.');
$this->assertUuid($node_test->vuuid, 'The new node revision UUID was valid.');
// Test entity_uuid_load().
// Save some variables that we will test against.
$nid_test = $node_test->nid;
$vid_test = $node_test->vid;
$uid_test = $user->uuid;
$uuid_test = $node_test->uuid;
$vuuid_test = $node_test->vuuid;
$nodes = entity_uuid_load('node', array($node->uuid), array(), TRUE);
$node_test = reset($nodes);
$this->assertEqual($node_test->nid, $nid_test, 'Node ID was correct when loading with UUID.');
$this->assertEqual($node_test->vid, $vid_test, 'Node revision ID was correct when loading with UUID.');
$this->assertEqual($node_test->uid, $uid_test, "Node author ID was transformed to UUID when loaded with UUID.");
$this->assertEqual($node_test->uuid, $uuid_test, 'Node UUID was correct when loading with UUID.');
$this->assertEqual($node_test->vuuid, $vuuid_test, 'Node revision UUID was correct when loading with UUID.');
// Test entity_uuid_load() with conditions.
// Load the previous revision UUID that we saved earlier.
$nodes = entity_uuid_load('node', array($uuid_test), array('vuuid' => $vuuid_old));
$node_test = reset($nodes);
$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.');
$this->assertEqual($node_test->vuuid, $vuuid_old, 'Correct revision UUID was loaded when loading a universal entity with a revision UUID condition.');
$this->assertEqual($node_test->vid, $vid_old, 'Correct revision ID was loaded when loading a universal entity with a revision UUID condition.');
$this->assertEqual($node_test->title, 'original title', 'Correct title was loaded when loading a universal entity with a revision UUID condition.');
// The following tests depends on the optional Entity API module.
if (module_exists('entity')) {
// Reload the node again because we have created new revisions above.
$node = node_load($node->nid, FALSE, TRUE);
// Test entity_uuid_save() for nodes.
$nodes = entity_uuid_load('node', array($node->uuid), array(), TRUE);
$node_test = reset($nodes);
$node_test->nid = rand();
$node_test->vid = rand();
$node_test->title = 'new title';
$node_test->revision = FALSE;
entity_uuid_save('node', $node_test);
$node_test = node_load($node->nid, FALSE, TRUE);
$this->assertEqual($node_test->title, 'new title', 'Saving node with UUID mapped to correct node, when not creating new revision.');
$this->assertEqual($node_test->uuid, $node->uuid, 'Node UUID was intact after saving with UUID, when not creating new revision.');
$this->assertEqual($node_test->vuuid, $node->vuuid, 'Node revison UUID was intact after saving with UUID, when not creating new revision.');
$this->assertEqual($node_test->uid, $node->uid, "Node property 'uid' was intact after saving with UUID, when not creating new revision.");
// Test the same thing again, but now triggering a new revision.
$nodes = entity_uuid_load('node', array($node->uuid), array(), TRUE);
$node_test = reset($nodes);
$node_test->nid = rand();
$node_test->vid = rand();
$node_test->title = 'newer title';
$node_test->revision = TRUE;
entity_uuid_save('node', $node_test);
$node_test = node_load($node->nid, FALSE, TRUE);
$this->assertEqual($node_test->title, 'newer title', 'Saving node with UUID mapped to correct node, when creating new revision.');
$this->assertEqual($node_test->uuid, $node->uuid, 'Node UUID was intact after saving with UUID, when creating new revision.');
$this->assertNotEqual($node_test->vuuid, $node->vuuid, 'A new node revison UUID was generated after saving with UUID, when creating new revision.');
$this->assertUuid($node_test->vuuid, 'New node revision UUID was valid.');
$this->assertEqual($node_test->uid, $node->uid, "Node property 'uid' was intact after saving with UUID, when creating new revision.");
// Test the same thing again, but now triggering a new revision from a
// remote environment.
// TODO: Move this test to the uuid_services module.
$nodes = entity_uuid_load('node', array($node->uuid), array(), TRUE);
$node_test = reset($nodes);
// Store the current local revision ID to test with later.
$vid_old1 = $node_test->vid;
// Simulate this node coming from a remote environment by generating
// IDs that won't match. Only the UUID match at this point.
$node_test->uuid_services = TRUE;
$vuuid_test = uuid_generate();
$node_test->nid = $nid_test;
$node_test->vid = $vid_test;
$node_test->vuuid = $vuuid_test;
$node_test->revision = TRUE;
entity_uuid_save('node', $node_test);
$node_test = node_load($node->nid, FALSE, TRUE);
$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');
$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.');
// Test the same thing again from a remote environment, but now with the
// same vuuid as once previosuly. This should not trigger a new revision.
// This covers the case of "dupe deployments" where a client might push a
// node several times.
// TODO: Move this test to the uuid_services module.
$nodes = entity_uuid_load('node', array($node->uuid), array(), TRUE);
$node_test = reset($nodes);
// Store the current local revision ID to test with later.
$vid_old2 = $node_test->vid;
// Simulate this node coming from a remote environment by generating
// IDs that won't match.
$node_test->uuid_services = TRUE;
$node_test->nid = $nid_test;
$node_test->vid = $vid_test;
$node_test->vuuid = $vuuid_test;
$node_test->revision = TRUE;
entity_uuid_save('node', $node_test);
$node_test = node_load($node->nid, FALSE, TRUE);
$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.');
$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.');
// Test the same this again, but now with an old revision.
$nodes = entity_uuid_load('node', array($uuid_old), array('vuuid' => $vuuid_old), TRUE);
$node_test = reset($nodes);
// Simulate this node coming from a remote environment by generating
// IDs that won't match.
$node_test->uuid_services = TRUE;
$node_test->nid = rand();
$node_test->vid = rand();
$node_test->revision = TRUE;
$node_test->title = 'newest title';
entity_uuid_save('node', $node_test);
$node_test = node_load($node->nid, $vid_old, TRUE);
$this->assertEqual($node_test->title, 'newest title', 'The revision was updated, when updating old revision with existing revision UUID from remote site.');
$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.');
// Setting the node options variable should also trigger a new revision.
$nodes = entity_uuid_load('node', array($node->uuid), array(), TRUE);
$node_test = reset($nodes);
variable_set('node_options_' . $node_test->type, array('revision'));
entity_uuid_save('node', $node_test);
$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.');
// Test entity_uuid_delete() for nodes.
entity_uuid_delete('node', $node->uuid);
$node_test = node_load($node->nid);
$this->assertFalse($node_test, 'Deleting node with UUID worked.');
}
}
}
/**
* Tests the Comment implementation.
*
* @todo
* Contribute patch to CommentHelperCase::setUp() to make it extendable.
*/
class UUIDCommentTestCase extends CommentHelperCase {
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => 'Comment implementation',
'description' => 'Tests the Comment implementation.',
'group' => 'UUID',
);
}
/**
* Helper function that asserts a UUID.
*/
protected function assertUuid($uuid, $message = NULL) {
$this->assertTrue(uuid_is_valid($uuid), $message);
}
/**
* Test CRUD on comments with UUID functions.
*/
public function testCommentCrud() {
// This is sub optimal, but due to how CommentHelperCase::setUp() is
// constructed we are enforced to do this. So unfortunately this test
// depends on 'entity' module for now.
module_enable(array('uuid', 'entity'));
$user = $this->drupalCreateUser();
$this->drupalLogin($user);
$node = $this->drupalCreateNode();
$return = $this->postComment($node, 'Lorem ipsum');
$comment = comment_load($return->id);
$this->assertUuid($comment->uuid, 'Comment UUID was generated.');
// Test updating comment.
$comment_test = clone $comment;
$comment_test->subject = 'new subject';
comment_save($comment_test);
$comment_test = comment_load($comment->cid);
$this->assertEqual($comment_test->uuid, $comment->uuid, 'Comment UUID was intact after update.');
// Test entity_uuid_load().
$comments = entity_uuid_load('comment', array($comment->uuid), array(), TRUE);
$comment_test = reset($comments);
$this->assertEqual($comment_test->cid, $return->id, 'Comment was correctly loaded with UUID.');
$this->assertEqual($comment_test->uid, $user->uuid, "Comment property 'uid' was transformed to UUID when loaded with UUID.");
$this->assertEqual($comment_test->nid, $node->uuid, "Comment property 'nid' was transformed to UUID when loaded with UUID.");
// The following tests depends on the optional Entity API module.
if (module_exists('entity')) {
// Test entity_uuid_save() for comments.
$comments = entity_uuid_load('comment', array($comment->uuid), array(), TRUE);
$comment_test = reset($comments);
$comment_test->cid = rand();
$comment_test->subject = 'newer subject';
entity_uuid_save('comment', $comment_test);
$comment_test = comment_load($comment->cid);
$this->assertEqual($comment_test->subject, 'newer subject', 'Saving comment with UUID mapped to correct comment.');
$this->assertEqual($comment_test->uuid, $comment->uuid, 'Comment UUID was intact after saving with UUID.');
$this->assertEqual($comment_test->uid, $user->uid, "Comment property 'uid' was after saving with UUID.");
$this->assertEqual($comment_test->nid, $node->nid, "Comment property 'nid' was after saving with UUID.");
// Test entity_uuid_delete() for comments.
entity_uuid_delete('comment', $comment->uuid);
$comment_test = comment_load($comment->cid);
$this->assertFalse($comment_test, 'Deleting comment with UUID worked.');
}
}
}
/**
* Tests the Taxonomy implementation.
*/
class UUIDTaxonomyTestCase extends TaxonomyWebTestCase {
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => 'Taxonomy implementation',
'description' => 'Tests the Taxonomy implementation.',
'group' => 'UUID',
);
}
/**
* {@inheritdoc}
*
* A lot of code here is taken from TaxonomyTermTestCase::setUp().
*/
protected function setUp() {
$modules = array('taxonomy', 'uuid');
// Some tests depends on the optional Entity API module.
if (module_exists('entity')) {
$modules[] = 'entity';
}
parent::setUp($modules);
}
/**
* Helper function that asserts a UUID.
*/
protected function assertUuid($uuid, $message = NULL) {
$this->assertTrue(uuid_is_valid($uuid), $message);
}
/**
* Test CRUD on comments with UUID functions.
*/
public function testTaxonomyCrud() {
$perms = array(
'administer taxonomy',
'administer nodes',
'bypass node access',
);
$user = $this->drupalCreateUser($perms);
$this->drupalLogin($user);
// Create a term by tagging a node. We'll use this node later too.
$vocabulary = new stdClass();
$vocabulary->vid = 1;
$term = $this->createTerm($vocabulary);
$this->assertUuid($term->uuid, 'Term UUID was generated.');
// Test updating term.
$term_test = clone $term;
$term_test->name = 'new name';
taxonomy_term_save($term_test);
$term_test = taxonomy_term_load($term->tid);
$this->assertEqual($term_test->uuid, $term->uuid, 'Term UUID was intact after update.');
// Test entity_uuid_load().
$terms = entity_uuid_load('taxonomy_term', array($term->uuid), array(), TRUE);
$term_test = reset($terms);
$this->assertEqual($term_test->tid, $term->tid, 'Term was correctly loaded with UUID.');
// The following tests depends on the Entity API module.
if (module_exists('entity')) {
// Test entity_uuid_save() for terms.
$terms = entity_uuid_load('taxonomy_term', array($term->uuid), array(), TRUE);
$term_test = reset($terms);
$term_test->tid = rand();
$term_test->name = 'newer name';
entity_uuid_save('taxonomy_term', $term_test);
$term_test = taxonomy_term_load($term->tid);
$this->assertEqual($term_test->name, 'newer name', 'Saving term with UUID mapped to correct term.');
$this->assertEqual($term_test->uuid, $term->uuid, 'Term UUID was intact after saving with UUID.');
// Test entity_uuid_delete() for nodes.
entity_uuid_delete('taxonomy_term', $term->uuid);
$term_test = taxonomy_term_load($term->tid);
$this->assertFalse($term_test, 'Deleting term with UUID worked.');
}
}
}
/**
* Tests for the UUID synchronization.
*/
class UUIDSyncTestCase extends UUIDTestCase {
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => 'UUID sync',
'description' => 'Tests the UUID synchronization.',
'group' => 'UUID',
);
}
/**
* Helper function that asserts that a database table column exists.
*
* @todo
* There are something weird around this assertion.
*/
protected function assertTableColumn($table, $column, $message) {
$this->assertTrue(db_field_exists($table, $column), $message);
}
/**
* Tests creating UUIDs for entities that don't have them.
*/
public function testSync() {
// These entities will not have UUID from the start, since the UUID module
// isn't installed yet.
$user = $this->drupalCreateUser();
$node = $this->drupalCreateNode();
$this->assertTrue(!isset($node->uuid), "Node has no UUID before installation of UUID module.");
$this->assertTrue(!isset($node->vuuid), "Node has no revision UUID before installation of UUID module.");
$this->assertTrue(!isset($user->uuid), "User has no UUID before installation of UUID module.");
// Now enable the UUID module.
module_enable(array('uuid'), TRUE);
drupal_flush_all_caches();
drupal_static_reset();
// Check that the UUID column was generated for {node}.
$this->assertTableColumn('node', 'uuid', 'UUID column was generated for the node table.');
$this->assertTableColumn('node_revision', 'vuuid', 'Revision UUID column was generated for the node_revision table.');
$this->assertTableColumn('users', 'uuid', 'UUID column was generated for the user table.');
// Login with a user and click the sync button.
$web_user = $this->drupalCreateUser(array('administer uuid'));
$this->drupalLogin($web_user);
$this->drupalPost('admin/config/system/uuid', array(), t('Create missing UUIDs'));
// Test if UUID was generated for nodes.
$node_test = node_load($node->nid, FALSE, TRUE);
$this->assertUuid($node_test->uuid, 'Node UUID was generated when clicking the sync button.');
$this->assertUuid($node_test->vuuid, 'Node revision UUID was generated when clicking the sync button.');
// Test if UUID was generated for users.
$user_test = user_load($user->uid, TRUE);
$this->assertUuid($user_test->uuid, 'User UUID was generated when clicking the sync button.');
}
}