You are here

public function HandlerTest::testAccess in Views (for Drupal 7) 8.3

Tests access to a handler.

See also

views_test_data_handler_test_access_callback

File

lib/Drupal/views/Tests/Handler/HandlerTest.php, line 335
Definition of Drupal\views\Tests\Handler\HandlerTest.

Class

HandlerTest
Tests abstract handlers of views.

Namespace

Drupal\views\Tests\Handler

Code

public function testAccess() {
  $view = views_get_view('test_handler_test_access');
  $views_data = $this
    ->viewsData();
  $views_data = $views_data['views_test_data'];

  // Enable access to callback only field and deny for callback + arguments.
  config('views_test_data.tests')
    ->set('handler_access_callback', TRUE)
    ->save();
  config('views_test_data.tests')
    ->set('handler_access_callback_argument', FALSE)
    ->save();
  $view
    ->initDisplay();
  $view
    ->initHandlers();
  foreach ($views_data['access_callback'] as $type => $info) {
    if (!in_array($type, array(
      'title',
      'help',
    ))) {
      $this
        ->assertTrue($view->field['access_callback'] instanceof HandlerBase, 'Make sure the user got access to the access_callback field ');
      $this
        ->assertFalse(isset($view->field['access_callback_arguments']), 'Make sure the user got no access to the access_callback_arguments field ');
    }
  }

  // Enable access to the callback + argument handlers and deny for callback.
  config('views_test_data.tests')
    ->set('handler_access_callback', FALSE)
    ->save();
  config('views_test_data.tests')
    ->set('handler_access_callback_argument', TRUE)
    ->save();
  $view
    ->destroy();
  $view
    ->initDisplay();
  $view
    ->initHandlers();
  foreach ($views_data['access_callback'] as $type => $info) {
    if (!in_array($type, array(
      'title',
      'help',
    ))) {
      $this
        ->assertFalse(isset($view->field['access_callback']), 'Make sure the user got no access to the access_callback field ');
      $this
        ->assertTrue($view->field['access_callback_arguments'] instanceof HandlerBase, 'Make sure the user got access to the access_callback_arguments field ');
    }
  }
}