function ModuleApiTest::testModuleImplements

Test module_implements() caching.

File

drupal/core/modules/system/lib/Drupal/system/Tests/Module/ModuleApiTest.php, line 86
Definition of Drupal\system\Tests\Module\ModuleApiTest.

Class

ModuleApiTest
Unit tests for the module API.

Namespace

Drupal\system\Tests\Module

Code

function testModuleImplements() {

  // Clear the cache.
  cache('bootstrap')
    ->delete('module_implements');
  $this
    ->assertFalse(cache('bootstrap')
    ->get('module_implements'), 'The module implements cache is empty.');
  $this
    ->drupalGet('');
  $this
    ->assertTrue(cache('bootstrap')
    ->get('module_implements'), 'The module implements cache is populated after requesting a page.');

  // Test again with an authenticated user.
  $this->user = $this
    ->drupalCreateUser();
  $this
    ->drupalLogin($this->user);
  cache('bootstrap')
    ->delete('module_implements');
  $this
    ->drupalGet('');
  $this
    ->assertTrue(cache('bootstrap')
    ->get('module_implements'), 'The module implements cache is populated after requesting a page.');

  // Prime ModuleHandler's hook implementation cache by invoking a random hook
  // name. The subsequent module_enable() below will only call into
  // setModuleList(), but will not explicitly reset the hook implementation
  // cache, as that is expected to happen implicitly by setting the module
  // list. This verifies that the hook implementation cache is cleared
  // whenever setModuleList() is called.
  $module_handler = \Drupal::moduleHandler();
  $module_handler
    ->invokeAll('test');

  // Make sure group include files are detected properly even when the file is
  // already loaded when the cache is rebuilt.
  // For that activate the module_test which provides the file to load.
  module_enable(array(
    'module_test',
  ));
  $module_handler
    ->loadAll();
  module_load_include('inc', 'module_test', 'module_test.file');
  $modules = $module_handler
    ->getImplementations('test_hook');
  $this
    ->assertTrue(in_array('module_test', $modules), 'Hook found.');
}