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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
<?php
/**
* @author Robin McCorkell <robin@mccorkell.me.uk>
*
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Files_External\Tests\Service;
use \OCA\Files_External\Service\BackendService;
class BackendServiceTest extends \Test\TestCase {
/** @var \OCP\IConfig */
protected $config;
/** @var \OCP\IL10N */
protected $l10n;
protected function setUp() {
$this->config = $this->getMock('\OCP\IConfig');
$this->l10n = $this->getMock('\OCP\IL10N');
}
/**
* @param string $class
*
* @return \OCA\Files_External\Lib\Backend\Backend
*/
protected function getBackendMock($class) {
$backend = $this->getMockBuilder('\OCA\Files_External\Lib\Backend\Backend')
->disableOriginalConstructor()
->getMock();
$backend->method('getIdentifier')->will($this->returnValue('identifier:'.$class));
$backend->method('getIdentifierAliases')->will($this->returnValue(['identifier:'.$class]));
return $backend;
}
/**
* @param string $class
*
* @return \OCA\Files_External\Lib\Auth\AuthMechanism
*/
protected function getAuthMechanismMock($class) {
$backend = $this->getMockBuilder('\OCA\Files_External\Lib\Auth\AuthMechanism')
->disableOriginalConstructor()
->getMock();
$backend->method('getIdentifier')->will($this->returnValue('identifier:'.$class));
$backend->method('getIdentifierAliases')->will($this->returnValue(['identifier:'.$class]));
return $backend;
}
public function testRegisterBackend() {
$service = new BackendService($this->config, $this->l10n);
$backend = $this->getBackendMock('\Foo\Bar');
$backendAlias = $this->getMockBuilder('\OCA\Files_External\Lib\Backend\Backend')
->disableOriginalConstructor()
->getMock();
$backendAlias->method('getIdentifierAliases')
->willReturn(['identifier_real', 'identifier_alias']);
$backendAlias->method('getIdentifier')
->willReturn('identifier_real');
$service->registerBackend($backend);
$service->registerBackend($backendAlias);
$this->assertEquals($backend, $service->getBackend('identifier:\Foo\Bar'));
$this->assertEquals($backendAlias, $service->getBackend('identifier_real'));
$this->assertEquals($backendAlias, $service->getBackend('identifier_alias'));
$backends = $service->getBackends();
$this->assertCount(2, $backends);
$this->assertArrayHasKey('identifier:\Foo\Bar', $backends);
$this->assertArrayHasKey('identifier_real', $backends);
$this->assertArrayNotHasKey('identifier_alias', $backends);
}
public function testBackendProvider() {
$service = new BackendService($this->config, $this->l10n);
$backend1 = $this->getBackendMock('\Foo\Bar');
$backend2 = $this->getBackendMock('\Bar\Foo');
$providerMock = $this->getMock('\OCA\Files_External\Lib\Config\IBackendProvider');
$providerMock->expects($this->once())
->method('getBackends')
->willReturn([$backend1, $backend2]);
$service->registerBackendProvider($providerMock);
$this->assertEquals($backend1, $service->getBackend('identifier:\Foo\Bar'));
$this->assertEquals($backend2, $service->getBackend('identifier:\Bar\Foo'));
$this->assertCount(2, $service->getBackends());
}
public function testAuthMechanismProvider() {
$service = new BackendService($this->config, $this->l10n);
$backend1 = $this->getAuthMechanismMock('\Foo\Bar');
$backend2 = $this->getAuthMechanismMock('\Bar\Foo');
$providerMock = $this->getMock('\OCA\Files_External\Lib\Config\IAuthMechanismProvider');
$providerMock->expects($this->once())
->method('getAuthMechanisms')
->willReturn([$backend1, $backend2]);
$service->registerAuthMechanismProvider($providerMock);
$this->assertEquals($backend1, $service->getAuthMechanism('identifier:\Foo\Bar'));
$this->assertEquals($backend2, $service->getAuthMechanism('identifier:\Bar\Foo'));
$this->assertCount(2, $service->getAuthMechanisms());
}
public function testMultipleBackendProviders() {
$service = new BackendService($this->config, $this->l10n);
$backend1a = $this->getBackendMock('\Foo\Bar');
$backend1b = $this->getBackendMock('\Bar\Foo');
$backend2 = $this->getBackendMock('\Dead\Beef');
$provider1Mock = $this->getMock('\OCA\Files_External\Lib\Config\IBackendProvider');
$provider1Mock->expects($this->once())
->method('getBackends')
->willReturn([$backend1a, $backend1b]);
$service->registerBackendProvider($provider1Mock);
$provider2Mock = $this->getMock('\OCA\Files_External\Lib\Config\IBackendProvider');
$provider2Mock->expects($this->once())
->method('getBackends')
->willReturn([$backend2]);
$service->registerBackendProvider($provider2Mock);
$this->assertEquals($backend1a, $service->getBackend('identifier:\Foo\Bar'));
$this->assertEquals($backend1b, $service->getBackend('identifier:\Bar\Foo'));
$this->assertEquals($backend2, $service->getBackend('identifier:\Dead\Beef'));
$this->assertCount(3, $service->getBackends());
}
public function testUserMountingBackends() {
$this->config->expects($this->exactly(2))
->method('getAppValue')
->will($this->returnValueMap([
['files_external', 'allow_user_mounting', 'yes', 'yes'],
['files_external', 'user_mounting_backends', '', 'identifier:\User\Mount\Allowed,identifier_alias']
]));
$service = new BackendService($this->config, $this->l10n);
$backendAllowed = $this->getBackendMock('\User\Mount\Allowed');
$backendAllowed->expects($this->never())
->method('removeVisibility');
$backendNotAllowed = $this->getBackendMock('\User\Mount\NotAllowed');
$backendNotAllowed->expects($this->once())
->method('removeVisibility')
->with(BackendService::VISIBILITY_PERSONAL);
$backendAlias = $this->getMockBuilder('\OCA\Files_External\Lib\Backend\Backend')
->disableOriginalConstructor()
->getMock();
$backendAlias->method('getIdentifierAliases')
->willReturn(['identifier_real', 'identifier_alias']);
$backendAlias->expects($this->never())
->method('removeVisibility');
$service->registerBackend($backendAllowed);
$service->registerBackend($backendNotAllowed);
$service->registerBackend($backendAlias);
}
public function testGetAvailableBackends() {
$service = new BackendService($this->config, $this->l10n);
$backendAvailable = $this->getBackendMock('\Backend\Available');
$backendAvailable->expects($this->once())
->method('checkDependencies')
->will($this->returnValue([]));
$backendNotAvailable = $this->getBackendMock('\Backend\NotAvailable');
$backendNotAvailable->expects($this->once())
->method('checkDependencies')
->will($this->returnValue([
$this->getMockBuilder('\OCA\Files_External\Lib\MissingDependency')
->disableOriginalConstructor()
->getMock()
]));
$service->registerBackend($backendAvailable);
$service->registerBackend($backendNotAvailable);
$availableBackends = $service->getAvailableBackends();
$this->assertArrayHasKey('identifier:\Backend\Available', $availableBackends);
$this->assertArrayNotHasKey('identifier:\Backend\NotAvailable', $availableBackends);
}
}
|