View source
<?php
namespace Drupal\Tests\system\Functional\Module;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Config\InstallStorage;
use Drupal\Core\Database\Database;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\Tests\BrowserTestBase;
use Drupal\TestTools\Extension\SchemaInspector;
abstract class ModuleTestBase extends BrowserTestBase {
protected static $modules = [
'system_test',
];
protected $adminUser;
protected function setUp() : void {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'access administration pages',
'administer modules',
]);
$this
->drupalLogin($this->adminUser);
}
public function assertModuleTablesExist($module) {
$tables = array_keys(SchemaInspector::getTablesSpecification(\Drupal::moduleHandler(), $module));
$tables_exist = TRUE;
$schema = Database::getConnection()
->schema();
foreach ($tables as $table) {
if (!$schema
->tableExists($table)) {
$tables_exist = FALSE;
}
}
$this
->assertTrue($tables_exist, new FormattableMarkup('All database tables defined by the @module module exist.', [
'@module' => $module,
]));
}
public function assertModuleTablesDoNotExist($module) {
$tables = array_keys(SchemaInspector::getTablesSpecification(\Drupal::moduleHandler(), $module));
$tables_exist = FALSE;
$schema = Database::getConnection()
->schema();
foreach ($tables as $table) {
if ($schema
->tableExists($table)) {
$tables_exist = TRUE;
}
}
$this
->assertFalse($tables_exist, new FormattableMarkup('None of the database tables defined by the @module module exist.', [
'@module' => $module,
]));
}
public function assertModuleConfig($module) {
$module_config_dir = $this
->getModulePath($module) . '/' . InstallStorage::CONFIG_INSTALL_DIRECTORY;
if (!is_dir($module_config_dir)) {
return;
}
$module_file_storage = new FileStorage($module_config_dir);
$all_names = $module_file_storage
->listAll();
if (empty($all_names)) {
return;
}
$this
->assertNotEmpty($all_names);
$module_config_dependencies = \Drupal::service('config.manager')
->findConfigEntityDependencies('module', [
$module,
]);
$names = $module_file_storage
->listAll();
foreach ($names as $key => $name) {
if ($this
->config($name)
->get()) {
unset($names[$key]);
}
$this
->assertTrue(strpos($name, $module . '.') === 0 || isset($module_config_dependencies[$name]), "Configuration {$name} provided by {$module} in its config/install directory does not depend on it.");
}
$this
->assertEmpty($names, new FormattableMarkup('All default configuration of @module module found.', [
'@module' => $module,
]));
}
public function assertNoModuleConfig($module) {
$names = \Drupal::configFactory()
->listAll($module . '.');
$this
->assertEmpty($names, new FormattableMarkup('No configuration found for @module module.', [
'@module' => $module,
]));
}
public function assertModules(array $modules, $enabled) {
$this
->rebuildContainer();
foreach ($modules as $module) {
if ($enabled) {
$message = 'Module "@module" is enabled.';
}
else {
$message = 'Module "@module" is not enabled.';
}
$this
->assertEquals($enabled, $this->container
->get('module_handler')
->moduleExists($module), new FormattableMarkup($message, [
'@module' => $module,
]));
}
}
public function assertLogMessage($type, $message, $variables = [], $severity = RfcLogLevel::NOTICE, $link = '') {
$this
->assertNotEmpty(Database::getConnection()
->select('watchdog', 'w')
->condition('type', $type)
->condition('message', $message)
->condition('variables', serialize($variables))
->condition('severity', $severity)
->condition('link', $link)
->countQuery()
->execute()
->fetchField());
}
}