You are here

public function TaxonomyTestTrait::createTerm in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/taxonomy/tests/src/Traits/TaxonomyTestTrait.php \Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait::createTerm()
  2. 9 core/modules/taxonomy/tests/src/Traits/TaxonomyTestTrait.php \Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait::createTerm()

Returns a new term with random properties given a vocabulary.

Parameters

\Drupal\taxonomy\VocabularyInterface $vocabulary: The vocabulary object.

array $values: (optional) An array of values to set, keyed by property name.

Return value

\Drupal\taxonomy\TermInterface The new taxonomy term object.

4 calls to TaxonomyTestTrait::createTerm()
PrepareUninstallTest::setUp in core/modules/system/tests/src/Functional/Module/PrepareUninstallTest.php
TaxonomyFieldTidTest::setUp in core/modules/taxonomy/tests/src/Kernel/Views/TaxonomyFieldTidTest.php
TaxonomyFieldVidTest::setUp in core/modules/taxonomy/tests/src/Kernel/Views/TaxonomyFieldVidTest.php
TermContextTest::testGetRuntimeContexts in core/modules/taxonomy/tests/src/Kernel/ContextProvider/TermContextTest.php
@covers ::getRuntimeContexts

File

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

Class

TaxonomyTestTrait
Provides common helper methods for Taxonomy module tests.

Namespace

Drupal\Tests\taxonomy\Traits

Code

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;
}