View source
<?php
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');
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),
)));
}
$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'));
$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'));
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') {
}
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),
)));
}
}
}
}
$result = $migration
->processRollback(array(
'force' => TRUE,
));
$this
->assertEqual($result, Migration::RESULT_COMPLETED, t('Variety term rollback returned RESULT_COMPLETED'));
$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'));
}
}