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