Commit 987c9896 authored by catch's avatar catch

Issue #3041055 by claudiu.cristea, Manuel Garcia, jibran: Convert...

Issue #3041055 by claudiu.cristea, Manuel Garcia, jibran: Convert TaxonomyQueryAlterTest to a kernel test
parent 83bee34c
...@@ -6,8 +6,8 @@ ...@@ -6,8 +6,8 @@
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Language\LanguageInterface;
use Drupal\comment\CommentInterface; use Drupal\comment\CommentInterface;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
use Drupal\comment\Entity\Comment; use Drupal\comment\Entity\Comment;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
/** /**
* Tests comments with other entities. * Tests comments with other entities.
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
*/ */
class CommentEntityTest extends CommentTestBase { class CommentEntityTest extends CommentTestBase {
use TaxonomyTestTrait;
/** /**
* Modules to install. * Modules to install.
* *
...@@ -23,8 +25,6 @@ class CommentEntityTest extends CommentTestBase { ...@@ -23,8 +25,6 @@ class CommentEntityTest extends CommentTestBase {
*/ */
public static $modules = ['block', 'comment', 'node', 'history', 'field_ui', 'datetime', 'taxonomy']; public static $modules = ['block', 'comment', 'node', 'history', 'field_ui', 'datetime', 'taxonomy'];
use TaxonomyTestTrait;
protected $vocab; protected $vocab;
protected $commentType; protected $commentType;
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
use Drupal\simpletest\WebTestBase; use Drupal\simpletest\WebTestBase;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait; use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
use Drupal\Tests\taxonomy\Functional\TaxonomyTestTrait; use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
/** /**
* Provides common helper methods for Taxonomy module tests. * Provides common helper methods for Taxonomy module tests.
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
namespace Drupal\taxonomy\Tests; namespace Drupal\taxonomy\Tests;
@trigger_error(__NAMESPACE__ . '\TaxonomyTestTrait is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Instead, use \Drupal\Tests\taxonomy\Functional\TaxonomyTestTrait', E_USER_DEPRECATED); @trigger_error(__NAMESPACE__ . '\TaxonomyTestTrait is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Instead, use \Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait instead', E_USER_DEPRECATED);
use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Language\LanguageInterface;
use Drupal\taxonomy\Entity\Vocabulary; use Drupal\taxonomy\Entity\Vocabulary;
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
* Provides common helper methods for Taxonomy module tests. * Provides common helper methods for Taxonomy module tests.
* *
* @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. * @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0.
* Use \Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait * Use \Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait instead.
*/ */
trait TaxonomyTestTrait { trait TaxonomyTestTrait {
......
<?php <?php
namespace Drupal\Tests\taxonomy\Functional; namespace Drupal\Tests\taxonomy\Kernel;
use Drupal\Core\Database\Database; use Drupal\Core\Database\Database;
use Drupal\Tests\BrowserTestBase; use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait; use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
/** /**
...@@ -11,21 +11,27 @@ ...@@ -11,21 +11,27 @@
* *
* @group taxonomy * @group taxonomy
*/ */
class TaxonomyQueryAlterTest extends BrowserTestBase { class TaxonomyQueryAlterTest extends KernelTestBase {
use TaxonomyTestTrait; use TaxonomyTestTrait;
/** /**
* Modules to enable. * {@inheritdoc}
*
* @var array
*/ */
public static $modules = ['taxonomy', 'taxonomy_test']; protected static $modules = [
'filter',
'taxonomy',
'taxonomy_test',
'text',
'user',
];
/** /**
* Tests that appropriate tags are added when querying the database. * Tests that appropriate tags are added when querying the database.
*/ */
public function testTaxonomyQueryAlter() { public function testTaxonomyQueryAlter() {
$this->installEntitySchema('taxonomy_term');
// Create a new vocabulary and add a few terms to it. // Create a new vocabulary and add a few terms to it.
$vocabulary = $this->createVocabulary(); $vocabulary = $this->createVocabulary();
$terms = []; $terms = [];
...@@ -37,27 +43,36 @@ public function testTaxonomyQueryAlter() { ...@@ -37,27 +43,36 @@ public function testTaxonomyQueryAlter() {
$terms[2]->parent = $terms[1]->id(); $terms[2]->parent = $terms[1]->id();
$terms[2]->save(); $terms[2]->save();
$term_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term'); /** @var \Drupal\taxonomy\TermStorageInterface $term_storage */
$term_storage = $this->container->get('entity_type.manager')->getStorage('taxonomy_term');
$this->setupQueryTagTestHooks(); $this->setupQueryTagTestHooks();
$loaded_term = $term_storage->load($terms[0]->id()); $loaded_term = $term_storage->load($terms[0]->id());
$this->assertEqual($loaded_term->id(), $terms[0]->id(), 'First term was loaded'); // First term was loaded.
$this->assertQueryTagTestResult(1, 0, 'TermStorage::load()'); $this->assertEquals($terms[0]->id(), $loaded_term->id());
// TermStorage::load().
$this->assertQueryTagTestResult(1, 0);
$this->setupQueryTagTestHooks(); $this->setupQueryTagTestHooks();
$loaded_terms = $term_storage->loadTree($vocabulary->id()); $loaded_terms = $term_storage->loadTree($vocabulary->id());
$this->assertEqual(count($loaded_terms), count($terms), 'All terms were loaded'); // All terms were loaded.
$this->assertQueryTagTestResult(1, 1, 'TermStorage::loadTree()'); $this->assertCount(5, $loaded_terms);
// TermStorage::loadTree().
$this->assertQueryTagTestResult(1, 1);
$this->setupQueryTagTestHooks(); $this->setupQueryTagTestHooks();
$loaded_terms = $term_storage->loadParents($terms[2]->id()); $loaded_terms = $term_storage->loadParents($terms[2]->id());
$this->assertEqual(count($loaded_terms), 1, 'All parent terms were loaded'); // All parent terms were loaded.
$this->assertQueryTagTestResult(3, 1, 'TermStorage::loadParents()'); $this->assertCount(1, $loaded_terms);
// TermStorage::loadParents().
$this->assertQueryTagTestResult(3, 1);
$this->setupQueryTagTestHooks(); $this->setupQueryTagTestHooks();
$loaded_terms = $term_storage->loadChildren($terms[1]->id()); $loaded_terms = $term_storage->loadChildren($terms[1]->id());
$this->assertEqual(count($loaded_terms), 1, 'All child terms were loaded'); // All child terms were loaded.
$this->assertQueryTagTestResult(3, 1, 'TermStorage::loadChildren()'); $this->assertCount(1, $loaded_terms);
// TermStorage::loadChildren().
$this->assertQueryTagTestResult(3, 1);
$this->setupQueryTagTestHooks(); $this->setupQueryTagTestHooks();
$connection = Database::getConnection(); $connection = Database::getConnection();
...@@ -65,30 +80,38 @@ public function testTaxonomyQueryAlter() { ...@@ -65,30 +80,38 @@ public function testTaxonomyQueryAlter() {
$query->addField('t', 'tid'); $query->addField('t', 'tid');
$query->addTag('taxonomy_term_access'); $query->addTag('taxonomy_term_access');
$tids = $query->execute()->fetchCol(); $tids = $query->execute()->fetchCol();
$this->assertEqual(count($tids), count($terms), 'All term IDs were retrieved'); // All term IDs were retrieved.
$this->assertQueryTagTestResult(1, 1, 'custom db_select() with taxonomy_term_access tag (preferred)'); $this->assertCount(5, $tids);
// Database custom ::select() with 'taxonomy_term_access' tag (preferred).
$this->assertQueryTagTestResult(1, 1);
$this->setupQueryTagTestHooks(); $this->setupQueryTagTestHooks();
$query = $connection->select('taxonomy_term_data', 't'); $query = $connection->select('taxonomy_term_data', 't');
$query->addField('t', 'tid'); $query->addField('t', 'tid');
$query->addTag('term_access'); $query->addTag('term_access');
$tids = $query->execute()->fetchCol(); $tids = $query->execute()->fetchCol();
$this->assertEqual(count($tids), count($terms), 'All term IDs were retrieved'); // All term IDs were retrieved.
$this->assertQueryTagTestResult(1, 1, 'custom db_select() with term_access tag (deprecated)'); $this->assertCount(5, $tids);
// Database custom ::select() with term_access tag (deprecated).
$this->assertQueryTagTestResult(1, 1);
$this->setupQueryTagTestHooks(); $this->setupQueryTagTestHooks();
$query = \Drupal::entityQuery('taxonomy_term'); $query = \Drupal::entityQuery('taxonomy_term');
$query->addTag('taxonomy_term_access'); $query->addTag('taxonomy_term_access');
$result = $query->execute(); $result = $query->execute();
$this->assertEqual(count($result), count($terms), 'All term IDs were retrieved'); // All term IDs were retrieved.
$this->assertQueryTagTestResult(1, 1, 'custom EntityFieldQuery with taxonomy_term_access tag (preferred)'); $this->assertCount(5, $result);
// Custom entity query with taxonomy_term_access tag (preferred).
$this->assertQueryTagTestResult(1, 1);
$this->setupQueryTagTestHooks(); $this->setupQueryTagTestHooks();
$query = \Drupal::entityQuery('taxonomy_term'); $query = \Drupal::entityQuery('taxonomy_term');
$query->addTag('term_access'); $query->addTag('term_access');
$result = $query->execute(); $result = $query->execute();
$this->assertEqual(count($result), count($terms), 'All term IDs were retrieved'); // All term IDs were retrieved.
$this->assertQueryTagTestResult(1, 1, 'custom EntityFieldQuery with term_access tag (deprecated)'); $this->assertCount(5, $result);
// Custom entity query with taxonomy_term_access tag (preferred).
$this->assertQueryTagTestResult(1, 1);
} }
/** /**
...@@ -96,9 +119,10 @@ public function testTaxonomyQueryAlter() { ...@@ -96,9 +119,10 @@ public function testTaxonomyQueryAlter() {
*/ */
protected function setupQueryTagTestHooks() { protected function setupQueryTagTestHooks() {
taxonomy_terms_static_reset(); taxonomy_terms_static_reset();
\Drupal::state()->set('taxonomy_test_query_alter', 0); $state = $this->container->get('state');
\Drupal::state()->set('taxonomy_test_query_term_access_alter', 0); $state->set('taxonomy_test_query_alter', 0);
\Drupal::state()->set('taxonomy_test_query_taxonomy_term_access_alter', 0); $state->set('taxonomy_test_query_term_access_alter', 0);
$state->set('taxonomy_test_query_taxonomy_term_access_alter', 0);
} }
/** /**
...@@ -110,13 +134,12 @@ protected function setupQueryTagTestHooks() { ...@@ -110,13 +134,12 @@ protected function setupQueryTagTestHooks() {
* @param int $expected_specific_invocations * @param int $expected_specific_invocations
* The number of times the tag-specific query_alter hooks are expected to * The number of times the tag-specific query_alter hooks are expected to
* have been invoked. * have been invoked.
* @param string $method
* A string describing the invoked function which generated the query.
*/ */
protected function assertQueryTagTestResult($expected_generic_invocations, $expected_specific_invocations, $method) { protected function assertQueryTagTestResult($expected_generic_invocations, $expected_specific_invocations) {
$this->assertIdentical($expected_generic_invocations, \Drupal::state()->get('taxonomy_test_query_alter'), 'hook_query_alter() invoked when executing ' . $method); $state = $this->container->get('state');
$this->assertIdentical($expected_specific_invocations, \Drupal::state()->get('taxonomy_test_query_term_access_alter'), 'Deprecated hook_query_term_access_alter() invoked when executing ' . $method); $this->assertEquals($expected_generic_invocations, $state->get('taxonomy_test_query_alter'));
$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); $this->assertEquals($expected_specific_invocations, $state->get('taxonomy_test_query_term_access_alter'));
$this->assertEquals($expected_specific_invocations, $state->get('taxonomy_test_query_taxonomy_term_access_alter'));
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment