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