Projet

Général

Profil

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

root / htmltest / sites / all / modules / uuid / uuid.test @ a5572547

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
  function setUp() {
14
    parent::setUp(func_get_args());
15
  }
16

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

    
25
/**
26
 * Tests the UUID API functions.
27
 */
28
class UUIDAPITestCase extends UUIDTestCase {
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
  function setUp() {
39
    parent::setUp('uuid');
40
  }
41

    
42
  function testAPIFunctions() {
43
    // This is a valid UUID, we know that.
44
    $valid_uuid = '0ab26e6b-f074-4e44-9da6-1205fa0e9761';
45
    // Test the uuid_is_valid() function.
46
    $this->assertUUID($valid_uuid, 'UUID validation works.');
47

    
48
    // The default generator is 'php'.
49
    $uuid = uuid_generate();
50
    $this->assertUUID($uuid, 'PHP generator works.');
51

    
52
    // Test the 'mysql' generator.
53
    variable_set('uuid_generator', 'mysql');
54
    drupal_static_reset('uuid_generate');
55
    $uuid = uuid_generate();
56
    $this->assertUUID($uuid, 'MySQL generator works.');
57
  }
58
}
59

    
60
/**
61
 * Tests the Entity API functions.
62
 */
63
class UUIDEntityTestCase extends UUIDTestCase {
64

    
65
  public static function getInfo() {
66
    return array(
67
      'name' => 'Entity API functions',
68
      'description' => 'Tests the Entity API functions.',
69
      'group' => 'UUID',
70
    );
71
  }
72

    
73
  function setUp() {
74
    parent::setUp('uuid');
75
  }
76

    
77
  /**
78
   * Tests Entity API's UUID functions.
79
   */
80
  function testEntityAPIFunctions() {
81
    // Create some entities that we will work with.
82
    $user = $this->drupalCreateUser();
83
    $node = $this->drupalCreateNode(array('title' => 'original title', 'uid' => $user->uid));
84

    
85
    // Test entity_get_id_by_uuid().
86
    $nids = entity_get_id_by_uuid('node', array($node->uuid), FALSE);
87
    $this->assertTrue(in_array($node->nid, $nids), 'Lookup of entity ID works.');
88
    $vids = entity_get_id_by_uuid('node', array($node->vuuid), TRUE);
89
    $this->assertTrue(in_array($node->vid, $vids), 'Lookup of entity revision ID works.');
90

    
91
    // Test entity_get_uuid_by_id().
92
    $uuids = entity_get_uuid_by_id('node', array($node->nid), FALSE);
93
    $this->assertTrue(in_array($node->uuid, $uuids), 'Lookup of entity UUID works.');
94
    $vuuids = entity_get_uuid_by_id('node', array($node->vid), TRUE);
95
    $this->assertTrue(in_array($node->vuuid, $vuuids), 'Lookup of entity revision UUID works.');
96
  }
97
}
98

    
99
/**
100
 * Tests the User implementation.
101
 */
102
class UUIDUserTestCase extends UUIDTestCase {
103

    
104
  public static function getInfo() {
105
    return array(
106
      'name' => 'User implementation',
107
      'description' => 'Tests the User implementation.',
108
      'group' => 'UUID',
109
    );
110
  }
111

    
112
  function setUp() {
113
    // Some tests depends on the optional Entity API module.
114
    if (module_exists('entity')) {
115
      parent::setUp('uuid', 'entity');
116
    }
117
    else {
118
      parent::setUp('uuid');
119
    }
120
  }
121

    
122
  /**
123
   * Test CRUD on users with UUID functions.
124
   */
125
  function testUserCRUD() {
126
    $user = $this->drupalCreateUser();
127
    $this->assertUUID($user->uuid, 'User UUID was generated.');
128

    
129
    // Test updating user.
130
    $user_test = clone $user;
131
    user_save($user_test, array('name' => 'new name'));
132
    $user_test = user_load($user->uid, TRUE);
133
    $this->assertEqual($user_test->uuid, $user->uuid, 'User UUID was intact after update.');
134

    
135
    // Test entity_uuid_load().
136
    $users = entity_uuid_load('user', array($user->uuid), array(), TRUE);
137
    $user_test = reset($users);
138
    $this->assertEqual($user_test->uid, $user->uid, 'User was correctly loaded with UUID.');
139

    
140
    // The following tests depends on the optional Entity API module.
141
    if (module_exists('entity')) {
142
      // Test entity_uuid_save() for users.
143
      $user_test = clone $user;
144
      $user_test->uid = rand();
145
      $user_test->name = 'new name';
146
      entity_uuid_save('user', $user_test);
147
      $user_test = user_load($user->uid, TRUE);
148
      $this->assertEqual($user_test->name, 'new name', 'Saving user with UUID mapped to correct user.');
149
      $this->assertEqual($user_test->uuid, $user->uuid, 'User UUID was intact after saving with UUID.');
150

    
151
      // Test entity_uuid_delete() for users.
152
      entity_uuid_delete('user', $user->uuid);
153
      $user_test = user_load($user->uid);
154
      $this->assertFalse($user_test, 'Deleting user with UUID worked.');
155
    }
156
  }
157
}
158

    
159
/**
160
 * Tests the Node implementation.
161
 */
