You are here

term.test in Migrate 6.2

Same filename and directory in other branches
  1. 7.2 tests/plugins/destinations/term.test

File

tests/plugins/destinations/term.test
View source
<?php

/**
 * Test taxonomy migration.
 */
class MigrateTaxonomyUnitTest extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Taxonomy migration',
      'description' => 'Test migration of taxonomy data',
      'group' => 'Migrate',
    );
  }
  function setUp() {
    parent::setUp('autoload', 'dbtng', 'taxonomy', 'content', 'text', 'number', 'date_api', 'date_timezone', 'date', 'filefield', 'imagefield', 'migrate', 'migrate_extras', 'migrate_example');

    // Make sure the migrations are registered.
    migrate_get_module_apis();
  }
  function testTermImport() {
    $migration = Migration::getInstance('WineVariety');
    $result = $migration
      ->processImport();
    $this
      ->assertEqual($result, Migration::RESULT_COMPLETED, t('Variety term import returned RESULT_COMPLETED'));
    $vid = db_select('vocabulary', 'v')
      ->fields('v', array(
      'vid',
    ))
      ->condition('name', 'Migrate Example Wine Varieties')
      ->execute()
      ->fetchField();
    $rawterms = taxonomy_get_tree($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;
    }
    if (!$this
      ->assertEqual(count($terms), count($rows), t('Counts of variety terms and input rows match'))) {
      $this
        ->error(t('!terms of !rows rows successfully migrated', array(
        '!terms' => count($terms),
        '!rows' => count($rows),
      )));
    }

    // 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'));
    $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_get_tree($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'));

    // We can't use taxonomy_get_tree again, because it cached everything the first
    // time and there's no way to reset it - we must fetch each term with a cache reset,
    // and we will ignore the depth and parent fields which are populated by taxonomy_get_tree
    foreach ($original_terms as $tid => $original_term) {
      $final_term = taxonomy_get_term($original_term->tid, TRUE);
      foreach ($original_term as $field => $value) {
        if ($field == 'description') {
          if ($value == $final_term->{$field}) {
            $this
              ->error(t('Field !field should have changed but did not, value=!value', array(
              '!field' => $field,
              '!value' => print_r($value, TRUE),
            )));
          }
        }
        elseif ($field == 'depth' || $field == 'parents') {

          // Just ignore
        }
        else {
          if ($value != $final_term->{$field}) {
            $this
              ->error(t('Field !field mismatch: original !value1, result !value2', array(
              '!field' => $field,
              '!value1' => print_r($value, TRUE),
              '!value2' => print_r($final_term->{$field}, TRUE),
            )));
          }
        }
      }
    }

    // Test rollback
    $result = $migration
      ->processRollback(array(
      'force' => TRUE,
    ));
    $this
      ->assertEqual($result, Migration::RESULT_COMPLETED, t('Variety term rollback returned RESULT_COMPLETED'));

    // You'd think we could call taxonomy_get_tree() again, but its static cache
    // doesn't know about the deletions
    $count = db_select('term_data', 'td')
      ->fields('td', array(
      'tid',
    ))
      ->condition('vid', $vid)
      ->countQuery()
      ->execute()
      ->fetchField();
    if (!$this
      ->assertEqual($count, 0, t('All terms deleted'))) {
      $this
        ->error(t('!count terms remaining', array(
        '!count' => $count,
      )));
    }
    $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'));
  }

}

Classes

Namesort descending Description
MigrateTaxonomyUnitTest Test taxonomy migration.