3 namespace Drupal\Tests\path\Functional;
5 use Drupal\taxonomy\Entity\Vocabulary;
8 * Tests URL aliases for taxonomy terms.
12 class PathTaxonomyTermTest extends PathTestBase {
19 public static $modules = ['taxonomy'];
21 protected function setUp() {
24 // Create a Tags vocabulary for the Article node type.
25 $vocabulary = Vocabulary::create([
31 // Create and log in user.
32 $web_user = $this->drupalCreateUser(['administer url aliases', 'administer taxonomy', 'access administration pages']);
33 $this->drupalLogin($web_user);
37 * Tests alias functionality through the admin interfaces.
39 public function testTermAlias() {
40 // Create a term in the default 'Tags' vocabulary with URL alias.
41 $vocabulary = Vocabulary::load('tags');
42 $description = $this->randomMachineName();
44 'name[0][value]' => $this->randomMachineName(),
45 'description[0][value]' => $description,
46 'path[0][alias]' => '/' . $this->randomMachineName(),
48 $this->drupalPostForm('admin/structure/taxonomy/manage/' . $vocabulary->id() . '/add', $edit, t('Save'));
49 $tid = db_query("SELECT tid FROM {taxonomy_term_field_data} WHERE name = :name AND default_langcode = 1", [':name' => $edit['name[0][value]']])->fetchField();
51 // Confirm that the alias works.
52 $this->drupalGet($edit['path[0][alias]']);
53 $this->assertText($description, 'Term can be accessed on URL alias.');
55 // Confirm the 'canonical' and 'shortlink' URLs.
56 $elements = $this->xpath("//link[contains(@rel, 'canonical') and contains(@href, '" . $edit['path[0][alias]'] . "')]");
57 $this->assertTrue(!empty($elements), 'Term page contains canonical link URL.');
58 $elements = $this->xpath("//link[contains(@rel, 'shortlink') and contains(@href, 'taxonomy/term/" . $tid . "')]");
59 $this->assertTrue(!empty($elements), 'Term page contains shortlink URL.');
61 // Change the term's URL alias.
63 $edit2['path[0][alias]'] = '/' . $this->randomMachineName();
64 $this->drupalPostForm('taxonomy/term/' . $tid . '/edit', $edit2, t('Save'));
66 // Confirm that the changed alias works.
67 $this->drupalGet(trim($edit2['path[0][alias]'], '/'));
68 $this->assertText($description, 'Term can be accessed on changed URL alias.');
70 // Confirm that the old alias no longer works.
71 $this->drupalGet(trim($edit['path[0][alias]'], '/'));
72 $this->assertNoText($description, 'Old URL alias has been removed after altering.');
73 $this->assertResponse(404, 'Old URL alias returns 404.');
75 // Remove the term's URL alias.
77 $edit3['path[0][alias]'] = '';
78 $this->drupalPostForm('taxonomy/term/' . $tid . '/edit', $edit3, t('Save'));
80 // Confirm that the alias no longer works.
81 $this->drupalGet(trim($edit2['path[0][alias]'], '/'));
82 $this->assertNoText($description, 'Old URL alias has been removed after altering.');
83 $this->assertResponse(404, 'Old URL alias returns 404.');