Use the proper dbtng api to query for author or comment. added tests for this parts... 7.x-3.0-rc3
authorDaniel Wehner
Wed, 16 Nov 2011 18:23:49 +0000 (19:23 +0100)
committerDaniel Wehner
Wed, 16 Nov 2011 18:23:49 +0000 (19:23 +0100)
modules/comment/views_handler_argument_comment_user_uid.inc
modules/comment/views_handler_filter_comment_user_uid.inc
tests/comment/views_handler_argument_comment_user_uid.test [new file with mode: 0644]
tests/comment/views_handler_filter_comment_user_uid.test [new file with mode: 0644]
views.info

index 39a689d..1ba56e8 100644 (file)
@@ -40,15 +40,12 @@ class views_handler_argument_comment_user_uid extends views_handler_argument {
 
     $subselect = db_select('comment', 'c');
     $subselect->addField('c', 'cid');
-    // @TODO: Figure out why condition/placeholders doesn't work here.
-    $subselect->where("c.uid =  $this->argument");
+    $subselect->condition('c.uid', $this->argument);
     $subselect->where("c.nid = $nid_alias");
-    $subselect = $subselect->countQuery();
-    $subselect->preExecute();
 
     $condition = db_or()
       ->condition("$this->table_alias.uid", $this->argument, '=')
-      ->where("0 < (" . (string) $subselect . ")");
+      ->exists($subselect);
 
     $this->query->add_where(0, $condition);
   }
index 9002019..1dbf2f1 100644 (file)
@@ -8,16 +8,14 @@ class views_handler_filter_comment_user_uid extends views_handler_filter_user_na
   function query() {
     $this->ensure_my_table();
 
-    $subselect = db_select('comments', 'c');
+    $subselect = db_select('comment', 'c');
     $subselect->addField('c', 'cid');
-    $subselect->where("uid $this->operator $this->value");
+    $subselect->condition('c.uid', $this->value, $this->operator);
     $subselect->where("nid = $this->table_alias.nid");
-    $subselect = $subselect->countQuery();
 
     $condition = db_or()
       ->condition("$this->table_alias.uid", $this->value, $this->operator)
-      ->where("0 < (" . (string) $subselect . ")")
-      ->condition(0, $subselect, '<');
+      ->exists($subselect);
 
     $this->query->add_where(0, $condition);
   }