162
class UUIDNodeTestCase extends UUIDTestCase {
163

    
164
  public static function getInfo() {
165
    return array(
166
      'name' => 'Node implementation',
167
      'description' => 'Tests the Node implementation.',
168
      'group' => 'UUID',
169
    );
170
  }
171

    
172
  function setUp() {
173
    // Some tests depends on the optional Entity API module.
174
    if (module_exists('entity')) {
175
      parent::setUp('uuid', 'entity');
176
    }
177
    else {
178
      parent::setUp('uuid');
179
    }
180
  }
181

    
182
  /**
183
   * Tests CRUD on nodes with UUID functions.
184
   */
185
  function testNodeCRUD() {
186
    // Create some entities that we will work with.
187
    $user = $this->drupalCreateUser();
188
    $node = $this->drupalCreateNode(array('title' => 'original title', 'uid' => $user->uid));
189

    
190
    $this->assertUUID($node->uuid, 'Node UUID was generated.');
191
    $this->assertUUID($node->vuuid, 'Node revision UUID was generated.');
192

    
193
    // Test node update, without creating new revision.
194
    $node_test = clone $node;
195
    $node_test->title = 'new title';
196
    $node_test->revision = FALSE;
197
    node_save($node_test);
198
    $node_test = node_load($node->nid, FALSE, TRUE);
199
    $this->assertEqual($node_test->uuid, $node->uuid, 'Node UUID was intact after update, when not creating new revision.');
200
    $this->assertEqual($node_test->vuuid, $node->vuuid, 'Node revision UUID was intact after updating, when not creating new revision.');
201

    
202
    // Test node update, with new revision.
203
    $node_test = clone $node;
204
    $node_test->title = 'newer title';
205
    $node_test->revision = TRUE;
206
    node_save($node_test);
207
    $node_test = node_load($node->nid, FALSE, TRUE);
208
    $this->assertEqual($node_test->uuid, $node->uuid, 'Node UUID was intact after updating, when creating new revision.');
209
    $this->assertNotEqual($node_test->vuuid, $node->vuuid, 'A new node revision UUID was generated, when creating new revision.');
210
    $this->assertUUID($node_test->vuuid, 'The new node revision UUID was valid.');
211

    
212
    // Test entity_uuid_load().
213
    $nodes = entity_uuid_load('node', array($node->uuid), array(), TRUE);
214
    $node_test = reset($nodes);
215
    $this->assertEqual($node_test->nid, $node->nid, 'Node was correctly loaded with UUID.');
216
    $this->assertEqual($node_test->uid, $user->uuid, "Node property 'uid' was transformed to UUID when loaded with UUID.");
217

    
218
    // The following tests depends on the optional Entity API module.
219
    if (module_exists('entity')) {
220
      // Reload the node again because we have created new revisions above.
221
      $node = node_load($node->nid, FALSE, TRUE);
222
      // Test entity_uuid_save() for nodes.
223
      $nodes = entity_uuid_load('node', array($node->uuid), array(), TRUE);
224
      $node_test = reset($nodes);
225
      $node_test->nid = rand();
226
      $node_test->vid = rand();
227
      $node_test->title = 'new title';
228
      $node_test->revision = FALSE;
229
      entity_uuid_save('node', $node_test);
230
      $node_test = node_load($node->nid, FALSE, TRUE);
231
      $this->assertEqual($node_test->title, 'new title', 'Saving node with UUID mapped to correct node, when not creating new revision.');
232
      $this->assertEqual($node_test->uuid, $node->uuid, 'Node UUID was intact after saving with UUID, when not creating new revision.');
233
      $this->assertEqual($node_test->vuuid, $node->vuuid, 'Node revison UUID was intact after saving with UUID, when not creating new revision.');
234
      $this->assertEqual($node_test->uid, $node->uid, "Node property 'uid' was intact after saving with UUID, when not creating new revision.");
235

    
236
      // Test the same thing again, but now triggering a new revision.
237
      $nodes = entity_uuid_load('node', array($node->uuid), array(), TRUE);
238
      $node_test = reset($nodes);
239
      $node_test->nid = rand();
240
      $node_test->vid = rand();
241
      $node_test->title = 'newer title';
242
      $node_test->revision = TRUE;
243
      entity_uuid_save('node', $node_test);
244
      $node_test = node_load($node->nid, FALSE, TRUE);
245
      $this->assertEqual($node_test->title, 'newer title', 'Saving node with UUID mapped to correct node, when creating new revision.');
246
      $this->assertEqual($node_test->uuid, $node->uuid, 'Node UUID was intact after saving with UUID, when creating new revision.');
247
      $this->assertNotEqual($node_test->vuuid, $node->vuuid, 'A new node revison UUID was generated after saving with UUID, when creating new revision.');
248
      $this->assertUUID($node_test->vuuid, 'New node revision UUID was valid.');
249
      $this->assertEqual($node_test->uid, $node->uid, "Node property 'uid' was intact after saving with UUID, when creating new revision.");
250

    
251
      // Test entity_uuid_delete() for nodes.
252
      entity_uuid_delete('node', $node->uuid);
253
      $node_test = node_load($node->nid);
254
      $this->assertFalse($node_test, 'Deleting node with UUID worked.');
255
    }
256
  }
257
}
258

    
259
/**
260
 * Tests the Comment implementation.
261
 *
262
 * @todo
263
 *   Contribute patch to CommentHelperCase::setUp() to make it extendable.
264
 */
