You are here

public function TermParentsTest::testAddWithParents in Drupal 10

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

Tests specifying parents when creating terms.

File

core/modules/taxonomy/tests/src/Functional/TermParentsTest.php, line 65

Class

TermParentsTest
Tests managing taxonomy parents through the user interface.

Namespace

Drupal\Tests\taxonomy\Functional

Code

public function testAddWithParents() {
  $this
    ->drupalGet("/admin/structure/taxonomy/manage/{$this->vocabularyId}/add");
  $page = $this
    ->getSession()
    ->getPage();

  // Create a term without any parents.
  $term_1 = $this
    ->submitAddTermForm('Test term 1');
  $expected = [
    [
      'target_id' => 0,
    ],
  ];
  $this
    ->assertEquals($expected, $term_1
    ->get('parent')
    ->getValue());

  // Explicitly selecting <root> should have the same effect as not selecting
  // anything.
  $page
    ->selectFieldOption('Parent terms', '<root>');
  $term_2 = $this
    ->submitAddTermForm('Test term 2');
  $this
    ->assertEquals($expected, $term_2
    ->get('parent')
    ->getValue());

  // Create two terms with the previously created ones as parents,
  // respectively.
  $page
    ->selectFieldOption('Parent terms', 'Test term 1');
  $term_3 = $this
    ->submitAddTermForm('Test term 3');
  $expected = [
    [
      'target_id' => $term_1
        ->id(),
    ],
  ];
  $this
    ->assertEquals($expected, $term_3
    ->get('parent')
    ->getValue());
  $page
    ->selectFieldOption('Parent terms', 'Test term 2');
  $term_4 = $this
    ->submitAddTermForm('Test term 4');
  $expected = [
    [
      'target_id' => $term_2
        ->id(),
    ],
  ];
  $this
    ->assertEquals($expected, $term_4
    ->get('parent')
    ->getValue());

  // Create a term with term 3 as parent.
  $page
    ->selectFieldOption('Parent terms', '-Test term 3');
  $term_5 = $this
    ->submitAddTermForm('Test term 5');
  $expected = [
    [
      'target_id' => $term_3
        ->id(),
    ],
  ];
  $this
    ->assertEquals($expected, $term_5
    ->get('parent')
    ->getValue());

  // Create a term with multiple parents.
  $page
    ->selectFieldOption('Parent terms', '--Test term 5');
  $page
    ->selectFieldOption('Parent terms', '-Test term 4', TRUE);
  $term_6 = $this
    ->submitAddTermForm('Test term 6');
  $expected = [
    [
      'target_id' => $term_5
        ->id(),
    ],
    [
      'target_id' => $term_4
        ->id(),
    ],
  ];
  $this
    ->assertEquals($expected, $term_6
    ->get('parent')
    ->getValue());
}