function EfqTest::testTaxonomyEfq

Tests that a basic taxonomy entity query works.

File

drupal/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/EfqTest.php, line 34
Definition of Drupal\taxonomy\Tests\EfqTest.

Class

EfqTest
Tests the functionality of EntityQueryInterface for taxonomy entities.

Namespace

Drupal\taxonomy\Tests

Code

function testTaxonomyEfq() {
  $terms = array();
  for ($i = 0; $i < 5; $i++) {
    $term = $this
      ->createTerm($this->vocabulary);
    $terms[$term->tid] = $term;
  }
  $result = entity_query('taxonomy_term')
    ->execute();
  sort($result);
  $this
    ->assertEqual(array_keys($terms), $result, 'Taxonomy terms were retrieved by entity query.');
  $tid = reset($result);
  $ids = (object) array(
    'entity_type' => 'taxonomy_term',
    'entity_id' => $tid,
    'bundle' => $this->vocabulary->machine_name,
  );
  $term = _field_create_entity_from_ids($ids);
  $this
    ->assertEqual($term->tid, $tid, 'Taxonomy term can be created based on the IDs');

  // Create a second vocabulary and five more terms.
  $vocabulary2 = $this
    ->createVocabulary();
  $terms2 = array();
  for ($i = 0; $i < 5; $i++) {
    $term = $this
      ->createTerm($vocabulary2);
    $terms2[$term->tid] = $term;
  }
  $result = entity_query('taxonomy_term')
    ->condition('vid', $vocabulary2->vid)
    ->execute();
  sort($result);
  $this
    ->assertEqual(array_keys($terms2), $result, format_string('Taxonomy terms from the %name vocabulary were retrieved by entity query.', array(
    '%name' => $vocabulary2->name,
  )));
  $tid = reset($result);
  $ids = (object) array(
    'entity_type' => 'taxonomy_term',
    'entity_id' => $tid,
    'bundle' => $vocabulary2->machine_name,
  );
  $term = _field_create_entity_from_ids($ids);
  $this
    ->assertEqual($term->tid, $tid, 'Taxonomy term can be created based on the IDs');
}