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