3 namespace Drupal\Tests\taxonomy\Functional;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Language\LanguageInterface;
7 use Drupal\taxonomy\Entity\Vocabulary;
8 use Drupal\taxonomy\Entity\Term;
11 * Provides common helper methods for Taxonomy module tests.
13 trait TaxonomyTestTrait {
16 * Returns a new vocabulary with random properties.
18 public function createVocabulary() {
19 // Create a vocabulary.
20 $vocabulary = Vocabulary::create([
21 'name' => $this->randomMachineName(),
22 'description' => $this->randomMachineName(),
23 'vid' => Unicode::strtolower($this->randomMachineName()),
24 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
25 'weight' => mt_rand(0, 10),
32 * Returns a new term with random properties in vocabulary $vid.
34 * @param \Drupal\taxonomy\Entity\Vocabulary $vocabulary
35 * The vocabulary object.
36 * @param array $values
37 * (optional) An array of values to set, keyed by property name. If the
38 * entity type has bundles, the bundle key has to be specified.
40 * @return \Drupal\taxonomy\Entity\Term
41 * The new taxonomy term object.
43 public function createTerm(Vocabulary $vocabulary, $values = []) {
44 $filter_formats = filter_formats();
45 $format = array_pop($filter_formats);
46 $term = Term::create($values + [
47 'name' => $this->randomMachineName(),
49 'value' => $this->randomMachineName(),
50 // Use the first available text format.
51 'format' => $format->id(),
53 'vid' => $vocabulary->id(),
54 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,