You are here

public function EntityReferenceHandlersTestCase::testUserHandler in Entity reference 7

Test the user-specific overrides of the entity handler.

File

tests/entityreference.handlers.test, line 217
Contains EntityReferenceHandlersTestCase

Class

EntityReferenceHandlersTestCase
Test for Entity Reference handlers.

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.
  $users = array(
    'anonymous' => user_load(0),
    'admin' => user_load(1),
    'non_admin' => (object) array(
      'name' => 'non_admin <&>',
      'mail' => 'non_admin@example.com',
      'roles' => array(),
      'pass' => user_password(),
      'status' => 1,
    ),
    'blocked' => (object) array(
      'name' => 'blocked <&>',
      'mail' => 'blocked@example.com',
      'roles' => array(),
      'pass' => user_password(),
      'status' => 0,
    ),
  );

  // The label of the anonymous user is variable_get('anonymous').
  $users['anonymous']->name = variable_get('anonymous', t('Anonymous'));
  $user_labels = array();
  foreach ($users as $key => $user) {
    if (!isset($user->uid)) {
      $users[$key] = $user = user_save(drupal_anonymous_user(), (array) $user);
    }
    $user_labels[$key] = check_plain($user->name);
  }

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