<?php

/**
 * @file
 * Tests for the XML source plugins.
 */

/**
 * Test node migration.
 */
class MigrateXMLUnitTest extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'XML migration',
      'description' => 'Test migration from XML source',
      'group' => 'Migrate',
    );
  }

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

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

  function testNodeImport() {
    $migration = Migration::getInstance('WineRegion');
    $result = $migration->processImport();
    $this->assertEqual($result, Migration::RESULT_COMPLETED,
      t('Region term import returned RESULT_COMPLETED'));

    $migration = Migration::getInstance('WineFileCopy');
    $result = $migration->processImport();
    $this->assertEqual($result, Migration::RESULT_COMPLETED,
      t('File import returned RESULT_COMPLETED'));

    $migration = Migration::getInstance('WineRole');
    $result = $migration->processImport();
    $this->assertEqual($result, Migration::RESULT_COMPLETED,
      t('Role import returned RESULT_COMPLETED'));

    $migration = Migration::getInstance('WineUser');
    $result = $migration->processImport();
    $this->assertEqual($result, Migration::RESULT_COMPLETED,
      t('User import returned RESULT_COMPLETED'));

    $migration1 = Migration::getInstance('WineProducerXML');
    $result = $migration1->processImport();
    $this->assertEqual($result, Migration::RESULT_COMPLETED,
      t('Producer node import 1 returned RESULT_COMPLETED'));

    $migration2 = Migration::getInstance('WineProducerNamespaceXML');
    $result = $migration2->processImport();
    $this->assertEqual($result, Migration::RESULT_COMPLETED,
      t('Producer node import 2 returned RESULT_COMPLETED'));

    $migration3 = Migration::getInstance('WineProducerMultiXML');
    $result = $migration3->processImport();
    $this->assertEqual($result, Migration::RESULT_COMPLETED,
      t('Producer node import 3 returned RESULT_COMPLETED'));

    $migration4 = Migration::getInstance('WineProducerMultiNamespaceXML');
    $result = $migration4->processImport();
    $this->assertEqual($result, Migration::RESULT_COMPLETED,
      t('Producer node import 4 returned RESULT_COMPLETED'));

    $migration5 = Migration::getInstance('WineProducerXMLPull');
    $result = $migration5->processImport();
    $this->assertEqual($result, Migration::RESULT_COMPLETED,
      t('Producer node import 5 returned RESULT_COMPLETED'));

    $migration6 = Migration::getInstance('WineProducerNamespaceXMLPull');
    $result = $migration6->processImport();
    $this->assertEqual($result, Migration::RESULT_COMPLETED,
      t('Producer node import 6 returned RESULT_COMPLETED'));

    // Gather producer nodes, and their corresponding input data
    $rawnodes = node_load_multiple(FALSE, array('type' => 'migrate_example_producer'), TRUE);
    // Index by title
    $producer_nodes = array();
    foreach ($rawnodes as $node) {
      $producer_nodes[$node->title] = $node;
    }

    $this->assertEqual(count($producer_nodes), 10,
      t('Counts of producer nodes and input rows match'));

    // Test each base node field
    $producer_node = $producer_nodes['Lolonis Winery'];
    $user_migration = MigrationBase::getInstance('WineUser');

    $mapped_uid = $user_migration->getMap()->lookupDestinationID(array(3));
    if (is_array($mapped_uid)) {
      $this->assertEqual($producer_node->uid, reset($mapped_uid),
        t('uid properly migrated'));
    }
    else {
      $this->error(t('Account ID !id not migrated', array('!id' => 3)));
    }

    // Test Field API fields of all types
    // body_with_summary
    $body = field_get_items('node', $producer_node, 'body');
    $this->assertEqual($body[0]['value'], 'Makers of Ladybug Red',
      t('body properly migrated'));
    $region = field_get_items('node', $producer_node, 'migrate_example_wine_regions');
    $term = taxonomy_get_term_by_name('Redwood Valley');
    $term = reset($term);
    $this->assertEqual($region[0]['tid'], $term->tid,
      t('region properly migrated'));

    // Rollback producer migrations
    $result = $migration1->processRollback();
    $this->assertEqual($result, Migration::RESULT_COMPLETED,
      t('Producer node rollback 1 returned RESULT_COMPLETED'));

    $result = $migration2->processRollback();
    $this->assertEqual($result, Migration::RESULT_COMPLETED,
      t('Producer node rollback 2 returned RESULT_COMPLETED'));

    $result = $migration3->processRollback();
    $this->assertEqual($result, Migration::RESULT_COMPLETED,
      t('Producer node rollback 3 returned RESULT_COMPLETED'));

    $result = $migration4->processRollback();
    $this->assertEqual($result, Migration::RESULT_COMPLETED,
      t('Producer node rollback 4 returned RESULT_COMPLETED'));

    $result = $migration5->processRollback();
    $this->assertEqual($result, Migration::RESULT_COMPLETED,
      t('Producer node rollback 5 returned RESULT_COMPLETED'));

    $result = $migration6->processRollback();
    $this->assertEqual($result, Migration::RESULT_COMPLETED,
      t('Producer node rollback 6 returned RESULT_COMPLETED'));

    // Test rollback
    $rawnodes = node_load_multiple(FALSE, array('type' => 'migrate_example_producer'), TRUE);
    $this->assertEqual(count($rawnodes), 0, t('All nodes deleted'));
    $count = db_select('migrate_map_wineproducerxml', 'map')
              ->fields('map', array('sourceid1'))
              ->countQuery()
              ->execute()
              ->fetchField();
    $this->assertEqual($count, 0, t('Map cleared'));
    $count = db_select('migrate_message_wineproducerxml', 'msg')
              ->fields('msg', array('sourceid1'))
              ->countQuery()
              ->execute()
              ->fetchField();
    $this->assertEqual($count, 0, t('Messages cleared'));
  }
}