265
class UUIDCommentTestCase extends CommentHelperCase {
266

    
267
  public static function getInfo() {
268
    return array(
269
      'name' => 'Comment implementation',
270
      'description' => 'Tests the Comment implementation.',
271
      'group' => 'UUID',
272
    );
273
  }
274

    
275
  /**
276
   * Helper function that asserts a UUID.
277
   *
278
   * We have duplicated this function from UUIDTestCase since we have to extend
279
   * CommentHelperCase instead.
280
   */
281
  function assertUUID($uuid, $message = NULL) {
282
    $this->assertTrue(uuid_is_valid($uuid), $message);
283
  }
284

    
285
  /**
286
   * Test CRUD on comments with UUID functions.
287
   */
288
  function testCommentCRUD() {
289
    // This is sub optimal, but due to how CommentHelperCase::setUp() is
290
    // constructed we are enforced to do this. So unfortunately this test
291
    // depends on 'entity' module for now.
292
    module_enable(array('uuid', 'entity'), TRUE);
293
    $user = $this->drupalCreateUser();
294
    $this->drupalLogin($user);
295
    $node = $this->drupalCreateNode();
296
    $return = $this->postComment($node, 'Lorem ipsum');
297

    
298
    $comment = comment_load($return->id);
299
    $this->assertUUID($comment->uuid, 'Comment UUID was generated.');
300

    
301
    // Test updating comment.
302
    $comment_test = clone $comment;
303
    $comment_test->subject = 'new subject';
304
    comment_save($comment_test);
305
    $comment_test = comment_load($comment->cid);
306
    $this->assertEqual($comment_test->uuid, $comment->uuid, 'Comment UUID was intact after update.');
307

    
308
    // Test entity_uuid_load().
309
    $comments = entity_uuid_load('comment', array($comment->uuid), array(), TRUE);
310
    $comment_test = reset($comments);
311
    $this->assertEqual($comment_test->cid, $return->id, 'Comment was correctly loaded with UUID.');
312
    $this->assertEqual($comment_test->uid, $user->uuid, "Comment property 'uid' was transformed to UUID when loaded with UUID.");
313
    $this->assertEqual($comment_test->nid, $node->uuid, "Comment property 'nid' was transformed to UUID when loaded with UUID.");
314

    
315
    // The following tests depends on the optional Entity API module.
316
    if (module_exists('entity')) {
317
      // Test entity_uuid_save() for comments.
318
      $comments = entity_uuid_load('comment', array($comment->uuid), array(), TRUE);
319
      $comment_test = reset($comments);
320
      $comment_test->cid = rand();
321
      $comment_test->subject = 'newer subject';
322
      entity_uuid_save('comment', $comment_test);
323
      $comment_test = comment_load($comment->cid);
324
      $this->assertEqual($comment_test->subject, 'newer subject', 'Saving comment with UUID mapped to correct comment.');
325
      $this->assertEqual($comment_test->uuid, $comment->uuid, 'Comment UUID was intact after saving with UUID.');
326
      $this->assertEqual($comment_test->uid, $user->uid, "Comment property 'uid' was after saving with UUID.");
327
      $this->assertEqual($comment_test->nid, $node->nid, "Comment property 'nid' was after saving with UUID.");
328

    
329
      // Test entity_uuid_delete() for comments.
330
      entity_uuid_delete('comment', $comment->uuid);
331
      $comment_test = comment_load($comment->cid);
332
      $this->assertFalse($comment_test, 'Deleting comment with UUID worked.');
333
    }
334
  }
335
}
336

    
337
/**
338
 * Tests the Taxonomy implementation.
339
 */
