TaxonomyTermFormTest.php in URL Alias Permissions 8
File
tests/src/Functional/TaxonomyTermFormTest.php
View source
<?php
namespace Drupal\Tests\url_alias_permissions\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
class TaxonomyTermFormTest extends BrowserTestBase {
use TaxonomyTestTrait;
protected static $modules = [
'url_alias_permissions_test',
'taxonomy',
];
protected $defaultTheme = 'stark';
protected $vocabulary;
protected $userWithUrlAliasPermissions;
protected $userWithoutUrlAliasPermissions;
protected $taxonomyTermStorage;
protected function setUp() : void {
parent::setup();
$this->vocabulary = $this
->createVocabulary();
$this->userWithUrlAliasPermissions = $this
->drupalCreateUser([
'create terms in ' . $this->vocabulary
->id(),
'edit terms in ' . $this->vocabulary
->id(),
'create url aliases',
'edit ' . $this->vocabulary
->id() . ' taxonomy_term url alias',
]);
$this->userWithoutUrlAliasPermissions = $this
->drupalCreateUser([
'create terms in ' . $this->vocabulary
->id(),
'edit terms in ' . $this->vocabulary
->id(),
]);
$this->taxonomyTermStorage = $this->container
->get('entity_type.manager')
->getStorage('taxonomy_term');
}
public function testPathAliasFormElementAccess() {
$this
->drupalLogin($this->userWithUrlAliasPermissions);
$this
->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary
->id() . '/add');
$this
->assertSession()
->fieldExists('path[0][alias]');
$name = $this
->randomMachineName();
$this
->submitForm([
'name[0][value]' => $name,
'path[0][alias]' => '/test-alias',
], 'Save');
$term = $this
->getTermByName($name, $this->vocabulary
->id());
$this
->assertEquals('/test-alias', $term
->get('path')->alias);
$this
->drupalGet('taxonomy/term/' . $term
->id() . '/edit');
$this
->assertSession()
->fieldExists('path[0][alias]');
$this
->drupalLogin($this->userWithoutUrlAliasPermissions);
$this
->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary
->id() . '/add');
$this
->assertSession()
->fieldNotExists('path[0][alias]');
$this
->drupalGet('taxonomy/term/' . $term
->id() . '/edit');
$this
->assertSession()
->fieldNotExists('path[0][alias]');
$this
->submitForm([], 'Save');
$term = $this
->getTermByName($name, $this->vocabulary
->id(), TRUE);
$this
->assertEquals('/test-alias', $term
->get('path')->alias);
}
protected function getTermByName(string $name, string $vocabulary_id, bool $reset = FALSE) {
if ($reset === TRUE) {
$this->taxonomyTermStorage
->resetCache();
}
$terms = $this->taxonomyTermStorage
->loadByProperties([
'name' => trim($name),
'vid' => $vocabulary_id,
]);
return current($terms);
}
}