function HookCollectorPassTest::testCollectAllHookImplementations

Tests collect all hook implementations.

@legacy-covers ::collectAllHookImplementations @legacy-covers ::filterIterator

File

core/tests/Drupal/Tests/Core/Hook/HookCollectorPassTest.php, line 32

Class

HookCollectorPassTest
Tests Drupal\Core\Hook\HookCollectorPass.

Namespace

Drupal\Tests\Core\Hook

Code

public function testCollectAllHookImplementations() : void {
  vfsStream::setup('drupal_root');
  $files = [
    'modules/test_module/test_module_info.yml',
    // This creates a submodule which is not installed.
'modules/test_module/test_sub_module/test_sub_module.info.yml',
  ];
  $file_data = [];
  foreach ($files as &$filename) {
    NestedArray::setValue($file_data, explode('/', $filename), '');
  }
  vfsStream::create($file_data);
  $module_filenames = [
    'test_module' => [
      'pathname' => 'vfs://drupal_root/modules/test_module/test_module_info.yml',
    ],
  ];
  // This directory, however, should be included.
  mkdir('vfs://drupal_root/modules/test_module/includes');
  file_put_contents('vfs://drupal_root/modules/test_module/includes/test_module.inc', <<<__EOF__
  <?php
  
  function test_module_test_hook();
  
  __EOF__);
  // This is the not installed submodule.
  file_put_contents('vfs://drupal_root/modules/test_module/test_sub_module/test_sub_module.module', <<<__EOF__
  <?php
  
  function test_module_should_be_skipped();
  
  __EOF__);
  $container = new ContainerBuilder();
  $container->setParameter('container.modules', $module_filenames);
  (new HookCollectorPass())->process($container);
  $this->assertEquals([
    'test_module_test_hook' => 'test_module',
  ], $container->getParameter('.hook_data')['hook_list']['test_hook']);
  $this->assertEquals([
    'test_hook' => [
      'vfs://drupal_root/modules/test_module/includes/test_module.inc',
    ],
  ], $container->getParameter('.hook_data')['includes']);
}

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