1 taxonomy.test TaxonomyLoadMultipleUnitTest::testTaxonomyTermMultipleLoad()

Create a vocabulary and some taxonomy terms, ensuring they're loaded correctly using taxonomy_term_load_multiple().


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


Test the taxonomy_term_load_multiple() function.


function testTaxonomyTermMultipleLoad() {
  // Create a vocabulary.
  $vocabulary = $this->createVocabulary();

  // Create five terms in the vocabulary.
  $i = 0;
  while ($i < 5) {
  // Load the terms from the vocabulary.
  $terms = taxonomy_term_load_multiple(NULL, array('vocabulary' => $vocabulary->machine_name));
  $count = count($terms);
  $this->assertEqual($count, 5, format_string('Correct number of terms were loaded. !count terms.', array('!count' => $count)));

  // Load the same terms again by tid.
  $terms2 = taxonomy_term_load_multiple(array_keys($terms));
  $this->assertEqual($count, count($terms2), 'Five terms were loaded by tid.');
  $this->assertEqual($terms, $terms2, 'Both arrays contain the same terms.');

  // Load the terms by tid, with a condition on machine name.
  $terms3 = taxonomy_term_load_multiple(array_keys($terms2), array('vocabulary' => $vocabulary->machine_name));
  $this->assertEqual($terms2, $terms3, 'Same terms found when limiting load to vocabulary.');

  // Remove one term from the array, then delete it.
  $deleted = array_shift($terms3);
  $deleted_term = taxonomy_term_load($deleted->tid);

  // Load terms from the vocabulary by vocabulary name.
  $terms4 = taxonomy_term_load_multiple(NULL, array('vocabulary' => $vocabulary->machine_name));
  $this->assertEqual(count($terms4), 4, 'Correct number of terms were loaded.');

  // Create a single term and load it by name.
  $term = $this->createTerm($vocabulary);
  $loaded_terms = taxonomy_term_load_multiple(array(), array('name' => $term->name));
  $this->assertEqual(count($loaded_terms), 1, 'One term was loaded.');
  $loaded_term = reset($loaded_terms);
  $this->assertEqual($term->tid, $loaded_term->tid, 'Term loaded by name successfully.');