From 840e8dfc4e1f6fee61c47d1527f74d5356e35989 Mon Sep 17 00:00:00 2001
From: Marco Villegas <git@marvil07.net>
Date: Fri, 11 Oct 2013 02:24:16 -0500
Subject: [PATCH] Starting d6 comment source plugin and its test.

---
 .../migrate/Plugin/migrate/source/d6/Comment.php   |   31 ++++++++
 lib/Drupal/migrate/Tests/D6CommentSourceTest.php   |   80 ++++++++++++++++++++
 2 files changed, 111 insertions(+)
 create mode 100644 lib/Drupal/migrate/Plugin/migrate/source/d6/Comment.php
 create mode 100644 lib/Drupal/migrate/Tests/D6CommentSourceTest.php

diff --git a/lib/Drupal/migrate/Plugin/migrate/source/d6/Comment.php b/lib/Drupal/migrate/Plugin/migrate/source/d6/Comment.php
new file mode 100644
index 0000000..2b7691b
--- /dev/null
+++ b/lib/Drupal/migrate/Plugin/migrate/source/d6/Comment.php
@@ -0,0 +1,31 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\migrate\Plugin\migrate\source\d6\Comment.
+ */
+
+namespace Drupal\migrate\Plugin\migrate\source\d6;
+
+use Drupal\migrate\Plugin\migrate\source\SqlBase;
+
+/**
+ * Drupal 6 comment source from database.
+ *
+ * @fixme Make this work.
+ *
+ * @PluginId("drupal6_comment")
+ */
+class Comment extends SqlBase {
+  function query() {
+    $query = $this->database
+      ->select('comments', 'c')
+      ->fields('c', array('cid', 'pid', 'nid', 'uid', 'subject',
+        'comment', 'hostname', 'timestamp', 'status', 'thread', 'name',
+        'mail', 'homepage', 'format'));
+    $query->join('node', 'n', 'c.nid = n.nid');
+    // @todo Pass node type on constructor to filter?.
+    $query->orderBy('timestamp');
+    return $query;
+  }
+}
diff --git a/lib/Drupal/migrate/Tests/D6CommentSourceTest.php b/lib/Drupal/migrate/Tests/D6CommentSourceTest.php
new file mode 100644
index 0000000..e611c72
--- /dev/null
+++ b/lib/Drupal/migrate/Tests/D6CommentSourceTest.php
@@ -0,0 +1,80 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\migrate\Tests\D6CommentSourceTest.
+ */
+
+namespace Drupal\migrate\Tests;
+
+use Drupal\Core\Cache\CacheBackendInterface;
+use Drupal\Core\Database\Connection;
+use Drupal\Tests\UnitTestCase;
+use Drupal\migrate\Plugin\migrate\source\d6\Comment;
+
+/**
+ * Tests comment migration from D6 to D8.
+ */
+class D6CommentSourceTest extends UnitTestCase {
+  /**
+   * The tested source plugin.
+   *
+   * @var \Drupal\migrate\Plugin\migrate\source\d6\Comment.
+   */
+  protected $source;
+
+  /**
+   * Database connection to use.
+   *
+   * @var \Drupal\Core\Database\Connection
+   */
+  protected $connection;
+
+  public static function getInfo() {
+    return array(
+      'name' => 'D6 comment source functionality',
+      'description' => 'Tests D6 comment source plugin.',
+      'group' => 'Migrate',
+    );
+  }
+
+  protected function setUp() {
+    $this->connection = $this->getMockBuilder('Drupal\Core\Database\Connection')
+      ->disableOriginalConstructor()
+      ->getMock();
+
+    // @todo Figure out how Comment::query() is used.
+    // @todo create a StatementInterface object with relevant data attached? (or mocks)
+    $statement = null;
+    $this->connection->expects($this->any())
+      ->method('query')
+      ->will($this->returnValue($statement));
+
+    $configuration = array();
+    $plugin_definition = array();
+    // @todo Instanciate a CacheBackendInterface object;
+    $cache = null;
+    $this->source = new Comment($configuration, 'drupal6_comment', $plugin_definition, $cache, $this->connection);
+  }
+
+  /**
+   * Tests retrieval.
+   */
+  public function testRetrieval() {
+    $source->rewind();
+    // @todo mock two rows.
+    $expected_data_keys = array('cid', 'pid', 'nid', 'uid', 'subject', 'comment', 'hostname', 'timestamp', 'status', 'thread', 'name', 'mail', 'homepage', 'format');
+    // First row.
+    $this->assertTrue($this->source->valid(), 'Valid row found in source.');
+    $data_row = $source->current();
+    foreach ($expected_data_keys as $expected_data_key) {
+      $this->assertTrue(isset($data_row[$expected_data_key]), sprintf('Found key "%s" on source data row.', $expected_data_key));
+    }
+    // Second row.
+    $data_row = $source->current();
+    $source->next();
+    foreach ($expected_data_keys as $expected_data_key) {
+      $this->assertTrue(isset($data_row[$expected_data_key]), sprintf('Found key "%s" on source data row.', $expected_data_key));
+    }
+  }
+}
-- 
1.7.10.4

