PathTaxonomyTermTest.php 3.1 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\path\Tests;

5 6
use Drupal\taxonomy\Entity\Vocabulary;

7 8
/**
 * Tests URL aliases for taxonomy terms.
9 10
 *
 * @group path
11 12
 */
class PathTaxonomyTermTest extends PathTestBase {
13 14 15 16 17 18 19 20

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('taxonomy');

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 30
    $vocabulary->save();

31
    // Create and log in user.
32 33 34 35 36 37 38 39 40
    $web_user = $this->drupalCreateUser(array('administer url aliases', 'administer taxonomy', 'access administration pages'));
    $this->drupalLogin($web_user);
  }

  /**
   * Tests alias functionality through the admin interfaces.
   */
  function testTermAlias() {
    // Create a term in the default 'Tags' vocabulary with URL alias.
41
    $vocabulary = Vocabulary::load('tags');
42
    $description = $this->randomMachineName();
43
    $edit = array(
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", array(':name' => $edit['name[0][value]']))->fetchField();
50 51

    // Confirm that the alias works.
52
    $this->drupalGet($edit['path[0][alias]']);
53 54
    $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 58 59 60
    $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.');

61 62
    // Change the term's URL alias.
    $edit2 = array();
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 69 70
    $this->assertText($description, 'Term can be accessed on changed URL alias.');

    // Confirm that the old alias no longer works.
71
    $this->drupalGet(trim($edit['path[0][alias]'], '/'));
72 73 74 75 76
    $this->assertNoText($description, 'Old URL alias has been removed after altering.');
    $this->assertResponse(404, 'Old URL alias returns 404.');

    // Remove the term's URL alias.
    $edit3 = array();
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 83 84 85
    $this->assertNoText($description, 'Old URL alias has been removed after altering.');
    $this->assertResponse(404, 'Old URL alias returns 404.');
  }
}