3 namespace Drupal\Tests\taxonomy\Functional;
5 use Drupal\taxonomy\Entity\Term;
8 * Tests the loading of multiple taxonomy terms at once.
12 class LoadMultipleTest extends TaxonomyTestBase {
14 protected function setUp() {
16 $this->drupalLogin($this->drupalCreateUser(['administer taxonomy']));
20 * Create a vocabulary and some taxonomy terms, ensuring they're loaded
21 * correctly using entity_load_multiple().
23 public function testTaxonomyTermMultipleLoad() {
24 // Create a vocabulary.
25 $vocabulary = $this->createVocabulary();
27 // Create five terms in the vocabulary.
31 $this->createTerm($vocabulary);
33 // Load the terms from the vocabulary.
34 $terms = entity_load_multiple_by_properties('taxonomy_term', ['vid' => $vocabulary->id()]);
35 $count = count($terms);
36 $this->assertEqual($count, 5, format_string('Correct number of terms were loaded. @count terms.', ['@count' => $count]));
38 // Load the same terms again by tid.
39 $terms2 = Term::loadMultiple(array_keys($terms));
40 $this->assertEqual($count, count($terms2), 'Five terms were loaded by tid.');
41 $this->assertEqual($terms, $terms2, 'Both arrays contain the same terms.');
43 // Remove one term from the array, then delete it.
44 $deleted = array_shift($terms2);
46 $deleted_term = Term::load($deleted->id());
47 $this->assertFalse($deleted_term);
49 // Load terms from the vocabulary by vid.
50 $terms3 = entity_load_multiple_by_properties('taxonomy_term', ['vid' => $vocabulary->id()]);
51 $this->assertEqual(count($terms3), 4, 'Correct number of terms were loaded.');
52 $this->assertFalse(isset($terms3[$deleted->id()]));
54 // Create a single term and load it by name.
55 $term = $this->createTerm($vocabulary);
56 $loaded_terms = entity_load_multiple_by_properties('taxonomy_term', ['name' => $term->getName()]);
57 $this->assertEqual(count($loaded_terms), 1, 'One term was loaded.');
58 $loaded_term = reset($loaded_terms);
59 $this->assertEqual($term->id(), $loaded_term->id(), 'Term loaded by name successfully.');