PathTaxonomyTermTest.php in Drupal 9
File
core/modules/path/tests/src/Functional/PathTaxonomyTermTest.php
View source
<?php
namespace Drupal\Tests\path\Functional;
use Drupal\taxonomy\Entity\Vocabulary;
class PathTaxonomyTermTest extends PathTestBase {
protected static $modules = [
'taxonomy',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$vocabulary = Vocabulary::create([
'name' => t('Tags'),
'vid' => 'tags',
]);
$vocabulary
->save();
$web_user = $this
->drupalCreateUser([
'administer url aliases',
'administer taxonomy',
'access administration pages',
]);
$this
->drupalLogin($web_user);
}
public function testTermAlias() {
$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);
$this
->drupalGet($edit['path[0][alias]']);
$this
->assertSession()
->pageTextContains($description);
$elements = $this
->xpath("//link[contains(@rel, 'canonical') and contains(@href, '" . $edit['path[0][alias]'] . "')]");
$this
->assertTrue(!empty($elements), 'Term page contains canonical link URL.');
$elements = $this
->xpath("//link[contains(@rel, 'shortlink') and contains(@href, 'taxonomy/term/" . $tid . "')]");
$this
->assertTrue(!empty($elements), 'Term page contains shortlink URL.');
$edit2 = [];
$edit2['path[0][alias]'] = '/' . $this
->randomMachineName();
$this
->drupalGet('taxonomy/term/' . $tid . '/edit');
$this
->submitForm($edit2, 'Save');
$this
->drupalGet(trim($edit2['path[0][alias]'], '/'));
$this
->assertSession()
->pageTextContains($description);
$this
->drupalGet(trim($edit['path[0][alias]'], '/'));
$this
->assertSession()
->pageTextNotContains($description);
$this
->assertSession()
->statusCodeEquals(404);
$edit3 = [];
$edit3['path[0][alias]'] = '';
$this
->drupalGet('taxonomy/term/' . $tid . '/edit');
$this
->submitForm($edit3, 'Save');
$this
->drupalGet(trim($edit2['path[0][alias]'], '/'));
$this
->assertSession()
->pageTextNotContains($description);
$this
->assertSession()
->statusCodeEquals(404);
}
}