annotate core/modules/taxonomy/tests/src/Functional/LoadMultipleTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\taxonomy\Functional;
Chris@0 4
Chris@0 5 use Drupal\taxonomy\Entity\Term;
Chris@0 6
Chris@0 7 /**
Chris@0 8 * Tests the loading of multiple taxonomy terms at once.
Chris@0 9 *
Chris@0 10 * @group taxonomy
Chris@0 11 */
Chris@0 12 class LoadMultipleTest extends TaxonomyTestBase {
Chris@0 13
Chris@0 14 protected function setUp() {
Chris@0 15 parent::setUp();
Chris@0 16 $this->drupalLogin($this->drupalCreateUser(['administer taxonomy']));
Chris@0 17 }
Chris@0 18
Chris@0 19 /**
Chris@0 20 * Create a vocabulary and some taxonomy terms, ensuring they're loaded
Chris@0 21 * correctly using entity_load_multiple().
Chris@0 22 */
Chris@0 23 public function testTaxonomyTermMultipleLoad() {
Chris@0 24 // Create a vocabulary.
Chris@0 25 $vocabulary = $this->createVocabulary();
Chris@0 26
Chris@0 27 // Create five terms in the vocabulary.
Chris@0 28 $i = 0;
Chris@0 29 while ($i < 5) {
Chris@0 30 $i++;
Chris@0 31 $this->createTerm($vocabulary);
Chris@0 32 }
Chris@0 33 // Load the terms from the vocabulary.
Chris@0 34 $terms = entity_load_multiple_by_properties('taxonomy_term', ['vid' => $vocabulary->id()]);
Chris@0 35 $count = count($terms);
Chris@0 36 $this->assertEqual($count, 5, format_string('Correct number of terms were loaded. @count terms.', ['@count' => $count]));
Chris@0 37
Chris@0 38 // Load the same terms again by tid.
Chris@0 39 $terms2 = Term::loadMultiple(array_keys($terms));
Chris@0 40 $this->assertEqual($count, count($terms2), 'Five terms were loaded by tid.');
Chris@0 41 $this->assertEqual($terms, $terms2, 'Both arrays contain the same terms.');
Chris@0 42
Chris@0 43 // Remove one term from the array, then delete it.
Chris@0 44 $deleted = array_shift($terms2);
Chris@0 45 $deleted->delete();
Chris@0 46 $deleted_term = Term::load($deleted->id());
Chris@0 47 $this->assertFalse($deleted_term);
Chris@0 48
Chris@0 49 // Load terms from the vocabulary by vid.
Chris@0 50 $terms3 = entity_load_multiple_by_properties('taxonomy_term', ['vid' => $vocabulary->id()]);
Chris@0 51 $this->assertEqual(count($terms3), 4, 'Correct number of terms were loaded.');
Chris@0 52 $this->assertFalse(isset($terms3[$deleted->id()]));
Chris@0 53
Chris@0 54 // Create a single term and load it by name.
Chris@0 55 $term = $this->createTerm($vocabulary);
Chris@0 56 $loaded_terms = entity_load_multiple_by_properties('taxonomy_term', ['name' => $term->getName()]);
Chris@0 57 $this->assertEqual(count($loaded_terms), 1, 'One term was loaded.');
Chris@0 58 $loaded_term = reset($loaded_terms);
Chris@0 59 $this->assertEqual($term->id(), $loaded_term->id(), 'Term loaded by name successfully.');
Chris@0 60 }
Chris@0 61
Chris@0 62 }