From ac7663fa2824cb4e5058753bc77207e17c79a71b Mon Sep 17 00:00:00 2001
From: Kyle Browning <kylebrowning@me.com>
Date: Tue, 29 Mar 2011 12:22:31 -0700
Subject: [PATCH] Issue #1058934 heyrocker, voxpelli, kylebrowning Convert comment/nodeComments action to node/x/comments relationship

---
 resources/comment_resource.inc                     |   55 ---------------
 resources/node_resource.inc                        |   72 +++++++++++++++++++-
 tests/functional/ServicesResourceCommentTests.test |   60 ----------------
 tests/functional/ServicesResourceNodeTests.test    |   42 +++++++++++-
 tests/services.test                                |   30 +++++++-
 5 files changed, 138 insertions(+), 121 deletions(-)

diff --git a/resources/comment_resource.inc b/resources/comment_resource.inc
index 94ce4ad..ecf7627 100644
--- a/resources/comment_resource.inc
+++ b/resources/comment_resource.inc
@@ -86,39 +86,6 @@ function _comment_resource_definition() {
         ),
       ),
       'actions' => array(
-        'loadNodeComments' => array(
-          'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/comment_resource'),
-          'help'   => t('This method returns the number of new comments on a given node since a given timestamp.'),
-          'access callback' => '_comment_resource_access',
-          'access arguments' => array('view'),
-          'access arguments append' => TRUE,
-          'callback' => '_comment_resource_load_node_comments',
-          'args'     => array(
-            array(
-              'name'         => 'nid',
-              'type'         => 'int',
-              'description'  => t('The node id to load comments for.'),
-              'source'       => 'data',
-              'optional'     => FALSE,
-            ),
-            array(
-              'name'         => 'count',
-              'type'         => 'int',
-              'description'  => t('Number of comments to load.'),
-              'source'       => 'data',
-              'optional'     => TRUE,
-              'default value'=> 10,
-            ),
-            array(
-              'name'         => 'start',
-              'type'         => 'int',
-              'description'  => t('If count is set to non-zero value, you can pass also non-zero value for start. For example to get comments from 5 to 15, pass count=10 and start=5.'),
-              'source'       => 'data',
-              'optional'     => TRUE,
-              'default value'=> 0,
-            ),
-          ),
-        ),
         'countAll' => array(
           'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/comment_resource'),
           'help'   => t('This method returns the number of comments on a given node.'),
@@ -276,28 +243,6 @@ function _comment_resource_delete($cid) {
 }
 
 /**
- * Returns the comments of a specified node.
- *
- * @param $nid
- *   Unique identifier for the node.
- * @param $count
- *   Number of comments to return.
- * @param $start
- *   Which comment to start with. if present, $start and $count are used together
- *   to create a LIMIT clause for selecting comments. This could be used to do paging.
- * @return
- *   An array of comment objects.
- */
-function _comment_resource_load_node_comments($nid, $count, $start) {
-  $comments = array();
-  $result = db_query_range('SELECT c.cid FROM {comment} c WHERE c.nid = :nid', $start, $count, array(':nid' => $nid));
-  foreach ($result as $record) {
-    $comments[] = comment_load($record->cid);
-  }
-  return $comments;
-}
-
-/**
  * Returns the number of comments on a given node id.
  *
  * @param $nid
diff --git a/resources/node_resource.inc b/resources/node_resource.inc
index 64ab64b..d6da8cc 100644
--- a/resources/node_resource.inc
+++ b/resources/node_resource.inc
@@ -1,7 +1,7 @@
 <?php
 
 function _node_resource_definition() {
-  return array(
+  $node_resource = array(
     'node' => array(
       'retrieve' => array(
         'file' => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/node_resource'),
@@ -133,6 +133,41 @@ function _node_resource_definition() {
       ),
     ),
   );
+  if (module_exists('comment')) {
+    $comments = array(
+      'file'                    => array('type' => 'inc', 'module' => 'services', 'name' => 'resources/node_resource'),
+      'help'                    => t('This method returns the number of new comments on a given node.'),
+      'access callback'         => 'user_access',
+      'access arguments'        => array('access comments'),
+      'access arguments append' => FALSE,
+      'callback'                => '_node_resource_load_node_comments',
+      'args'                    => array(
+        array(
+          'name'         => 'nid',
+          'type'         => 'int',
+          'description'  => t('The node id to load comments for.'),
+          'source'       => array('path' => 0),
+          'optional'     => FALSE,
+        ),
+        array(
+          'name'         => 'count',
+          'type'         => 'int',
+          'description'  => t('Number of comments to load.'),
+          'source'       => array('param' => 'count'),
+          'optional'     => TRUE,
+        ),
+        array(
+          'name'         => 'offset',
+          'type'         => 'int',
+          'description'  => t('If count is set to non-zero value, you can pass also non-zero value for start. For example to get comments from 5 to 15, pass count=10 and start=5.'),
+          'source'       => array('param' => 'offset'),
+          'optional'     => TRUE,
+        ),
+      ),
+    );
+    $node_resource['node']['relationships']['comments'] =  $comments;
+  }
+  return $node_resource;
 }
 
 /**
@@ -424,3 +459,38 @@ function _node_resource_load_node_files($nid, $include_file_contents = TRUE) {
   return $files;
 }
 
+/**
+ * Returns the comments of a specified node.
+ *
+ * @param $nid
+ *   Unique identifier for the node.
+ * @param $count
+ *   Number of comments to return.
+ * @param $start
+ *   Which comment to start with. If present, $start and $count are used together
+ *   to create a LIMIT clause for selecting comments. This could be used to do paging.
+ * @return
+ *   An array of comment objects.
+ */
+function _node_resource_load_node_comments($nid, $count = 0, $start = 0) {
+  $comments = array();
+
+  $query = db_select('comment', 'c');
+  $query->innerJoin('node', 'n', 'n.nid = c.nid');
+  $query->addTag('node_access');
+  $query->fields('c', array('cid'))
+    ->condition('c.nid', $nid);
+
+  if ($count) {
+    $query->range($start, $count);
+  }
+
+  $result = $query->execute()
+    ->fetchAll();
+
+  foreach ($result as $record) {
+    $cids[] = $record->cid;
+  }
+  $comments = comment_load_multiple($cids);
+  return $comments;
+}
diff --git a/tests/functional/ServicesResourceCommentTests.test b/tests/functional/ServicesResourceCommentTests.test
index 58a9fc6..e642126 100644
--- a/tests/functional/ServicesResourceCommentTests.test
+++ b/tests/functional/ServicesResourceCommentTests.test
@@ -170,43 +170,6 @@ class ServicesResourceCommentTests extends ServicesWebTestCase {
     $this->assertTrue(empty($comment_load), t('Comment deleted properly.'), 'CommentResource: Delete');
   }
 
