示例#1
0
 /**
  * Tests that we can insert fields with values and defaults in the same query.
  */
 function testDefaultInsertWithFields()
 {
     $query = db_insert('test')->fields(array('name' => 'Bob'))->useDefaults(array('job'));
     $id = $query->execute();
     $schema = drupal_get_module_schema('database_test', 'test');
     $job = db_query('SELECT job FROM {test} WHERE id = :id', array(':id' => $id))->fetchField();
     $this->assertEqual($job, $schema['fields']['job']['default'], 'Default field value is set.');
 }
 /**
  * {@inheritdoc}
  */
 public function setUp()
 {
     parent::setUp();
     // Drupal 8.0.x needs the router table installed which is done automatically
     // in Drupal 8.1.x. Remove this once Drupal 8.0.x is unsupported.
     if (!empty(drupal_get_module_schema('system', 'router'))) {
         $this->installSchema('system', ['router']);
         $this->container->get('router.builder')->rebuild();
     }
 }
示例#3
0
 /**
  * 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_module_schema($module));
     $tables_exist = FALSE;
     foreach ($tables as $table) {
         if (db_table_exists($table)) {
             $tables_exist = TRUE;
         }
     }
     return $this->assertFalse($tables_exist, format_string('None of the database tables defined by the @module module exist.', array('@module' => $module)));
 }
示例#4
0
 /**
  * {@inheritdoc}
  */
 public function setUp()
 {
     parent::setUp();
     $this->installConfig(['system']);
     $this->installConfig(['field']);
     $this->installConfig(['node']);
     $this->installSchema('system', ['sequences']);
     // Drupal 8.0.x needs the router table installed which is done automatically
     // in Drupal 8.1.x. Remove this once Drupal 8.0.x is unsupported.
     if (!empty(drupal_get_module_schema('system', 'router'))) {
         $this->installSchema('system', ['router']);
     }
     $this->installEntitySchema('user');
     $this->installEntitySchema('node');
     // Make sure that the node routes get picked when used during rendering.
     $this->container->get('router.builder')->rebuild();
 }
示例#5
0
 /**
  * Installs a specific table from a module schema definition.
  *
  * @param string $module
  *   The name of the module that defines the table's schema.
  * @param string|array $tables
  *   The name or an array of the names of the tables to install.
  *
  * @throws \RuntimeException
  *   Thrown when $module is not enabled or when the table schema cannot be
  *   found in the module specified.
  */
 protected function installSchema($module, $tables)
 {
     // drupal_get_module_schema() is technically able to install a schema
     // of a non-enabled module, but its ability to load the module's .install
     // file depends on many other factors. To prevent differences in test
     // behavior and non-reproducible test failures, we only allow the schema of
     // explicitly loaded/enabled modules to be installed.
     if (!$this->container->get('module_handler')->moduleExists($module)) {
         throw new \RuntimeException("'{$module}' module is not enabled");
     }
     $tables = (array) $tables;
     foreach ($tables as $table) {
         $schema = drupal_get_module_schema($module, $table);
         if (empty($schema)) {
             throw new \RuntimeException("Unknown '{$table}' table schema in '{$module}' module.");
         }
         $this->container->get('database')->schema()->createTable($table, $schema);
     }
     $this->pass(format_string('Installed %module tables: %tables.', array('%tables' => '{' . implode('}, {', $tables) . '}', '%module' => $module)));
 }
