You are here

public function FlagServiceTest::testFlagServiceGetFlaggingUsers in Flag 8.4

Tests that getFlaggingUsers method returns the expected result.

File

tests/src/Kernel/FlagServiceTest.php, line 174

Class

FlagServiceTest
Tests the FlagService.

Namespace

Drupal\Tests\flag\Kernel

Code

public function testFlagServiceGetFlaggingUsers() {

  // The service methods don't check access, so our user can be anybody.
  $accounts = [
    $this
      ->createUser(),
    $this
      ->createUser(),
  ];

  // Create a flag.
  $flag = Flag::create([
    'id' => strtolower($this
      ->randomMachineName()),
    'label' => $this
      ->randomString(),
    'entity_type' => 'node',
    'bundles' => [
      'article',
    ],
    'flag_type' => 'entity:node',
    'link_type' => 'reload',
    'flagTypeConfig' => [],
    'linkTypeConfig' => [],
  ]);
  $flag
    ->save();

  // Flag the node.
  $flaggable_node = Node::create([
    'type' => 'article',
    'title' => $this
      ->randomMachineName(8),
  ]);
  $flaggable_node
    ->save();
  foreach ($accounts as $account) {
    $this->flagService
      ->flag($flag, $flaggable_node, $account);
  }
  $flagging_users = $this->flagService
    ->getFlaggingUsers($flaggable_node, $flag);
  $this
    ->assertTrue(is_array($flagging_users), "The method getFlaggingUsers() returns an array.");
  foreach ($accounts as $account) {
    foreach ($flagging_users as $flagging_user) {
      if ($flagging_user
        ->id() == $account
        ->id()) {
        break;
      }
    }
    $this
      ->assertTrue($flagging_user
      ->id() == $account
      ->id(), "The returned array has the flagged account included.");
  }
}