You are here

public function ProtectedEntityDeleteTest::testRoleDelete in User protect 8

Tests reaction upon role deletion.

Tests if a role based protection rule is cleaned up when the protected role is deleted.

File

tests/src/Kernel/ProtectedEntityDeleteTest.php, line 44

Class

ProtectedEntityDeleteTest
Tests if protection rules are cleaned up upon entity deletion.

Namespace

Drupal\Tests\userprotect\Kernel

Code

public function testRoleDelete() {

  // Create a role.
  $rid = $this
    ->drupalCreateRole([]);

  // Protect this role.
  $protection_rule = $this
    ->createProtectionRule($rid, [], 'user_role');
  $protection_rule
    ->save();

  // Assert that the rule was saved.
  $protection_rule = $this
    ->reloadEntity($protection_rule);
  $this
    ->assertNotNull($protection_rule, 'The protection rule was saved.');

  // Now delete the role.
  $role = \Drupal::entityTypeManager()
    ->getStorage('user_role')
    ->load($rid);
  $role
    ->delete();

  // Assert that the rule no longer exists.
  $protection_rule = $this
    ->reloadEntity($protection_rule);
  $this
    ->assertNull($protection_rule, 'The protection rule was deleted.');
}