namespace Drupal\Tests\Core\Extension;
+use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\Extension;
use Drupal\Core\Extension\ModuleHandler;
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\Core\Extension\ModuleHandler
+ * @runTestsInSeparateProcesses
+ *
* @group Extension
*/
class ModuleHandlerTest extends UnitTestCase {
*/
protected $cacheBackend;
- /**
- * The tested module handler.
- *
- * @var \Drupal\Core\Extension\ModuleHandler
- */
- protected $moduleHandler;
-
/**
* {@inheritdoc}
*
*/
protected function setUp() {
parent::setUp();
+ // We can mock the cache handler here, but not the module handler.
+ $this->cacheBackend = $this->getMock(CacheBackendInterface::class);
+ }
- $this->cacheBackend = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
- $this->moduleHandler = new ModuleHandler($this->root, [
+ /**
+ * Get a module handler object to test.
+ *
+ * Since we have to run these tests in separate processes, we have to use
+ * test objects which are serializable. Since ModuleHandler will populate
+ * itself with Extension objects, and since Extension objects will try to
+ * access DRUPAL_ROOT when they're unserialized, we can't store our mocked
+ * ModuleHandler objects as a property in unit tests. They must be generated
+ * by the test method by calling this method.
+ *
+ * @return \Drupal\Core\Extension\ModuleHandler
+ * The module handler to test.
+ */
+ protected function getModuleHandler() {
+ $module_handler = new ModuleHandler($this->root, [
'module_handler_test' => [
'type' => 'module',
'pathname' => 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test/module_handler_test.info.yml',
'filename' => 'module_handler_test.module',
]
], $this->cacheBackend);
+ return $module_handler;
}
/**
* @covers ::load
*/
public function testLoadModule() {
+ $module_handler = $this->getModuleHandler();
$this->assertFalse(function_exists('module_handler_test_hook'));
- $this->assertTrue($this->moduleHandler->load('module_handler_test'));
+ $this->assertTrue($module_handler->load('module_handler_test'));
$this->assertTrue(function_exists('module_handler_test_hook'));
- $this->moduleHandler->addModule('module_handler_test_added', 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_added');
+ $module_handler->addModule('module_handler_test_added', 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_added');
$this->assertFalse(function_exists('module_handler_test_added_hook'), 'Function does not exist before being loaded.');
- $this->assertTrue($this->moduleHandler->load('module_handler_test_added'));
+ $this->assertTrue($module_handler->load('module_handler_test_added'));
$this->assertTrue(function_exists('module_handler_test_added_helper'), 'Function exists after being loaded.');
- $this->assertTrue($this->moduleHandler->load('module_handler_test_added'));
+ $this->assertTrue($module_handler->load('module_handler_test_added'));
- $this->assertFalse($this->moduleHandler->load('module_handler_test_dne'), 'Non-existent modules returns false.');
+ $this->assertFalse($module_handler->load('module_handler_test_dne'), 'Non-existent modules returns false.');
}
/**
* @covers ::loadAll
*/
public function testLoadAllModules() {
- $this->moduleHandler->addModule('module_handler_test_all1', 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_all1');
- $this->moduleHandler->addModule('module_handler_test_all2', 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_all2');
+ $module_handler = $this->getModuleHandler();
+ $module_handler->addModule('module_handler_test_all1', 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_all1');
+ $module_handler->addModule('module_handler_test_all2', 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_all2');
$this->assertFalse(function_exists('module_handler_test_all1_hook'), 'Function does not exist before being loaded.');
$this->assertFalse(function_exists('module_handler_test_all2_hook'), 'Function does not exist before being loaded.');
- $this->moduleHandler->loadAll();
+ $module_handler->loadAll();
$this->assertTrue(function_exists('module_handler_test_all1_hook'), 'Function exists after being loaded.');
$this->assertTrue(function_exists('module_handler_test_all2_hook'), 'Function exists after being loaded.');
}
* @covers ::reload
*/
public function testModuleReloading() {
- $module_handler = $this->getMockBuilder('Drupal\Core\Extension\ModuleHandler')
+ $module_handler = $this->getMockBuilder(ModuleHandler::class)
->setConstructorArgs([
$this->root,
[
* @covers ::isLoaded
*/
public function testIsLoaded() {
- $this->assertFalse($this->moduleHandler->isLoaded());
- $this->moduleHandler->loadAll();
- $this->assertTrue($this->moduleHandler->isLoaded());
+ $module_handler = $this->getModuleHandler();
+ $this->assertFalse($module_handler->isLoaded());
+ $module_handler->loadAll();
+ $this->assertTrue($module_handler->isLoaded());
}
/**
* @covers ::getModuleList
*/
public function testGetModuleList() {
- $this->assertEquals($this->moduleHandler->getModuleList(), [
+ $this->assertEquals($this->getModuleHandler()->getModuleList(), [
'module_handler_test' => new Extension($this->root, 'module', 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test/module_handler_test.info.yml', 'module_handler_test.module'),
]);
}
* @covers ::getModule
*/
public function testGetModuleWithExistingModule() {
- $this->assertEquals($this->moduleHandler->getModule('module_handler_test'), new Extension($this->root, 'module', 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test/module_handler_test.info.yml', 'module_handler_test.module'));
+ $this->assertEquals($this->getModuleHandler()->getModule('module_handler_test'), new Extension($this->root, 'module', 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test/module_handler_test.info.yml', 'module_handler_test.module'));
}
/**
*/
public function testGetModuleWithNonExistingModule() {
$this->setExpectedException(\InvalidArgumentException::class);
- $this->moduleHandler->getModule('claire_alice_watch_my_little_pony_module_that_does_not_exist');
+ $this->getModuleHandler()->getModule('claire_alice_watch_my_little_pony_module_that_does_not_exist');
}
/**
* @covers ::setModuleList
*/
public function testSetModuleList() {
- $module_handler = $this->getMockBuilder('Drupal\Core\Extension\ModuleHandler')
+ $fixture_module_handler = $this->getModuleHandler();
+ $module_handler = $this->getMockBuilder(ModuleHandler::class)
->setConstructorArgs([
$this->root, [], $this->cacheBackend
])
$this->assertEquals($module_handler->getModuleList(), []);
// Replace the list with a prebuilt list.
- $module_handler->setModuleList($this->moduleHandler->getModuleList());
+ $module_handler->setModuleList($fixture_module_handler->getModuleList());
// Ensure those changes are stored.
- $this->assertEquals($this->moduleHandler->getModuleList(), $module_handler->getModuleList());
+ $this->assertEquals($fixture_module_handler->getModuleList(), $module_handler->getModuleList());
}
/**
*/
public function testAddModule() {
- $module_handler = $this->getMockBuilder('Drupal\Core\Extension\ModuleHandler')
+ $module_handler = $this->getMockBuilder(ModuleHandler::class)
->setConstructorArgs([
$this->root, [], $this->cacheBackend
])
*/
public function testAddProfile() {
- $module_handler = $this->getMockBuilder('Drupal\Core\Extension\ModuleHandler')
+ $module_handler = $this->getMockBuilder(ModuleHandler::class)
->setConstructorArgs([
$this->root, [], $this->cacheBackend
])
* @covers ::moduleExists
*/
public function testModuleExists() {
- $this->assertTrue($this->moduleHandler->moduleExists('module_handler_test'));
- $this->assertFalse($this->moduleHandler->moduleExists('module_handler_test_added'));
+ $module_handler = $this->getModuleHandler();
+ $this->assertTrue($module_handler->moduleExists('module_handler_test'));
+ $this->assertFalse($module_handler->moduleExists('module_handler_test_added'));
}
/**
*/
public function testLoadAllIncludes() {
$this->assertTrue(TRUE);
- $module_handler = $this->getMockBuilder('Drupal\Core\Extension\ModuleHandler')
+ $module_handler = $this->getMockBuilder(ModuleHandler::class)
->setConstructorArgs([
$this->root,
[
* @preserveGlobalState disabled
*/
public function testLoadInclude() {
+ $module_handler = $this->getModuleHandler();
// Include exists.
- $this->assertEquals(__DIR__ . '/modules/module_handler_test/hook_include.inc', $this->moduleHandler->loadInclude('module_handler_test', 'inc', 'hook_include'));
+ $this->assertEquals(__DIR__ . '/modules/module_handler_test/hook_include.inc', $module_handler->loadInclude('module_handler_test', 'inc', 'hook_include'));
$this->assertTrue(function_exists('module_handler_test_hook_include'));
// Include doesn't exist.
- $this->assertFalse($this->moduleHandler->loadInclude('module_handler_test', 'install'));
+ $this->assertFalse($module_handler->loadInclude('module_handler_test', 'install'));
}
/**
* @covers ::invoke
*/
public function testInvokeModuleEnabled() {
- $this->assertTrue($this->moduleHandler->invoke('module_handler_test', 'hook', [TRUE]), 'Installed module runs hook.');
- $this->assertFalse($this->moduleHandler->invoke('module_handler_test', 'hook', [FALSE]), 'Installed module runs hook.');
- $this->assertNull($this->moduleHandler->invoke('module_handler_test_fake', 'hook', [FALSE]), 'Installed module runs hook.');
+ $module_handler = $this->getModuleHandler();
+ $this->assertTrue($module_handler->invoke('module_handler_test', 'hook', [TRUE]), 'Installed module runs hook.');
+ $this->assertFalse($module_handler->invoke('module_handler_test', 'hook', [FALSE]), 'Installed module runs hook.');
+ $this->assertNull($module_handler->invoke('module_handler_test_fake', 'hook', [FALSE]), 'Installed module runs hook.');
}
/**
* @covers ::loadAllIncludes
*/
public function testImplementsHookModuleEnabled() {
- $this->assertTrue($this->moduleHandler->implementsHook('module_handler_test', 'hook'), 'Installed module implementation found.');
+ $module_handler = $this->getModuleHandler();
+ $this->assertTrue($module_handler->implementsHook('module_handler_test', 'hook'), 'Installed module implementation found.');
- $this->moduleHandler->addModule('module_handler_test_added', 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_added');
- $this->assertTrue($this->moduleHandler->implementsHook('module_handler_test_added', 'hook'), 'Runtime added module with implementation in include found.');
+ $module_handler->addModule('module_handler_test_added', 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_added');
+ $this->assertTrue($module_handler->implementsHook('module_handler_test_added', 'hook'), 'Runtime added module with implementation in include found.');
- $this->moduleHandler->addModule('module_handler_test_no_hook', 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_added');
- $this->assertFalse($this->moduleHandler->implementsHook('module_handler_test_no_hook', 'hook', [TRUE]), 'Missing implementation not found.');
+ $module_handler->addModule('module_handler_test_no_hook', 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_added');
+ $this->assertFalse($module_handler->implementsHook('module_handler_test_no_hook', 'hook', [TRUE]), 'Missing implementation not found.');
}
/**
* @covers ::buildImplementationInfo
*/
public function testGetImplementations() {
- $this->assertEquals(['module_handler_test'], $this->moduleHandler->getImplementations('hook'));
+ $this->assertEquals(['module_handler_test'], $this->getModuleHandler()->getImplementations('hook'));
}
/**
));
// Ensure buildImplementationInfo doesn't get called and that we work off cached results.
- $module_handler = $this->getMockBuilder('Drupal\Core\Extension\ModuleHandler')
+ $module_handler = $this->getMockBuilder(ModuleHandler::class)
->setConstructorArgs([
$this->root, [
'module_handler_test' => [
])
->setMethods(['buildImplementationInfo', 'loadInclude'])
->getMock();
+ $module_handler->load('module_handler_test');
$module_handler->expects($this->never())->method('buildImplementationInfo');
$module_handler->expects($this->once())->method('loadInclude');
public function testCachedGetImplementationsMissingMethod() {
$this->cacheBackend->expects($this->exactly(1))
->method('get')
- ->will($this->onConsecutiveCalls(
- (object) ['data' => ['hook' => [
- 'module_handler_test' => [],
- 'module_handler_test_missing' => [],
- ]]]
- ));
+ ->will($this->onConsecutiveCalls((object) [
+ 'data' => [
+ 'hook' => [
+ 'module_handler_test' => [],
+ 'module_handler_test_missing' => [],
+ ],
+ ],
+ ]));
// Ensure buildImplementationInfo doesn't get called and that we work off cached results.
- $module_handler = $this->getMockBuilder('Drupal\Core\Extension\ModuleHandler')
+ $module_handler = $this->getMockBuilder(ModuleHandler::class)
->setConstructorArgs([
$this->root, [
'module_handler_test' => [
])
->setMethods(['buildImplementationInfo'])
->getMock();
+ $module_handler->load('module_handler_test');
$module_handler->expects($this->never())->method('buildImplementationInfo');
$this->assertEquals(['module_handler_test'], $module_handler->getImplementations('hook'));
* @covers ::invokeAll
*/
public function testInvokeAll() {
- $this->moduleHandler->addModule('module_handler_test_all1', 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_all1');
- $this->moduleHandler->addModule('module_handler_test_all2', 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_all2');
- $this->assertEquals([TRUE, TRUE, TRUE], $this->moduleHandler->invokeAll('hook', [TRUE]));
+ $module_handler = $this->getModuleHandler();
+ $module_handler->addModule('module_handler_test_all1', 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_all1');
+ $module_handler->addModule('module_handler_test_all2', 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_all2');
+ $this->assertEquals([TRUE, TRUE, TRUE], $module_handler->invokeAll('hook', [TRUE]));
}
/**
* @covers ::writeCache
*/
public function testWriteCache() {
+ $module_handler = $this->getModuleHandler();
$this->cacheBackend
->expects($this->exactly(2))
->method('get')
->expects($this->exactly(2))
->method('set')
->with($this->logicalOr('module_implements', 'hook_info'));
- $this->moduleHandler->getImplementations('hook');
- $this->moduleHandler->writeCache();
+ $module_handler->getImplementations('hook');
+ $module_handler->writeCache();
}
/**
* @covers ::buildHookInfo
*/
public function testGetHookInfo() {
-
+ $module_handler = $this->getModuleHandler();
// Set up some synthetic results.
$this->cacheBackend
->expects($this->exactly(2))
->method('get')
->will($this->onConsecutiveCalls(
NULL,
- (object) ['data' =>
- ['hook_foo' => ['group' => 'hook']]])
- );
+ (object) ['data' => ['hook_foo' => ['group' => 'hook']]]
+ ));
// Results from building from mocked environment.
$this->assertEquals([
'hook' => ['group' => 'hook'],
- ], $this->moduleHandler->getHookInfo());
+ ], $module_handler->getHookInfo());
// Reset local cache so we get our synthetic result from the cache handler.
- $this->moduleHandler->resetImplementations();
+ $module_handler->resetImplementations();
$this->assertEquals([
'hook_foo' => ['group' => 'hook'],
- ], $this->moduleHandler->getHookInfo());
+ ], $module_handler->getHookInfo());
}
/**
* @covers ::resetImplementations
*/
public function testResetImplementations() {
-
+ $module_handler = $this->getModuleHandler();
// Prime caches
- $this->moduleHandler->getImplementations('hook');
- $this->moduleHandler->getHookInfo();
+ $module_handler->getImplementations('hook');
+ $module_handler->getHookInfo();
// Reset all caches internal and external.
$this->cacheBackend
// reset sets module_implements to array() and getHookInfo later
// populates hook_info.
->with($this->logicalOr('module_implements', 'hook_info'));
- $this->moduleHandler->resetImplementations();
+ $module_handler->resetImplementations();
// Request implementation and ensure hook_info and module_implements skip
// local caches.
->expects($this->exactly(2))
->method('get')
->with($this->logicalOr('module_implements', 'hook_info'));
- $this->moduleHandler->getImplementations('hook');
+ $module_handler->getImplementations('hook');
}
/**
* @covers ::getModuleDirectories
*/
public function testGetModuleDirectories() {
- $this->moduleHandler->setModuleList([]);
- $this->moduleHandler->addModule('module', 'place');
- $this->assertEquals(['module' => $this->root . '/place'], $this->moduleHandler->getModuleDirectories());
+ $module_handler = $this->getModuleHandler();
+ $module_handler->setModuleList([]);
+ $module_handler->addModule('module', 'place');
+ $this->assertEquals(['module' => $this->root . '/place'], $module_handler->getModuleDirectories());
}
}