Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / path / tests / src / Functional / PathTaxonomyTermTest.php
1 <?php
2
3 namespace Drupal\Tests\path\Functional;
4
5 use Drupal\taxonomy\Entity\Vocabulary;
6
7 /**
8  * Tests URL aliases for taxonomy terms.
9  *
10  * @group path
11  */
12 class PathTaxonomyTermTest extends PathTestBase {
13
14   /**
15    * Modules to enable.
16    *
17    * @var array
18    */
19   public static $modules = ['taxonomy'];
20
21   protected function setUp() {
22     parent::setUp();
23
24     // Create a Tags vocabulary for the Article node type.
25     $vocabulary = Vocabulary::create([
26       'name' => t('Tags'),
27       'vid' => 'tags',
28     ]);
29     $vocabulary->save();
30
31     // Create and log in user.
32     $web_user = $this->drupalCreateUser(['administer url aliases', 'administer taxonomy', 'access administration pages']);
33     $this->drupalLogin($web_user);
34   }
35
36   /**
37    * Tests alias functionality through the admin interfaces.
38    */
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();
43     $edit = [
44       'name[0][value]' => $this->randomMachineName(),
45       'description[0][value]' => $description,
46       'path[0][alias]' => '/' . $this->randomMachineName(),
47     ];
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();
50
51     // Confirm that the alias works.
52     $this->drupalGet($edit['path[0][alias]']);
53     $this->assertText($description, 'Term can be accessed on URL alias.');
54
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.');
60
61     // Change the term's URL alias.
62     $edit2 = [];
63     $edit2['path[0][alias]'] = '/' . $this->randomMachineName();
64     $this->drupalPostForm('taxonomy/term/' . $tid . '/edit', $edit2, t('Save'));
65
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.');
69
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.');
74
75     // Remove the term's URL alias.
76     $edit3 = [];
77     $edit3['path[0][alias]'] = '';
78     $this->drupalPostForm('taxonomy/term/' . $tid . '/edit', $edit3, t('Save'));
79
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.');
84   }
85
86 }