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