<?php

/**
 * @file
 * Tests for the table destination plugin.
 */

/**
 * Test table migration.
 */
class MigrateTableUnitTest extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Table migration',
      'description' => 'Test migration of table data',
      'group' => 'Migrate',
    );
  }

  function setUp() {
    parent::setUp('migrate', 'migrate_example');

    // Make sure the migrations are registered.
    migrate_static_registration();
  }

  function testTableImport() {
    $migration = Migration::getInstance('WineTable');
    $result = $migration->processImport();
    $this->assertEqual($result, Migration::RESULT_COMPLETED,
      t('Table import returned RESULT_COMPLETED'));

    $result = db_query(
      "SELECT COUNT(*)
       FROM {migrate_example_wine_table_source} s
       INNER JOIN {migrate_map_winetable} map ON s.fooid=map.sourceid1
       INNER JOIN {migrate_example_wine_table_dest} d ON map.destid1=d.recordid"
    );

    $this->assertEqual($result->fetchField(), 3,
      t('Count of imported records is correct'));

    // Test rollback
    $result = $migration->processRollback();
    $this->assertEqual($result, Migration::RESULT_COMPLETED,
      t('Variety term rollback returned RESULT_COMPLETED'));
    $result = db_query("SELECT COUNT(*) FROM {migrate_example_wine_table_dest}");
    $this->assertEqual($result->fetchField(), 0, t('All migrated rows removed'));
    $result = db_query("SELECT COUNT(*) FROM {migrate_map_winetable}");
    $this->assertEqual($result->fetchField(), 0, t('All map rows removed'));
  }
}
