1
0

EMailTemplateTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\Mail;
  7. use OC\Mail\EMailTemplate;
  8. use OCP\Defaults;
  9. use OCP\IL10N;
  10. use OCP\IURLGenerator;
  11. use OCP\L10N\IFactory;
  12. use Test\TestCase;
  13. class EMailTemplateTest extends TestCase {
  14. /** @var Defaults|\PHPUnit\Framework\MockObject\MockObject */
  15. private $defaults;
  16. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
  17. private $urlGenerator;
  18. /** @var IFactory|\PHPUnit\Framework\MockObject\MockObject */
  19. private $l10n;
  20. /** @var EMailTemplate */
  21. private $emailTemplate;
  22. protected function setUp(): void {
  23. parent::setUp();
  24. $this->defaults = $this->createMock(Defaults::class);
  25. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  26. $this->l10n = $this->createMock(IFactory::class);
  27. $this->l10n->method('get')
  28. ->with('lib', '')
  29. ->willReturn($this->createMock(IL10N::class));
  30. $this->emailTemplate = new EMailTemplate(
  31. $this->defaults,
  32. $this->urlGenerator,
  33. $this->l10n,
  34. 'test.TestTemplate',
  35. []
  36. );
  37. }
  38. public function testEMailTemplateCustomFooter() {
  39. $this->defaults
  40. ->expects($this->any())
  41. ->method('getDefaultColorPrimary')
  42. ->willReturn('#0082c9');
  43. $this->defaults
  44. ->expects($this->any())
  45. ->method('getLogo')
  46. ->willReturn('/img/logo-mail-header.png');
  47. $this->defaults
  48. ->expects($this->any())
  49. ->method('getName')
  50. ->willReturn('TestCloud');
  51. $this->defaults
  52. ->expects($this->any())
  53. ->method('getTextColorPrimary')
  54. ->willReturn('#ffffff');
  55. $this->urlGenerator
  56. ->expects($this->once())
  57. ->method('getAbsoluteURL')
  58. ->with('/img/logo-mail-header.png')
  59. ->willReturn('https://example.org/img/logo-mail-header.png');
  60. $this->emailTemplate->addHeader();
  61. $this->emailTemplate->addHeading('Welcome aboard');
  62. $this->emailTemplate->addBodyText('Welcome to your Nextcloud account, you can add, protect, and share your data.');
  63. $this->emailTemplate->addBodyText('Your username is: abc');
  64. $this->emailTemplate->addBodyButtonGroup(
  65. 'Set your password', 'https://example.org/resetPassword/123',
  66. 'Install Client', 'https://nextcloud.com/install/#install-clients'
  67. );
  68. $this->emailTemplate->addFooter(
  69. 'TestCloud - A safe home for your data<br>This is an automatically sent email, please do not reply.'
  70. );
  71. $expectedHTML = file_get_contents(\OC::$SERVERROOT . '/tests/data/emails/new-account-email.html');
  72. $this->assertSame($expectedHTML, $this->emailTemplate->renderHtml());
  73. $expectedTXT = file_get_contents(\OC::$SERVERROOT . '/tests/data/emails/new-account-email.txt');
  74. $this->assertSame($expectedTXT, $this->emailTemplate->renderText());
  75. }
  76. public function testEMailTemplateDefaultFooter() {
  77. $this->defaults
  78. ->expects($this->any())
  79. ->method('getDefaultColorPrimary')
  80. ->willReturn('#0082c9');
  81. $this->defaults
  82. ->expects($this->any())
  83. ->method('getName')
  84. ->willReturn('TestCloud');
  85. $this->defaults
  86. ->expects($this->any())
  87. ->method('getSlogan')
  88. ->willReturn('A safe home for your data');
  89. $this->defaults
  90. ->expects($this->any())
  91. ->method('getLogo')
  92. ->willReturn('/img/logo-mail-header.png');
  93. $this->defaults
  94. ->expects($this->any())
  95. ->method('getTextColorPrimary')
  96. ->willReturn('#ffffff');
  97. $this->urlGenerator
  98. ->expects($this->once())
  99. ->method('getAbsoluteURL')
  100. ->with('/img/logo-mail-header.png')
  101. ->willReturn('https://example.org/img/logo-mail-header.png');
  102. $this->emailTemplate->addHeader();
  103. $this->emailTemplate->addHeading('Welcome aboard');
  104. $this->emailTemplate->addBodyText('Welcome to your Nextcloud account, you can add, protect, and share your data.');
  105. $this->emailTemplate->addBodyText('Your username is: abc');
  106. $this->emailTemplate->addBodyButtonGroup(
  107. 'Set your password', 'https://example.org/resetPassword/123',
  108. 'Install Client', 'https://nextcloud.com/install/#install-clients'
  109. );
  110. $this->emailTemplate->addFooter();
  111. $expectedHTML = file_get_contents(\OC::$SERVERROOT . '/tests/data/emails/new-account-email-custom.html');
  112. $this->assertSame($expectedHTML, $this->emailTemplate->renderHtml());
  113. $expectedTXT = file_get_contents(\OC::$SERVERROOT . '/tests/data/emails/new-account-email-custom.txt');
  114. $this->assertSame($expectedTXT, $this->emailTemplate->renderText());
  115. }
  116. public function testEMailTemplateSingleButton() {
  117. $this->defaults
  118. ->expects($this->any())
  119. ->method('getDefaultColorPrimary')
  120. ->willReturn('#0082c9');
  121. $this->defaults
  122. ->expects($this->any())
  123. ->method('getName')
  124. ->willReturn('TestCloud');
  125. $this->defaults
  126. ->expects($this->any())
  127. ->method('getSlogan')
  128. ->willReturn('A safe home for your data');
  129. $this->defaults
  130. ->expects($this->any())
  131. ->method('getLogo')
  132. ->willReturn('/img/logo-mail-header.png');
  133. $this->defaults
  134. ->expects($this->any())
  135. ->method('getTextColorPrimary')
  136. ->willReturn('#ffffff');
  137. $this->urlGenerator
  138. ->expects($this->once())
  139. ->method('getAbsoluteURL')
  140. ->with('/img/logo-mail-header.png')
  141. ->willReturn('https://example.org/img/logo-mail-header.png');
  142. $this->emailTemplate->addHeader();
  143. $this->emailTemplate->addHeading('Welcome aboard');
  144. $this->emailTemplate->addBodyText('Welcome to your Nextcloud account, you can add, protect, and share your data.');
  145. $this->emailTemplate->addBodyText('Your username is: abc');
  146. $this->emailTemplate->addBodyButton(
  147. 'Set your password', 'https://example.org/resetPassword/123',
  148. false
  149. );
  150. $this->emailTemplate->addFooter();
  151. $expectedHTML = file_get_contents(\OC::$SERVERROOT . '/tests/data/emails/new-account-email-single-button.html');
  152. $this->assertSame($expectedHTML, $this->emailTemplate->renderHtml());
  153. $expectedTXT = file_get_contents(\OC::$SERVERROOT . '/tests/data/emails/new-account-email-single-button.txt');
  154. $this->assertSame($expectedTXT, $this->emailTemplate->renderText());
  155. }
  156. public function testEMailTemplateAlternativePlainTexts() {
  157. $this->defaults
  158. ->expects($this->any())
  159. ->method('getDefaultColorPrimary')
  160. ->willReturn('#0082c9');
  161. $this->defaults
  162. ->expects($this->any())
  163. ->method('getName')
  164. ->willReturn('TestCloud');
  165. $this->defaults
  166. ->expects($this->any())
  167. ->method('getSlogan')
  168. ->willReturn('A safe home for your data');
  169. $this->defaults
  170. ->expects($this->any())
  171. ->method('getLogo')
  172. ->willReturn('/img/logo-mail-header.png');
  173. $this->defaults
  174. ->expects($this->any())
  175. ->method('getTextColorPrimary')
  176. ->willReturn('#ffffff');
  177. $this->urlGenerator
  178. ->expects($this->once())
  179. ->method('getAbsoluteURL')
  180. ->with('/img/logo-mail-header.png')
  181. ->willReturn('https://example.org/img/logo-mail-header.png');
  182. $this->emailTemplate->addHeader();
  183. $this->emailTemplate->addHeading('Welcome aboard', 'Welcome aboard - text');
  184. $this->emailTemplate->addBodyText('Welcome to your Nextcloud account, you can add, protect, and share your data.', 'Welcome to your Nextcloud account, you can add, protect, and share your data. - text');
  185. $this->emailTemplate->addBodyText('Your username is: abc');
  186. $this->emailTemplate->addBodyButtonGroup(
  187. 'Set your password', 'https://example.org/resetPassword/123',
  188. 'Install Client', 'https://nextcloud.com/install/#install-clients',
  189. 'Set your password - text', 'Install Client - text'
  190. );
  191. $this->emailTemplate->addFooter();
  192. $expectedHTML = file_get_contents(\OC::$SERVERROOT . '/tests/data/emails/new-account-email-custom.html');
  193. $this->assertSame($expectedHTML, $this->emailTemplate->renderHtml());
  194. $expectedTXT = file_get_contents(\OC::$SERVERROOT . '/tests/data/emails/new-account-email-custom-text-alternative.txt');
  195. $this->assertSame($expectedTXT, $this->emailTemplate->renderText());
  196. }
  197. }