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 $schema = Database::getConnection()->schema();
60 foreach ($tables as $table) {
61 if (!$schema->tableExists($table)) {
62 $tables_exist = FALSE;
65 return $this->assertTrue($tables_exist, format_string('All database tables defined by the @module module exist.', ['@module' => $module]));
69 * Assert that none of the tables defined in a module's hook_schema() exist.
72 * The name of the module.
74 public function assertModuleTablesDoNotExist($module) {
75 $tables = array_keys(drupal_get_module_schema($module));
76 $tables_exist = FALSE;
77 $schema = Database::getConnection()->schema();
78 foreach ($tables as $table) {
79 if ($schema->tableExists($table)) {
83 return $this->assertFalse($tables_exist, format_string('None of the database tables defined by the @module module exist.', ['@module' => $module]));
87 * Asserts that the default configuration of a module has been installed.
89 * @param string $module
90 * The name of the module.
93 * TRUE if configuration has been installed, FALSE otherwise. Returns NULL
94 * if the module configuration directory does not exist or does not contain
95 * any configuration files.
97 public function assertModuleConfig($module) {
98 $module_config_dir = drupal_get_path('module', $module) . '/' . InstallStorage::CONFIG_INSTALL_DIRECTORY;
99 if (!is_dir($module_config_dir)) {
102 $module_file_storage = new FileStorage($module_config_dir);
104 // Verify that the module's default config directory is not empty and
105 // contains default configuration files (instead of something else).
106 $all_names = $module_file_storage->listAll();
107 if (empty($all_names)) {
108 // Module has an empty config directory. For example it might contain a
112 $this->assertTrue($all_names);
114 $module_config_dependencies = \Drupal::service('config.manager')->findConfigEntityDependents('module', [$module]);
115 // Look up each default configuration object name in the active
116 // configuration, and if it exists, remove it from the stack.
117 $names = $module_file_storage->listAll();
118 foreach ($names as $key => $name) {
119 if ($this->config($name)->get()) {
122 // All configuration in a module's config/install directory should depend
123 // on the module as it must be removed on uninstall or the module will not
124 // be re-installable.
125 $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.");
127 // Verify that all configuration has been installed (which means that $names
129 return $this->assertFalse($names, format_string('All default configuration of @module module found.', ['@module' => $module]));
133 * Asserts that no configuration exists for a given module.
135 * @param string $module
136 * The name of the module.
139 * TRUE if no configuration was found, FALSE otherwise.
141 public function assertNoModuleConfig($module) {
142 $names = \Drupal::configFactory()->listAll($module . '.');
143 return $this->assertFalse($names, format_string('No configuration found for @module module.', ['@module' => $module]));
147 * Assert the list of modules are enabled or disabled.
150 * Module list to check.
152 * Expected module state.
154 public function assertModules(array $modules, $enabled) {
155 $this->rebuildContainer();
156 foreach ($modules as $module) {
158 $message = 'Module "@module" is enabled.';
161 $message = 'Module "@module" is not enabled.';
163 $this->assertEqual($this->container->get('module_handler')->moduleExists($module), $enabled, format_string($message, ['@module' => $module]));
168 * Verify a log entry was entered for a module's status change.
171 * The category to which this message belongs.
173 * The message to store in the log. Keep $message translatable
174 * by not concatenating dynamic values into it! Variables in the
175 * message should be added by using placeholder strings alongside
176 * the variables argument to declare the value of the placeholders.
177 * See t() for documentation on how $message and $variables interact.
179 * Array of variables to replace in the message on display or
180 * NULL if message is already translated or not possible to
183 * The severity of the message, as per RFC 3164.
185 * A link to associate with the message.
187 public function assertLogMessage($type, $message, $variables = [], $severity = RfcLogLevel::NOTICE, $link = '') {
188 $count = db_select('watchdog', 'w')
189 ->condition('type', $type)
190 ->condition('message', $message)
191 ->condition('variables', serialize($variables))
192 ->condition('severity', $severity)
193 ->condition('link', $link)
197 $this->assertTrue($count > 0, format_string('watchdog table contains @count rows for @message', ['@count' => $count, '@message' => format_string($message, $variables)]));