ModuleTestBase.php 6.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
<?php

/**
 * @file
 * Definition of Drupal\system\Tests\Module\ModuleTestBase.
 */

namespace Drupal\system\Tests\Module;

10
use Drupal\Core\Config\InstallStorage;
11
12
use Drupal\Core\Database\Database;
use Drupal\Core\Config\FileStorage;
13
use Drupal\Core\Logger\RfcLogLevel;
14
15
16
17
18
use Drupal\simpletest\WebTestBase;

/**
 * Helper class for module test cases.
 */
19
abstract class ModuleTestBase extends WebTestBase {
20
21
22
23
24
25
26
27

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('system_test');

28
29
  protected $admin_user;

30
  protected function setUp() {
31
    parent::setUp();
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

    $this->admin_user = $this->drupalCreateUser(array('access administration pages', 'administer modules'));
    $this->drupalLogin($this->admin_user);
  }

  /**
   * Assert there are tables that begin with the specified base table name.
   *
   * @param $base_table
   *   Beginning of table name to look for.
   * @param $count
   *   (optional) Whether or not to assert that there are tables that match the
   *   specified base table. Defaults to TRUE.
   */
  function assertTableCount($base_table, $count = TRUE) {
    $tables = db_find_tables(Database::getConnection()->prefixTables('{' . $base_table . '}') . '%');

    if ($count) {
50
      return $this->assertTrue($tables, format_string('Tables matching "@base_table" found.', array('@base_table' => $base_table)));
51
    }
52
    return $this->assertFalse($tables, format_string('Tables matching "@base_table" not found.', array('@base_table' => $base_table)));
53
54
55
56
57
58
59
60
61
  }

  /**
   * Assert that all tables defined in a module's hook_schema() exist.
   *
   * @param $module
   *   The name of the module.
   */
  function assertModuleTablesExist($module) {
62
    $this->rebuildContainer();
63
64
65
66
67
68
69
    $tables = array_keys(drupal_get_schema_unprocessed($module));
    $tables_exist = TRUE;
    foreach ($tables as $table) {
      if (!db_table_exists($table)) {
        $tables_exist = FALSE;
      }
    }
70
    return $this->assertTrue($tables_exist, format_string('All database tables defined by the @module module exist.', array('@module' => $module)));
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
  }

  /**
   * Assert that none of the tables defined in a module's hook_schema() exist.
   *
   * @param $module
   *   The name of the module.
   */
  function assertModuleTablesDoNotExist($module) {
    $tables = array_keys(drupal_get_schema_unprocessed($module));
    $tables_exist = FALSE;
    foreach ($tables as $table) {
      if (db_table_exists($table)) {
        $tables_exist = TRUE;
      }
    }
87
    return $this->assertFalse($tables_exist, format_string('None of the database tables defined by the @module module exist.', array('@module' => $module)));
88
89
90
  }

  /**
91
   * Asserts that the default configuration of a module has been installed.
92
93
94
95
96
   *
   * @param string $module
   *   The name of the module.
   *
   * @return bool
97
   *   TRUE if configuration has been installed, FALSE otherwise.
98
   */
99
  function assertModuleConfig($module) {
100
    $module_config_dir = drupal_get_path('module', $module) . '/'. InstallStorage::CONFIG_INSTALL_DIRECTORY;
101
102
    if (!is_dir($module_config_dir)) {
      return;
103
    }
104
    $module_file_storage = new FileStorage($module_config_dir);
105

106
107
108
    // Verify that the module's default config directory is not empty and
    // contains default configuration files (instead of something else).
    $all_names = $module_file_storage->listAll();
109
110
111
112
113
    if (empty($all_names)) {
      // Module has an empty config directory. For example it might contain a
      // schema directory.
      return;
    }
114
    $this->assertTrue($all_names);
115

116
117
    // Look up each default configuration object name in the active
    // configuration, and if it exists, remove it from the stack.
118
119
120
121
    // Only default config that belongs to $module is guaranteed to exist; any
    // other default config depends on whether other modules are enabled. Thus,
    // list all default config once more, but filtered by $module.
    $names = $module_file_storage->listAll($module . '.');
122
    foreach ($names as $key => $name) {
123
      if (\Drupal::config($name)->get()) {
124
125
126
127
128
129
        unset($names[$key]);
      }
    }
    // Verify that all configuration has been installed (which means that $names
    // is empty).
    return $this->assertFalse($names, format_string('All default configuration of @module module found.', array('@module' => $module)));
130
131
132
  }

  /**
133
   * Asserts that no configuration exists for a given module.
134
135
136
137
138
   *
   * @param string $module
   *   The name of the module.
   *
   * @return bool
139
   *   TRUE if no configuration was found, FALSE otherwise.
140
   */
141
  function assertNoModuleConfig($module) {
142
    $names = \Drupal::configFactory()->listAll($module . '.');
143
    return $this->assertFalse($names, format_string('No configuration found for @module module.', array('@module' => $module)));
144
145
146
147
148
149
150
151
152
153
154
  }

  /**
   * Assert the list of modules are enabled or disabled.
   *
   * @param $modules
   *   Module list to check.
   * @param $enabled
   *   Expected module state.
   */
  function assertModules(array $modules, $enabled) {
155
    $this->rebuildContainer();
156
157
158
159
160
161
162
    foreach ($modules as $module) {
      if ($enabled) {
        $message = 'Module "@module" is enabled.';
      }
      else {
        $message = 'Module "@module" is not enabled.';
      }
163
      $this->assertEqual($this->container->get('module_handler')->moduleExists($module), $enabled, format_string($message, array('@module' => $module)));
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
    }
  }

  /**
   * Verify a log entry was entered for a module's status change.
   *
   * @param $type
   *   The category to which this message belongs.
   * @param $message
   *   The message to store in the log. Keep $message translatable
   *   by not concatenating dynamic values into it! Variables in the
   *   message should be added by using placeholder strings alongside
   *   the variables argument to declare the value of the placeholders.
   *   See t() for documentation on how $message and $variables interact.
   * @param $variables
   *   Array of variables to replace in the message on display or
   *   NULL if message is already translated or not possible to
   *   translate.
   * @param $severity
   *   The severity of the message, as per RFC 3164.
   * @param $link
   *   A link to associate with the message.
   */
187
  function assertLogMessage($type, $message, $variables = array(), $severity = RfcLogLevel::NOTICE, $link = '') {
188
189
190
191
192
193
194
195
196
    $count = db_select('watchdog', 'w')
      ->condition('type', $type)
      ->condition('message', $message)
      ->condition('variables', serialize($variables))
      ->condition('severity', $severity)
      ->condition('link', $link)
      ->countQuery()
      ->execute()
      ->fetchField();
197
    $this->assertTrue($count > 0, format_string('watchdog table contains @count rows for @message', array('@count' => $count, '@message' => format_string($message, $variables))));
198
199
  }
}