View source
<?php
namespace Drupal\Tests\taxonomy\Kernel;
use Drupal\Core\Database\Database;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
class TaxonomyQueryAlterTest extends KernelTestBase {
use TaxonomyTestTrait;
protected static $modules = [
'filter',
'taxonomy',
'taxonomy_test',
'text',
'user',
];
public function testTaxonomyQueryAlter() {
$this
->installEntitySchema('taxonomy_term');
$vocabulary = $this
->createVocabulary();
$terms = [];
for ($i = 0; $i < 5; $i++) {
$terms[$i] = $this
->createTerm($vocabulary);
}
$terms[2]->parent = $terms[1]
->id();
$terms[2]
->save();
$term_storage = $this->container
->get('entity_type.manager')
->getStorage('taxonomy_term');
$this
->setupQueryTagTestHooks();
$loaded_term = $term_storage
->load($terms[0]
->id());
$this
->assertEquals($terms[0]
->id(), $loaded_term
->id());
$this
->assertQueryTagTestResult(1, 0);
$this
->setupQueryTagTestHooks();
$loaded_terms = $term_storage
->loadTree($vocabulary
->id());
$this
->assertCount(5, $loaded_terms);
$this
->assertQueryTagTestResult(1, 1);
$this
->setupQueryTagTestHooks();
$loaded_terms = $term_storage
->loadParents($terms[2]
->id());
$this
->assertCount(1, $loaded_terms);
$this
->assertQueryTagTestResult(3, 1);
$this
->setupQueryTagTestHooks();
$loaded_terms = $term_storage
->loadChildren($terms[1]
->id());
$this
->assertCount(1, $loaded_terms);
$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();
$this
->assertCount(5, $tids);
$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();
$this
->assertCount(5, $tids);
$this
->assertQueryTagTestResult(1, 1);
$this
->setupQueryTagTestHooks();
$query = \Drupal::entityQuery('taxonomy_term')
->accessCheck(FALSE);
$query
->addTag('taxonomy_term_access');
$result = $query
->execute();
$this
->assertCount(5, $result);
$this
->assertQueryTagTestResult(1, 1);
$this
->setupQueryTagTestHooks();
$query = \Drupal::entityQuery('taxonomy_term')
->accessCheck(FALSE);
$query
->addTag('term_access');
$result = $query
->execute();
$this
->assertCount(5, $result);
$this
->assertQueryTagTestResult(1, 1);
}
protected function setupQueryTagTestHooks() {
$this->container
->get('entity_type.manager')
->getStorage('taxonomy_term')
->resetCache();
$state = $this->container
->get('state');
$state
->set('taxonomy_test_query_alter', 0);
$state
->set('taxonomy_test_query_term_access_alter', 0);
$state
->set('taxonomy_test_query_taxonomy_term_access_alter', 0);
}
protected function assertQueryTagTestResult($expected_generic_invocations, $expected_specific_invocations) {
$state = $this->container
->get('state');
$this
->assertEquals($expected_generic_invocations, $state
->get('taxonomy_test_query_alter'));
$this
->assertEquals($expected_specific_invocations, $state
->get('taxonomy_test_query_term_access_alter'));
$this
->assertEquals($expected_specific_invocations, $state
->get('taxonomy_test_query_taxonomy_term_access_alter'));
}
}