EMailTemplateTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. 252,
  35. 120,
  36. 'test.TestTemplate',
  37. []
  38. );
  39. }
  40. public function testEMailTemplateCustomFooter() {
  41. $this->defaults
  42. ->expects($this->any())
  43. ->method('getDefaultColorPrimary')
  44. ->willReturn('#0082c9');
  45. $this->defaults
  46. ->expects($this->any())
  47. ->method('getLogo')
  48. ->willReturn('/img/logo-mail-header.png');
  49. $this->defaults
  50. ->expects($this->any())
  51. ->method('getName')
  52. ->willReturn('TestCloud');
  53. $this->defaults
  54. ->expects($this->any())
  55. ->method('getTextColorPrimary')
  56. ->willReturn('#ffffff');
  57. $this->urlGenerator
  58. ->expects($this->once())
  59. ->method('getAbsoluteURL')
  60. ->with('/img/logo-mail-header.png')
  61. ->willReturn('https://example.org/img/logo-mail-header.png');
  62. $this->emailTemplate->addHeader();
  63. $this->emailTemplate->addHeading('Welcome aboard');
  64. $this->emailTemplate->addBodyText('Welcome to your Nextcloud account, you can add, protect, and share your data.');
  65. $this->emailTemplate->addBodyText('Your username is: abc');
  66. $this->emailTemplate->addBodyButtonGroup(
  67. 'Set your password', 'https://example.org/resetPassword/123',
  68. 'Install Client', 'https://nextcloud.com/install/#install-clients'
  69. );
  70. $this->emailTemplate->addFooter(
  71. 'TestCloud - A safe home for your data<br>This is an automatically sent email, please do not reply.'
  72. );
  73. $expectedHTML = file_get_contents(\OC::$SERVERROOT . '/tests/data/emails/new-account-email.html');
  74. $this->assertSame($expectedHTML, $this->emailTemplate->renderHtml());
  75. $expectedTXT = file_get_contents(\OC::$SERVERROOT . '/tests/data/emails/new-account-email.txt');
  76. $this->assertSame($expectedTXT, $this->emailTemplate->renderText());
  77. }
  78. public function testEMailTemplateDefaultFooter() {
  79. $this->defaults
  80. ->expects($this->any())
  81. ->method('getDefaultColorPrimary')
  82. ->willReturn('#0082c9');
  83. $this->defaults
  84. ->expects($this->any())
  85. ->method('getName')
  86. ->willReturn('TestCloud');
  87. $this->defaults
  88. ->expects($this->any())
  89. ->method('getSlogan')
  90. ->willReturn('A safe home for your data');
  91. $this->defaults
  92. ->expects($this->any())
  93. ->method('getLogo')
  94. ->willReturn('/img/logo-mail-header.png');
  95. $this->defaults
  96. ->expects($this->any())
  97. ->method('getTextColorPrimary')
  98. ->willReturn('#ffffff');
  99. $this->urlGenerator
  100. ->expects($this->once())
  101. ->method('getAbsoluteURL')
  102. ->with('/img/logo-mail-header.png')
  103. ->willReturn('https://example.org/img/logo-mail-header.png');
  104. $this->emailTemplate->addHeader();
  105. $this->emailTemplate->addHeading('Welcome aboard');
  106. $this->emailTemplate->addBodyText('Welcome to your Nextcloud account, you can add, protect, and share your data.');
  107. $this->emailTemplate->addBodyText('Your username is: abc');
  108. $this->emailTemplate->addBodyButtonGroup(
  109. 'Set your password', 'https://example.org/resetPassword/123',
  110. 'Install Client', 'https://nextcloud.com/install/#install-clients'
  111. );
  112. $this->emailTemplate->addFooter();
  113. $expectedHTML = file_get_contents(\OC::$SERVERROOT . '/tests/data/emails/new-account-email-custom.html');
  114. $this->assertSame($expectedHTML, $this->emailTemplate->renderHtml());
  115. $expectedTXT = file_get_contents(\OC::$SERVERROOT . '/tests/data/emails/new-account-email-custom.txt');
  116. $this->assertSame($expectedTXT, $this->emailTemplate->renderText());
  117. }
  118. public function testEMailTemplateSingleButton() {
  119. $this->defaults
  120. ->expects($this->any())
  121. ->method('getDefaultColorPrimary')
  122. ->willReturn('#0082c9');
  123. $this->defaults
  124. ->expects($this->any())
  125. ->method('getName')
  126. ->willReturn('TestCloud');
  127. $this->defaults
  128. ->expects($this->any())
  129. ->method('getSlogan')
  130. ->willReturn('A safe home for your data');
  131. $this->defaults
  132. ->expects($this->any())
  133. ->method('getLogo')
  134. ->willReturn('/img/logo-mail-header.png');
  135. $this->defaults
  136. ->expects($this->any())
  137. ->method('getTextColorPrimary')
  138. ->willReturn('#ffffff');
  139. $this->urlGenerator
  140. ->expects($this->once())
  141. ->method('getAbsoluteURL')
  142. ->with('/img/logo-mail-header.png')
  143. ->willReturn('https://example.org/img/logo-mail-header.png');
  144. $this->emailTemplate->addHeader();
  145. $this->emailTemplate->addHeading('Welcome aboard');
  146. $this->emailTemplate->addBodyText('Welcome to your Nextcloud account, you can add, protect, and share your data.');
  147. $this->emailTemplate->addBodyText('Your username is: abc');
  148. $this->emailTemplate->addBodyButton(
  149. 'Set your password', 'https://example.org/resetPassword/123',
  150. false
  151. );
  152. $this->emailTemplate->addFooter();
  153. $expectedHTML = file_get_contents(\OC::$SERVERROOT . '/tests/data/emails/new-account-email-single-button.html');
  154. $this->assertSame($expectedHTML, $this->emailTemplate->renderHtml());
  155. $expectedTXT = file_get_contents(\OC::$SERVERROOT . '/tests/data/emails/new-account-email-single-button.txt');
  156. $this->assertSame($expectedTXT, $this->emailTemplate->renderText());
  157. }
  158. public function testEMailTemplateAlternativePlainTexts() {
  159. $this->defaults
  160. ->expects($this->any())
  161. ->method('getDefaultColorPrimary')
  162. ->willReturn('#0082c9');
  163. $this->defaults
  164. ->expects($this->any())
  165. ->method('getName')
  166. ->willReturn('TestCloud');
  167. $this->defaults
  168. ->expects($this->any())
  169. ->method('getSlogan')
  170. ->willReturn('A safe home for your data');
  171. $this->defaults
  172. ->expects($this->any())
  173. ->method('getLogo')
  174. ->willReturn('/img/logo-mail-header.png');
  175. $this->defaults
  176. ->expects($this->any())
  177. ->method('getTextColorPrimary')
  178. ->willReturn('#ffffff');
  179. $this->urlGenerator
  180. ->expects($this->once())
  181. ->method('getAbsoluteURL')
  182. ->with('/img/logo-mail-header.png')
  183. ->willReturn('https://example.org/img/logo-mail-header.png');
  184. $this->emailTemplate->addHeader();
  185. $this->emailTemplate->addHeading('Welcome aboard', 'Welcome aboard - text');
  186. $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');
  187. $this->emailTemplate->addBodyText('Your username is: abc');
  188. $this->emailTemplate->addBodyButtonGroup(
  189. 'Set your password', 'https://example.org/resetPassword/123',
  190. 'Install Client', 'https://nextcloud.com/install/#install-clients',
  191. 'Set your password - text', 'Install Client - text'
  192. );
  193. $this->emailTemplate->addFooter();
  194. $expectedHTML = file_get_contents(\OC::$SERVERROOT . '/tests/data/emails/new-account-email-custom.html');
  195. $this->assertSame($expectedHTML, $this->emailTemplate->renderHtml());
  196. $expectedTXT = file_get_contents(\OC::$SERVERROOT . '/tests/data/emails/new-account-email-custom-text-alternative.txt');
  197. $this->assertSame($expectedTXT, $this->emailTemplate->renderText());
  198. }
  199. }