annotate core/modules/taxonomy/src/Tests/TaxonomyTestTrait.php @ 0:4c8ae668cc8c

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