You are here

public function ModuleImplementsAlterTest::testModuleImplementsAlter in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Extension/ModuleImplementsAlterTest.php \Drupal\KernelTests\Core\Extension\ModuleImplementsAlterTest::testModuleImplementsAlter()
  2. 10 core/tests/Drupal/KernelTests/Core/Extension/ModuleImplementsAlterTest.php \Drupal\KernelTests\Core\Extension\ModuleImplementsAlterTest::testModuleImplementsAlter()

Tests hook_module_implements_alter() adding an implementation.

See also

\Drupal\Core\Extension\ModuleHandler::buildImplementationInfo()

module_test_module_implements_alter()

File

core/tests/Drupal/KernelTests/Core/Extension/ModuleImplementsAlterTest.php, line 25

Class

ModuleImplementsAlterTest
Tests hook_module_implements_alter().

Namespace

Drupal\KernelTests\Core\Extension

Code

public function testModuleImplementsAlter() {

  // Get an instance of the module handler, to observe how it is going to be
  // replaced.
  $module_handler = \Drupal::moduleHandler();
  $this
    ->assertTrue($module_handler === \Drupal::moduleHandler(), 'Module handler instance is still the same.');

  // Install the module_test module.
  \Drupal::service('module_installer')
    ->install([
    'module_test',
  ]);

  // Assert that the \Drupal::moduleHandler() instance has been replaced.
  $this
    ->assertFalse($module_handler === \Drupal::moduleHandler(), 'The \\Drupal::moduleHandler() instance has been replaced during \\Drupal::moduleHandler()->install().');

  // Assert that module_test.module is now included.
  $this
    ->assertTrue(function_exists('module_test_modules_installed'), 'The file module_test.module was successfully included.');
  $this
    ->assertArrayHasKey('module_test', \Drupal::moduleHandler()
    ->getModuleList());
  $this
    ->assertContains('module_test', \Drupal::moduleHandler()
    ->getImplementations('modules_installed'), 'module_test implements hook_modules_installed().');
  $this
    ->assertContains('module_test', \Drupal::moduleHandler()
    ->getImplementations('module_implements_alter'), 'module_test implements hook_module_implements_alter().');

  // Assert that module_test.implementations.inc is not included yet.
  $this
    ->assertFalse(function_exists('module_test_altered_test_hook'), 'The file module_test.implementations.inc is not included yet.');

  // Trigger hook discovery for hook_altered_test_hook().
  // Assert that module_test_module_implements_alter(*, 'altered_test_hook')
  // has added an implementation.
  $this
    ->assertContains('module_test', \Drupal::moduleHandler()
    ->getImplementations('altered_test_hook'), 'module_test implements hook_altered_test_hook().');

  // Assert that module_test.implementations.inc was included as part of the process.
  $this
    ->assertTrue(function_exists('module_test_altered_test_hook'), 'The file module_test.implementations.inc was included.');
}