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