Commit 6874218a authored by catch's avatar catch

Issue #3041030 by claudiu.cristea, Krzysztof Domański, Lendude, Berdir,...

Issue #3041030 by claudiu.cristea, Krzysztof Domański, Lendude, Berdir, jibran: Convert functional EfqTest to a kernel test and move TaxonomyTestTrait from tests/src/Functional to tests/src/Traits
parent 0f73e397
......@@ -6,7 +6,7 @@
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\comment\CommentInterface;
use Drupal\Tests\taxonomy\Functional\TaxonomyTestTrait;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
use Drupal\comment\Entity\Comment;
/**
......
......@@ -3,7 +3,7 @@
namespace Drupal\Tests\system\Functional\Module;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\taxonomy\Functional\TaxonomyTestTrait;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
/**
* Tests that modules which provide entity types can be uninstalled.
......
......@@ -12,7 +12,7 @@
* Provides common helper methods for Taxonomy module tests.
*
* @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0.
* Use \Drupal\Tests\taxonomy\Functional\TaxonomyTestTrait
* Use \Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait
*/
trait TaxonomyTestTrait {
......
......@@ -4,6 +4,7 @@
use Drupal\Core\Database\Database;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
/**
* Tests that appropriate query tags are added.
......
......@@ -4,6 +4,7 @@
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
/**
* Provides common helper methods for Taxonomy module tests.
......
......@@ -2,58 +2,20 @@
namespace Drupal\Tests\taxonomy\Functional;
use Drupal\Core\Language\LanguageInterface;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\taxonomy\Entity\Term;
@trigger_error(__NAMESPACE__ . '\TaxonomyTestTrait trait is deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0. Use \Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait instead. See https://www.drupal.org/node/3041703.', E_USER_DEPRECATED);
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait as ActualTaxonomyTestTrait;
/**
* Provides common helper methods for Taxonomy module tests.
*
* @deprecated in Drupal 8.8.0 and will be removed before Drupal 9.0.0.
* Use \Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait instead.
*
* @see https://www.drupal.org/node/3041703
*/
trait TaxonomyTestTrait {
/**
* Returns a new vocabulary with random properties.
*/
public function createVocabulary() {
// Create a vocabulary.
$vocabulary = Vocabulary::create([
'name' => $this->randomMachineName(),
'description' => $this->randomMachineName(),
'vid' => mb_strtolower($this->randomMachineName()),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
'weight' => mt_rand(0, 10),
]);
$vocabulary->save();
return $vocabulary;
}
/**
* Returns a new term with random properties in vocabulary $vid.
*
* @param \Drupal\taxonomy\Entity\Vocabulary $vocabulary
* The vocabulary object.
* @param array $values
* (optional) An array of values to set, keyed by property name. If the
* entity type has bundles, the bundle key has to be specified.
*
* @return \Drupal\taxonomy\Entity\Term
* The new taxonomy term object.
*/
public function createTerm(Vocabulary $vocabulary, $values = []) {
$filter_formats = filter_formats();
$format = array_pop($filter_formats);
$term = Term::create($values + [
'name' => $this->randomMachineName(),
'description' => [
'value' => $this->randomMachineName(),
// Use the first available text format.
'format' => $format->id(),
],
'vid' => $vocabulary->id(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
$term->save();
return $term;
}
use ActualTaxonomyTestTrait;
}
......@@ -5,7 +5,7 @@
use Drupal\KernelTests\KernelTestBase;
use Drupal\taxonomy\TermInterface;
use Drupal\taxonomy\VocabularyInterface;
use Drupal\Tests\taxonomy\Functional\TaxonomyTestTrait;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
/**
* Tests legacy user functionality.
......
<?php
namespace Drupal\Tests\taxonomy\Functional;
namespace Drupal\Tests\taxonomy\Kernel;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
/**
* Verifies operation of a taxonomy-based Entity Query.
*
* @group taxonomy
*/
class EfqTest extends TaxonomyTestBase {
class TermEntityQueryTest extends KernelTestBase {
use TaxonomyTestTrait;
/**
* Vocabulary for testing.
*
* @var \Drupal\taxonomy\VocabularyInterface
* {@inheritdoc}
*/
protected $vocabulary;
protected function setUp() {
parent::setUp();
$this->drupalLogin($this->drupalCreateUser(['administer taxonomy']));
$this->vocabulary = $this->createVocabulary();
}
protected static $modules = [
'field',
'filter',
'taxonomy',
'text',
'user',
];
/**
* Tests that a basic taxonomy entity query works.
*/
public function testTaxonomyEfq() {
public function testTermEntityQuery() {
$this->installEntitySchema('taxonomy_term');
$vocabulary = $this->createVocabulary();
$terms = [];
for ($i = 0; $i < 5; $i++) {
$term = $this->createTerm($this->vocabulary);
$term = $this->createTerm($vocabulary);
$terms[$term->id()] = $term;
}
$result = \Drupal::entityQuery('taxonomy_term')->execute();
sort($result);
$this->assertEqual(array_keys($terms), $result, 'Taxonomy terms were retrieved by entity query.');
$this->assertEquals(array_keys($terms), $result);
$tid = reset($result);
$ids = (object) [
'entity_type' => 'taxonomy_term',
'entity_id' => $tid,
'bundle' => $this->vocabulary->id(),
'bundle' => $vocabulary->id(),
];
$term = _field_create_entity_from_ids($ids);
$this->assertEqual($term->id(), $tid, 'Taxonomy term can be created based on the IDs.');
$this->assertEquals($tid, $term->id());
// Create a second vocabulary and five more terms.
$vocabulary2 = $this->createVocabulary();
......@@ -55,7 +61,7 @@ public function testTaxonomyEfq() {
->condition('vid', $vocabulary2->id())
->execute();
sort($result);
$this->assertEqual(array_keys($terms2), $result, format_string('Taxonomy terms from the %name vocabulary were retrieved by entity query.', ['%name' => $vocabulary2->label()]));
$this->assertEqual(array_keys($terms2), $result);
$tid = reset($result);
$ids = (object) [
'entity_type' => 'taxonomy_term',
......@@ -63,7 +69,7 @@ public function testTaxonomyEfq() {
'bundle' => $vocabulary2->id(),
];
$term = _field_create_entity_from_ids($ids);
$this->assertEqual($term->id(), $tid, 'Taxonomy term can be created based on the IDs.');
$this->assertEquals($tid, $term->id());
}
}
......@@ -4,7 +4,7 @@
use Drupal\taxonomy\Entity\Term;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\taxonomy\Functional\TaxonomyTestTrait;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
/**
* Kernel tests for taxonomy term functions.
......
......@@ -4,7 +4,7 @@
use Drupal\Core\Link;
use Drupal\Core\Render\RenderContext;
use Drupal\Tests\taxonomy\Functional\TaxonomyTestTrait;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\views\Tests\ViewTestData;
use Drupal\views\Views;
......
......@@ -3,7 +3,7 @@
namespace Drupal\Tests\taxonomy\Kernel\Views;
use Drupal\Core\Render\RenderContext;
use Drupal\Tests\taxonomy\Functional\TaxonomyTestTrait;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\user\Entity\User;
use Drupal\views\Tests\ViewTestData;
......
<?php
namespace Drupal\Tests\taxonomy\Traits;
use Drupal\Core\Language\LanguageInterface;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\VocabularyInterface;
/**
* Provides common helper methods for Taxonomy module tests.
*/
trait TaxonomyTestTrait {
/**
* Returns a new vocabulary with random properties.
*
* @return \Drupal\taxonomy\VocabularyInterface
* A vocabulary used for testing.
*/
public function createVocabulary() {
$vocabulary = Vocabulary::create([
'name' => $this->randomMachineName(),
'description' => $this->randomMachineName(),
'vid' => mb_strtolower($this->randomMachineName()),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
'weight' => mt_rand(0, 10),
]);
$vocabulary->save();
return $vocabulary;
}
/**
* Returns a new term with random properties given a vocabulary.
*
* @param \Drupal\taxonomy\VocabularyInterface $vocabulary
* The vocabulary object.
* @param array $values
* (optional) An array of values to set, keyed by property name.
*
* @return \Drupal\taxonomy\TermInterface
* The new taxonomy term object.
*/
public function createTerm(VocabularyInterface $vocabulary, $values = []) {
$term = Term::create($values + [
'name' => $this->randomMachineName(),
'description' => [
'value' => $this->randomMachineName(),
// Use the fallback text format.
'format' => filter_fallback_format(),
],
'vid' => $vocabulary->id(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
$term->save();
return $term;
}
}
......@@ -2,7 +2,7 @@
namespace Drupal\Tests\views\Functional;
use Drupal\Tests\taxonomy\Functional\TaxonomyTestTrait;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
/**
* Tests glossary functionality of taxonomy views.
......
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