trait TaxonomyTestTrait

Same name in this branch
  1. 8.9.x core/modules/taxonomy/src/Tests/TaxonomyTestTrait.php \Drupal\taxonomy\Tests\TaxonomyTestTrait
  2. 8.9.x core/modules/taxonomy/tests/src/Functional/TaxonomyTestTrait.php \Drupal\Tests\taxonomy\Functional\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

13 files declare their use of TaxonomyTestTrait
CommentEntityTest.php in core/modules/comment/tests/src/Functional/CommentEntityTest.php
PrepareUninstallTest.php in core/modules/system/tests/src/Functional/Module/PrepareUninstallTest.php
TaxonomyFieldTidTest.php in core/modules/taxonomy/tests/src/Kernel/Views/TaxonomyFieldTidTest.php
TaxonomyFieldVidTest.php in core/modules/taxonomy/tests/src/Kernel/Views/TaxonomyFieldVidTest.php
TaxonomyGlossaryTest.php in core/modules/views/tests/src/Functional/TaxonomyGlossaryTest.php

... See full list

File

core/modules/taxonomy/tests/src/Traits/TaxonomyTestTrait.php, line 13

Namespace

Drupal\Tests\taxonomy\Traits
View source
trait TaxonomyTestTrait {
    
    /**
     * Returns a new vocabulary with random properties.
     *
     * @return \Drupal\taxonomy\VocabularyInterface
     *   A vocabulary used for testing.
     */
    public function createVocabulary() {
        $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 given a vocabulary.
     *
     * @param \Drupal\taxonomy\VocabularyInterface $vocabulary
     *   The vocabulary object.
     * @param array $values
     *   (optional) An array of values to set, keyed by property name.
     *
     * @return \Drupal\taxonomy\TermInterface
     *   The new taxonomy term object.
     */
    public function createTerm(VocabularyInterface $vocabulary, $values = []) {
        $term = Term::create($values + [
            'name' => $this->randomMachineName(),
            'description' => [
                'value' => $this->randomMachineName(),
                // Use the fallback text format.
'format' => filter_fallback_format(),
            ],
            '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 given a vocabulary.
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.