Chris@0: t('Tags'), Chris@0: 'vid' => 'tags', Chris@0: ]); Chris@0: $vocabulary->save(); Chris@0: Chris@0: // Create and log in user. Chris@0: $web_user = $this->drupalCreateUser(['administer url aliases', 'administer taxonomy', 'access administration pages']); Chris@0: $this->drupalLogin($web_user); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests alias functionality through the admin interfaces. Chris@0: */ Chris@0: public function testTermAlias() { Chris@0: // Create a term in the default 'Tags' vocabulary with URL alias. Chris@0: $vocabulary = Vocabulary::load('tags'); Chris@0: $description = $this->randomMachineName(); Chris@0: $edit = [ Chris@0: 'name[0][value]' => $this->randomMachineName(), Chris@0: 'description[0][value]' => $description, Chris@0: 'path[0][alias]' => '/' . $this->randomMachineName(), Chris@0: ]; Chris@0: $this->drupalPostForm('admin/structure/taxonomy/manage/' . $vocabulary->id() . '/add', $edit, t('Save')); Chris@0: $tid = db_query("SELECT tid FROM {taxonomy_term_field_data} WHERE name = :name AND default_langcode = 1", [':name' => $edit['name[0][value]']])->fetchField(); Chris@0: Chris@0: // Confirm that the alias works. Chris@0: $this->drupalGet($edit['path[0][alias]']); Chris@0: $this->assertText($description, 'Term can be accessed on URL alias.'); Chris@0: Chris@0: // Confirm the 'canonical' and 'shortlink' URLs. Chris@0: $elements = $this->xpath("//link[contains(@rel, 'canonical') and contains(@href, '" . $edit['path[0][alias]'] . "')]"); Chris@0: $this->assertTrue(!empty($elements), 'Term page contains canonical link URL.'); Chris@0: $elements = $this->xpath("//link[contains(@rel, 'shortlink') and contains(@href, 'taxonomy/term/" . $tid . "')]"); Chris@0: $this->assertTrue(!empty($elements), 'Term page contains shortlink URL.'); Chris@0: Chris@0: // Change the term's URL alias. Chris@0: $edit2 = []; Chris@0: $edit2['path[0][alias]'] = '/' . $this->randomMachineName(); Chris@0: $this->drupalPostForm('taxonomy/term/' . $tid . '/edit', $edit2, t('Save')); Chris@0: Chris@0: // Confirm that the changed alias works. Chris@0: $this->drupalGet(trim($edit2['path[0][alias]'], '/')); Chris@0: $this->assertText($description, 'Term can be accessed on changed URL alias.'); Chris@0: Chris@0: // Confirm that the old alias no longer works. Chris@0: $this->drupalGet(trim($edit['path[0][alias]'], '/')); Chris@0: $this->assertNoText($description, 'Old URL alias has been removed after altering.'); Chris@0: $this->assertResponse(404, 'Old URL alias returns 404.'); Chris@0: Chris@0: // Remove the term's URL alias. Chris@0: $edit3 = []; Chris@0: $edit3['path[0][alias]'] = ''; Chris@0: $this->drupalPostForm('taxonomy/term/' . $tid . '/edit', $edit3, t('Save')); Chris@0: Chris@0: // Confirm that the alias no longer works. Chris@0: $this->drupalGet(trim($edit2['path[0][alias]'], '/')); Chris@0: $this->assertNoText($description, 'Old URL alias has been removed after altering.'); Chris@0: $this->assertResponse(404, 'Old URL alias returns 404.'); Chris@0: } Chris@0: Chris@0: }