You are here

public function ProtectionRuleUnitTest::testIsProtected in User protect 8

Tests isProtected().

File

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

Class

ProtectionRuleUnitTest
Various unit tests for protection rules.

Namespace

Drupal\Tests\userprotect\Kernel\Entity

Code

public function testIsProtected() {

  // 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);

  // Create an operating account.
  $account = new UserSession();

  // Assert that the operation is protected on the user with the administrator
  // role and not on the authenticated user.
  $this
    ->assertTrue($this->protectionRule
    ->isProtected($lorem, 'user_mail', $account));
  $this
    ->assertFalse($this->protectionRule
    ->isProtected($ipsum, 'user_mail', $account));
}