Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,13 @@ public static function sendVerify($data)
$mailer->Body = $mailHtmlBody;
$mailer->AltBody = $mailPlainBody;

return $mailer->send();
$result = $mailer->send();

if ($result === false || $mailer->isError()) {
throw new \Exception($mailer->ErrorInfo);
}

return $result;
}

public static function sendResetPassword($data)
Expand Down
19 changes: 16 additions & 3 deletions lib/Routes/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,23 @@ public static function respondToAccountVerify()
];

$verifyToken = User::saveVerifyToken('verify', $verifyData);
Mailer::sendVerify($verifyToken);

$responseData = "OK";
header("HTTP/1.1 201 Created");
try {
Mailer::sendVerify($verifyToken);

$responseCode = 201;
$responseData = "OK";
} catch (\Throwable $e) {
error_log('Could not send verification email (' . get_class($e) . '):' . $e->getMessage());

$responseCode = 502;
$responseData = [
'title' => 'Mail could not be sent',
'errors' => [['detail' => 'Failed to send verification email']]
];
}

http_response_code($responseCode);
header("Content-type: application/json");
echo json_encode($responseData, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR);
}
Expand Down
3 changes: 2 additions & 1 deletion tests/phpunit/DbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

use Pdsinterop\PhpSolid\Db;

const DBPATH = ":memory:";
defined('DBPATH') || define('DBPATH', ":memory:");

class DbTest extends \PHPUnit\Framework\TestCase
{
public function testConnect()
Expand Down
22 changes: 20 additions & 2 deletions tests/phpunit/MailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,26 @@

const MAILSTYLES = [];

const BASEURL = "https://example.com";
defined('BASEURL') || define('BASEURL', "https://example.com");

class MailerMock
{
public $Subject;
public $Body;
public $AltBody;
public $addresses = [];
public $ErrorInfo = '';

public function addAddress($address)
{
$this->addresses[] = $address;
}

public function isError()
{
return $this->ErrorInfo !== '';
}

public function send()
{
return true;
Expand Down Expand Up @@ -77,7 +83,7 @@ public function testAccountCreated()
$this->assertEquals("Welcome to Solid!", $doc->getElementsByTagName("title")[0]->textContent); // If this works, I'm assuming it is valid HTML.
}

public function testVerify()
public function testVerifySucces()
{
Mailer::$mailer = new MailerMock();
Mailer::sendVerify([
Expand All @@ -99,6 +105,18 @@ public function testVerify()
$this->assertEquals("Confirm your e-mail", $doc->getElementsByTagName("title")[0]->textContent); // If this works, I'm assuming it is valid HTML.
}

public function testVerifyFailure()
{
$this->expectException(\Exception::class);

Mailer::$mailer = new MailerMock();
Mailer::$mailer->ErrorInfo = 'Mock Error';
Mailer::sendVerify([
'email' => 'alice@example.com',
'code' => '654321'
]);
}

public function testResetPassword()
{
Mailer::$mailer = new MailerMock();
Expand Down
Loading