mirror of
https://github.com/nextcloud/server.git
synced 2024-08-30 17:57:26 +02:00
Adding unit test for MailNotifications::sendInternalShareMail()
This commit is contained in:
parent
eebe2b9c23
commit
dcc7ff09ba
@ -99,7 +99,7 @@ class MailNotifications {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$items = \OCP\Share::getItemSharedWithUser($itemType, $itemSource, $recipient);
|
$items = $this->getItemSharedWithUser($itemSource, $itemType, $recipient);
|
||||||
$filename = trim($items[0]['file_target'], '/');
|
$filename = trim($items[0]['file_target'], '/');
|
||||||
$subject = (string) $this->l->t('%s shared »%s« with you', array($this->senderDisplayName, $filename));
|
$subject = (string) $this->l->t('%s shared »%s« with you', array($this->senderDisplayName, $filename));
|
||||||
$expiration = null;
|
$expiration = null;
|
||||||
@ -130,7 +130,7 @@ class MailNotifications {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$link = \OCP\Util::linkToAbsolute('files', 'index.php', $args);
|
$link = Util::linkToAbsolute('files', 'index.php', $args);
|
||||||
|
|
||||||
list($htmlBody, $textBody) = $this->createMailBody($filename, $link, $expiration, 'internal');
|
list($htmlBody, $textBody) = $this->createMailBody($filename, $link, $expiration, 'internal');
|
||||||
|
|
||||||
@ -142,7 +142,7 @@ class MailNotifications {
|
|||||||
$message->setHtmlBody($htmlBody);
|
$message->setHtmlBody($htmlBody);
|
||||||
$message->setPlainBody($textBody);
|
$message->setPlainBody($textBody);
|
||||||
$message->setFrom([
|
$message->setFrom([
|
||||||
\OCP\Util::getDefaultEmailAddress('sharing-noreply') =>
|
Util::getDefaultEmailAddress('sharing-noreply') =>
|
||||||
(string)$this->l->t('%s via %s', [
|
(string)$this->l->t('%s via %s', [
|
||||||
$this->senderDisplayName,
|
$this->senderDisplayName,
|
||||||
$this->defaults->getName()
|
$this->defaults->getName()
|
||||||
@ -183,7 +183,7 @@ class MailNotifications {
|
|||||||
$message->setHtmlBody($htmlBody);
|
$message->setHtmlBody($htmlBody);
|
||||||
$message->setPlainBody($textBody);
|
$message->setPlainBody($textBody);
|
||||||
$message->setFrom([
|
$message->setFrom([
|
||||||
\OCP\Util::getDefaultEmailAddress('sharing-noreply') =>
|
Util::getDefaultEmailAddress('sharing-noreply') =>
|
||||||
(string)$this->l->t('%s via %s', [
|
(string)$this->l->t('%s via %s', [
|
||||||
$this->senderDisplayName,
|
$this->senderDisplayName,
|
||||||
$this->defaults->getName()
|
$this->defaults->getName()
|
||||||
@ -206,7 +206,7 @@ class MailNotifications {
|
|||||||
* @param string $filename the shared file
|
* @param string $filename the shared file
|
||||||
* @param string $link link to the shared file
|
* @param string $link link to the shared file
|
||||||
* @param int $expiration expiration date (timestamp)
|
* @param int $expiration expiration date (timestamp)
|
||||||
* @param bool $prefix prefix of mail template files
|
* @param string $prefix prefix of mail template files
|
||||||
* @return array an array of the html mail body and the plain text mail body
|
* @return array an array of the html mail body and the plain text mail body
|
||||||
*/
|
*/
|
||||||
private function createMailBody($filename, $link, $expiration, $prefix = '') {
|
private function createMailBody($filename, $link, $expiration, $prefix = '') {
|
||||||
|
@ -74,7 +74,7 @@ interface IL10N {
|
|||||||
/**
|
/**
|
||||||
* Localization
|
* Localization
|
||||||
* @param string $type Type of localization
|
* @param string $type Type of localization
|
||||||
* @param array $data parameters for this localization
|
* @param int|string $data parameters for this localization
|
||||||
* @param array $options currently supports following options:
|
* @param array $options currently supports following options:
|
||||||
* - 'width': handed into \Punic\Calendar::formatDate as second parameter
|
* - 'width': handed into \Punic\Calendar::formatDate as second parameter
|
||||||
* @return string|false
|
* @return string|false
|
||||||
|
@ -171,37 +171,7 @@ class MailNotificationsTest extends \Test\TestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function testSendLinkShareMailException() {
|
public function testSendLinkShareMailException() {
|
||||||
$message = $this->getMockBuilder('\OC\Mail\Message')
|
$this->setupMailerMock('TestUser shared »MyFile« with you', ['lukas@owncloud.com']);
|
||||||
->disableOriginalConstructor()->getMock();
|
|
||||||
|
|
||||||
$message
|
|
||||||
->expects($this->once())
|
|
||||||
->method('setSubject')
|
|
||||||
->with('TestUser shared »MyFile« with you');
|
|
||||||
$message
|
|
||||||
->expects($this->once())
|
|
||||||
->method('setTo')
|
|
||||||
->with(['lukas@owncloud.com']);
|
|
||||||
$message
|
|
||||||
->expects($this->once())
|
|
||||||
->method('setHtmlBody');
|
|
||||||
$message
|
|
||||||
->expects($this->once())
|
|
||||||
->method('setPlainBody');
|
|
||||||
$message
|
|
||||||
->expects($this->once())
|
|
||||||
->method('setFrom')
|
|
||||||
->with([\OCP\Util::getDefaultEmailAddress('sharing-noreply') => 'TestUser via UnitTestCloud']);
|
|
||||||
|
|
||||||
$this->mailer
|
|
||||||
->expects($this->once())
|
|
||||||
->method('createMessage')
|
|
||||||
->will($this->returnValue($message));
|
|
||||||
$this->mailer
|
|
||||||
->expects($this->once())
|
|
||||||
->method('send')
|
|
||||||
->with($message)
|
|
||||||
->will($this->throwException(new Exception('Some Exception Message')));
|
|
||||||
|
|
||||||
$mailNotifications = new MailNotifications(
|
$mailNotifications = new MailNotifications(
|
||||||
$this->user,
|
$this->user,
|
||||||
@ -214,4 +184,73 @@ class MailNotificationsTest extends \Test\TestCase {
|
|||||||
$this->assertSame(['lukas@owncloud.com'], $mailNotifications->sendLinkShareMail('lukas@owncloud.com', 'MyFile', 'https://owncloud.com/file/?foo=bar', 3600));
|
$this->assertSame(['lukas@owncloud.com'], $mailNotifications->sendLinkShareMail('lukas@owncloud.com', 'MyFile', 'https://owncloud.com/file/?foo=bar', 3600));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSendInternalShareMail() {
|
||||||
|
$this->setupMailerMock('TestUser shared »welcome.txt« with you', ['recipient@owncloud.com' => 'Recipient'], false);
|
||||||
|
|
||||||
|
/** @var MailNotifications | PHPUnit_Framework_MockObject_MockObject $mailNotifications */
|
||||||
|
$mailNotifications = $this->getMock('OC\Share\MailNotifications',['getItemSharedWithUser'], [
|
||||||
|
$this->user,
|
||||||
|
$this->l10n,
|
||||||
|
$this->mailer,
|
||||||
|
$this->logger,
|
||||||
|
$this->defaults]);
|
||||||
|
|
||||||
|
$mailNotifications->method('getItemSharedWithUser')
|
||||||
|
->withAnyParameters()
|
||||||
|
->willReturn([
|
||||||
|
['file_target' => '/welcome.txt']
|
||||||
|
]);
|
||||||
|
|
||||||
|
$recipient = $this->getMockBuilder('\OCP\IUser')
|
||||||
|
->disableOriginalConstructor()->getMock();
|
||||||
|
$recipient
|
||||||
|
->expects($this->once())
|
||||||
|
->method('getEMailAddress')
|
||||||
|
->willReturn('recipient@owncloud.com');
|
||||||
|
$recipient
|
||||||
|
->expects($this->once())
|
||||||
|
->method('getDisplayName')
|
||||||
|
->willReturn('Recipient');
|
||||||
|
|
||||||
|
$recipientList = [$recipient];
|
||||||
|
$result = $mailNotifications->sendInternalShareMail($recipientList, '3', 'file');
|
||||||
|
$this->assertSame([], $result);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function setupMailerMock($subject, $to, $exceptionOnSend = true) {
|
||||||
|
$message = $this->getMockBuilder('\OC\Mail\Message')
|
||||||
|
->disableOriginalConstructor()->getMock();
|
||||||
|
|
||||||
|
$message
|
||||||
|
->expects($this->once())
|
||||||
|
->method('setSubject')
|
||||||
|
->with($subject);
|
||||||
|
$message
|
||||||
|
->expects($this->once())
|
||||||
|
->method('setTo')
|
||||||
|
->with($to);
|
||||||
|
$message
|
||||||
|
->expects($this->once())
|
||||||
|
->method('setHtmlBody');
|
||||||
|
$message
|
||||||
|
->expects($this->once())
|
||||||
|
->method('setPlainBody');
|
||||||
|
$message
|
||||||
|
->expects($this->once())
|
||||||
|
->method('setFrom')
|
||||||
|
->with([\OCP\Util::getDefaultEmailAddress('sharing-noreply') => 'TestUser via UnitTestCloud']);
|
||||||
|
|
||||||
|
$this->mailer
|
||||||
|
->expects($this->once())
|
||||||
|
->method('createMessage')
|
||||||
|
->will($this->returnValue($message));
|
||||||
|
if ($exceptionOnSend) {
|
||||||
|
$this->mailer
|
||||||
|
->expects($this->once())
|
||||||
|
->method('send')
|
||||||
|
->with($message)
|
||||||
|
->will($this->throwException(new Exception('Some Exception Message')));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user