public function ProtectionRuleCrudTest::testCrudUserProtectionRule in User protect 8
Tests if user based protection rules can be created through the UI.
File
- tests/
src/ Functional/ ProtectionRuleCrudTest.php, line 105
Class
- ProtectionRuleCrudTest
- Tests creating, editing and deleting protection rules through the UI.
Namespace
Drupal\Tests\userprotect\FunctionalCode
public function testCrudUserProtectionRule() {
$account = $this
->drupalCreateUser();
$rule_id = strtolower($this
->randomMachineName());
$label = $this
->randomMachineName();
// Create rule.
$edit = [
'label' => $label,
'name' => $rule_id,
'entity_id' => $account
->getAccountName(),
'protection[user_mail]' => TRUE,
];
$this
->drupalPostForm('admin/config/people/userprotect/add/user', $edit, t('Save'));
// Assert that the rule was created.
$protection_rule = ProtectionRule::load($rule_id);
$this
->assertInstanceOf(ProtectionRuleInterface::class, $protection_rule, 'A protection rule was created through the UI.');
// Assert that the rule has the expected values.
$this
->assertEquals($rule_id, $protection_rule
->id());
$this
->assertEquals($label, $protection_rule->label);
$this
->assertEquals('user', $protection_rule
->getProtectedEntityTypeId());
$this
->assertEquals($account
->id(), $protection_rule
->getProtectedEntityId());
$enabled_plugins = $protection_rule
->getProtections()
->getEnabledPlugins();
$this
->assertCount(1, $enabled_plugins, 'One plugin was enabled.');
$plugin = reset($enabled_plugins);
$this
->assertEquals('user_mail', $plugin
->getPluginId());
// Edit rule.
$edit = [
'protection[user_name]' => TRUE,
'protection[user_mail]' => FALSE,
];
$this
->drupalPostForm('admin/config/people/userprotect/manage/' . $rule_id, $edit, t('Save'));
// Assert that the rule was updated with the expected values.
$protection_rule = ProtectionRule::load($rule_id);
$this
->assertEquals($rule_id, $protection_rule
->id());
$this
->assertEquals($label, $protection_rule->label);
$this
->assertEquals('user', $protection_rule
->getProtectedEntityTypeId());
$this
->assertEquals($account
->id(), $protection_rule
->getProtectedEntityId());
$enabled_plugins = $protection_rule
->getProtections()
->getEnabledPlugins();
$this
->assertCount(1, $enabled_plugins, 'One plugin was enabled.');
$plugin = reset($enabled_plugins);
$this
->assertEquals('user_name', $plugin
->getPluginId());
// Attempt to create a rule with the same name.
$edit = [
'label' => $label,
'name' => $rule_id,
'entity_id' => $account
->getAccountName(),
'protection[user_mail]' => TRUE,
];
$this
->drupalPostForm('admin/config/people/userprotect/add/user', $edit, t('Save'));
$this
->assertSession()
->pageTextContains('The machine-readable name is already in use. It must be unique.');
// Assert only one protection rule exists.
$entities = ProtectionRule::loadMultiple(NULL);
$this
->assertCount(1, $entities, 'Only one protection rule exists.');
// Delete rule.
$this
->drupalPostForm('admin/config/people/userprotect/manage/' . $rule_id . '/delete', [], t('Delete'));
// Assert the rule no longer exists.
$protection_rule = ProtectionRule::load($rule_id);
$this
->assertEmpty($protection_rule, 'The protection rule was deleted.');
}