View source
<?php
namespace Drupal\Tests\userprotect\Kernel\Entity;
use Drupal\user\Entity\User;
use Drupal\Core\Session\UserSession;
use Drupal\userprotect\Entity\ProtectionRule;
use Drupal\userprotect\Entity\ProtectionRuleInterface;
use Drupal\userprotect\Plugin\UserProtection\UserProtectionInterface;
use Drupal\userprotect\UserProtect;
use Drupal\userprotect\Plugin\UserProtection\UserProtectionPluginCollection;
use Drupal\KernelTests\KernelTestBase;
class ProtectionRuleUnitTest extends KernelTestBase {
public static $modules = [
'user',
'userprotect',
'field',
];
protected $manager;
protected $protectionRule;
protected function setUp() {
parent::setUp();
$this->manager = UserProtect::pluginManager();
$this->protectionRule = ProtectionRule::create([
'name' => 'dummy',
'label' => 'Dummy',
'protections' => [
'user_mail' => [
'status' => TRUE,
],
],
'protectedEntityTypeId' => 'user_role',
'protectedEntityId' => 'administrator',
]);
}
public function testId() {
$this
->assertIdentical('dummy', $this->protectionRule
->id());
}
public function testProtectedEntityTypeId() {
$this
->assertIdentical('user_role', $this->protectionRule
->getProtectedEntityTypeId());
$entity_type = 'user';
$this
->assertInstanceOf(ProtectionRuleInterface::class, $this->protectionRule
->setProtectedEntityTypeId($entity_type));
$this
->assertIdentical($entity_type, $this->protectionRule
->getProtectedEntityTypeId());
}
public function testProtectedEntityId() {
$this
->assertIdentical('administrator', $this->protectionRule
->getProtectedEntityId());
$entity_id = 'authenticated';
$this
->assertInstanceOf(ProtectionRuleInterface::class, $this->protectionRule
->setProtectedEntityId($entity_id));
$this
->assertIdentical($entity_id, $this->protectionRule
->getProtectedEntityId());
}
public function testBypassRoles() {
$this
->assertIdentical([], $this->protectionRule
->getBypassRoles());
$roles = [
'administrator',
];
$this
->assertInstanceOf(ProtectionRuleInterface::class, $this->protectionRule
->setBypassRoles($roles));
$this
->assertIdentical($roles, $this->protectionRule
->getBypassRoles());
}
public function testGetProtection() {
$this
->assertInstanceOf(UserProtectionInterface::class, $this->protectionRule
->getProtection('user_mail'));
}
public function testGetProtections() {
$this
->assertInstanceOf(UserProtectionPluginCollection::class, $this->protectionRule
->getProtections());
}
public function testEnableProtection() {
$this
->assertInstanceOf(ProtectionRuleInterface::class, $this->protectionRule
->enableProtection('user_name'));
$this
->assertTrue($this->protectionRule
->hasProtection('user_name'));
}
public function testDisableProtection() {
$this
->assertInstanceOf(ProtectionRuleInterface::class, $this->protectionRule
->disableProtection('user_mail'));
$this
->assertFalse($this->protectionRule
->hasProtection('user_mail'));
}
public function testToArray() {
$array = $this->protectionRule
->toArray();
$this
->assertIdentical('dummy', $array['name']);
$this
->assertIdentical('Dummy', $array['label']);
$expected_protections = [
'user_mail' => [
'status' => TRUE,
],
];
$this
->assertIdentical($expected_protections, $array['protections']);
$this
->assertIdentical('user_role', $array['protectedEntityTypeId']);
$this
->assertIdentical('administrator', $array['protectedEntityId']);
}
public function testGetPermissionName() {
$this
->assertIdentical('userprotect.dummy.bypass', $this->protectionRule
->getPermissionName());
}
public function testAppliesTo() {
$values = [
'uid' => 3,
'name' => 'lorem',
'roles' => [
'administrator',
],
];
$lorem = User::create($values);
$values = [
'uid' => 4,
'name' => 'ipsum',
];
$ipsum = User::create($values);
$this
->assertTrue($this->protectionRule
->appliesTo($lorem));
$this
->assertFalse($this->protectionRule
->appliesTo($ipsum));
$user_protection_rule = ProtectionRule::create([
'name' => 'dummy',
'label' => 'Dummy',
'protections' => [
'user_mail' => [
'status' => TRUE,
],
],
'protectedEntityTypeId' => 'user',
'protectedEntityId' => 4,
]);
$this
->assertFalse($user_protection_rule
->appliesTo($lorem));
$this
->assertTrue($user_protection_rule
->appliesTo($ipsum));
}
public function testHasProtection() {
$this
->assertTrue($this->protectionRule
->hasProtection('user_mail'));
$this
->assertFalse($this->protectionRule
->hasProtection('user_name'));
$this
->assertFalse($this->protectionRule
->hasProtection('non_existing_plugin_id'));
}
public function testIsProtected() {
$values = [
'uid' => 3,
'name' => 'lorem',
'roles' => [
'administrator',
],
];
$lorem = User::create($values);
$values = [
'uid' => 4,
'name' => 'ipsum',
];
$ipsum = User::create($values);
$account = new UserSession();
$this
->assertTrue($this->protectionRule
->isProtected($lorem, 'user_mail', $account));
$this
->assertFalse($this->protectionRule
->isProtected($ipsum, 'user_mail', $account));
}
}