You are here

public function TaxonomyTermFormTest::testPathAliasFormElementAccess in URL Alias Permissions 8

Test if the path alias form elements is correctly hidden when no access.

File

tests/src/Functional/TaxonomyTermFormTest.php, line 82

Class

TaxonomyTermFormTest
Test url_alias_permissions with taxonomy terms.

Namespace

Drupal\Tests\url_alias_permissions\Functional

Code

public function testPathAliasFormElementAccess() {

  // Visit the taxonomy term add page and check that the path field is
  // present.
  $this
    ->drupalLogin($this->userWithUrlAliasPermissions);
  $this
    ->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary
    ->id() . '/add');
  $this
    ->assertSession()
    ->fieldExists('path[0][alias]');

  // Create a taxonomy term.
  $name = $this
    ->randomMachineName();
  $this
    ->submitForm([
    'name[0][value]' => $name,
    'path[0][alias]' => '/test-alias',
  ], 'Save');

  // Check that the URL alias was successfully saved.
  $term = $this
    ->getTermByName($name, $this->vocabulary
    ->id());
  $this
    ->assertEquals('/test-alias', $term
    ->get('path')->alias);

  // Visit the taxonomy term edit page and check that the path field is also
  // present.
  $this
    ->drupalGet('taxonomy/term/' . $term
    ->id() . '/edit');
  $this
    ->assertSession()
    ->fieldExists('path[0][alias]');

  // Log in as a user that doens't have access to edit the URL alias.
  $this
    ->drupalLogin($this->userWithoutUrlAliasPermissions);

  // Check that the field is not present.
  $this
    ->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary
    ->id() . '/add');
  $this
    ->assertSession()
    ->fieldNotExists('path[0][alias]');

  // Check that the field on the taxonomy term edit form is not present.
  $this
    ->drupalGet('taxonomy/term/' . $term
    ->id() . '/edit');
  $this
    ->assertSession()
    ->fieldNotExists('path[0][alias]');

  // Save the form and check that the URL alias still exists.
  $this
    ->submitForm([], 'Save');
  $term = $this
    ->getTermByName($name, $this->vocabulary
    ->id(), TRUE);
  $this
    ->assertEquals('/test-alias', $term
    ->get('path')->alias);
}