3 namespace Drupal\system\Tests\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\simpletest\WebTestBase;
12 * Helper class for module test cases.
14 * @deprecated Scheduled for removal in Drupal 9.0.0.
15 * Use \Drupal\Tests\system\Functional\Module\ModuleTestBase instead.
17 abstract class ModuleTestBase extends WebTestBase {
24 public static $modules = ['system_test'];
28 protected function setUp() {
31 $this->adminUser = $this->drupalCreateUser(['access administration pages', 'administer modules']);
32 $this->drupalLogin($this->adminUser);
36 * Assert there are tables that begin with the specified base table name.
39 * Beginning of table name to look for.
41 * (optional) Whether or not to assert that there are tables that match the
42 * specified base table. Defaults to TRUE.
44 public function assertTableCount($base_table, $count = TRUE) {
45 $tables = db_find_tables(Database::getConnection()->prefixTables('{' . $base_table . '}') . '%');
48 return $this->assertTrue($tables, format_string('Tables matching "@base_table" found.', ['@base_table' => $base_table]));
50 return $this->assertFalse($tables, format_string('Tables matching "@base_table" not found.', ['@base_table' => $base_table]));
54 * Assert that all tables defined in a module's hook_schema() exist.
57 * The name of the module.
59 public function assertModuleTablesExist($module) {
60 $tables = array_keys(drupal_get_module_schema($module));
62 foreach ($tables as $table) {
63 if (!db_table_exists($table)) {
64 $tables_exist = FALSE;
67 return $this->assertTrue($tables_exist, format_string('All database tables defined by the @module module exist.', ['@module' => $module]));
71 * Assert that none of the tables defined in a module's hook_schema() exist.
74 * The name of the module.
76 public function assertModuleTablesDoNotExist($module) {
77 $tables = array_keys(drupal_get_module_schema($module));
78 $tables_exist = FALSE;
79 foreach ($tables as $table) {
80 if (db_table_exists($table)) {
84 return $this->assertFalse($tables_exist, format_string('None of the database tables defined by the @module module exist.', ['@module' => $module]));
88 * Asserts that the default configuration of a module has been installed.
90 * @param string $module
91 * The name of the module.
94 * TRUE if configuration has been installed, FALSE otherwise.
96 public function assertModuleConfig($module) {
97 $module_config_dir = drupal_get_path('module', $module) . '/' . InstallStorage::CONFIG_INSTALL_DIRECTORY;
98 if (!is_dir($module_config_dir)) {
101 $module_file_storage = new FileStorage($module_config_dir);
103 // Verify that the module's default config directory is not empty and
104 // contains default configuration files (instead of something else).
105 $all_names = $module_file_storage->listAll();
106 if (empty($all_names)) {
107 // Module has an empty config directory. For example it might contain a
111 $this->assertTrue($all_names);
113 // Look up each default configuration object name in the active
114 // configuration, and if it exists, remove it from the stack.
115 // Only default config that belongs to $module is guaranteed to exist; any
116 // other default config depends on whether other modules are enabled. Thus,
117 // list all default config once more, but filtered by $module.
118 $names = $module_file_storage->listAll($module . '.');
119 foreach ($names as $key => $name) {
120 if ($this->config($name)->get()) {
124 // Verify that all configuration has been installed (which means that $names
126 return $this->assertFalse($names, format_string('All default configuration of @module module found.', ['@module' => $module]));
130 * Asserts that no configuration exists for a given module.
132 * @param string $module
133 * The name of the module.
136 * TRUE if no configuration was found, FALSE otherwise.
138 public function assertNoModuleConfig($module) {
139 $names = \Drupal::configFactory()->listAll($module . '.');
140 return $this->assertFalse($names, format_string('No configuration found for @module module.', ['@module' => $module]));
144 * Assert the list of modules are enabled or disabled.
147 * Module list to check.
149 * Expected module state.
151 public function assertModules(array $modules, $enabled) {
152 $this->rebuildContainer();
153 foreach ($modules as $module) {
155 $message = 'Module "@module" is enabled.';
158 $message = 'Module "@module" is not enabled.';
160 $this->assertEqual($this->container->get('module_handler')->moduleExists($module), $enabled, format_string($message, ['@module' => $module]));
165 * Verify a log entry was entered for a module's status change.
168 * The category to which this message belongs.
170 * The message to store in the log. Keep $message translatable
171 * by not concatenating dynamic values into it! Variables in the
172 * message should be added by using placeholder strings alongside
173 * the variables argument to declare the value of the placeholders.
174 * See t() for documentation on how $message and $variables interact.
176 * Array of variables to replace in the message on display or
177 * NULL if message is already translated or not possible to
180 * The severity of the message, as per RFC 3164.
182 * A link to associate with the message.
184 public function assertLogMessage($type, $message, $variables = [], $severity = RfcLogLevel::NOTICE, $link = '') {
185 $count = db_select('watchdog', 'w')
186 ->condition('type', $type)
187 ->condition('message', $message)
188 ->condition('variables', serialize($variables))
189 ->condition('severity', $severity)
190 ->condition('link', $link)
194 $this->assertTrue($count > 0, format_string('watchdog table contains @count rows for @message', ['@count' => $count, '@message' => format_string($message, $variables)]));