You are here

public function RecipientTypePluginsTest::testDelivery in Workbench Email 8

Same name and namespace in other branches
  1. 2.x tests/src/Kernel/RecipientTypePluginsTest.php \Drupal\Tests\workbench_email\Kernel\RecipientTypePluginsTest::testDelivery()

Tests recipient delivery.

@dataProvider providerDelivery

Parameters

string $pluginId: Recipient plugin ID.

array $expectedRecipients: Expected recipients.

array $settings: Plugin settings.

array $expectedDependencies: Expected configuration dependencies.

Throws

\Drupal\Core\Entity\EntityStorageException

File

tests/src/Kernel/RecipientTypePluginsTest.php, line 183

Class

RecipientTypePluginsTest
Defines a class for testing handlers.

Namespace

Drupal\Tests\workbench_email\Kernel

Code

public function testDelivery($pluginId, array $expectedRecipients, array $settings = [], array $expectedDependencies = []) {
  $template = Template::create([
    'id' => 'test',
    'label' => 'Test',
    'body' => [
      'value' => 'Content with title [node:title] needs review.',
      'format' => 'plain_text',
    ],
    'subject' => 'Content needs review: [node:title]',
    'recipient_types' => [
      $pluginId => [
        'id' => $pluginId,
        'provider' => 'workbench_email',
        'status' => 1,
        'settings' => $settings,
      ],
    ],
  ]);
  $template
    ->save();
  if ($expectedDependencies) {
    $dependencies = $template
      ->calculateDependencies()
      ->getDependencies()['config'];
    $this
      ->assertEquals($expectedDependencies, $dependencies);
  }
  $transition = ModerationStateTransition::load('draft_needs_review');
  $transition
    ->setThirdPartySetting('workbench_email', 'workbench_email_templates', [
    'test' => 'test',
  ]);
  $transition
    ->save();
  $this
    ->assertContains('workbench_email.workbench_email_template.test', $transition
    ->calculateDependencies()
    ->getDependencies()['config']);
  $node = Node::create([
    'title' => 'test',
    'uid' => $this->author
      ->id(),
    'type' => 'test',
    'field_email' => 'random@example.com',
    'field_approver' => $this->approver
      ->id(),
    'revision_uid' => $this->lastAuthor
      ->id(),
    'moderation_state' => 'draft',
  ]);
  $node
    ->save();
  $node
    ->setNewRevision();
  $node->revision_uid = $this->lastAuthor
    ->id();
  $node
    ->save();

  // Reset email.
  $this->container
    ->get('state')
    ->set('system.test_mail_collector', []);

  // Send for review.
  $node->moderation_state = 'needs_review';
  $node->revision_uid = $this->lastAuthor
    ->id();
  $node
    ->setNewRevision();
  $node
    ->save();

  // Check mail goes to recipients.
  $captured_emails = $this->container
    ->get('state')
    ->get('system.test_mail_collector') ?: [];
  $this
    ->assertEqualsCanonicalizing($expectedRecipients, array_map(function (array $mail) {
    return $mail['to'];
  }, $captured_emails));
  foreach ($captured_emails as $email) {
    $this
      ->assertEquals(sprintf('Content needs review: %s', $node
      ->getTitle()), $email['subject']);
    $this
      ->assertStringContainsString(sprintf('Content with title %s needs review.', $node
      ->label()), preg_replace('/\\s+/', ' ', $email['body']));
  }
}