function PathTaxonomyTermTest::testTermAlias

Tests alias functionality through the admin interfaces.

File

drupal/core/modules/path/lib/Drupal/path/Tests/PathTaxonomyTermTest.php, line 48
Definition of Drupal\path\Tests\PathTaxonomyTermTest.

Class

PathTaxonomyTermTest
Tests URL aliases for taxonomy terms.

Namespace

Drupal\path\Tests

Code

function testTermAlias() {

  // Create a term in the default 'Tags' vocabulary with URL alias.
  $vocabulary = taxonomy_vocabulary_load(1);
  $description = $this
    ->randomName();
  $edit = array();
  $edit['name'] = $this
    ->randomName();
  $edit['description[value]'] = $description;
  $edit['path[alias]'] = $this
    ->randomName();
  $this
    ->drupalPost('admin/structure/taxonomy/' . $vocabulary->machine_name . '/add', $edit, t('Save'));

  // Confirm that the alias works.
  $this
    ->drupalGet($edit['path[alias]']);
  $this
    ->assertText($description, 'Term can be accessed on URL alias.');

  // Change the term's URL alias.
  $tid = db_query("SELECT tid FROM {taxonomy_term_data} WHERE name = :name", array(
    ':name' => $edit['name'],
  ))
    ->fetchField();
  $edit2 = array();
  $edit2['path[alias]'] = $this
    ->randomName();
  $this
    ->drupalPost('taxonomy/term/' . $tid . '/edit', $edit2, t('Save'));

  // Confirm that the changed alias works.
  $this
    ->drupalGet($edit2['path[alias]']);
  $this
    ->assertText($description, 'Term can be accessed on changed URL alias.');

  // Confirm that the old alias no longer works.
  $this
    ->drupalGet($edit['path[alias]']);
  $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();
  $edit3['path[alias]'] = '';
  $this
    ->drupalPost('taxonomy/term/' . $tid . '/edit', $edit3, t('Save'));

  // Confirm that the alias no longer works.
  $this
    ->drupalGet($edit2['path[alias]']);
  $this
    ->assertNoText($description, 'Old URL alias has been removed after altering.');
  $this
    ->assertResponse(404, 'Old URL alias returns 404.');
}