You are here

function MigrateCommentTest::testComment in Migrate 6

Test comment migration

File

tests/modules/core/migrate_comment.test, line 140
Tests for comment migration

Class

MigrateCommentTest
@file Tests for comment migration

Code

function testComment() {

  // First, test migration from source IDs
  // Create the content sets
  $content_set = new stdClass();
  $content_set->view_name = $this->storytable;
  $content_set->sourcekey = 'id';
  $content_set->contenttype = 'node';
  $content_set->desttype = 'story';
  $content_set->machine_name = 'stories';
  $content_set->description = 'Stories';
  $content_set->weight = 1;
  $storymcsid = migrate_save_content_set($content_set, array(
    'base_table' => $this->storytable,
  ));
  $storymap = migrate_map_table_name($storymcsid);
  $mapping = new stdClass();
  $mapping->mcsid = $storymcsid;
  $mapping->srcfield = $this->storytable . '_title';
  $mapping->destfield = 'title';
  migrate_save_content_mapping($mapping);
  $mapping = new stdClass();
  $mapping->mcsid = $storymcsid;
  $mapping->srcfield = $this->storytable . '_body';
  $mapping->destfield = 'body';
  migrate_save_content_mapping($mapping);
  $content_set = new stdClass();
  $content_set->view_name = $this->usertable;
  $content_set->sourcekey = 'id';
  $content_set->contenttype = 'user';
  $content_set->machine_name = 'users';
  $content_set->description = 'Users';
  $content_set->weight = 2;
  $usermcsid = migrate_save_content_set($content_set, array(
    'base_table' => $this->usertable,
  ));
  $usermap = migrate_map_table_name($usermcsid);
  $mapping = new stdClass();
  $mapping->mcsid = $usermcsid;
  $mapping->srcfield = $this->usertable . '_username';
  $mapping->destfield = 'name';
  migrate_save_content_mapping($mapping);
  $mapping = new stdClass();
  $mapping->mcsid = $usermcsid;
  $mapping->srcfield = $this->usertable . '_email';
  $mapping->destfield = 'mail';
  migrate_save_content_mapping($mapping);
  $content_set = new stdClass();
  $content_set->view_name = $this->commenttable;
  $content_set->sourcekey = 'id';
  $content_set->contenttype = 'comment';
  $content_set->machine_name = 'comments';
  $content_set->description = 'Comments';
  $content_set->weight = 3;
  $commentmcsid = migrate_save_content_set($content_set, array(
    'base_table' => $this->commenttable,
  ));
  $commentmap = migrate_map_table_name($commentmcsid);
  $storymapping = new stdClass();
  $storymapping->mcsid = $commentmcsid;
  $storymapping->srcfield = $this->commenttable . '_storyid';
  $storymapping->destfield = 'nodesourceid';
  migrate_save_content_mapping($storymapping);
  $usermapping = new stdClass();
  $usermapping->mcsid = $commentmcsid;
  $usermapping->srcfield = $this->commenttable . '_userid';
  $usermapping->destfield = 'usersourceid';
  migrate_save_content_mapping($usermapping);
  $parentmapping = new stdClass();
  $parentmapping->mcsid = $commentmcsid;
  $parentmapping->srcfield = $this->commenttable . '_parentid';
  $parentmapping->destfield = 'commentsourceid';
  migrate_save_content_mapping($parentmapping);
  $mapping = new stdClass();
  $mapping->mcsid = $commentmcsid;
  $mapping->srcfield = $this->commenttable . '_comment_text';
  $mapping->destfield = 'comment';
  migrate_save_content_mapping($mapping);
  $mapping = new stdClass();
  $mapping->mcsid = $commentmcsid;
  $mapping->srcfield = $this->commenttable . '_status';
  $mapping->destfield = 'status';
  migrate_save_content_mapping($mapping);
  $edit = array(
    "importing[{$storymcsid}]" => $storymcsid,
    "importing[{$usermcsid}]" => $usermcsid,
    "importing[{$commentmcsid}]" => $commentmcsid,
  );
  $this
    ->drupalPost('admin/content/migrate', $edit, t('Run'));
  if (!$this
    ->assertText('Imported 3 in') || !$this
    ->assertText('Imported 4 in') || !$this
    ->assertText("done with 'Stories'") || !$this
    ->assertText("done with 'Users'") || !$this
    ->assertText("done with 'Comments")) {
    $result = preg_match('|<div class="messages status">(.*?)</div>|si', $this->content, $matches);
    $this
      ->error('Actual messages: ' . $matches[1]);
  }

  // Make sure all the data matches from source to destination
  $sql = "SELECT COUNT(*)\n            FROM {migrate_simpletest_comment} c\n\n            INNER JOIN {migrate_simpletest_story} s ON c.storyid=s.id\n            INNER JOIN {" . $storymap . "} smap ON s.id=smap.sourceid\n            INNER JOIN {node} ds ON smap.destid=ds.nid\n            INNER JOIN {node_revisions} dsr ON ds.vid=dsr.vid\n\n            INNER JOIN {migrate_simpletest_user} u ON c.userid=u.id\n            INNER JOIN {" . $usermap . "} umap ON u.id=umap.sourceid\n            INNER JOIN {users} du ON umap.destid=du.uid\n\n            INNER JOIN {" . $commentmap . "} cmap ON c.id=cmap.sourceid\n            INNER JOIN {comments} dc ON cmap.destid=dc.cid\n\n            LEFT JOIN {" . $commentmap . "} cmap2 ON c.parentid=cmap2.sourceid\n\n            WHERE s.title=ds.title AND s.body=dsr.body\n              AND u.username=du.name AND u.email=du.mail\n              AND c.comment_text=dc.comment AND (c.parentid=0 OR cmap2.destid=dc.pid)\n              AND c.status=dc.status";
  $count = db_result(db_query($sql));
  if (!$this
    ->assertEqual($count, 4, t('4 comments migrated successfully'))) {
    $this
      ->error(t('Actual count: !count', array(
      '!count' => $count,
    )));
  }

  // Clear the comment content sets
  $edit = array(
    "clearing[{$commentmcsid}]" => $commentmcsid,
  );
  $this
    ->drupalPost('admin/content/migrate', $edit, t('Run'));
  if (!$this
    ->assertText('Deleted 4 in') || !$this
    ->assertText("done with 'Comments")) {
    $result = preg_match('|<div class="messages status">(.*?)</div>|si', $this->content, $matches);
    $this
      ->error('Actual messages: ' . $matches[1]);
  }

  // Now, let's do it again, but expressing incoming IDs as Drupal IDs
  // Convert source IDs to Drupal IDs
  $sql = "SELECT * FROM {migrate_simpletest_comment}";
  $result = db_query($sql);
  while ($row = db_fetch_object($result)) {
    $nodeid = _migrate_xlat_get_new_id('node', $row->storyid);
    $userid = _migrate_xlat_get_new_id('user', $row->userid);
    $parentid = _migrate_xlat_get_new_id('comment', $row->parentid);
    db_query("UPDATE {migrate_simpletest_comment}\n                 SET storyid=%d, userid=%d, parentid=%d\n                WHERE id=%d", $nodeid, $userid, $parentid, $row->id);
  }

  // Change the mappings to indicate the sources are Drupal IDs
  $storymapping->destfield = 'nid';
  migrate_save_content_mapping($storymapping);
  $usermapping->destfield = 'uid';
  migrate_save_content_mapping($usermapping);
  $parentmapping->destfield = 'pid';
  migrate_save_content_mapping($parentmapping);

  // Reimport
  $edit = array(
    "importing[{$commentmcsid}]" => $commentmcsid,
  );
  $this
    ->drupalPost('admin/content/migrate', $edit, t('Run'));
  if (!$this
    ->assertText('Imported 4 in') || !$this
    ->assertText("done with 'Comments")) {
    $result = preg_match('|<div class="messages status">(.*?)</div>|si', $this->content, $matches);
    $this
      ->error('Actual messages: ' . $matches[1]);
  }

  // Make sure all the data matches from source to destination
  $sql = "SELECT COUNT(*)\n            FROM {migrate_simpletest_comment} c\n            INNER JOIN {" . $commentmap . "} cmap ON c.id=cmap.sourceid\n            INNER JOIN {comments} dc ON cmap.destid=dc.cid\n            WHERE c.comment_text=dc.comment AND c.storyid=dc.nid\n              AND c.userid=dc.uid AND c.parentid=dc.pid";
  $count = db_result(db_query($sql));
  if (!$this
    ->assertEqual($count, 4, t('4 comments migrated successfully'))) {
    $this
      ->error(t('Actual count: !count', array(
      '!count' => $count,
    )));
  }
}