Commit e3e12db3 authored by alexpott's avatar alexpott

Issue #2044859 by Berdir, klausi: Convert user roles to entity_reference_field.

parent 52a101bc
......@@ -201,7 +201,7 @@ function testMultiValuedWidget() {
*/
function testPrivateFileSetting() {
// Grant the admin user required permissions.
user_role_grant_permissions($this->admin_user->roles[0]->value, array('administer node fields'));
user_role_grant_permissions($this->admin_user->roles[0]->target_id, array('administer node fields'));
$type_name = 'article';
$field_name = strtolower($this->randomMachineName());
......
......@@ -174,8 +174,8 @@ public function getRoles($exclude_locked_roles = FALSE) {
$roles = array();
foreach ($this->get('roles') as $role) {
if (!($exclude_locked_roles && in_array($role->value, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID)))) {
$roles[] = $role->value;
if (!($exclude_locked_roles && in_array($role->target_id, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID)))) {
$roles[] = $role->target_id;
}
}
......@@ -531,13 +531,12 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setDescription(t('The email address used for initial account creation.'))
->setDefaultValue('');
// @todo Convert this to entity_reference_field, see
// https://drupal.org/node/2044859.
$fields['roles'] = BaseFieldDefinition::create('string')
$fields['roles'] = BaseFieldDefinition::create('entity_reference')
->setCustomStorage(TRUE)
->setLabel(t('Roles'))
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
->setDescription(t('The roles the user has.'));
->setDescription(t('The roles the user has.'))
->setSetting('target_type', 'user_role');
return $fields;
}
......
......@@ -11,6 +11,8 @@
use Drupal\Core\Field\Plugin\Field\FieldType\EmailItem;
use Drupal\Core\Render\Element\Email;
use Drupal\simpletest\DrupalUnitTestBase;
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;
/**
* Verify that user validity checks behave as designed.
......@@ -33,6 +35,10 @@ protected function setUp() {
parent::setUp();
$this->installEntitySchema('user');
$this->installSchema('system', array('sequences'));
// Make sure that the default roles exist.
$this->installConfig(array('user'));
}
/**
......@@ -68,7 +74,7 @@ function testUsernames() {
* Runs entity validation checks.
*/
function testValidation() {
$user = entity_create('user', array('name' => 'test'));
$user = User::create(array('name' => 'test'));
$violations = $user->validate();
$this->assertEqual(count($violations), 0, 'No violations when validating a default user.');
......@@ -137,6 +143,9 @@ function testValidation() {
$this->assertEqual($violations[0]->getPropertyPath(), 'init.0.value');
$this->assertEqual($violations[0]->getMessage(), t('This value is not a valid email address.'));
Role::create(array('id' => 'role1'))->save();
Role::create(array('id' => 'role2'))->save();
// Test cardinality of user roles.
$user = entity_create('user', array(
'name' => 'role_test',
......@@ -144,8 +153,12 @@ function testValidation() {
));
$violations = $user->validate();
$this->assertEqual(count($violations), 0);
// @todo Test user role validation once https://drupal.org/node/2044859 got
// committed.
$user->roles[1]->target_id = 'unknown_role';
$violations = $user->validate();
$this->assertEqual(count($violations), 1);
$this->assertEqual($violations[0]->getPropertyPath(), 'roles.1');
$this->assertEqual($violations[0]->getMessage(), t('The referenced entity (%entity_type: %name) does not exist.', array('%entity_type' => 'user_role', '%name' => 'unknown_role')));
}
/**
......
......@@ -31,7 +31,7 @@ protected function createUserSession(array $rids = array()) {
$roles = array();
foreach ($rids as $rid) {
$roles[] = (object) array(
'value' => $rid,
'target_id' => $rid,
);
}
$user->expects($this->any())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment