Projet

Général

Profil

Révision e7101f36

Ajouté par Julien Enselme il y a environ 9 ans

Update uuid 1.0-alpha5 1.0-alpha6

Voir les différences:

drupal7/sites/all/modules/uuid/uuid.test
10 10
 */
11 11
class UUIDTestCase extends DrupalWebTestCase {
12 12

  
13
  /**
14
   * {@inheritdoc}
15
   */
13 16
  function setUp() {
14 17
    parent::setUp(func_get_args());
15 18
  }
......
27 30
 */
28 31
class UUIDAPITestCase extends UUIDTestCase {
29 32

  
33
  /**
34
   * {@inheritdoc}
35
   */
30 36
  public static function getInfo() {
31 37
    return array(
32 38
      'name' => 'UUID API',
......
35 41
    );
36 42
  }
37 43

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

  
51
  /**
52
   * Tests uuid function calls.
53
   */
42 54
  function testAPIFunctions() {
43 55
    // This is a valid UUID, we know that.
44 56
    $valid_uuid = '0ab26e6b-f074-4e44-9da6-1205fa0e9761';
......
62 74
 */
63 75
class UUIDEntityTestCase extends UUIDTestCase {
64 76

  
77
  /**
78
   * {@inheritdoc}
79
   */
65 80
  public static function getInfo() {
66 81
    return array(
67 82
      'name' => 'Entity API functions',
......
70 85
    );
71 86
  }
72 87

  
88
  /**
89
   * {@inheritdoc}
90
   */
73 91
  function setUp() {
74 92
    parent::setUp('uuid');
75 93
  }
......
101 119
 */
102 120
class UUIDUserTestCase extends UUIDTestCase {
103 121

  
122
  /**
123
   * {@inheritdoc}
124
   */
104 125
  public static function getInfo() {
105 126
    return array(
106 127
      'name' => 'User implementation',
......
109 130
    );
110 131
  }
111 132

  
133
  /**
134
   * {@inheritdoc}
135
   */
112 136
  function setUp() {
113 137
    // Some tests depends on the optional Entity API module.
114 138
    if (module_exists('entity')) {
......
161 185
 */
162 186
class UUIDNodeTestCase extends UUIDTestCase {
163 187

  
188
  /**
189
   * {@inheritdoc}
190
   */
164 191
  public static function getInfo() {
165 192
    return array(
166 193
      'name' => 'Node implementation',
......
169 196
    );
170 197
  }
171 198

  
199
  /**
200
   * {@inheritdoc}
201
   */
172 202
  function setUp() {
173 203
    // Some tests depends on the optional Entity API module.
174 204
    if (module_exists('entity')) {
......
181 211

  
182 212
  /**
183 213
   * Tests CRUD on nodes with UUID functions.
214
   *
215
   * @todo
216
   *   Break out into multiple test methods to loosen coupling between tests.
184 217
   */
185 218
  function testNodeCRUD() {
186 219
    // Create some entities that we will work with.
......
192 225

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

  
202 239
    // Test node update, with new revision.
203 240
    $node_test = clone $node;
......
210 247
    $this->assertUUID($node_test->vuuid, 'The new node revision UUID was valid.');
211 248

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

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

  
218 273
    // The following tests depends on the optional Entity API module.
219 274
    if (module_exists('entity')) {
......
248 303
      $this->assertUUID($node_test->vuuid, 'New node revision UUID was valid.');
249 304
      $this->assertEqual($node_test->uid, $node->uid, "Node property 'uid' was intact after saving with UUID, when creating new revision.");
250 305

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

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

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

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

  
251 372
      // Test entity_uuid_delete() for nodes.
252 373
      entity_uuid_delete('node', $node->uuid);
253 374
      $node_test = node_load($node->nid);
......
264 385
 */
265 386
class UUIDCommentTestCase extends CommentHelperCase {
266 387

  
388
  /**
389
   * {@inheritdoc}
390
   */
267 391
  public static function getInfo() {
268 392
    return array(
269 393
      'name' => 'Comment implementation',
......
339 463
 */
340 464
class UUIDTaxonomyTestCase extends TaxonomyWebTestCase {
341 465

  
466
  /**
467
   * {@inheritdoc}
468
   */
342 469
  public static function getInfo() {
343 470
    return array(
344 471
      'name' => 'Taxonomy implementation',
......
348 475
  }
349 476

  
350 477
  /**
478
   * {@inheritdoc}
479
   *
351 480
   * A lot of code here is taken from TaxonomyTermTestCase::setUp().
352 481
   */
353 482
  function setUp() {
......
420 549
 */
421 550
class UUIDSyncTestCase extends UUIDTestCase {
422 551

  
552
  /**
553
   * {@inheritdoc}
554
   */
423 555
  public static function getInfo() {
424 556
    return array(
425 557
      'name' => 'UUID sync',
......
435 567
   *   There are something weird around this assertion.
436 568
   */
437 569
  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);
570
    $this->assertTrue(db_field_exists($table, $column), $message);
447 571
  }
448 572

  
573
  /**
574
   * Tests creating UUIDs for entities that don't have them.
575
   */
449 576
  function testSync() {
450 577
    // These entities will not have UUID from the start, since the UUID module
451 578
    // isn't installed yet.
......
481 608
    $this->assertUUID($user_test->uuid, 'User UUID was generated when clicking the sync button.');
482 609
  }
483 610
}
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
}

Formats disponibles : Unified diff