term.test 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * @file
  4. * Tests for the taxonomy term destination plugin.
  5. */
  6. /**
  7. * Test taxonomy migration.
  8. */
  9. class MigrateTaxonomyUnitTest extends DrupalWebTestCase {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Taxonomy migration',
  13. 'description' => 'Test migration of taxonomy data',
  14. 'group' => 'Migrate',
  15. );
  16. }
  17. function setUp() {
  18. parent::setUp('taxonomy', 'migrate', 'migrate_example');
  19. }
  20. function testTermImport() {
  21. $migration = Migration::getInstance('WineVariety');
  22. $result = $migration->processImport();
  23. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  24. t('Variety term import returned RESULT_COMPLETED'));
  25. $vocab = taxonomy_vocabulary_machine_name_load('migrate_example_wine_varieties');
  26. $rawterms = taxonomy_term_load_multiple(array(), array('vid' => $vocab->vid));
  27. $terms = array();
  28. foreach ($rawterms as $term) {
  29. $terms[$term->name] = $term;
  30. }
  31. $query = db_select('migrate_example_wine_categories', 'wc')
  32. ->fields('wc', array('categoryid', 'name', 'details', 'category_parent', 'ordering'))
  33. ->condition('wc.type', 'variety');
  34. $query->leftJoin('migrate_example_wine_categories', 'wcpar',
  35. 'wc.category_parent=wcpar.categoryid');
  36. $query->addField('wcpar', 'name', 'parent_name');
  37. $result = $query->execute();
  38. $rows = array();
  39. foreach ($result as $row) {
  40. $rows[$row->name] = $row;
  41. }
  42. $this->assertEqual(count($terms), count($rows), t('Counts of variety terms and input rows match'));
  43. // Test each base term field
  44. $this->assert(isset($terms['Merlot']) && isset($rows['Merlot']),
  45. t("Name 'Merlot' migrated correctly"));
  46. $this->assertEqual($terms['Merlot']->description, $rows['Merlot']->details,
  47. t('Descriptions match'));
  48. $this->assertEqual($terms['Merlot']->weight, $rows['Merlot']->ordering,
  49. t('Weights match'));
  50. $this->assertEqual($terms['Merlot']->format, $migration->basicFormat->format,
  51. t('Formats match'));
  52. $parents = taxonomy_get_parents($terms['White wine']->tid);
  53. $this->assertEqual(count($parents), 0, t('Term without parent properly migrated'));
  54. $parents = taxonomy_get_parents($terms['Merlot']->tid);
  55. $parent = array_pop($parents);
  56. $this->assertEqual($parent->name, 'Red wine', t('Parents match'));
  57. // Test updates
  58. // Capture original terms
  59. $tempterms = taxonomy_term_load_multiple(array(), array('vid' => $vocab->vid));
  60. foreach ($tempterms as $tid => $term) {
  61. $original_terms[$tid] = clone $term;
  62. }
  63. $update_migration = Migration::getInstance('WineVarietyUpdates');
  64. $result = $update_migration->processImport();
  65. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  66. t('Wine variety term updates import returned RESULT_COMPLETED'));
  67. $final_terms = taxonomy_term_load_multiple(array(), array('vid' => $vocab->vid));
  68. foreach ($original_terms as $tid => $original_term) {
  69. foreach ($original_term as $field => $value) {
  70. if ($field == 'description') {
  71. if ($value == $final_terms[$tid]->$field) {
  72. $this->error(t('Field !field should have changed but did not, value=!value',
  73. array('!field' => $field, '!value' => print_r($value, TRUE))));
  74. }
  75. }
  76. else {
  77. if ($value != $final_terms[$tid]->$field) {
  78. $this->error(t('Field !field mismatch: original !value1, result !value2',
  79. array('!field' => $field, '!value1' => print_r($value, TRUE),
  80. '!value2' => print_r($final_terms[$tid]->$field, TRUE))));
  81. }
  82. }
  83. }
  84. }
  85. // Test rollback
  86. $result = $migration->processRollback(array('force' => TRUE));
  87. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  88. t('Variety term rollback returned RESULT_COMPLETED'));
  89. $rawterms = taxonomy_term_load_multiple(array(), array('vid' => $vocab->vid));
  90. $this->assertEqual(count($rawterms), 0, t('All terms deleted'));
  91. $count = db_select('migrate_map_winevariety', 'map')
  92. ->fields('map', array('sourceid1'))
  93. ->countQuery()
  94. ->execute()
  95. ->fetchField();
  96. $this->assertEqual($count, 0, t('Map cleared'));
  97. $count = db_select('migrate_message_winevariety', 'msg')
  98. ->fields('msg', array('sourceid1'))
  99. ->countQuery()
  100. ->execute()
  101. ->fetchField();
  102. $this->assertEqual($count, 0, t('Messages cleared'));
  103. }
  104. }