From 8b72eb1e657adde02f30f12384cf5697dcbc0a6e Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Wed, 9 Jul 2014 10:32:17 +0100
Subject: [PATCH] Issue #2292115 by slashrsm: Fixed Comment statistics is no
 correctly loaded into entity object if more than 1 comments field.

---
 .../modules/comment/src/CommentStatistics.php | 11 ++++--
 .../src/CommentStatisticsInterface.php        |  2 +-
 .../tests/src/CommentStatisticsUnitTest.php   | 35 +++++++++++++++++--
 3 files changed, 41 insertions(+), 7 deletions(-)

diff --git a/core/modules/comment/src/CommentStatistics.php b/core/modules/comment/src/CommentStatistics.php
index fbbdff424633..394d425f4829 100644
--- a/core/modules/comment/src/CommentStatistics.php
+++ b/core/modules/comment/src/CommentStatistics.php
@@ -69,12 +69,17 @@ public function __construct(Connection $database, AccountInterface $current_user
    * {@inheritdoc}
    */
   public function read($entities, $entity_type) {
-    return $this->database->select('comment_entity_statistics', 'ces')
+    $stats = $this->database->select('comment_entity_statistics', 'ces')
       ->fields('ces')
       ->condition('ces.entity_id', array_keys($entities))
       ->condition('ces.entity_type', $entity_type)
-      ->execute()
-      ->fetchAllAssoc('entity_id');
+      ->execute();
+
+    $statistics_records = array();
+    while ($entry = $stats->fetchObject()) {
+      $statistics_records[] = $entry;
+    }
+    return $statistics_records;
   }
 
   /**
diff --git a/core/modules/comment/src/CommentStatisticsInterface.php b/core/modules/comment/src/CommentStatisticsInterface.php
index dc34e9606f7b..92a0b7af45ea 100644
--- a/core/modules/comment/src/CommentStatisticsInterface.php
+++ b/core/modules/comment/src/CommentStatisticsInterface.php
@@ -34,7 +34,7 @@ public function getRankingInfo();
    *   The entity type of the passed entities.
    *
    * @return object[]
-   *   Array of statistics records keyed by entity id.
+   *   Array of statistics records.
    */
   public function read($entities, $entity_type);
 
diff --git a/core/modules/comment/tests/src/CommentStatisticsUnitTest.php b/core/modules/comment/tests/src/CommentStatisticsUnitTest.php
index 3f994df503b1..49933a77955d 100644
--- a/core/modules/comment/tests/src/CommentStatisticsUnitTest.php
+++ b/core/modules/comment/tests/src/CommentStatisticsUnitTest.php
@@ -45,6 +45,13 @@ class CommentStatisticsUnitTest extends UnitTestCase {
    */
   protected $commentStatistics;
 
+  /**
+   * Counts calls to fetchAssoc().
+   *
+   * @var int
+   */
+  protected $calls_to_fetch;
+
   public static function getInfo() {
     return array(
       'name' => 'Comment statistics test',
@@ -62,8 +69,8 @@ public function setUp() {
       ->getMock();
 
     $this->statement->expects($this->any())
-      ->method('fetchAllAssoc')
-      ->will($this->returnValue(array('1' => 'something', '2' => 'something-else')));
+      ->method('fetchObject')
+      ->will($this->returnCallback(array($this, 'fetchObjectCallback')));
 
     $this->select = $this->getMockBuilder('Drupal\Core\Database\Query\Select')
       ->disableOriginalConstructor()
@@ -101,8 +108,30 @@ public function setUp() {
    * @group Comment
    */
   public function testRead() {
+    $this->calls_to_fetch = 0;
     $results = $this->commentStatistics->read(array('1' => 'boo', '2' => 'foo'), 'snafoos');
-    $this->assertEquals($results, array('1' => 'something', '2' => 'something-else'));
+    $this->assertEquals($results, array('something', 'something-else'));
   }
 
+  /**
+   * Return value callback for fetchObject() function on mocked object.
+   *
+   * @return bool|string
+   *   'Something' on first, 'something-else' on second and FALSE for the
+   *   other calls to function.
+   */
+  public function fetchObjectCallback() {
+    $this->calls_to_fetch++;
+    switch ($this->calls_to_fetch) {
+      case 1:
+        return 'something';
+        break;
+      case 2:
+        return 'something-else';
+        break;
+      default:
+        return FALSE;
+        break;
+    }
+  }
 }
-- 
GitLab