Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nc12 emailtemplate follow up #483

Merged
merged 2 commits into from
Jun 10, 2017
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
42 changes: 17 additions & 25 deletions controller/emailcontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,37 +78,29 @@ public function __construct($appName, IRequest $request, IUserSession $userSessi
}

/**
* @param string $to
* @param string $recipient
* @param string $url
* @param string $name
* @param string $calendarName
* @return JSONResponse
* @NoAdminRequired
*/
public function sendEmailPublicLink($to, $url, $name) {
public function sendEmailPublicLink($recipient, $url, $calendarName) {
$user = $this->userSession->getUser();
$username = $user->getDisplayName();
$displayName = $user->getDisplayName();

$subject = $this->l10n->t('%s has published the calendar "%s"', [$username, $name]);
$subject = $this->l10n->t('%s has published the calendar »%s«', [$displayName, $calendarName]);

$serverVersion = $this->config->getSystemValue('version');
if (version_compare($serverVersion, '12', '>=')) {
$emailTemplate = $this->mailer->createEMailTemplate();

$emailTemplate->addHeader();
$emailTemplate->addHeading($this->l10n->t('%s has published the calendar %s', [$username, $name]));
$emailTemplate->addHeading($this->l10n->t('%s has published the calendar »%s«', [$displayName, $calendarName]));

$emailTemplate->addBodyText($this->l10n->t('Hello,'));
$emailTemplate->addBodyText($this->l10n->t('We wanted to inform you that %s has published the calendar »%s«.', [$displayName, $calendarName]));

$htmlText = str_replace(
['{boldstart}', '{boldend}'],
['<b>', '</b>'],
$this->l10n->t('We wanted to inform you that %s has published the calendar {boldstart}%s{boldend}.', [$username, $name])
);

$plainText = $this->l10n->t('We wanted to inform you that %s has published the calendar %s.', [$username, $name]);
$emailTemplate->addBodyText($htmlText, $plainText);

$emailTemplate->addBodyButton($this->l10n->t('Click here to access it'), $url);
$emailTemplate->addBodyButton($this->l10n->t('Open »%s«', [$calendarName]), $url);

// TRANSLATORS term at the end of a mail
$emailTemplate->addBodyText($this->l10n->t('Cheers!'));
Expand All @@ -120,36 +112,36 @@ public function sendEmailPublicLink($to, $url, $name) {
} else {
$emailTemplateHTML = new TemplateResponse('calendar', 'mail.publication.html', [
'subject' => $subject,
'username' => $username,
'calendarname' => $name,
'username' => $displayName,
'calendarname' => $calendarName,
'calendarurl' => $url,
'defaults' => $this->defaults
], 'public');
$bodyHTML = $emailTemplateHTML->render();

$emailTemplateText = new TemplateResponse('calendar', 'mail.publication.text', [
'subject' => $subject,
'username' => $username,
'calendarname' => $name,
'username' => $displayName,
'calendarname' => $calendarName,
'calendarurl' => $url
], 'blank');
$textBody = $emailTemplateText->render();
}

$status = $this->sendEmail($to, $subject, $bodyHTML, $textBody);
$status = $this->sendEmail($recipient, $subject, $bodyHTML, $textBody);

return new JSONResponse([], $status);
}

/**
* @param string $target
* @param string $recipient
* @param string $subject
* @param string $body
* @param string $textBody
* @return int
*/
private function sendEmail($target, $subject, $body, $textBody) {
if (!$this->mailer->validateMailAddress($target)) {
private function sendEmail($recipient, $subject, $body, $textBody) {
if (!$this->mailer->validateMailAddress($recipient)) {
return Http::STATUS_BAD_REQUEST;
}

Expand All @@ -160,7 +152,7 @@ private function sendEmail($target, $subject, $body, $textBody) {
$message = $this->mailer->createMessage();
$message->setSubject($subject);
$message->setFrom([$sendFrom => $this->defaults->getName()]);
$message->setTo([$target => 'Recipient']);
$message->setTo([$recipient => $this->l10n->t('Recipient')]);
$message->setPlainBody($textBody);
$message->setHtmlBody($body);
$this->mailer->send($message);
Expand Down
165 changes: 146 additions & 19 deletions tests/php/controller/emailcontrollerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,42 @@ class EmailControllerTest extends \PHPUnit_Framework_TestCase {

private $appName;
private $request;
private $config;
private $userSession;
private $dummyUser;
private $config;
private $mailer;
private $emailTemplate;
private $message;
private $l10n;
private $defaults;

private $dummyUser;

private $controller;

public function setUp() {
$this->appName = 'calendar';
$this->request = $this->getMockBuilder('\OCP\IRequest')
->disableOriginalConstructor()
->getMock();
$this->config = $this->getMockBuilder('\OCP\IConfig')

$this->userSession = $this->getMockBuilder('\OCP\IUserSession')
->disableOriginalConstructor()
->getMock();
$this->userSession = $this->getMockBuilder('\OCP\IUserSession')
$this->dummyUser = $this->getMockBuilder('\OCP\IUser')
->disableOriginalConstructor()
->getMock();

$this->dummyUser = $this->getMockBuilder('OCP\IUser')
$this->config = $this->getMockBuilder('\OCP\IConfig')
->disableOriginalConstructor()
->getMock();

$this->mailer = $this->getMockBuilder('\OCP\Mail\IMailer')
->disableOriginalConstructor()
->getMock();
$this->emailTemplate = $this->getMockBuilder('\OCP\Mail\IEMailTemplate')
->disableOriginalConstructor()
->getMock();
$this->message = $this->getMockBuilder('\OC\Mail\Message')
->disableOriginalConstructor()
->getMock();

$this->l10n = $this->getMockBuilder('OC\L10N\L10N')
->disableOriginalConstructor()
Expand All @@ -68,24 +75,144 @@ public function setUp() {
}

/**
* @dataProvider indexEmailPublicLink
* @dataProvider emailPublicLinkProvider
*/
public function testEmailPublicLink($to, $url, $name) {

$this->userSession->expects($this->exactly(1))
public function testEmailPublicLink($validEmailAddress) {
$this->userSession->expects($this->once())
->method('getUser')
->will($this->returnValue($this->dummyUser));

$actual = $this->controller->sendEmailPublicLink($to, $url, $name);

$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $actual);

$this->dummyUser->expects($this->once())
->method('getDisplayName')
->will($this->returnValue('Fancy displayname 42'));

$this->l10n->expects($this->at(0))
->method('t')
->with('%s has published the calendar »%s«', ['Fancy displayname 42', 'Calendarname 1337'])
->will($this->returnValue('localized_1'));

$this->config->expects($this->at(0))
->method('getSystemValue')
->with('version')
->will($this->returnValue('12.0.0'));

$this->mailer->expects($this->at(0))
->method('createEMailTemplate')
->will($this->returnValue($this->emailTemplate));

$this->emailTemplate->expects($this->at(1))
->method('addHeader');

$this->l10n->expects($this->at(1))
->method('t')
->with('%s has published the calendar »%s«', ['Fancy displayname 42', 'Calendarname 1337'])
->will($this->returnValue('localized_2'));
$this->emailTemplate->expects($this->at(1))
->method('addHeading')
->with('localized_2');

$this->l10n->expects($this->at(2))
->method('t')
->with('Hello,')
->will($this->returnValue('localized_3'));
$this->emailTemplate->expects($this->at(2))
->method('addBodyText')
->with('localized_3');

$this->l10n->expects($this->at(3))
->method('t')
->with('We wanted to inform you that %s has published the calendar »%s«.', ['Fancy displayname 42', 'Calendarname 1337'])
->will($this->returnValue('localized_4'));
$this->emailTemplate->expects($this->at(3))
->method('addBodyText')
->with('localized_4');

$this->l10n->expects($this->at(4))
->method('t')
->with('Open »%s«', ['Calendarname 1337'])
->will($this->returnValue('localized_5'));
$this->emailTemplate->expects($this->at(4))
->method('addBodyButton')
->with('localized_5', 'https://url-to-public-calendar');

$this->l10n->expects($this->at(5))
->method('t')
->with('Cheers!')
->will($this->returnValue('localized_6'));
$this->emailTemplate->expects($this->at(5))
->method('addBodyText')
->with('localized_6');

$this->emailTemplate->expects($this->at(6))
->method('addFooter');
$this->emailTemplate->expects($this->at(7))
->method('renderHtml')
->will($this->returnValue('html_body'));
$this->emailTemplate->expects($this->at(8))
->method('renderText')
->will($this->returnValue('text_body'));

$this->mailer->expects($this->at(1))
->method('validateMailAddress')
->with('test@test.tld')
->will($this->returnValue($validEmailAddress));

if ($validEmailAddress) {
$this->config->expects($this->at(1))
->method('getSystemValue')
->with('mail_domain', 'domain.org')
->will($this->returnValue('domain_123'));
$this->config->expects($this->at(2))
->method('getSystemValue')
->with('mail_from_address', 'nextcloud')
->will($this->returnValue('from_456'));

$this->mailer->expects($this->at(2))
->method('createMessage')
->will($this->returnValue($this->message));

$this->message->expects($this->at(0))
->method('setSubject')
->with('localized_1');
$this->defaults->expects($this->at(0))
->method('getName')
->will($this->returnValue('default_instance_name'));
$this->message->expects($this->at(1))
->method('setFrom')
->with(['from_456@domain_123' => 'default_instance_name']);
$this->l10n->expects($this->at(6))
->method('t')
->with('Recipient')
->will($this->returnValue('localized_7'));
$this->message->expects($this->at(2))
->method('setTo')
->with(['test@test.tld' => 'localized_7']);
$this->message->expects($this->at(3))
->method('setPlainBody')
->with('text_body');
$this->message->expects($this->at(4))
->method('setHtmlBody')
->with('html_body');

$this->mailer->expects($this->at(3))
->method('send')
->with($this->message);
}

$response = $this->controller->sendEmailPublicLink('test@test.tld', 'https://url-to-public-calendar', 'Calendarname 1337');
$this->assertInstanceOf('OCP\AppFramework\Http\JSONResponse', $response);
$this->assertEquals([], $response->getData());

if ($validEmailAddress) {
$this->assertEquals(200, $response->getStatus());
} else {
$this->assertEquals(400, $response->getStatus());
}
}

public function indexEmailPublicLink() {
public function emailPublicLinkProvider() {
return [
['test@test.tld', 'myurl.tld', 'user123'],
['testtesttld', 'myurl.tld', 'user123'],
[true],
[false],
];
}
}