You are here

public function RoleTest::testGetRecipients in Mass Contact 8

Test gathering recipients.

@covers ::getRecipients

File

tests/src/Kernel/Plugin/MassContact/GroupingMethod/RoleTest.php, line 76

Class

RoleTest
Tests the role grouping method plugin.

Namespace

Drupal\Tests\mass_contact\Kernel\Plugin\MassContact\GroupingMethod

Code

public function testGetRecipients() {

  // Test authenticated role special behavior.
  $config = [
    'conjunction' => 'OR',
    'categories' => [
      RoleInterface::AUTHENTICATED_ID,
      $this->roles[3]
        ->id(),
    ],
  ];

  /** @var \Drupal\mass_contact\Plugin\MassContact\GroupingMethod\Role $instance */
  $instance = $this->groupingManager
    ->createInstance('role', $config);
  $this
    ->assertEquals(9, count($instance
    ->getRecipients($config['categories'])));

  // Switch conjunction.
  $config['conjunction'] = 'AND';

  /** @var \Drupal\mass_contact\Plugin\MassContact\GroupingMethod\Role $instance */
  $instance = $this->groupingManager
    ->createInstance('role', $config);
  $this
    ->assertEmpty($instance
    ->getRecipients($config['categories']));

  // Add role 3 to 2 users.
  foreach ([
    7,
    9,
  ] as $uid) {
    $this->recipients[$uid]
      ->addRole($this->roles[3]
      ->id());
    $this->recipients[$uid]
      ->save();
  }

  // Add role 1 to user 7.
  $this->recipients[7]
    ->addRole($this->roles[1]
    ->id());
  $this->recipients[7]
    ->save();

  /** @var \Drupal\mass_contact\Plugin\MassContact\GroupingMethod\Role $instance */
  $instance = $this->groupingManager
    ->createInstance('role', $config);
  $this
    ->assertEquals(2, count($instance
    ->getRecipients($config['categories'])));
  $config['categories'] = [
    $this->roles[3]
      ->id(),
    $this->roles[1]
      ->id(),
  ];

  /** @var \Drupal\mass_contact\Plugin\MassContact\GroupingMethod\Role $instance */
  $instance = $this->groupingManager
    ->createInstance('role', $config);
  $this
    ->assertEquals(1, count($instance
    ->getRecipients($config['categories'])));

  // Switch back to OR.
  $config['conjunction'] = 'OR';

  /** @var \Drupal\mass_contact\Plugin\MassContact\GroupingMethod\Role $instance */
  $instance = $this->groupingManager
    ->createInstance('role', $config);
  $this
    ->assertEquals(2, count($instance
    ->getRecipients($config['categories'])));
}