PathTaxonomyTermTest.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace Drupal\Tests\path\Functional;
  3. use Drupal\taxonomy\Entity\Vocabulary;
  4. /**
  5. * Tests URL aliases for taxonomy terms.
  6. *
  7. * @group path
  8. */
  9. class PathTaxonomyTermTest extends PathTestBase {
  10. /**
  11. * Modules to enable.
  12. *
  13. * @var array
  14. */
  15. public static $modules = ['taxonomy'];
  16. protected function setUp() {
  17. parent::setUp();
  18. // Create a Tags vocabulary for the Article node type.
  19. $vocabulary = Vocabulary::create([
  20. 'name' => t('Tags'),
  21. 'vid' => 'tags',
  22. ]);
  23. $vocabulary->save();
  24. // Create and log in user.
  25. $web_user = $this->drupalCreateUser(['administer url aliases', 'administer taxonomy', 'access administration pages']);
  26. $this->drupalLogin($web_user);
  27. }
  28. /**
  29. * Tests alias functionality through the admin interfaces.
  30. */
  31. public function testTermAlias() {
  32. // Create a term in the default 'Tags' vocabulary with URL alias.
  33. $vocabulary = Vocabulary::load('tags');
  34. $description = $this->randomMachineName();
  35. $edit = [
  36. 'name[0][value]' => $this->randomMachineName(),
  37. 'description[0][value]' => $description,
  38. 'path[0][alias]' => '/' . $this->randomMachineName(),
  39. ];
  40. $this->drupalPostForm('admin/structure/taxonomy/manage/' . $vocabulary->id() . '/add', $edit, t('Save'));
  41. $tid = db_query("SELECT tid FROM {taxonomy_term_field_data} WHERE name = :name AND default_langcode = 1", [':name' => $edit['name[0][value]']])->fetchField();
  42. // Confirm that the alias works.
  43. $this->drupalGet($edit['path[0][alias]']);
  44. $this->assertText($description, 'Term can be accessed on URL alias.');
  45. // Confirm the 'canonical' and 'shortlink' URLs.
  46. $elements = $this->xpath("//link[contains(@rel, 'canonical') and contains(@href, '" . $edit['path[0][alias]'] . "')]");
  47. $this->assertTrue(!empty($elements), 'Term page contains canonical link URL.');
  48. $elements = $this->xpath("//link[contains(@rel, 'shortlink') and contains(@href, 'taxonomy/term/" . $tid . "')]");
  49. $this->assertTrue(!empty($elements), 'Term page contains shortlink URL.');
  50. // Change the term's URL alias.
  51. $edit2 = [];
  52. $edit2['path[0][alias]'] = '/' . $this->randomMachineName();
  53. $this->drupalPostForm('taxonomy/term/' . $tid . '/edit', $edit2, t('Save'));
  54. // Confirm that the changed alias works.
  55. $this->drupalGet(trim($edit2['path[0][alias]'], '/'));
  56. $this->assertText($description, 'Term can be accessed on changed URL alias.');
  57. // Confirm that the old alias no longer works.
  58. $this->drupalGet(trim($edit['path[0][alias]'], '/'));
  59. $this->assertNoText($description, 'Old URL alias has been removed after altering.');
  60. $this->assertResponse(404, 'Old URL alias returns 404.');
  61. // Remove the term's URL alias.
  62. $edit3 = [];
  63. $edit3['path[0][alias]'] = '';
  64. $this->drupalPostForm('taxonomy/term/' . $tid . '/edit', $edit3, t('Save'));
  65. // Confirm that the alias no longer works.
  66. $this->drupalGet(trim($edit2['path[0][alias]'], '/'));
  67. $this->assertNoText($description, 'Old URL alias has been removed after altering.');
  68. $this->assertResponse(404, 'Old URL alias returns 404.');
  69. }
  70. }