Skip to content
Snippets Groups Projects
Commit 4ea2796c authored by Prem Suthar's avatar Prem Suthar Committed by Andrei Ivnitskii
Browse files

Issue #3124495: Add a test to make sure...

Issue #3124495: Add a test to make sure FlaggingStorage::loadIsFlaggedMultiple() actually supports multiple entities
parent a8d09e3c
No related branches found
No related tags found
1 merge request!86Addedd the MR Form the Patch #11 as per the suggestion.
Pipeline #316891 passed with warnings
......@@ -97,7 +97,7 @@ class FlaggingStorage extends SqlContentEntityStorage implements FlaggingStorage
$this->globalFlagIdsByEntity[$entity_type_id] += $ids_to_load;
$flag_ids_by_entity += $ids_to_load;
// Directly query the table to avoid he overhead of loading the content
// Directly query the table to avoid the overhead of loading the content
// entities.
$query = $this->database->select('flagging', 'f')
->fields('f', ['entity_id', 'flag_id', 'global'])
......
......@@ -36,12 +36,20 @@ class FlaggingStorageTest extends FlagKernelTestBase {
*/
protected $node;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->entityTypeManager = \Drupal::entityTypeManager();
$this->account = $this->createUser();
$this->flag = $this->createFlag('node', ['article']);
......@@ -64,4 +72,49 @@ class FlaggingStorageTest extends FlagKernelTestBase {
$this->assertFalse($this->flag->isFlagged($this->node, $this->account));
}
/**
* Test that we can find flaggings based on a single entity.
*/
public function testLoadIsFlagged() {
// Flag the node on behalf of the user.
$this->flagService->flag($this->flag, $this->node, $this->account);
// Retrieve the flaggingStorage from the entity type manager.
/** @var \Drupal\flag\Entity\Storage\FlaggingStorageInterface $flaggingStorage */
$flaggingStorage = $this->entityTypeManager->getStorage('flagging');
$flaggings = $flaggingStorage->loadIsFlagged($this->node, $this->account);
$this->assertCount(1, $flaggings, 'Node should be flagged with one flag.');
$this->assertContains($this->flag->id(), $flaggings, 'Node should be flagged with the test flag.');
}
/**
* Test that we can find flaggings based on multiple entities.
*/
public function testLoadIsFlaggedMultiple() {
// Flag the node on behalf of the user.
$this->flagService->flag($this->flag, $this->node, $this->account);
// Create another node, flag it too.
$anotherNode = $this->createNode(['type' => 'article']);
$this->flagService->flag($this->flag, $anotherNode, $this->account);
$nodes = [$this->node, $anotherNode];
// Retrieve the flaggingStorage from the entity type manager.
/** @var \Drupal\flag\Entity\Storage\FlaggingStorageInterface $flaggingStorage */
$flaggingStorage = $this->entityTypeManager->getStorage('flagging');
$flaggings = $flaggingStorage->loadIsFlaggedMultiple($nodes, $this->account);
$this->assertCount(2, $flaggings, 'Result should contain flaggings for two nodes.');
foreach ($nodes as $node) {
$this->assertArrayHasKey($node->id(), $flaggings, 'Result should have flagging for node ' . $node->id() . '.');
$this->assertCount(1, $flaggings[$node->id()], 'Node ' . $node->id() . ' should be flagged with one flag.');
$this->assertContains($this->flag->id(), $flaggings[$node->id()], 'Node ' . $node->id() . ' should be flagged with the test flag.');
}
}
}
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