function TermTest::testTermMultipleParentsInterface

Same name and namespace in other branches
  1. 9 core/modules/taxonomy/tests/src/Functional/TermTest.php \Drupal\Tests\taxonomy\Functional\TermTest::testTermMultipleParentsInterface()
  2. 8.9.x core/modules/taxonomy/tests/src/Functional/TermTest.php \Drupal\Tests\taxonomy\Functional\TermTest::testTermMultipleParentsInterface()
  3. 11.x 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 514

Class

TermTest
Tests load, save and delete for taxonomy terms.

Namespace

Drupal\Tests\taxonomy\Functional

Code

public function testTermMultipleParentsInterface() : void {
  // Add two new terms to the vocabulary so that we can have multiple parents.
  // These will be terms with tids of 1 and 2 respectively.
  $this->createTerm($this->vocabulary);
  $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,
      1,
    ],
  ];
  // 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.
  $term = $this->reloadTermByName($edit['name[0][value]']);
  $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 we have the expected parents.
  $this->assertEquals([
    0,
    1,
  ], $this->getParentTids($term), 'Term parents (root plus one) were successfully saved.');
  // Load the edit form and save again to ensure parent are preserved.
  // Generate a new name, so we know that the term really is saved.
  $this->drupalGet('taxonomy/term/' . $term->id() . '/edit');
  $edit = [
    'name[0][value]' => $this->randomMachineName(12),
  ];
  $this->submitForm($edit, 'Save');
  $this->assertSession()
    ->pageTextContains('Updated term ' . $edit['name[0][value]']);
  $this->drupalGet('taxonomy/term/' . $term->id() . '/edit');
  $this->submitForm([], 'Save');
  $this->assertSession()
    ->pageTextContains('Updated term ' . $edit['name[0][value]']);
  // Check that we still have the expected parents.
  $term = $this->reloadTermByName($edit['name[0][value]']);
  $this->assertEquals([
    0,
    1,
  ], $this->getParentTids($term), 'Term parents (root plus one) were successfully saved again.');
  // Save with two real parents. i.e., not including <root>.
  $this->drupalGet('taxonomy/term/' . $term->id() . '/edit');
  $edit = [
    'name[0][value]' => $this->randomMachineName(12),
    'parent[]' => [
      1,
      2,
    ],
  ];
  $this->submitForm($edit, 'Save');
  $this->assertSession()
    ->pageTextContains('Updated term ' . $edit['name[0][value]']);
  $this->drupalGet('taxonomy/term/' . $term->id() . '/edit');
  $this->submitForm([], 'Save');
  $this->assertSession()
    ->pageTextContains('Updated term ' . $edit['name[0][value]']);
  // Check that we have the expected parents.
  $term = $this->reloadTermByName($edit['name[0][value]']);
  $this->assertEquals([
    1,
    2,
  ], $this->getParentTids($term), 'Term parents (two real) were successfully saved.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.