PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/core/tests/Drupal/KernelTests/Core/Extension/ModuleImplementsAlterTest.php

http://github.com/drupal/drupal
PHP | 92 lines | 39 code | 20 blank | 33 comment | 0 complexity | 0f9f651cee9c245ce676844d919721c2 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  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->assertTrue(array_key_exists('module_test', \Drupal::moduleHandler()->getModuleList()),
  35. 'module_test is in the module list.');
  36. $this->assertTrue(in_array('module_test', \Drupal::moduleHandler()->getImplementations('modules_installed')),
  37. 'module_test implements hook_modules_installed().');
  38. $this->assertTrue(in_array('module_test', \Drupal::moduleHandler()->getImplementations('module_implements_alter')),
  39. 'module_test implements hook_module_implements_alter().');
  40. // Assert that module_test.implementations.inc is not included yet.
  41. $this->assertFalse(function_exists('module_test_altered_test_hook'),
  42. 'The file module_test.implementations.inc is not included yet.');
  43. // Trigger hook discovery for hook_altered_test_hook().
  44. // Assert that module_test_module_implements_alter(*, 'altered_test_hook')
  45. // has added an implementation.
  46. $this->assertTrue(in_array('module_test', \Drupal::moduleHandler()->getImplementations('altered_test_hook')),
  47. 'module_test implements hook_altered_test_hook().');
  48. // Assert that module_test.implementations.inc was included as part of the process.
  49. $this->assertTrue(function_exists('module_test_altered_test_hook'),
  50. 'The file module_test.implementations.inc was included.');
  51. }
  52. /**
  53. * Tests what happens if hook_module_implements_alter() adds a non-existing
  54. * function to the implementations.
  55. *
  56. * @see \Drupal\Core\Extension\ModuleHandler::buildImplementationInfo()
  57. * @see module_test_module_implements_alter()
  58. */
  59. public function testModuleImplementsAlterNonExistingImplementation() {
  60. // Install the module_test module.
  61. \Drupal::service('module_installer')->install(['module_test']);
  62. try {
  63. // Trigger hook discovery.
  64. \Drupal::moduleHandler()->getImplementations('unimplemented_test_hook');
  65. $this->fail('An exception should be thrown for the non-existing implementation.');
  66. }
  67. catch (\RuntimeException $e) {
  68. $this->pass('An exception should be thrown for the non-existing implementation.');
  69. $this->assertEqual($e->getMessage(), 'An invalid implementation module_test_unimplemented_test_hook was added by hook_module_implements_alter()');
  70. }
  71. }
  72. }