Skip to content

Commit 8847918

Browse files
committed
test: added tests for the Mail class
1 parent 8bdbcc0 commit 8847918

File tree

2 files changed

+238
-49
lines changed

2 files changed

+238
-49
lines changed

phpmyfaq/src/phpMyFAQ/Mail.php

Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,9 @@ private function setEmailTo(array &$target, string $targetAlias, string $address
256256
// Check for the permitted number of items into the $target array
257257
if (count($target) > 2) {
258258
$keys = array_keys($target);
259-
trigger_error(
260-
sprintf('<strong>Mail Class</strong>: a valid e-mail address, %s, ', $keys[0]) .
261-
sprintf('has been already added as \'%s\'!', $targetAlias),
262-
E_USER_ERROR
259+
throw new Exception(
260+
sprintf('<strong>Mail Class</strong>: too many e-mail addresses, %s, ', $keys[0]) .
261+
sprintf('have been already added as \'%s\'!', $targetAlias)
263262
);
264263
}
265264

@@ -285,9 +284,7 @@ private function addEmailTo(array &$target, string $targetAlias, string $address
285284

286285
// Don't allow duplicated addresses
287286
if (array_key_exists($address, $target)) {
288-
throw new Exception(
289-
'<strong>Mail Class</strong>: ' . $address . ' has been already added in ' . $targetAlias . '!'
290-
);
287+
throw new Exception('"' . $address . '" has been already added in ' . $targetAlias . '!');
291288
}
292289

293290
if (isset($name)) {
@@ -335,47 +332,6 @@ public static function validateEmail(string $address): bool
335332
return (bool) filter_var($address, FILTER_VALIDATE_EMAIL);
336333
}
337334

338-
/**
339-
* Add an attachment.
340-
*
341-
* @param string $path File path.
342-
* @param string|null $name File name. Defaults to the basename.
343-
* @param string $mimetype File MIME type. Defaults to 'application/octet-stream'.
344-
* @param string $disposition Attachment disposition. Defaults to 'attachment'.
345-
* @param string $cid Content ID, required when disposition is 'inline'. Defaults to ''.
346-
* @return bool True if successful, false otherwise.
347-
*/
348-
public function addAttachment(
349-
string $path,
350-
string $name = null,
351-
string $mimetype = 'application/octet-stream',
352-
string $disposition = 'attachment',
353-
string $cid = ''
354-
): bool {
355-
if (!file_exists($path)) {
356-
// File isn't found
357-
return false;
358-
}
359-
if (('inline' == $disposition) && ($cid === '' || $cid === '0')) {
360-
// Content ID is required
361-
return false;
362-
} else {
363-
if ($name === null || $name === '' || $name === '0') {
364-
$name = basename($path);
365-
}
366-
367-
$this->attachments[] = [
368-
'cid' => $cid,
369-
'disposition' => $disposition,
370-
'mimetype' => $mimetype,
371-
'name' => $name,
372-
'path' => $path
373-
];
374-
375-
return true;
376-
}
377-
}
378-
379335
/**
380336
* Add a recipient as <CC>.
381337
*
@@ -773,7 +729,7 @@ public function setReplyTo(string $address, string $name = null): bool
773729

774730
/**
775731
* If the email spam protection has been activated from the general
776-
* phpMyFAQ configuration this method converts an email address e.g.
732+
* phpMyFAQ configuration, this method converts an email address e.g.,
777733
* from "[email protected]" to "user_AT_example_DOT_org". Otherwise,
778734
* it will return the plain email address.
779735
*

tests/phpMyFAQ/MailTest.php

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
<?php
2+
3+
namespace phpMyFAQ;
4+
5+
use phpMyFAQ\Core\Exception;
6+
use phpMyFAQ\Database\Sqlite3;
7+
use phpMyFAQ\Mail\Builtin;
8+
use phpMyFAQ\Mail\SMTP;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class MailTest extends TestCase
12+
{
13+
private Mail $mail;
14+
15+
protected function setUp(): void
16+
{
17+
parent::setUp();
18+
19+
$dbHandle = new Sqlite3();
20+
$dbHandle->connect(PMF_TEST_DIR . '/test.db', '', '');
21+
$configuration = new Configuration($dbHandle);
22+
23+
$this->mail = new Mail($configuration);
24+
}
25+
26+
public function testCreateBoundaryReturnsString(): void
27+
{
28+
$result = Mail::createBoundary();
29+
30+
$this->assertIsString($result);
31+
$this->assertStringStartsWith('-----', $result);
32+
$this->assertSame(37, strlen($result));
33+
$this->assertMatchesRegularExpression('/^-----[a-f0-9]{32}$/', $result);
34+
}
35+
36+
public function testGetServerNameWithNoHostHeaders(): void
37+
{
38+
unset($_SERVER['HTTP_HOST']);
39+
unset($_SERVER['SERVER_NAME']);
40+
41+
$result = Mail::getServerName();
42+
43+
$this->assertSame('localhost.localdomain', $result);
44+
}
45+
46+
/**
47+
* @throws Exception
48+
*/
49+
public function testSetFromWithValidAddress(): void
50+
{
51+
$result = $this->mail->setFrom('[email protected]', 'John Doe');
52+
$this->assertTrue($result);
53+
}
54+
55+
public function testSetFromWithInvalidAddress(): void
56+
{
57+
$this->expectException(Exception::class);
58+
$this->mail->setFrom('invalid-email');
59+
}
60+
61+
public function testValidateEmailWithValidAddress(): void
62+
{
63+
$result = Mail::validateEmail('[email protected]');
64+
$this->assertTrue($result);
65+
}
66+
67+
public function testValidateEmailWithInvalidAddress(): void
68+
{
69+
$result = Mail::validateEmail('invalid-email');
70+
$this->assertFalse($result);
71+
}
72+
73+
public function testValidateEmailWithEmptyAddress(): void
74+
{
75+
$result = Mail::validateEmail('');
76+
$this->assertFalse($result);
77+
}
78+
79+
public function testValidateEmailWithZeroAddress(): void
80+
{
81+
$result = Mail::validateEmail('0');
82+
$this->assertFalse($result);
83+
}
84+
85+
public function testValidateEmailWithUnsafeCharacters(): void
86+
{
87+
$result = Mail::validateEmail("example@\r\nexample.com");
88+
$this->assertFalse($result);
89+
}
90+
91+
/**
92+
* @throws Exception
93+
*/
94+
public function testAddCcWithValidAddress(): void
95+
{
96+
$result = $this->mail->addCc('[email protected]', 'John Doe');
97+
$this->assertTrue($result);
98+
}
99+
100+
public function testAddCcWithInvalidAddress(): void
101+
{
102+
$this->expectException(Exception::class);
103+
$this->mail->addCc('invalid-email');
104+
}
105+
106+
/**
107+
* @throws Exception
108+
*/
109+
public function testAddToWithValidAddress(): void
110+
{
111+
$result = $this->mail->addTo('[email protected]', 'John Doe');
112+
$this->assertTrue($result);
113+
}
114+
115+
public function testAddToWithInvalidAddress(): void
116+
{
117+
$this->expectException(Exception::class);
118+
$this->mail->addTo('invalid-email');
119+
}
120+
121+
public function testGetDateWithValidTimestamp(): void
122+
{
123+
$timestamp = strtotime('2023-01-01 12:00:00');
124+
$result = Mail::getDate($timestamp);
125+
126+
$this->assertEquals(date('r', $timestamp), $result);
127+
}
128+
129+
public function testGetTimeWithRequestTimeSet(): void
130+
{
131+
$_SERVER['REQUEST_TIME'] = strtotime('2023-01-01 12:00:00');
132+
$result = Mail::getTime();
133+
134+
$this->assertEquals($_SERVER['REQUEST_TIME'], $result);
135+
}
136+
137+
public function testGetTimeWithNoRequestTime(): void
138+
{
139+
$requestTimeToRestore = $_SERVER['REQUEST_TIME'] ?? null;
140+
unset($_SERVER['REQUEST_TIME']);
141+
$result = Mail::getTime();
142+
143+
$this->assertLessThanOrEqual(time(), $result);
144+
$this->assertGreaterThanOrEqual(time() - 1, $result); // Allow for up to 1 second of difference
145+
146+
$_SERVER['REQUEST_TIME'] = $requestTimeToRestore;
147+
}
148+
149+
public function testWrapLinesWithDefaultWidthAndNoCut(): void
150+
{
151+
$message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum acnunc quis neque tempor varius.";
152+
$result = $this->mail->wrapLines($message);
153+
154+
$expectedResult = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum\r\nacnunc quis neque tempor varius.";
155+
$this->assertSame($expectedResult, $result);
156+
}
157+
158+
public function testWrapLinesWithCustomWidthAndCut(): void
159+
{
160+
$message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum ac nunc quis neque tempor varius.";
161+
$result = $this->mail->wrapLines($message, 20, true);
162+
163+
$expectedResult = "Lorem ipsum dolor\r\nsit amet,\r\nconsectetur\r\nadipiscing elit.\r\nVestibulum ac nunc\r\nquis neque tempor\r\nvarius.";
164+
$this->assertSame($expectedResult, $result);
165+
}
166+
167+
public function testWrapLinesWithCustomWidthAndNoCut(): void
168+
{
169+
$message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum ac nunc quis neque tempor varius.";
170+
$result = $this->mail->wrapLines($message, 30);
171+
172+
$expectedResult = "Lorem ipsum dolor sit amet,\r\nconsectetur adipiscing elit.\r\nVestibulum ac nunc quis neque\r\ntempor varius.";
173+
$this->assertSame($expectedResult, $result);
174+
}
175+
176+
public function testFixEOL(): void
177+
{
178+
$text = "Line 1\r\nLine 2\rLine 3\nLine 4\r\n";
179+
$result = $this->mail->fixEOL($text);
180+
181+
$expectedResult = "Line 1\r\nLine 2\r\nLine 3\r\nLine 4\r\n";
182+
$this->assertSame($expectedResult, $result);
183+
}
184+
185+
public function testGetMUAWithBuiltin(): void
186+
{
187+
$result = Mail::getMUA('builtin');
188+
$this->assertInstanceOf(Builtin::class, $result);
189+
}
190+
191+
public function testGetMUAWithSMTP(): void
192+
{
193+
$result = Mail::getMUA('smtp');
194+
$this->assertInstanceOf(SMTP::class, $result);
195+
}
196+
197+
/**
198+
* @throws Exception
199+
*/
200+
public function testSetReplyToWithValidAddress(): void
201+
{
202+
$result = $this->mail->setReplyTo('[email protected]', 'John Doe');
203+
$this->assertTrue($result);
204+
}
205+
206+
public function testSetReplyToWithInvalidAddress(): void
207+
{
208+
$this->expectException(Exception::class);
209+
$this->mail->setReplyTo('invalid-email');
210+
}
211+
212+
public function testSafeEmailWithSafeEmailEnabled(): void
213+
{
214+
$configurationMock = $this->createMock(Configuration::class);
215+
$configurationMock->method('get')->willReturn(true);
216+
217+
$instance = new Mail($configurationMock);
218+
219+
$result = $instance->safeEmail('[email protected]');
220+
$this->assertSame('test_AT_example_DOT_com', $result);
221+
}
222+
223+
public function testSafeEmailWithSafeEmailDisabled(): void
224+
{
225+
$configurationMock = $this->createMock(Configuration::class);
226+
$configurationMock->method('get')->willReturn(false);
227+
228+
$instance = new Mail($configurationMock);
229+
230+
$result = $instance->safeEmail('[email protected]');
231+
$this->assertSame('[email protected]', $result);
232+
}
233+
}

0 commit comments

Comments
 (0)