function ViewsHooksTest::testHooks

Same name in other branches
  1. 9 core/modules/views/tests/src/Kernel/ViewsHooksTest.php \Drupal\Tests\views\Kernel\ViewsHooksTest::testHooks()
  2. 8.9.x core/modules/views/tests/src/Kernel/ViewsHooksTest.php \Drupal\Tests\views\Kernel\ViewsHooksTest::testHooks()
  3. 10 core/modules/views/tests/src/Kernel/ViewsHooksTest.php \Drupal\Tests\views\Kernel\ViewsHooksTest::testHooks()

Tests the hooks.

File

core/modules/views/tests/src/Kernel/ViewsHooksTest.php, line 76

Class

ViewsHooksTest
Tests that views hooks are registered when defined in $module.views.inc.

Namespace

Drupal\Tests\views\Kernel

Code

public function testHooks() : void {
    $view = Views::getView('test_view');
    $view->setDisplay();
    // Test each hook is found in the implementations array and is invoked.
    foreach (static::$hooks as $hook => $type) {
        $this->assertTrue($this->moduleHandler
            ->hasImplementations($hook, 'views_test_data'), "The hook {$hook} was registered.");
        if ($hook == 'views_post_render') {
            $this->moduleHandler
                ->invoke('views_test_data', $hook, [
                $view,
                &$view->display_handler->output,
                $view->display_handler
                    ->getPlugin('cache'),
            ]);
            continue;
        }
        switch ($type) {
            case 'view':
                $this->moduleHandler
                    ->invoke('views_test_data', $hook, [
                    $view,
                ]);
                break;
            case 'alter':
                $data = [];
                $this->moduleHandler
                    ->alter($hook, $data);
                break;
            default:
                $this->moduleHandler
                    ->invoke('views_test_data', $hook);
        }
        $this->assertTrue($this->container
            ->get('state')
            ->get('views_hook_test_' . $hook), "The {$hook} hook was invoked.");
        // Reset the module implementations cache, so we ensure that the
        // .views.inc file is loaded actively.
        $this->moduleHandler
            ->resetImplementations();
    }
}

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