function VocabularyUiTest::testVocabularyInterface

Same name in other branches
  1. 8.9.x 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 41

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('Add vocabulary');
    $edit = [];
    $vid = mb_strtolower($this->randomMachineName());
    $edit['name'] = $this->randomMachineName();
    $edit['description'] = $this->randomMachineName();
    $edit['vid'] = $vid;
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains("Created new vocabulary {$edit['name']}.");
    // Edit the vocabulary.
    $this->drupalGet('admin/structure/taxonomy');
    $this->assertSession()
        ->pageTextContains($edit['name']);
    $this->assertSession()
        ->pageTextContains($edit['description']);
    $this->assertSession()
        ->linkByHrefExists(Url::fromRoute('entity.taxonomy_term.add_form', [
        'taxonomy_vocabulary' => $edit['vid'],
    ])->toString());
    $this->clickLink('Edit vocabulary');
    $edit = [];
    $edit['name'] = $this->randomMachineName();
    $edit['description'] = $this->randomMachineName();
    $this->submitForm($edit, 'Save');
    $this->drupalGet('admin/structure/taxonomy');
    $this->assertSession()
        ->pageTextContains($edit['name']);
    $this->assertSession()
        ->pageTextContains($edit['description']);
    // Try to submit a vocabulary with a duplicate machine name.
    $edit['vid'] = $vid;
    $this->drupalGet('admin/structure/taxonomy/add');
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('The machine-readable name is already in use. It must be unique.');
    // Try to submit an invalid machine name.
    $edit['vid'] = '!&^%';
    $this->drupalGet('admin/structure/taxonomy/add');
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('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->drupalGet('admin/structure/taxonomy/add');
    $this->submitForm($edit, 'Save');
    $site_name = $this->config('system.site')
        ->get('name');
    $this->assertSession()
        ->titleEquals("Don't Panic | {$site_name}");
}

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