Issue #2431477 by martin107: make TermUnitTest a KernelTestBase by moving createTerm and createVocabulary to a trait

8.0.x
Alex Pott 2015-03-01 12:49:07 +00:00
parent a120969efe
commit a0e9ed19f3
3 changed files with 94 additions and 54 deletions

View File

@ -7,16 +7,16 @@
namespace Drupal\taxonomy\Tests;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Language\LanguageInterface;
use Drupal\simpletest\WebTestBase;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\taxonomy\Tests\TaxonomyTestTrait;
/**
* Provides common helper methods for Taxonomy module tests.
*/
abstract class TaxonomyTestBase extends WebTestBase {
use TaxonomyTestTrait;
/**
* Modules to enable.
*
@ -36,49 +36,4 @@ abstract class TaxonomyTestBase extends WebTestBase {
$this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article'));
}
}
/**
* Returns a new vocabulary with random properties.
*/
function createVocabulary() {
// Create a vocabulary.
$vocabulary = entity_create('taxonomy_vocabulary', array(
'name' => $this->randomMachineName(),
'description' => $this->randomMachineName(),
'vid' => Unicode::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.
*/
function createTerm(Vocabulary $vocabulary, $values = array()) {
$filter_formats = filter_formats();
$format = array_pop($filter_formats);
$term = entity_create('taxonomy_term', $values + array(
'name' => $this->randomMachineName(),
'description' => array(
'value' => $this->randomMachineName(),
// Use the first available text format.
'format' => $format->id(),
),
'vid' => $vocabulary->id(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
));
$term->save();
return $term;
}
}

View File

@ -0,0 +1,63 @@
<?php
/**
* @file
* Contains \Drupal\taxonomy\Tests\TaxonomyTestTrait.
*/
namespace Drupal\taxonomy\Tests;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Language\LanguageInterface;
use Drupal\taxonomy\Entity\Vocabulary;
/**
* Provides common helper methods for Taxonomy module tests.
*/
trait TaxonomyTestTrait {
/**
* Returns a new vocabulary with random properties.
*/
function createVocabulary() {
// Create a vocabulary.
$vocabulary = entity_create('taxonomy_vocabulary', array(
'name' => $this->randomMachineName(),
'description' => $this->randomMachineName(),
'vid' => Unicode::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.
*/
function createTerm(Vocabulary $vocabulary, $values = array()) {
$filter_formats = filter_formats();
$format = array_pop($filter_formats);
$term = entity_create('taxonomy_term', $values + array(
'name' => $this->randomMachineName(),
'description' => array(
'value' => $this->randomMachineName(),
// Use the first available text format.
'format' => $format->id(),
),
'vid' => $vocabulary->id(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
));
$term->save();
return $term;
}
}

View File

@ -2,21 +2,43 @@
/**
* @file
* Definition of Drupal\taxonomy\Tests\TermUnitTest.
* Contains \Drupal\taxonomy\Tests\TermKernelTest.
*/
namespace Drupal\taxonomy\Tests;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Tests\TaxonomyTestTrait;
use Drupal\simpletest\KernelTestBase;
/**
* Unit tests for taxonomy term functions.
* Kernel tests for taxonomy term functions.
*
* @group taxonomy
*/
class TermUnitTest extends TaxonomyTestBase {
class TermKernelTest extends KernelTestBase {
function testTermDelete() {
use TaxonomyTestTrait;
/**
* {@inheritdoc}
*/
public static $modules = array( 'filter', 'taxonomy', 'taxonomy_term', 'text', 'user' );
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installConfig(array('filter'));
$this->installEntitySchema('taxonomy_term');
}
/**
* Deleting terms should also remove related vocabulary.
* Deleting an invalid term should silently fail.
*/
public function testTermDelete() {
$vocabulary = $this->createVocabulary();
$valid_term = $this->createTerm($vocabulary);
// Delete a valid term.
@ -31,7 +53,7 @@ class TermUnitTest extends TaxonomyTestBase {
/**
* Deleting a parent of a term with multiple parents does not delete the term.
*/
function testMultipleParentDelete() {
public function testMultipleParentDelete() {
$vocabulary = $this->createVocabulary();
$parent_term1 = $this->createTerm($vocabulary);
$parent_term2 = $this->createTerm($vocabulary);
@ -55,7 +77,7 @@ class TermUnitTest extends TaxonomyTestBase {
/**
* Test a taxonomy with terms that have multiple parents of different depths.
*/
function testTaxonomyVocabularyTree() {
public function testTaxonomyVocabularyTree() {
// Create a new vocabulary with 6 terms.
$vocabulary = $this->createVocabulary();
$term = array();