annotate core/modules/taxonomy/tests/src/Functional/EfqTest.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 /**
Chris@0 6 * Verifies operation of a taxonomy-based Entity Query.
Chris@0 7 *
Chris@0 8 * @group taxonomy
Chris@0 9 */
Chris@0 10 class EfqTest extends TaxonomyTestBase {
Chris@0 11
Chris@0 12 /**
Chris@0 13 * Vocabulary for testing.
Chris@0 14 *
Chris@0 15 * @var \Drupal\taxonomy\VocabularyInterface
Chris@0 16 */
Chris@0 17 protected $vocabulary;
Chris@0 18
Chris@0 19 protected function setUp() {
Chris@0 20 parent::setUp();
Chris@0 21 $this->drupalLogin($this->drupalCreateUser(['administer taxonomy']));
Chris@0 22 $this->vocabulary = $this->createVocabulary();
Chris@0 23 }
Chris@0 24
Chris@0 25 /**
Chris@0 26 * Tests that a basic taxonomy entity query works.
Chris@0 27 */
Chris@0 28 public function testTaxonomyEfq() {
Chris@0 29 $terms = [];
Chris@0 30 for ($i = 0; $i < 5; $i++) {
Chris@0 31 $term = $this->createTerm($this->vocabulary);
Chris@0 32 $terms[$term->id()] = $term;
Chris@0 33 }
Chris@0 34 $result = \Drupal::entityQuery('taxonomy_term')->execute();
Chris@0 35 sort($result);
Chris@0 36 $this->assertEqual(array_keys($terms), $result, 'Taxonomy terms were retrieved by entity query.');
Chris@0 37 $tid = reset($result);
Chris@0 38 $ids = (object) [
Chris@0 39 'entity_type' => 'taxonomy_term',
Chris@0 40 'entity_id' => $tid,
Chris@0 41 'bundle' => $this->vocabulary->id(),
Chris@0 42 ];
Chris@0 43 $term = _field_create_entity_from_ids($ids);
Chris@0 44 $this->assertEqual($term->id(), $tid, 'Taxonomy term can be created based on the IDs.');
Chris@0 45
Chris@0 46 // Create a second vocabulary and five more terms.
Chris@0 47 $vocabulary2 = $this->createVocabulary();
Chris@0 48 $terms2 = [];
Chris@0 49 for ($i = 0; $i < 5; $i++) {
Chris@0 50 $term = $this->createTerm($vocabulary2);
Chris@0 51 $terms2[$term->id()] = $term;
Chris@0 52 }
Chris@0 53
Chris@0 54 $result = \Drupal::entityQuery('taxonomy_term')
Chris@0 55 ->condition('vid', $vocabulary2->id())
Chris@0 56 ->execute();
Chris@0 57 sort($result);
Chris@0 58 $this->assertEqual(array_keys($terms2), $result, format_string('Taxonomy terms from the %name vocabulary were retrieved by entity query.', ['%name' => $vocabulary2->label()]));
Chris@0 59 $tid = reset($result);
Chris@0 60 $ids = (object) [
Chris@0 61 'entity_type' => 'taxonomy_term',
Chris@0 62 'entity_id' => $tid,
Chris@0 63 'bundle' => $vocabulary2->id(),
Chris@0 64 ];
Chris@0 65 $term = _field_create_entity_from_ids($ids);
Chris@0 66 $this->assertEqual($term->id(), $tid, 'Taxonomy term can be created based on the IDs.');
Chris@0 67 }
Chris@0 68
Chris@0 69 }