public function RolesRidTest::testTitleQuery

Tests the title_query method.

See also

\Drupal\user\Plugin\views\argument\RolesRid::title_query()

File

drupal/core/modules/user/tests/Drupal/Tests/user/Views/Argument/RolesRidTest.php, line 49
Contains \Drupal\Tests\user\Views\Argument\RolesRidTest.

Class

RolesRidTest
Tests the roles argument handler.

Namespace

Drupal\Tests\user\Views\Argument

Code

public function testTitleQuery() {
  $config = array(
    'user.role.test_rid_1' => array(
      'id' => 'test_rid_1',
      'label' => 'test rid 1',
    ),
    'user.role.test_rid_2' => array(
      'id' => 'test_rid_2',
      'label' => 'test <strong>rid 2</strong>',
    ),
  );
  $config_factory = $this
    ->getConfigFactoryStub($config);
  $config_storage = $this
    ->getConfigStorageStub($config);

  // Creates a stub role storage controller and replace the attachLoad()
  // method with an empty version, because attachLoad() calls
  // module_implements().
  $role_storage_controller = $this
    ->getMock('Drupal\\user\\RoleStorageController', array(
    'attachLoad',
  ), array(
    'user_role',
    static::$entityInfo,
    $config_factory,
    $config_storage,
  ));
  $entity_manager = $this
    ->getMockBuilder('Drupal\\Core\\Entity\\EntityManager')
    ->disableOriginalConstructor()
    ->getMock();
  $entity_manager
    ->expects($this
    ->any())
    ->method('getDefinition')
    ->with($this
    ->equalTo('user_role'))
    ->will($this
    ->returnValue(static::$entityInfo));
  $entity_manager
    ->expects($this
    ->once())
    ->method('getStorageController')
    ->with($this
    ->equalTo('user_role'))
    ->will($this
    ->returnValue($role_storage_controller));

  // @todo \Drupal\Core\Entity\Entity::entityInfo() uses a global call to
  //   entity_get_info(), which in turn wraps \Drupal::entityManager(). Set
  //   the entity manager until this is fixed.
  $container = new ContainerBuilder();
  $container
    ->set('plugin.manager.entity', $entity_manager);
  \Drupal::setContainer($container);
  $roles_rid_argument = new RolesRid($config, 'users_roles_rid', array(), $entity_manager);
  $roles_rid_argument->value = array();
  $titles = $roles_rid_argument
    ->title_query();
  $this
    ->assertEquals(array(), $titles);
  $roles_rid_argument->value = array(
    'test_rid_1',
  );
  $titles = $roles_rid_argument
    ->title_query();
  $this
    ->assertEquals(array(
    'test rid 1',
  ), $titles);
  $roles_rid_argument->value = array(
    'test_rid_1',
    'test_rid_2',
  );
  $titles = $roles_rid_argument
    ->title_query();
  $this
    ->assertEquals(array(
    'test rid 1',
    String::checkPlain('test <strong>rid 2</strong>'),
  ), $titles);
}