function TermTest::testTermInterface

Same name in other branches
  1. 9 core/modules/taxonomy/tests/src/Functional/TermTest.php \Drupal\Tests\taxonomy\Functional\TermTest::testTermInterface()
  2. 8.9.x core/modules/taxonomy/tests/src/Functional/TermTest.php \Drupal\Tests\taxonomy\Functional\TermTest::testTermInterface()
  3. 11.x core/modules/taxonomy/tests/src/Functional/TermTest.php \Drupal\Tests\taxonomy\Functional\TermTest::testTermInterface()

Save, edit and delete a term using the user interface.

File

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

Class

TermTest
Tests load, save and delete for taxonomy terms.

Namespace

Drupal\Tests\taxonomy\Functional

Code

public function testTermInterface() : void {
    \Drupal::service('module_installer')->install([
        'views',
    ]);
    $edit = [
        'name[0][value]' => $this->randomMachineName(12),
        'description[0][value]' => $this->randomMachineName(100),
    ];
    // Explicitly set the parents field to 'root', to ensure that
    // TermForm::save() handles the invalid term ID correctly.
    $edit['parent[]'] = [
        0,
    ];
    // Create the term to edit.
    $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary
        ->id() . '/add');
    $this->submitForm($edit, 'Save');
    // Ensure form redirected back to term add page.
    $this->assertSession()
        ->addressEquals('admin/structure/taxonomy/manage/' . $this->vocabulary
        ->id() . '/add');
    $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')
        ->loadByProperties([
        'name' => $edit['name[0][value]'],
    ]);
    $term = reset($terms);
    $this->assertNotNull($term, 'Term found in database.');
    // Submitting a term takes us to the add page; we need the List page.
    $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary
        ->id() . '/overview');
    $this->clickLink('Edit', 1);
    // Verify that the randomly generated term is present.
    $this->assertSession()
        ->pageTextContains($edit['name[0][value]']);
    $this->assertSession()
        ->pageTextContains($edit['description[0][value]']);
    // Test the "Add child" link on the overview page.
    $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary
        ->id() . '/overview');
    $this->assertSession()
        ->linkExistsExact('Add child');
    $this->clickLink('Add child');
    $edit = [
        'name[0][value]' => 'Child term',
    ];
    $this->submitForm($edit, 'Save');
    $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')
        ->loadByProperties([
        'name' => 'Child term',
    ]);
    $child = reset($terms);
    $this->assertNotNull($child, 'Child term found in database.');
    $this->assertEquals($term->id(), $child->get('parent')
        ->getValue()[0]['target_id']);
    // Edit the term.
    $edit = [
        'name[0][value]' => $this->randomMachineName(14),
        'description[0][value]' => $this->randomMachineName(102),
    ];
    $this->drupalGet('taxonomy/term/' . $term->id() . '/edit');
    $this->submitForm($edit, 'Save');
    // Ensure form redirected back to term view.
    $this->assertSession()
        ->addressEquals('taxonomy/term/' . $term->id());
    // Check that the term is still present at admin UI after edit.
    $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary
        ->id() . '/overview');
    $this->assertSession()
        ->pageTextContains($edit['name[0][value]']);
    $this->assertSession()
        ->linkExists('Edit');
    // Unpublish the term.
    $this->drupalGet('taxonomy/term/' . $term->id() . '/edit');
    $this->submitForm([
        "status[value]" => 0,
    ], 'Save');
    // Check that the term is now unpublished in the list.
    $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary
        ->id() . '/overview');
    $this->assertSession()
        ->elementTextContains('css', "#edit-terms-tid{$term->id()}0-status", 'Unpublished');
    // Check the term link can be clicked through to the term page.
    $this->clickLink($edit['name[0][value]']);
    $this->assertSession()
        ->statusCodeEquals(200);
    // View the term and check that it is correct.
    $this->drupalGet('taxonomy/term/' . $term->id());
    $this->assertSession()
        ->pageTextContains($edit['name[0][value]']);
    $this->assertSession()
        ->pageTextContains($edit['description[0][value]']);
    // Did this page request display a 'term-listing-heading'?
    $this->assertSession()
        ->elementExists('xpath', '//div[@class="views-element-container"]/div/header/div/div/p');
    // Check that it does NOT show a description when description is blank.
    $term->setDescription(NULL);
    $term->save();
    $this->drupalGet('taxonomy/term/' . $term->id());
    $this->assertSession()
        ->elementNotExists('xpath', '//div[@class="views-element-container"]/div/header/div/div/p');
    // Check that the description value is processed.
    $value = $this->randomMachineName();
    $term->setDescription($value);
    $term->save();
    $this->assertSame("<p>{$value}</p>\n", (string) $term->description->processed);
    // Check that the term feed page is working.
    $this->drupalGet('taxonomy/term/' . $term->id() . '/feed');
    // Delete the term.
    $this->drupalGet('taxonomy/term/' . $term->id() . '/edit');
    $this->clickLink('Delete');
    $this->submitForm([], 'Delete');
    // Assert that the term no longer exists.
    $this->drupalGet('taxonomy/term/' . $term->id());
    $this->assertSession()
        ->statusCodeEquals(404);
    // Test "save and go to list" action while creating term.
    // Create the term to edit.
    $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary
        ->id() . '/add');
    $edit = [
        'name[0][value]' => $this->randomMachineName(12),
        'description[0][value]' => $this->randomMachineName(100),
    ];
    // Create the term to edit.
    $this->submitForm($edit, 'Save and go to list');
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSession()
        ->addressEquals('admin/structure/taxonomy/manage/' . $this->vocabulary
        ->id() . '/overview');
    $this->assertSession()
        ->pageTextContains($edit['name[0][value]']);
    // Validate that "Save and go to list" doesn't exist when destination
    // parameter is present.
    $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary
        ->id() . '/add', [
        'query' => [
            'destination' => 'node/add',
        ],
    ]);
    $this->assertSession()
        ->pageTextNotContains('Save and go to list');
    // Validate that "Save and go to list" doesn't exist when missing permission
    // 'access taxonomy overview'.
    $this->drupalLogin($this->drupalCreateUser([
        'administer taxonomy',
        'bypass node access',
    ]));
    $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary
        ->id() . '/add');
    $this->assertSession()
        ->pageTextNotContains('Save and go to list');
}

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