3 namespace Drupal\Tests\system\Functional\Module;
5 use Drupal\Core\Config\InstallStorage;
6 use Drupal\Core\Database\Database;
7 use Drupal\Core\Config\FileStorage;
8 use Drupal\Core\Logger\RfcLogLevel;
9 use Drupal\Tests\BrowserTestBase;
12 * Helper class for module test cases.
14 abstract class ModuleTestBase extends BrowserTestBase {
21 public static $modules = ['system_test'];
25 protected function setUp() {
28 $this->adminUser = $this->drupalCreateUser(['access administration pages', 'administer modules']);
29 $this->drupalLogin($this->adminUser);
33 * Assert there are tables that begin with the specified base table name.
36 * Beginning of table name to look for.
38 * (optional) Whether or not to assert that there are tables that match the
39 * specified base table. Defaults to TRUE.
41 public function assertTableCount($base_table, $count = TRUE) {
42 $tables = db_find_tables(Database::getConnection()->prefixTables('{' . $base_table . '}') . '%');
45 return $this->assertTrue($tables, format_string('Tables matching "@base_table" found.', ['@base_table' => $base_table]));
47 return $this->assertFalse($tables, format_string('Tables matching "@base_table" not found.', ['@base_table' => $base_table]));
51 * Assert that all tables defined in a module's hook_schema() exist.
54 * The name of the module.
56 public function assertModuleTablesExist($module) {
57 $tables = array_keys(drupal_get_module_schema($module));
59 foreach ($tables as $table) {
60 if (!db_table_exists($table)) {
61 $tables_exist = FALSE;
64 return $this->assertTrue($tables_exist, format_string('All database tables defined by the @module module exist.', ['@module' => $module]));
68 * Assert that none of the tables defined in a module's hook_schema() exist.
71 * The name of the module.
73 public function assertModuleTablesDoNotExist($module) {
74 $tables = array_keys(drupal_get_module_schema($module));
75 $tables_exist = FALSE;
76 foreach ($tables as $table) {
77 if (db_table_exists($table)) {
81 return $this->assertFalse($tables_exist, format_string('None of the database tables defined by the @module module exist.', ['@module' => $module]));
85 * Asserts that the default configuration of a module has been installed.
87 * @param string $module
88 * The name of the module.
91 * TRUE if configuration has been installed, FALSE otherwise.
93 public function assertModuleConfig($module) {
94 $module_config_dir = drupal_get_path('module', $module) . '/' . InstallStorage::CONFIG_INSTALL_DIRECTORY;
95 if (!is_dir($module_config_dir)) {
98 $module_file_storage = new FileStorage($module_config_dir);
100 // Verify that the module's default config directory is not empty and
101 // contains default configuration files (instead of something else).
102 $all_names = $module_file_storage->listAll();
103 if (empty($all_names)) {
104 // Module has an empty config directory. For example it might contain a
108 $this->assertTrue($all_names);
110 // Look up each default configuration object name in the active
111 // configuration, and if it exists, remove it from the stack.
112 // Only default config that belongs to $module is guaranteed to exist; any
113 // other default config depends on whether other modules are enabled. Thus,
114 // list all default config once more, but filtered by $module.
115 $names = $module_file_storage->listAll($module . '.');
116 foreach ($names as $key => $name) {
117 if ($this->config($name)->get()) {
121 // Verify that all configuration has been installed (which means that $names
123 return $this->assertFalse($names, format_string('All default configuration of @module module found.', ['@module' => $module]));
127 * Asserts that no configuration exists for a given module.
129 * @param string $module
130 * The name of the module.
133 * TRUE if no configuration was found, FALSE otherwise.
135 public function assertNoModuleConfig($module) {
136 $names = \Drupal::configFactory()->listAll($module . '.');
137 return $this->assertFalse($names, format_string('No configuration found for @module module.', ['@module' => $module]));
141 * Assert the list of modules are enabled or disabled.
144 * Module list to check.
146 * Expected module state.
148 public function assertModules(array $modules, $enabled) {
149 $this->rebuildContainer();
150 foreach ($modules as $module) {
152 $message = 'Module "@module" is enabled.';
155 $message = 'Module "@module" is not enabled.';
157 $this->assertEqual($this->container->get('module_handler')->moduleExists($module), $enabled, format_string($message, ['@module' => $module]));
162 * Verify a log entry was entered for a module's status change.
165 * The category to which this message belongs.
167 * The message to store in the log. Keep $message translatable
168 * by not concatenating dynamic values into it! Variables in the
169 * message should be added by using placeholder strings alongside
170 * the variables argument to declare the value of the placeholders.
171 * See t() for documentation on how $message and $variables interact.
173 * Array of variables to replace in the message on display or
174 * NULL if message is already translated or not possible to
177 * The severity of the message, as per RFC 3164.
179 * A link to associate with the message.
181 public function assertLogMessage($type, $message, $variables = [], $severity = RfcLogLevel::NOTICE, $link = '') {
182 $count = db_select('watchdog', 'w')
183 ->condition('type', $type)
184 ->condition('message', $message)
185 ->condition('variables', serialize($variables))
186 ->condition('severity', $severity)
187 ->condition('link', $link)
191 $this->assertTrue($count > 0, format_string('watchdog table contains @count rows for @message', ['@count' => $count, '@message' => format_string($message, $variables)]));