You are here

function MigrateTaxonomyUnitTest::testTermImport in Migrate 7.2

Same name and namespace in other branches
  1. 6.2 tests/plugins/destinations/term.test \MigrateTaxonomyUnitTest::testTermImport()

File

tests/plugins/destinations/term.test, line 28
Tests for the taxonomy term destination plugin.

Class

MigrateTaxonomyUnitTest
Test taxonomy migration.

Code

function testTermImport() {
  $migration = Migration::getInstance('WineVariety');
  $result = $migration
    ->processImport();
  $this
    ->assertEqual($result, Migration::RESULT_COMPLETED, t('Variety term import returned RESULT_COMPLETED'));
  $vocab = taxonomy_vocabulary_machine_name_load('migrate_example_wine_varieties');
  $rawterms = taxonomy_term_load_multiple(array(), array(
    'vid' => $vocab->vid,
  ));
  $terms = array();
  foreach ($rawterms as $term) {
    $terms[$term->name] = $term;
  }
  $query = db_select('migrate_example_wine_categories', 'wc')
    ->fields('wc', array(
    'categoryid',
    'name',
    'details',
    'category_parent',
    'ordering',
  ))
    ->condition('wc.type', 'variety');
  $query
    ->leftJoin('migrate_example_wine_categories', 'wcpar', 'wc.category_parent=wcpar.categoryid');
  $query
    ->addField('wcpar', 'name', 'parent_name');
  $result = $query
    ->execute();
  $rows = array();
  foreach ($result as $row) {
    $rows[$row->name] = $row;
  }
  $this
    ->assertEqual(count($terms), count($rows), t('Counts of variety terms and input rows match'));

  // Test each base term field
  $this
    ->assert(isset($terms['Merlot']) && isset($rows['Merlot']), t("Name 'Merlot' migrated correctly"));
  $this
    ->assertEqual($terms['Merlot']->description, $rows['Merlot']->details, t('Descriptions match'));
  $this
    ->assertEqual($terms['Merlot']->weight, $rows['Merlot']->ordering, t('Weights match'));
  $this
    ->assertEqual($terms['Merlot']->format, $migration->basicFormat->format, t('Formats match'));
  $parents = taxonomy_get_parents($terms['White wine']->tid);
  $this
    ->assertEqual(count($parents), 0, t('Term without parent properly migrated'));
  $parents = taxonomy_get_parents($terms['Merlot']->tid);
  $parent = array_pop($parents);
  $this
    ->assertEqual($parent->name, 'Red wine', t('Parents match'));

  // Test updates
  // Capture original terms
  $tempterms = taxonomy_term_load_multiple(array(), array(
    'vid' => $vocab->vid,
  ));
  foreach ($tempterms as $tid => $term) {
    $original_terms[$tid] = clone $term;
  }
  $update_migration = Migration::getInstance('WineVarietyUpdates');
  $result = $update_migration
    ->processImport();
  $this
    ->assertEqual($result, Migration::RESULT_COMPLETED, t('Wine variety term updates import returned RESULT_COMPLETED'));
  $final_terms = taxonomy_term_load_multiple(array(), array(
    'vid' => $vocab->vid,
  ));
  foreach ($original_terms as $tid => $original_term) {
    foreach ($original_term as $field => $value) {
      if ($field == 'description') {
        if ($value == $final_terms[$tid]->{$field}) {
          $this
            ->error(t('Field !field should have changed but did not, value=!value', array(
            '!field' => $field,
            '!value' => print_r($value, TRUE),
          )));
        }
      }
      else {
        if ($value != $final_terms[$tid]->{$field}) {
          $this
            ->error(t('Field !field mismatch: original !value1, result !value2', array(
            '!field' => $field,
            '!value1' => print_r($value, TRUE),
            '!value2' => print_r($final_terms[$tid]->{$field}, TRUE),
          )));
        }
      }
    }
  }

  // Test rollback
  $result = $migration
    ->processRollback(array(
    'force' => TRUE,
  ));
  $this
    ->assertEqual($result, Migration::RESULT_COMPLETED, t('Variety term rollback returned RESULT_COMPLETED'));
  $rawterms = taxonomy_term_load_multiple(array(), array(
    'vid' => $vocab->vid,
  ));
  $this
    ->assertEqual(count($rawterms), 0, t('All terms deleted'));
  $count = db_select('migrate_map_winevariety', 'map')
    ->fields('map', array(
    'sourceid1',
  ))
    ->countQuery()
    ->execute()
    ->fetchField();
  $this
    ->assertEqual($count, 0, t('Map cleared'));
  $count = db_select('migrate_message_winevariety', 'msg')
    ->fields('msg', array(
    'sourceid1',
  ))
    ->countQuery()
    ->execute()
    ->fetchField();
  $this
    ->assertEqual($count, 0, t('Messages cleared'));
}