340
class UUIDTaxonomyTestCase extends TaxonomyWebTestCase {
341

    
342
  public static function getInfo() {
343
    return array(
344
      'name' => 'Taxonomy implementation',
345
      'description' => 'Tests the Taxonomy implementation.',
346
      'group' => 'UUID',
347
    );
348
  }
349

    
350
  /**
351
   * A lot of code here is taken from TaxonomyTermTestCase::setUp().
352
   */
353
  function setUp() {
354
    // Some tests depends on the optional Entity API module.
355
    if (module_exists('entity')) {
356
      parent::setUp('taxonomy', 'uuid', 'entity');
357
    }
358
    else {
359
      parent::setUp('taxonomy', 'uuid');
360
    }
361
  }
362

    
363
  /**
364
   * Helper function that asserts a UUID.
365
   *
366
   * We have duplicated this function from UUIDTestCase since we have to extend
367
   * TaxonomyWebTestCase instead.
368
   */
369
  function assertUUID($uuid, $message = NULL) {
370
    $this->assertTrue(uuid_is_valid($uuid), $message);
371
  }
372

    
373
  /**
374
   * Test CRUD on comments with UUID functions.
375
   */
376
  function testTaxonomyCRUD() {
377
    $user = $this->drupalCreateUser(array('administer taxonomy', 'administer nodes', 'bypass node access'));
378
    $this->drupalLogin($user);
379

    
380
    // Create a term by tagging a node. We'll use this node later too.
381
    $vocabulary = new stdClass;
382
    $vocabulary->vid = 1;
383
    $term = $this->createTerm($vocabulary);
384
    $this->assertUUID($term->uuid, 'Term UUID was generated.');
385

    
386
    // Test updating term.
387
    $term_test = clone $term;
388
    $term_test->name = 'new name';
389
    taxonomy_term_save($term_test);
390
    $term_test = taxonomy_term_load($term->tid);
391
    $this->assertEqual($term_test->uuid, $term->uuid, 'Term UUID was intact after update.');
392

    
393
    // Test entity_uuid_load().
394
    $terms = entity_uuid_load('taxonomy_term', array($term->uuid), array(), TRUE);
395
    $term_test = reset($terms);
396
    $this->assertEqual($term_test->tid, $term->tid, 'Term was correctly loaded with UUID.');
397

    
398
    // The following tests depends on the Entity API module.
399
    if (module_exists('entity')) {
400
      // Test entity_uuid_save() for terms.
401
      $terms = entity_uuid_load('taxonomy_term', array($term->uuid), array(), TRUE);
402
      $term_test = reset($terms);
403
      $term_test->tid = rand();
404
      $term_test->name = 'newer name';
405
      entity_uuid_save('taxonomy_term', $term_test);
406
      $term_test = taxonomy_term_load($term->tid);
407
      $this->assertEqual($term_test->name, 'newer name', 'Saving term with UUID mapped to correct term.');
408
      $this->assertEqual($term_test->uuid, $term->uuid, 'Term UUID was intact after saving with UUID.');
409

    
410
      // Test entity_uuid_delete() for nodes.
411
      entity_uuid_delete('taxonomy_term', $term->uuid);
412
      $term_test = taxonomy_term_load($term->tid);
413
      $this->assertFalse($term_test, 'Deleting term with UUID worked.');
414
    }
415
  }
416
}
417

    
418
/**
419
 * Tests for the UUID synchronization.
420
 */
