Chris@0: createVocabulary(); Chris@0: $terms = []; Chris@0: for ($i = 0; $i < 5; $i++) { Chris@0: $terms[$i] = $this->createTerm($vocabulary); Chris@0: } Chris@0: Chris@0: // Set up hierarchy. Term 2 is a child of 1. Chris@0: $terms[2]->parent = $terms[1]->id(); Chris@0: $terms[2]->save(); Chris@0: Chris@0: $term_storage = \Drupal::entityManager()->getStorage('taxonomy_term'); Chris@0: Chris@0: $this->setupQueryTagTestHooks(); Chris@0: $loaded_term = $term_storage->load($terms[0]->id()); Chris@0: $this->assertEqual($loaded_term->id(), $terms[0]->id(), 'First term was loaded'); Chris@0: $this->assertQueryTagTestResult(1, 0, 'TermStorage::load()'); Chris@0: Chris@0: $this->setupQueryTagTestHooks(); Chris@0: $loaded_terms = $term_storage->loadTree($vocabulary->id()); Chris@0: $this->assertEqual(count($loaded_terms), count($terms), 'All terms were loaded'); Chris@0: $this->assertQueryTagTestResult(1, 1, 'TermStorage::loadTree()'); Chris@0: Chris@0: $this->setupQueryTagTestHooks(); Chris@0: $loaded_terms = $term_storage->loadParents($terms[2]->id()); Chris@0: $this->assertEqual(count($loaded_terms), 1, 'All parent terms were loaded'); Chris@17: $this->assertQueryTagTestResult(3, 1, 'TermStorage::loadParents()'); Chris@0: Chris@0: $this->setupQueryTagTestHooks(); Chris@0: $loaded_terms = $term_storage->loadChildren($terms[1]->id()); Chris@0: $this->assertEqual(count($loaded_terms), 1, 'All child terms were loaded'); Chris@17: $this->assertQueryTagTestResult(3, 1, 'TermStorage::loadChildren()'); Chris@0: Chris@0: $this->setupQueryTagTestHooks(); Chris@18: $connection = Database::getConnection(); Chris@18: $query = $connection->select('taxonomy_term_data', 't'); Chris@0: $query->addField('t', 'tid'); Chris@0: $query->addTag('taxonomy_term_access'); Chris@0: $tids = $query->execute()->fetchCol(); Chris@0: $this->assertEqual(count($tids), count($terms), 'All term IDs were retrieved'); Chris@0: $this->assertQueryTagTestResult(1, 1, 'custom db_select() with taxonomy_term_access tag (preferred)'); Chris@0: Chris@0: $this->setupQueryTagTestHooks(); Chris@18: $query = $connection->select('taxonomy_term_data', 't'); Chris@0: $query->addField('t', 'tid'); Chris@0: $query->addTag('term_access'); Chris@0: $tids = $query->execute()->fetchCol(); Chris@0: $this->assertEqual(count($tids), count($terms), 'All term IDs were retrieved'); Chris@0: $this->assertQueryTagTestResult(1, 1, 'custom db_select() with term_access tag (deprecated)'); Chris@0: Chris@0: $this->setupQueryTagTestHooks(); Chris@0: $query = \Drupal::entityQuery('taxonomy_term'); Chris@0: $query->addTag('taxonomy_term_access'); Chris@0: $result = $query->execute(); Chris@0: $this->assertEqual(count($result), count($terms), 'All term IDs were retrieved'); Chris@0: $this->assertQueryTagTestResult(1, 1, 'custom EntityFieldQuery with taxonomy_term_access tag (preferred)'); Chris@0: Chris@0: $this->setupQueryTagTestHooks(); Chris@0: $query = \Drupal::entityQuery('taxonomy_term'); Chris@0: $query->addTag('term_access'); Chris@0: $result = $query->execute(); Chris@0: $this->assertEqual(count($result), count($terms), 'All term IDs were retrieved'); Chris@0: $this->assertQueryTagTestResult(1, 1, 'custom EntityFieldQuery with term_access tag (deprecated)'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets up the hooks in the test module. Chris@0: */ Chris@0: protected function setupQueryTagTestHooks() { Chris@0: taxonomy_terms_static_reset(); Chris@0: \Drupal::state()->set('taxonomy_test_query_alter', 0); Chris@0: \Drupal::state()->set('taxonomy_test_query_term_access_alter', 0); Chris@0: \Drupal::state()->set('taxonomy_test_query_taxonomy_term_access_alter', 0); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Verifies invocation of the hooks in the test module. Chris@0: * Chris@0: * @param int $expected_generic_invocations Chris@0: * The number of times the generic query_alter hook is expected to have Chris@0: * been invoked. Chris@0: * @param int $expected_specific_invocations Chris@0: * The number of times the tag-specific query_alter hooks are expected to Chris@0: * have been invoked. Chris@0: * @param string $method Chris@0: * A string describing the invoked function which generated the query. Chris@0: */ Chris@0: protected function assertQueryTagTestResult($expected_generic_invocations, $expected_specific_invocations, $method) { Chris@0: $this->assertIdentical($expected_generic_invocations, \Drupal::state()->get('taxonomy_test_query_alter'), 'hook_query_alter() invoked when executing ' . $method); Chris@0: $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: $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: } Chris@0: Chris@0: }