aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorSebastianKrupinski <krupinskis05@gmail.com>2024-05-16 19:06:34 -0400
committerSebastianKrupinski <krupinskis05@gmail.com>2024-07-23 16:20:36 -0400
commitfc0b694d379efefc50ba46262bd448abd29da6ed (patch)
treee49d558e18465da8156b1705f682364e1f8e6950 /tests
parentf9d4becf60da69f272f6c5700bbdf5cb99761bc4 (diff)
downloadnextcloud-server-fc0b694d379efefc50ba46262bd448abd29da6ed.tar.gz
nextcloud-server-fc0b694d379efefc50ba46262bd448abd29da6ed.zip
feat: mail provider backend
Signed-off-by: SebastianKrupinski <krupinskis05@gmail.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/lib/Mail/Provider/AddressTest.php46
-rw-r--r--tests/lib/Mail/Provider/AttachmentTest.php71
-rw-r--r--tests/lib/Mail/Provider/ManagerTest.php189
-rw-r--r--tests/lib/Mail/Provider/MessageTest.php163
4 files changed, 469 insertions, 0 deletions
diff --git a/tests/lib/Mail/Provider/AddressTest.php b/tests/lib/Mail/Provider/AddressTest.php
new file mode 100644
index 00000000000..ee03f6f1e83
--- /dev/null
+++ b/tests/lib/Mail/Provider/AddressTest.php
@@ -0,0 +1,46 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+namespace Test\Mail\Provider;
+
+use OCP\Mail\Provider\Address;
+use Test\TestCase;
+
+class AddressTest extends TestCase {
+
+ /** @var Address&MockObject */
+ private Address $address;
+
+ protected function setUp(): void {
+ parent::setUp();
+
+ $this->address = new Address('user1@testing.com', 'User One');
+
+ }
+
+ public function testAddress(): void {
+
+ // test set by constructor
+ $this->assertEquals('user1@testing.com', $this->address->getAddress());
+ // test set by setter
+ $this->address->setAddress('user2@testing.com');
+ $this->assertEquals('user2@testing.com', $this->address->getAddress());
+
+ }
+
+ public function testLabel(): void {
+
+ // test set by constructor
+ $this->assertEquals('User One', $this->address->getLabel());
+ // test set by setter
+ $this->address->setLabel('User Two');
+ $this->assertEquals('User Two', $this->address->getLabel());
+
+ }
+
+}
diff --git a/tests/lib/Mail/Provider/AttachmentTest.php b/tests/lib/Mail/Provider/AttachmentTest.php
new file mode 100644
index 00000000000..283391650b5
--- /dev/null
+++ b/tests/lib/Mail/Provider/AttachmentTest.php
@@ -0,0 +1,71 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+namespace Test\Mail\Provider;
+
+use OCP\Mail\Provider\Attachment;
+use Test\TestCase;
+
+class AttachmentTest extends TestCase {
+
+ /** @var Attachment&MockObject */
+ private Attachment $attachment;
+
+ protected function setUp(): void {
+ parent::setUp();
+
+ $this->attachment = new Attachment(
+ 'This is the contents of a file',
+ 'example1.txt',
+ 'text/plain',
+ false
+ );
+
+ }
+
+ public function testName(): void {
+
+ // test set by constructor
+ $this->assertEquals('example1.txt', $this->attachment->getName());
+ // test set by setter
+ $this->attachment->setName('example2.txt');
+ $this->assertEquals('example2.txt', $this->attachment->getName());
+
+ }
+
+ public function testType(): void {
+
+ // test set by constructor
+ $this->assertEquals('text/plain', $this->attachment->getType());
+ // test set by setter
+ $this->attachment->setType('text/html');
+ $this->assertEquals('text/html', $this->attachment->getType());
+
+ }
+
+ public function testContents(): void {
+
+ // test set by constructor
+ $this->assertEquals('This is the contents of a file', $this->attachment->getContents());
+ // test set by setter
+ $this->attachment->setContents('This is the modified contents of a file');
+ $this->assertEquals('This is the modified contents of a file', $this->attachment->getContents());
+
+ }
+
+ public function testEmbedded(): void {
+
+ // test set by constructor
+ $this->assertEquals(false, $this->attachment->getEmbedded());
+ // test set by setter
+ $this->attachment->setEmbedded(true);
+ $this->assertEquals(true, $this->attachment->getEmbedded());
+
+ }
+
+}
diff --git a/tests/lib/Mail/Provider/ManagerTest.php b/tests/lib/Mail/Provider/ManagerTest.php
new file mode 100644
index 00000000000..2658c324521
--- /dev/null
+++ b/tests/lib/Mail/Provider/ManagerTest.php
@@ -0,0 +1,189 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+namespace Test\Mail\Provider;
+
+use OC\AppFramework\Bootstrap\Coordinator;
+use OC\AppFramework\Bootstrap\RegistrationContext;
+use OC\AppFramework\Bootstrap\ServiceRegistration;
+use OC\Mail\Provider\Manager;
+use OCP\Mail\Provider\Address;
+use OCP\Mail\Provider\IProvider;
+use OCP\Mail\Provider\IService;
+use Psr\Container\ContainerInterface;
+use Psr\Log\LoggerInterface;
+use Test\TestCase;
+
+class ManagerTest extends TestCase {
+
+ /** @var Coordinator&MockObject */
+ private Coordinator $coordinator;
+ /** @var ContainerInterface&MockObject */
+ private ContainerInterface $container;
+ /** @var LoggerInterface&MockObject */
+ private LoggerInterface $logger;
+ /** @var IProvider&MockObject */
+ private IProvider $provider;
+ /** @var IService&MockObject */
+ private IService $service;
+
+ protected function setUp(): void {
+ parent::setUp();
+
+ $this->logger = $this->createMock(LoggerInterface::class);
+
+ // construct service registration
+ $registration = $this->createMock(ServiceRegistration::class);
+ $registration
+ ->method('getService')
+ ->willReturn('Mock\Provider\MailProvider');
+ // construct registration context
+ $context = $this->createMock(RegistrationContext::class);
+ $context
+ ->method('getMailProviders')
+ ->willReturn([$registration]);
+ // construct coordinator
+ $this->coordinator = $this->createMock(Coordinator::class);
+ $this->coordinator
+ ->method('getRegistrationContext')
+ ->willReturn($context);
+
+ // construct mail service
+ $this->service = $this->createMock(IService::class);
+ $this->service
+ ->method('id')
+ ->willReturn('100');
+ $this->service
+ ->method('getLabel')
+ ->willReturn('Mock Mail Service');
+ $this->service
+ ->method('getPrimaryAddress')
+ ->willReturn((new Address('user1@testing.com', 'User One')));
+ // construct mail provider
+ $this->provider = $this->createMock(IProvider::class);
+ $this->provider
+ ->method('id')
+ ->willReturn('mock-provider');
+ $this->provider
+ ->method('label')
+ ->willReturn('Mock Provider');
+ $this->provider
+ ->method('listServices')
+ ->willReturnMap([
+ ['user0', []],
+ ['user1', [$this->service->id() => $this->service]]
+ ]);
+ $this->provider
+ ->method('findServiceById')
+ ->willReturnMap([
+ ['user0', '100', null],
+ ['user1', '100', $this->service]
+ ]);
+ $this->provider
+ ->method('findServiceByAddress')
+ ->willReturnMap([
+ ['user0', 'user0@testing.com', null],
+ ['user1', 'user1@testing.com', $this->service]
+ ]);
+ // construct container interface
+ $this->container = $this->createMock(ContainerInterface::class);
+ $this->container
+ ->method('get')
+ ->willReturnMap([
+ ['Mock\Provider\MailProvider', $this->provider]
+ ]);
+
+ }
+
+ public function testHas(): void {
+
+ // construct mail manager
+ $manager = new Manager($this->coordinator, $this->container, $this->logger);
+ // test result with providers found
+ $this->assertTrue($manager->has());
+
+ }
+
+ public function testCount(): void {
+
+ // construct mail manager
+ $manager = new Manager($this->coordinator, $this->container, $this->logger);
+ // test result with providers found
+ $this->assertGreaterThan(0, $manager->count());
+
+ }
+
+ public function testTypes(): void {
+
+ // construct mail manager
+ $manager = new Manager($this->coordinator, $this->container, $this->logger);
+ // test result with providers found
+ $this->assertEquals(['mock-provider' => 'Mock Provider'], $manager->types());
+
+ }
+
+ public function testProviders(): void {
+
+ // construct mail manager
+ $manager = new Manager($this->coordinator, $this->container, $this->logger);
+ // test result with providers found
+ $this->assertEquals([$this->provider->id() => $this->provider], $manager->providers());
+
+ }
+
+ public function testFindProviderById(): void {
+
+ // construct mail manager
+ $manager = new Manager($this->coordinator, $this->container, $this->logger);
+ // test result with providers found
+ $this->assertEquals($this->provider, $manager->findProviderById($this->provider->id()));
+
+ }
+
+ public function testServices(): void {
+
+ // construct mail manager
+ $manager = new Manager($this->coordinator, $this->container, $this->logger);
+ // test result with no services found
+ $this->assertEquals([], $manager->services('user0'));
+ // test result with services found
+ $this->assertEquals([$this->provider->id() => [$this->service->id() => $this->service]], $manager->services('user1'));
+
+ }
+
+ public function testFindServiceById(): void {
+
+ // construct mail manager
+ $manager = new Manager($this->coordinator, $this->container, $this->logger);
+ // test result with no services found and not provider specified
+ $this->assertEquals(null, $manager->findServiceById('user0', '100'));
+ // test result with no services found and provider specified
+ $this->assertEquals(null, $manager->findServiceById('user0', '100', $this->provider->id()));
+ // test result with services found and not provider specified
+ $this->assertEquals($this->service, $manager->findServiceById('user1', '100'));
+ // test result with services found and provider specified
+ $this->assertEquals($this->service, $manager->findServiceById('user1', '100', $this->provider->id()));
+
+ }
+
+ public function testFindServiceByAddress(): void {
+
+ // construct mail manager
+ $manager = new Manager($this->coordinator, $this->container, $this->logger);
+ // test result with no services found and not provider specified
+ $this->assertEquals(null, $manager->findServiceByAddress('user0', 'user0@testing.com'));
+ // test result with no services found and provider specified
+ $this->assertEquals(null, $manager->findServiceByAddress('user0', 'user0@testing.com', $this->provider->id()));
+ // test result with services found and not provider specified
+ $this->assertEquals($this->service, $manager->findServiceByAddress('user1', 'user1@testing.com'));
+ // test result with services found and provider specified
+ $this->assertEquals($this->service, $manager->findServiceByAddress('user1', 'user1@testing.com', $this->provider->id()));
+
+ }
+
+}
diff --git a/tests/lib/Mail/Provider/MessageTest.php b/tests/lib/Mail/Provider/MessageTest.php
new file mode 100644
index 00000000000..1791a03421c
--- /dev/null
+++ b/tests/lib/Mail/Provider/MessageTest.php
@@ -0,0 +1,163 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+namespace Test\Mail\Provider;
+
+use OCP\Mail\Provider\Address;
+use OCP\Mail\Provider\Attachment;
+use OCP\Mail\Provider\Message;
+use Test\TestCase;
+
+class MessageTest extends TestCase {
+
+ /** @var Message&MockObject */
+ private Message $message;
+ /** @var Address&MockObject */
+ private Address $address1;
+ /** @var Address&MockObject */
+ private Address $address2;
+ /** @var Attachment&MockObject */
+ private Attachment $attachment1;
+ /** @var Attachment&MockObject */
+ private Attachment $attachment2;
+
+ protected function setUp(): void {
+ parent::setUp();
+
+ $this->message = new Message(
+ ['id' => 'cd02ea42-feac-4863-b9d8-484d16a587ea']
+ );
+ $this->address1 = new Address(
+ 'user1@testing.com',
+ 'User One'
+ );
+ $this->address2 = new Address(
+ 'user2@testing.com',
+ 'User Two'
+ );
+ $this->attachment1 = new Attachment(
+ 'This is the contents of the first attachment',
+ 'example1.txt',
+ 'text/plain',
+ false
+ );
+ $this->attachment2 = new Attachment(
+ 'This is the contents of the second attachment',
+ 'example1.txt',
+ 'text/plain',
+ false
+ );
+
+ }
+
+ public function testId(): void {
+
+ // test set by constructor
+ $this->assertEquals('cd02ea42-feac-4863-b9d8-484d16a587ea', $this->message->id());
+
+ }
+
+ public function testFrom(): void {
+
+ // test not set
+ $this->assertNull($this->message->getFrom());
+ // test set by setter
+ $this->message->setFrom($this->address1);
+ $this->assertEquals($this->address1, $this->message->getFrom());
+
+ }
+
+ public function testReplyTo(): void {
+
+ // test not set
+ $this->assertNull($this->message->getReplyTo());
+ // test set by setter
+ $this->message->setReplyTo($this->address1);
+ $this->assertEquals($this->address1, $this->message->getReplyTo());
+
+ }
+
+ public function testTo(): void {
+
+ // test not set
+ $this->assertEquals([], $this->message->getTo());
+ // test set by setter single
+ $this->message->setTo($this->address1);
+ $this->assertEquals([$this->address1], $this->message->getTo());
+ // test set by setter multiple
+ $this->message->setTo($this->address1, $this->address2);
+ $this->assertEquals([$this->address1, $this->address2], $this->message->getTo());
+
+ }
+
+ public function testCc(): void {
+
+ // test not set
+ $this->assertEquals([], $this->message->getCc());
+ // test set by setter single
+ $this->message->setCc($this->address1);
+ $this->assertEquals([$this->address1], $this->message->getCc());
+ // test set by setter multiple
+ $this->message->setCc($this->address1, $this->address2);
+ $this->assertEquals([$this->address1, $this->address2], $this->message->getCc());
+
+ }
+
+ public function testBcc(): void {
+
+ // test not set
+ $this->assertEquals([], $this->message->getBcc());
+ // test set by setter single
+ $this->message->setBcc($this->address1);
+ $this->assertEquals([$this->address1], $this->message->getBcc());
+ // test set by setter multiple
+ $this->message->setBcc($this->address1, $this->address2);
+ $this->assertEquals([$this->address1, $this->address2], $this->message->getBcc());
+
+ }
+
+ public function testSubject(): void {
+
+ // test not set
+ $this->assertNull($this->message->getSubject());
+ // test set by setter
+ $this->message->setSubject('Testing Mail Subject');
+ $this->assertEquals('Testing Mail Subject', $this->message->getSubject());
+
+ }
+
+ public function testBody(): void {
+
+ // test not set
+ $this->assertNull($this->message->getBody());
+ // test set by setter - text body
+ $this->message->setBody('Testing Text Body', false);
+ $this->assertEquals('Testing Text Body', $this->message->getBody());
+ $this->message->setBodyPlain('Testing Text Body Again', false);
+ $this->assertEquals('Testing Text Body Again', $this->message->getBodyPlain());
+ // test set by setter - html body
+ $this->message->setBody('Testing HTML Body', true);
+ $this->assertEquals('Testing HTML Body', $this->message->getBody());
+ $this->message->setBodyHtml('Testing HTML Body Again', false);
+ $this->assertEquals('Testing HTML Body Again', $this->message->getBodyHtml());
+
+ }
+
+ public function testAttachments(): void {
+
+ // test not set
+ $this->assertEquals([], $this->message->getAttachments());
+ // test set by setter single
+ $this->message->setAttachments($this->attachment1);
+ $this->assertEquals([$this->attachment1], $this->message->getAttachments());
+ // test set by setter multiple
+ $this->message->setAttachments($this->attachment1, $this->attachment2);
+ $this->assertEquals([$this->attachment1, $this->attachment2], $this->message->getAttachments());
+
+ }
+}