421
class UUIDSyncTestCase extends UUIDTestCase {
422

    
423
  public static function getInfo() {
424
    return array(
425
      'name' => 'UUID sync',
426
      'description' => 'Tests the UUID synchronization.',
427
      'group' => 'UUID',
428
    );
429
  }
430

    
431
  /**
432
   * Helper function that asserts that a database table column exists.
433
   *
434
   * @todo
435
   *   There are something weird around this assertion.
436
   */
437
  function assertTableColumn($table, $column, $message) {
438
    $result = db_query("SHOW COLUMNS FROM {$table}");
439
    $exists = FALSE;
440
    foreach ($result as $record) {
441
      if ($record->field == $column) {
442
        $exists = TRUE;
443
        break;
444
      }
445
    }
446
    $this->assertTrue($exists, $message);
447
  }
448

    
449
  function testSync() {
450
    // These entities will not have UUID from the start, since the UUID module
451
    // isn't installed yet.
452
    $user = $this->drupalCreateUser();
453
    $node = $this->drupalCreateNode();
454

    
455
    $this->assertTrue(!isset($node->uuid), "Node has no UUID before installation of UUID module.");
456
    $this->assertTrue(!isset($node->vuuid), "Node has no revision UUID before installation of UUID module.");
457
    $this->assertTrue(!isset($user->uuid), "User has no UUID before installation of UUID module.");
458

    
459
    // Now enable the UUID module.
460
    module_enable(array('uuid'), TRUE);
461
    drupal_flush_all_caches();
462
    drupal_static_reset();
463

    
464
    // Check that the UUID column was generated for {node}.
465
    $this->assertTableColumn('node', 'uuid', 'UUID column was generated for the node table.');
466
    $this->assertTableColumn('node_revision', 'vuuid', 'Revision UUID column was generated for the node_revision table.');
467
    $this->assertTableColumn('users', 'uuid', 'UUID column was generated for the user table.');
468

    
469
    // Login with a user and click the sync button.
470
    $web_user = $this->drupalCreateUser(array('administer uuid'));
471
    $this->drupalLogin($web_user);
472
    $this->drupalPost('admin/config/system/uuid', array(), t('Create missing UUIDs'));
473

    
474
    // Test if UUID was generated for nodes.
475
    $node_test = node_load($node->nid, FALSE, TRUE);
476
    $this->assertUUID($node_test->uuid, 'Node UUID was generated when clicking the sync button.');
477
    $this->assertUUID($node_test->vuuid, 'Node revision UUID was generated when clicking the sync button.');
478

    
479
    // Test if UUID was generated for users.
480
    $user_test = user_load($user->uid, TRUE);
481
    $this->assertUUID($user_test->uuid, 'User UUID was generated when clicking the sync button.');
482
  }
483
}
484

    
485
class UUIDExportEntitiesWithDeploy extends DrupalWebTestCase {
486

    
487
  public static function getInfo() {
488
    return array(
489
      'name' => 'Export UUID entities',
490
      'description' => 'Test exporting UUID entities with Deploy and Features.',
491
      'group' => 'UUID',
492
    );
493
  }
494

    
495
  function setUp() {
496
    parent::setUp('taxonomy', 'uuid', 'entity', 'features', 'deploy', 'deploy_example');
497
  }
498

    
499
  function testExport() {
500
    $test_user = $this->drupalCreateUser();
501
    $test_node = $this->drupalCreateNode(array(
502
      'uid' => $test_user->uid,
503
    ));
504
    deploy_manager_add_to_plan('deploy_example_plan', 'node', $test_node);
505
    // TODO: Test the actual insert.
506
    $this->assertTrue(TRUE, 'Added a node with a user dependency to be exported as a Feature module.');
507

    
508
    // Login and recreate the example feature. The feature isn't installed. But
509
    // Features can still export the code, and we can test it.
510
    $web_user = $this->drupalCreateUser(array('administer features'));
511
    $this->drupalLogin($web_user);
512
    $code = $this->drupalPost('admin/structure/features/uuid_default_entities_example/recreate', array(), t('Download feature'));
513
    $this->assertTrue($code, 'Feature module was exported.');
514

    
515
    // Ensure that we find what we expect in the exported code.
516
    $node_test1 = preg_match('/' . $test_node->title . '/', $code);
517
    $node_test2 = preg_match("/'uri' => 'node\/" . $test_node->uuid . "'/", $code);
518
    $this->assertTrue($node_test1, 'Node title was found in the expoted code.');
519
    $this->assertTrue($node_test2, 'Node URI was found in the expoted code.');
520
    $user_test1 = preg_match('/' . $test_user->name . '/', $code);
521
    $user_test2 = preg_match("/'uri' => 'user\/" . $test_user->uuid . "'/", $code);
522
    $this->assertTrue($user_test1, 'User was found in the expoted code.');
523
    $this->assertTrue($user_test2, 'User URI was found in the expoted code.');
524
  }
525
}
526

    
527
/**
528
 * Tests for the UUID synchronization.
529
 */
