You are here

public function UUIDNodeTestCase::testNodeCrud in Universally Unique IDentifier 7

Tests CRUD on nodes with UUID functions.

@todo Break out into multiple test methods to loosen coupling between tests.

File

./uuid.test, line 312
Test suite for UUID module.

Class

UUIDNodeTestCase
Tests the Node implementation.

Code

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.');
  }
}