Backup of db before drupal security update
[yaffs-website] / web / core / modules / taxonomy / src / Tests / TaxonomyQueryAlterTest.php
1 <?php
2
3 namespace Drupal\taxonomy\Tests;
4
5 use Drupal\simpletest\WebTestBase;
6
7 /**
8  * Tests that appropriate query tags are added.
9  *
10  * @group taxonomy
11  */
12 class TaxonomyQueryAlterTest extends WebTestBase {
13
14   use TaxonomyTestTrait;
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = ['taxonomy', 'taxonomy_test'];
22
23   /**
24    * Tests that appropriate tags are added when querying the database.
25    */
26   public function testTaxonomyQueryAlter() {
27     // Create a new vocabulary and add a few terms to it.
28     $vocabulary = $this->createVocabulary();
29     $terms = [];
30     for ($i = 0; $i < 5; $i++) {
31       $terms[$i] = $this->createTerm($vocabulary);
32     }
33
34     // Set up hierarchy. Term 2 is a child of 1.
35     $terms[2]->parent = $terms[1]->id();
36     $terms[2]->save();
37
38     $term_storage = \Drupal::entityManager()->getStorage('taxonomy_term');
39
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()');
44
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()');
49
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()');
54
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()');
59
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)');
67
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)');
75
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)');
82
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)');
89   }
90
91   /**
92    * Sets up the hooks in the test module.
93    */
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);
99   }
100
101   /**
102    * Verifies invocation of the hooks in the test module.
103    *
104    * @param int $expected_generic_invocations
105    *   The number of times the generic query_alter hook is expected to have
106    *   been invoked.
107    * @param int $expected_specific_invocations
108    *   The number of times the tag-specific query_alter hooks are expected to
109    *   have been invoked.
110    * @param string $method
111    *   A string describing the invoked function which generated the query.
112    */
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);
117   }
118
119 }