-  /**
-   * Test loadNodeComments method.
-   */
-  function testCommentLoadNodeComments() {
-    $path = $this->endpoint->path;
-
-    // Create node with commenting.
-    $node = $this->drupalCreateNode();
-    $nid = $node->nid;
-
-    // Generate 15 comments for node.
-    $comments = array();
-    for ($i = 0; $i < 15; $i++) {
-      $comment = (object)$this->getCommentValues($nid);
-      comment_save($comment);
-      $comments[] = comment_load($comment->cid);
-    }
-
-    // Generate some comments for another node.
-    $node2 = $this->drupalCreateNode();
-    for ($i = 0; $i < 5; $i++) {
-      $comment = (object)$this->getCommentValues($node2->nid);
-      comment_save($comment);
-    }
-
-    // Load all comments of the first node.
-    $response = $this->servicesPost($path . '/comment/loadNodeComments', array('nid' => $nid));
-    $this->assertEqual(array_slice($comments, 0, 10), $response['body'], t('Received all 15 comments.'), 'CommentResource: loadNodeComments');
-
-    // Load only 5 comments of the first node.
-    $response = $this->servicesPost($path . '/comment/loadNodeComments', array('nid' => $nid, 'count' => 5));
-    $this->assertEqual(array_slice($comments, 0, 5), $response['body'], t('Received last 5 comments.'), 'CommentResource: loadNodeComments');
-
-    // Load only 5 comments of the first node starting from fifth comment.
-    $response = $this->servicesPost($path . '/comment/loadNodeComments', array('nid' => $nid, 'count' => 5, 'start' => 5));
-    $this->assertEqual(array_slice($comments, 5, 5), $response['body'], t('Received 5 comments starting from fifth comment.'), 'CommentResource: loadNodeComments');
-  }
 
   /**
    * Test countAll method.
@@ -248,27 +211,4 @@ class ServicesResourceCommentTests extends ServicesWebTestCase {
     $response = $this->servicesPost($path . '/comment/countNew', array('nid' => $node->nid, 'since' => $since));
     $this->assertEqual($response['body'], 2, t('Received number of new comments.'), 'CommentResource: countNew');
   }
-
-  /**
-   * Default values of comment for creating.
-   */
-  public function getCommentValues($nid) {
-    return array(
-      'subject' => $this->randomString(),
-      'comment_body' => array(
-        LANGUAGE_NONE => array(
-          array(
-            'value' => $this->randomString(),
-            'format' => filter_default_format(),
-          )
-        )
-      ),
-      'name' => $this->privileged_user->name,
-      'language' => LANGUAGE_NONE,
-      'nid' => $nid,
-      'uid' => $this->privileged_user->uid,
-      'cid' => NULL,
-      'pid' => 0,
-    );
-  }
 }
