annotate core/modules/taxonomy/src/Tests/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\taxonomy\Tests;
Chris@0 4
Chris@0 5 @trigger_error(__NAMESPACE__ . '\TaxonomyTestTrait is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Instead, use \Drupal\Tests\taxonomy\Functional\TaxonomyTestTrait', E_USER_DEPRECATED);
Chris@0 6
Chris@0 7 use Drupal\Core\Language\LanguageInterface;
Chris@0 8 use Drupal\taxonomy\Entity\Vocabulary;
Chris@0 9 use Drupal\taxonomy\Entity\Term;
Chris@0 10
Chris@0 11 /**
Chris@0 12 * Provides common helper methods for Taxonomy module tests.
Chris@0 13 *
Chris@0 14 * @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0.
Chris@0 15 * Use \Drupal\Tests\taxonomy\Functional\TaxonomyTestTrait
Chris@0 16 */
Chris@0 17 trait TaxonomyTestTrait {
Chris@0 18
Chris@0 19 /**
Chris@0 20 * Returns a new vocabulary with random properties.
Chris@0 21 */
Chris@0 22 public function createVocabulary() {
Chris@0 23 // Create a vocabulary.
Chris@0 24 $vocabulary = Vocabulary::create([
Chris@0 25 'name' => $this->randomMachineName(),
Chris@0 26 'description' => $this->randomMachineName(),
Chris@17 27 'vid' => mb_strtolower($this->randomMachineName()),
Chris@0 28 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
Chris@0 29 'weight' => mt_rand(0, 10),
Chris@0 30 ]);
Chris@0 31 $vocabulary->save();
Chris@0 32 return $vocabulary;
Chris@0 33 }
Chris@0 34
Chris@0 35 /**
Chris@0 36 * Returns a new term with random properties in vocabulary $vid.
Chris@0 37 *
Chris@0 38 * @param \Drupal\taxonomy\Entity\Vocabulary $vocabulary
Chris@0 39 * The vocabulary object.
Chris@0 40 * @param array $values
Chris@0 41 * (optional) An array of values to set, keyed by property name. If the
Chris@0 42 * entity type has bundles, the bundle key has to be specified.
Chris@0 43 *
Chris@0 44 * @return \Drupal\taxonomy\Entity\Term
Chris@0 45 * The new taxonomy term object.
Chris@0 46 */
Chris@0 47 public function createTerm(Vocabulary $vocabulary, $values = []) {
Chris@0 48 $filter_formats = filter_formats();
Chris@0 49 $format = array_pop($filter_formats);
Chris@0 50 $term = Term::create($values + [
Chris@0 51 'name' => $this->randomMachineName(),
Chris@0 52 'description' => [
Chris@0 53 'value' => $this->randomMachineName(),
Chris@0 54 // Use the first available text format.
Chris@0 55 'format' => $format->id(),
Chris@0 56 ],
Chris@0 57 'vid' => $vocabulary->id(),
Chris@0 58 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
Chris@0 59 ]);
Chris@0 60 $term->save();
Chris@0 61 return $term;
Chris@0 62 }
Chris@0 63
Chris@0 64 }