diff --git a/core/modules/comment/src/CommentStatistics.php b/core/modules/comment/src/CommentStatistics.php index fbbdff42463348896adc60d625a67642a736e1e0..394d425f4829851427af04868167509c7565c8b4 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 dc34e9606f7b285a2557e0471ed948ae3cb2cb91..92a0b7af45eac9d49e6ced5f1e99fea2f3ee1ae3 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 3f994df503b122c0e31dcfe119bbcab0be80d246..49933a77955dd6d309de993fa04ff3d4e3c5c155 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; + } + } }