comment.test 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * @file
  4. * Tests for the comment destination plugin.
  5. */
  6. /**
  7. * Test comment migration.
  8. */
  9. class MigrateCommentUnitTest extends DrupalWebTestCase {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Comment migration',
  13. 'description' => 'Test migration of comment data',
  14. 'group' => 'Migrate',
  15. );
  16. }
  17. function setUp() {
  18. parent::setUp('taxonomy', 'image', 'comment', 'migrate', 'migrate_example');
  19. }
  20. function testCommentImport() {
  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. $migration = Migration::getInstance('WineRegion');
  26. $result = $migration->processImport();
  27. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  28. t('Region term import returned RESULT_COMPLETED'));
  29. $migration = Migration::getInstance('WineBestWith');
  30. $result = $migration->processImport();
  31. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  32. t('"Best With" term import returned RESULT_COMPLETED'));
  33. $migration = Migration::getInstance('WineFileCopy');
  34. $result = $migration->processImport();
  35. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  36. t('File import returned RESULT_COMPLETED'));
  37. $migration = Migration::getInstance('WineRole');
  38. $result = $migration->processImport();
  39. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  40. t('Role import returned RESULT_COMPLETED'));
  41. $migration = Migration::getInstance('WineUser');
  42. $result = $migration->processImport();
  43. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  44. t('User import returned RESULT_COMPLETED'));
  45. $migration = Migration::getInstance('WineProducer');
  46. $result = $migration->processImport();
  47. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  48. t('Producer node import returned RESULT_COMPLETED'));
  49. $migration = Migration::getInstance('WineWine');
  50. $result = $migration->processImport();
  51. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  52. t('Wine node import returned RESULT_COMPLETED'));
  53. $migration = Migration::getInstance('WineComment');
  54. $result = $migration->processImport();
  55. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  56. t('Comment import returned RESULT_COMPLETED'));
  57. $result = db_select('migrate_message_winecomment', 'w')
  58. ->fields('w', array('message'))
  59. ->execute();
  60. foreach ($result as $row) {
  61. $this->error($row->message);
  62. }
  63. $result = db_select('migrate_example_wine_comment', 'wc')
  64. ->fields('wc', array('commentid', 'comment_parent', 'name', 'mail',
  65. 'accountid', 'body', 'wineid', 'subject', 'commenthost', 'userpage',
  66. 'posted', 'lastchanged'))
  67. ->orderBy('comment_parent')
  68. ->execute();
  69. $rawcomments = comment_load_multiple(FALSE);
  70. // Index by subject
  71. $comments = array();
  72. foreach ($rawcomments as $comment) {
  73. $comments[$comment->subject] = $comment;
  74. }
  75. $rows = array();
  76. foreach ($result as $row) {
  77. $rows[$row->subject] = $row;
  78. }
  79. if (!$this->assertEqual(count($comments), count($rows), t('Counts of comments and input rows match'))) {
  80. $this->error(t('!comments comments, should be !rows',
  81. array('!comments' => count($comments), '!rows' => count($rows))));
  82. }
  83. $comment = $comments['im second'];
  84. $row = $rows['im second'];
  85. $this->assertEqual($comment->mail, $row->mail, t('Mail matches'));
  86. $this->assertEqual($comment->name, $row->name, t('Name matches'));
  87. $this->assertEqual($comment->status, COMMENT_PUBLISHED, t('Status matches'));
  88. $wine_migration = MigrationBase::getInstance('WineWine');
  89. $destid = $wine_migration->getMap()->lookupDestinationID(array($row->wineid));
  90. $this->assertEqual($comment->nid, reset($destid), t('Nid matches'));
  91. $body = field_get_items('comment', $comment, 'comment_body');
  92. $this->assertEqual($body[0]['value'], $row->body, t('Body matches'));
  93. $this->assertEqual($comment->hostname, $row->commenthost, t('Hostname matches'));
  94. $this->assertEqual($comment->homepage, $row->userpage, t('Homepage matches'));
  95. $this->assertEqual($comment->created, $row->posted, t('Created matches'));
  96. $this->assertEqual($comment->changed, $row->lastchanged, t('Changed matches'));
  97. $comment = $comments['im child'];
  98. $row = $rows['im child'];
  99. $user_migration = MigrationBase::getInstance('WineUser');
  100. $destid = $user_migration->getMap()->lookupDestinationID(array($row->accountid));
  101. $this->assertEqual($comment->uid, reset($destid), t('Uid matches'));
  102. $this->assertEqual($comment->pid, $comments['im parent']->cid, t('Parent matches'));
  103. // Test updates
  104. // Capture original comments
  105. $original_comments = comment_load_multiple(FALSE);
  106. $update_migration = Migration::getInstance('WineCommentUpdates');
  107. $result = $update_migration->processImport();
  108. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  109. t('Wine comment updates import returned RESULT_COMPLETED'));
  110. $final_comments = comment_load_multiple(FALSE);
  111. foreach ($original_comments as $cid => $original_comment) {
  112. foreach ($original_comment as $field => $value) {
  113. if ($field == 'subject') {
  114. if ($value == $final_comments[$cid]->$field) {
  115. $this->error(t('Field !field should have changed but did not, value=!value',
  116. array('!field' => $field, '!value' => print_r($value, TRUE))));
  117. }
  118. }
  119. else {
  120. if ($value != $final_comments[$cid]->$field) {
  121. $this->error(t('Field !field mismatch: original !value1, result !value2',
  122. array('!field' => $field, '!value1' => print_r($value, TRUE),
  123. '!value2' => print_r($final_comments[$cid]->$field, TRUE))));
  124. }
  125. }
  126. }
  127. }
  128. // Test rollback
  129. $result = $migration->processRollback(array('force' => TRUE));
  130. $this->assertEqual($result, Migration::RESULT_COMPLETED,
  131. t('Comment rollback returned RESULT_COMPLETED'));
  132. $rawcomments = comment_load_multiple(FALSE);
  133. $this->assertEqual(count($rawcomments), 0, t('All comments deleted'));
  134. $count = db_select('migrate_map_winecomment', 'map')
  135. ->fields('map', array('sourceid1'))
  136. ->countQuery()
  137. ->execute()
  138. ->fetchField();
  139. $this->assertEqual($count, 0, t('Map cleared'));
  140. $count = db_select('migrate_message_winecomment', 'msg')
  141. ->fields('msg', array('sourceid1'))
  142. ->countQuery()
  143. ->execute()
  144. ->fetchField();
  145. $this->assertEqual($count, 0, t('Messages cleared'));
  146. }
  147. }