getRegistrationContext()?->registerConfigLexicon(TestConfigLexicon_I::APPID, TestConfigLexicon_I::class); $bootstrapCoordinator->getRegistrationContext()?->registerConfigLexicon(TestConfigLexicon_N::APPID, TestConfigLexicon_N::class); $bootstrapCoordinator->getRegistrationContext()?->registerConfigLexicon(TestConfigLexicon_W::APPID, TestConfigLexicon_W::class); $bootstrapCoordinator->getRegistrationContext()?->registerConfigLexicon(TestConfigLexicon_E::APPID, TestConfigLexicon_E::class); $this->appConfig = Server::get(IAppConfig::class); $this->userConfig = Server::get(IUserConfig::class); $this->configManager = Server::get(ConfigManager::class); } protected function tearDown(): void { parent::tearDown(); $this->appConfig->deleteApp(TestConfigLexicon_I::APPID); $this->appConfig->deleteApp(TestConfigLexicon_N::APPID); $this->appConfig->deleteApp(TestConfigLexicon_W::APPID); $this->appConfig->deleteApp(TestConfigLexicon_E::APPID); $this->userConfig->deleteApp(TestConfigLexicon_I::APPID); $this->userConfig->deleteApp(TestConfigLexicon_N::APPID); $this->userConfig->deleteApp(TestConfigLexicon_W::APPID); $this->userConfig->deleteApp(TestConfigLexicon_E::APPID); } public function testAppLexiconSetCorrect() { $this->assertSame(true, $this->appConfig->setValueString(TestConfigLexicon_E::APPID, 'key1', 'new_value')); $this->assertSame(true, $this->appConfig->isLazy(TestConfigLexicon_E::APPID, 'key1')); $this->assertSame(true, $this->appConfig->isSensitive(TestConfigLexicon_E::APPID, 'key1')); $this->appConfig->deleteKey(TestConfigLexicon_E::APPID, 'key1'); } public function testAppLexiconGetCorrect() { $this->assertSame('abcde', $this->appConfig->getValueString(TestConfigLexicon_E::APPID, 'key1', 'default')); } public function testAppLexiconSetIncorrectValueType() { $this->expectException(AppConfigTypeConflictException::class); $this->appConfig->setValueInt(TestConfigLexicon_E::APPID, 'key1', -1); } public function testAppLexiconGetIncorrectValueType() { $this->expectException(AppConfigTypeConflictException::class); $this->appConfig->getValueInt(TestConfigLexicon_E::APPID, 'key1'); } public function testAppLexiconIgnore() { $this->appConfig->setValueString(TestConfigLexicon_I::APPID, 'key_ignore', 'new_value'); $this->assertSame('new_value', $this->appConfig->getValueString(TestConfigLexicon_I::APPID, 'key_ignore', '')); } public function testAppLexiconNotice() { $this->appConfig->setValueString(TestConfigLexicon_N::APPID, 'key_notice', 'new_value'); $this->assertSame('new_value', $this->appConfig->getValueString(TestConfigLexicon_N::APPID, 'key_notice', '')); } public function testAppLexiconWarning() { $this->appConfig->setValueString(TestConfigLexicon_W::APPID, 'key_warning', 'new_value'); $this->assertSame('', $this->appConfig->getValueString(TestConfigLexicon_W::APPID, 'key_warning', '')); } public function testAppLexiconSetException() { $this->expectException(AppConfigUnknownKeyException::class); $this->appConfig->setValueString(TestConfigLexicon_E::APPID, 'key_exception', 'new_value'); $this->assertSame('', $this->appConfig->getValueString(TestConfigLexicon_E::APPID, 'key3', '')); } public function testAppLexiconGetException() { $this->expectException(AppConfigUnknownKeyException::class); $this->appConfig->getValueString(TestConfigLexicon_E::APPID, 'key_exception'); } public function testUserLexiconSetCorrect() { $this->assertSame(true, $this->userConfig->setValueString('user1', TestConfigLexicon_E::APPID, 'key1', 'new_value')); $this->assertSame(true, $this->userConfig->isLazy('user1', TestConfigLexicon_E::APPID, 'key1')); $this->assertSame(true, $this->userConfig->isSensitive('user1', TestConfigLexicon_E::APPID, 'key1')); $this->userConfig->deleteKey(TestConfigLexicon_E::APPID, 'key1'); } public function testUserLexiconGetCorrect() { $this->assertSame('abcde', $this->userConfig->getValueString('user1', TestConfigLexicon_E::APPID, 'key1', 'default')); } public function testUserLexiconSetIncorrectValueType() { $this->expectException(TypeConflictException::class); $this->userConfig->setValueInt('user1', TestConfigLexicon_E::APPID, 'key1', -1); } public function testUserLexiconGetIncorrectValueType() { $this->expectException(TypeConflictException::class); $this->userConfig->getValueInt('user1', TestConfigLexicon_E::APPID, 'key1'); } public function testUserLexiconIgnore() { $this->userConfig->setValueString('user1', TestConfigLexicon_I::APPID, 'key_ignore', 'new_value'); $this->assertSame('new_value', $this->userConfig->getValueString('user1', TestConfigLexicon_I::APPID, 'key_ignore', '')); } public function testUserLexiconNotice() { $this->userConfig->setValueString('user1', TestConfigLexicon_N::APPID, 'key_notice', 'new_value'); $this->assertSame('new_value', $this->userConfig->getValueString('user1', TestConfigLexicon_N::APPID, 'key_notice', '')); } public function testUserLexiconWarning() { $this->userConfig->setValueString('user1', TestConfigLexicon_W::APPID, 'key_warning', 'new_value'); $this->assertSame('', $this->userConfig->getValueString('user1', TestConfigLexicon_W::APPID, 'key_warning', '')); } public function testUserLexiconSetException() { $this->expectException(UnknownKeyException::class); $this->userConfig->setValueString('user1', TestConfigLexicon_E::APPID, 'key_exception', 'new_value'); $this->assertSame('', $this->userConfig->getValueString('user1', TestConfigLexicon_E::APPID, 'key5', '')); } public function testUserLexiconGetException() { $this->expectException(UnknownKeyException::class); $this->userConfig->getValueString('user1', TestConfigLexicon_E::APPID, 'key_exception'); } public function testAppConfigLexiconRenameSetNewValue() { $this->assertSame(12345, $this->appConfig->getValueInt(TestConfigLexicon_I::APPID, 'key3', 123)); $this->appConfig->setValueInt(TestConfigLexicon_I::APPID, 'old_key3', 994); $this->assertSame(994, $this->appConfig->getValueInt(TestConfigLexicon_I::APPID, 'key3', 123)); } public function testAppConfigLexiconRenameSetOldValuePreMigration() { $this->appConfig->ignoreLexiconAliases(true); $this->appConfig->setValueInt(TestConfigLexicon_I::APPID, 'old_key3', 993); $this->appConfig->ignoreLexiconAliases(false); $this->assertSame(12345, $this->appConfig->getValueInt(TestConfigLexicon_I::APPID, 'key3', 123)); } public function testAppConfigLexiconRenameSetOldValuePostMigration() { $this->appConfig->ignoreLexiconAliases(true); $this->appConfig->setValueInt(TestConfigLexicon_I::APPID, 'old_key3', 994); $this->appConfig->ignoreLexiconAliases(false); $this->configManager->migrateConfigLexiconKeys(TestConfigLexicon_I::APPID); $this->assertSame(994, $this->appConfig->getValueInt(TestConfigLexicon_I::APPID, 'key3', 123)); } public function testAppConfigLexiconRenameGetNewValue() { $this->appConfig->setValueInt(TestConfigLexicon_I::APPID, 'key3', 981); $this->assertSame(981, $this->appConfig->getValueInt(TestConfigLexicon_I::APPID, 'old_key3', 123)); } public function testAppConfigLexiconRenameGetOldValuePreMigration() { $this->appConfig->ignoreLexiconAliases(true); $this->appConfig->setValueInt(TestConfigLexicon_I::APPID, 'key3', 984); $this->assertSame(123, $this->appConfig->getValueInt(TestConfigLexicon_I::APPID, 'old_key3', 123)); $this->appConfig->ignoreLexiconAliases(false); } public function testAppConfigLexiconRenameGetOldValuePostMigration() { $this->appConfig->ignoreLexiconAliases(true); $this->appConfig->setValueInt(TestConfigLexicon_I::APPID, 'key3', 987); $this->appConfig->ignoreLexiconAliases(false); $this->configManager->migrateConfigLexiconKeys(TestConfigLexicon_I::APPID); $this->assertSame(987, $this->appConfig->getValueInt(TestConfigLexicon_I::APPID, 'old_key3', 123)); } public function testAppConfigLexiconRenameInvertBoolean() { $this->appConfig->ignoreLexiconAliases(true); $this->appConfig->setValueBool(TestConfigLexicon_I::APPID, 'old_key4', true); $this->appConfig->ignoreLexiconAliases(false); $this->assertSame(true, $this->appConfig->getValueBool(TestConfigLexicon_I::APPID, 'key4')); $this->configManager->migrateConfigLexiconKeys(TestConfigLexicon_I::APPID); $this->assertSame(false, $this->appConfig->getValueBool(TestConfigLexicon_I::APPID, 'key4')); } } _disabled_users_search'>artonge/fix/prevent_missing_users_from_crashing_disabled_users_search Nextcloud server, a safe home for all your data: https://github.com/nextcloud/serverwww-data
aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/Mail/Provider/ManagerTest.php
blob: d3dabc0ea75bbcbedc081acd8d7c7789c32f17b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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()));

	}

}