Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\taxonomy\Functional;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Tests\BrowserTestBase;
|
Chris@0
|
6
|
Chris@0
|
7 /**
|
Chris@0
|
8 * Tests that appropriate query tags are added.
|
Chris@0
|
9 *
|
Chris@0
|
10 * @group taxonomy
|
Chris@0
|
11 */
|
Chris@0
|
12 class TaxonomyQueryAlterTest extends BrowserTestBase {
|
Chris@0
|
13
|
Chris@0
|
14 use TaxonomyTestTrait;
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * Modules to enable.
|
Chris@0
|
18 *
|
Chris@0
|
19 * @var array
|
Chris@0
|
20 */
|
Chris@0
|
21 public static $modules = ['taxonomy', 'taxonomy_test'];
|
Chris@0
|
22
|
Chris@0
|
23 /**
|
Chris@0
|
24 * Tests that appropriate tags are added when querying the database.
|
Chris@0
|
25 */
|
Chris@0
|
26 public function testTaxonomyQueryAlter() {
|
Chris@0
|
27 // Create a new vocabulary and add a few terms to it.
|
Chris@0
|
28 $vocabulary = $this->createVocabulary();
|
Chris@0
|
29 $terms = [];
|
Chris@0
|
30 for ($i = 0; $i < 5; $i++) {
|
Chris@0
|
31 $terms[$i] = $this->createTerm($vocabulary);
|
Chris@0
|
32 }
|
Chris@0
|
33
|
Chris@0
|
34 // Set up hierarchy. Term 2 is a child of 1.
|
Chris@0
|
35 $terms[2]->parent = $terms[1]->id();
|
Chris@0
|
36 $terms[2]->save();
|
Chris@0
|
37
|
Chris@0
|
38 $term_storage = \Drupal::entityManager()->getStorage('taxonomy_term');
|
Chris@0
|
39
|
Chris@0
|
40 $this->setupQueryTagTestHooks();
|
Chris@0
|
41 $loaded_term = $term_storage->load($terms[0]->id());
|
Chris@0
|
42 $this->assertEqual($loaded_term->id(), $terms[0]->id(), 'First term was loaded');
|
Chris@0
|
43 $this->assertQueryTagTestResult(1, 0, 'TermStorage::load()');
|
Chris@0
|
44
|
Chris@0
|
45 $this->setupQueryTagTestHooks();
|
Chris@0
|
46 $loaded_terms = $term_storage->loadTree($vocabulary->id());
|
Chris@0
|
47 $this->assertEqual(count($loaded_terms), count($terms), 'All terms were loaded');
|
Chris@0
|
48 $this->assertQueryTagTestResult(1, 1, 'TermStorage::loadTree()');
|
Chris@0
|
49
|
Chris@0
|
50 $this->setupQueryTagTestHooks();
|
Chris@0
|
51 $loaded_terms = $term_storage->loadParents($terms[2]->id());
|
Chris@0
|
52 $this->assertEqual(count($loaded_terms), 1, 'All parent terms were loaded');
|
Chris@0
|
53 $this->assertQueryTagTestResult(2, 1, 'TermStorage::loadParents()');
|
Chris@0
|
54
|
Chris@0
|
55 $this->setupQueryTagTestHooks();
|
Chris@0
|
56 $loaded_terms = $term_storage->loadChildren($terms[1]->id());
|
Chris@0
|
57 $this->assertEqual(count($loaded_terms), 1, 'All child terms were loaded');
|
Chris@0
|
58 $this->assertQueryTagTestResult(2, 1, 'TermStorage::loadChildren()');
|
Chris@0
|
59
|
Chris@0
|
60 $this->setupQueryTagTestHooks();
|
Chris@0
|
61 $query = db_select('taxonomy_term_data', 't');
|
Chris@0
|
62 $query->addField('t', 'tid');
|
Chris@0
|
63 $query->addTag('taxonomy_term_access');
|
Chris@0
|
64 $tids = $query->execute()->fetchCol();
|
Chris@0
|
65 $this->assertEqual(count($tids), count($terms), 'All term IDs were retrieved');
|
Chris@0
|
66 $this->assertQueryTagTestResult(1, 1, 'custom db_select() with taxonomy_term_access tag (preferred)');
|
Chris@0
|
67
|
Chris@0
|
68 $this->setupQueryTagTestHooks();
|
Chris@0
|
69 $query = db_select('taxonomy_term_data', 't');
|
Chris@0
|
70 $query->addField('t', 'tid');
|
Chris@0
|
71 $query->addTag('term_access');
|
Chris@0
|
72 $tids = $query->execute()->fetchCol();
|
Chris@0
|
73 $this->assertEqual(count($tids), count($terms), 'All term IDs were retrieved');
|
Chris@0
|
74 $this->assertQueryTagTestResult(1, 1, 'custom db_select() with term_access tag (deprecated)');
|
Chris@0
|
75
|
Chris@0
|
76 $this->setupQueryTagTestHooks();
|
Chris@0
|
77 $query = \Drupal::entityQuery('taxonomy_term');
|
Chris@0
|
78 $query->addTag('taxonomy_term_access');
|
Chris@0
|
79 $result = $query->execute();
|
Chris@0
|
80 $this->assertEqual(count($result), count($terms), 'All term IDs were retrieved');
|
Chris@0
|
81 $this->assertQueryTagTestResult(1, 1, 'custom EntityFieldQuery with taxonomy_term_access tag (preferred)');
|
Chris@0
|
82
|
Chris@0
|
83 $this->setupQueryTagTestHooks();
|
Chris@0
|
84 $query = \Drupal::entityQuery('taxonomy_term');
|
Chris@0
|
85 $query->addTag('term_access');
|
Chris@0
|
86 $result = $query->execute();
|
Chris@0
|
87 $this->assertEqual(count($result), count($terms), 'All term IDs were retrieved');
|
Chris@0
|
88 $this->assertQueryTagTestResult(1, 1, 'custom EntityFieldQuery with term_access tag (deprecated)');
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 /**
|
Chris@0
|
92 * Sets up the hooks in the test module.
|
Chris@0
|
93 */
|
Chris@0
|
94 protected function setupQueryTagTestHooks() {
|
Chris@0
|
95 taxonomy_terms_static_reset();
|
Chris@0
|
96 \Drupal::state()->set('taxonomy_test_query_alter', 0);
|
Chris@0
|
97 \Drupal::state()->set('taxonomy_test_query_term_access_alter', 0);
|
Chris@0
|
98 \Drupal::state()->set('taxonomy_test_query_taxonomy_term_access_alter', 0);
|
Chris@0
|
99 }
|
Chris@0
|
100
|
Chris@0
|
101 /**
|
Chris@0
|
102 * Verifies invocation of the hooks in the test module.
|
Chris@0
|
103 *
|
Chris@0
|
104 * @param int $expected_generic_invocations
|
Chris@0
|
105 * The number of times the generic query_alter hook is expected to have
|
Chris@0
|
106 * been invoked.
|
Chris@0
|
107 * @param int $expected_specific_invocations
|
Chris@0
|
108 * The number of times the tag-specific query_alter hooks are expected to
|
Chris@0
|
109 * have been invoked.
|
Chris@0
|
110 * @param string $method
|
Chris@0
|
111 * A string describing the invoked function which generated the query.
|
Chris@0
|
112 */
|
Chris@0
|
113 protected function assertQueryTagTestResult($expected_generic_invocations, $expected_specific_invocations, $method) {
|
Chris@0
|
114 $this->assertIdentical($expected_generic_invocations, \Drupal::state()->get('taxonomy_test_query_alter'), 'hook_query_alter() invoked when executing ' . $method);
|
Chris@0
|
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);
|
Chris@0
|
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);
|
Chris@0
|
117 }
|
Chris@0
|
118
|
Chris@0
|
119 }
|