View source
<?php
require_once dirname(__FILE__) . '/../mimemail.inc';
class MimeMailUnitTestCase extends DrupalUnitTestCase {
public static function getInfo() {
return array(
'name' => 'Mime Mail unit tests',
'description' => 'Test that Mime Mail helper functions work properly.',
'group' => 'Mime Mail',
);
}
protected function setUp() {
drupal_load('module', 'mimemail');
parent::setUp();
}
public function testHeaders() {
$chars = array(
'-',
'.',
'+',
'_',
);
$name = $this
->randomString();
$local = $this
->randomName() . $chars[array_rand($chars)] . $this
->randomName();
$domain = $this
->randomName() . '-' . $this
->randomName() . '.' . $this
->randomName(rand(2, 4));
$headers = mimemail_headers(array(), "{$name} <{$local}@{$domain}>");
$result = $headers['Return-Path'];
$expected = "<{$local}@{$domain}>";
$this
->assertIdentical($result, $expected, 'Return-Path header field correctly set.');
}
public function testUrl() {
$result = _mimemail_url('#');
$this
->assertIdentical($result, '#', 'Hash mark URL without fragment left intact.');
$url = '/sites/default/files/styles/thumbnail/public/image.jpg?itok=Wrl6Qi9U';
$result = _mimemail_url($url, TRUE);
$expected = 'sites/default/files/styles/thumbnail/public/image.jpg';
$this
->assertIdentical($result, $expected, 'Security token removed from styled image URL.');
$expected = $url = 'public://' . $this
->randomName() . ' ' . $this
->randomName() . '.' . $this
->randomName(3);
$result = _mimemail_url($url, TRUE);
$this
->assertIdentical($result, $expected, 'Space in the filename of the attachment left intact.');
}
}
class MimeMailWebTestCase extends DrupalWebTestCase {
protected $adminUser;
public static function getInfo() {
return array(
'name' => 'Mime Mail web tests',
'description' => 'Test that Mime Mail works properly.',
'group' => 'Mime Mail',
);
}
protected function setUp() {
parent::setUp('mailsystem', 'mimemail');
$permissions = array(
'access administration pages',
'administer site configuration',
);
$this
->checkPermissions($permissions, TRUE);
$this->adminUser = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($this->adminUser);
}
public function testMailPreparationPlaintextBodyConversion() {
$message = array(
'module' => 'mimemail_test',
'key' => 'mimemail_test',
'to' => 'test@example.org',
'from' => 'test@example.org',
'subject' => 'Testing body to plain conversion',
'body' => 'Link: "<a href="http://example.org">example.org</a>" <b>Important stuff</b>, <h1>Title Heading</h1>, <ul><li>List Item One</li><li>List Item Two</li></ul>',
'params' => array(
'plain' => TRUE,
),
'headers' => array(),
);
$prepared_message = mimemail_prepare_message($message);
$expected_plaintext = <<<EOF
Link: "example.org [1]" *Important stuff*,
======== TITLE HEADING =======================================================
,
* List Item One
* List Item Two
[1] http://example.org
EOF;
$this
->assertIdentical($prepared_message['body'], $expected_plaintext, 'Body to plaintext conversion without dedicated plaintext works properly');
$message = array(
'module' => 'mimemail_test',
'key' => 'mimemail_test',
'to' => 'test@example.org',
'from' => 'test@example.org',
'subject' => 'Testing body to plain conversion',
'body' => 'Link: "<a href="http://example.org">example.org</a>" <b>Important stuff</b>, <h1>Title Heading</h1>, <ul><li>List Item One</li><li>List Item Two</li></ul>',
'params' => array(
'plain' => TRUE,
'plaintext' => 'This is the actual plaintext',
),
'headers' => array(),
);
$prepared_message = mimemail_prepare_message($message);
$this
->assertIdentical($prepared_message['body'], $message['params']['plaintext'], 'Dedicated plaintext is preferred.');
}
public function testUrl() {
$this
->drupalPost('admin/config/system/mimemail', array(
'mimemail_linkonly' => TRUE,
), t('Save configuration'));
$url = 'public://' . $this
->randomName() . ' ' . $this
->randomName() . '.jpg';
$result = _mimemail_url($url, TRUE);
$expected = str_replace(' ', '%20', file_create_url($url));
$message = 'Stream wrapper converted to web accessible URL for linked image.';
$this
->assertIdentical($result, $expected, $message);
}
}