You are here

public function TermTest::testTermMultipleParentsInterface in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/taxonomy/tests/src/Functional/TermTest.php \Drupal\Tests\taxonomy\Functional\TermTest::testTermMultipleParentsInterface()

Tests saving a term with multiple parents through the UI.

File

core/modules/taxonomy/tests/src/Functional/TermTest.php, line 503

Class

TermTest
Tests load, save and delete for taxonomy terms.

Namespace

Drupal\Tests\taxonomy\Functional

Code

public function testTermMultipleParentsInterface() {

  // Add a new term to the vocabulary so that we can have multiple parents.
  $parent = $this
    ->createTerm($this->vocabulary);

  // Add a new term with multiple parents.
  $edit = [
    'name[0][value]' => $this
      ->randomMachineName(12),
    'description[0][value]' => $this
      ->randomMachineName(100),
    'parent[]' => [
      0,
      $parent
        ->id(),
    ],
  ];

  // Save the new term.
  $this
    ->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary
    ->id() . '/add');
  $this
    ->submitForm($edit, 'Save');

  // Check that the term was successfully created.
  $terms = \Drupal::entityTypeManager()
    ->getStorage('taxonomy_term')
    ->loadByProperties([
    'name' => $edit['name[0][value]'],
  ]);
  $term = reset($terms);
  $this
    ->assertNotNull($term, 'Term found in database.');
  $this
    ->assertEquals($edit['name[0][value]'], $term
    ->getName(), 'Term name was successfully saved.');
  $this
    ->assertEquals($edit['description[0][value]'], $term
    ->getDescription(), 'Term description was successfully saved.');

  // Check that the parent tid is still there. The other parent (<root>) is
  // not added by \Drupal\taxonomy\TermStorageInterface::loadParents().
  $parents = $this->container
    ->get('entity_type.manager')
    ->getStorage('taxonomy_term')
    ->loadParents($term
    ->id());
  $parent = reset($parents);
  $this
    ->assertEquals($edit['parent[]'][1], $parent
    ->id(), 'Term parents were successfully saved.');
}