forked from PHPMailer/PHPMailer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phpmailerLangTest.php
340 lines (310 loc) · 10.7 KB
/
phpmailerLangTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<?php
/**
* PHPMailer - language file tests
* Before running these tests you need to install PHPUnit 3.3 or later through pear, like this:
* pear install "channel://pear.phpunit.de/PHPUnit"
* Then run the tests like this:
* phpunit phpmailerLangTest
* @package PHPMailer
* @author Andy Prevost
* @author Marcus Bointon
* @copyright 2004 - 2009 Andy Prevost
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
*/
require 'PHPUnit/Autoload.php';
/**
* PHPMailer - PHP email transport unit test class
* Performs authentication tests
*/
class PHPMailerLangTest extends PHPUnit_Framework_TestCase
{
/**
* Holds the default phpmailer instance.
* @private
* @var PHPMailer
*/
public $Mail;
/**
* Holds the SMTP mail host.
* @public
* @var string
*/
public $Host = "";
/**
* Holds the change log.
* @private
* @var string[]
*/
public $ChangeLog = array();
/**
* Holds the note log.
* @private
* @var string[]
*/
public $NoteLog = array();
/**
* @var string Default include path
*/
public $INCLUDE_DIR = '../';
/**
* Run before each test is started.
*/
public function setUp()
{
if (file_exists('./testbootstrap.php')) {
include './testbootstrap.php'; //Overrides go in here
}
require_once $this->INCLUDE_DIR . 'class.phpmailer.php';
$this->Mail = new PHPMailer;
$this->Mail->Priority = 3;
$this->Mail->Encoding = "8bit";
$this->Mail->CharSet = "iso-8859-1";
if (array_key_exists('mail_from', $_REQUEST)) {
$this->Mail->From = $_REQUEST['mail_from'];
} else {
$this->Mail->From = '[email protected]';
}
$this->Mail->FromName = "Unit Tester";
$this->Mail->Sender = "";
$this->Mail->Subject = "Unit Test";
$this->Mail->Body = "";
$this->Mail->AltBody = "";
$this->Mail->WordWrap = 0;
if (array_key_exists('mail_host', $_REQUEST)) {
$this->Mail->Host = $_REQUEST['mail_host'];
} else {
$this->Mail->Host = 'mail.example.com';
}
if (array_key_exists('mail_port', $_REQUEST)) {
$this->Mail->Port = $_REQUEST['mail_port'];
} else {
$this->Mail->Port = 25;
}
$this->Mail->Helo = "localhost.localdomain";
$this->Mail->SMTPAuth = false;
$this->Mail->Username = "";
$this->Mail->Password = "";
$this->Mail->PluginDir = $this->INCLUDE_DIR;
$this->Mail->addReplyTo("[email protected]", "Reply Guy");
$this->Mail->Sender = "[email protected]";
if (strlen($this->Mail->Host) > 0) {
$this->Mail->Mailer = "smtp";
} else {
$this->Mail->Mailer = "mail";
$this->Mail->Sender = "[email protected]";
}
if (array_key_exists('mail_to', $_REQUEST)) {
$this->setAddress($_REQUEST['mail_to'], 'Test User', 'to');
}
if (array_key_exists('mail_cc', $_REQUEST) and strlen($_REQUEST['mail_cc']) > 0) {
$this->setAddress($_REQUEST['mail_cc'], 'Carbon User', 'cc');
}
}
/**
* Run after each test is completed.
*/
public function tearDown()
{
// Clean global variables
$this->Mail = null;
$this->ChangeLog = array();
$this->NoteLog = array();
}
/**
* Build the body of the message in the appropriate format.
* @private
* @returns void
*/
public function buildBody()
{
$this->CheckChanges();
// Determine line endings for message
if ($this->Mail->ContentType == "text/html" || strlen($this->Mail->AltBody) > 0) {
$eol = "<br/>";
$bullet = "<li>";
$bullet_start = "<ul>";
$bullet_end = "</ul>";
} else {
$eol = "\n";
$bullet = " - ";
$bullet_start = "";
$bullet_end = "";
}
$ReportBody = "";
$ReportBody .= "---------------------" . $eol;
$ReportBody .= "Unit Test Information" . $eol;
$ReportBody .= "---------------------" . $eol;
$ReportBody .= "phpmailer version: " . $this->Mail->Version . $eol;
$ReportBody .= "Content Type: " . $this->Mail->ContentType . $eol;
if (strlen($this->Mail->Host) > 0) {
$ReportBody .= "Host: " . $this->Mail->Host . $eol;
}
// If attachments then create an attachment list
$attachments = $this->Mail->getAttachments();
if (count($attachments) > 0) {
$ReportBody .= "Attachments:" . $eol;
$ReportBody .= $bullet_start;
foreach ($attachments as $attachment) {
$ReportBody .= $bullet . "Name: " . $attachment[1] . ", ";
$ReportBody .= "Encoding: " . $attachment[3] . ", ";
$ReportBody .= "Type: " . $attachment[4] . $eol;
}
$ReportBody .= $bullet_end . $eol;
}
// If there are changes then list them
if (count($this->ChangeLog) > 0) {
$ReportBody .= "Changes" . $eol;
$ReportBody .= "-------" . $eol;
$ReportBody .= $bullet_start;
for ($i = 0; $i < count($this->ChangeLog); $i++) {
$ReportBody .= $bullet . $this->ChangeLog[$i][0] . " was changed to [" .
$this->ChangeLog[$i][1] . "]" . $eol;
}
$ReportBody .= $bullet_end . $eol . $eol;
}
// If there are notes then list them
if (count($this->NoteLog) > 0) {
$ReportBody .= "Notes" . $eol;
$ReportBody .= "-----" . $eol;
$ReportBody .= $bullet_start;
for ($i = 0; $i < count($this->NoteLog); $i++) {
$ReportBody .= $bullet . $this->NoteLog[$i] . $eol;
}
$ReportBody .= $bullet_end;
}
// Re-attach the original body
$this->Mail->Body .= $eol . $eol . $ReportBody;
}
/**
* Check which default settings have been changed for the report.
* @private
* @returns void
*/
public function checkChanges()
{
if ($this->Mail->Priority != 3) {
$this->addChange("Priority", $this->Mail->Priority);
}
if ($this->Mail->Encoding != "8bit") {
$this->addChange("Encoding", $this->Mail->Encoding);
}
if ($this->Mail->CharSet != "iso-8859-1") {
$this->addChange("CharSet", $this->Mail->CharSet);
}
if ($this->Mail->Sender != "") {
$this->addChange("Sender", $this->Mail->Sender);
}
if ($this->Mail->WordWrap != 0) {
$this->addChange("WordWrap", $this->Mail->WordWrap);
}
if ($this->Mail->Mailer != "mail") {
$this->addChange("Mailer", $this->Mail->Mailer);
}
if ($this->Mail->Port != 25) {
$this->addChange("Port", $this->Mail->Port);
}
if ($this->Mail->Helo != "localhost.localdomain") {
$this->addChange("Helo", $this->Mail->Helo);
}
if ($this->Mail->SMTPAuth) {
$this->addChange("SMTPAuth", "true");
}
}
/**
* Add a changelog entry.
* @access private
* @param string $sName
* @param string $sNewValue
* @return void
*/
public function addChange($sName, $sNewValue)
{
$this->ChangeLog[] = array($sName, $sNewValue);
}
/**
* Adds a simple note to the message.
* @public
* @param string $sValue
* @return void
*/
public function addNote($sValue)
{
$this->NoteLog[] = $sValue;
}
/**
* Adds all of the addresses
* @access public
* @param string $sAddress
* @param string $sName
* @param string $sType
* @return boolean
*/
public function setAddress($sAddress, $sName = '', $sType = 'to')
{
switch ($sType) {
case 'to':
return $this->Mail->addAddress($sAddress, $sName);
case 'cc':
return $this->Mail->addCC($sAddress, $sName);
case "bcc":
return $this->Mail->addBCC($sAddress, $sName);
}
return false;
}
/**
* Test language files for missing and excess translations
* All languages are compared with English
*/
public function testTranslations()
{
$this->Mail->setLanguage('en');
$definedStrings = $this->Mail->getTranslations();
$err = '';
foreach (new DirectoryIterator('../language') as $fileInfo) {
if ($fileInfo->isDot()) {
continue;
}
$matches = array();
//Only look at language files, ignore anything else in there
if (preg_match('/^phpmailer\.lang-([a-z_]{2,})\.php$/', $fileInfo->getFilename(), $matches)) {
$lang = $matches[1]; //Extract language code
$PHPMAILER_LANG = array(); //Language strings get put in here
include $fileInfo->getPathname(); //Get language strings
$missing = array_diff(array_keys($definedStrings), array_keys($PHPMAILER_LANG));
$extra = array_diff(array_keys($PHPMAILER_LANG), array_keys($definedStrings));
if (!empty($missing)) {
$err .= "\nMissing translations in $lang: " . implode(', ', $missing);
}
if (!empty($extra)) {
$err .= "\nExtra translations in $lang: " . implode(', ', $extra);
}
}
}
$this->assertEmpty($err, $err);
}
}
/**
* This is a sample form for setting appropriate test values through a browser
* These values can also be set using a file called testbootstrap.php (not in svn) in the same folder as this script
* which is probably more useful if you run these tests a lot
* <html>
* <body>
* <h3>phpmailer Unit Test</h3>
* By entering a SMTP hostname it will automatically perform tests with SMTP.
*
* <form name="phpmailer_unit" action=__FILE__ method="get">
* <input type="hidden" name="submitted" value="1"/>
* From Address: <input type="text" size="50" name="mail_from" value="<?php echo get("mail_from"); ?>"/>
* <br/>
* To Address: <input type="text" size="50" name="mail_to" value="<?php echo get("mail_to"); ?>"/>
* <br/>
* Cc Address: <input type="text" size="50" name="mail_cc" value="<?php echo get("mail_cc"); ?>"/>
* <br/>
* SMTP Hostname: <input type="text" size="50" name="mail_host" value="<?php echo get("mail_host"); ?>"/>
* <p/>
* <input type="submit" value="Run Test"/>
*
* </form>
* </body>
* </html>
*/