function HandlerFilterRolesTest::testMissingRole

Same name and namespace in other branches
  1. 11.x core/modules/user/tests/src/Kernel/Views/HandlerFilterRolesTest.php \Drupal\Tests\user\Kernel\Views\HandlerFilterRolesTest::testMissingRole()
  2. 9 core/modules/user/tests/src/Kernel/Views/HandlerFilterRolesTest.php \Drupal\Tests\user\Kernel\Views\HandlerFilterRolesTest::testMissingRole()
  3. 8.9.x core/modules/user/tests/src/Kernel/Views/HandlerFilterRolesTest.php \Drupal\Tests\user\Kernel\Views\HandlerFilterRolesTest::testMissingRole()
  4. main core/modules/user/tests/src/Kernel/Views/HandlerFilterRolesTest.php \Drupal\Tests\user\Kernel\Views\HandlerFilterRolesTest::testMissingRole()

Tests that a warning is triggered if the filter references a missing role.

File

core/modules/user/tests/src/Kernel/Views/HandlerFilterRolesTest.php, line 106

Class

HandlerFilterRolesTest
Tests the roles filter handler.

Namespace

Drupal\Tests\user\Kernel\Views

Code

public function testMissingRole() : void {
  $logger = $this->prophesize(LoggerInterface::class);
  $this->container
    ->get('logger.factory')
    ->get('system')
    ->addLogger($logger->reveal());
  $role = Role::create([
    'id' => 'test_user_role',
    'label' => 'Test user role',
  ]);
  $role->save();
  /** @var \Drupal\views\Entity\View $view */
  $view = View::load('test_user_name');
  $display =& $view->getDisplay('default');
  $display['display_options']['filters']['roles_target_id'] = [
    'id' => 'roles_target_id',
    'table' => 'user__roles',
    'field' => 'roles_target_id',
    'value' => [
      'test_user_role' => 'test_user_role',
    ],
    'plugin_id' => 'user_roles',
  ];
  // Ensure no warning is triggered before the role is deleted.
  $view->calculateDependencies();
  $role->delete();
  // Recalculate after role deletion.
  $logger->log(RfcLogLevel::WARNING, 'View %view depends on role %role, but the role does not exist.', Argument::allOf(Argument::withEntry('%view', 'test_user_name'), Argument::withEntry('%role', 'test_user_role')))
    ->shouldBeCalled();
  $view->calculateDependencies();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.