/** * Gets enabled protections for entity as a string. * * @param \Drupal\userprotect\Entity\ProtectionRuleInterface $entity * The entity the to get protections for. * * @return string * The enabled protections, comma-separated. */ public function getProtections(ProtectionRuleInterface $entity) { $all_protections = $entity->getProtections()->getAll(); $enabled_protections = $entity->getProtections()->getEnabledPlugins(); if (count($all_protections) == count($enabled_protections)) { return $this->t('All'); } return implode(', ', array_map(function ($item) { return $item->label(); }, $enabled_protections)); }
/** * Tests isProtected(). */ protected function testIsProtected() { // Create an user with administrator role. $values = array('uid' => 3, 'name' => 'lorem', 'roles' => array('administrator')); $lorem = entity_create('user', $values); // Create an authenticated user. $values = array('uid' => 4, 'name' => 'ipsum'); $ipsum = entity_create('user', $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)); }