You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EMailTemplateTest.php 8.1KB

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