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