<?php

/**
 * @file
 * Tests for the comment destination plugin.
 */

/**
 * Test comment migration.
 */
class MigrateCommentUnitTest extends DrupalWebTestCase {

  public static function getInfo() {
    return array(
      'name' => 'Comment migration',
      'description' => 'Test migration of comment data',
      'group' => 'Migrate',
    );
  }

  function setUp() {
    parent::setUp('taxonomy', 'image', 'comment', 'migrate', 'migrate_example');

    // Make sure the migrations are registered.
    migrate_static_registration();
  }

  function testCommentImport() {
    $migration = Migration::getInstance('WineVariety');
    $result = $migration->processImport();
    $this->assertEqual($result, Migration::RESULT_COMPLETED,
      t('Variety term import returned RESULT_COMPLETED'));
    $migration = Migration::getInstance('WineRegion');
    $result = $migration->processImport();
    $this->assertEqual($result, Migration::RESULT_COMPLETED,
      t('Region term import returned RESULT_COMPLETED'));
    $migration = Migration::getInstance('WineBestWith');
    $result = $migration->processImport();
    $this->assertEqual($result, Migration::RESULT_COMPLETED,
      t('"Best With" term import returned RESULT_COMPLETED'));
    $migration = Migration::getInstance('WineFileCopy');
    $result = $migration->processImport();
    $this->assertEqual($result, Migration::RESULT_COMPLETED,
      t('File import returned RESULT_COMPLETED'));
    $migration = Migration::getInstance('WineRole');
    $result = $migration->processImport();
    $this->assertEqual($result, Migration::RESULT_COMPLETED,
      t('Role import returned RESULT_COMPLETED'));
    $migration = Migration::getInstance('WineUser');
    $result = $migration->processImport();
    $this->assertEqual($result, Migration::RESULT_COMPLETED,
      t('User import returned RESULT_COMPLETED'));
    $migration = Migration::getInstance('WineProducer');
    $result = $migration->processImport();
    $this->assertEqual($result, Migration::RESULT_COMPLETED,
      t('Producer node import returned RESULT_COMPLETED'));
    $migration = Migration::getInstance('WineWine');
    $result = $migration->processImport();
    $this->assertEqual($result, Migration::RESULT_COMPLETED,
      t('Wine node import returned RESULT_COMPLETED'));
    $migration = Migration::getInstance('WineComment');
    $result = $migration->processImport();
    $this->assertEqual($result, Migration::RESULT_COMPLETED,
      t('Comment import returned RESULT_COMPLETED'));
    $result = db_select('migrate_message_winecomment', 'w')
      ->fields('w', array('message'))
      ->execute();
    foreach ($result as $row) {
      $this->error($row->message);
    }

    $result = db_select('migrate_example_wine_comment', 'wc')
      ->fields('wc', array(
        'commentid',
        'comment_parent',
        'name',
        'mail',
        'accountid',
        'body',
        'wineid',
        'subject',
        'commenthost',
        'userpage',
        'posted',
        'lastchanged',
      ))
      ->orderBy('comment_parent')
      ->execute();

    $rawcomments = comment_load_multiple(FALSE);
    // Index by subject
    $comments = array();
    foreach ($rawcomments as $comment) {
      $comments[$comment->subject] = $comment;
    }
    $rows = array();
    foreach ($result as $row) {
      $rows[$row->subject] = $row;
    }
    if (!$this->assertEqual(count($comments), count($rows), t('Counts of comments and input rows match'))) {
      $this->error(t('!comments comments, should be !rows',
        array('!comments' => count($comments), '!rows' => count($rows))));
    }

    $comment = $comments['im second'];
    $row = $rows['im second'];
    $this->assertEqual($comment->mail, $row->mail, t('Mail matches'));
    $this->assertEqual($comment->name, $row->name, t('Name matches'));
    $this->assertEqual($comment->status, COMMENT_PUBLISHED, t('Status matches'));
    $wine_migration = MigrationBase::getInstance('WineWine');
    $destid = $wine_migration->getMap()
      ->lookupDestinationID(array($row->wineid));
    $this->assertEqual($comment->nid, reset($destid), t('Nid matches'));
    $body = field_get_items('comment', $comment, 'comment_body');
    $this->assertEqual($body[0]['value'], $row->body, t('Body matches'));
    $this->assertEqual($comment->hostname, $row->commenthost, t('Hostname matches'));
    $this->assertEqual($comment->homepage, $row->userpage, t('Homepage matches'));
    $this->assertEqual($comment->created, $row->posted, t('Created matches'));
    $this->assertEqual($comment->changed, $row->lastchanged, t('Changed matches'));

    $comment = $comments['im child'];
    $row = $rows['im child'];
    $user_migration = MigrationBase::getInstance('WineUser');
    $destid = $user_migration->getMap()
      ->lookupDestinationID(array($row->accountid));
    $this->assertEqual($comment->uid, reset($destid), t('Uid matches'));
    $this->assertEqual($comment->pid, $comments['im parent']->cid, t('Parent matches'));

    // Test updates
    // Capture original comments
    $original_comments = comment_load_multiple(FALSE);
    $update_migration = Migration::getInstance('WineCommentUpdates');
    $result = $update_migration->processImport();
    $this->assertEqual($result, Migration::RESULT_COMPLETED,
      t('Wine comment updates import returned RESULT_COMPLETED'));
    $final_comments = comment_load_multiple(FALSE);
    foreach ($original_comments as $cid => $original_comment) {
      foreach ($original_comment as $field => $value) {
        if ($field == 'subject') {
          if ($value == $final_comments[$cid]->{$field}) {
            $this->error(t('Field !field should have changed but did not, value=!value',
              array('!field' => $field, '!value' => print_r($value, TRUE))));
          }
        }
        else {
          if ($value != $final_comments[$cid]->{$field}) {
            $this->error(t('Field !field mismatch: original !value1, result !value2',
              array(
                '!field' => $field,
                '!value1' => print_r($value, TRUE),
                '!value2' => print_r($final_comments[$cid]->{$field}, TRUE),
              )));
          }
        }
      }
    }

    // Test rollback
    $result = $migration->processRollback(array('force' => TRUE));
    $this->assertEqual($result, Migration::RESULT_COMPLETED,
      t('Comment rollback returned RESULT_COMPLETED'));
    $rawcomments = comment_load_multiple(FALSE);
    $this->assertEqual(count($rawcomments), 0, t('All comments deleted'));
    $count = db_select('migrate_map_winecomment', 'map')
      ->fields('map', array('sourceid1'))
      ->countQuery()
      ->execute()
      ->fetchField();
    $this->assertEqual($count, 0, t('Map cleared'));
    $count = db_select('migrate_message_winecomment', 'msg')
      ->fields('msg', array('sourceid1'))
      ->countQuery()
      ->execute()
      ->fetchField();
    $this->assertEqual($count, 0, t('Messages cleared'));
  }
}
