ModuleImplementsAlterTest.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace Drupal\KernelTests\Core\Extension;
  3. use Drupal\KernelTests\KernelTestBase;
  4. /**
  5. * Tests hook_module_implements_alter().
  6. *
  7. * @group Module
  8. */
  9. class ModuleImplementsAlterTest extends KernelTestBase {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. public static $modules = ['system'];
  14. /**
  15. * Tests hook_module_implements_alter() adding an implementation.
  16. *
  17. * @see \Drupal\Core\Extension\ModuleHandler::buildImplementationInfo()
  18. * @see module_test_module_implements_alter()
  19. */
  20. public function testModuleImplementsAlter() {
  21. // Get an instance of the module handler, to observe how it is going to be
  22. // replaced.
  23. $module_handler = \Drupal::moduleHandler();
  24. $this->assertTrue($module_handler === \Drupal::moduleHandler(),
  25. 'Module handler instance is still the same.');
  26. // Install the module_test module.
  27. \Drupal::service('module_installer')->install(['module_test']);
  28. // Assert that the \Drupal::moduleHandler() instance has been replaced.
  29. $this->assertFalse($module_handler === \Drupal::moduleHandler(),
  30. 'The \Drupal::moduleHandler() instance has been replaced during \Drupal::moduleHandler()->install().');
  31. // Assert that module_test.module is now included.
  32. $this->assertTrue(function_exists('module_test_modules_installed'),
  33. 'The file module_test.module was successfully included.');
  34. $this->assertArrayHasKey('module_test', \Drupal::moduleHandler()->getModuleList());
  35. $this->assertContains('module_test', \Drupal::moduleHandler()->getImplementations('modules_installed'),
  36. 'module_test implements hook_modules_installed().');
  37. $this->assertContains('module_test', \Drupal::moduleHandler()->getImplementations('module_implements_alter'),
  38. 'module_test implements hook_module_implements_alter().');
  39. // Assert that module_test.implementations.inc is not included yet.
  40. $this->assertFalse(function_exists('module_test_altered_test_hook'),
  41. 'The file module_test.implementations.inc is not included yet.');
  42. // Trigger hook discovery for hook_altered_test_hook().
  43. // Assert that module_test_module_implements_alter(*, 'altered_test_hook')
  44. // has added an implementation.
  45. $this->assertContains('module_test', \Drupal::moduleHandler()->getImplementations('altered_test_hook'),
  46. 'module_test implements hook_altered_test_hook().');
  47. // Assert that module_test.implementations.inc was included as part of the process.
  48. $this->assertTrue(function_exists('module_test_altered_test_hook'),
  49. 'The file module_test.implementations.inc was included.');
  50. }
  51. /**
  52. * Tests what happens if hook_module_implements_alter() adds a non-existing
  53. * function to the implementations.
  54. *
  55. * @see \Drupal\Core\Extension\ModuleHandler::buildImplementationInfo()
  56. * @see module_test_module_implements_alter()
  57. */
  58. public function testModuleImplementsAlterNonExistingImplementation() {
  59. // Install the module_test module.
  60. \Drupal::service('module_installer')->install(['module_test']);
  61. // Trigger hook discovery.
  62. $this->expectException(\RuntimeException::class);
  63. $this->expectExceptionMessage('An invalid implementation module_test_unimplemented_test_hook was added by hook_module_implements_alter()');
  64. \Drupal::moduleHandler()->getImplementations('unimplemented_test_hook');
  65. }
  66. }