function TermParentsTest::testParentFromQuery

Same name in other branches
  1. 10 core/modules/taxonomy/tests/src/Functional/TermParentsTest.php \Drupal\Tests\taxonomy\Functional\TermParentsTest::testParentFromQuery()

Test the term add/edit form with parent query parameter.

File

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

Class

TermParentsTest
Tests managing taxonomy parents through the user interface.

Namespace

Drupal\Tests\taxonomy\Functional

Code

public function testParentFromQuery() : void {
    // Create three terms without any parents.
    $term_1 = $this->createTerm('Test term 1');
    $term_2 = $this->createTerm('Test term 2');
    $term_3 = $this->createTerm('Test term 3');
    // Add term form with one parent.
    $this->drupalGet("/admin/structure/taxonomy/manage/{$this->vocabularyId}/add", [
        'query' => [
            'parent' => $term_1->id(),
        ],
    ]);
    $this->assertParentOption('Test term 1', TRUE);
    $this->assertParentOption('Test term 2', FALSE);
    $this->assertParentOption('Test term 3', FALSE);
    // Add term form with two parents.
    $this->drupalGet("/admin/structure/taxonomy/manage/{$this->vocabularyId}/add", [
        'query' => [
            'parent[0]' => $term_1->id(),
            'parent[1]' => $term_2->id(),
        ],
    ]);
    $this->assertParentOption('Test term 1', TRUE);
    $this->assertParentOption('Test term 2', TRUE);
    $this->assertParentOption('Test term 3', FALSE);
    // Add term form with no parents.
    $this->drupalGet("/admin/structure/taxonomy/manage/{$this->vocabularyId}/add", [
        'query' => [
            'parent' => '',
        ],
    ]);
    $this->assertParentOption('Test term 1', FALSE);
    $this->assertParentOption('Test term 2', FALSE);
    $this->assertParentOption('Test term 3', FALSE);
    // Add term form with invalid parent.
    $this->drupalGet("/admin/structure/taxonomy/manage/{$this->vocabularyId}/add", [
        'query' => [
            'parent' => -1,
        ],
    ]);
    $this->assertParentOption('Test term 1', FALSE);
    $this->assertParentOption('Test term 2', FALSE);
    $this->assertParentOption('Test term 3', FALSE);
    // Edit term form with one parent.
    $this->drupalGet($term_1->toUrl('edit-form'), [
        'query' => [
            'parent' => $term_2->id(),
        ],
    ]);
    $this->assertParentOption('Test term 2', TRUE);
    $this->assertParentOption('Test term 3', FALSE);
    // Edit term form with two parents.
    $this->drupalGet($term_1->toUrl('edit-form'), [
        'query' => [
            'parent[0]' => $term_2->id(),
            'parent[1]' => $term_3->id(),
        ],
    ]);
    $this->assertParentOption('Test term 2', TRUE);
    $this->assertParentOption('Test term 3', TRUE);
    // Edit term form with no parents.
    $this->drupalGet($term_1->toUrl('edit-form'), [
        'query' => [
            'parent' => '',
        ],
    ]);
    $this->assertParentOption('Test term 2', FALSE);
    $this->assertParentOption('Test term 3', FALSE);
    // Edit term form with invalid parent.
    $this->drupalGet($term_1->toUrl('edit-form'), [
        'query' => [
            'parent' => -1,
        ],
    ]);
    $this->assertParentOption('Test term 2', FALSE);
    $this->assertParentOption('Test term 3', FALSE);
}

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