function ModuleHandlerTest::testResetImplementationsClearsHooks

Tests that resetImplementations() clears the hook memory cache.

@covers ::resetImplementations

File

core/tests/Drupal/KernelTests/Core/Extension/ModuleHandlerTest.php, line 45

Class

ModuleHandlerTest
@coversDefaultClass \Drupal\Core\Extension\ModuleHandler[[api-linebreak]]

Namespace

Drupal\KernelTests\Core\Extension

Code

public function testResetImplementationsClearsHooks() : void {
  $oldModuleHandler = \Drupal::moduleHandler();
  $this->assertHasResetHookImplementations(FALSE, $oldModuleHandler);
  // Installing a module does not trigger ->resetImplementations().
  /** @var \Drupal\Core\Extension\ModuleInstallerInterface $moduleInstaller */
  $moduleInstaller = \Drupal::service('module_installer');
  $moduleInstaller->install([
    'module_test',
  ]);
  $this->assertHasResetHookImplementations(FALSE, $oldModuleHandler);
  // Only the new ModuleHandler instance has the updated implementations.
  $moduleHandler = \Drupal::moduleHandler();
  $this->assertHasResetHookImplementations(TRUE, $moduleHandler);
  $backupModuleList = $moduleHandler->getModuleList();
  $moduleListWithout = array_diff_key($backupModuleList, [
    'module_test' => TRUE,
  ]);
  $this->assertArrayHasKey('module_test', $backupModuleList);
  // Silently setting the property does not clear the hooks cache.
  $moduleListProperty = new \ReflectionProperty($moduleHandler, 'moduleList');
  $this->assertSame($backupModuleList, $moduleListProperty->getValue($moduleHandler));
  $moduleListProperty->setValue($moduleHandler, $moduleListWithout);
  $this->assertHasResetHookImplementations(TRUE, $moduleHandler);
  // Directly calling ->resetImplementations() clears the hook caches.
  $moduleHandler->resetImplementations();
  $this->assertHasResetHookImplementations(FALSE, $moduleHandler);
  $moduleListProperty->setValue($moduleHandler, $backupModuleList);
  $this->assertHasResetHookImplementations(FALSE, $moduleHandler);
  $moduleHandler->resetImplementations();
  $this->assertHasResetHookImplementations(TRUE, $moduleHandler);
  // Calling ->setModuleList() triggers ->resetImplementations().
  $moduleHandler->setModuleList([
    'system',
  ]);
  $this->assertHasResetHookImplementations(FALSE, $moduleHandler);
  $moduleHandler->setModuleList($backupModuleList);
  $this->assertHasResetHookImplementations(TRUE, $moduleHandler);
  // Uninstalling a module triggers ->resetImplementations().
  /** @var \Drupal\Core\Extension\ModuleInstallerInterface $moduleInstaller */
  $moduleInstaller = \Drupal::service('module_installer');
  $moduleInstaller->uninstall([
    'module_test',
  ]);
  $this->assertSame($moduleListWithout, $moduleHandler->getModuleList());
  $this->assertHasResetHookImplementations(FALSE, $moduleHandler);
}

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