Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Extension / ModuleImplementsAlterTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Extension;
4
5 use Drupal\KernelTests\KernelTestBase;
6
7 /**
8  * Tests hook_module_implements_alter().
9  *
10  * @group Module
11  */
12 class ModuleImplementsAlterTest extends KernelTestBase {
13
14   /**
15    * {@inheritdoc}
16    */
17   public static $modules = ['system'];
18
19   /**
20    * Tests hook_module_implements_alter() adding an implementation.
21    *
22    * @see \Drupal\Core\Extension\ModuleHandler::buildImplementationInfo()
23    * @see module_test_module_implements_alter()
24    */
25   public function testModuleImplementsAlter() {
26
27     // Get an instance of the module handler, to observe how it is going to be
28     // replaced.
29     $module_handler = \Drupal::moduleHandler();
30
31     $this->assertTrue($module_handler === \Drupal::moduleHandler(),
32       'Module handler instance is still the same.');
33
34     // Install the module_test module.
35     \Drupal::service('module_installer')->install(['module_test']);
36
37     // Assert that the \Drupal::moduleHandler() instance has been replaced.
38     $this->assertFalse($module_handler === \Drupal::moduleHandler(),
39       'The \Drupal::moduleHandler() instance has been replaced during \Drupal::moduleHandler()->install().');
40
41     // Assert that module_test.module is now included.
42     $this->assertTrue(function_exists('module_test_modules_installed'),
43       'The file module_test.module was successfully included.');
44
45     $this->assertTrue(array_key_exists('module_test', \Drupal::moduleHandler()->getModuleList()),
46       'module_test is in the module list.');
47
48     $this->assertTrue(in_array('module_test', \Drupal::moduleHandler()->getImplementations('modules_installed')),
49       'module_test implements hook_modules_installed().');
50
51     $this->assertTrue(in_array('module_test', \Drupal::moduleHandler()->getImplementations('module_implements_alter')),
52       'module_test implements hook_module_implements_alter().');
53
54     // Assert that module_test.implementations.inc is not included yet.
55     $this->assertFalse(function_exists('module_test_altered_test_hook'),
56       'The file module_test.implementations.inc is not included yet.');
57
58     // Trigger hook discovery for hook_altered_test_hook().
59     // Assert that module_test_module_implements_alter(*, 'altered_test_hook')
60     // has added an implementation.
61     $this->assertTrue(in_array('module_test', \Drupal::moduleHandler()->getImplementations('altered_test_hook')),
62       'module_test implements hook_altered_test_hook().');
63
64     // Assert that module_test.implementations.inc was included as part of the process.
65     $this->assertTrue(function_exists('module_test_altered_test_hook'),
66       'The file module_test.implementations.inc was included.');
67   }
68
69   /**
70    * Tests what happens if hook_module_implements_alter() adds a non-existing
71    * function to the implementations.
72    *
73    * @see \Drupal\Core\Extension\ModuleHandler::buildImplementationInfo()
74    * @see module_test_module_implements_alter()
75    */
76   public function testModuleImplementsAlterNonExistingImplementation() {
77
78     // Install the module_test module.
79     \Drupal::service('module_installer')->install(['module_test']);
80
81     try {
82       // Trigger hook discovery.
83       \Drupal::moduleHandler()->getImplementations('unimplemented_test_hook');
84       $this->fail('An exception should be thrown for the non-existing implementation.');
85     }
86     catch (\RuntimeException $e) {
87       $this->pass('An exception should be thrown for the non-existing implementation.');
88       $this->assertEqual($e->getMessage(), 'An invalid implementation module_test_unimplemented_test_hook was added by hook_module_implements_alter()');
89     }
90   }
91
92 }