diff --git a/tests/functional/ServicesResourceNodeTests.test b/tests/functional/ServicesResourceNodeTests.test
index 73a40e1..4737b50 100644
--- a/tests/functional/ServicesResourceNodeTests.test
+++ b/tests/functional/ServicesResourceNodeTests.test
@@ -23,7 +23,8 @@ class ServicesResourceNodetests extends ServicesWebTestCase {
     parent::setUp(
       'ctools',
       'services',
-      'rest_server'
+      'rest_server',
+      'comment'
     );
     // Set up endpoint.
     $this->endpoint =  $this->saveNewEndpoint();
@@ -106,7 +107,44 @@ class ServicesResourceNodetests extends ServicesWebTestCase {
     $responseArray = $this->servicesGet($this->endpoint->path . '/node/99');
     $this->assertTrue($responseArray['code'] == '404', t('Successfully was rejected to non existent node'), 'NodeResource: Retrieve');
   }
+  /**
+   * Test loadNodeComments method.
+   */
+  function testCommentLoadNodeComments() {
+    $path = $this->endpoint->path;
+    $this->privileged_user = $this->drupalCreateUser();
+    $this->drupalLogin($this->privileged_user);
+    
+    // Create node with commenting.
+    $node = $this->drupalCreateNode();
+    $nid = $node->nid;
+
+    // Generate 15 comments for node.
+    $comments = array();
+    for ($i = 1; $i <= 15; $i++) {
+      $comment = (object)$this->getCommentValues($nid);
+      comment_save($comment);
+      $comments[$i] = comment_load($comment->cid);
+    }
 
+    // Generate some comments for another node.
+    $node2 = $this->drupalCreateNode();
+    for ($i = 1; $i <= 5; $i++) {
+      $comment = (object)$this->getCommentValues($node2->nid);
+      comment_save($comment);
+    }
+  
+    // Load all comments of the first node.
+    $response = $this->servicesGet($path . '/node/'. $nid .'/comments');
+    $this->assertEqual($comments, $response['body'], t('Received all 15 comments.'), 'NodeResource: comments');
+
+    // Load only 5 comments of the first node.
+    $response = $this->servicesGet($path . '/node/'. $nid .'/comments', array('count' => 5));
+    $this->assertEqual(array_slice($comments, 0, 5), array_slice($response['body'], 0, 5), t('Received last 5 comments.'), 'NodeResource: comments');
+    // Load only 5 comments of the first node starting from fifth comment.
+    $response = $this->servicesGet($path . '/node/'. $nid .'/comments', array('count' => 5, 'offset' => 5));
+    $this->assertEqual(array_slice($comments, 5, 5), array_merge(array(),$response['body']), t('Received 5 comments starting from fifth comment.'), 'NodeResource: comments');
+  }
   /**
    * testing node_resource Create
    */
@@ -274,4 +312,4 @@ class ServicesResourceNodetests extends ServicesWebTestCase {
 
     $this->assertFalse($responseArray['code'] == 200, t('Node was deleted. It shoudlnt have been because it doesnt exist'), 'NodeResource: Deleted');
   }
-}
+}
\ No newline at end of file
diff --git a/tests/services.test b/tests/services.test
index d62ff75..0730663 100644
--- a/tests/services.test
+++ b/tests/services.test
@@ -195,6 +195,11 @@ class ServicesWebTestCase extends DrupalWebTestCase {
             'enabled' => 1,
           ),
         ),
+        'relationships' => array(
+          'comments' => array(
+            'enabled' => 1,
+          ),
+        )
       ),
       'system' => array(
         'alias' => '',
@@ -299,9 +304,6 @@ class ServicesWebTestCase extends DrupalWebTestCase {
           ),
         ),
         'actions' => array(
-          'loadNodeComments' => array(
-            'enabled' => 1,
-          ),
           'countAll' => array(
             'enabled' => 1,
           ),
@@ -432,4 +434,26 @@ class ServicesWebTestCase extends DrupalWebTestCase {
     $this->assertTrue($this->drupalGetContent() !== FALSE, $message, t('Browser'));
     return $this->drupalGetContent();
   }
+    /**
+   * Default values of comment for creating.
+   */
+  public function getCommentValues($nid) {
+    return array(
+      'subject' => $this->randomString(),
+      'comment_body' => array(
+        LANGUAGE_NONE => array(
+          array(
+            'value' => $this->randomString(),
+            'format' => filter_default_format(),
+          )
+        )
+      ),
+      'name' => $this->privileged_user->name,
+      'language' => LANGUAGE_NONE,
+      'nid' => $nid,
+      'uid' => $this->privileged_user->uid,
+      'cid' => NULL,
+      'pid' => 0,
+    );
+  }
 }
-- 
1.7.3.4

