Chris@0: adminUser = $this->drupalCreateUser(['access administration pages', 'administer modules']); Chris@0: $this->drupalLogin($this->adminUser); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Assert there are tables that begin with the specified base table name. Chris@0: * Chris@0: * @param $base_table Chris@0: * Beginning of table name to look for. Chris@0: * @param $count Chris@0: * (optional) Whether or not to assert that there are tables that match the Chris@0: * specified base table. Defaults to TRUE. Chris@0: */ Chris@0: public function assertTableCount($base_table, $count = TRUE) { Chris@18: $connection = Database::getConnection(); Chris@18: $tables = $connection->schema()->findTables($connection->prefixTables('{' . $base_table . '}') . '%'); Chris@0: Chris@0: if ($count) { Chris@0: return $this->assertTrue($tables, format_string('Tables matching "@base_table" found.', ['@base_table' => $base_table])); Chris@0: } Chris@0: return $this->assertFalse($tables, format_string('Tables matching "@base_table" not found.', ['@base_table' => $base_table])); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Assert that all tables defined in a module's hook_schema() exist. Chris@0: * Chris@0: * @param $module Chris@0: * The name of the module. Chris@0: */ Chris@0: public function assertModuleTablesExist($module) { Chris@0: $tables = array_keys(drupal_get_module_schema($module)); Chris@0: $tables_exist = TRUE; Chris@17: $schema = Database::getConnection()->schema(); Chris@0: foreach ($tables as $table) { Chris@17: if (!$schema->tableExists($table)) { Chris@0: $tables_exist = FALSE; Chris@0: } Chris@0: } Chris@0: return $this->assertTrue($tables_exist, format_string('All database tables defined by the @module module exist.', ['@module' => $module])); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Assert that none of the tables defined in a module's hook_schema() exist. Chris@0: * Chris@0: * @param $module Chris@0: * The name of the module. Chris@0: */ Chris@0: public function assertModuleTablesDoNotExist($module) { Chris@0: $tables = array_keys(drupal_get_module_schema($module)); Chris@0: $tables_exist = FALSE; Chris@17: $schema = Database::getConnection()->schema(); Chris@0: foreach ($tables as $table) { Chris@17: if ($schema->tableExists($table)) { Chris@0: $tables_exist = TRUE; Chris@0: } Chris@0: } Chris@0: return $this->assertFalse($tables_exist, format_string('None of the database tables defined by the @module module exist.', ['@module' => $module])); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts that the default configuration of a module has been installed. Chris@0: * Chris@0: * @param string $module Chris@0: * The name of the module. Chris@0: * Chris@0: * @return bool|null Chris@0: * TRUE if configuration has been installed, FALSE otherwise. Returns NULL Chris@0: * if the module configuration directory does not exist or does not contain Chris@0: * any configuration files. Chris@0: */ Chris@0: public function assertModuleConfig($module) { Chris@0: $module_config_dir = drupal_get_path('module', $module) . '/' . InstallStorage::CONFIG_INSTALL_DIRECTORY; Chris@0: if (!is_dir($module_config_dir)) { Chris@0: return; Chris@0: } Chris@0: $module_file_storage = new FileStorage($module_config_dir); Chris@0: Chris@0: // Verify that the module's default config directory is not empty and Chris@0: // contains default configuration files (instead of something else). Chris@0: $all_names = $module_file_storage->listAll(); Chris@0: if (empty($all_names)) { Chris@0: // Module has an empty config directory. For example it might contain a Chris@0: // schema directory. Chris@0: return; Chris@0: } Chris@0: $this->assertTrue($all_names); Chris@0: Chris@0: // Look up each default configuration object name in the active Chris@0: // configuration, and if it exists, remove it from the stack. Chris@0: // Only default config that belongs to $module is guaranteed to exist; any Chris@0: // other default config depends on whether other modules are enabled. Thus, Chris@0: // list all default config once more, but filtered by $module. Chris@0: $names = $module_file_storage->listAll($module . '.'); Chris@0: foreach ($names as $key => $name) { Chris@0: if ($this->config($name)->get()) { Chris@0: unset($names[$key]); Chris@0: } Chris@0: } Chris@0: // Verify that all configuration has been installed (which means that $names Chris@0: // is empty). Chris@0: return $this->assertFalse($names, format_string('All default configuration of @module module found.', ['@module' => $module])); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Asserts that no configuration exists for a given module. Chris@0: * Chris@0: * @param string $module Chris@0: * The name of the module. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE if no configuration was found, FALSE otherwise. Chris@0: */ Chris@0: public function assertNoModuleConfig($module) { Chris@0: $names = \Drupal::configFactory()->listAll($module . '.'); Chris@0: return $this->assertFalse($names, format_string('No configuration found for @module module.', ['@module' => $module])); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Assert the list of modules are enabled or disabled. Chris@0: * Chris@0: * @param $modules Chris@0: * Module list to check. Chris@0: * @param $enabled Chris@0: * Expected module state. Chris@0: */ Chris@0: public function assertModules(array $modules, $enabled) { Chris@0: $this->rebuildContainer(); Chris@0: foreach ($modules as $module) { Chris@0: if ($enabled) { Chris@0: $message = 'Module "@module" is enabled.'; Chris@0: } Chris@0: else { Chris@0: $message = 'Module "@module" is not enabled.'; Chris@0: } Chris@0: $this->assertEqual($this->container->get('module_handler')->moduleExists($module), $enabled, format_string($message, ['@module' => $module])); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Verify a log entry was entered for a module's status change. Chris@0: * Chris@0: * @param $type Chris@0: * The category to which this message belongs. Chris@0: * @param $message Chris@0: * The message to store in the log. Keep $message translatable Chris@0: * by not concatenating dynamic values into it! Variables in the Chris@0: * message should be added by using placeholder strings alongside Chris@0: * the variables argument to declare the value of the placeholders. Chris@0: * See t() for documentation on how $message and $variables interact. Chris@0: * @param $variables Chris@0: * Array of variables to replace in the message on display or Chris@0: * NULL if message is already translated or not possible to Chris@0: * translate. Chris@0: * @param $severity Chris@0: * The severity of the message, as per RFC 3164. Chris@0: * @param $link Chris@0: * A link to associate with the message. Chris@0: */ Chris@0: public function assertLogMessage($type, $message, $variables = [], $severity = RfcLogLevel::NOTICE, $link = '') { Chris@18: $count = Database::getConnection()->select('watchdog', 'w') Chris@0: ->condition('type', $type) Chris@0: ->condition('message', $message) Chris@0: ->condition('variables', serialize($variables)) Chris@0: ->condition('severity', $severity) Chris@0: ->condition('link', $link) Chris@0: ->countQuery() Chris@0: ->execute() Chris@0: ->fetchField(); Chris@0: $this->assertTrue($count > 0, format_string('watchdog table contains @count rows for @message', ['@count' => $count, '@message' => format_string($message, $variables)])); Chris@0: } Chris@0: Chris@0: }