You are here

function MigrateUnitTest::testCRUD in Migrate 6

Test API for managing content sets

File

tests/migrate_api.test, line 40
Tests for the Migrate API.

Class

MigrateUnitTest
API tests for the Migrate module

Code

function testCRUD() {

  // Create test table
  $ret = array();
  $schema = array(
    'fields' => array(
      'id' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'title' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
      'body' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      ),
    ),
    'primary key' => array(
      'id',
    ),
  );
  $tablename = 'migrate_simpletest_sample';
  db_create_table($ret, $tablename, $schema);
  $sql = "INSERT INTO {" . $tablename . "} (title, body) VALUES('%s', '%s')";
  db_query($sql, 'Title 1', 'This is a body');
  db_query($sql, 'Title 2', 'This is another body');
  db_query($sql, 'Title 3', 'This is yet another body');

  // Creates default view '$tablename'
  tw_add_tables($tablename);

  // migrate_save_content_set() cases to test:
  //  Int vs. serial vs. string key
  //  Changing key
  //  Array vs. object parameter
  //  New vs. updated content set
  //  External database
  //  Passing explicit base_table/base_database options
  $content_set = new stdClass();
  $content_set->view_name = $tablename;
  $content_set->sourcekey = 'id';
  $content_set->contenttype = 'node';
  $content_set->desttype = 'page';
  $content_set->machine_name = 'node_test';
  $content_set->description = 'Node test';
  $content_set->weight = 3;
  $content_set->lastimported = NULL;
  $mcsid = migrate_save_content_set($content_set, array(
    'base_table' => $tablename,
  ));
  if ($this
    ->assertTrue($mcsid, t('Create simple page content set'))) {
    $row = db_fetch_object(db_query("SELECT * FROM {migrate_content_sets} WHERE mcsid=%d", $mcsid));
    if (!$this
      ->assertEqual($row, $content_set, t('Content set data matches'))) {
      $this
        ->error('row: ' . print_r($row, TRUE));
      $this
        ->error('content_set: ' . print_r($content_set, TRUE));
    }
    $sourceidschema = array(
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
    );
    $maptablename = migrate_map_table_name($mcsid);
    if ($this
      ->assertTrue(db_table_exists($maptablename), t('Map table exists'))) {
      $mapschema = _migrate_map_table_schema($sourceidschema);
      $mapschema['name'] = $maptablename;
      $info = schema_compare_table($mapschema);
      if (!$this
        ->assertEqual($info['status'], 'same', t('Map table schema matches'))) {
        $this
          ->error('Comparison returned: ' . print_r($info, TRUE));
      }
    }
    $msgtablename = migrate_message_table_name($mcsid);
    if ($this
      ->assertTrue(db_table_exists($msgtablename), t('Message table exists'))) {
      $msgschema = _migrate_message_table_schema($sourceidschema);
      $msgschema['name'] = $msgtablename;
      $info = schema_compare_table($msgschema);
      if (!$this
        ->assertEqual($info['status'], 'same', t('Message table schema matches'))) {
        $this
          ->error('Comparison returned: ' . print_r($info, TRUE));
      }
    }
  }

  // Regression test for #560380 - don't break Views if message table is missing
  tw_remove_tables(array(
    $msgtablename,
  ));
  $this
    ->drupalGet('admin/build/views');
  $this
    ->assertResponse(200, t('Successfully got views listing'));

  // Testing that deleting removes all traces
  migrate_delete_content_set($mcsid);
  $this
    ->assertFalse(db_table_exists($maptablename), t('Map table deleted'));
  $this
    ->assertFalse(db_table_exists($msgtablename), t('Message table deleted'));
  $count = db_result(db_query("SELECT COUNT(*) FROM {migrate_content_sets} WHERE mcsid=%d", $mcsid));
  $this
    ->assertEqual($count, 0, t('Content set row deleted'));
  $count = db_result(db_query("SELECT COUNT(*) FROM {migrate_content_mappings} WHERE mcsid=%d", $mcsid));
  $this
    ->assertEqual($count, 0, t('Content set mapping rows deleted'));

  // migrate_save_content_mapping()
  // migrate_delete_content_mapping()
}