MessageTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Mail\Provider;
  8. use OCP\Mail\Provider\Address;
  9. use OCP\Mail\Provider\Attachment;
  10. use OCP\Mail\Provider\Message;
  11. use Test\TestCase;
  12. class MessageTest extends TestCase {
  13. /** @var Message&MockObject */
  14. private Message $message;
  15. /** @var Address&MockObject */
  16. private Address $address1;
  17. /** @var Address&MockObject */
  18. private Address $address2;
  19. /** @var Attachment&MockObject */
  20. private Attachment $attachment1;
  21. /** @var Attachment&MockObject */
  22. private Attachment $attachment2;
  23. protected function setUp(): void {
  24. parent::setUp();
  25. $this->message = new Message(
  26. ['id' => 'cd02ea42-feac-4863-b9d8-484d16a587ea']
  27. );
  28. $this->address1 = new Address(
  29. 'user1@testing.com',
  30. 'User One'
  31. );
  32. $this->address2 = new Address(
  33. 'user2@testing.com',
  34. 'User Two'
  35. );
  36. $this->attachment1 = new Attachment(
  37. 'This is the contents of the first attachment',
  38. 'example1.txt',
  39. 'text/plain',
  40. false
  41. );
  42. $this->attachment2 = new Attachment(
  43. 'This is the contents of the second attachment',
  44. 'example1.txt',
  45. 'text/plain',
  46. false
  47. );
  48. }
  49. public function testId(): void {
  50. // test set by constructor
  51. $this->assertEquals('cd02ea42-feac-4863-b9d8-484d16a587ea', $this->message->id());
  52. }
  53. public function testFrom(): void {
  54. // test not set
  55. $this->assertNull($this->message->getFrom());
  56. // test set by setter
  57. $this->message->setFrom($this->address1);
  58. $this->assertEquals($this->address1, $this->message->getFrom());
  59. }
  60. public function testReplyTo(): void {
  61. // test not set
  62. $this->assertNull($this->message->getReplyTo());
  63. // test set by setter
  64. $this->message->setReplyTo($this->address1);
  65. $this->assertEquals($this->address1, $this->message->getReplyTo());
  66. }
  67. public function testTo(): void {
  68. // test not set
  69. $this->assertEquals([], $this->message->getTo());
  70. // test set by setter single
  71. $this->message->setTo($this->address1);
  72. $this->assertEquals([$this->address1], $this->message->getTo());
  73. // test set by setter multiple
  74. $this->message->setTo($this->address1, $this->address2);
  75. $this->assertEquals([$this->address1, $this->address2], $this->message->getTo());
  76. }
  77. public function testCc(): void {
  78. // test not set
  79. $this->assertEquals([], $this->message->getCc());
  80. // test set by setter single
  81. $this->message->setCc($this->address1);
  82. $this->assertEquals([$this->address1], $this->message->getCc());
  83. // test set by setter multiple
  84. $this->message->setCc($this->address1, $this->address2);
  85. $this->assertEquals([$this->address1, $this->address2], $this->message->getCc());
  86. }
  87. public function testBcc(): void {
  88. // test not set
  89. $this->assertEquals([], $this->message->getBcc());
  90. // test set by setter single
  91. $this->message->setBcc($this->address1);
  92. $this->assertEquals([$this->address1], $this->message->getBcc());
  93. // test set by setter multiple
  94. $this->message->setBcc($this->address1, $this->address2);
  95. $this->assertEquals([$this->address1, $this->address2], $this->message->getBcc());
  96. }
  97. public function testSubject(): void {
  98. // test not set
  99. $this->assertNull($this->message->getSubject());
  100. // test set by setter
  101. $this->message->setSubject('Testing Mail Subject');
  102. $this->assertEquals('Testing Mail Subject', $this->message->getSubject());
  103. }
  104. public function testBody(): void {
  105. // test not set
  106. $this->assertNull($this->message->getBody());
  107. // test set by setter - text body
  108. $this->message->setBody('Testing Text Body', false);
  109. $this->assertEquals('Testing Text Body', $this->message->getBody());
  110. $this->message->setBodyPlain('Testing Text Body Again', false);
  111. $this->assertEquals('Testing Text Body Again', $this->message->getBodyPlain());
  112. // test set by setter - html body
  113. $this->message->setBody('Testing HTML Body', true);
  114. $this->assertEquals('Testing HTML Body', $this->message->getBody());
  115. $this->message->setBodyHtml('Testing HTML Body Again', false);
  116. $this->assertEquals('Testing HTML Body Again', $this->message->getBodyHtml());
  117. }
  118. public function testAttachments(): void {
  119. // test not set
  120. $this->assertEquals([], $this->message->getAttachments());
  121. // test set by setter single
  122. $this->message->setAttachments($this->attachment1);
  123. $this->assertEquals([$this->attachment1], $this->message->getAttachments());
  124. // test set by setter multiple
  125. $this->message->setAttachments($this->attachment1, $this->attachment2);
  126. $this->assertEquals([$this->attachment1, $this->attachment2], $this->message->getAttachments());
  127. }
  128. }