summaryrefslogtreecommitdiffstats
path: root/core/modules/simpletest/lib/Drupal/simpletest/Tests/DrupalUnitTestBaseTest.php
blob: 242338efc533d23b336fbec22fff046372c1d4cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php

/**
 * @file
 * Contains Drupal\simpletest\Tests\DrupalUnitTestBaseTest.
 */

namespace Drupal\simpletest\Tests;

use Drupal\simpletest\DrupalUnitTestBase;

/**
 * Tests DrupalUnitTestBase functionality.
 */
class DrupalUnitTestBaseTest extends DrupalUnitTestBase {

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

  public static function getInfo() {
    return array(
      'name' => 'DrupalUnitTestBase',
      'description' => 'Tests DrupalUnitTestBase functionality.',
      'group' => 'SimpleTest',
    );
  }

  /**
   * Tests expected behavior of setUp().
   */
  function testSetUp() {
    $module = 'entity_test';
    $table = 'entity_test';

    // Verify that specified $modules have been loaded.
    $this->assertTrue(function_exists('entity_test_permission'), "$module.module was loaded.");
    // Verify that there is a fixed module list.
    $this->assertIdentical(array_keys(drupal_container()->get('module_handler')->getModuleList()), array($module));
    $this->assertIdentical(module_implements('permission'), array($module));

    // Verify that no modules have been installed.
    $this->assertFalse(db_table_exists($table), "'$table' database table not found.");
  }

  /**
   * Tests expected load behavior of enableModules().
   */
  function testEnableModulesLoad() {
    $module = 'field_test';

    // Verify that the module does not exist yet.
    $this->assertFalse(module_exists($module), "$module module not found.");
    $list = array_keys(drupal_container()->get('module_handler')->getModuleList());
    $this->assertFalse(in_array($module, $list), "$module module not found in the extension handler's module list.");
    $list = module_implements('permission');
    $this->assertFalse(in_array($module, $list), "{$module}_permission() in module_implements() not found.");

    // Enable the module.
    $this->enableModules(array($module), FALSE);

    // Verify that the module exists.
    $this->assertTrue(module_exists($module), "$module module found.");
    $list = array_keys(drupal_container()->get('module_handler')->getModuleList());
    $this->assertTrue(in_array($module, $list), "$module module found in the extension handler's module list.");
    $list = module_implements('permission');
    $this->assertTrue(in_array($module, $list), "{$module}_permission() in module_implements() found.");
  }

  /**
   * Tests expected installation behavior of enableModules().
   */
  function testEnableModulesInstall() {
    $module = 'node';
    $table = 'node';

    // @todo Remove after configuration system conversion.
    $this->enableModules(array('system'), FALSE);
    $this->installSchema('system', 'variable');

    // Verify that the module does not exist yet.
    $this->assertFalse(module_exists($module), "$module module not found.");
    $list = array_keys(drupal_container()->get('module_handler')->getModuleList());
    $this->assertFalse(in_array($module, $list), "$module module not found in the extension handler's module list.");
    $list = module_implements('permission');
    $this->assertFalse(in_array($module, $list), "{$module}_permission() in module_implements() not found.");

    $this->assertFalse(db_table_exists($table), "'$table' database table not found.");
    $schema = drupal_get_schema($table);
    $this->assertFalse($schema, "'$table' table schema not found.");

    // Enable the module.
    $this->enableModules(array($module));

    // Verify that the enabled module exists.
    $this->assertTrue(module_exists($module), "$module module found.");
    $list = array_keys(drupal_container()->get('module_handler')->getModuleList());
    $this->assertTrue(in_array($module, $list), "$module module found in the extension handler's module list.");
    $list = module_implements('permission');
    $this->assertTrue(in_array($module, $list), "{$module}_permission() in module_implements() found.");

    $this->assertTrue(db_table_exists($table), "'$table' database table found.");
    $schema = drupal_get_schema($table);
    $this->assertTrue($schema, "'$table' table schema found.");
  }

  /**
   * Tests installing of multiple modules via enableModules().
   *
   * Regression test: Each passed module has to be enabled and installed on its
   * own, in the same way as module_enable() enables only one module after the
   * other.
   */
  function testEnableModulesInstallMultiple() {
    // Field retrieves entity type plugins, and EntityTypeManager calls into
    // hook_entity_info_alter(). If both modules would be first enabled together
    // instead of each on its own, then Node module's alter implementation
    // would be called and this simply blows up. To further complicate matters,
    // additionally install Comment module, whose entity bundles depend on node
    // types.
    $this->enableModules(array('field', 'node', 'comment'));
    $this->pass('Comment module was installed.');
  }

