Skip to content
Snippets Groups Projects
Commit 8b72eb1e authored by Alex Pott's avatar Alex Pott
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
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
...@@ -69,12 +69,17 @@ public function __construct(Connection $database, AccountInterface $current_user ...@@ -69,12 +69,17 @@ public function __construct(Connection $database, AccountInterface $current_user
* {@inheritdoc} * {@inheritdoc}
*/ */
public function read($entities, $entity_type) { public function read($entities, $entity_type) {
return $this->database->select('comment_entity_statistics', 'ces') $stats = $this->database->select('comment_entity_statistics', 'ces')
->fields('ces') ->fields('ces')
->condition('ces.entity_id', array_keys($entities)) ->condition('ces.entity_id', array_keys($entities))
->condition('ces.entity_type', $entity_type) ->condition('ces.entity_type', $entity_type)
->execute() ->execute();
->fetchAllAssoc('entity_id');
$statistics_records = array();
while ($entry = $stats->fetchObject()) {
$statistics_records[] = $entry;
}
return $statistics_records;
} }
/** /**
......
...@@ -34,7 +34,7 @@ public function getRankingInfo(); ...@@ -34,7 +34,7 @@ public function getRankingInfo();
* The entity type of the passed entities. * The entity type of the passed entities.
* *
* @return object[] * @return object[]
* Array of statistics records keyed by entity id. * Array of statistics records.
*/ */
public function read($entities, $entity_type); public function read($entities, $entity_type);
......
...@@ -45,6 +45,13 @@ class CommentStatisticsUnitTest extends UnitTestCase { ...@@ -45,6 +45,13 @@ class CommentStatisticsUnitTest extends UnitTestCase {
*/ */
protected $commentStatistics; protected $commentStatistics;
/**
* Counts calls to fetchAssoc().
*
* @var int
*/
protected $calls_to_fetch;
public static function getInfo() { public static function getInfo() {
return array( return array(
'name' => 'Comment statistics test', 'name' => 'Comment statistics test',
...@@ -62,8 +69,8 @@ public function setUp() { ...@@ -62,8 +69,8 @@ public function setUp() {
->getMock(); ->getMock();
$this->statement->expects($this->any()) $this->statement->expects($this->any())
->method('fetchAllAssoc') ->method('fetchObject')
->will($this->returnValue(array('1' => 'something', '2' => 'something-else'))); ->will($this->returnCallback(array($this, 'fetchObjectCallback')));
$this->select = $this->getMockBuilder('Drupal\Core\Database\Query\Select') $this->select = $this->getMockBuilder('Drupal\Core\Database\Query\Select')
->disableOriginalConstructor() ->disableOriginalConstructor()
...@@ -101,8 +108,30 @@ public function setUp() { ...@@ -101,8 +108,30 @@ public function setUp() {
* @group Comment * @group Comment
*/ */
public function testRead() { public function testRead() {
$this->calls_to_fetch = 0;
$results = $this->commentStatistics->read(array('1' => 'boo', '2' => 'foo'), 'snafoos'); $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;
}
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment