ModuleTestBase.php 6.73 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
13
14
15
16
17
use Drupal\Core\Database\Database;
use Drupal\Core\Config\FileStorage;
use Drupal\simpletest\WebTestBase;

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

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

27
28
29
  protected $admin_user;

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

    $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) {
49
      return $this->assertTrue($tables, format_string('Tables matching "@base_table" found.', array('@base_table' => $base_table)));
50
    }
51
    return $this->assertFalse($tables, format_string('Tables matching "@base_table" not found.', array('@base_table' => $base_table)));
52
53
54
55
56
57
58
59
60
  }

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

  /**
   * 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;
      }
    }
86
    return $this->assertFalse($tables_exist, format_string('None of the database tables defined by the @module module exist.', array('@module' => $module)));
87
88
89
  }

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

105
106
107
    // 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();
108
109
110
111
112
    if (empty($all_names)) {
      // Module has an empty config directory. For example it might contain a
      // schema directory.
      return;
    }
113
    $this->assertTrue($all_names);
114

115
116
    // Look up each default configuration object name in the active
    // configuration, and if it exists, remove it from the stack.
117
118
119
120
    // 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 . '.');
121
    foreach ($names as $key => $name) {
122
      if (\Drupal::config($name)->get()) {
123
124
125
126
127
128
        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)));
129
130
131
  }

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

  /**
   * 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) {
154
    $this->rebuildContainer();
155
156
157
158
159
160
161
    foreach ($modules as $module) {
      if ($enabled) {
        $message = 'Module "@module" is enabled.';
      }
      else {
        $message = 'Module "@module" is not enabled.';
      }
162
      $this->assertEqual($this->container->get('module_handler')->moduleExists($module), $enabled, format_string($message, array('@module' => $module)));
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
    }
  }

  /**
   * Verify a log entry was entered for a module's status change.
   * Called in the same way of the expected original watchdog() execution.
   *
   * @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.
   */
  function assertLogMessage($type, $message, $variables = array(), $severity = WATCHDOG_NOTICE, $link = '') {
    $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
  }
}