function EmailActionTest::testEmailAction

Same name in other branches
  1. 8.9.x core/tests/Drupal/KernelTests/Core/Action/EmailActionTest.php \Drupal\KernelTests\Core\Action\EmailActionTest::testEmailAction()
  2. 10 core/tests/Drupal/KernelTests/Core/Action/EmailActionTest.php \Drupal\KernelTests\Core\Action\EmailActionTest::testEmailAction()
  3. 11.x core/tests/Drupal/KernelTests/Core/Action/EmailActionTest.php \Drupal\KernelTests\Core\Action\EmailActionTest::testEmailAction()

Tests the email action plugin.

File

core/tests/Drupal/KernelTests/Core/Action/EmailActionTest.php, line 33

Class

EmailActionTest
Tests for the EmailAction plugin.

Namespace

Drupal\KernelTests\Core\Action

Code

public function testEmailAction() {
    $this->config('system.site')
        ->set('mail', 'test@example.com')
        ->save();
    
    /** @var \Drupal\Core\Action\ActionManager $plugin_manager */
    $plugin_manager = $this->container
        ->get('plugin.manager.action');
    $configuration = [
        'recipient' => 'test@example.com',
        'subject' => 'Test subject',
        'message' => 'Test message',
    ];
    $plugin_manager->createInstance('action_send_email_action', $configuration)
        ->execute();
    $mails = $this->getMails();
    $this->assertCount(1, $this->getMails());
    $this->assertEquals('test@example.com', $mails[0]['to']);
    $this->assertEquals('Test subject', $mails[0]['subject']);
    $this->assertEquals("Test message\n", $mails[0]['body']);
    // Ensure that the email sending is logged.
    $log = \Drupal::database()->select('watchdog', 'w')
        ->fields('w', [
        'message',
        'variables',
    ])
        ->orderBy('wid', 'DESC')
        ->range(0, 1)
        ->execute()
        ->fetch();
    $this->assertEquals('Sent email to %recipient', $log->message);
    $variables = unserialize($log->variables);
    $this->assertEquals('test@example.com', $variables['%recipient']);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.