comparison core/modules/taxonomy/tests/src/Functional/LoadMultipleTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Tests\taxonomy\Functional;
4
5 use Drupal\taxonomy\Entity\Term;
6
7 /**
8 * Tests the loading of multiple taxonomy terms at once.
9 *
10 * @group taxonomy
11 */
12 class LoadMultipleTest extends TaxonomyTestBase {
13
14 protected function setUp() {
15 parent::setUp();
16 $this->drupalLogin($this->drupalCreateUser(['administer taxonomy']));
17 }
18
19 /**
20 * Create a vocabulary and some taxonomy terms, ensuring they're loaded
21 * correctly using entity_load_multiple().
22 */
23 public function testTaxonomyTermMultipleLoad() {
24 // Create a vocabulary.
25 $vocabulary = $this->createVocabulary();
26
27 // Create five terms in the vocabulary.
28 $i = 0;
29 while ($i < 5) {
30 $i++;
31 $this->createTerm($vocabulary);
32 }
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]));
37
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.');
42
43 // Remove one term from the array, then delete it.
44 $deleted = array_shift($terms2);
45 $deleted->delete();
46 $deleted_term = Term::load($deleted->id());
47 $this->assertFalse($deleted_term);
48
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()]));
53
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.');
60 }
61
62 }