Issue #2292115 by slashrsm: Fixed Comment statistics is no correctly loaded into entity object if more than 1 comments field.
parent
cf4cc20b1c
commit
8b72eb1e65
|
@ -69,12 +69,17 @@ class CommentStatistics implements CommentStatisticsInterface {
|
|||
* {@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 @@ interface CommentStatisticsInterface {
|
|||
* 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 @@ class CommentStatisticsUnitTest extends UnitTestCase {
|
|||
->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 @@ class CommentStatisticsUnitTest extends UnitTestCase {
|
|||
* @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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue