trait TaxonomyTestTrait

Same name in this branch
  1. 8.9.x core/modules/taxonomy/tests/src/Functional/TaxonomyTestTrait.php \Drupal\Tests\taxonomy\Functional\TaxonomyTestTrait
  2. 8.9.x core/modules/taxonomy/tests/src/Traits/TaxonomyTestTrait.php \Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait
Same name in other branches
  1. 9 core/modules/taxonomy/tests/src/Traits/TaxonomyTestTrait.php \Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait
  2. 10 core/modules/taxonomy/tests/src/Traits/TaxonomyTestTrait.php \Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait
  3. 11.x core/modules/taxonomy/tests/src/Traits/TaxonomyTestTrait.php \Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait

Provides common helper methods for Taxonomy module tests.

Hierarchy

Deprecated

in drupal:8.4.0 and is removed from drupal:9.0.0. Use \Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait instead.

File

core/modules/taxonomy/src/Tests/TaxonomyTestTrait.php, line 17

Namespace

Drupal\taxonomy\Tests
View source
trait TaxonomyTestTrait {
    
    /**
     * Returns a new vocabulary with random properties.
     */
    public function createVocabulary() {
        // Create a vocabulary.
        $vocabulary = Vocabulary::create([
            'name' => $this->randomMachineName(),
            'description' => $this->randomMachineName(),
            'vid' => mb_strtolower($this->randomMachineName()),
            'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
            'weight' => mt_rand(0, 10),
        ]);
        $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.
     */
    public function createTerm(Vocabulary $vocabulary, $values = []) {
        $filter_formats = filter_formats();
        $format = array_pop($filter_formats);
        $term = Term::create($values + [
            'name' => $this->randomMachineName(),
            'description' => [
                'value' => $this->randomMachineName(),
                // Use the first available text format.
'format' => $format->id(),
            ],
            'vid' => $vocabulary->id(),
            'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
        ]);
        $term->save();
        return $term;
    }

}

Members

Title Sort descending Modifiers Object type Summary
TaxonomyTestTrait::createTerm public function Returns a new term with random properties in vocabulary $vid.
TaxonomyTestTrait::createVocabulary public function Returns a new vocabulary with random properties.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.