diff --git a/tests/comment/views_handler_argument_comment_user_uid.test b/tests/comment/views_handler_argument_comment_user_uid.test
new file mode 100644 (file)
index 0000000..8de9777
--- /dev/null
@@ -0,0 +1,101 @@
+<?php
+/**
+ * @file
+ * Test the argument_comment_user_uid handler.
+ */
+class viewsHandlerArgumentCommentUserUidTest extends ViewsSqlTest {
+  public static function getInfo() {
+    return array(
+      'name' => 'Tests handler argument_comment_user_uid',
+      'description' => 'Tests the user posted or commented argument handler',
+      'group' => 'Views Modules',
+    );
+  }
+
+  /**
+   * Post comment.
+   *
+   * @param $node
+   *   Node to post comment on.
+   * @param $comment
+   *   Comment to save
+   */
+  function postComment($node, $comment = array()) {
+    $comment += array(
+      'uid' => $this->loggedInUser->uid,
+      'nid' => $node->nid,
+      'cid' => '',
+      'pid' => '',
+    );
+    return comment_save((object) $comment);
+  }
+
+  function setUp() {
+    parent::setUp();
+
+    // Add two users, create a node with the user1 as author and another node with user2 as author.
+    // For the second node add a comment from user1.
+    $this->account = $this->drupalCreateUser();
+    $this->account2 = $this->drupalCreateUser();
+    $this->drupalLogin($this->account);
+    $this->node_user_posted = $this->drupalCreateNode();
+    $this->node_user_commented = $this->drupalCreateNode(array('uid' => $this->account2->uid));
+    $this->postComment($this->node_user_commented);
+  }
+
+  function testCommentUserUidTest() {
+    $view = $this->view_comment_user_uid();
+
+
+    $this->executeView($view, array($this->account->uid));
+    $resultset = array(
+      array(
+        'nid' => $this->node_user_posted->nid,
+      ),
+      array(
+        'nid' => $this->node_user_commented->nid,
+      ),
+    );
+    $this->column_map = array('nid' => 'nid');
+    debug($view->result);
+    $this->assertIdenticalResultset($view, $resultset, $this->column_map);
+  }
+
+  function view_comment_user_uid() {
+    $view = new view;
+    $view->name = 'test_comment_user_uid';
+    $view->description = '';
+    $view->tag = 'default';
+    $view->base_table = 'node';
+    $view->human_name = 'test_comment_user_uid';
+    $view->core = 7;
+    $view->api_version = '3.0';
+    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
+
+    /* Display: Master */
+    $handler = $view->new_display('default', 'Master', 'default');
+    $handler->display->display_options['access']['type'] = 'perm';
+    $handler->display->display_options['cache']['type'] = 'none';
+    $handler->display->display_options['query']['type'] = 'views_query';
+    $handler->display->display_options['query']['options']['query_comment'] = FALSE;
+    $handler->display->display_options['exposed_form']['type'] = 'basic';
+    $handler->display->display_options['pager']['type'] = 'full';
+    $handler->display->display_options['style_plugin'] = 'default';
+    $handler->display->display_options['row_plugin'] = 'node';
+    /* Field: Content: nid */
+    $handler->display->display_options['fields']['nid']['id'] = 'nid';
+    $handler->display->display_options['fields']['nid']['table'] = 'node';
+    $handler->display->display_options['fields']['nid']['field'] = 'nid';
+    /* Contextual filter: Content: User posted or commented */
+    $handler->display->display_options['arguments']['uid_touch']['id'] = 'uid_touch';
+    $handler->display->display_options['arguments']['uid_touch']['table'] = 'node';
+    $handler->display->display_options['arguments']['uid_touch']['field'] = 'uid_touch';
+    $handler->display->display_options['arguments']['uid_touch']['default_argument_type'] = 'fixed';
+    $handler->display->display_options['arguments']['uid_touch']['default_argument_skip_url'] = 0;
+    $handler->display->display_options['arguments']['uid_touch']['summary']['number_of_records'] = '0';
+    $handler->display->display_options['arguments']['uid_touch']['summary']['format'] = 'default_summary';
+    $handler->display->display_options['arguments']['uid_touch']['summary_options']['items_per_page'] = '25';
+
+    return $view;
+  }
+}
diff --git a/tests/comment/views_handler_filter_comment_user_uid.test b/tests/comment/views_handler_filter_comment_user_uid.test
new file mode 100644 (file)
index 0000000..6560e2b
--- /dev/null
@@ -0,0 +1,35 @@
+<?php
+/**
+ * @file
+ * Test the filter_comment_user_uid handler.
+ * The actual stuff is done in the parent class.
+ */
+class viewsHandlerFilterCommentUserUidTest extends viewsHandlerArgumentCommentUserUidTest {
+  public static function getInfo() {
+    return array(
+      'name' => 'Tests handler filter_comment_user_uid',
+      'description' => 'Tests the user posted or commented filter handler',
+      'group' => 'Views Modules',
+    );
+  }
+
+  /**
+   * Override the view from the argument test case to remove the argument and
+   * add filter with the uid as the value.
+   */
+  function view_comment_user_uid() {
+    $view = parent::view_comment_user_uid();
+    // Remove the argument.
+    $view->set_item('default', 'argument', 'uid_touch', NULL);
+
+    $options = array(
+      'id' => 'uid_touch',
+      'table' => 'node',
+      'field' => 'uid_touch',
+      'value' => array($this->loggedInUser->uid),
+    );
+    $view->add_item('default', 'filter', 'node', 'uid_touch', $options);
+
+    return $view;
+  }
+}
\ No newline at end of file
index fdfb7fd..f40e9a4 100644 (file)
@@ -278,6 +278,8 @@ files[] = tests/views_translatable.test
 files[] = tests/views_query.test
 files[] = tests/views_upgrade.test
 files[] = tests/views_test.views_default.inc
+files[] = tests/comment/views_handler_argument_comment_user_uid.test
+files[] = tests/comment/views_handler_filter_comment_user_uid.test
 files[] = tests/user/views_user_argument_default.test
 files[] = tests/user/views_user_argument_validate.test
 files[] = tests/user/views_user.test