EntityWithoutBundleFormTest.php in URL Alias Permissions 8
File
tests/src/Functional/EntityWithoutBundleFormTest.php
View source
<?php
namespace Drupal\Tests\url_alias_permissions\Functional;
use Drupal\Tests\BrowserTestBase;
class EntityWithoutBundleFormTest extends BrowserTestBase {
protected static $modules = [
'url_alias_permissions_test',
'entity_test',
];
protected $defaultTheme = 'stark';
protected $userWithUrlAliasPermissions;
protected $userWithoutUrlAliasPermissions;
protected $entityTestStorage;
protected function setUp() : void {
parent::setup();
$this->userWithUrlAliasPermissions = $this
->drupalCreateUser([
'administer entity_test content',
'create url aliases',
'edit entity_test url alias',
'view test entity',
]);
$this->userWithoutUrlAliasPermissions = $this
->drupalCreateUser([
'administer entity_test content',
'view test entity',
]);
$this->entityTestStorage = $this->container
->get('entity_type.manager')
->getStorage('entity_test');
}
public function testPathAliasFormElementAccess() {
$this
->drupalLogin($this->userWithUrlAliasPermissions);
$this
->drupalGet('/entity_test/add');
$this
->assertSession()
->fieldExists('url_alias[0][alias]');
$name = $this
->randomMachineName();
$this
->submitForm([
'name[0][value]' => $name,
'url_alias[0][alias]' => '/test-alias',
], 'Save');
$entity = $this
->getEntityTestByName($name);
$this
->assertEquals('/test-alias', $entity
->get('url_alias')->alias);
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$this
->assertSession()
->fieldExists('url_alias[0][alias]');
$this
->drupalLogin($this->userWithoutUrlAliasPermissions);
$this
->drupalGet('/entity_test/add');
$this
->assertSession()
->fieldNotExists('url_alias[0][alias]');
$this
->drupalGet('entity_test/manage/' . $entity
->id() . '/edit');
$this
->assertSession()
->fieldNotExists('url_alias[0][alias]');
$this
->submitForm([], 'Save');
$entity = $this
->getEntityTestByName($name);
$this
->assertEquals('/test-alias', $entity
->get('url_alias')->alias);
}
protected function getEntityTestByName(string $name, bool $reset = FALSE) {
if ($reset === TRUE) {
$this->entityTestStorage
->resetCache();
}
$entities = $this->entityTestStorage
->loadByProperties([
'name' => $name,
]);
return current($entities);
}
}