comparison core/modules/system/src/Tests/Module/ModuleTestBase.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\system\Tests\Module;
4
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;
10
11 /**
12 * Helper class for module test cases.
13 *
14 * @deprecated Scheduled for removal in Drupal 9.0.0.
15 * Use \Drupal\Tests\system\Functional\Module\ModuleTestBase instead.
16 */
17 abstract class ModuleTestBase extends WebTestBase {
18
19 /**
20 * Modules to enable.
21 *
22 * @var array
23 */
24 public static $modules = ['system_test'];
25
26 protected $adminUser;
27
28 protected function setUp() {
29 parent::setUp();
30
31 $this->adminUser = $this->drupalCreateUser(['access administration pages', 'administer modules']);
32 $this->drupalLogin($this->adminUser);
33 }
34
35 /**
36 * Assert there are tables that begin with the specified base table name.
37 *
38 * @param $base_table
39 * Beginning of table name to look for.
40 * @param $count
41 * (optional) Whether or not to assert that there are tables that match the
42 * specified base table. Defaults to TRUE.
43 */
44 public function assertTableCount($base_table, $count = TRUE) {
45 $tables = db_find_tables(Database::getConnection()->prefixTables('{' . $base_table . '}') . '%');
46
47 if ($count) {
48 return $this->assertTrue($tables, format_string('Tables matching "@base_table" found.', ['@base_table' => $base_table]));
49 }
50 return $this->assertFalse($tables, format_string('Tables matching "@base_table" not found.', ['@base_table' => $base_table]));
51 }
52
53 /**
54 * Assert that all tables defined in a module's hook_schema() exist.
55 *
56 * @param $module
57 * The name of the module.
58 */
59 public function assertModuleTablesExist($module) {
60 $tables = array_keys(drupal_get_module_schema($module));
61 $tables_exist = TRUE;
62 foreach ($tables as $table) {
63 if (!db_table_exists($table)) {
64 $tables_exist = FALSE;
65 }
66 }
67 return $this->assertTrue($tables_exist, format_string('All database tables defined by the @module module exist.', ['@module' => $module]));
68 }
69
70 /**
71 * Assert that none of the tables defined in a module's hook_schema() exist.
72 *
73 * @param $module
74 * The name of the module.
75 */
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)) {
81 $tables_exist = TRUE;
82 }
83 }
84 return $this->assertFalse($tables_exist, format_string('None of the database tables defined by the @module module exist.', ['@module' => $module]));
85 }
86
87 /**
88 * Asserts that the default configuration of a module has been installed.
89 *
90 * @param string $module
91 * The name of the module.
92 *
93 * @return bool|null
94 * TRUE if configuration has been installed, FALSE otherwise. Returns NULL
95 * if the module configuration directory does not exist or does not contain
96 * any configuration files.
97 */
98 public function assertModuleConfig($module) {
99 $module_config_dir = drupal_get_path('module', $module) . '/' . InstallStorage::CONFIG_INSTALL_DIRECTORY;
100 if (!is_dir($module_config_dir)) {
101 return;
102 }
103 $module_file_storage = new FileStorage($module_config_dir);
104
105 // Verify that the module's default config directory is not empty and
106 // contains default configuration files (instead of something else).
107 $all_names = $module_file_storage->listAll();
108 if (empty($all_names)) {
109 // Module has an empty config directory. For example it might contain a
110 // schema directory.
111 return;
112 }
113 $this->assertTrue($all_names);
114
115 // Look up each default configuration object name in the active
116 // configuration, and if it exists, remove it from the stack.
117 // Only default config that belongs to $module is guaranteed to exist; any
118 // other default config depends on whether other modules are enabled. Thus,
119 // list all default config once more, but filtered by $module.
120 $names = $module_file_storage->listAll($module . '.');
121 foreach ($names as $key => $name) {
122 if ($this->config($name)->get()) {
123 unset($names[$key]);
124 }
125 }
126 // Verify that all configuration has been installed (which means that $names
127 // is empty).
128 return $this->assertFalse($names, format_string('All default configuration of @module module found.', ['@module' => $module]));
129 }
130
131 /**
132 * Asserts that no configuration exists for a given module.
133 *
134 * @param string $module
135 * The name of the module.
136 *
137 * @return bool
138 * TRUE if no configuration was found, FALSE otherwise.
139 */
140 public function assertNoModuleConfig($module) {
141 $names = \Drupal::configFactory()->listAll($module . '.');
142 return $this->assertFalse($names, format_string('No configuration found for @module module.', ['@module' => $module]));
143 }
144
145 /**
146 * Assert the list of modules are enabled or disabled.
147 *
148 * @param $modules
149 * Module list to check.
150 * @param $enabled
151 * Expected module state.
152 */
153 public function assertModules(array $modules, $enabled) {
154 $this->rebuildContainer();
155 foreach ($modules as $module) {
156 if ($enabled) {
157 $message = 'Module "@module" is enabled.';
158 }
159 else {
160 $message = 'Module "@module" is not enabled.';
161 }
162 $this->assertEqual($this->container->get('module_handler')->moduleExists($module), $enabled, format_string($message, ['@module' => $module]));
163 }
164 }
165
166 /**
167 * Verify a log entry was entered for a module's status change.
168 *
169 * @param $type
170 * The category to which this message belongs.
171 * @param $message
172 * The message to store in the log. Keep $message translatable
173 * by not concatenating dynamic values into it! Variables in the
174 * message should be added by using placeholder strings alongside
175 * the variables argument to declare the value of the placeholders.
176 * See t() for documentation on how $message and $variables interact.
177 * @param $variables
178 * Array of variables to replace in the message on display or
179 * NULL if message is already translated or not possible to
180 * translate.
181 * @param $severity
182 * The severity of the message, as per RFC 3164.
183 * @param $link
184 * A link to associate with the message.
185 */
186 public function assertLogMessage($type, $message, $variables = [], $severity = RfcLogLevel::NOTICE, $link = '') {
187 $count = db_select('watchdog', 'w')
188 ->condition('type', $type)
189 ->condition('message', $message)
190 ->condition('variables', serialize($variables))
191 ->condition('severity', $severity)
192 ->condition('link', $link)
193 ->countQuery()
194 ->execute()
195 ->fetchField();
196 $this->assertTrue($count > 0, format_string('watchdog table contains @count rows for @message', ['@count' => $count, '@message' => format_string($message, $variables)]));
197 }
198
199 }