View source
<?php
namespace Drupal\Tests\bamboo_twig\Kernel;
use Drupal\KernelTests\KernelTestBase;
class BambooTwigSecurityTest extends KernelTestBase {
protected $entityTypeManager;
protected static $modules = [
'system',
'user',
'bamboo_twig',
'bamboo_twig_security',
];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('user');
$this
->installSchema('system', [
'sequences',
]);
$this->entityTypeManager = $this->container
->get('entity_type.manager');
$this->securityExtension = $this->container
->get('bamboo_twig_security.twig.security');
$adminUser = $this->entityTypeManager
->getStorage('user')
->create([
'uid' => 1,
'mail' => 'admin',
'name' => 'admin',
'status' => 1,
]);
$adminUser
->addRole('administrator');
$adminUser
->save();
$adminUser = $this->entityTypeManager
->getStorage('user')
->create([
'uid' => 2,
'mail' => 'anonymous',
'name' => 'anonymous',
'status' => 1,
]);
$adminUser
->save();
}
public function testHasPermissions() {
$result = $this->securityExtension
->hasPermissions([
'bypass node access',
], 'OR', 1);
$this
->assertTrue($result);
}
public function testHasNotPermissions() {
$result = $this->securityExtension
->hasPermissions([
'bypass node access',
], 'OR', 2);
$this
->assertFalse($result);
}
public function testHasRoles() {
$result = $this->securityExtension
->hasRoles([
'administrator',
], 'OR', 1);
$this
->assertTrue($result);
}
public function testHasNotRoles() {
$result = $this->securityExtension
->hasRoles([
'administrator',
], 'OR', 2);
$this
->assertFalse($result);
}
public function testHasPermissionsInvalidConjunction() {
$this
->expectException(\InvalidArgumentException::class);
$this
->expectExceptionMessage('Invalid conjunction type "XOR".');
$this->securityExtension
->hasPermissions([
'bypass node access',
], 'XOR', 1);
}
public function testHasRolesInvalidConjunction() {
$this
->expectException(\InvalidArgumentException::class);
$this
->expectExceptionMessage('Invalid conjunction type "XOR".');
$this->securityExtension
->hasRoles([
'administrator',
], 'XOR', 1);
}
}