MassContactTest.php in Mass Contact 8
File
tests/src/Kernel/MassContactTest.php
View source
<?php
namespace Drupal\Tests\mass_contact\Kernel;
use Drupal\mass_contact\Entity\MassContactMessage;
use Drupal\simpletest\UserCreationTrait;
class MassContactTest extends MassContactTestBase {
use CategoryCreationTrait;
use UserCreationTrait;
protected $massContact;
public static $modules = [
'field',
'options',
'text',
];
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('user');
$this
->installConfig([
'mass_contact',
]);
$this
->installSchema('system', [
'sequences',
]);
$this->massContact = $this->container
->get('mass_contact');
}
public function testQueueRecipients() {
$category = $this
->createCategory();
$role = $this
->createRole([]);
$category
->setRecipients([
'role' => [
'categories' => [
$role,
],
'conjunction' => 'AND',
],
]);
$category
->save();
$message = MassContactMessage::create([
'categories' => [
$category
->id(),
],
]);
$this->massContact
->queueRecipients($message);
$queue = \Drupal::queue('mass_contact_send_message');
$this
->assertEquals(0, $queue
->numberOfItems());
$accounts = [];
foreach (range(1, 5) as $i) {
$accounts[$i] = $this
->createUser();
$accounts[$i]
->addRole($role);
$accounts[$i]
->save();
}
$this->massContact
->queueRecipients($message);
$queue = \Drupal::queue('mass_contact_send_message');
$this
->assertEquals(1, $queue
->numberOfItems());
$item = $queue
->claimItem();
$this
->assertEquals(5, count($item->data['recipients']));
$this
->assertEquals(array_keys($accounts), $item->data['recipients']);
$queue
->deleteItem($item);
$copy_user = $this
->createUser();
$this->massContact
->queueRecipients($message, [
'send_me_copy_user' => $copy_user
->id(),
]);
$queue = \Drupal::queue('mass_contact_send_message');
$this
->assertEquals(1, $queue
->numberOfItems());
$item = $queue
->claimItem();
$this
->assertEquals(6, count($item->data['recipients']));
$this
->assertEquals($copy_user
->id(), array_shift($item->data['recipients']));
$this
->assertEquals(array_keys($accounts), $item->data['recipients']);
$queue
->deleteItem($item);
}
}