function VocabularyUiTest::testVocabularyInterface

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

Create, edit and delete a vocabulary via the user interface.

File

core/modules/taxonomy/tests/src/Functional/VocabularyUiTest.php, line 38

Class

VocabularyUiTest
Tests the taxonomy vocabulary interface.

Namespace

Drupal\Tests\taxonomy\Functional

Code

public function testVocabularyInterface() {
    // Visit the main taxonomy administration page.
    $this->drupalGet('admin/structure/taxonomy');
    // Create a new vocabulary.
    $this->clickLink(t('Add vocabulary'));
    $edit = [];
    $vid = mb_strtolower($this->randomMachineName());
    $edit['name'] = $this->randomMachineName();
    $edit['description'] = $this->randomMachineName();
    $edit['vid'] = $vid;
    $this->drupalPostForm(NULL, $edit, t('Save'));
    $this->assertRaw(t('Created new vocabulary %name.', [
        '%name' => $edit['name'],
    ]), 'Vocabulary created successfully.');
    // Edit the vocabulary.
    $this->drupalGet('admin/structure/taxonomy');
    $this->assertText($edit['name'], 'Vocabulary name found in the vocabulary overview listing.');
    $this->assertText($edit['description'], 'Vocabulary description found in the vocabulary overview listing.');
    $this->assertLinkByHref(Url::fromRoute('entity.taxonomy_term.add_form', [
        'taxonomy_vocabulary' => $edit['vid'],
    ])->toString());
    $this->clickLink(t('Edit vocabulary'));
    $edit = [];
    $edit['name'] = $this->randomMachineName();
    $edit['description'] = $this->randomMachineName();
    $this->drupalPostForm(NULL, $edit, t('Save'));
    $this->drupalGet('admin/structure/taxonomy');
    $this->assertText($edit['name'], 'Vocabulary name found in the vocabulary overview listing.');
    $this->assertText($edit['description'], 'Vocabulary description found in the vocabulary overview listing.');
    // Try to submit a vocabulary with a duplicate machine name.
    $edit['vid'] = $vid;
    $this->drupalPostForm('admin/structure/taxonomy/add', $edit, t('Save'));
    $this->assertText(t('The machine-readable name is already in use. It must be unique.'));
    // Try to submit an invalid machine name.
    $edit['vid'] = '!&^%';
    $this->drupalPostForm('admin/structure/taxonomy/add', $edit, t('Save'));
    $this->assertText(t('The machine-readable name must contain only lowercase letters, numbers, and underscores.'));
    // Ensure that vocabulary titles are escaped properly.
    $edit = [];
    $edit['name'] = 'Don\'t Panic';
    $edit['description'] = $this->randomMachineName();
    $edit['vid'] = 'don_t_panic';
    $this->drupalPostForm('admin/structure/taxonomy/add', $edit, t('Save'));
    $site_name = $this->config('system.site')
        ->get('name');
    $this->assertTitle("Don't Panic | {$site_name}");
}

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