530
class UUIDImportEntitiesTestCase extends UUIDTestCase {
531

    
532
  /**
533
   * Representation of the UUIDs that is exported in our example feature, that
534
   * we use for testing.
535
   */
536
  public $term1_uuid = 'bcb92ce8-2236-e264-65c8-0c163ae716d1';
537
  public $term2_uuid = '4293a15c-531a-6164-7d1b-668ed019a6bd';
538
  public $term3_uuid = 'af738a46-f278-cf84-d94d-9e03879fd71e';
539
  public $node1_uuid = 'b0558664-c94b-3674-d9df-3e1696b2e471';
540
  public $node2_uuid = '5e3d8bbe-a1f2-f2d4-fdc0-71e6c23aa837';
541
  public $user1_uuid = '7cf875e6-dc15-4404-f190-5a7c3e91d14c';
542

    
543
  /**
544
   * Helper method to assert the uuid_entities component in any features.
545
   */
546
  function assertFeatureState($feature, $state, $message = '') {
547
    if (empty($message)) {
548
      switch ($state) {
549
        case FEATURES_DEFAULT:
550
          $readable_state = 'default';
551
          break;
552
        case FEATURES_OVERRIDDEN:
553
          $readable_state = 'overridden';
554
          break;
555
        default:
556
          $readable_state = 'unknown';
557
          break;
558
      }
559
      $message = format_string('%component in %feature had state: %state', array('%component' => 'uuid_entities', '%feature' => $feature, '%state' => $readable_state));
560
    }
561
    // Ensure that the features we used is in default state.
562
    $states = features_get_component_states(array($feature), TRUE, TRUE);
563
    if (!$this->assertEqual($states[$feature]['uuid_entities'], $state, $message)) {
564
      debug(format_string('Enabling functionality to show diff output for debug purposes.'));
565
      $success = module_enable(array('diff'));
566
      if ($success) {
567
        // Make sure we run on cold caches.
568
        drupal_flush_all_caches();
569
        drupal_static_reset();
570

    
571
        $user = $this->drupalCreateUser(array('administer features'));
572
        $this->drupalLogin($user);
573
        $this->drupalGet('admin/structure/features/' . $feature . '/diff');
574
      }
575
      else {
576
        debug(format_string('Download !module to see diff output for debug purposes.', array('!module' => 'diff.module')));
577
      }
578
    }
579
  }
580

    
581
  function getEntityByUuid($entity_type, $uuid) {
582
    $ids = entity_get_id_by_uuid($entity_type, array($uuid));
583
    $entities = entity_load($entity_type, $ids, NULL, TRUE);
584
    return reset($entities);
585
  }
586

    
587
  function enableFeature($feature) {
588
    $success = module_enable(array($feature), TRUE);
589
    $this->assertTrue($success, t('Enabled modules: %modules', array('%modules' => implode(', ', array($feature)))));
590
    // Make sure we run on cold caches.
591
    drupal_flush_all_caches();
592
    drupal_static_reset();
593
  }
594

    
595
  function revertFeature($feature) {
596
    features_revert(array($feature => array('uuid_entities')));
597
    $this->assertTrue(TRUE, format_string('Reverted feature: %feature', array('%feature' => $feature)));
598
  }
599

    
600
  function testImport() {
601
    $term1 = $this->getEntityByUuid('taxonomy_term', $this->term1_uuid);
602
    $term2 = $this->getEntityByUuid('taxonomy_term', $this->term2_uuid);
603
    $term3 = $this->getEntityByUuid('taxonomy_term', $this->term3_uuid);
604
    $node1 = $this->getEntityByUuid('node', $this->node1_uuid);
605
    $node2 = $this->getEntityByUuid('node', $this->node2_uuid);
606
    $user1 = $this->getEntityByUuid('user', $this->user1_uuid);
607

    
608
    // Ensure that we don't have our entities yet.
609
    $this->assertTrue(empty($term1), 'Term 1 has not been created yet.');
610
    $this->assertTrue(empty($term2), 'Term 2 has not been created yet.');
611
    $this->assertTrue(empty($term3), 'Term 3 has not been created yet.');
612
    $this->assertTrue(empty($node1), 'Node 1 has not been created yet.');
613
    $this->assertTrue(empty($node2), 'Node 2 has not been created yet.');
614
    $this->assertTrue(empty($user1), 'User 1 has not been created yet.');
615

    
616
    $this->enableFeature('uuid_default_entities_example');
617

    
618
    $term1 = $this->getEntityByUuid('taxonomy_term', $this->term1_uuid);
619
    $term2 = $this->getEntityByUuid('taxonomy_term', $this->term2_uuid);
620
    $term3 = $this->getEntityByUuid('taxonomy_term', $this->term3_uuid);
621
    $node1 = $this->getEntityByUuid('node', $this->node1_uuid);
622
    $node2 = $this->getEntityByUuid('node', $this->node2_uuid);
623
    $user1 = $this->getEntityByUuid('user', $this->user1_uuid);
624

    
625
    // Ensure that our entities was created.
626
    $this->assertEqual($term1->uuid, $this->term1_uuid, 'Term 1 was created.');
627
    $this->assertEqual($term2->uuid, $this->term2_uuid, 'Term 2 was created.');
628
    $this->assertEqual($term3->uuid, $this->term3_uuid, 'Term 3 was created.');
629
    $this->assertEqual($node1->uuid, $this->node1_uuid, 'Node 1 was created.');
630
    $this->assertEqual($node2->uuid, $this->node2_uuid, 'Node 2 was created.');
631
    $this->assertEqual($user1->uuid, $this->user1_uuid, 'User 1 was created.');
632

    
633
    // Check the features state.
634
    $this->assertFeatureState('uuid_default_entities_example', FEATURES_DEFAULT);
635

    
636
    // New property.
637
    $new = 'foo bar';
638
    // Change a term.
639
    $term1->name = $new;
640
    $status = taxonomy_term_save($term1);
641
    $this->assertEqual($status, SAVED_UPDATED, 'Updated term 1.');
642
    // Change a node.
643
    $node1->title = $new;
644
    node_save($node1);
645
    $this->assertEqual($node1->title, $new, 'Updated node 1.');
646
    // Change a user.
647
    $user1->name = $new;
648
    $updated_user = user_save($user1);
649
    $this->assertEqual($user1->name, $updated_user->name, 'Updated user 1.');
650

    
651
    // Check the features state.
652
    $this->assertFeatureState('uuid_default_entities_example', FEATURES_OVERRIDDEN);
653

    
654
    // Revert the feature.
655
    $this->revertFeature('uuid_default_entities_example');
656

    
657
    // Check the features state.
658
    $this->assertFeatureState('uuid_default_entities_example', FEATURES_DEFAULT);
659
  }
660
}
661

    
662
class UUIDImportEntitiesWithDeploy extends UUIDImportEntitiesTestCase {
663

    
664
  public static function getInfo() {
665
    return array(
666
      'name' => 'Import UUID entities, with Deploy',
667
      'description' => 'Test importing UUID entities with Features and Deploy.',
668
      'group' => 'UUID',
669
    );
670
  }
671

    
672
  function setUp() {
673
    parent::setUp('taxonomy', 'uuid', 'entity', 'features', 'deploy', 'deploy_example');
674
  }
675
}
676

    
677
class UUIDImportEntitiesWithoutDeploy extends UUIDImportEntitiesTestCase {
678

    
679
  public static function getInfo() {
680
    return array(
681
      'name' => 'Import UUID entities, without Deploy',
682
      'description' => 'Test importing UUID entities with Features only.',
683
      'group' => 'UUID',
684
    );
685
  }
686

    
687
  function setUp() {
688
    parent::setUp('taxonomy', 'uuid', 'entity', 'features');
689
  }
690
}