You are here

protected function DataTestCaseUI::createTable in Data 6

Same name and namespace in other branches
  1. 7 data_ui/tests/data_ui.test \DataTestCaseUI::createTable()

Create a table.

1 call to DataTestCaseUI::createTable()
DataTestCaseUI::testCRUDTable in data_ui/tests/data_ui.test
CRUD table tests on UI.

File

data_ui/tests/data_ui.test, line 159

Class

DataTestCaseUI
Test basic Data API functionality.

Code

protected function createTable($num_fields = 5) {
  $table_name = $this
    ->randomName();
  $edit = array(
    'name' => $table_name,
    'title' => 'My table',
    'field_num' => $num_fields,
  );
  $this
    ->drupalPost('admin/build/data/create', $edit, 'Next');
  $this
    ->assertText('Define the fields of the new table.');
  $fields = $this
    ->randomFields($num_fields);
  $edit = $this
    ->formatEditFields($fields);
  $this
    ->drupalPost(NULL, $edit, 'Create');
  $this
    ->assertText('Created table ' . $table_name);

  // Test schema in DB.
  // @todo: why do we need to clear the cache here?
  if ($schema = drupal_get_schema($table_name, true)) {
    foreach ($schema['primary key'] as $key) {
      if (is_array($key)) {
        $primary_keys[] = $key[0];
      }
      else {
        $primary_keys[] = $key;
      }
    }
    foreach ($schema['fields'] as $field_name => $field) {
      $this
        ->assertEqual($fields[$field_name]['type'], $field['type'], "Field {$field_name} has correct type.");
      if ($field['type'] == 'int') {
        $this
          ->assertEqual(isset($fields[$field_name]['unsigned']), !empty($field['unsigned']), "Field {$field_name} has correct unsigned value.");
      }
    }
    foreach ($fields as $field_name => $config) {
      if (isset($config['index'])) {
        $this
          ->assertTrue(isset($schema['indexes'][$field_name]), "Field {$field_name} indexed.");
      }
      if (isset($config['primary'])) {
        $this
          ->assertTrue(in_array($field_name, $primary_keys), "Field {$field_name} in primary key.");
      }
    }
  }
  else {
    $this
      ->fail('Could not create schema - invalid schema definition?');
  }
  $this
    ->assertTrue(db_table_exists($table_name), 'Table ' . $table_name . ' exists in database.');
  return $table_name;
}