function PathTaxonomyTermTest::testTermAlias

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

Tests alias functionality through the admin interfaces.

File

core/modules/path/tests/src/Functional/PathTaxonomyTermTest.php, line 51

Class

PathTaxonomyTermTest
Tests URL aliases for taxonomy terms.

Namespace

Drupal\Tests\path\Functional

Code

public function testTermAlias() : void {
    // Create a term in the default 'Tags' vocabulary with URL alias.
    $vocabulary = Vocabulary::load('tags');
    $description = $this->randomMachineName();
    $edit = [
        'name[0][value]' => $this->randomMachineName(),
        'description[0][value]' => $description,
        'path[0][alias]' => '/' . $this->randomMachineName(),
    ];
    $this->drupalGet('admin/structure/taxonomy/manage/' . $vocabulary->id() . '/add');
    $this->submitForm($edit, 'Save');
    $tids = \Drupal::entityQuery('taxonomy_term')->accessCheck(FALSE)
        ->condition('name', $edit['name[0][value]'])
        ->condition('default_langcode', 1)
        ->execute();
    $tid = reset($tids);
    // Confirm that the alias works.
    $this->drupalGet($edit['path[0][alias]']);
    $this->assertSession()
        ->pageTextContains($description);
    // Confirm the 'canonical' and 'shortlink' URLs.
    $this->assertSession()
        ->elementExists('xpath', "//link[contains(@rel, 'canonical') and contains(@href, '" . $edit['path[0][alias]'] . "')]");
    $this->assertSession()
        ->elementExists('xpath', "//link[contains(@rel, 'shortlink') and contains(@href, 'taxonomy/term/" . $tid . "')]");
    // Change the term's URL alias.
    $edit2 = [];
    $edit2['path[0][alias]'] = '/' . $this->randomMachineName();
    $this->drupalGet('taxonomy/term/' . $tid . '/edit');
    $this->submitForm($edit2, 'Save');
    // Confirm that the changed alias works.
    $this->drupalGet(trim($edit2['path[0][alias]'], '/'));
    $this->assertSession()
        ->pageTextContains($description);
    // Confirm that the old alias no longer works.
    $this->drupalGet(trim($edit['path[0][alias]'], '/'));
    $this->assertSession()
        ->pageTextNotContains($description);
    $this->assertSession()
        ->statusCodeEquals(404);
    // Remove the term's URL alias.
    $edit3 = [];
    $edit3['path[0][alias]'] = '';
    $this->drupalGet('taxonomy/term/' . $tid . '/edit');
    $this->submitForm($edit3, 'Save');
    // Confirm that the alias no longer works.
    $this->drupalGet(trim($edit2['path[0][alias]'], '/'));
    $this->assertSession()
        ->pageTextNotContains($description);
    $this->assertSession()
        ->statusCodeEquals(404);
}

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