annotate core/modules/taxonomy/tests/src/Functional/TaxonomyTestTrait.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\taxonomy\Functional;
Chris@0 4
Chris@0 5 use Drupal\Core\Language\LanguageInterface;
Chris@0 6 use Drupal\taxonomy\Entity\Vocabulary;
Chris@0 7 use Drupal\taxonomy\Entity\Term;
Chris@0 8
Chris@0 9 /**
Chris@0 10 * Provides common helper methods for Taxonomy module tests.
Chris@0 11 */
Chris@0 12 trait TaxonomyTestTrait {
Chris@0 13
Chris@0 14 /**
Chris@0 15 * Returns a new vocabulary with random properties.
Chris@0 16 */
Chris@0 17 public function createVocabulary() {
Chris@0 18 // Create a vocabulary.
Chris@0 19 $vocabulary = Vocabulary::create([
Chris@0 20 'name' => $this->randomMachineName(),
Chris@0 21 'description' => $this->randomMachineName(),
Chris@17 22 'vid' => mb_strtolower($this->randomMachineName()),
Chris@0 23 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
Chris@0 24 'weight' => mt_rand(0, 10),
Chris@0 25 ]);
Chris@0 26 $vocabulary->save();
Chris@0 27 return $vocabulary;
Chris@0 28 }
Chris@0 29
Chris@0 30 /**
Chris@0 31 * Returns a new term with random properties in vocabulary $vid.
Chris@0 32 *
Chris@0 33 * @param \Drupal\taxonomy\Entity\Vocabulary $vocabulary
Chris@0 34 * The vocabulary object.
Chris@0 35 * @param array $values
Chris@0 36 * (optional) An array of values to set, keyed by property name. If the
Chris@0 37 * entity type has bundles, the bundle key has to be specified.
Chris@0 38 *
Chris@0 39 * @return \Drupal\taxonomy\Entity\Term
Chris@0 40 * The new taxonomy term object.
Chris@0 41 */
Chris@0 42 public function createTerm(Vocabulary $vocabulary, $values = []) {
Chris@0 43 $filter_formats = filter_formats();
Chris@0 44 $format = array_pop($filter_formats);
Chris@0 45 $term = Term::create($values + [
Chris@0 46 'name' => $this->randomMachineName(),
Chris@0 47 'description' => [
Chris@0 48 'value' => $this->randomMachineName(),
Chris@0 49 // Use the first available text format.
Chris@0 50 'format' => $format->id(),
Chris@0 51 ],
Chris@0 52 'vid' => $vocabulary->id(),
Chris@0 53 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
Chris@0 54 ]);
Chris@0 55 $term->save();
Chris@0 56 return $term;
Chris@0 57 }
Chris@0 58
Chris@0 59 }