1
0

EMailTemplateTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * @copyright 2017, Morris Jobke <hey@morrisjobke.de>
  4. *
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Test\Mail;
  24. use OC\Mail\EMailTemplate;
  25. use OCP\Defaults;
  26. use OCP\IL10N;
  27. use OCP\IURLGenerator;
  28. use OCP\L10N\IFactory;
  29. use Test\TestCase;
  30. class EMailTemplateTest extends TestCase {
  31. /** @var Defaults|\PHPUnit\Framework\MockObject\MockObject */
  32. private $defaults;
  33. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
  34. private $urlGenerator;
  35. /** @var IFactory|\PHPUnit\Framework\MockObject\MockObject */
  36. private $l10n;
  37. /** @var EMailTemplate */
  38. private $emailTemplate;
  39. protected function setUp(): void {
  40. parent::setUp();
  41. $this->defaults = $this->createMock(Defaults::class);
  42. $this->urlGenerator = $this->createMock(IURLGenerator::class);
  43. $this->l10n = $this->createMock(IFactory::class);
  44. $this->l10n->method('get')
  45. ->with('lib', '')
  46. ->willReturn($this->createMock(IL10N::class));
  47. $this->emailTemplate = new EMailTemplate(
  48. $this->defaults,
  49. $this->urlGenerator,
  50. $this->l10n,
  51. 'test.TestTemplate',
  52. []
  53. );
  54. }
  55. public function testEMailTemplateCustomFooter() {
  56. $this->defaults
  57. ->expects($this->any())
  58. ->method('getColorPrimary')
  59. ->willReturn('#0082c9');
  60. $this->defaults
  61. ->expects($this->any())
  62. ->method('getLogo')
  63. ->willReturn('/img/logo-mail-header.png');
  64. $this->defaults
  65. ->expects($this->any())
  66. ->method('getName')
  67. ->willReturn('TestCloud');
  68. $this->defaults
  69. ->expects($this->any())
  70. ->method('getTextColorPrimary')
  71. ->willReturn('#ffffff');
  72. $this->urlGenerator
  73. ->expects($this->once())
  74. ->method('getAbsoluteURL')
  75. ->with('/img/logo-mail-header.png')
  76. ->willReturn('https://example.org/img/logo-mail-header.png');
  77. $this->emailTemplate->addHeader();
  78. $this->emailTemplate->addHeading('Welcome aboard');
  79. $this->emailTemplate->addBodyText('Welcome to your Nextcloud account, you can add, protect, and share your data.');
  80. $this->emailTemplate->addBodyText('Your username is: abc');
  81. $this->emailTemplate->addBodyButtonGroup(
  82. 'Set your password', 'https://example.org/resetPassword/123',
  83. 'Install Client', 'https://nextcloud.com/install/#install-clients'
  84. );
  85. $this->emailTemplate->addFooter(
  86. 'TestCloud - A safe home for your data<br>This is an automatically sent email, please do not reply.'
  87. );
  88. $expectedHTML = file_get_contents(\OC::$SERVERROOT . '/tests/data/emails/new-account-email.html');
  89. $this->assertSame($expectedHTML, $this->emailTemplate->renderHtml());
  90. $expectedTXT = file_get_contents(\OC::$SERVERROOT . '/tests/data/emails/new-account-email.txt');
  91. $this->assertSame($expectedTXT, $this->emailTemplate->renderText());
  92. }
  93. public function testEMailTemplateDefaultFooter() {
  94. $this->defaults
  95. ->expects($this->any())
  96. ->method('getColorPrimary')
  97. ->willReturn('#0082c9');
  98. $this->defaults
  99. ->expects($this->any())
  100. ->method('getName')
  101. ->willReturn('TestCloud');
  102. $this->defaults
  103. ->expects($this->any())
  104. ->method('getSlogan')
  105. ->willReturn('A safe home for your data');
  106. $this->defaults
  107. ->expects($this->any())
  108. ->method('getLogo')
  109. ->willReturn('/img/logo-mail-header.png');
  110. $this->defaults
  111. ->expects($this->any())
  112. ->method('getTextColorPrimary')
  113. ->willReturn('#ffffff');
  114. $this->urlGenerator
  115. ->expects($this->once())
  116. ->method('getAbsoluteURL')
  117. ->with('/img/logo-mail-header.png')
  118. ->willReturn('https://example.org/img/logo-mail-header.png');
  119. $this->emailTemplate->addHeader();
  120. $this->emailTemplate->addHeading('Welcome aboard');
  121. $this->emailTemplate->addBodyText('Welcome to your Nextcloud account, you can add, protect, and share your data.');
  122. $this->emailTemplate->addBodyText('Your username is: abc');
  123. $this->emailTemplate->addBodyButtonGroup(
  124. 'Set your password', 'https://example.org/resetPassword/123',
  125. 'Install Client', 'https://nextcloud.com/install/#install-clients'
  126. );
  127. $this->emailTemplate->addFooter();
  128. $expectedHTML = file_get_contents(\OC::$SERVERROOT . '/tests/data/emails/new-account-email-custom.html');
  129. $this->assertSame($expectedHTML, $this->emailTemplate->renderHtml());
  130. $expectedTXT = file_get_contents(\OC::$SERVERROOT . '/tests/data/emails/new-account-email-custom.txt');
  131. $this->assertSame($expectedTXT, $this->emailTemplate->renderText());
  132. }
  133. public function testEMailTemplateSingleButton() {
  134. $this->defaults
  135. ->expects($this->any())
  136. ->method('getColorPrimary')
  137. ->willReturn('#0082c9');
  138. $this->defaults
  139. ->expects($this->any())
  140. ->method('getName')
  141. ->willReturn('TestCloud');
  142. $this->defaults
  143. ->expects($this->any())
  144. ->method('getSlogan')
  145. ->willReturn('A safe home for your data');
  146. $this->defaults
  147. ->expects($this->any())
  148. ->method('getLogo')
  149. ->willReturn('/img/logo-mail-header.png');
  150. $this->defaults
  151. ->expects($this->any())
  152. ->method('getTextColorPrimary')
  153. ->willReturn('#ffffff');
  154. $this->urlGenerator
  155. ->expects($this->once())
  156. ->method('getAbsoluteURL')
  157. ->with('/img/logo-mail-header.png')
  158. ->willReturn('https://example.org/img/logo-mail-header.png');
  159. $this->emailTemplate->addHeader();
  160. $this->emailTemplate->addHeading('Welcome aboard');
  161. $this->emailTemplate->addBodyText('Welcome to your Nextcloud account, you can add, protect, and share your data.');
  162. $this->emailTemplate->addBodyText('Your username is: abc');
  163. $this->emailTemplate->addBodyButton(
  164. 'Set your password', 'https://example.org/resetPassword/123',
  165. false
  166. );
  167. $this->emailTemplate->addFooter();
  168. $expectedHTML = file_get_contents(\OC::$SERVERROOT . '/tests/data/emails/new-account-email-single-button.html');
  169. $this->assertSame($expectedHTML, $this->emailTemplate->renderHtml());
  170. $expectedTXT = file_get_contents(\OC::$SERVERROOT . '/tests/data/emails/new-account-email-single-button.txt');
  171. $this->assertSame($expectedTXT, $this->emailTemplate->renderText());
  172. }
  173. public function testEMailTemplateAlternativePlainTexts() {
  174. $this->defaults
  175. ->expects($this->any())
  176. ->method('getColorPrimary')
  177. ->willReturn('#0082c9');
  178. $this->defaults
  179. ->expects($this->any())
  180. ->method('getName')
  181. ->willReturn('TestCloud');
  182. $this->defaults
  183. ->expects($this->any())
  184. ->method('getSlogan')
  185. ->willReturn('A safe home for your data');
  186. $this->defaults
  187. ->expects($this->any())
  188. ->method('getLogo')
  189. ->willReturn('/img/logo-mail-header.png');
  190. $this->defaults
  191. ->expects($this->any())
  192. ->method('getTextColorPrimary')
  193. ->willReturn('#ffffff');
  194. $this->urlGenerator
  195. ->expects($this->once())
  196. ->method('getAbsoluteURL')
  197. ->with('/img/logo-mail-header.png')
  198. ->willReturn('https://example.org/img/logo-mail-header.png');
  199. $this->emailTemplate->addHeader();
  200. $this->emailTemplate->addHeading('Welcome aboard', 'Welcome aboard - text');
  201. $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');
  202. $this->emailTemplate->addBodyText('Your username is: abc');
  203. $this->emailTemplate->addBodyButtonGroup(
  204. 'Set your password', 'https://example.org/resetPassword/123',
  205. 'Install Client', 'https://nextcloud.com/install/#install-clients',
  206. 'Set your password - text', 'Install Client - text'
  207. );
  208. $this->emailTemplate->addFooter();
  209. $expectedHTML = file_get_contents(\OC::$SERVERROOT . '/tests/data/emails/new-account-email-custom.html');
  210. $this->assertSame($expectedHTML, $this->emailTemplate->renderHtml());
  211. $expectedTXT = file_get_contents(\OC::$SERVERROOT . '/tests/data/emails/new-account-email-custom-text-alternative.txt');
  212. $this->assertSame($expectedTXT, $this->emailTemplate->renderText());
  213. }
  214. }