You are here

function EntityDependencyTestCase::testCumbersomeIterator in Entity Dependency API 7

Test cumbersome scenario with nodes, taxonomy and users.

File

./entity_dependency.test, line 171
Entity Dependency tests.

Class

EntityDependencyTestCase
Tests the entity dependency iterator.

Code

function testCumbersomeIterator() {

  // Add the 'field_tags' field.
  $user = $this
    ->drupalCreateUser();
  $term1 = $this
    ->createTerm();
  $term2 = $this
    ->createTerm();
  $term3 = $this
    ->createTerm();
  $term4 = $this
    ->createTerm();
  $term1->parent = array(
    $term3->tid,
  );
  taxonomy_term_save($term1);
  $node1 = $this
    ->drupalCreateNode(array(
    'type' => 'article',
    'uid' => $user->uid,
    'field_tags' => array(
      LANGUAGE_NONE => array(
        array(
          'tid' => $term1->tid,
        ),
        array(
          'tid' => $term2->tid,
        ),
      ),
    ),
  ));
  $node2 = $this
    ->drupalCreateNode(array(
    'type' => 'article',
    'uid' => $user->uid,
    // We fake that nid 1 is the translation of this node, just to test if
    // the dependency works. We don't want to depend on a node reference.
    'tnid' => $node1->nid,
    'field_tags' => array(
      LANGUAGE_NONE => array(
        array(
          'tid' => $term1->tid,
        ),
        array(
          'tid' => $term2->tid,
        ),
        array(
          'tid' => $term3->tid,
        ),
        array(
          'tid' => $term4->tid,
        ),
      ),
    ),
  ));

  // This node only has dependencies that should be detected by $node2
  // already.
  $node3 = $this
    ->drupalCreateNode(array(
    'type' => 'article',
    'uid' => $user->uid,
    'field_tags' => array(
      LANGUAGE_NONE => array(
        array(
          'tid' => $term1->tid,
        ),
        array(
          'tid' => $term2->tid,
        ),
        array(
          'tid' => $term3->tid,
        ),
      ),
    ),
  ));

  // Add only the last node to the collection. What should come out of the
  // iterator should be all it's dependencies, and last the node it self.
  $this->entities = array(
    array(
      'id' => $node2->nid,
      'type' => 'node',
    ),
    array(
      'id' => $node3->nid,
      'type' => 'node',
    ),
  );
  $i = 0;
  foreach ($this
    ->getIterator() as $entity) {
    switch ($i) {
      case 0:
        $this
          ->assertCorrectEntityOrder($entity, 'taxonomy_term', 'tid', $term3->tid);
        break;
      case 1:
        $this
          ->assertCorrectEntityOrder($entity, 'taxonomy_term', 'tid', $term1->tid);
        break;
      case 2:
        $this
          ->assertCorrectEntityOrder($entity, 'taxonomy_term', 'tid', $term2->tid);
        break;
      case 3:
        $this
          ->assertCorrectEntityOrder($entity, 'taxonomy_term', 'tid', $term4->tid);
        break;
      case 4:
        $this
          ->assertCorrectEntityOrder($entity, 'user', 'uid', $user->uid);
        break;
      case 5:
        $this
          ->assertCorrectEntityOrder($entity, 'node', 'nid', $node1->nid);
        break;
      case 6:
        $this
          ->assertCorrectEntityOrder($entity, 'node', 'nid', $node2->nid);
        break;
      case 7:
        $this
          ->assertCorrectEntityOrder($entity, 'node', 'nid', $node3->nid);
        break;
    }
    $i++;
  }
  $this
    ->assertEqual($i, 8, 'Correct number of entities was iterated over.');
}