You are here

protected function UserProtectCreationTrait::createProtectionRule in User protect 8

Creates protection rule.

Parameters

int|string $entity_id: The id of the entity to protect.

array $protections: (optional) The active protections. Defaults to an empty array.

string $entity_type: (optional) The protected entity type. Defaults to "user_role".

array $values: (optional) Extra values of the protection rule.

Return value

\Drupal\userprotect\Entity\ProtectionRuleInterface An instance of ProtectionRuleInterface.

6 calls to UserProtectCreationTrait::createProtectionRule()
ProtectedEntityDeleteTest::testNonExistentProtectedRole in tests/src/Functional/ProtectedEntityDeleteTest.php
Tests UI for non-existent protected roles.
ProtectedEntityDeleteTest::testNonExistentProtectedUser in tests/src/Functional/ProtectedEntityDeleteTest.php
Tests UI for non-existent protected users.
ProtectedEntityDeleteTest::testRoleDelete in tests/src/Kernel/ProtectedEntityDeleteTest.php
Tests reaction upon role deletion.
ProtectedEntityDeleteTest::testUserDelete in tests/src/Kernel/ProtectedEntityDeleteTest.php
Tests reaction upon user deletion.
UserProtectCreationTrait::createProtectedRole in tests/src/Traits/UserProtectCreationTrait.php
Creates a protected role.

... See full list

File

tests/src/Traits/UserProtectCreationTrait.php, line 100

Class

UserProtectCreationTrait
Provides methods to create protection rules.

Namespace

Drupal\Tests\userprotect\Traits

Code

protected function createProtectionRule($entity_id, array $protections = [], $entity_type = 'user_role', array $values = []) {

  // Setup default values.
  $values += [
    'name' => 'dummy',
    'label' => 'Dummy',
    'protections' => [],
    'protectedEntityTypeId' => $entity_type,
    'protectedEntityId' => $entity_id,
  ];

  // Define protections.
  foreach ($protections as $key) {
    $values['protections'][$key] = [
      'status' => TRUE,
    ];
  }

  // Create protection rule.
  $protection_rule = ProtectionRule::create($values);
  $this
    ->assertInstanceOf(ProtectionRuleInterface::class, $protection_rule);
  return $protection_rule;
}