@@ -1983,3 +1983,113 @@ class TaxonomyEFQTestCase extends TaxonomyWebTestCase {
19831983 }
19841984
19851985}
1986+
1987+ /**
1988+ * Tests that appropriate query tags are added.
1989+ */
1990+ class TaxonomyQueryAlterTestCase extends TaxonomyWebTestCase {
1991+ public static function getInfo() {
1992+ return array(
1993+ 'name' => 'Taxonomy query tags',
1994+ 'description' => 'Verifies that taxonomy_term_access tags are added to queries.',
1995+ 'group' => 'Taxonomy',
1996+ );
1997+ }
1998+
1999+ public function setUp() {
2000+ parent::setUp('taxonomy_test');
2001+ }
2002+
2003+ /**
2004+ * Tests that appropriate tags are added when querying the database.
2005+ */
2006+ public function testTaxonomyQueryAlter() {
2007+ // Create a new vocabulary and add a few terms to it.
2008+ $vocabulary = $this->createVocabulary();
2009+ $terms = array();
2010+ for ($i = 0; $i < 5; $i++) {
2011+ $terms[$i] = $this->createTerm($vocabulary);
2012+ }
2013+
2014+ // Set up hierarchy. Term 2 is a child of 1.
2015+ $terms[2]->parent = array($terms[1]->tid);
2016+ taxonomy_term_save($terms[2]);
2017+
2018+ $this->setupQueryTagTestHooks();
2019+ $loaded_term = taxonomy_term_load($terms[0]->tid);
2020+ $this->assertEqual($loaded_term->tid, $terms[0]->tid, 'First term was loaded');
2021+ $this->assertQueryTagTestResult(1, 'taxonomy_term_load()');
2022+
2023+ $this->setupQueryTagTestHooks();
2024+ $loaded_terms = taxonomy_get_tree($vocabulary->vid);
2025+ $this->assertEqual(count($loaded_terms), count($terms), 'All terms were loaded');
2026+ $this->assertQueryTagTestResult(1, 'taxonomy_get_tree()');
2027+
2028+ $this->setupQueryTagTestHooks();
2029+ $loaded_terms = taxonomy_get_parents($terms[2]->tid);
2030+ $this->assertEqual(count($loaded_terms), 1, 'All parent terms were loaded');
2031+ $this->assertQueryTagTestResult(2, 'taxonomy_get_parents()');
2032+
2033+ $this->setupQueryTagTestHooks();
2034+ $loaded_terms = taxonomy_get_children($terms[1]->tid);
2035+ $this->assertEqual(count($loaded_terms), 1, 'All child terms were loaded');
2036+ $this->assertQueryTagTestResult(2, 'taxonomy_get_children()');
2037+
2038+ $this->setupQueryTagTestHooks();
2039+ $query = db_select('taxonomy_term_data', 't');
2040+ $query->addField('t', 'tid');
2041+ $query->addTag('taxonomy_term_access');
2042+ $tids = $query->execute()->fetchCol();
2043+ $this->assertEqual(count($tids), count($terms), 'All term IDs were retrieved');
2044+ $this->assertQueryTagTestResult(1, 'custom db_select() with taxonomy_term_access tag (preferred)');
2045+
2046+ $this->setupQueryTagTestHooks();
2047+ $query = db_select('taxonomy_term_data', 't');
2048+ $query->addField('t', 'tid');
2049+ $query->addTag('term_access');
2050+ $tids = $query->execute()->fetchCol();
2051+ $this->assertEqual(count($tids), count($terms), 'All term IDs were retrieved');
2052+ $this->assertQueryTagTestResult(1, 'custom db_select() with term_access tag (deprecated)');
2053+
2054+ $this->setupQueryTagTestHooks();
2055+ $query = new EntityFieldQuery();
2056+ $query->entityCondition('entity_type', 'taxonomy_term');
2057+ $query->addTag('taxonomy_term_access');
2058+ $result = $query->execute();
2059+ $this->assertEqual(count($result['taxonomy_term']), count($terms), 'All term IDs were retrieved');
2060+ $this->assertQueryTagTestResult(1, 'custom EntityFieldQuery with taxonomy_term_access tag (preferred)');
2061+
2062+ $this->setupQueryTagTestHooks();
2063+ $query = new EntityFieldQuery();
2064+ $query->entityCondition('entity_type', 'taxonomy_term');
2065+ $query->addTag('term_access');
2066+ $result = $query->execute();
2067+ $this->assertEqual(count($result['taxonomy_term']), count($terms), 'All term IDs were retrieved');
2068+ $this->assertQueryTagTestResult(1, 'custom EntityFieldQuery with term_access tag (deprecated)');
2069+ }
2070+
2071+ /**
2072+ * Sets up the hooks in the test module.
2073+ */
2074+ protected function setupQueryTagTestHooks() {
2075+ taxonomy_terms_static_reset();
2076+ variable_set('taxonomy_test_query_alter', 0);
2077+ variable_set('taxonomy_test_query_term_access_alter', 0);
2078+ variable_set('taxonomy_test_query_taxonomy_term_access_alter', 0);
2079+ }
2080+
2081+ /**
2082+ * Verifies invocation of the hooks in the test module.
2083+ *
2084+ * @param int $expected_invocations
2085+ * The number of times the hooks are expected to have been invoked.
2086+ * @param string $method
2087+ * A string describing the invoked function which generated the query.
2088+ */
2089+ protected function assertQueryTagTestResult($expected_invocations, $method) {
2090+ $this->assertIdentical($expected_invocations, variable_get('taxonomy_test_query_alter'), 'hook_query_alter() invoked when executing ' . $method);
2091+ $this->assertIdentical($expected_invocations, variable_get('taxonomy_test_query_term_access_alter'), 'Deprecated hook_query_term_access_alter() invoked when executing ' . $method);
2092+ $this->assertIdentical($expected_invocations, variable_get('taxonomy_test_query_taxonomy_term_access_alter'), 'Preferred hook_query_taxonomy_term_access_alter() invoked when executing ' . $method);
2093+ }
2094+
2095+ }
0 commit comments