You are here

public function DataTestCaseUI::testChangeManagement in Data 6

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

Test change management on UI.

File

data_ui/tests/data_ui.test, line 53

Class

DataTestCaseUI
Test basic Data API functionality.

Code

public function testChangeManagement() {

  // Check for presence of default table.
  $this
    ->drupalGet('admin/build/data');
  $this
    ->assertText('data_table_kittens');
  $this
    ->assertText('Default');
  $this
    ->assertText('Override | Export');

  // Go to schema comparisons, verify that table is present and doesn't differ from
  // schema definition.
  $this
    ->drupalGet('admin/build/data/compare');
  $this
    ->assertText('data_table_kittens');
  $this
    ->assertText('same');

  // Drop the table bypassing the API.
  $table = data_get_table('data_table_kittens');
  $ret = array();
  db_drop_table($ret, $table
    ->get('name'));
  $this
    ->assertTrue(isset($ret[0]['success']), 'Dropped table bypassing the API.');

  // Go to schema comparisons, now the table should be missing.
  $this
    ->drupalGet('admin/build/data/compare');
  $this
    ->assertText('data_table_kittens');
  $this
    ->assertText('missing - adjust');

  // Go to schema comparison of data_table_kittens.
  $this
    ->drupalGet('admin/build/data/compare/data_table_kittens');
  $this
    ->assertText('Adjust data_table_kittens');
  $this
    ->assertText('Status:');
  $this
    ->assertText('missing');
  $this
    ->assertText('Create a new table from schema information.');

  // Create table.
  $this
    ->drupalPost('admin/build/data/compare/data_table_kittens', array(), t('Create table'));
  $this
    ->assertText('Created table data_table_kittens');
  $this
    ->assertTrue(db_table_exists('data_table_kittens'), 'Table data_table_kittens exists in DB.');
  $schema = schema_invoke('inspect', db_prefix_tables('{data_table_kittens}'));
  $this
    ->assertTrue(isset($schema['data_table_kittens']), 'Schema API inspector detects table.');
  $this
    ->assertTrue(!empty($table), 'Table loaded');
  $comp = $table
    ->compareSchema();
  $this
    ->assertEqual($comp['status'], 'same');

  // Drop the table bypassing the API.
  $ret = array();
  db_drop_table($ret, $table
    ->get('name'));
  $this
    ->assertTrue(isset($ret[0]['success']), 'Dropped table bypassing the API.');

  // Override table.
  $this
    ->drupalGet('admin/build/data');
  $this
    ->assertText('Override');
  $edit = array(
    'new[name]' => 'weight',
    'new[label]' => 'Weight',
    'new[type]' => 'int',
    'new[unsigned]' => TRUE,
    'new[index]' => TRUE,
  );
  $this
    ->drupalPost('admin/build/data/edit/data_table_kittens', $edit, 'Add new');

  // We are expecting an error here.
  $this
    ->assertText('Table does not exist in database');
  $this
    ->assertText('Go to Compare schemas to resolve conflicts.');

  // Go to admin/build/data/compare and create the table again.
  $this
    ->drupalPost('admin/build/data/compare/data_table_kittens', array(), t('Create table'));
  $this
    ->assertText('Created table data_table_kittens');

  // Repost the new field - this should work now.
  $this
    ->drupalPost('admin/build/data/edit/data_table_kittens', $edit, 'Add new');
  $this
    ->assertText('Added field weight');
  $this
    ->assertText('Added index for field weight');

  // @todo: Add a new PK configuration - this does not work right now as DB layer writes one thing while schema API it reads another.
  // $this->drupalPost('admin/build/data/edit/data_table_kittens', array('fields[weight][primary]' => TRUE), 'Save');
  // $this->assertText('Saved changes');
  $this
    ->drupalGet('admin/build/data');
  $this
    ->assertText('Overridden');
  $this
    ->drupalGet('admin/build/data/compare');
  $this
    ->assertText('same');

  // Drop field that we just created and try to recreate it.
  $ret = array();
  db_drop_field($ret, 'data_table_kittens', 'weight');
  $this
    ->assertTrue(isset($ret[0]['success']), 'Dropped weight field bypassing the API.');
  $this
    ->drupalGet('admin/build/data/compare');
  $this
    ->assertText('different - adjust');
  $this
    ->drupalGet('admin/build/data/compare/data_table_kittens');
  $this
    ->assertText('Status:');
  $this
    ->assertText('different');
  $this
    ->assertText('Reasons:');
  $this
    ->assertText('weight: not in database');
  $this
    ->assertText('indexes weight: missing in database');

  // First try to alter table.
  $this
    ->drupalPost('admin/build/data/compare/data_table_kittens', array(), 'Alter table');
  $this
    ->assertText('Resolved');
  $this
    ->assertText('weight: not in database');
  $this
    ->assertText('indexes weight: missing in database');
  $this
    ->assertText('same');

  // Drop field again and now try to adjust schema info about table.
  $ret = array();
  db_drop_field($ret, 'data_table_kittens', 'weight');
  $this
    ->assertTrue(isset($ret[0]['success']), 'Dropped weight field bypassing the API.');
  $this
    ->drupalPost('admin/build/data/compare/data_table_kittens', array(), 'Update schema information');
  $this
    ->assertText('Updated schema for data_table_kittens');
  $this
    ->assertText('same');
}