annotate core/modules/taxonomy/tests/src/Functional/TaxonomyTestTrait.php @ 6:875880e46745

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