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 $schema = Database::getConnection()->schema();
63 foreach ($tables as $table) {
64 if (!$schema->tableExists($table)) {
65 $tables_exist = FALSE;
68 return $this->assertTrue($tables_exist, format_string('All database tables defined by the @module module exist.', ['@module' => $module]));
72 * Assert that none of the tables defined in a module's hook_schema() exist.
75 * The name of the module.
77 public function assertModuleTablesDoNotExist($module) {
78 $tables = array_keys(drupal_get_module_schema($module));
79 $tables_exist = FALSE;
80 $schema = Database::getConnection()->schema();
81 foreach ($tables as $table) {
82 if ($schema->tableExists($table)) {
86 return $this->assertFalse($tables_exist, format_string('None of the database tables defined by the @module module exist.', ['@module' => $module]));
90 * Asserts that the default configuration of a module has been installed.
92 * @param string $module
93 * The name of the module.
96 * TRUE if configuration has been installed, FALSE otherwise. Returns NULL
97 * if the module configuration directory does not exist or does not contain
98 * any configuration files.
100 public function assertModuleConfig($module) {
101 $module_config_dir = drupal_get_path('module', $module) . '/' . InstallStorage::CONFIG_INSTALL_DIRECTORY;
102 if (!is_dir($module_config_dir)) {
105 $module_file_storage = new FileStorage($module_config_dir);
107 // Verify that the module's default config directory is not empty and
108 // contains default configuration files (instead of something else).
109 $all_names = $module_file_storage->listAll();
110 if (empty($all_names)) {
111 // Module has an empty config directory. For example it might contain a
115 $this->assertTrue($all_names);
117 // Look up each default configuration object name in the active
118 // configuration, and if it exists, remove it from the stack.
119 // Only default config that belongs to $module is guaranteed to exist; any
120 // other default config depends on whether other modules are enabled. Thus,
121 // list all default config once more, but filtered by $module.
122 $names = $module_file_storage->listAll($module . '.');
123 foreach ($names as $key => $name) {
124 if ($this->config($name)->get()) {
128 // Verify that all configuration has been installed (which means that $names
130 return $this->assertFalse($names, format_string('All default configuration of @module module found.', ['@module' => $module]));
134 * Asserts that no configuration exists for a given module.
136 * @param string $module
137 * The name of the module.
140 * TRUE if no configuration was found, FALSE otherwise.
142 public function assertNoModuleConfig($module) {
143 $names = \Drupal::configFactory()->listAll($module . '.');
144 return $this->assertFalse($names, format_string('No configuration found for @module module.', ['@module' => $module]));
148 * Assert the list of modules are enabled or disabled.
151 * Module list to check.
153 * Expected module state.
155 public function assertModules(array $modules, $enabled) {
156 $this->rebuildContainer();
157 foreach ($modules as $module) {
159 $message = 'Module "@module" is enabled.';
162 $message = 'Module "@module" is not enabled.';
164 $this->assertEqual($this->container->get('module_handler')->moduleExists($module), $enabled, format_string($message, ['@module' => $module]));
169 * Verify a log entry was entered for a module's status change.
172 * The category to which this message belongs.
174 * The message to store in the log. Keep $message translatable
175 * by not concatenating dynamic values into it! Variables in the
176 * message should be added by using placeholder strings alongside
177 * the variables argument to declare the value of the placeholders.
178 * See t() for documentation on how $message and $variables interact.
180 * Array of variables to replace in the message on display or
181 * NULL if message is already translated or not possible to
184 * The severity of the message, as per RFC 3164.
186 * A link to associate with the message.
188 public function assertLogMessage($type, $message, $variables = [], $severity = RfcLogLevel::NOTICE, $link = '') {
189 $count = db_select('watchdog', 'w')
190 ->condition('type', $type)
191 ->condition('message', $message)
192 ->condition('variables', serialize($variables))
193 ->condition('severity', $severity)
194 ->condition('link', $link)
198 $this->assertTrue($count > 0, format_string('watchdog table contains @count rows for @message', ['@count' => $count, '@message' => format_string($message, $variables)]));