3 namespace Drupal\taxonomy\Tests;
5 @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);
7 use Drupal\Component\Utility\Unicode;
8 use Drupal\Core\Language\LanguageInterface;
9 use Drupal\taxonomy\Entity\Vocabulary;
10 use Drupal\taxonomy\Entity\Term;
13 * Provides common helper methods for Taxonomy module tests.
15 * @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0.
16 * Use \Drupal\Tests\taxonomy\Functional\TaxonomyTestTrait
18 trait TaxonomyTestTrait {
21 * Returns a new vocabulary with random properties.
23 public function createVocabulary() {
24 // Create a vocabulary.
25 $vocabulary = Vocabulary::create([
26 'name' => $this->randomMachineName(),
27 'description' => $this->randomMachineName(),
28 'vid' => Unicode::strtolower($this->randomMachineName()),
29 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
30 'weight' => mt_rand(0, 10),
37 * Returns a new term with random properties in vocabulary $vid.
39 * @param \Drupal\taxonomy\Entity\Vocabulary $vocabulary
40 * The vocabulary object.
41 * @param array $values
42 * (optional) An array of values to set, keyed by property name. If the
43 * entity type has bundles, the bundle key has to be specified.
45 * @return \Drupal\taxonomy\Entity\Term
46 * The new taxonomy term object.
48 public function createTerm(Vocabulary $vocabulary, $values = []) {
49 $filter_formats = filter_formats();
50 $format = array_pop($filter_formats);
51 $term = Term::create($values + [
52 'name' => $this->randomMachineName(),
54 'value' => $this->randomMachineName(),
55 // Use the first available text format.
56 'format' => $format->id(),
58 'vid' => $vocabulary->id(),
59 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,