示例#6
0
 /**
  * Installs database tables from a module schema definition.
  *
  * @param string $module
  *   The name of the module that defines the table's schema.
  * @param string|array $tables
  *   The name or an array of the names of the tables to install.
  *
  * @throws \LogicException
  *   If $module is not enabled or the table schema cannot be found.
  */
 protected function installSchema($module, $tables)
 {
     // drupal_get_module_schema() is technically able to install a schema
     // of a non-enabled module, but its ability to load the module's .install
     // file depends on many other factors. To prevent differences in test
     // behavior and non-reproducible test failures, we only allow the schema of
     // explicitly loaded/enabled modules to be installed.
     if (!$this->container->get('module_handler')->moduleExists($module)) {
         throw new \LogicException("{$module} module is not enabled.");
     }
     $tables = (array) $tables;
     foreach ($tables as $table) {
         $schema = drupal_get_module_schema($module, $table);
         if (empty($schema)) {
             // BC layer to avoid some contrib tests to fail.
             // @todo Remove the BC layer before 8.1.x release.
             // @see https://www.drupal.org/node/2670360
             // @see https://www.drupal.org/node/2670454
             if ($module == 'system') {
                 continue;
             }
             throw new \LogicException("{$module} module does not define a schema for table '{$table}'.");
         }
         $this->container->get('database')->schema()->createTable($table, $schema);
     }
 }
示例#7
0
  /**
   * Batch method to truncate all remaining flag tables.
   */
  public static function clearTables(&$context) {
    // First, set the number of tables we'll truncate each invocation.

    $batch_size = 1;

    // Get the module schema.
    $schema = drupal_get_module_schema('flag');

    // If this is the first invocation, set our index and maximum.
    if (empty($context['sandbox'])) {
      $context['sandbox']['progress'] = 0;
      $context['sandbox']['max'] = count($schema);
    }

    // Get the database connection.
    $connection = Database::getConnection();

    // Truncate tables as needed in the batch.
    $tables = array_keys($schema);
    $progress = $context['sandbox']['progress'];
    for ($i = $progress; $i < $progress + $batch_size; $i++) {
      $table = $tables[$i];
      $connection->truncate($table)->execute();
    }

    // Increment our progress.
    $context['sandbox']['progress'] += $batch_size;

    // If we still have tables to truncate, set our progress percentage.
    if ($context['sandbox']['progress'] < $context['sandbox']['max']) {
      $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
    }
  }
示例#8
0
 /**
  * Tests expected behavior of installSchema().
  */
 function testInstallSchema()
 {
     $module = 'entity_test';
     $table = 'entity_test_example';
     // 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_module_schema($module, $table);
     $this->assertTrue($schema, "'{$table}' table schema found.");
     // 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_module_schema($module, $table);
     $this->assertFalse($schema, "'{$table}' table schema not 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_module_schema($module, $table);
     $this->assertTrue($schema, "'{$table}' table schema found.");
     // Verify that the same table can be installed after enabling the module.
     $this->enableModules(array($module));
     $this->installSchema($module, $table);
     $this->assertTrue(db_table_exists($table), "'{$table}' database table found.");
     $schema = drupal_get_module_schema($module, $table);
     $this->assertTrue($schema, "'{$table}' table schema found.");
 }
示例#9
0
 /**
  * Installs database tables from a module schema definition.
  *
  * @param string $module
  *   The name of the module that defines the table's schema.
  * @param string|array $tables
  *   The name or an array of the names of the tables to install.
  *
  * @throws \LogicException
  *   If $module is not enabled or the table schema cannot be found.
  */
 protected function installSchema($module, $tables)
 {
     // drupal_get_module_schema() is technically able to install a schema
     // of a non-enabled module, but its ability to load the module's .install
     // file depends on many other factors. To prevent differences in test
     // behavior and non-reproducible test failures, we only allow the schema of
     // explicitly loaded/enabled modules to be installed.
     if (!$this->container->get('module_handler')->moduleExists($module)) {
         throw new \LogicException("{$module} module is not enabled.");
     }
     $tables = (array) $tables;
     foreach ($tables as $table) {
         $schema = drupal_get_module_schema($module, $table);
         if (empty($schema)) {
             throw new \LogicException("{$module} module does not define a schema for table '{$table}'.");
         }
         $this->container->get('database')->schema()->createTable($table, $schema);
     }
 }