  /**
   * Tests installing modules via enableModules() with DepedencyInjection services.
   */
  function testEnableModulesInstallContainer() {
    // Install Node module.
    // @todo field_sql_storage and field should technically not be necessary
    //   for an entity query.
    $this->enableModules(array('field_sql_storage', 'field', 'node'));
    // Perform an entity query against node.
    $query = entity_query('node');
    // Disable node access checks, since User module is not enabled.
    $query->accessCheck(FALSE);
    $query->condition('nid', 1);
    $query->execute();
    $this->pass('Entity field query was executed.');
  }

  /**
   * Tests expected behavior of installSchema().
   */
  function testInstallSchema() {
    $module = 'entity_test';
    $table = 'entity_test';
    // Verify that we can install a table from the module schema.
    $this->installSchema($module, $table);
    $this->assertTrue(db_table_exists($table), "'$table' database table found.");

    // Verify that the schema is known to Schema API.
    $schema = drupal_get_schema();
    $this->assertTrue($schema[$table], "'$table' table found in schema.");
    $schema = drupal_get_schema($table);
    $this->assertTrue($schema, "'$table' table schema found.");

    // Verify that a table from a unknown module cannot be installed.
    $module = 'database_test';
    $table = 'test';
    try {
      $this->installSchema($module, $table);
      $this->fail('Exception for non-retrievable schema found.');
    }
    catch (\Exception $e) {
      $this->pass('Exception for non-retrievable schema found.');
    }
    $this->assertFalse(db_table_exists($table), "'$table' database table not found.");
    $schema = drupal_get_schema($table);
    $this->assertFalse($schema, "'$table' table schema not found.");

    // Verify that the same table can be installed after enabling the module.
    $this->enableModules(array($module), FALSE);
    $this->installSchema($module, $table);
    $this->assertTrue(db_table_exists($table), "'$table' database table found.");
    $schema = drupal_get_schema($table);
    $this->assertTrue($schema, "'$table' table schema found.");
  }

  /**
   * Tests that the fixed module list is retained after enabling and installing modules.
   */
  function testEnableModulesFixedList() {
    // entity_test is loaded via $modules; its entity type should exist.
    $this->assertEqual($this->container->get('module_handler')->moduleExists('entity_test'), TRUE);
    $this->assertTrue(TRUE == entity_get_info('entity_test'));

    // Load some additional modules; entity_test should still exist.
    $this->enableModules(array('entity', 'field', 'field_sql_storage', 'text', 'entity_test'), FALSE);
    $this->assertEqual($this->container->get('module_handler')->moduleExists('entity_test'), TRUE);
    $this->assertTrue(TRUE == entity_get_info('entity_test'));

    // Install some other modules; entity_test should still exist.
    module_enable(array('field', 'field_sql_storage', 'field_test'), FALSE);
    $this->assertEqual($this->container->get('module_handler')->moduleExists('entity_test'), TRUE);
    $this->assertTrue(TRUE == entity_get_info('entity_test'));

    // Disable one of those modules; entity_test should still exist.
    module_disable(array('field_test'));
    $this->assertEqual($this->container->get('module_handler')->moduleExists('entity_test'), TRUE);
    $this->assertTrue(TRUE == entity_get_info('entity_test'));

    // Set the weight of a module; entity_test should still exist.
    module_set_weight('entity', -1);
    $this->assertEqual($this->container->get('module_handler')->moduleExists('entity_test'), TRUE);
    $this->assertTrue(TRUE == entity_get_info('entity_test'));

    // Reactivate the disabled module without enabling it.
    $this->enableModules(array('field_test'), FALSE);

    // Create a field and an instance.
    $display = entity_create('entity_display', array(
      'targetEntityType' => 'entity_test',
      'bundle' => 'entity_test',
      'viewMode' => 'default',
    ));
    $field = array(
      'field_name' => 'test_field',
      'type' => 'test_field'
    );
    field_create_field($field);
    $instance = array(
      'field_name' => $field['field_name'],
      'entity_type' => 'entity_test',
      'bundle' => 'entity_test',
    );
    field_create_instance($instance);
  }

  /**
   * Tests that theme() works right after loading a module.
   */
  function testEnableModulesTheme() {
    $element = array(
      '#type' => 'container',
      '#markup' => 'Foo',
      '#attributes' => array(),
    );
    $this->enableModules(array('system'), FALSE);
    // theme() throws an exception if modules are not loaded yet.
    drupal_render($element);
  }

}