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@17
|
62 $schema = Database::getConnection()->schema();
|
Chris@0
|
63 foreach ($tables as $table) {
|
Chris@17
|
64 if (!$schema->tableExists($table)) {
|
Chris@0
|
65 $tables_exist = FALSE;
|
Chris@0
|
66 }
|
Chris@0
|
67 }
|
Chris@0
|
68 return $this->assertTrue($tables_exist, format_string('All database tables defined by the @module module exist.', ['@module' => $module]));
|
Chris@0
|
69 }
|
Chris@0
|
70
|
Chris@0
|
71 /**
|
Chris@0
|
72 * Assert that none of the tables defined in a module's hook_schema() exist.
|
Chris@0
|
73 *
|
Chris@0
|
74 * @param $module
|
Chris@0
|
75 * The name of the module.
|
Chris@0
|
76 */
|
Chris@0
|
77 public function assertModuleTablesDoNotExist($module) {
|
Chris@0
|
78 $tables = array_keys(drupal_get_module_schema($module));
|
Chris@0
|
79 $tables_exist = FALSE;
|
Chris@17
|
80 $schema = Database::getConnection()->schema();
|
Chris@0
|
81 foreach ($tables as $table) {
|
Chris@17
|
82 if ($schema->tableExists($table)) {
|
Chris@0
|
83 $tables_exist = TRUE;
|
Chris@0
|
84 }
|
Chris@0
|
85 }
|
Chris@0
|
86 return $this->assertFalse($tables_exist, format_string('None of the database tables defined by the @module module exist.', ['@module' => $module]));
|
Chris@0
|
87 }
|
Chris@0
|
88
|
Chris@0
|
89 /**
|
Chris@0
|
90 * Asserts that the default configuration of a module has been installed.
|
Chris@0
|
91 *
|
Chris@0
|
92 * @param string $module
|
Chris@0
|
93 * The name of the module.
|
Chris@0
|
94 *
|
Chris@0
|
95 * @return bool|null
|
Chris@0
|
96 * TRUE if configuration has been installed, FALSE otherwise. Returns NULL
|
Chris@0
|
97 * if the module configuration directory does not exist or does not contain
|
Chris@0
|
98 * any configuration files.
|
Chris@0
|
99 */
|
Chris@0
|
100 public function assertModuleConfig($module) {
|
Chris@0
|
101 $module_config_dir = drupal_get_path('module', $module) . '/' . InstallStorage::CONFIG_INSTALL_DIRECTORY;
|
Chris@0
|
102 if (!is_dir($module_config_dir)) {
|
Chris@0
|
103 return;
|
Chris@0
|
104 }
|
Chris@0
|
105 $module_file_storage = new FileStorage($module_config_dir);
|
Chris@0
|
106
|
Chris@0
|
107 // Verify that the module's default config directory is not empty and
|
Chris@0
|
108 // contains default configuration files (instead of something else).
|
Chris@0
|
109 $all_names = $module_file_storage->listAll();
|
Chris@0
|
110 if (empty($all_names)) {
|
Chris@0
|
111 // Module has an empty config directory. For example it might contain a
|
Chris@0
|
112 // schema directory.
|
Chris@0
|
113 return;
|
Chris@0
|
114 }
|
Chris@0
|
115 $this->assertTrue($all_names);
|
Chris@0
|
116
|
Chris@0
|
117 // Look up each default configuration object name in the active
|
Chris@0
|
118 // configuration, and if it exists, remove it from the stack.
|
Chris@0
|
119 // Only default config that belongs to $module is guaranteed to exist; any
|
Chris@0
|
120 // other default config depends on whether other modules are enabled. Thus,
|
Chris@0
|
121 // list all default config once more, but filtered by $module.
|
Chris@0
|
122 $names = $module_file_storage->listAll($module . '.');
|
Chris@0
|
123 foreach ($names as $key => $name) {
|
Chris@0
|
124 if ($this->config($name)->get()) {
|
Chris@0
|
125 unset($names[$key]);
|
Chris@0
|
126 }
|
Chris@0
|
127 }
|
Chris@0
|
128 // Verify that all configuration has been installed (which means that $names
|
Chris@0
|
129 // is empty).
|
Chris@0
|
130 return $this->assertFalse($names, format_string('All default configuration of @module module found.', ['@module' => $module]));
|
Chris@0
|
131 }
|
Chris@0
|
132
|
Chris@0
|
133 /**
|
Chris@0
|
134 * Asserts that no configuration exists for a given module.
|
Chris@0
|
135 *
|
Chris@0
|
136 * @param string $module
|
Chris@0
|
137 * The name of the module.
|
Chris@0
|
138 *
|
Chris@0
|
139 * @return bool
|
Chris@0
|
140 * TRUE if no configuration was found, FALSE otherwise.
|
Chris@0
|
141 */
|
Chris@0
|
142 public function assertNoModuleConfig($module) {
|
Chris@0
|
143 $names = \Drupal::configFactory()->listAll($module . '.');
|
Chris@0
|
144 return $this->assertFalse($names, format_string('No configuration found for @module module.', ['@module' => $module]));
|
Chris@0
|
145 }
|
Chris@0
|
146
|
Chris@0
|
147 /**
|
Chris@0
|
148 * Assert the list of modules are enabled or disabled.
|
Chris@0
|
149 *
|
Chris@0
|
150 * @param $modules
|
Chris@0
|
151 * Module list to check.
|
Chris@0
|
152 * @param $enabled
|
Chris@0
|
153 * Expected module state.
|
Chris@0
|
154 */
|
Chris@0
|
155 public function assertModules(array $modules, $enabled) {
|
Chris@0
|
156 $this->rebuildContainer();
|
Chris@0
|
157 foreach ($modules as $module) {
|
Chris@0
|
158 if ($enabled) {
|
Chris@0
|
159 $message = 'Module "@module" is enabled.';
|
Chris@0
|
160 }
|
Chris@0
|
161 else {
|
Chris@0
|
162 $message = 'Module "@module" is not enabled.';
|
Chris@0
|
163 }
|
Chris@0
|
164 $this->assertEqual($this->container->get('module_handler')->moduleExists($module), $enabled, format_string($message, ['@module' => $module]));
|
Chris@0
|
165 }
|
Chris@0
|
166 }
|
Chris@0
|
167
|
Chris@0
|
168 /**
|
Chris@0
|
169 * Verify a log entry was entered for a module's status change.
|
Chris@0
|
170 *
|
Chris@0
|
171 * @param $type
|
Chris@0
|
172 * The category to which this message belongs.
|
Chris@0
|
173 * @param $message
|
Chris@0
|
174 * The message to store in the log. Keep $message translatable
|
Chris@0
|
175 * by not concatenating dynamic values into it! Variables in the
|
Chris@0
|
176 * message should be added by using placeholder strings alongside
|
Chris@0
|
177 * the variables argument to declare the value of the placeholders.
|
Chris@0
|
178 * See t() for documentation on how $message and $variables interact.
|
Chris@0
|
179 * @param $variables
|
Chris@0
|
180 * Array of variables to replace in the message on display or
|
Chris@0
|
181 * NULL if message is already translated or not possible to
|
Chris@0
|
182 * translate.
|
Chris@0
|
183 * @param $severity
|
Chris@0
|
184 * The severity of the message, as per RFC 3164.
|
Chris@0
|
185 * @param $link
|
Chris@0
|
186 * A link to associate with the message.
|
Chris@0
|
187 */
|
Chris@0
|
188 public function assertLogMessage($type, $message, $variables = [], $severity = RfcLogLevel::NOTICE, $link = '') {
|
Chris@0
|
189 $count = db_select('watchdog', 'w')
|
Chris@0
|
190 ->condition('type', $type)
|
Chris@0
|
191 ->condition('message', $message)
|
Chris@0
|
192 ->condition('variables', serialize($variables))
|
Chris@0
|
193 ->condition('severity', $severity)
|
Chris@0
|
194 ->condition('link', $link)
|
Chris@0
|
195 ->countQuery()
|
Chris@0
|
196 ->execute()
|
Chris@0
|
197 ->fetchField();
|
Chris@0
|
198 $this->assertTrue($count > 0, format_string('watchdog table contains @count rows for @message', ['@count' => $count, '@message' => format_string($message, $variables)]));
|
Chris@0
|
199 }
|
Chris@0
|
200
|
Chris@0
|
201 }
|