3 namespace Drupal\Tests\taxonomy\Functional;
5 use Drupal\Tests\BrowserTestBase;
8 * Tests that appropriate query tags are added.
12 class TaxonomyQueryAlterTest extends BrowserTestBase {
14 use TaxonomyTestTrait;
21 public static $modules = ['taxonomy', 'taxonomy_test'];
24 * Tests that appropriate tags are added when querying the database.
26 public function testTaxonomyQueryAlter() {
27 // Create a new vocabulary and add a few terms to it.
28 $vocabulary = $this->createVocabulary();
30 for ($i = 0; $i < 5; $i++) {
31 $terms[$i] = $this->createTerm($vocabulary);
34 // Set up hierarchy. Term 2 is a child of 1.
35 $terms[2]->parent = $terms[1]->id();
38 $term_storage = \Drupal::entityManager()->getStorage('taxonomy_term');
40 $this->setupQueryTagTestHooks();
41 $loaded_term = $term_storage->load($terms[0]->id());
42 $this->assertEqual($loaded_term->id(), $terms[0]->id(), 'First term was loaded');
43 $this->assertQueryTagTestResult(1, 0, 'TermStorage::load()');
45 $this->setupQueryTagTestHooks();
46 $loaded_terms = $term_storage->loadTree($vocabulary->id());
47 $this->assertEqual(count($loaded_terms), count($terms), 'All terms were loaded');
48 $this->assertQueryTagTestResult(1, 1, 'TermStorage::loadTree()');
50 $this->setupQueryTagTestHooks();
51 $loaded_terms = $term_storage->loadParents($terms[2]->id());
52 $this->assertEqual(count($loaded_terms), 1, 'All parent terms were loaded');
53 $this->assertQueryTagTestResult(2, 1, 'TermStorage::loadParents()');
55 $this->setupQueryTagTestHooks();
56 $loaded_terms = $term_storage->loadChildren($terms[1]->id());
57 $this->assertEqual(count($loaded_terms), 1, 'All child terms were loaded');
58 $this->assertQueryTagTestResult(2, 1, 'TermStorage::loadChildren()');
60 $this->setupQueryTagTestHooks();
61 $query = db_select('taxonomy_term_data', 't');
62 $query->addField('t', 'tid');
63 $query->addTag('taxonomy_term_access');
64 $tids = $query->execute()->fetchCol();
65 $this->assertEqual(count($tids), count($terms), 'All term IDs were retrieved');
66 $this->assertQueryTagTestResult(1, 1, 'custom db_select() with taxonomy_term_access tag (preferred)');
68 $this->setupQueryTagTestHooks();
69 $query = db_select('taxonomy_term_data', 't');
70 $query->addField('t', 'tid');
71 $query->addTag('term_access');
72 $tids = $query->execute()->fetchCol();
73 $this->assertEqual(count($tids), count($terms), 'All term IDs were retrieved');
74 $this->assertQueryTagTestResult(1, 1, 'custom db_select() with term_access tag (deprecated)');
76 $this->setupQueryTagTestHooks();
77 $query = \Drupal::entityQuery('taxonomy_term');
78 $query->addTag('taxonomy_term_access');
79 $result = $query->execute();
80 $this->assertEqual(count($result), count($terms), 'All term IDs were retrieved');
81 $this->assertQueryTagTestResult(1, 1, 'custom EntityFieldQuery with taxonomy_term_access tag (preferred)');
83 $this->setupQueryTagTestHooks();
84 $query = \Drupal::entityQuery('taxonomy_term');
85 $query->addTag('term_access');
86 $result = $query->execute();
87 $this->assertEqual(count($result), count($terms), 'All term IDs were retrieved');
88 $this->assertQueryTagTestResult(1, 1, 'custom EntityFieldQuery with term_access tag (deprecated)');
92 * Sets up the hooks in the test module.
94 protected function setupQueryTagTestHooks() {
95 taxonomy_terms_static_reset();
96 \Drupal::state()->set('taxonomy_test_query_alter', 0);
97 \Drupal::state()->set('taxonomy_test_query_term_access_alter', 0);
98 \Drupal::state()->set('taxonomy_test_query_taxonomy_term_access_alter', 0);
102 * Verifies invocation of the hooks in the test module.
104 * @param int $expected_generic_invocations
105 * The number of times the generic query_alter hook is expected to have
107 * @param int $expected_specific_invocations
108 * The number of times the tag-specific query_alter hooks are expected to
110 * @param string $method
111 * A string describing the invoked function which generated the query.
113 protected function assertQueryTagTestResult($expected_generic_invocations, $expected_specific_invocations, $method) {
114 $this->assertIdentical($expected_generic_invocations, \Drupal::state()->get('taxonomy_test_query_alter'), 'hook_query_alter() invoked when executing ' . $method);
115 $this->assertIdentical($expected_specific_invocations, \Drupal::state()->get('taxonomy_test_query_term_access_alter'), 'Deprecated hook_query_term_access_alter() invoked when executing ' . $method);
116 $this->assertIdentical($expected_specific_invocations, \Drupal::state()->get('taxonomy_test_query_taxonomy_term_access_alter'), 'Preferred hook_query_taxonomy_term_access_alter() invoked when executing ' . $method);