You are here

public function EntityReferenceSelectionAccessTest::testUserHandler in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php \Drupal\system\Tests\Entity\EntityReferenceSelection\EntityReferenceSelectionAccessTest::testUserHandler()

Test the user-specific overrides of the entity handler.

File

core/modules/system/src/Tests/Entity/EntityReferenceSelection/EntityReferenceSelectionAccessTest.php, line 201
Contains \Drupal\system\Tests\Entity\EntityReferenceSelection\EntityReferenceSelectionAccessTest.

Class

EntityReferenceSelectionAccessTest
Tests for the base handlers provided by Entity Reference.

Namespace

Drupal\system\Tests\Entity\EntityReferenceSelection

Code

public function testUserHandler() {
  $selection_options = array(
    'target_type' => 'user',
    'handler' => 'default',
    'handler_settings' => array(
      'target_bundles' => NULL,
      'include_anonymous' => TRUE,
    ),
  );

  // 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 = $this
    ->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] = Html::escape($account
      ->getUsername());
  }

  // Test as a non-admin.
  \Drupal::currentUser()
    ->setAccount($users['non_admin']);
  $referenceable_tests = array(
    array(
      'arguments' => array(
        array(
          NULL,
          'CONTAINS',
        ),
      ),
      'result' => array(
        'user' => array(
          $users['admin']
            ->id() => $user_labels['admin'],
          $users['non_admin']
            ->id() => $user_labels['non_admin'],
        ),
      ),
    ),
    array(
      'arguments' => array(
        array(
          'non_admin',
          'CONTAINS',
        ),
        array(
          'NON_ADMIN',
          'CONTAINS',
        ),
      ),
      'result' => array(
        'user' => array(
          $users['non_admin']
            ->id() => $user_labels['non_admin'],
        ),
      ),
    ),
    array(
      'arguments' => array(
        array(
          'invalid user',
          'CONTAINS',
        ),
      ),
      'result' => array(),
    ),
    array(
      'arguments' => array(
        array(
          'blocked',
          'CONTAINS',
        ),
      ),
      'result' => array(),
    ),
  );
  $this
    ->assertReferenceable($selection_options, $referenceable_tests, 'User handler');
  \Drupal::currentUser()
    ->setAccount($users['admin']);
  $referenceable_tests = array(
    array(
      'arguments' => array(
        array(
          NULL,
          'CONTAINS',
        ),
      ),
      'result' => array(
        'user' => array(
          $users['anonymous']
            ->id() => $user_labels['anonymous'],
          $users['admin']
            ->id() => $user_labels['admin'],
          $users['non_admin']
            ->id() => $user_labels['non_admin'],
          $users['blocked']
            ->id() => $user_labels['blocked'],
        ),
      ),
    ),
    array(
      'arguments' => array(
        array(
          'blocked',
          'CONTAINS',
        ),
      ),
      'result' => array(
        'user' => array(
          $users['blocked']
            ->id() => $user_labels['blocked'],
        ),
      ),
    ),
    array(
      'arguments' => array(
        array(
          'Anonymous',
          'CONTAINS',
        ),
        array(
          'anonymous',
          'CONTAINS',
        ),
      ),
      'result' => array(
        'user' => array(
          $users['anonymous']
            ->id() => $user_labels['anonymous'],
        ),
      ),
    ),
  );
  $this
    ->assertReferenceable($selection_options, $referenceable_tests, 'User handler (admin)');

  // Test the 'include_anonymous' option.
  $selection_options['handler_settings']['include_anonymous'] = FALSE;
  $referenceable_tests = array(
    array(
      'arguments' => array(
        array(
          'Anonymous',
          'CONTAINS',
        ),
        array(
          'anonymous',
          'CONTAINS',
        ),
      ),
      'result' => array(),
    ),
  );
  $this
    ->assertReferenceable($selection_options, $referenceable_tests, 'User handler (does not include anonymous)');
}