DrupalUnitTestBaseTest.php 10.1 KB
Newer Older
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
<?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.
42
    $this->assertIdentical(array_keys(drupal_container()->get('module_handler')->getModuleList()), array($module));
43 44 45 46 47 48 49 50 51 52 53 54 55 56
    $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.");
57 58 59
    $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');
60 61 62
    $this->assertFalse(in_array($module, $list), "{$module}_permission() in module_implements() not found.");

    // Enable the module.
63
    $this->enableModules(array($module));
64 65 66

    // Verify that the module exists.
    $this->assertTrue(module_exists($module), "$module module found.");
67 68 69
    $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');
70 71 72 73 74 75 76
    $this->assertTrue(in_array($module, $list), "{$module}_permission() in module_implements() found.");
  }

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

    // Verify that the module does not exist yet.
    $this->assertFalse(module_exists($module), "$module module not found.");
82 83 84
    $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');
85 86 87 88 89 90
    $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.");

91 92
    // Install the module.
    module_enable(array($module));
93 94 95

    // Verify that the enabled module exists.
    $this->assertTrue(module_exists($module), "$module module found.");
96 97 98
    $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');
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
    $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 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'));
114 115 116
    $this->installSchema('field', array('field_config', 'field_config_instance'));

    $this->installSchema('node', array('node_type', 'node'));
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
    // 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.");

142 143 144 145 146 147 148 149 150 151 152 153 154
    // Verify that a unknown table from an enabled module throws an error.
    $table = 'unknown_entity_test_table';
    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.");

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    // 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.
170
    $this->enableModules(array($module));
171 172 173 174 175 176
    $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.");
  }

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
   * Tests expected behavior of installConfig().
   */
  function testInstallConfig() {
    $module = 'user';

    // Verify that default config can only be installed for enabled modules.
    try {
      $this->installConfig(array($module));
      $this->fail('Exception for non-enabled module found.');
    }
    catch (\Exception $e) {
      $this->pass('Exception for non-enabled module found.');
    }
    $this->assertFalse($this->container->get('config.storage')->exists('user.settings'));

    // Verify that default config can be installed.
    $this->enableModules(array('user'));
    $this->installConfig(array('user'));
    $this->assertTrue($this->container->get('config.storage')->exists('user.settings'));
    $this->assertTrue(config('user.settings')->get('register'));
  }

  /**
   * Tests that the module list is retained after enabling/installing/disabling modules.
202 203 204 205 206 207 208
   */
  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.
209
    $this->enableModules(array('entity', 'field', 'field_sql_storage', 'text', 'entity_test'));
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
    $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.
229
    $this->enableModules(array('field_test'));
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253

    // 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() {
254
    $original_element = $element = array(
255 256 257 258
      '#type' => 'container',
      '#markup' => 'Foo',
      '#attributes' => array(),
    );
259
    $this->enableModules(array('system'));
260
    // theme() throws an exception if modules are not loaded yet.
261 262 263 264 265
    $this->assertTrue(drupal_render($element));

    $element = $original_element;
    $this->disableModules(array('entity_test'));
    $this->assertTrue(drupal_render($element));
266 267
  }

268
}