a few more base modules
This commit is contained in:
+12
@@ -0,0 +1,12 @@
|
||||
name: 'Maillog test'
|
||||
description: 'Contains helper logic for the Maillog tests.'
|
||||
package: 'Mail'
|
||||
# core: 8.x
|
||||
type: module
|
||||
hidden: TRUE
|
||||
|
||||
# Information added by Drupal.org packaging script on 2016-08-05
|
||||
version: '8.x-1.x-dev'
|
||||
core: '8.x'
|
||||
project: 'maillog'
|
||||
datestamp: 1470431946
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
/**
|
||||
* @file
|
||||
* Primary hook implementations for the Maillog test helper module.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_mail().
|
||||
*/
|
||||
function maillog_test_mail($key, &$message, $params) {
|
||||
// A very rudimentary test.
|
||||
if ($key == 'maillog_basic_test') {
|
||||
$message['subject'] = t('Test email subject');
|
||||
$message['body'][] = t('Test email body.');
|
||||
}
|
||||
|
||||
// This has a very long subject line.
|
||||
elseif ($key == 'maillog_long_subject_test') {
|
||||
$message['subject'] = str_repeat(t('Test email subject') . ' ', 20);
|
||||
$message['body'][] = t('Test email body.');
|
||||
}
|
||||
|
||||
// This has an XSS test for the subject line.
|
||||
elseif ($key == 'maillog_subject_xss_test') {
|
||||
$message['subject'] = '<script type="text/javascript">alert("XSS test")</script>';
|
||||
$message['body'][] = t('Test email body.');
|
||||
}
|
||||
|
||||
// This has an XSS test for the body field.
|
||||
elseif ($key == 'maillog_body_xss_test') {
|
||||
$message['subject'] = t('Test email subject');
|
||||
$message['body'][] = '<script type="text/javascript">alert("XSS test")</script>';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\maillog\Kernel;
|
||||
|
||||
use Drupal\Component\Utility\Unicode;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
|
||||
/**
|
||||
* Tests the maillog plugin.
|
||||
*
|
||||
* @group maillog
|
||||
*/
|
||||
class MailTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('maillog', 'maillog_test', 'user', 'system', 'views');
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installSchema('maillog', 'maillog');
|
||||
$this->installConfig(['system', 'maillog']);
|
||||
// The system.site.mail setting goes into the From header of outgoing mails.
|
||||
$this->config('system.site')->set('mail', 'simpletest@example.com')->save();
|
||||
|
||||
// Use the maillog mail plugin.
|
||||
$GLOBALS['config']['system.mail']['interface']['default'] = 'maillog';
|
||||
|
||||
// Disables E-Mail Sending, only tracking.
|
||||
$this->config('maillog.settings')
|
||||
->set('send', FALSE)
|
||||
->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests logging mail with maillog module.
|
||||
*/
|
||||
public function testLogging() {
|
||||
$language = \Drupal::languageManager()->getCurrentLanguage();
|
||||
|
||||
// Send an email.
|
||||
$from_email = 'simpletest@example.com';
|
||||
$reply_email = 'someone_else@example.com';
|
||||
\Drupal::service('plugin.manager.mail')->mail('simpletest', 'from_test', 'from_test@example.com', $language, array(), $reply_email);
|
||||
|
||||
// Compare the maillog db entry with the sent mail.
|
||||
$logged_email = $this->getLatestMaillogEntry();
|
||||
$this->assertTrue(is_array($logged_email), 'Email is captured.');
|
||||
$this->assertEqual($from_email, $logged_email['header_from'], 'Email is sent correctly.');
|
||||
$this->assertEqual($reply_email, $logged_email['header_all']['Reply-to'], 'Message is sent with correct reply address.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the latest Maillog entry.
|
||||
*
|
||||
* @return array
|
||||
* Maillog entry
|
||||
*/
|
||||
protected function getLatestMaillogEntry() {
|
||||
$query = 'SELECT idmaillog, header_from, header_to, header_reply_to, header_all, subject, body FROM {maillog} ORDER BY idmaillog DESC';
|
||||
$result = \Drupal::database()->queryRange($query, 0, 1);
|
||||
|
||||
if ($maillog = $result->fetchAssoc()) {
|
||||
// Unserialize values.
|
||||
$maillog['header_all'] = unserialize($maillog['header_all']);
|
||||
}
|
||||
return $maillog;
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirm what happens with long subject lines.
|
||||
*/
|
||||
public function testLongSubject() {
|
||||
// Send an email.
|
||||
$from_email = 'from_test@example.com';
|
||||
$to_email = 'to_test@example.com';
|
||||
\Drupal::service('plugin.manager.mail')->mail('maillog_test', 'maillog_long_subject_test', $to_email, 'en', [], $from_email);
|
||||
|
||||
// Compare the maillog db entry with the sent mail.
|
||||
$logged_email = $this->getLatestMaillogEntry();
|
||||
$this->assertTrue(!empty($logged_email), 'The test email was captured.');
|
||||
|
||||
// The original subject line, as copied from maillog_test_mail().
|
||||
$subject = str_repeat(t('Test email subject@space', ['@space' => ' ']), 20);
|
||||
|
||||
// Duplicate the string trimming.
|
||||
$subject_trimmed = Unicode::substr($subject, 0, 255);
|
||||
self::assertEquals($subject_trimmed, $logged_email['subject'], 'Email subject was trimmed correctly.');
|
||||
self::assertNotEquals($subject, $logged_email['subject'], 'Email subject is not untrimmed.');
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user