trait TaxonomyTestTrait

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

Provides common helper methods for Taxonomy module tests.

Hierarchy

23 files declare their use of TaxonomyTestTrait
AddModerationConfigActionTest.php in core/modules/content_moderation/tests/src/Kernel/ConfigAction/AddModerationConfigActionTest.php
CommentEntityTest.php in core/modules/comment/tests/src/Functional/CommentEntityTest.php
ContentImportTest.php in core/tests/Drupal/FunctionalTests/DefaultContent/ContentImportTest.php
DefaultValueWidgetTest.php in core/modules/field_ui/tests/src/FunctionalJavascript/DefaultValueWidgetTest.php
LoadMultipleTest.php in core/modules/taxonomy/tests/src/Kernel/LoadMultipleTest.php

... See full list

File

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

Namespace

Drupal\Tests\taxonomy\Traits
View source
trait TaxonomyTestTrait {
    
    /**
     * Returns a new vocabulary with random properties.
     *
     * @param array $values
     *   (optional) Default values for the Vocabulary::create() method.
     *
     * @return \Drupal\taxonomy\VocabularyInterface
     *   A vocabulary used for testing.
     */
    protected function createVocabulary(array $values = []) {
        $values += [
            'name' => $this->randomMachineName(),
            'description' => $this->randomMachineName(),
            'vid' => $this->randomMachineName(),
            'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
            'weight' => mt_rand(0, 10),
        ];
        $vocabulary = Vocabulary::create($values);
        $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.
     */
    protected 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;
    }
    
    /**
     * Creates a new revision for a given taxonomy term.
     *
     * @param \Drupal\taxonomy\TermInterface $term
     *   A taxonomy term object.
     *
     * @return \Drupal\taxonomy\TermInterface
     *   The new taxonomy term object.
     */
    protected function createTaxonomyTermRevision(TermInterface $term) {
        $term->set('name', $this->randomMachineName());
        $term->setNewRevision();
        $term->save();
        return $term;
    }

}

Members

Title Sort descending Modifiers Object type Summary
TaxonomyTestTrait::createTaxonomyTermRevision protected function Creates a new revision for a given taxonomy term.
TaxonomyTestTrait::createTerm protected function Returns a new term with random properties given a vocabulary.
TaxonomyTestTrait::createVocabulary protected 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.