1 taxonomy.test TaxonomyEFQTestCase::testTaxonomyEFQ()

Tests that a basic taxonomy EntityFieldQuery works.

File

core/modules/taxonomy/tests/taxonomy.test, line 2052
Tests for taxonomy.module.

Class

TaxonomyEFQTestCase
Tests the functionality of EntityFieldQuery for taxonomy entities.

Code

function testTaxonomyEFQ() {
  $terms = array();
  for ($i = 0; $i < 5; $i++) {
    $term = $this->createTerm($this->vocabulary);
    $terms[$term->tid] = $term;
  }
  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'taxonomy_term');
  $result = $query->execute();
  $result = $result['taxonomy_term'];
  asort($result);
  $this->assertEqual(array_keys($terms), array_keys($result), 'Taxonomy terms were retrieved by EntityFieldQuery.');

  $first_result = reset($result);
  $term = _field_create_entity_from_ids($first_result);
  $this->assertEqual($term->tid, $first_result->entity_id, '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;
  }
  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'taxonomy_term');
  $query->entityCondition('bundle', $vocabulary2->machine_name);
  $result = $query->execute();
  $result = $result['taxonomy_term'];
  asort($result);
  $this->assertEqual(array_keys($terms2), array_keys($result), format_string('Taxonomy terms from the %name vocabulary were retrieved by EntityFieldQuery.', array('%name' => $vocabulary2->name)));
}