function TaxonomyQueryAlterTest::testTaxonomyQueryAlter

Same name in other branches
  1. 9 core/modules/taxonomy/tests/src/Kernel/TaxonomyQueryAlterTest.php \Drupal\Tests\taxonomy\Kernel\TaxonomyQueryAlterTest::testTaxonomyQueryAlter()
  2. 8.9.x core/modules/taxonomy/tests/src/Kernel/TaxonomyQueryAlterTest.php \Drupal\Tests\taxonomy\Kernel\TaxonomyQueryAlterTest::testTaxonomyQueryAlter()
  3. 10 core/modules/taxonomy/tests/src/Kernel/TaxonomyQueryAlterTest.php \Drupal\Tests\taxonomy\Kernel\TaxonomyQueryAlterTest::testTaxonomyQueryAlter()

Tests that appropriate tags are added when querying the database.

File

core/modules/taxonomy/tests/src/Kernel/TaxonomyQueryAlterTest.php, line 34

Class

TaxonomyQueryAlterTest
Tests that appropriate query tags are added.

Namespace

Drupal\Tests\taxonomy\Kernel

Code

public function testTaxonomyQueryAlter() : void {
    $this->installEntitySchema('taxonomy_term');
    // Create a new vocabulary and add a few terms to it.
    $vocabulary = $this->createVocabulary();
    $terms = [];
    for ($i = 0; $i < 5; $i++) {
        $terms[$i] = $this->createTerm($vocabulary);
    }
    // Set up hierarchy. Term 2 is a child of 1.
    $terms[2]->parent = $terms[1]->id();
    $terms[2]->save();
    
    /** @var \Drupal\taxonomy\TermStorageInterface $term_storage */
    $term_storage = $this->container
        ->get('entity_type.manager')
        ->getStorage('taxonomy_term');
    $this->setupQueryTagTestHooks();
    $loaded_term = $term_storage->load($terms[0]->id());
    // First term was loaded.
    $this->assertEquals($terms[0]->id(), $loaded_term->id());
    // TermStorage::load().
    $this->assertQueryTagTestResult(1, 0);
    $this->setupQueryTagTestHooks();
    $loaded_terms = $term_storage->loadTree($vocabulary->id());
    // All terms were loaded.
    $this->assertCount(5, $loaded_terms);
    // TermStorage::loadTree().
    $this->assertQueryTagTestResult(1, 1);
    $this->setupQueryTagTestHooks();
    $loaded_terms = $term_storage->loadParents($terms[2]->id());
    // All parent terms were loaded.
    $this->assertCount(1, $loaded_terms);
    // TermStorage::loadParents().
    $this->assertQueryTagTestResult(3, 1);
    $this->setupQueryTagTestHooks();
    $loaded_terms = $term_storage->loadChildren($terms[1]->id());
    // All child terms were loaded.
    $this->assertCount(1, $loaded_terms);
    // TermStorage::loadChildren().
    $this->assertQueryTagTestResult(3, 1);
    $this->setupQueryTagTestHooks();
    $connection = Database::getConnection();
    $query = $connection->select('taxonomy_term_data', 't');
    $query->addField('t', 'tid');
    $query->addTag('taxonomy_term_access');
    $tids = $query->execute()
        ->fetchCol();
    // All term IDs were retrieved.
    $this->assertCount(5, $tids);
    // Database custom ::select() with 'taxonomy_term_access' tag (preferred).
    $this->assertQueryTagTestResult(1, 1);
    $this->setupQueryTagTestHooks();
    $query = $connection->select('taxonomy_term_data', 't');
    $query->addField('t', 'tid');
    $query->addTag('term_access');
    $tids = $query->execute()
        ->fetchCol();
    // All term IDs were retrieved.
    $this->assertCount(5, $tids);
    // Database custom ::select() with term_access tag (deprecated).
    $this->assertQueryTagTestResult(1, 1);
    $this->setupQueryTagTestHooks();
    $query = \Drupal::entityQuery('taxonomy_term')->accessCheck(FALSE);
    $query->addTag('taxonomy_term_access');
    $result = $query->execute();
    // All term IDs were retrieved.
    $this->assertCount(5, $result);
    // Custom entity query with taxonomy_term_access tag (preferred).
    $this->assertQueryTagTestResult(1, 1);
    $this->setupQueryTagTestHooks();
    $query = \Drupal::entityQuery('taxonomy_term')->accessCheck(FALSE);
    $query->addTag('term_access');
    $result = $query->execute();
    // All term IDs were retrieved.
    $this->assertCount(5, $result);
    // Custom entity query with taxonomy_term_access tag (preferred).
    $this->assertQueryTagTestResult(1, 1);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.