function TermTest::testTermInterface

Same name and namespace in other branches
  1. 9 core/modules/taxonomy/tests/src/Functional/TermTest.php \Drupal\Tests\taxonomy\Functional\TermTest::testTermInterface()
  2. 10 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 325

Class

TermTest
Tests load, save and delete for taxonomy terms.

Namespace

Drupal\Tests\taxonomy\Functional

Code

public function testTermInterface() {
    \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->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary
        ->id() . '/add', $edit, t('Save'));
    $terms = taxonomy_term_load_multiple_by_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(t('Edit'));
    $this->assertRaw($edit['name[0][value]'], 'The randomly generated term name is present.');
    $this->assertText($edit['description[0][value]'], 'The randomly generated term description is present.');
    $edit = [
        'name[0][value]' => $this->randomMachineName(14),
        'description[0][value]' => $this->randomMachineName(102),
    ];
    // Edit the term.
    $this->drupalPostForm('taxonomy/term/' . $term->id() . '/edit', $edit, t('Save'));
    // Check that the term is still present at admin UI after edit.
    $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary
        ->id() . '/overview');
    $this->assertText($edit['name[0][value]'], 'The randomly generated term name is present.');
    $this->assertSession()
        ->linkExists(t('Edit'));
    // 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->assertText($edit['name[0][value]'], 'The randomly generated term name is present.');
    $this->assertText($edit['description[0][value]'], 'The randomly generated term description is present.');
    // Did this page request display a 'term-listing-heading'?
    $this->assertSession()
        ->elementExists('xpath', '//div[contains(@class, "field--name-description")]');
    // 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[contains(@class, "field--entity-taxonomy-term--description")]');
    // Check that the description value is processed.
    $value = $this->randomMachineName();
    $term->setDescription($value);
    $term->save();
    $this->assertEqual($term->description->processed, "<p>{$value}</p>\n");
    // 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(t('Delete'));
    $this->drupalPostForm(NULL, NULL, t('Delete'));
    // Assert that the term no longer exists.
    $this->drupalGet('taxonomy/term/' . $term->id());
    $this->assertSession()
        ->statusCodeEquals(404);
}

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