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