UserEntityReferenceTest.php 2.62 KB
Newer Older
1 2 3 4 5 6 7 8
<?php
/**
 * @file
 * Contains \Drupal\user\Tests\UserEntityReferenceTest.
 */

namespace Drupal\user\Tests;

9
use Drupal\field\Entity\FieldConfig;
10 11 12
use Drupal\system\Tests\Entity\EntityUnitTestBase;

/**
13 14 15
 * Tests the user reference field functionality.
 *
 * @group user
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
 */
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}
   */
39
  protected function setUp() {
40 41 42
    parent::setUp();

    $this->role1 = entity_create('user_role', array(
43 44
      'id' => strtolower($this->randomMachineName(8)),
      'label' => $this->randomMachineName(8),
45 46 47 48
    ));
    $this->role1->save();

    $this->role2 = entity_create('user_role', array(
49 50
      'id' => strtolower($this->randomMachineName(8)),
      'label' => $this->randomMachineName(8),
51 52 53
    ));
    $this->role2->save();

54
    entity_reference_create_field('user', 'user', 'user_reference', 'User reference', 'user');
55 56 57 58 59 60
  }

  /**
   * Tests user selection by roles.
   */
  function testUserSelectionByRole() {
61
    $field_definition = FieldConfig::loadByName('user', 'user', 'user_reference');
62
    $field_definition->settings['handler_settings']['filter']['role'] = array(
63 64 65
      $this->role1->id() => $this->role1->id(),
      $this->role2->id() => 0,
    );
66 67
    $field_definition->settings['handler_settings']['filter']['type'] = 'role';
    $field_definition->save();
68 69 70 71 72 73 74 75 76 77 78 79 80

    $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();

81

82 83 84
    /** @var \Drupal\entity_reference\EntityReferenceAutocomplete $autocomplete */
    $autocomplete = \Drupal::service('entity_reference.autocomplete');

85
    $matches = $autocomplete->getMatches($field_definition, 'user', 'user', 'NULL', '', 'aabb');
86 87 88 89 90 91 92 93 94
    $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));

95
    $matches = $autocomplete->getMatches($field_definition, 'user', 'user', 'NULL', '', 'aabbbb');
96 97 98
    $this->assertEqual(count($matches), 0, '');
  }
}