You are here

public function entityReferenceSelectionAccessTest::testUserHandler in Entity reference 8

Test the user-specific overrides of the entity handler.

File

lib/Drupal/entityreference/Tests/entityReferenceSelectionAccessTest.php, line 168
Contains Drupal\entityreference\Tests\entityReferenceSelectionAccessTest.

Class

entityReferenceSelectionAccessTest
Test for Entity Reference handlers.

Namespace

Drupal\entityreference\Tests

Code

public function testUserHandler() {

  // Build a fake field instance.
  $field = array(
    'translatable' => FALSE,
    'entity_types' => array(),
    'settings' => array(
      'handler' => 'base',
      'target_type' => 'user',
      'handler_settings' => array(
        'target_bundles' => array(),
      ),
    ),
    'field_name' => 'test_field',
    'type' => 'entityreference',
    'cardinality' => '1',
  );

  // Build a set of test data.
  $user_values = array(
    'anonymous' => user_load(0),
    'admin' => user_load(1),
    'non_admin' => array(
      'name' => 'non_admin <&>',
      'mail' => 'non_admin@example.com',
      'roles' => array(),
      'pass' => user_password(),
      'status' => 1,
    ),
    'blocked' => array(
      'name' => 'blocked <&>',
      'mail' => 'blocked@example.com',
      'roles' => array(),
      'pass' => user_password(),
      'status' => 0,
    ),
  );
  $user_values['anonymous']->name = config('user.settings')
    ->get('anonymous');
  $users = array();
  $user_labels = array();
  foreach ($user_values as $key => $values) {
    if (is_array($values)) {
      $account = entity_create('user', $values);
      $account
        ->save();
    }
    else {
      $account = $values;
    }
    $users[$key] = $account;
    $user_labels[$key] = check_plain($account->name);
  }

  // Test as a non-admin.
  $GLOBALS['user'] = $users['non_admin'];
  $referencable_tests = array(
    array(
      'arguments' => array(
        array(
          NULL,
          'CONTAINS',
        ),
      ),
      'result' => array(
        $users['admin']->uid => $user_labels['admin'],
        $users['non_admin']->uid => $user_labels['non_admin'],
      ),
    ),
    array(
      'arguments' => array(
        array(
          'non_admin',
          'CONTAINS',
        ),
        array(
          'NON_ADMIN',
          'CONTAINS',
        ),
      ),
      'result' => array(
        $users['non_admin']->uid => $user_labels['non_admin'],
      ),
    ),
    array(
      'arguments' => array(
        array(
          'invalid user',
          'CONTAINS',
        ),
      ),
      'result' => array(),
    ),
    array(
      'arguments' => array(
        array(
          'blocked',
          'CONTAINS',
        ),
      ),
      'result' => array(),
    ),
  );
  $this
    ->assertReferencable($field, $referencable_tests, 'User handler');
  $GLOBALS['user'] = $users['admin'];
  $referencable_tests = array(
    array(
      'arguments' => array(
        array(
          NULL,
          'CONTAINS',
        ),
      ),
      'result' => array(
        $users['anonymous']->uid => $user_labels['anonymous'],
        $users['admin']->uid => $user_labels['admin'],
        $users['non_admin']->uid => $user_labels['non_admin'],
        $users['blocked']->uid => $user_labels['blocked'],
      ),
    ),
    array(
      'arguments' => array(
        array(
          'blocked',
          'CONTAINS',
        ),
      ),
      'result' => array(
        $users['blocked']->uid => $user_labels['blocked'],
      ),
    ),
    array(
      'arguments' => array(
        array(
          'Anonymous',
          'CONTAINS',
        ),
        array(
          'anonymous',
          'CONTAINS',
        ),
      ),
      'result' => array(
        $users['anonymous']->uid => $user_labels['anonymous'],
      ),
    ),
  );
  $this
    ->assertReferencable($field, $referencable_tests, 'User handler (admin)');
}