1 entityreference.handlers.test public EntityReferenceHandlersTestCase::testUserHandler()

Test the user-specific overrides of the entity handler.

File

core/modules/entityreference/tests/entityreference.handlers.test, line 218
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' => NULL,
    'admin' => user_load(1),
    'non_admin' => entity_create('user', array(
      'name' => 'non_admin <&>',
      'mail' => 'non_admin@example.com',
      'roles' => array(),
      'pass' => user_password(),
      'status' => 1,
    )),
    'blocked' => entity_create('user', array(
      'name' => 'blocked <&>',
      'mail' => 'blocked@example.com',
      'roles' => array(),
      'pass' => user_password(),
      'status' => 0,
    )),
  );

  $user_labels = array();
  foreach ($users as $key => $user) {
    if ($key == 'anonymous') {
      $users[$key] = backdrop_anonymous_user();
      $name = ENTITYREFERENCE_DENIED;
    }
    else {
      user_save($user);
      $users[$key] = $user;
      $name = check_plain($user->name);
    }
    $user_labels[$key] = $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)');
}