TYPO3 CMS  TYPO3_6-2
MailUtilityTest.php
Go to the documentation of this file.
1 <?php
3 
23 
27  protected $singletonInstances = array();
28 
29  public function setUp() {
31  }
32 
33  public function tearDown() {
35  parent::tearDown();
36  }
37 
39  // Tests concerning mail
41 
44  public function mailCallsHook() {
45  $this->doMailCallsHook();
46  }
47 
52  $this->doMailCallsHook('no-reply@localhost', 'TYPO3 Mailer');
53  }
54 
58  protected function doMailCallsHook($fromAddress = '', $fromName = '') {
59  // Backup configuration
60  $mailConfigurationBackup = $GLOBALS['TYPO3_CONF_VARS']['MAIL'];
61  $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'] = $fromAddress;
62  $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName'] = $fromName;
63  $to = 'john@example.com';
64  $subject = 'Good news everybody!';
65  $messageBody = 'The hooks works!';
66  $additionalHeaders = 'Reply-to: jane@example.com';
67  $additionalParameters = '-f postmaster@example.com';
68  $fakeThis = FALSE;
69  $additionalHeadersExpected = $additionalHeaders;
70  if ($fromAddress !== '' && $fromName !== '') {
71  $additionalHeadersExpected .= LF . sprintf('From: "%s" <%s>', $fromName, $fromAddress);
72  }
73  $mockMailer = $this->getMock('TYPO3\\CMS\\Core\\Mail\\MailerAdapterInterface', array('mail'));
74  $mockClassName = get_class($mockMailer);
75  \TYPO3\CMS\Core\Utility\GeneralUtility::addInstance($mockClassName, $mockMailer);
76  $mockMailer->expects($this->once())
77  ->method('mail')
78  ->with($to, $subject, $messageBody, $additionalHeadersExpected, $additionalParameters, $fakeThis)
79  ->will($this->returnValue(TRUE));
80  $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/utility/class.t3lib_utility_mail.php']['substituteMailDelivery'] = array($mockClassName);
81  \TYPO3\CMS\Core\Utility\MailUtility::mail($to, $subject, $messageBody, $additionalHeaders, $additionalParameters);
82  // Restore configuration
83  $GLOBALS['TYPO3_CONF_VARS']['MAIL'] = $mailConfigurationBackup;
84  }
85 
90  $this->assertEmpty(\TYPO3\CMS\Core\Utility\MailUtility::breakLinesForEmail(''));
91  }
92 
97  $newlineChar = LF;
98  $lineWidth = 76;
99  $str = 'This text is not longer than 76 chars and therefore will not be broken.';
100  $returnString = \TYPO3\CMS\Core\Utility\MailUtility::breakLinesForEmail($str, $newlineChar, $lineWidth);
101  $this->assertEquals(1, count(explode($newlineChar, $returnString)));
102  }
103 
108  $newlineChar = LF;
109  $lineWidth = 50;
110  $str = 'This text is longer than 50 chars and therefore will be broken.';
111  $returnString = \TYPO3\CMS\Core\Utility\MailUtility::breakLinesForEmail($str, $newlineChar, $lineWidth);
112  $this->assertEquals(2, count(explode($newlineChar, $returnString)));
113  }
114 
119  $newlineChar = LF;
120  $lineWidth = 10;
121  // first space after 20 chars (more than $lineWidth)
122  $str = 'abcdefghijklmnopqrst uvwxyz 123456';
123  $returnString = \TYPO3\CMS\Core\Utility\MailUtility::breakLinesForEmail($str, $newlineChar, $lineWidth);
124  $this->assertEquals($returnString, 'abcdefghijklmnopqrst' . LF . 'uvwxyz' . LF . '123456');
125  }
126 
131  $str = 'Mein Link auf eine News (Link: http://zzzzzzzzzzzzz.xxxxxxxxx.de/index.php?id=10&tx_ttnews%5Btt_news%5D=1&cHash=66f5af320da29b7ae1cda49047ca7358)';
133  $this->assertEquals($returnString, 'Mein Link auf eine News (Link:' . LF . 'http://zzzzzzzzzzzzz.xxxxxxxxx.de/index.php?id=10&tx_ttnews%5Btt_news%5D=1&cHash=66f5af320da29b7ae1cda49047ca7358)');
134  }
135 
141  public function parseAddressesProvider() {
142  return array(
143  'name &ltemail&gt;' => array('name <email@example.org>', array('email@example.org' => 'name')),
144  '&lt;email&gt;' => array('<email@example.org>', array('email@example.org')),
145  '@localhost' => array('@localhost', array()),
146  '000@example.com' => array('000@example.com', array('000@example.com')),
147  'email' => array('email@example.org', array('email@example.org')),
148  'email1,email2' => array('email1@example.org,email2@example.com', array('email1@example.org', 'email2@example.com')),
149  'name &ltemail&gt;,email2' => array('name <email1@example.org>,email2@example.com', array('email1@example.org' => 'name', 'email2@example.com')),
150  '"last, first" &lt;name@example.org&gt;' => array('"last, first" <email@example.org>', array('email@example.org' => '"last, first"')),
151  'email,name &ltemail&gt;,"last, first" &lt;name@example.org&gt;' => array(
152  'email1@example.org, name <email2@example.org>, "last, first" <email3@example.org>',
153  array(
154  'email1@example.org',
155  'email2@example.org' => 'name',
156  'email3@example.org' => '"last, first"'
157  )
158  )
159  );
160  }
161 
166  public function parseAddressesTest($source, $addressList) {
167  $returnArray = \TYPO3\CMS\Core\Utility\MailUtility::parseAddresses($source);
168  $this->assertEquals($addressList, $returnArray);
169  }
170 
171 }
doMailCallsHook($fromAddress='', $fromName='')
static mail($to, $subject, $messageBody, $additionalHeaders=NULL, $additionalParameters=NULL)
Definition: MailUtility.php:41
static addInstance($className, $instance)
static breakLinesForEmail($str, $newlineChar=LF, $lineWidth=76)
static resetSingletonInstances(array $newSingletonInstances)
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]