View source
<?php
namespace Drupal\Tests\Core\Extension;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\Extension;
use Drupal\Core\Extension\ModuleHandler;
use Drupal\Core\Extension\Exception\UnknownExtensionException;
use Drupal\Tests\UnitTestCase;
class ModuleHandlerTest extends UnitTestCase {
protected $cacheBackend;
protected function setUp() {
parent::setUp();
$this->cacheBackend = $this
->createMock(CacheBackendInterface::class);
}
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;
}
public function testLoadModule() {
$module_handler = $this
->getModuleHandler();
$this
->assertFalse(function_exists('module_handler_test_hook'));
$this
->assertTrue($module_handler
->load('module_handler_test'));
$this
->assertTrue(function_exists('module_handler_test_hook'));
$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($module_handler
->load('module_handler_test_added'));
$this
->assertTrue(function_exists('module_handler_test_added_helper'), 'Function exists after being loaded.');
$this
->assertTrue($module_handler
->load('module_handler_test_added'));
$this
->assertFalse($module_handler
->load('module_handler_test_dne'), 'Non-existent modules returns false.');
}
public function testLoadAllModules() {
$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.');
$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.');
}
public function testModuleReloading() {
$module_handler = $this
->getMockBuilder(ModuleHandler::class)
->setConstructorArgs([
$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,
])
->setMethods([
'load',
])
->getMock();
$module_handler
->expects($this
->at(0))
->method('load')
->with($this
->equalTo('module_handler_test'));
$module_handler
->expects($this
->at(1))
->method('load')
->with($this
->equalTo('module_handler_test'));
$module_handler
->expects($this
->at(2))
->method('load')
->with($this
->equalTo('module_handler_test_added'));
$module_handler
->reload();
$module_handler
->addModule('module_handler_test_added', 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_added');
$module_handler
->reload();
}
public function testIsLoaded() {
$module_handler = $this
->getModuleHandler();
$this
->assertFalse($module_handler
->isLoaded());
$module_handler
->loadAll();
$this
->assertTrue($module_handler
->isLoaded());
}
public function testGetModuleList() {
$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'),
]);
}
public function testGetModuleWithExistingModule() {
$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
->expectException(UnknownExtensionException::class);
$this
->getModuleHandler()
->getModule('claire_alice_watch_my_little_pony_module_that_does_not_exist');
}
public function testSetModuleList() {
$fixture_module_handler = $this
->getModuleHandler();
$module_handler = $this
->getMockBuilder(ModuleHandler::class)
->setConstructorArgs([
$this->root,
[],
$this->cacheBackend,
])
->setMethods([
'resetImplementations',
])
->getMock();
$module_handler
->expects($this
->once())
->method('resetImplementations');
$this
->assertEquals($module_handler
->getModuleList(), []);
$module_handler
->setModuleList($fixture_module_handler
->getModuleList());
$this
->assertEquals($fixture_module_handler
->getModuleList(), $module_handler
->getModuleList());
}
public function testAddModule() {
$module_handler = $this
->getMockBuilder(ModuleHandler::class)
->setConstructorArgs([
$this->root,
[],
$this->cacheBackend,
])
->setMethods([
'resetImplementations',
])
->getMock();
$module_handler
->expects($this
->once())
->method('resetImplementations');
$module_handler
->addModule('module_handler_test', 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test');
$this
->assertTrue($module_handler
->moduleExists('module_handler_test'));
}
public function testAddProfile() {
$module_handler = $this
->getMockBuilder(ModuleHandler::class)
->setConstructorArgs([
$this->root,
[],
$this->cacheBackend,
])
->setMethods([
'resetImplementations',
])
->getMock();
$module_handler
->expects($this
->once())
->method('resetImplementations');
$module_handler
->addProfile('module_handler_test', 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test');
$this
->assertTrue($module_handler
->moduleExists('module_handler_test'));
}
public function testModuleExists() {
$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(ModuleHandler::class)
->setConstructorArgs([
$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,
])
->setMethods([
'loadInclude',
])
->getMock();
$module_handler
->expects($this
->once())
->method('loadInclude');
$module_handler
->loadAllIncludes('hook');
}
public function testLoadInclude() {
$module_handler = $this
->getModuleHandler();
$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'));
$this
->assertFalse($module_handler
->loadInclude('module_handler_test', 'install'));
}
public function testInvokeModuleEnabled() {
$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.');
}
public function testImplementsHookModuleEnabled() {
$module_handler = $this
->getModuleHandler();
$this
->assertTrue($module_handler
->implementsHook('module_handler_test', 'hook'), 'Installed module implementation 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.');
$module_handler
->addModule('module_handler_test_no_hook', 'core/tests/Drupal/Tests/Core/Extension/modules/module_handler_test_no_hook');
$this
->assertFalse($module_handler
->implementsHook('module_handler_test_no_hook', 'hook', [
TRUE,
]), 'Missing implementation not found.');
}
public function testGetImplementations() {
$this
->assertEquals([
'module_handler_test',
], $this
->getModuleHandler()
->getImplementations('hook'));
}
public function testCachedGetImplementations() {
$this->cacheBackend
->expects($this
->exactly(1))
->method('get')
->will($this
->onConsecutiveCalls((object) [
'data' => [
'hook' => [
'module_handler_test' => 'test',
],
],
]));
$module_handler = $this
->getMockBuilder(ModuleHandler::class)
->setConstructorArgs([
$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,
])
->setMethods([
'buildImplementationInfo',
'loadInclude',
])
->getMock();
$module_handler
->load('module_handler_test');
$module_handler
->expects($this
->never())
->method('buildImplementationInfo');
$module_handler
->expects($this
->once())
->method('loadInclude');
$this
->assertEquals([
'module_handler_test',
], $module_handler
->getImplementations('hook'));
}
public function testCachedGetImplementationsMissingMethod() {
$this->cacheBackend
->expects($this
->exactly(1))
->method('get')
->will($this
->onConsecutiveCalls((object) [
'data' => [
'hook' => [
'module_handler_test' => [],
'module_handler_test_missing' => [],
],
],
]));
$module_handler = $this
->getMockBuilder(ModuleHandler::class)
->setConstructorArgs([
$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,
])
->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'));
}
public function testInvokeAll() {
$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,
]));
}
public function testWriteCache() {
$module_handler = $this
->getModuleHandler();
$this->cacheBackend
->expects($this
->exactly(2))
->method('get')
->will($this
->returnValue(NULL));
$this->cacheBackend
->expects($this
->exactly(2))
->method('set')
->with($this
->logicalOr('module_implements', 'hook_info'));
$module_handler
->getImplementations('hook');
$module_handler
->writeCache();
}
public function testGetHookInfo() {
$module_handler = $this
->getModuleHandler();
$this->cacheBackend
->expects($this
->exactly(2))
->method('get')
->will($this
->onConsecutiveCalls(NULL, (object) [
'data' => [
'hook_foo' => [
'group' => 'hook',
],
],
]));
$this
->assertEquals([
'hook' => [
'group' => 'hook',
],
], $module_handler
->getHookInfo());
$module_handler
->resetImplementations();
$this
->assertEquals([
'hook_foo' => [
'group' => 'hook',
],
], $module_handler
->getHookInfo());
}
public function testResetImplementations() {
$module_handler = $this
->getModuleHandler();
$module_handler
->getImplementations('hook');
$module_handler
->getHookInfo();
$this->cacheBackend
->expects($this
->once())
->method('delete')
->with('hook_info');
$this->cacheBackend
->expects($this
->exactly(2))
->method('set')
->with($this
->logicalOr('module_implements', 'hook_info'));
$module_handler
->resetImplementations();
$this->cacheBackend
->expects($this
->exactly(2))
->method('get')
->with($this
->logicalOr('module_implements', 'hook_info'));
$module_handler
->getImplementations('hook');
}
public function testGetModuleDirectories() {
$module_handler = $this
->getModuleHandler();
$module_handler
->setModuleList([]);
$module_handler
->addModule('node', 'core/modules/node');
$this
->assertEquals([
'node' => $this->root . '/core/modules/node',
], $module_handler
->getModuleDirectories());
}
}