Commit 8b72eb1e authored by alexpott's avatar alexpott
Browse files

Issue #2292115 by slashrsm: Fixed Comment statistics is no correctly loaded...

Issue #2292115 by slashrsm: Fixed Comment statistics is no correctly loaded into entity object if more than 1 comments field.
parent cf4cc20b
......@@ -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;
}
/**
......
......@@ -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);
......
......@@ -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;
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment