8dcdf170073f9afbde27337c22fa7d80fdbd0931
[yaffs-website] / web / core / modules / taxonomy / tests / src / Functional / TaxonomyTestTrait.php
1 <?php
2
3 namespace Drupal\Tests\taxonomy\Functional;
4
5 use Drupal\Core\Language\LanguageInterface;
6 use Drupal\taxonomy\Entity\Vocabulary;
7 use Drupal\taxonomy\Entity\Term;
8
9 /**
10  * Provides common helper methods for Taxonomy module tests.
11  */
12 trait TaxonomyTestTrait {
13
14   /**
15    * Returns a new vocabulary with random properties.
16    */
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),
25     ]);
26     $vocabulary->save();
27     return $vocabulary;
28   }
29
30   /**
31    * Returns a new term with random properties in vocabulary $vid.
32    *
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.
38    *
39    * @return \Drupal\taxonomy\Entity\Term
40    *   The new taxonomy term object.
41    */
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(),
47       'description' => [
48         'value' => $this->randomMachineName(),
49         // Use the first available text format.
50         'format' => $format->id(),
51       ],
52       'vid' => $vocabulary->id(),
53       'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
54     ]);
55     $term->save();
56     return $term;
57   }
58
59 }