TaxonomyTestTrait.php 2.02 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\taxonomy\Tests;

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\Traits\TaxonomyTestTrait instead', E_USER_DEPRECATED);
6

7 8
use Drupal\Core\Language\LanguageInterface;
use Drupal\taxonomy\Entity\Vocabulary;
9
use Drupal\taxonomy\Entity\Term;
10 11 12

/**
 * Provides common helper methods for Taxonomy module tests.
13 14
 *
 * @deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0.
15
 *   Use \Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait instead.
16 17 18 19 20 21
 */
trait TaxonomyTestTrait {

  /**
   * Returns a new vocabulary with random properties.
   */
22
  public function createVocabulary() {
23
    // Create a vocabulary.
24
    $vocabulary = Vocabulary::create([
25 26
      'name' => $this->randomMachineName(),
      'description' => $this->randomMachineName(),
27
      'vid' => mb_strtolower($this->randomMachineName()),
28 29
      'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
      'weight' => mt_rand(0, 10),
30
    ]);
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    $vocabulary->save();
    return $vocabulary;
  }

  /**
   * Returns a new term with random properties in vocabulary $vid.
   *
   * @param \Drupal\taxonomy\Entity\Vocabulary $vocabulary
   *   The vocabulary object.
   * @param array $values
   *   (optional) An array of values to set, keyed by property name. If the
   *   entity type has bundles, the bundle key has to be specified.
   *
   * @return \Drupal\taxonomy\Entity\Term
   *   The new taxonomy term object.
   */
47
  public function createTerm(Vocabulary $vocabulary, $values = []) {
48 49
    $filter_formats = filter_formats();
    $format = array_pop($filter_formats);
50
    $term = Term::create($values + [
51
      'name' => $this->randomMachineName(),
52
      'description' => [
53 54 55
        'value' => $this->randomMachineName(),
        // Use the first available text format.
        'format' => $format->id(),
56
      ],
57 58
      'vid' => $vocabulary->id(),
      'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
59
    ]);
60 61 62
    $term->save();
    return $term;
  }
63

64
}