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