You are here

public function TermTest::testTermMultipleParentsInterface in Drupal 8

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

Test saving a term with multiple parents through the UI.

File

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

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
    ->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary
    ->id() . '/add', $edit, t('Save'));

  // Check that the term was successfully created.
  $terms = taxonomy_term_load_multiple_by_name($edit['name[0][value]']);
  $term = reset($terms);
  $this
    ->assertNotNull($term, 'Term found in database.');
  $this
    ->assertEqual($edit['name[0][value]'], $term
    ->getName(), 'Term name was successfully saved.');
  $this
    ->assertEqual($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
    ->assertEqual($edit['parent[]'][1], $parent
    ->id(), 'Term parents were successfully saved.');
}