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