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