-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replacing Ushahidi_Mailer with a Lumen implementation
- Add new implementation - Add illuminate/mail - Add mockery for tests - Add tests
Showing
11 changed files
with
719 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
/** | ||
* Ushahidi Mailer | ||
* | ||
* @author Ushahidi Team <team@ushahidi.com> | ||
* @package Ushahidi\Application | ||
* @copyright 2014 Ushahidi | ||
* @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3) | ||
*/ | ||
|
||
namespace Ushahidi\App\Tools; | ||
|
||
use Ushahidi\Core\Tool\Mailer as MailerContract; | ||
use Illuminate\Contracts\Mail\Mailer; | ||
use Illuminate\Support\Str; | ||
|
||
class LumenMailer implements MailerContract | ||
{ | ||
public function __construct(Mailer $mailer, $siteConfig, $clientUrl) | ||
{ | ||
$this->mailer = $mailer; | ||
$this->siteConfig = $siteConfig; | ||
$this->clientUrl = $clientUrl; | ||
} | ||
|
||
public function send($to, $type, Array $params = null) | ||
{ | ||
// Only available type right now is 'resetpassword' | ||
$method = "send" . Str::ucfirst($type); | ||
if (method_exists($this, $method)) { | ||
$this->$method($to, $params); | ||
} else { | ||
// Exception | ||
throw new Exception('Unsupported mail type: ' + $type); | ||
} | ||
} | ||
|
||
protected function sendResetpassword($to, $params) | ||
{ | ||
$site_name = $this->siteConfig['name']; | ||
$site_email = $this->siteConfig['email']; | ||
$multisite_email = config('multisite.email'); | ||
|
||
// @todo make this more robust | ||
if ($multisite_email) { | ||
$from_email = $multisite_email; | ||
} elseif ($site_email) { | ||
$from_email = $site_email; | ||
} else { | ||
$from_email = false; | ||
// Get host from lumen | ||
// $host = app()->make('request')->getHost(); | ||
// $from_email = 'noreply@' . $host; | ||
} | ||
|
||
$data = [ | ||
'site_name' => $site_name, | ||
'token' => $params['token'], | ||
'client_url' => $this->clientUrl | ||
]; | ||
|
||
$subject = $site_name . ': Password reset'; | ||
|
||
$this->mailer->send( | ||
'emails/forgot-password', | ||
$data, | ||
function ($message) use ($to, $subject, $from_email, $site_name) { | ||
$message->to($to); | ||
$message->subject($subject); | ||
if ($from_email) { | ||
$message->from($from_email, $site_name); | ||
} | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
|
||
return [ | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Mail Driver | ||
|-------------------------------------------------------------------------- | ||
| | ||
| Laravel supports both SMTP and PHP's "mail" function as drivers for the | ||
| sending of e-mail. You may specify which one you're using throughout | ||
| your application here. By default, Laravel is setup for SMTP mail. | ||
| | ||
| Supported: "smtp", "mail", "sendmail", "mailgun", "mandrill", "ses", "log" | ||
| | ||
*/ | ||
|
||
'driver' => env('MAIL_DRIVER', 'mail'), | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| SMTP Host Address | ||
|-------------------------------------------------------------------------- | ||
| | ||
| Here you may provide the host address of the SMTP server used by your | ||
| applications. A default option is provided that is compatible with | ||
| the Mailgun mail service which will provide reliable deliveries. | ||
| | ||
*/ | ||
|
||
'host' => env('MAIL_HOST', 'smtp.mailgun.org'), | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| SMTP Host Port | ||
|-------------------------------------------------------------------------- | ||
| | ||
| This is the SMTP port used by your application to deliver e-mails to | ||
| users of the application. Like the host we have set this value to | ||
| stay compatible with the Mailgun e-mail application by default. | ||
| | ||
*/ | ||
|
||
'port' => env('MAIL_PORT', 587), | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Global "From" Address | ||
|-------------------------------------------------------------------------- | ||
| | ||
| You may wish for all e-mails sent by your application to be sent from | ||
| the same address. Here, you may specify a name and address that is | ||
| used globally for all e-mails that are sent by your application. | ||
| | ||
*/ | ||
|
||
'from' => ['address' => env('MAIL_ADDRESS'), 'name' => env('MAIL_NAME')], | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| E-Mail Encryption Protocol | ||
|-------------------------------------------------------------------------- | ||
| | ||
| Here you may specify the encryption protocol that should be used when | ||
| the application send e-mail messages. A sensible default using the | ||
| transport layer security protocol should provide great security. | ||
| | ||
*/ | ||
|
||
'encryption' => env('MAIL_ENCRYPTION', 'tls'), | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| SMTP Server Username | ||
|-------------------------------------------------------------------------- | ||
| | ||
| If your SMTP server requires a username for authentication, you should | ||
| set it here. This will get used to authenticate with your server on | ||
| connection. You may also set the "password" value below this one. | ||
| | ||
*/ | ||
|
||
'username' => env('MAIL_USERNAME'), | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| SMTP Server Password | ||
|-------------------------------------------------------------------------- | ||
| | ||
| Here you may set the password required by your SMTP server to send out | ||
| messages from your application. This will be given to the server on | ||
| connection so that the application will be able to send messages. | ||
| | ||
*/ | ||
|
||
'password' => env('MAIL_PASSWORD'), | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Sendmail System Path | ||
|-------------------------------------------------------------------------- | ||
| | ||
| When using the "sendmail" driver to send e-mails, we will need to know | ||
| the path to where Sendmail lives on this server. A default path has | ||
| been provided here, which will work well on most of your systems. | ||
| | ||
*/ | ||
|
||
'sendmail' => '/usr/sbin/sendmail -bs', | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Mail "Pretend" | ||
|-------------------------------------------------------------------------- | ||
| | ||
| When this option is enabled, e-mail will not actually be sent over the | ||
| web and will instead be written to your application's logs files so | ||
| you may inspect the message. This is great for local development. | ||
| | ||
*/ | ||
|
||
'pretend' => env('MAIL_PRETEND', false), | ||
|
||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
<?php | ||
|
||
/** | ||
* Unit tests for Lumen implementation of Ushahidi\Core\Tool\Mailer | ||
* | ||
* @author Ushahidi Team <team@ushahidi.com> | ||
* @package Ushahidi\Application\Tests | ||
* @copyright 2013 Ushahidi | ||
* @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3) | ||
*/ | ||
|
||
namespace Tests\Unit\App\Tools; | ||
|
||
use Ushahidi\App\Tools\LumenMailer; | ||
// use Illuminate\Support\Facades\Mail; | ||
use Tests\TestCase; | ||
use Mockery as M; | ||
|
||
/** | ||
* @backupGlobals disabled | ||
* @preserveGlobalState disabled | ||
*/ | ||
class LumenMailerTest extends TestCase | ||
{ | ||
|
||
protected function buildMailer() { | ||
|
||
} | ||
|
||
public function testSendWithMultisite() { | ||
config([ | ||
'mail.pretend' => true | ||
]); | ||
|
||
config([ | ||
'multisite.email' => 'deploy@multisite.ushahidi.app' | ||
]); | ||
|
||
$illuminateMailer = M::spy(app('mailer')); | ||
|
||
$mailer = new LumenMailer( | ||
$illuminateMailer, | ||
[ | ||
'name' => 'TestDeploy', | ||
'email' => 'test@ushahidi.app' | ||
], | ||
'https://ushahidi.app/' | ||
); | ||
|
||
$mailer->send('noone@ushahidi.com', 'Resetpassword', [ | ||
'token' => 'abc123' | ||
]); | ||
|
||
$illuminateMailer->shouldHaveReceived('send') | ||
->once() | ||
->with( | ||
'emails/forgot-password', | ||
M::on(function($data) { | ||
$this->assertArrayHasKey( 'site_name', $data ); | ||
$this->assertArrayHasKey( 'token', $data ); | ||
$this->assertArrayHasKey( 'client_url', $data ); | ||
|
||
return true; | ||
}), | ||
M::on(function(\Closure $closure) { | ||
$mock = M::mock('Illuminate\Mailer\Message'); | ||
$mock->shouldReceive('to')->once()->with('noone@ushahidi.com') | ||
->andReturn($mock); // simulate the chaining | ||
$mock->shouldReceive('from')->once()->with('deploy@multisite.ushahidi.app', 'TestDeploy') | ||
->andReturn($mock); // simulate the chaining | ||
$mock->shouldReceive('subject')->once()->with('TestDeploy: Password reset') | ||
->andReturn($mock); // simulate the chaining | ||
|
||
$closure($mock); | ||
return true; | ||
}) | ||
); | ||
} | ||
|
||
public function testSendWithSiteEmail() { | ||
config([ | ||
'mail.pretend' => true | ||
]); | ||
|
||
config([ | ||
'multisite.email' => false | ||
]); | ||
|
||
$illuminateMailer = M::spy(app('mailer')); | ||
|
||
$mailer = new LumenMailer( | ||
$illuminateMailer, | ||
[ | ||
'name' => 'TestDeploy', | ||
'email' => 'test@ushahidi.app' | ||
], | ||
'https://ushahidi.app/' | ||
); | ||
|
||
$mailer->send('noone@ushahidi.com', 'Resetpassword', [ | ||
'token' => 'abc123' | ||
]); | ||
|
||
$illuminateMailer->shouldHaveReceived('send') | ||
->once() | ||
->with( | ||
'emails/forgot-password', | ||
M::on(function($data) { | ||
$this->assertArrayHasKey( 'site_name', $data ); | ||
$this->assertArrayHasKey( 'token', $data ); | ||
$this->assertArrayHasKey( 'client_url', $data ); | ||
|
||
return true; | ||
}), | ||
M::on(function(\Closure $closure) { | ||
$mock = M::mock('Illuminate\Mailer\Message'); | ||
$mock->shouldReceive('to')->once()->with('noone@ushahidi.com') | ||
->andReturn($mock); // simulate the chaining | ||
$mock->shouldReceive('from')->once()->with('test@ushahidi.app', 'TestDeploy') | ||
->andReturn($mock); // simulate the chaining | ||
$mock->shouldReceive('subject')->once()->with('TestDeploy: Password reset') | ||
->andReturn($mock); // simulate the chaining | ||
|
||
$closure($mock); | ||
return true; | ||
}) | ||
); | ||
} | ||
|
||
public function testSendWithFallbackEmail() { | ||
|
||
config([ | ||
'mail.pretend' => true | ||
]); | ||
|
||
config([ | ||
'multisite.email' => false | ||
]); | ||
|
||
$illuminateMailer = M::spy(app('mailer')); | ||
|
||
$mailer = new LumenMailer( | ||
$illuminateMailer, | ||
[ | ||
'name' => 'TestDeploy', | ||
'email' => false | ||
], | ||
'https://ushahidi.app/' | ||
); | ||
|
||
$mailer->send('noone@ushahidi.com', 'Resetpassword', [ | ||
'token' => 'abc123' | ||
]); | ||
|
||
$illuminateMailer->shouldHaveReceived('send') | ||
->once() | ||
->with( | ||
'emails/forgot-password', | ||
M::on(function($data) { | ||
$this->assertArrayHasKey( 'site_name', $data ); | ||
$this->assertArrayHasKey( 'token', $data ); | ||
$this->assertArrayHasKey( 'client_url', $data ); | ||
|
||
return true; | ||
}), | ||
M::on(function(\Closure $closure) { | ||
$mock = M::mock('Illuminate\Mailer\Message'); | ||
$mock->shouldReceive('to')->once()->with('noone@ushahidi.com') | ||
->andReturn($mock); // simulate the chaining | ||
$mock->shouldNotReceive('from'); | ||
$mock->shouldReceive('subject')->once()->with('TestDeploy: Password reset') | ||
->andReturn($mock); // simulate the chaining | ||
|
||
$closure($mock); | ||
return true; | ||
}) | ||
); | ||
} | ||
} |