1
0

EMailTemplateTest.php 8.3 KB

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