You are here

public function ProtectionRuleUnitTest::testAppliesTo in User protect 8

Tests appliesTo().

File

tests/src/Kernel/Entity/ProtectionRuleUnitTest.php, line 153

Class

ProtectionRuleUnitTest
Various unit tests for protection rules.

Namespace

Drupal\Tests\userprotect\Kernel\Entity

Code

public function testAppliesTo() {

  // Create an user with administrator role.
  $values = [
    'uid' => 3,
    'name' => 'lorem',
    'roles' => [
      'administrator',
    ],
  ];
  $lorem = User::create($values);

  // Create an authenticated user.
  $values = [
    'uid' => 4,
    'name' => 'ipsum',
  ];
  $ipsum = User::create($values);

  // Assert that the protection rule applies to the user with the
  // administrator role and not to the authenticated user.
  $this
    ->assertTrue($this->protectionRule
    ->appliesTo($lorem));
  $this
    ->assertFalse($this->protectionRule
    ->appliesTo($ipsum));

  // Create an user based protection rule.
  $user_protection_rule = ProtectionRule::create([
    'name' => 'dummy',
    'label' => 'Dummy',
    'protections' => [
      'user_mail' => [
        'status' => TRUE,
      ],
    ],
    'protectedEntityTypeId' => 'user',
    'protectedEntityId' => 4,
  ]);

  // Assert that the protection rule applies to "ipsum", but no to "lorem".
  $this
    ->assertFalse($user_protection_rule
    ->appliesTo($lorem));
  $this
    ->assertTrue($user_protection_rule
    ->appliesTo($ipsum));
}