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