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. 8.9.x 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 44

Class

VocabularyUiTest
Tests the taxonomy vocabulary interface.

Namespace

Drupal\Tests\taxonomy\Functional

Code

public function testVocabularyInterface() : void {
    // Visit the main taxonomy administration page.
    $this->drupalGet('admin/structure/taxonomy');
    // Create a new vocabulary.
    $this->clickLink('Add vocabulary');
    $edit = [];
    $vid = $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}");
    // Delete the vocabulary.
    $this->drupalGet('admin/structure/taxonomy');
    $href = Url::fromRoute('entity.taxonomy_vocabulary.delete_form', [
        'taxonomy_vocabulary' => $edit['vid'],
    ])->toString();
    $xpath = $this->assertSession()
        ->buildXPathQuery('//a[contains(@href, :href)]', [
        ':href' => $href,
    ]);
    $link = $this->assertSession()
        ->elementExists('xpath', $xpath);
    $this->assertEquals('Delete vocabulary', $link->getText());
    $link->click();
    // Confirm deletion.
    $this->assertSession()
        ->responseContains(new FormattableMarkup('Are you sure you want to delete the vocabulary %name?', [
        '%name' => $edit['name'],
    ]));
    $this->submitForm([], 'Delete');
    $this->assertSession()
        ->responseContains(new FormattableMarkup('Deleted vocabulary %name.', [
        '%name' => $edit['name'],
    ]));
    $this->container
        ->get('entity_type.manager')
        ->getStorage('taxonomy_vocabulary')
        ->resetCache();
    $this->assertNull(Vocabulary::load($edit['vid']), 'Vocabulary not found.');
}

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