summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2014-04-18 12:51:36 (GMT)
committerAlex Pott2014-04-18 12:51:36 (GMT)
commitc1b72d4e02cab59130445ce702375b8e89b6af70 (patch)
tree36e7bdfb7d7c5b8408d20528ddd10e925520270b
parentbd468f23ad01bda970e18be7845663b7c0f657ce (diff)
Issue #2242967 by blueminds: User entity reference filter configuration broken.
-rw-r--r--core/modules/user/lib/Drupal/user/Plugin/entity_reference/selection/UserSelection.php4
-rw-r--r--core/modules/user/lib/Drupal/user/Tests/UserEntityReferenceTest.php106
2 files changed, 108 insertions, 2 deletions
diff --git a/core/modules/user/lib/Drupal/user/Plugin/entity_reference/selection/UserSelection.php b/core/modules/user/lib/Drupal/user/Plugin/entity_reference/selection/UserSelection.php
index 71f0c3f..2c3fd93 100644
--- a/core/modules/user/lib/Drupal/user/Plugin/entity_reference/selection/UserSelection.php
+++ b/core/modules/user/lib/Drupal/user/Plugin/entity_reference/selection/UserSelection.php
@@ -133,8 +133,8 @@ class UserSelection extends SelectionBase {
}
// Add the filter by role option.
- if (!empty($this->instance['settings']['handler_settings']['filter'])) {
- $filter_settings = $this->instance['settings']['handler_settings']['filter'];
+ if (!empty($this->fieldDefinition->getSetting('handler_settings')['filter'])) {
+ $filter_settings = $this->fieldDefinition->getSetting('handler_settings')['filter'];
if ($filter_settings['type'] == 'role') {
$tables = $query->getTables();
$base_table = $tables['base_table']['alias'];
diff --git a/core/modules/user/lib/Drupal/user/Tests/UserEntityReferenceTest.php b/core/modules/user/lib/Drupal/user/Tests/UserEntityReferenceTest.php
new file mode 100644
index 0000000..c5e8ae9
--- /dev/null
+++ b/core/modules/user/lib/Drupal/user/Tests/UserEntityReferenceTest.php
@@ -0,0 +1,106 @@
+<?php
+/**
+ * @file
+ * Contains \Drupal\user\Tests\UserEntityReferenceTest.
+ */
+
+namespace Drupal\user\Tests;
+
+use Drupal\field\Field;
+use Drupal\system\Tests\Entity\EntityUnitTestBase;
+
+/**
+ * User entity reference test cases.
+ */
+class UserEntityReferenceTest extends EntityUnitTestBase {
+
+ /**
+ * @var \Drupal\user\Entity\Role
+ */
+ protected $role1;
+
+ /**
+ * @var \Drupal\user\Entity\Role
+ */
+ protected $role2;
+
+ /**
+ * Modules to enable.
+ *
+ * @var array
+ */
+ public static $modules = array('entity_reference', 'user');
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function getInfo() {
+ return array(
+ 'name' => 'User entity reference',
+ 'description' => 'Tests the user reference field functionality.',
+ 'group' => 'User',
+ );
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ function setUp() {
+ parent::setUp();
+
+ $this->role1 = entity_create('user_role', array(
+ 'id' => strtolower($this->randomName(8)),
+ 'label' => $this->randomName(8),
+ ));
+ $this->role1->save();
+
+ $this->role2 = entity_create('user_role', array(
+ 'id' => strtolower($this->randomName(8)),
+ 'label' => $this->randomName(8),
+ ));
+ $this->role2->save();
+
+ entity_reference_create_instance('user', 'user', 'user_reference', 'User reference', 'user');
+ }
+
+ /**
+ * Tests user selection by roles.
+ */
+ function testUserSelectionByRole() {
+ $fields = Field::fieldInfo()->getBundleInstances('user', 'user');
+ $fields['user_reference']->settings['handler_settings']['filter']['role'] = array(
+ $this->role1->id() => $this->role1->id(),
+ $this->role2->id() => 0,
+ );
+ $fields['user_reference']->settings['handler_settings']['filter']['type'] = 'role';
+ $fields['user_reference']->save();
+
+ $user1 = $this->createUser(array('name' => 'aabb'));
+ $user1->addRole($this->role1->id());
+ $user1->save();
+
+ $user2 = $this->createUser(array('name' => 'aabbb'));
+ $user2->addRole($this->role1->id());
+ $user2->save();
+
+ $user3 = $this->createUser(array('name' => 'aabbbb'));
+ $user3->addRole($this->role2->id());
+ $user3->save();
+
+ /** @var \Drupal\entity_reference\EntityReferenceAutocomplete $autocomplete */
+ $autocomplete = \Drupal::service('entity_reference.autocomplete');
+
+ $matches = $autocomplete->getMatches($fields['user_reference'], 'user', 'user', 'NULL', '', 'aabb');
+ $this->assertEqual(count($matches), 2);
+ $users = array();
+ foreach ($matches as $match) {
+ $users[] = $match['label'];
+ }
+ $this->assertTrue(in_array($user1->label(), $users));
+ $this->assertTrue(in_array($user2->label(), $users));
+ $this->assertFalse(in_array($user3->label(), $users));
+
+ $matches = $autocomplete->getMatches($fields['user_reference'], 'user', 'user', 'NULL', '', 'aabbbb');
+ $this->assertEqual(count($matches), 0, '');
+ }
+}