@@ -12,61 +12,61 @@ |
||
12 | 12 | use Test\TestCase; |
13 | 13 | |
14 | 14 | class LDAPTest extends TestCase { |
15 | - private LDAP&MockObject $ldap; |
|
15 | + private LDAP&MockObject $ldap; |
|
16 | 16 | |
17 | - protected function setUp(): void { |
|
18 | - parent::setUp(); |
|
19 | - $this->ldap = $this->getMockBuilder(LDAP::class) |
|
20 | - ->onlyMethods(['invokeLDAPMethod']) |
|
21 | - ->getMock(); |
|
22 | - } |
|
17 | + protected function setUp(): void { |
|
18 | + parent::setUp(); |
|
19 | + $this->ldap = $this->getMockBuilder(LDAP::class) |
|
20 | + ->onlyMethods(['invokeLDAPMethod']) |
|
21 | + ->getMock(); |
|
22 | + } |
|
23 | 23 | |
24 | - public static function errorProvider(): array { |
|
25 | - return [ |
|
26 | - [ |
|
27 | - 'ldap_search(): Partial search results returned: Sizelimit exceeded at /srv/http/nextcloud/master/apps/user_ldap/lib/LDAP.php#292', |
|
28 | - false |
|
29 | - ], |
|
30 | - [ |
|
31 | - 'Some other error', true |
|
32 | - ] |
|
33 | - ]; |
|
34 | - } |
|
24 | + public static function errorProvider(): array { |
|
25 | + return [ |
|
26 | + [ |
|
27 | + 'ldap_search(): Partial search results returned: Sizelimit exceeded at /srv/http/nextcloud/master/apps/user_ldap/lib/LDAP.php#292', |
|
28 | + false |
|
29 | + ], |
|
30 | + [ |
|
31 | + 'Some other error', true |
|
32 | + ] |
|
33 | + ]; |
|
34 | + } |
|
35 | 35 | |
36 | - #[\PHPUnit\Framework\Attributes\DataProvider('errorProvider')] |
|
37 | - public function testSearchWithErrorHandler(string $errorMessage, bool $passThrough): void { |
|
38 | - $wasErrorHandlerCalled = false; |
|
39 | - $errorHandler = function ($number, $message, $file, $line) use (&$wasErrorHandlerCalled): void { |
|
40 | - $wasErrorHandlerCalled = true; |
|
41 | - }; |
|
36 | + #[\PHPUnit\Framework\Attributes\DataProvider('errorProvider')] |
|
37 | + public function testSearchWithErrorHandler(string $errorMessage, bool $passThrough): void { |
|
38 | + $wasErrorHandlerCalled = false; |
|
39 | + $errorHandler = function ($number, $message, $file, $line) use (&$wasErrorHandlerCalled): void { |
|
40 | + $wasErrorHandlerCalled = true; |
|
41 | + }; |
|
42 | 42 | |
43 | - set_error_handler($errorHandler); |
|
43 | + set_error_handler($errorHandler); |
|
44 | 44 | |
45 | - $this->ldap |
|
46 | - ->expects($this->once()) |
|
47 | - ->method('invokeLDAPMethod') |
|
48 | - ->with('search', $this->anything(), $this->anything(), $this->anything(), $this->anything(), $this->anything()) |
|
49 | - ->willReturnCallback(function () use ($errorMessage): void { |
|
50 | - trigger_error($errorMessage); |
|
51 | - }); |
|
45 | + $this->ldap |
|
46 | + ->expects($this->once()) |
|
47 | + ->method('invokeLDAPMethod') |
|
48 | + ->with('search', $this->anything(), $this->anything(), $this->anything(), $this->anything(), $this->anything()) |
|
49 | + ->willReturnCallback(function () use ($errorMessage): void { |
|
50 | + trigger_error($errorMessage); |
|
51 | + }); |
|
52 | 52 | |
53 | - $fakeResource = ldap_connect(); |
|
54 | - $this->ldap->search($fakeResource, 'base', 'filter', []); |
|
55 | - $this->assertSame($wasErrorHandlerCalled, $passThrough); |
|
53 | + $fakeResource = ldap_connect(); |
|
54 | + $this->ldap->search($fakeResource, 'base', 'filter', []); |
|
55 | + $this->assertSame($wasErrorHandlerCalled, $passThrough); |
|
56 | 56 | |
57 | - restore_error_handler(); |
|
58 | - } |
|
57 | + restore_error_handler(); |
|
58 | + } |
|
59 | 59 | |
60 | - public function testModReplace(): void { |
|
61 | - $link = $this->createMock(LDAP::class); |
|
62 | - $userDN = 'CN=user'; |
|
63 | - $password = 'MyPassword'; |
|
64 | - $this->ldap |
|
65 | - ->expects($this->once()) |
|
66 | - ->method('invokeLDAPMethod') |
|
67 | - ->with('mod_replace', $link, $userDN, ['userPassword' => $password]) |
|
68 | - ->willReturn(true); |
|
60 | + public function testModReplace(): void { |
|
61 | + $link = $this->createMock(LDAP::class); |
|
62 | + $userDN = 'CN=user'; |
|
63 | + $password = 'MyPassword'; |
|
64 | + $this->ldap |
|
65 | + ->expects($this->once()) |
|
66 | + ->method('invokeLDAPMethod') |
|
67 | + ->with('mod_replace', $link, $userDN, ['userPassword' => $password]) |
|
68 | + ->willReturn(true); |
|
69 | 69 | |
70 | - $this->assertTrue($this->ldap->modReplace($link, $userDN, $password)); |
|
71 | - } |
|
70 | + $this->assertTrue($this->ldap->modReplace($link, $userDN, $password)); |
|
71 | + } |
|
72 | 72 | } |
@@ -36,7 +36,7 @@ discard block |
||
36 | 36 | #[\PHPUnit\Framework\Attributes\DataProvider('errorProvider')] |
37 | 37 | public function testSearchWithErrorHandler(string $errorMessage, bool $passThrough): void { |
38 | 38 | $wasErrorHandlerCalled = false; |
39 | - $errorHandler = function ($number, $message, $file, $line) use (&$wasErrorHandlerCalled): void { |
|
39 | + $errorHandler = function($number, $message, $file, $line) use (&$wasErrorHandlerCalled): void { |
|
40 | 40 | $wasErrorHandlerCalled = true; |
41 | 41 | }; |
42 | 42 | |
@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | ->expects($this->once()) |
47 | 47 | ->method('invokeLDAPMethod') |
48 | 48 | ->with('search', $this->anything(), $this->anything(), $this->anything(), $this->anything(), $this->anything()) |
49 | - ->willReturnCallback(function () use ($errorMessage): void { |
|
49 | + ->willReturnCallback(function() use ($errorMessage): void { |
|
50 | 50 | trigger_error($errorMessage); |
51 | 51 | }); |
52 | 52 |
@@ -31,175 +31,175 @@ |
||
31 | 31 | * @package OCA\User_LDAP\Tests\User |
32 | 32 | */ |
33 | 33 | class ManagerTest extends \Test\TestCase { |
34 | - protected Access&MockObject $access; |
|
35 | - protected IConfig&MockObject $config; |
|
36 | - protected LoggerInterface&MockObject $logger; |
|
37 | - protected IAvatarManager&MockObject $avatarManager; |
|
38 | - protected Image&MockObject $image; |
|
39 | - protected IDBConnection&MockObject $dbc; |
|
40 | - protected IUserManager&MockObject $ncUserManager; |
|
41 | - protected INotificationManager&MockObject $notificationManager; |
|
42 | - protected ILDAPWrapper&MockObject $ldapWrapper; |
|
43 | - protected Connection $connection; |
|
44 | - protected IManager&MockObject $shareManager; |
|
45 | - protected Manager $manager; |
|
46 | - |
|
47 | - protected function setUp(): void { |
|
48 | - parent::setUp(); |
|
49 | - |
|
50 | - $this->access = $this->createMock(Access::class); |
|
51 | - $this->config = $this->createMock(IConfig::class); |
|
52 | - $this->logger = $this->createMock(LoggerInterface::class); |
|
53 | - $this->avatarManager = $this->createMock(IAvatarManager::class); |
|
54 | - $this->image = $this->createMock(Image::class); |
|
55 | - $this->ncUserManager = $this->createMock(IUserManager::class); |
|
56 | - $this->notificationManager = $this->createMock(INotificationManager::class); |
|
57 | - $this->ldapWrapper = $this->createMock(ILDAPWrapper::class); |
|
58 | - $this->shareManager = $this->createMock(IManager::class); |
|
59 | - |
|
60 | - $this->connection = new Connection($this->ldapWrapper, '', null); |
|
61 | - |
|
62 | - $this->access->expects($this->any()) |
|
63 | - ->method('getConnection') |
|
64 | - ->willReturn($this->connection); |
|
65 | - |
|
66 | - /** @noinspection PhpUnhandledExceptionInspection */ |
|
67 | - $this->manager = new Manager( |
|
68 | - $this->config, |
|
69 | - $this->logger, |
|
70 | - $this->avatarManager, |
|
71 | - $this->image, |
|
72 | - $this->ncUserManager, |
|
73 | - $this->notificationManager, |
|
74 | - $this->shareManager |
|
75 | - ); |
|
76 | - |
|
77 | - $this->manager->setLdapAccess($this->access); |
|
78 | - } |
|
79 | - |
|
80 | - public static function dnProvider(): array { |
|
81 | - return [ |
|
82 | - ['cn=foo,dc=foobar,dc=bar'], |
|
83 | - ['uid=foo,o=foobar,c=bar'], |
|
84 | - ['ab=cde,f=ghei,mno=pq'], |
|
85 | - ]; |
|
86 | - } |
|
87 | - |
|
88 | - #[\PHPUnit\Framework\Attributes\DataProvider('dnProvider')] |
|
89 | - public function testGetByDNExisting(string $inputDN): void { |
|
90 | - $uid = '563418fc-423b-1033-8d1c-ad5f418ee02e'; |
|
91 | - |
|
92 | - $this->access->expects($this->once()) |
|
93 | - ->method('stringResemblesDN') |
|
94 | - ->with($this->equalTo($inputDN)) |
|
95 | - ->willReturn(true); |
|
96 | - $this->access->expects($this->once()) |
|
97 | - ->method('dn2username') |
|
98 | - ->with($this->equalTo($inputDN)) |
|
99 | - ->willReturn($uid); |
|
100 | - $this->access->expects($this->never()) |
|
101 | - ->method('username2dn'); |
|
102 | - |
|
103 | - /** @noinspection PhpUnhandledExceptionInspection */ |
|
104 | - $this->manager->get($inputDN); |
|
105 | - |
|
106 | - // Now we fetch the user again. If this leads to a failing test, |
|
107 | - // runtime caching the manager is broken. |
|
108 | - /** @noinspection PhpUnhandledExceptionInspection */ |
|
109 | - $user = $this->manager->get($inputDN); |
|
110 | - |
|
111 | - $this->assertInstanceOf(User::class, $user); |
|
112 | - } |
|
113 | - |
|
114 | - public function testGetByDNNotExisting(): void { |
|
115 | - $inputDN = 'cn=gone,dc=foobar,dc=bar'; |
|
116 | - |
|
117 | - $this->access->expects($this->once()) |
|
118 | - ->method('stringResemblesDN') |
|
119 | - ->with($this->equalTo($inputDN)) |
|
120 | - ->willReturn(true); |
|
121 | - $this->access->expects($this->once()) |
|
122 | - ->method('dn2username') |
|
123 | - ->with($this->equalTo($inputDN)) |
|
124 | - ->willReturn(false); |
|
125 | - $this->access->expects($this->once()) |
|
126 | - ->method('username2dn') |
|
127 | - ->with($this->equalTo($inputDN)) |
|
128 | - ->willReturn(false); |
|
129 | - |
|
130 | - /** @noinspection PhpUnhandledExceptionInspection */ |
|
131 | - $user = $this->manager->get($inputDN); |
|
132 | - |
|
133 | - $this->assertNull($user); |
|
134 | - } |
|
135 | - |
|
136 | - public function testGetByUidExisting(): void { |
|
137 | - $dn = 'cn=foo,dc=foobar,dc=bar'; |
|
138 | - $uid = '563418fc-423b-1033-8d1c-ad5f418ee02e'; |
|
139 | - |
|
140 | - $this->access->expects($this->never()) |
|
141 | - ->method('dn2username'); |
|
142 | - $this->access->expects($this->once()) |
|
143 | - ->method('username2dn') |
|
144 | - ->with($this->equalTo($uid)) |
|
145 | - ->willReturn($dn); |
|
146 | - $this->access->expects($this->once()) |
|
147 | - ->method('stringResemblesDN') |
|
148 | - ->with($this->equalTo($uid)) |
|
149 | - ->willReturn(false); |
|
150 | - |
|
151 | - /** @noinspection PhpUnhandledExceptionInspection */ |
|
152 | - $this->manager->get($uid); |
|
153 | - |
|
154 | - // Now we fetch the user again. If this leads to a failing test, |
|
155 | - // runtime caching the manager is broken. |
|
156 | - /** @noinspection PhpUnhandledExceptionInspection */ |
|
157 | - $user = $this->manager->get($uid); |
|
158 | - |
|
159 | - $this->assertInstanceOf(User::class, $user); |
|
160 | - } |
|
161 | - |
|
162 | - public function testGetByUidNotExisting(): void { |
|
163 | - $uid = 'gone'; |
|
164 | - |
|
165 | - $this->access->expects($this->never()) |
|
166 | - ->method('dn2username'); |
|
167 | - $this->access->expects($this->exactly(1)) |
|
168 | - ->method('username2dn') |
|
169 | - ->with($this->equalTo($uid)) |
|
170 | - ->willReturn(false); |
|
171 | - |
|
172 | - /** @noinspection PhpUnhandledExceptionInspection */ |
|
173 | - $user = $this->manager->get($uid); |
|
174 | - |
|
175 | - $this->assertNull($user); |
|
176 | - } |
|
177 | - |
|
178 | - public static function attributeRequestProvider(): array { |
|
179 | - return [ |
|
180 | - [false], |
|
181 | - [true], |
|
182 | - ]; |
|
183 | - } |
|
184 | - |
|
185 | - #[\PHPUnit\Framework\Attributes\DataProvider('attributeRequestProvider')] |
|
186 | - public function testGetAttributes($minimal): void { |
|
187 | - $this->connection->setConfiguration([ |
|
188 | - 'ldapEmailAttribute' => 'MAIL', |
|
189 | - 'ldapUserAvatarRule' => 'default', |
|
190 | - 'ldapQuotaAttribute' => '', |
|
191 | - 'ldapUserDisplayName2' => 'Mail', |
|
192 | - ]); |
|
193 | - |
|
194 | - $attributes = $this->manager->getAttributes($minimal); |
|
195 | - |
|
196 | - $this->assertContains('dn', $attributes); |
|
197 | - $this->assertContains(strtolower($this->access->getConnection()->ldapEmailAttribute), $attributes); |
|
198 | - $this->assertNotContains($this->access->getConnection()->ldapEmailAttribute, $attributes); #cases check |
|
199 | - $this->assertNotContains('', $attributes); |
|
200 | - $this->assertSame(!$minimal, in_array('jpegphoto', $attributes)); |
|
201 | - $this->assertSame(!$minimal, in_array('thumbnailphoto', $attributes)); |
|
202 | - $valueCounts = array_count_values($attributes); |
|
203 | - $this->assertSame(1, $valueCounts['mail']); |
|
204 | - } |
|
34 | + protected Access&MockObject $access; |
|
35 | + protected IConfig&MockObject $config; |
|
36 | + protected LoggerInterface&MockObject $logger; |
|
37 | + protected IAvatarManager&MockObject $avatarManager; |
|
38 | + protected Image&MockObject $image; |
|
39 | + protected IDBConnection&MockObject $dbc; |
|
40 | + protected IUserManager&MockObject $ncUserManager; |
|
41 | + protected INotificationManager&MockObject $notificationManager; |
|
42 | + protected ILDAPWrapper&MockObject $ldapWrapper; |
|
43 | + protected Connection $connection; |
|
44 | + protected IManager&MockObject $shareManager; |
|
45 | + protected Manager $manager; |
|
46 | + |
|
47 | + protected function setUp(): void { |
|
48 | + parent::setUp(); |
|
49 | + |
|
50 | + $this->access = $this->createMock(Access::class); |
|
51 | + $this->config = $this->createMock(IConfig::class); |
|
52 | + $this->logger = $this->createMock(LoggerInterface::class); |
|
53 | + $this->avatarManager = $this->createMock(IAvatarManager::class); |
|
54 | + $this->image = $this->createMock(Image::class); |
|
55 | + $this->ncUserManager = $this->createMock(IUserManager::class); |
|
56 | + $this->notificationManager = $this->createMock(INotificationManager::class); |
|
57 | + $this->ldapWrapper = $this->createMock(ILDAPWrapper::class); |
|
58 | + $this->shareManager = $this->createMock(IManager::class); |
|
59 | + |
|
60 | + $this->connection = new Connection($this->ldapWrapper, '', null); |
|
61 | + |
|
62 | + $this->access->expects($this->any()) |
|
63 | + ->method('getConnection') |
|
64 | + ->willReturn($this->connection); |
|
65 | + |
|
66 | + /** @noinspection PhpUnhandledExceptionInspection */ |
|
67 | + $this->manager = new Manager( |
|
68 | + $this->config, |
|
69 | + $this->logger, |
|
70 | + $this->avatarManager, |
|
71 | + $this->image, |
|
72 | + $this->ncUserManager, |
|
73 | + $this->notificationManager, |
|
74 | + $this->shareManager |
|
75 | + ); |
|
76 | + |
|
77 | + $this->manager->setLdapAccess($this->access); |
|
78 | + } |
|
79 | + |
|
80 | + public static function dnProvider(): array { |
|
81 | + return [ |
|
82 | + ['cn=foo,dc=foobar,dc=bar'], |
|
83 | + ['uid=foo,o=foobar,c=bar'], |
|
84 | + ['ab=cde,f=ghei,mno=pq'], |
|
85 | + ]; |
|
86 | + } |
|
87 | + |
|
88 | + #[\PHPUnit\Framework\Attributes\DataProvider('dnProvider')] |
|
89 | + public function testGetByDNExisting(string $inputDN): void { |
|
90 | + $uid = '563418fc-423b-1033-8d1c-ad5f418ee02e'; |
|
91 | + |
|
92 | + $this->access->expects($this->once()) |
|
93 | + ->method('stringResemblesDN') |
|
94 | + ->with($this->equalTo($inputDN)) |
|
95 | + ->willReturn(true); |
|
96 | + $this->access->expects($this->once()) |
|
97 | + ->method('dn2username') |
|
98 | + ->with($this->equalTo($inputDN)) |
|
99 | + ->willReturn($uid); |
|
100 | + $this->access->expects($this->never()) |
|
101 | + ->method('username2dn'); |
|
102 | + |
|
103 | + /** @noinspection PhpUnhandledExceptionInspection */ |
|
104 | + $this->manager->get($inputDN); |
|
105 | + |
|
106 | + // Now we fetch the user again. If this leads to a failing test, |
|
107 | + // runtime caching the manager is broken. |
|
108 | + /** @noinspection PhpUnhandledExceptionInspection */ |
|
109 | + $user = $this->manager->get($inputDN); |
|
110 | + |
|
111 | + $this->assertInstanceOf(User::class, $user); |
|
112 | + } |
|
113 | + |
|
114 | + public function testGetByDNNotExisting(): void { |
|
115 | + $inputDN = 'cn=gone,dc=foobar,dc=bar'; |
|
116 | + |
|
117 | + $this->access->expects($this->once()) |
|
118 | + ->method('stringResemblesDN') |
|
119 | + ->with($this->equalTo($inputDN)) |
|
120 | + ->willReturn(true); |
|
121 | + $this->access->expects($this->once()) |
|
122 | + ->method('dn2username') |
|
123 | + ->with($this->equalTo($inputDN)) |
|
124 | + ->willReturn(false); |
|
125 | + $this->access->expects($this->once()) |
|
126 | + ->method('username2dn') |
|
127 | + ->with($this->equalTo($inputDN)) |
|
128 | + ->willReturn(false); |
|
129 | + |
|
130 | + /** @noinspection PhpUnhandledExceptionInspection */ |
|
131 | + $user = $this->manager->get($inputDN); |
|
132 | + |
|
133 | + $this->assertNull($user); |
|
134 | + } |
|
135 | + |
|
136 | + public function testGetByUidExisting(): void { |
|
137 | + $dn = 'cn=foo,dc=foobar,dc=bar'; |
|
138 | + $uid = '563418fc-423b-1033-8d1c-ad5f418ee02e'; |
|
139 | + |
|
140 | + $this->access->expects($this->never()) |
|
141 | + ->method('dn2username'); |
|
142 | + $this->access->expects($this->once()) |
|
143 | + ->method('username2dn') |
|
144 | + ->with($this->equalTo($uid)) |
|
145 | + ->willReturn($dn); |
|
146 | + $this->access->expects($this->once()) |
|
147 | + ->method('stringResemblesDN') |
|
148 | + ->with($this->equalTo($uid)) |
|
149 | + ->willReturn(false); |
|
150 | + |
|
151 | + /** @noinspection PhpUnhandledExceptionInspection */ |
|
152 | + $this->manager->get($uid); |
|
153 | + |
|
154 | + // Now we fetch the user again. If this leads to a failing test, |
|
155 | + // runtime caching the manager is broken. |
|
156 | + /** @noinspection PhpUnhandledExceptionInspection */ |
|
157 | + $user = $this->manager->get($uid); |
|
158 | + |
|
159 | + $this->assertInstanceOf(User::class, $user); |
|
160 | + } |
|
161 | + |
|
162 | + public function testGetByUidNotExisting(): void { |
|
163 | + $uid = 'gone'; |
|
164 | + |
|
165 | + $this->access->expects($this->never()) |
|
166 | + ->method('dn2username'); |
|
167 | + $this->access->expects($this->exactly(1)) |
|
168 | + ->method('username2dn') |
|
169 | + ->with($this->equalTo($uid)) |
|
170 | + ->willReturn(false); |
|
171 | + |
|
172 | + /** @noinspection PhpUnhandledExceptionInspection */ |
|
173 | + $user = $this->manager->get($uid); |
|
174 | + |
|
175 | + $this->assertNull($user); |
|
176 | + } |
|
177 | + |
|
178 | + public static function attributeRequestProvider(): array { |
|
179 | + return [ |
|
180 | + [false], |
|
181 | + [true], |
|
182 | + ]; |
|
183 | + } |
|
184 | + |
|
185 | + #[\PHPUnit\Framework\Attributes\DataProvider('attributeRequestProvider')] |
|
186 | + public function testGetAttributes($minimal): void { |
|
187 | + $this->connection->setConfiguration([ |
|
188 | + 'ldapEmailAttribute' => 'MAIL', |
|
189 | + 'ldapUserAvatarRule' => 'default', |
|
190 | + 'ldapQuotaAttribute' => '', |
|
191 | + 'ldapUserDisplayName2' => 'Mail', |
|
192 | + ]); |
|
193 | + |
|
194 | + $attributes = $this->manager->getAttributes($minimal); |
|
195 | + |
|
196 | + $this->assertContains('dn', $attributes); |
|
197 | + $this->assertContains(strtolower($this->access->getConnection()->ldapEmailAttribute), $attributes); |
|
198 | + $this->assertNotContains($this->access->getConnection()->ldapEmailAttribute, $attributes); #cases check |
|
199 | + $this->assertNotContains('', $attributes); |
|
200 | + $this->assertSame(!$minimal, in_array('jpegphoto', $attributes)); |
|
201 | + $this->assertSame(!$minimal, in_array('thumbnailphoto', $attributes)); |
|
202 | + $valueCounts = array_count_values($attributes); |
|
203 | + $this->assertSame(1, $valueCounts['mail']); |
|
204 | + } |
|
205 | 205 | } |
@@ -32,1148 +32,1148 @@ |
||
32 | 32 | * @package OCA\User_LDAP\Tests\User |
33 | 33 | */ |
34 | 34 | class UserTest extends \Test\TestCase { |
35 | - protected Access&MockObject $access; |
|
36 | - protected Connection&MockObject $connection; |
|
37 | - protected IConfig&MockObject $config; |
|
38 | - protected INotificationManager&MockObject $notificationManager; |
|
39 | - protected IUserManager&MockObject $userManager; |
|
40 | - protected Image&MockObject $image; |
|
41 | - protected IAvatarManager&MockObject $avatarManager; |
|
42 | - protected LoggerInterface&MockObject $logger; |
|
43 | - protected string $uid = 'alice'; |
|
44 | - protected string $dn = 'uid=alice,dc=foo,dc=bar'; |
|
45 | - protected User $user; |
|
46 | - |
|
47 | - protected function setUp(): void { |
|
48 | - parent::setUp(); |
|
49 | - |
|
50 | - $this->connection = $this->getMockBuilder(Connection::class) |
|
51 | - ->setConstructorArgs([$this->createMock(ILDAPWrapper::class)]) |
|
52 | - ->getMock(); |
|
53 | - |
|
54 | - $this->access = $this->createMock(Access::class); |
|
55 | - $this->access->connection = $this->connection; |
|
56 | - $this->access->expects($this->any()) |
|
57 | - ->method('getConnection') |
|
58 | - ->willReturn($this->connection); |
|
59 | - |
|
60 | - $this->config = $this->createMock(IConfig::class); |
|
61 | - $this->logger = $this->createMock(LoggerInterface::class); |
|
62 | - $this->avatarManager = $this->createMock(IAvatarManager::class); |
|
63 | - $this->image = $this->createMock(Image::class); |
|
64 | - $this->userManager = $this->createMock(IUserManager::class); |
|
65 | - $this->notificationManager = $this->createMock(INotificationManager::class); |
|
66 | - |
|
67 | - $this->user = new User( |
|
68 | - $this->uid, |
|
69 | - $this->dn, |
|
70 | - $this->access, |
|
71 | - $this->config, |
|
72 | - $this->image, |
|
73 | - $this->logger, |
|
74 | - $this->avatarManager, |
|
75 | - $this->userManager, |
|
76 | - $this->notificationManager |
|
77 | - ); |
|
78 | - } |
|
79 | - |
|
80 | - public function testGetDNandUsername(): void { |
|
81 | - $this->assertSame($this->dn, $this->user->getDN()); |
|
82 | - $this->assertSame($this->uid, $this->user->getUsername()); |
|
83 | - } |
|
84 | - |
|
85 | - public function testUpdateEmailProvided(): void { |
|
86 | - $this->connection->expects($this->once()) |
|
87 | - ->method('__get') |
|
88 | - ->with($this->equalTo('ldapEmailAttribute')) |
|
89 | - ->willReturn('email'); |
|
90 | - |
|
91 | - $this->access->expects($this->once()) |
|
92 | - ->method('readAttribute') |
|
93 | - ->with($this->equalTo($this->dn), |
|
94 | - $this->equalTo('email')) |
|
95 | - ->willReturn(['[email protected]']); |
|
96 | - |
|
97 | - $coreUser = $this->createMock(IUser::class); |
|
98 | - $coreUser->expects($this->once()) |
|
99 | - ->method('setSystemEMailAddress') |
|
100 | - ->with('[email protected]'); |
|
101 | - |
|
102 | - $this->userManager->expects($this->any()) |
|
103 | - ->method('get') |
|
104 | - ->willReturn($coreUser); |
|
105 | - |
|
106 | - $this->user->updateEmail(); |
|
107 | - } |
|
108 | - |
|
109 | - public function testUpdateEmailNotProvided(): void { |
|
110 | - $this->connection->expects($this->once()) |
|
111 | - ->method('__get') |
|
112 | - ->with($this->equalTo('ldapEmailAttribute')) |
|
113 | - ->willReturn('email'); |
|
114 | - |
|
115 | - $this->access->expects($this->once()) |
|
116 | - ->method('readAttribute') |
|
117 | - ->with($this->equalTo($this->dn), |
|
118 | - $this->equalTo('email')) |
|
119 | - ->willReturn(false); |
|
120 | - |
|
121 | - $this->config->expects($this->never()) |
|
122 | - ->method('setUserValue'); |
|
123 | - |
|
124 | - $this->user->updateEmail(); |
|
125 | - } |
|
126 | - |
|
127 | - public function testUpdateEmailNotConfigured(): void { |
|
128 | - $this->connection->expects($this->once()) |
|
129 | - ->method('__get') |
|
130 | - ->with($this->equalTo('ldapEmailAttribute')) |
|
131 | - ->willReturn(''); |
|
132 | - |
|
133 | - $this->access->expects($this->never()) |
|
134 | - ->method('readAttribute'); |
|
135 | - |
|
136 | - $this->config->expects($this->never()) |
|
137 | - ->method('setUserValue'); |
|
138 | - |
|
139 | - $this->user->updateEmail(); |
|
140 | - } |
|
141 | - |
|
142 | - public function testUpdateQuotaAllProvided(): void { |
|
143 | - $this->connection->expects($this->exactly(2)) |
|
144 | - ->method('__get') |
|
145 | - ->willReturnMap([ |
|
146 | - ['ldapQuotaAttribute', 'myquota'], |
|
147 | - ['ldapQuotaDefault', ''] |
|
148 | - ]); |
|
149 | - |
|
150 | - $this->access->expects($this->once()) |
|
151 | - ->method('readAttribute') |
|
152 | - ->with($this->equalTo($this->dn), |
|
153 | - $this->equalTo('myquota')) |
|
154 | - ->willReturn(['42 GB']); |
|
155 | - |
|
156 | - $coreUser = $this->createMock(IUser::class); |
|
157 | - $coreUser->expects($this->once()) |
|
158 | - ->method('setQuota') |
|
159 | - ->with('42 GB'); |
|
160 | - |
|
161 | - $this->userManager->expects($this->atLeastOnce()) |
|
162 | - ->method('get') |
|
163 | - ->with($this->uid) |
|
164 | - ->willReturn($coreUser); |
|
165 | - |
|
166 | - $this->user->updateQuota(); |
|
167 | - } |
|
168 | - |
|
169 | - public function testUpdateQuotaToDefaultAllProvided(): void { |
|
170 | - $this->connection->expects($this->exactly(2)) |
|
171 | - ->method('__get') |
|
172 | - ->willReturnMap([ |
|
173 | - ['ldapQuotaAttribute', 'myquota'], |
|
174 | - ['ldapQuotaDefault', ''] |
|
175 | - ]); |
|
176 | - |
|
177 | - $this->access->expects($this->once()) |
|
178 | - ->method('readAttribute') |
|
179 | - ->with($this->equalTo($this->dn), |
|
180 | - $this->equalTo('myquota')) |
|
181 | - ->willReturn(['default']); |
|
182 | - |
|
183 | - $coreUser = $this->createMock(IUser::class); |
|
184 | - $coreUser->expects($this->once()) |
|
185 | - ->method('setQuota') |
|
186 | - ->with('default'); |
|
187 | - |
|
188 | - $this->userManager->expects($this->once()) |
|
189 | - ->method('get') |
|
190 | - ->with($this->uid) |
|
191 | - ->willReturn($coreUser); |
|
192 | - |
|
193 | - $this->user->updateQuota(); |
|
194 | - } |
|
195 | - |
|
196 | - public function testUpdateQuotaToNoneAllProvided(): void { |
|
197 | - $this->connection->expects($this->exactly(2)) |
|
198 | - ->method('__get') |
|
199 | - ->willReturnMap([ |
|
200 | - ['ldapQuotaAttribute', 'myquota'], |
|
201 | - ['ldapQuotaDefault', ''] |
|
202 | - ]); |
|
203 | - |
|
204 | - $this->access->expects($this->once()) |
|
205 | - ->method('readAttribute') |
|
206 | - ->with($this->equalTo($this->dn), |
|
207 | - $this->equalTo('myquota')) |
|
208 | - ->willReturn(['none']); |
|
209 | - |
|
210 | - $coreUser = $this->createMock(IUser::class); |
|
211 | - $coreUser->expects($this->once()) |
|
212 | - ->method('setQuota') |
|
213 | - ->with('none'); |
|
214 | - |
|
215 | - $this->userManager->expects($this->once()) |
|
216 | - ->method('get') |
|
217 | - ->with($this->uid) |
|
218 | - ->willReturn($coreUser); |
|
219 | - |
|
220 | - $this->user->updateQuota(); |
|
221 | - } |
|
222 | - |
|
223 | - public function testUpdateQuotaDefaultProvided(): void { |
|
224 | - $this->connection->expects($this->exactly(2)) |
|
225 | - ->method('__get') |
|
226 | - ->willReturnMap([ |
|
227 | - ['ldapQuotaAttribute', 'myquota'], |
|
228 | - ['ldapQuotaDefault', '25 GB'], |
|
229 | - ]); |
|
230 | - |
|
231 | - $this->access->expects($this->once()) |
|
232 | - ->method('readAttribute') |
|
233 | - ->with($this->equalTo($this->dn), |
|
234 | - $this->equalTo('myquota')) |
|
235 | - ->willReturn(false); |
|
236 | - |
|
237 | - $coreUser = $this->createMock(IUser::class); |
|
238 | - $coreUser->expects($this->once()) |
|
239 | - ->method('setQuota') |
|
240 | - ->with('25 GB'); |
|
241 | - |
|
242 | - $this->userManager->expects($this->once()) |
|
243 | - ->method('get') |
|
244 | - ->with($this->uid) |
|
245 | - ->willReturn($coreUser); |
|
246 | - |
|
247 | - $this->user->updateQuota(); |
|
248 | - } |
|
249 | - |
|
250 | - public function testUpdateQuotaIndividualProvided(): void { |
|
251 | - $this->connection->expects($this->exactly(2)) |
|
252 | - ->method('__get') |
|
253 | - ->willReturnMap([ |
|
254 | - ['ldapQuotaAttribute', 'myquota'], |
|
255 | - ['ldapQuotaDefault', ''] |
|
256 | - ]); |
|
257 | - |
|
258 | - $this->access->expects($this->once()) |
|
259 | - ->method('readAttribute') |
|
260 | - ->with($this->equalTo($this->dn), |
|
261 | - $this->equalTo('myquota')) |
|
262 | - ->willReturn(['27 GB']); |
|
263 | - |
|
264 | - $coreUser = $this->createMock(IUser::class); |
|
265 | - $coreUser->expects($this->once()) |
|
266 | - ->method('setQuota') |
|
267 | - ->with('27 GB'); |
|
268 | - |
|
269 | - $this->userManager->expects($this->once()) |
|
270 | - ->method('get') |
|
271 | - ->with($this->uid) |
|
272 | - ->willReturn($coreUser); |
|
273 | - |
|
274 | - $this->user->updateQuota(); |
|
275 | - } |
|
276 | - |
|
277 | - public function testUpdateQuotaNoneProvided(): void { |
|
278 | - $this->connection->expects($this->exactly(2)) |
|
279 | - ->method('__get') |
|
280 | - ->willReturnMap([ |
|
281 | - ['ldapQuotaAttribute', 'myquota'], |
|
282 | - ['ldapQuotaDefault', ''] |
|
283 | - ]); |
|
284 | - |
|
285 | - $this->access->expects($this->once()) |
|
286 | - ->method('readAttribute') |
|
287 | - ->with($this->equalTo($this->dn), |
|
288 | - $this->equalTo('myquota')) |
|
289 | - ->willReturn(false); |
|
290 | - |
|
291 | - $coreUser = $this->createMock(IUser::class); |
|
292 | - $coreUser->expects($this->never()) |
|
293 | - ->method('setQuota'); |
|
294 | - |
|
295 | - $this->userManager->expects($this->never()) |
|
296 | - ->method('get') |
|
297 | - ->with($this->uid); |
|
298 | - |
|
299 | - $this->config->expects($this->never()) |
|
300 | - ->method('setUserValue'); |
|
301 | - |
|
302 | - $this->user->updateQuota(); |
|
303 | - } |
|
304 | - |
|
305 | - public function testUpdateQuotaNoneConfigured(): void { |
|
306 | - $this->connection->expects($this->exactly(2)) |
|
307 | - ->method('__get') |
|
308 | - ->willReturnMap([ |
|
309 | - ['ldapQuotaAttribute', ''], |
|
310 | - ['ldapQuotaDefault', ''] |
|
311 | - ]); |
|
312 | - |
|
313 | - $coreUser = $this->createMock(IUser::class); |
|
314 | - $coreUser->expects($this->never()) |
|
315 | - ->method('setQuota'); |
|
316 | - |
|
317 | - $this->userManager->expects($this->never()) |
|
318 | - ->method('get'); |
|
319 | - |
|
320 | - $this->access->expects($this->never()) |
|
321 | - ->method('readAttribute'); |
|
322 | - |
|
323 | - $this->config->expects($this->never()) |
|
324 | - ->method('setUserValue'); |
|
325 | - |
|
326 | - $this->user->updateQuota(); |
|
327 | - } |
|
328 | - |
|
329 | - public function testUpdateQuotaFromValue(): void { |
|
330 | - $readQuota = '19 GB'; |
|
331 | - |
|
332 | - $this->connection->expects($this->exactly(2)) |
|
333 | - ->method('__get') |
|
334 | - ->willReturnMap([ |
|
335 | - ['ldapQuotaAttribute', 'myquota'], |
|
336 | - ['ldapQuotaDefault', ''] |
|
337 | - ]); |
|
338 | - |
|
339 | - $this->access->expects($this->never()) |
|
340 | - ->method('readAttribute'); |
|
341 | - |
|
342 | - $user = $this->createMock(IUser::class); |
|
343 | - $user->expects($this->once()) |
|
344 | - ->method('setQuota') |
|
345 | - ->with($readQuota); |
|
346 | - |
|
347 | - $this->userManager->expects($this->once()) |
|
348 | - ->method('get') |
|
349 | - ->with($this->uid) |
|
350 | - ->willReturn($user); |
|
351 | - |
|
352 | - $this->user->updateQuota($readQuota); |
|
353 | - } |
|
354 | - |
|
355 | - /** |
|
356 | - * Unparseable quota will fallback to use the LDAP default |
|
357 | - */ |
|
358 | - public function testUpdateWrongQuotaAllProvided(): void { |
|
359 | - $this->connection->expects($this->exactly(2)) |
|
360 | - ->method('__get') |
|
361 | - ->willReturnMap([ |
|
362 | - ['ldapQuotaAttribute', 'myquota'], |
|
363 | - ['ldapQuotaDefault', '23 GB'] |
|
364 | - ]); |
|
365 | - |
|
366 | - $this->access->expects($this->once()) |
|
367 | - ->method('readAttribute') |
|
368 | - ->with($this->equalTo($this->dn), |
|
369 | - $this->equalTo('myquota')) |
|
370 | - ->willReturn(['42 GBwos']); |
|
371 | - |
|
372 | - $coreUser = $this->createMock(IUser::class); |
|
373 | - $coreUser->expects($this->once()) |
|
374 | - ->method('setQuota') |
|
375 | - ->with('23 GB'); |
|
376 | - |
|
377 | - $this->userManager->expects($this->once()) |
|
378 | - ->method('get') |
|
379 | - ->with($this->uid) |
|
380 | - ->willReturn($coreUser); |
|
381 | - |
|
382 | - $this->user->updateQuota(); |
|
383 | - } |
|
384 | - |
|
385 | - /** |
|
386 | - * No user quota and wrong default will set 'default' as quota |
|
387 | - */ |
|
388 | - public function testUpdateWrongDefaultQuotaProvided(): void { |
|
389 | - $this->connection->expects($this->exactly(2)) |
|
390 | - ->method('__get') |
|
391 | - ->willReturnMap([ |
|
392 | - ['ldapQuotaAttribute', 'myquota'], |
|
393 | - ['ldapQuotaDefault', '23 GBwowowo'] |
|
394 | - ]); |
|
395 | - |
|
396 | - $this->access->expects($this->once()) |
|
397 | - ->method('readAttribute') |
|
398 | - ->with($this->equalTo($this->dn), |
|
399 | - $this->equalTo('myquota')) |
|
400 | - ->willReturn(false); |
|
401 | - |
|
402 | - $coreUser = $this->createMock(IUser::class); |
|
403 | - $coreUser->expects($this->never()) |
|
404 | - ->method('setQuota'); |
|
405 | - |
|
406 | - $this->userManager->expects($this->never()) |
|
407 | - ->method('get'); |
|
408 | - |
|
409 | - $this->user->updateQuota(); |
|
410 | - } |
|
411 | - |
|
412 | - /** |
|
413 | - * Wrong user quota and wrong default will set 'default' as quota |
|
414 | - */ |
|
415 | - public function testUpdateWrongQuotaAndDefaultAllProvided(): void { |
|
416 | - $this->connection->expects($this->exactly(2)) |
|
417 | - ->method('__get') |
|
418 | - ->willReturnMap([ |
|
419 | - ['ldapQuotaAttribute', 'myquota'], |
|
420 | - ['ldapQuotaDefault', '23 GBwowowo'] |
|
421 | - ]); |
|
422 | - |
|
423 | - $this->access->expects($this->once()) |
|
424 | - ->method('readAttribute') |
|
425 | - ->with($this->equalTo($this->dn), |
|
426 | - $this->equalTo('myquota')) |
|
427 | - ->willReturn(['23 flush']); |
|
428 | - |
|
429 | - $coreUser = $this->createMock(IUser::class); |
|
430 | - $coreUser->expects($this->never()) |
|
431 | - ->method('setQuota'); |
|
432 | - |
|
433 | - $this->userManager->expects($this->never()) |
|
434 | - ->method('get'); |
|
435 | - |
|
436 | - $this->user->updateQuota(); |
|
437 | - } |
|
438 | - |
|
439 | - /** |
|
440 | - * No quota attribute set and wrong default will set 'default' as quota |
|
441 | - */ |
|
442 | - public function testUpdateWrongDefaultQuotaNotProvided(): void { |
|
443 | - $this->connection->expects($this->exactly(2)) |
|
444 | - ->method('__get') |
|
445 | - ->willReturnMap([ |
|
446 | - ['ldapQuotaAttribute', ''], |
|
447 | - ['ldapQuotaDefault', '23 GBwowowo'] |
|
448 | - ]); |
|
449 | - |
|
450 | - $this->access->expects($this->never()) |
|
451 | - ->method('readAttribute'); |
|
452 | - |
|
453 | - $coreUser = $this->createMock(IUser::class); |
|
454 | - $coreUser->expects($this->never()) |
|
455 | - ->method('setQuota'); |
|
456 | - |
|
457 | - $this->userManager->expects($this->never()) |
|
458 | - ->method('get'); |
|
459 | - |
|
460 | - $this->user->updateQuota(); |
|
461 | - } |
|
462 | - |
|
463 | - //the testUpdateAvatar series also implicitly tests getAvatarImage |
|
464 | - public function XtestUpdateAvatarJpegPhotoProvided() { |
|
465 | - $this->access->expects($this->once()) |
|
466 | - ->method('readAttribute') |
|
467 | - ->with($this->equalTo($this->dn), |
|
468 | - $this->equalTo('jpegphoto')) |
|
469 | - ->willReturn(['this is a photo']); |
|
470 | - |
|
471 | - $this->image->expects($this->once()) |
|
472 | - ->method('loadFromBase64') |
|
473 | - ->willReturn('imageResource'); |
|
474 | - $this->image->expects($this->once()) |
|
475 | - ->method('valid') |
|
476 | - ->willReturn(true); |
|
477 | - $this->image->expects($this->once()) |
|
478 | - ->method('width') |
|
479 | - ->willReturn(128); |
|
480 | - $this->image->expects($this->once()) |
|
481 | - ->method('height') |
|
482 | - ->willReturn(128); |
|
483 | - $this->image->expects($this->once()) |
|
484 | - ->method('centerCrop') |
|
485 | - ->willReturn(true); |
|
486 | - $this->image->expects($this->once()) |
|
487 | - ->method('data') |
|
488 | - ->willReturn('this is a photo'); |
|
489 | - |
|
490 | - $this->config->expects($this->once()) |
|
491 | - ->method('getUserValue') |
|
492 | - ->with($this->uid, 'user_ldap', 'lastAvatarChecksum', '') |
|
493 | - ->willReturn(''); |
|
494 | - $this->config->expects($this->once()) |
|
495 | - ->method('setUserValue') |
|
496 | - ->with($this->uid, 'user_ldap', 'lastAvatarChecksum', md5('this is a photo')); |
|
497 | - |
|
498 | - $avatar = $this->createMock(IAvatar::class); |
|
499 | - $avatar->expects($this->once()) |
|
500 | - ->method('set') |
|
501 | - ->with($this->image); |
|
502 | - |
|
503 | - $this->avatarManager->expects($this->once()) |
|
504 | - ->method('getAvatar') |
|
505 | - ->with($this->equalTo($this->uid)) |
|
506 | - ->willReturn($avatar); |
|
507 | - |
|
508 | - $this->connection->expects($this->any()) |
|
509 | - ->method('resolveRule') |
|
510 | - ->with('avatar') |
|
511 | - ->willReturn(['jpegphoto', 'thumbnailphoto']); |
|
512 | - |
|
513 | - $this->user->updateAvatar(); |
|
514 | - } |
|
515 | - |
|
516 | - public function testUpdateAvatarKnownJpegPhotoProvided(): void { |
|
517 | - $this->access->expects($this->once()) |
|
518 | - ->method('readAttribute') |
|
519 | - ->with($this->equalTo($this->dn), |
|
520 | - $this->equalTo('jpegphoto')) |
|
521 | - ->willReturn(['this is a photo']); |
|
522 | - |
|
523 | - $this->image->expects($this->once()) |
|
524 | - ->method('loadFromBase64') |
|
525 | - ->willReturn('imageResource'); |
|
526 | - $this->image->expects($this->never()) |
|
527 | - ->method('valid'); |
|
528 | - $this->image->expects($this->never()) |
|
529 | - ->method('width'); |
|
530 | - $this->image->expects($this->never()) |
|
531 | - ->method('height'); |
|
532 | - $this->image->expects($this->never()) |
|
533 | - ->method('centerCrop'); |
|
534 | - $this->image->expects($this->once()) |
|
535 | - ->method('data') |
|
536 | - ->willReturn('this is a photo'); |
|
537 | - |
|
538 | - $this->config->expects($this->once()) |
|
539 | - ->method('getUserValue') |
|
540 | - ->with($this->uid, 'user_ldap', 'lastAvatarChecksum', '') |
|
541 | - ->willReturn(md5('this is a photo')); |
|
542 | - $this->config->expects($this->never()) |
|
543 | - ->method('setUserValue'); |
|
544 | - |
|
545 | - $avatar = $this->createMock(IAvatar::class); |
|
546 | - $avatar->expects($this->never()) |
|
547 | - ->method('set'); |
|
548 | - $avatar->expects($this->any()) |
|
549 | - ->method('exists') |
|
550 | - ->willReturn(true); |
|
551 | - $avatar->expects($this->any()) |
|
552 | - ->method('isCustomAvatar') |
|
553 | - ->willReturn(true); |
|
554 | - |
|
555 | - $this->avatarManager->expects($this->any()) |
|
556 | - ->method('getAvatar') |
|
557 | - ->with($this->uid) |
|
558 | - ->willReturn($avatar); |
|
559 | - |
|
560 | - $this->connection->expects($this->any()) |
|
561 | - ->method('resolveRule') |
|
562 | - ->with('avatar') |
|
563 | - ->willReturn(['jpegphoto', 'thumbnailphoto']); |
|
564 | - |
|
565 | - $this->assertTrue($this->user->updateAvatar()); |
|
566 | - } |
|
567 | - |
|
568 | - public function XtestUpdateAvatarThumbnailPhotoProvided() { |
|
569 | - $this->access->expects($this->any()) |
|
570 | - ->method('readAttribute') |
|
571 | - ->willReturnCallback(function ($dn, $attr) { |
|
572 | - if ($dn === $this->dn |
|
573 | - && $attr === 'jpegphoto') { |
|
574 | - return false; |
|
575 | - } elseif ($dn === $this->dn |
|
576 | - && $attr === 'thumbnailphoto') { |
|
577 | - return ['this is a photo']; |
|
578 | - } |
|
579 | - return null; |
|
580 | - }); |
|
581 | - |
|
582 | - $this->image->expects($this->once()) |
|
583 | - ->method('loadFromBase64') |
|
584 | - ->willReturn('imageResource'); |
|
585 | - $this->image->expects($this->once()) |
|
586 | - ->method('valid') |
|
587 | - ->willReturn(true); |
|
588 | - $this->image->expects($this->once()) |
|
589 | - ->method('width') |
|
590 | - ->willReturn(128); |
|
591 | - $this->image->expects($this->once()) |
|
592 | - ->method('height') |
|
593 | - ->willReturn(128); |
|
594 | - $this->image->expects($this->once()) |
|
595 | - ->method('centerCrop') |
|
596 | - ->willReturn(true); |
|
597 | - $this->image->expects($this->once()) |
|
598 | - ->method('data') |
|
599 | - ->willReturn('this is a photo'); |
|
600 | - |
|
601 | - $this->config->expects($this->once()) |
|
602 | - ->method('getUserValue') |
|
603 | - ->with($this->uid, 'user_ldap', 'lastAvatarChecksum', '') |
|
604 | - ->willReturn(''); |
|
605 | - $this->config->expects($this->once()) |
|
606 | - ->method('setUserValue') |
|
607 | - ->with($this->uid, 'user_ldap', 'lastAvatarChecksum', md5('this is a photo')); |
|
608 | - |
|
609 | - $avatar = $this->createMock(IAvatar::class); |
|
610 | - $avatar->expects($this->once()) |
|
611 | - ->method('set') |
|
612 | - ->with($this->image); |
|
613 | - |
|
614 | - $this->avatarManager->expects($this->once()) |
|
615 | - ->method('getAvatar') |
|
616 | - ->with($this->equalTo($this->uid)) |
|
617 | - ->willReturn($avatar); |
|
618 | - |
|
619 | - $this->connection->expects($this->any()) |
|
620 | - ->method('resolveRule') |
|
621 | - ->with('avatar') |
|
622 | - ->willReturn(['jpegphoto', 'thumbnailphoto']); |
|
623 | - |
|
624 | - $this->user->updateAvatar(); |
|
625 | - } |
|
626 | - |
|
627 | - public function testUpdateAvatarCorruptPhotoProvided(): void { |
|
628 | - $this->access->expects($this->any()) |
|
629 | - ->method('readAttribute') |
|
630 | - ->willReturnCallback(function ($dn, $attr) { |
|
631 | - if ($dn === $this->dn |
|
632 | - && $attr === 'jpegphoto') { |
|
633 | - return false; |
|
634 | - } elseif ($dn === $this->dn |
|
635 | - && $attr === 'thumbnailphoto') { |
|
636 | - return ['this is a photo']; |
|
637 | - } |
|
638 | - return null; |
|
639 | - }); |
|
640 | - |
|
641 | - $this->image->expects($this->once()) |
|
642 | - ->method('loadFromBase64') |
|
643 | - ->willReturn(false); |
|
644 | - $this->image->expects($this->never()) |
|
645 | - ->method('valid'); |
|
646 | - $this->image->expects($this->never()) |
|
647 | - ->method('width'); |
|
648 | - $this->image->expects($this->never()) |
|
649 | - ->method('height'); |
|
650 | - $this->image->expects($this->never()) |
|
651 | - ->method('centerCrop'); |
|
652 | - $this->image->expects($this->never()) |
|
653 | - ->method('data'); |
|
654 | - |
|
655 | - $this->config->expects($this->never()) |
|
656 | - ->method('getUserValue'); |
|
657 | - $this->config->expects($this->never()) |
|
658 | - ->method('setUserValue'); |
|
659 | - |
|
660 | - $avatar = $this->createMock(IAvatar::class); |
|
661 | - $avatar->expects($this->never()) |
|
662 | - ->method('set'); |
|
663 | - |
|
664 | - $this->avatarManager->expects($this->never()) |
|
665 | - ->method('getAvatar'); |
|
666 | - |
|
667 | - $this->connection->expects($this->any()) |
|
668 | - ->method('resolveRule') |
|
669 | - ->with('avatar') |
|
670 | - ->willReturn(['jpegphoto', 'thumbnailphoto']); |
|
671 | - |
|
672 | - $this->user->updateAvatar(); |
|
673 | - } |
|
674 | - |
|
675 | - public function XtestUpdateAvatarUnsupportedThumbnailPhotoProvided() { |
|
676 | - $this->access->expects($this->any()) |
|
677 | - ->method('readAttribute') |
|
678 | - ->willReturnCallback(function ($dn, $attr) { |
|
679 | - if ($dn === $this->dn |
|
680 | - && $attr === 'jpegphoto') { |
|
681 | - return false; |
|
682 | - } elseif ($dn === $this->dn |
|
683 | - && $attr === 'thumbnailphoto') { |
|
684 | - return ['this is a photo']; |
|
685 | - } |
|
686 | - return null; |
|
687 | - }); |
|
688 | - |
|
689 | - $this->image->expects($this->once()) |
|
690 | - ->method('loadFromBase64') |
|
691 | - ->willReturn('imageResource'); |
|
692 | - $this->image->expects($this->once()) |
|
693 | - ->method('valid') |
|
694 | - ->willReturn(true); |
|
695 | - $this->image->expects($this->once()) |
|
696 | - ->method('width') |
|
697 | - ->willReturn(128); |
|
698 | - $this->image->expects($this->once()) |
|
699 | - ->method('height') |
|
700 | - ->willReturn(128); |
|
701 | - $this->image->expects($this->once()) |
|
702 | - ->method('centerCrop') |
|
703 | - ->willReturn(true); |
|
704 | - $this->image->expects($this->once()) |
|
705 | - ->method('data') |
|
706 | - ->willReturn('this is a photo'); |
|
707 | - |
|
708 | - $this->config->expects($this->once()) |
|
709 | - ->method('getUserValue') |
|
710 | - ->with($this->uid, 'user_ldap', 'lastAvatarChecksum', '') |
|
711 | - ->willReturn(''); |
|
712 | - $this->config->expects($this->never()) |
|
713 | - ->method('setUserValue'); |
|
714 | - |
|
715 | - $avatar = $this->createMock(IAvatar::class); |
|
716 | - $avatar->expects($this->once()) |
|
717 | - ->method('set') |
|
718 | - ->with($this->image) |
|
719 | - ->willThrowException(new \Exception()); |
|
720 | - |
|
721 | - $this->avatarManager->expects($this->once()) |
|
722 | - ->method('getAvatar') |
|
723 | - ->with($this->equalTo($this->uid)) |
|
724 | - ->willReturn($avatar); |
|
725 | - |
|
726 | - $this->connection->expects($this->any()) |
|
727 | - ->method('resolveRule') |
|
728 | - ->with('avatar') |
|
729 | - ->willReturn(['jpegphoto', 'thumbnailphoto']); |
|
730 | - |
|
731 | - $this->assertFalse($this->user->updateAvatar()); |
|
732 | - } |
|
733 | - |
|
734 | - public function testUpdateAvatarNotProvided(): void { |
|
735 | - $this->access->expects($this->any()) |
|
736 | - ->method('readAttribute') |
|
737 | - ->willReturnCallback(function ($dn, $attr) { |
|
738 | - if ($dn === $this->dn |
|
739 | - && $attr === 'jpegPhoto') { |
|
740 | - return false; |
|
741 | - } elseif ($dn === $this->dn |
|
742 | - && $attr === 'thumbnailPhoto') { |
|
743 | - return false; |
|
744 | - } |
|
745 | - return null; |
|
746 | - }); |
|
747 | - |
|
748 | - $this->image->expects($this->never()) |
|
749 | - ->method('valid'); |
|
750 | - $this->image->expects($this->never()) |
|
751 | - ->method('width'); |
|
752 | - $this->image->expects($this->never()) |
|
753 | - ->method('height'); |
|
754 | - $this->image->expects($this->never()) |
|
755 | - ->method('centerCrop'); |
|
756 | - $this->image->expects($this->never()) |
|
757 | - ->method('data'); |
|
758 | - |
|
759 | - $this->config->expects($this->never()) |
|
760 | - ->method('getUserValue'); |
|
761 | - $this->config->expects($this->never()) |
|
762 | - ->method('setUserValue'); |
|
763 | - |
|
764 | - $this->avatarManager->expects($this->never()) |
|
765 | - ->method('getAvatar'); |
|
766 | - |
|
767 | - $this->connection->expects($this->any()) |
|
768 | - ->method('resolveRule') |
|
769 | - ->with('avatar') |
|
770 | - ->willReturn(['jpegphoto', 'thumbnailphoto']); |
|
771 | - |
|
772 | - $this->user->updateAvatar(); |
|
773 | - } |
|
774 | - |
|
775 | - public static function extStorageHomeDataProvider(): array { |
|
776 | - return [ |
|
777 | - [ 'myFolder', null ], |
|
778 | - [ '', null, false ], |
|
779 | - [ 'myFolder', 'myFolder' ], |
|
780 | - ]; |
|
781 | - } |
|
782 | - |
|
783 | - #[\PHPUnit\Framework\Attributes\DataProvider('extStorageHomeDataProvider')] |
|
784 | - public function testUpdateExtStorageHome(string $expected, ?string $valueFromLDAP = null, bool $isSet = true): void { |
|
785 | - if ($valueFromLDAP === null) { |
|
786 | - $this->connection->expects($this->once()) |
|
787 | - ->method('__get') |
|
788 | - ->willReturnMap([ |
|
789 | - ['ldapExtStorageHomeAttribute', 'homeDirectory'], |
|
790 | - ]); |
|
791 | - |
|
792 | - $return = []; |
|
793 | - if ($isSet) { |
|
794 | - $return[] = $expected; |
|
795 | - } |
|
796 | - $this->access->expects($this->once()) |
|
797 | - ->method('readAttribute') |
|
798 | - ->with($this->dn, 'homeDirectory') |
|
799 | - ->willReturn($return); |
|
800 | - } |
|
801 | - |
|
802 | - if ($expected !== '') { |
|
803 | - $this->config->expects($this->once()) |
|
804 | - ->method('setUserValue') |
|
805 | - ->with($this->uid, 'user_ldap', 'extStorageHome', $expected); |
|
806 | - } else { |
|
807 | - $this->config->expects($this->once()) |
|
808 | - ->method('deleteUserValue') |
|
809 | - ->with($this->uid, 'user_ldap', 'extStorageHome'); |
|
810 | - } |
|
811 | - |
|
812 | - $actual = $this->user->updateExtStorageHome($valueFromLDAP); |
|
813 | - $this->assertSame($expected, $actual); |
|
814 | - } |
|
815 | - |
|
816 | - public function testMarkLogin(): void { |
|
817 | - $this->config->expects($this->once()) |
|
818 | - ->method('setUserValue') |
|
819 | - ->with($this->equalTo($this->uid), |
|
820 | - $this->equalTo('user_ldap'), |
|
821 | - $this->equalTo(User::USER_PREFKEY_FIRSTLOGIN), |
|
822 | - $this->equalTo(1)) |
|
823 | - ->willReturn(true); |
|
824 | - |
|
825 | - $this->user->markLogin(); |
|
826 | - } |
|
827 | - |
|
828 | - public function testGetAvatarImageProvided(): void { |
|
829 | - $this->access->expects($this->once()) |
|
830 | - ->method('readAttribute') |
|
831 | - ->with($this->equalTo($this->dn), |
|
832 | - $this->equalTo('jpegphoto')) |
|
833 | - ->willReturn(['this is a photo']); |
|
834 | - $this->connection->expects($this->any()) |
|
835 | - ->method('resolveRule') |
|
836 | - ->with('avatar') |
|
837 | - ->willReturn(['jpegphoto', 'thumbnailphoto']); |
|
838 | - |
|
839 | - $photo = $this->user->getAvatarImage(); |
|
840 | - $this->assertSame('this is a photo', $photo); |
|
841 | - //make sure readAttribute is not called again but the already fetched |
|
842 | - //photo is returned |
|
843 | - $this->user->getAvatarImage(); |
|
844 | - } |
|
845 | - |
|
846 | - public function testGetAvatarImageDisabled(): void { |
|
847 | - $this->access->expects($this->never()) |
|
848 | - ->method('readAttribute') |
|
849 | - ->with($this->equalTo($this->dn), $this->anything()); |
|
850 | - $this->connection->expects($this->any()) |
|
851 | - ->method('resolveRule') |
|
852 | - ->with('avatar') |
|
853 | - ->willReturn([]); |
|
854 | - |
|
855 | - $this->assertFalse($this->user->getAvatarImage()); |
|
856 | - } |
|
857 | - |
|
858 | - public static function imageDataProvider(): array { |
|
859 | - return [ |
|
860 | - [ false, false ], |
|
861 | - [ 'corruptData', false ], |
|
862 | - [ 'validData', true ], |
|
863 | - ]; |
|
864 | - } |
|
865 | - |
|
866 | - public function testProcessAttributes(): void { |
|
867 | - $requiredMethods = [ |
|
868 | - 'updateQuota', |
|
869 | - 'updateEmail', |
|
870 | - 'composeAndStoreDisplayName', |
|
871 | - 'storeLDAPUserName', |
|
872 | - 'getHomePath', |
|
873 | - 'updateAvatar', |
|
874 | - 'updateExtStorageHome', |
|
875 | - ]; |
|
876 | - |
|
877 | - /** @var User&MockObject $userMock */ |
|
878 | - $userMock = $this->getMockBuilder(User::class) |
|
879 | - ->setConstructorArgs([ |
|
880 | - $this->uid, |
|
881 | - $this->dn, |
|
882 | - $this->access, |
|
883 | - $this->config, |
|
884 | - $this->image, |
|
885 | - $this->logger, |
|
886 | - $this->avatarManager, |
|
887 | - $this->userManager, |
|
888 | - $this->notificationManager |
|
889 | - ]) |
|
890 | - ->onlyMethods($requiredMethods) |
|
891 | - ->getMock(); |
|
892 | - |
|
893 | - $this->connection->setConfiguration([ |
|
894 | - 'homeFolderNamingRule' => 'homeDirectory' |
|
895 | - ]); |
|
896 | - $this->connection->expects($this->any()) |
|
897 | - ->method('__get') |
|
898 | - ->willReturnCallback(function ($name) { |
|
899 | - if ($name === 'homeFolderNamingRule') { |
|
900 | - return 'attr:homeDirectory'; |
|
901 | - } |
|
902 | - return $name; |
|
903 | - }); |
|
904 | - $this->connection->expects($this->any()) |
|
905 | - ->method('resolveRule') |
|
906 | - ->with('avatar') |
|
907 | - ->willReturn(['jpegphoto', 'thumbnailphoto']); |
|
908 | - |
|
909 | - $record = [ |
|
910 | - strtolower($this->connection->ldapQuotaAttribute) => ['4096'], |
|
911 | - strtolower($this->connection->ldapEmailAttribute) => ['[email protected]'], |
|
912 | - strtolower($this->connection->ldapUserDisplayName) => ['Aaaaalice'], |
|
913 | - strtolower($this->connection->ldapExtStorageHomeAttribute) => ['homeDirectory'], |
|
914 | - 'uid' => [$this->uid], |
|
915 | - 'homedirectory' => ['Alice\'s Folder'], |
|
916 | - 'memberof' => ['cn=groupOne', 'cn=groupTwo'], |
|
917 | - 'jpegphoto' => ['here be an image'] |
|
918 | - ]; |
|
919 | - |
|
920 | - foreach ($requiredMethods as $method) { |
|
921 | - $userMock->expects($this->once()) |
|
922 | - ->method($method); |
|
923 | - } |
|
924 | - \OC_Hook::clear();//disconnect irrelevant hooks |
|
925 | - $userMock->processAttributes($record); |
|
926 | - /** @noinspection PhpUnhandledExceptionInspection */ |
|
927 | - \OC_Hook::emit('OC_User', 'post_login', ['uid' => $this->uid]); |
|
928 | - } |
|
929 | - |
|
930 | - public static function emptyHomeFolderAttributeValueProvider(): array { |
|
931 | - return [ |
|
932 | - 'empty' => [''], |
|
933 | - 'prefixOnly' => ['attr:'], |
|
934 | - ]; |
|
935 | - } |
|
936 | - |
|
937 | - #[\PHPUnit\Framework\Attributes\DataProvider('emptyHomeFolderAttributeValueProvider')] |
|
938 | - public function testGetHomePathNotConfigured(string $attributeValue): void { |
|
939 | - $this->connection->expects($this->any()) |
|
940 | - ->method('__get') |
|
941 | - ->with($this->equalTo('homeFolderNamingRule')) |
|
942 | - ->willReturn($attributeValue); |
|
943 | - |
|
944 | - $this->access->expects($this->never()) |
|
945 | - ->method('readAttribute'); |
|
946 | - |
|
947 | - $this->config->expects($this->never()) |
|
948 | - ->method('getAppValue'); |
|
949 | - |
|
950 | - /** @noinspection PhpUnhandledExceptionInspection */ |
|
951 | - $this->assertFalse($this->user->getHomePath()); |
|
952 | - } |
|
953 | - |
|
954 | - public function testGetHomePathConfiguredNotAvailableAllowed(): void { |
|
955 | - $this->connection->expects($this->any()) |
|
956 | - ->method('__get') |
|
957 | - ->with($this->equalTo('homeFolderNamingRule')) |
|
958 | - ->willReturn('attr:foobar'); |
|
959 | - |
|
960 | - $this->access->expects($this->once()) |
|
961 | - ->method('readAttribute') |
|
962 | - ->willReturn(false); |
|
963 | - |
|
964 | - $this->access->expects($this->once()) |
|
965 | - ->method('username2dn') |
|
966 | - ->willReturn($this->dn); |
|
967 | - |
|
968 | - // asks for "enforce_home_folder_naming_rule" |
|
969 | - $this->config->expects($this->once()) |
|
970 | - ->method('getAppValue') |
|
971 | - ->willReturn(false); |
|
972 | - |
|
973 | - /** @noinspection PhpUnhandledExceptionInspection */ |
|
974 | - $this->assertFalse($this->user->getHomePath()); |
|
975 | - } |
|
976 | - |
|
977 | - |
|
978 | - public function testGetHomePathConfiguredNotAvailableNotAllowed(): void { |
|
979 | - $this->expectException(\Exception::class); |
|
980 | - |
|
981 | - $this->connection->expects($this->any()) |
|
982 | - ->method('__get') |
|
983 | - ->with($this->equalTo('homeFolderNamingRule')) |
|
984 | - ->willReturn('attr:foobar'); |
|
985 | - |
|
986 | - $this->access->expects($this->once()) |
|
987 | - ->method('readAttribute') |
|
988 | - ->willReturn(false); |
|
989 | - |
|
990 | - $this->access->expects($this->once()) |
|
991 | - ->method('username2dn') |
|
992 | - ->willReturn($this->dn); |
|
993 | - |
|
994 | - // asks for "enforce_home_folder_naming_rule" |
|
995 | - $this->config->expects($this->once()) |
|
996 | - ->method('getAppValue') |
|
997 | - ->willReturn(true); |
|
998 | - |
|
999 | - $this->user->getHomePath(); |
|
1000 | - } |
|
1001 | - |
|
1002 | - public static function displayNameProvider(): array { |
|
1003 | - return [ |
|
1004 | - ['Roland Deschain', '', 'Roland Deschain', false], |
|
1005 | - ['Roland Deschain', '', 'Roland Deschain', true], |
|
1006 | - ['Roland Deschain', '[email protected]', 'Roland Deschain ([email protected])', false], |
|
1007 | - ['Roland Deschain', '[email protected]', 'Roland Deschain ([email protected])', true], |
|
1008 | - ]; |
|
1009 | - } |
|
1010 | - |
|
1011 | - #[\PHPUnit\Framework\Attributes\DataProvider('displayNameProvider')] |
|
1012 | - public function testComposeAndStoreDisplayName(string $part1, string $part2, string $expected, bool $expectTriggerChange): void { |
|
1013 | - $this->config->expects($this->once()) |
|
1014 | - ->method('setUserValue'); |
|
1015 | - $oldName = $expectTriggerChange ? 'xxGunslingerxx' : null; |
|
1016 | - $this->config->expects($this->once()) |
|
1017 | - ->method('getUserValue') |
|
1018 | - ->with($this->user->getUsername(), 'user_ldap', 'displayName', null) |
|
1019 | - ->willReturn($oldName); |
|
1020 | - |
|
1021 | - $ncUserObj = $this->createMock(\OC\User\User::class); |
|
1022 | - if ($expectTriggerChange) { |
|
1023 | - $ncUserObj->expects($this->once()) |
|
1024 | - ->method('triggerChange') |
|
1025 | - ->with('displayName', $expected); |
|
1026 | - } else { |
|
1027 | - $ncUserObj->expects($this->never()) |
|
1028 | - ->method('triggerChange'); |
|
1029 | - } |
|
1030 | - $this->userManager->expects($this->once()) |
|
1031 | - ->method('get') |
|
1032 | - ->willReturn($ncUserObj); |
|
1033 | - |
|
1034 | - $displayName = $this->user->composeAndStoreDisplayName($part1, $part2); |
|
1035 | - $this->assertSame($expected, $displayName); |
|
1036 | - } |
|
1037 | - |
|
1038 | - public function testComposeAndStoreDisplayNameNoOverwrite(): void { |
|
1039 | - $displayName = 'Randall Flagg'; |
|
1040 | - $this->config->expects($this->never()) |
|
1041 | - ->method('setUserValue'); |
|
1042 | - $this->config->expects($this->once()) |
|
1043 | - ->method('getUserValue') |
|
1044 | - ->willReturn($displayName); |
|
1045 | - |
|
1046 | - $this->userManager->expects($this->never()) |
|
1047 | - ->method('get'); // Implicit: no triggerChange can be called |
|
1048 | - |
|
1049 | - $composedDisplayName = $this->user->composeAndStoreDisplayName($displayName); |
|
1050 | - $this->assertSame($composedDisplayName, $displayName); |
|
1051 | - } |
|
1052 | - |
|
1053 | - public function testHandlePasswordExpiryWarningDefaultPolicy(): void { |
|
1054 | - $this->connection->expects($this->any()) |
|
1055 | - ->method('__get') |
|
1056 | - ->willReturnCallback(function ($name) { |
|
1057 | - if ($name === 'ldapDefaultPPolicyDN') { |
|
1058 | - return 'cn=default,ou=policies,dc=foo,dc=bar'; |
|
1059 | - } |
|
1060 | - if ($name === 'turnOnPasswordChange') { |
|
1061 | - return '1'; |
|
1062 | - } |
|
1063 | - return $name; |
|
1064 | - }); |
|
1065 | - |
|
1066 | - $this->access->expects($this->any()) |
|
1067 | - ->method('search') |
|
1068 | - ->willReturnCallback(function ($filter, $base) { |
|
1069 | - if ($base === $this->dn) { |
|
1070 | - return [ |
|
1071 | - [ |
|
1072 | - 'pwdchangedtime' => [(new \DateTime())->sub(new \DateInterval('P28D'))->format('Ymdhis') . 'Z'], |
|
1073 | - 'pwdgraceusetime' => [], |
|
1074 | - ], |
|
1075 | - ]; |
|
1076 | - } |
|
1077 | - if ($base === 'cn=default,ou=policies,dc=foo,dc=bar') { |
|
1078 | - return [ |
|
1079 | - [ |
|
1080 | - 'pwdmaxage' => ['2592000'], |
|
1081 | - 'pwdexpirewarning' => ['2591999'], |
|
1082 | - ], |
|
1083 | - ]; |
|
1084 | - } |
|
1085 | - return []; |
|
1086 | - }); |
|
1087 | - |
|
1088 | - $notification = $this->getMockBuilder(INotification::class) |
|
1089 | - ->disableOriginalConstructor() |
|
1090 | - ->getMock(); |
|
1091 | - $notification->expects($this->any()) |
|
1092 | - ->method('setApp') |
|
1093 | - ->willReturn($notification); |
|
1094 | - $notification->expects($this->any()) |
|
1095 | - ->method('setUser') |
|
1096 | - ->willReturn($notification); |
|
1097 | - $notification->expects($this->any()) |
|
1098 | - ->method('setObject') |
|
1099 | - ->willReturn($notification); |
|
1100 | - $notification->expects($this->any()) |
|
1101 | - ->method('setDateTime') |
|
1102 | - ->willReturn($notification); |
|
1103 | - |
|
1104 | - $this->notificationManager->expects($this->exactly(2)) |
|
1105 | - ->method('createNotification') |
|
1106 | - ->willReturn($notification); |
|
1107 | - $this->notificationManager->expects($this->exactly(1)) |
|
1108 | - ->method('notify'); |
|
1109 | - |
|
1110 | - \OC_Hook::clear();//disconnect irrelevant hooks |
|
1111 | - Util::connectHook('OC_User', 'post_login', $this->user, 'handlePasswordExpiry'); |
|
1112 | - /** @noinspection PhpUnhandledExceptionInspection */ |
|
1113 | - \OC_Hook::emit('OC_User', 'post_login', ['uid' => $this->uid]); |
|
1114 | - } |
|
1115 | - |
|
1116 | - public function testHandlePasswordExpiryWarningCustomPolicy(): void { |
|
1117 | - $this->connection->expects($this->any()) |
|
1118 | - ->method('__get') |
|
1119 | - ->willReturnCallback(function ($name) { |
|
1120 | - if ($name === 'ldapDefaultPPolicyDN') { |
|
1121 | - return 'cn=default,ou=policies,dc=foo,dc=bar'; |
|
1122 | - } |
|
1123 | - if ($name === 'turnOnPasswordChange') { |
|
1124 | - return '1'; |
|
1125 | - } |
|
1126 | - return $name; |
|
1127 | - }); |
|
1128 | - |
|
1129 | - $this->access->expects($this->any()) |
|
1130 | - ->method('search') |
|
1131 | - ->willReturnCallback(function ($filter, $base) { |
|
1132 | - if ($base === $this->dn) { |
|
1133 | - return [ |
|
1134 | - [ |
|
1135 | - 'pwdpolicysubentry' => ['cn=custom,ou=policies,dc=foo,dc=bar'], |
|
1136 | - 'pwdchangedtime' => [(new \DateTime())->sub(new \DateInterval('P28D'))->format('Ymdhis') . 'Z'], |
|
1137 | - 'pwdgraceusetime' => [], |
|
1138 | - ] |
|
1139 | - ]; |
|
1140 | - } |
|
1141 | - if ($base === 'cn=custom,ou=policies,dc=foo,dc=bar') { |
|
1142 | - return [ |
|
1143 | - [ |
|
1144 | - 'pwdmaxage' => ['2592000'], |
|
1145 | - 'pwdexpirewarning' => ['2591999'], |
|
1146 | - ] |
|
1147 | - ]; |
|
1148 | - } |
|
1149 | - return []; |
|
1150 | - }); |
|
1151 | - |
|
1152 | - $notification = $this->getMockBuilder(INotification::class) |
|
1153 | - ->disableOriginalConstructor() |
|
1154 | - ->getMock(); |
|
1155 | - $notification->expects($this->any()) |
|
1156 | - ->method('setApp') |
|
1157 | - ->willReturn($notification); |
|
1158 | - $notification->expects($this->any()) |
|
1159 | - ->method('setUser') |
|
1160 | - ->willReturn($notification); |
|
1161 | - $notification->expects($this->any()) |
|
1162 | - ->method('setObject') |
|
1163 | - ->willReturn($notification); |
|
1164 | - $notification->expects($this->any()) |
|
1165 | - ->method('setDateTime') |
|
1166 | - ->willReturn($notification); |
|
1167 | - |
|
1168 | - $this->notificationManager->expects($this->exactly(2)) |
|
1169 | - ->method('createNotification') |
|
1170 | - ->willReturn($notification); |
|
1171 | - $this->notificationManager->expects($this->exactly(1)) |
|
1172 | - ->method('notify'); |
|
1173 | - |
|
1174 | - \OC_Hook::clear();//disconnect irrelevant hooks |
|
1175 | - Util::connectHook('OC_User', 'post_login', $this->user, 'handlePasswordExpiry'); |
|
1176 | - /** @noinspection PhpUnhandledExceptionInspection */ |
|
1177 | - \OC_Hook::emit('OC_User', 'post_login', ['uid' => $this->uid]); |
|
1178 | - } |
|
35 | + protected Access&MockObject $access; |
|
36 | + protected Connection&MockObject $connection; |
|
37 | + protected IConfig&MockObject $config; |
|
38 | + protected INotificationManager&MockObject $notificationManager; |
|
39 | + protected IUserManager&MockObject $userManager; |
|
40 | + protected Image&MockObject $image; |
|
41 | + protected IAvatarManager&MockObject $avatarManager; |
|
42 | + protected LoggerInterface&MockObject $logger; |
|
43 | + protected string $uid = 'alice'; |
|
44 | + protected string $dn = 'uid=alice,dc=foo,dc=bar'; |
|
45 | + protected User $user; |
|
46 | + |
|
47 | + protected function setUp(): void { |
|
48 | + parent::setUp(); |
|
49 | + |
|
50 | + $this->connection = $this->getMockBuilder(Connection::class) |
|
51 | + ->setConstructorArgs([$this->createMock(ILDAPWrapper::class)]) |
|
52 | + ->getMock(); |
|
53 | + |
|
54 | + $this->access = $this->createMock(Access::class); |
|
55 | + $this->access->connection = $this->connection; |
|
56 | + $this->access->expects($this->any()) |
|
57 | + ->method('getConnection') |
|
58 | + ->willReturn($this->connection); |
|
59 | + |
|
60 | + $this->config = $this->createMock(IConfig::class); |
|
61 | + $this->logger = $this->createMock(LoggerInterface::class); |
|
62 | + $this->avatarManager = $this->createMock(IAvatarManager::class); |
|
63 | + $this->image = $this->createMock(Image::class); |
|
64 | + $this->userManager = $this->createMock(IUserManager::class); |
|
65 | + $this->notificationManager = $this->createMock(INotificationManager::class); |
|
66 | + |
|
67 | + $this->user = new User( |
|
68 | + $this->uid, |
|
69 | + $this->dn, |
|
70 | + $this->access, |
|
71 | + $this->config, |
|
72 | + $this->image, |
|
73 | + $this->logger, |
|
74 | + $this->avatarManager, |
|
75 | + $this->userManager, |
|
76 | + $this->notificationManager |
|
77 | + ); |
|
78 | + } |
|
79 | + |
|
80 | + public function testGetDNandUsername(): void { |
|
81 | + $this->assertSame($this->dn, $this->user->getDN()); |
|
82 | + $this->assertSame($this->uid, $this->user->getUsername()); |
|
83 | + } |
|
84 | + |
|
85 | + public function testUpdateEmailProvided(): void { |
|
86 | + $this->connection->expects($this->once()) |
|
87 | + ->method('__get') |
|
88 | + ->with($this->equalTo('ldapEmailAttribute')) |
|
89 | + ->willReturn('email'); |
|
90 | + |
|
91 | + $this->access->expects($this->once()) |
|
92 | + ->method('readAttribute') |
|
93 | + ->with($this->equalTo($this->dn), |
|
94 | + $this->equalTo('email')) |
|
95 | + ->willReturn(['[email protected]']); |
|
96 | + |
|
97 | + $coreUser = $this->createMock(IUser::class); |
|
98 | + $coreUser->expects($this->once()) |
|
99 | + ->method('setSystemEMailAddress') |
|
100 | + ->with('[email protected]'); |
|
101 | + |
|
102 | + $this->userManager->expects($this->any()) |
|
103 | + ->method('get') |
|
104 | + ->willReturn($coreUser); |
|
105 | + |
|
106 | + $this->user->updateEmail(); |
|
107 | + } |
|
108 | + |
|
109 | + public function testUpdateEmailNotProvided(): void { |
|
110 | + $this->connection->expects($this->once()) |
|
111 | + ->method('__get') |
|
112 | + ->with($this->equalTo('ldapEmailAttribute')) |
|
113 | + ->willReturn('email'); |
|
114 | + |
|
115 | + $this->access->expects($this->once()) |
|
116 | + ->method('readAttribute') |
|
117 | + ->with($this->equalTo($this->dn), |
|
118 | + $this->equalTo('email')) |
|
119 | + ->willReturn(false); |
|
120 | + |
|
121 | + $this->config->expects($this->never()) |
|
122 | + ->method('setUserValue'); |
|
123 | + |
|
124 | + $this->user->updateEmail(); |
|
125 | + } |
|
126 | + |
|
127 | + public function testUpdateEmailNotConfigured(): void { |
|
128 | + $this->connection->expects($this->once()) |
|
129 | + ->method('__get') |
|
130 | + ->with($this->equalTo('ldapEmailAttribute')) |
|
131 | + ->willReturn(''); |
|
132 | + |
|
133 | + $this->access->expects($this->never()) |
|
134 | + ->method('readAttribute'); |
|
135 | + |
|
136 | + $this->config->expects($this->never()) |
|
137 | + ->method('setUserValue'); |
|
138 | + |
|
139 | + $this->user->updateEmail(); |
|
140 | + } |
|
141 | + |
|
142 | + public function testUpdateQuotaAllProvided(): void { |
|
143 | + $this->connection->expects($this->exactly(2)) |
|
144 | + ->method('__get') |
|
145 | + ->willReturnMap([ |
|
146 | + ['ldapQuotaAttribute', 'myquota'], |
|
147 | + ['ldapQuotaDefault', ''] |
|
148 | + ]); |
|
149 | + |
|
150 | + $this->access->expects($this->once()) |
|
151 | + ->method('readAttribute') |
|
152 | + ->with($this->equalTo($this->dn), |
|
153 | + $this->equalTo('myquota')) |
|
154 | + ->willReturn(['42 GB']); |
|
155 | + |
|
156 | + $coreUser = $this->createMock(IUser::class); |
|
157 | + $coreUser->expects($this->once()) |
|
158 | + ->method('setQuota') |
|
159 | + ->with('42 GB'); |
|
160 | + |
|
161 | + $this->userManager->expects($this->atLeastOnce()) |
|
162 | + ->method('get') |
|
163 | + ->with($this->uid) |
|
164 | + ->willReturn($coreUser); |
|
165 | + |
|
166 | + $this->user->updateQuota(); |
|
167 | + } |
|
168 | + |
|
169 | + public function testUpdateQuotaToDefaultAllProvided(): void { |
|
170 | + $this->connection->expects($this->exactly(2)) |
|
171 | + ->method('__get') |
|
172 | + ->willReturnMap([ |
|
173 | + ['ldapQuotaAttribute', 'myquota'], |
|
174 | + ['ldapQuotaDefault', ''] |
|
175 | + ]); |
|
176 | + |
|
177 | + $this->access->expects($this->once()) |
|
178 | + ->method('readAttribute') |
|
179 | + ->with($this->equalTo($this->dn), |
|
180 | + $this->equalTo('myquota')) |
|
181 | + ->willReturn(['default']); |
|
182 | + |
|
183 | + $coreUser = $this->createMock(IUser::class); |
|
184 | + $coreUser->expects($this->once()) |
|
185 | + ->method('setQuota') |
|
186 | + ->with('default'); |
|
187 | + |
|
188 | + $this->userManager->expects($this->once()) |
|
189 | + ->method('get') |
|
190 | + ->with($this->uid) |
|
191 | + ->willReturn($coreUser); |
|
192 | + |
|
193 | + $this->user->updateQuota(); |
|
194 | + } |
|
195 | + |
|
196 | + public function testUpdateQuotaToNoneAllProvided(): void { |
|
197 | + $this->connection->expects($this->exactly(2)) |
|
198 | + ->method('__get') |
|
199 | + ->willReturnMap([ |
|
200 | + ['ldapQuotaAttribute', 'myquota'], |
|
201 | + ['ldapQuotaDefault', ''] |
|
202 | + ]); |
|
203 | + |
|
204 | + $this->access->expects($this->once()) |
|
205 | + ->method('readAttribute') |
|
206 | + ->with($this->equalTo($this->dn), |
|
207 | + $this->equalTo('myquota')) |
|
208 | + ->willReturn(['none']); |
|
209 | + |
|
210 | + $coreUser = $this->createMock(IUser::class); |
|
211 | + $coreUser->expects($this->once()) |
|
212 | + ->method('setQuota') |
|
213 | + ->with('none'); |
|
214 | + |
|
215 | + $this->userManager->expects($this->once()) |
|
216 | + ->method('get') |
|
217 | + ->with($this->uid) |
|
218 | + ->willReturn($coreUser); |
|
219 | + |
|
220 | + $this->user->updateQuota(); |
|
221 | + } |
|
222 | + |
|
223 | + public function testUpdateQuotaDefaultProvided(): void { |
|
224 | + $this->connection->expects($this->exactly(2)) |
|
225 | + ->method('__get') |
|
226 | + ->willReturnMap([ |
|
227 | + ['ldapQuotaAttribute', 'myquota'], |
|
228 | + ['ldapQuotaDefault', '25 GB'], |
|
229 | + ]); |
|
230 | + |
|
231 | + $this->access->expects($this->once()) |
|
232 | + ->method('readAttribute') |
|
233 | + ->with($this->equalTo($this->dn), |
|
234 | + $this->equalTo('myquota')) |
|
235 | + ->willReturn(false); |
|
236 | + |
|
237 | + $coreUser = $this->createMock(IUser::class); |
|
238 | + $coreUser->expects($this->once()) |
|
239 | + ->method('setQuota') |
|
240 | + ->with('25 GB'); |
|
241 | + |
|
242 | + $this->userManager->expects($this->once()) |
|
243 | + ->method('get') |
|
244 | + ->with($this->uid) |
|
245 | + ->willReturn($coreUser); |
|
246 | + |
|
247 | + $this->user->updateQuota(); |
|
248 | + } |
|
249 | + |
|
250 | + public function testUpdateQuotaIndividualProvided(): void { |
|
251 | + $this->connection->expects($this->exactly(2)) |
|
252 | + ->method('__get') |
|
253 | + ->willReturnMap([ |
|
254 | + ['ldapQuotaAttribute', 'myquota'], |
|
255 | + ['ldapQuotaDefault', ''] |
|
256 | + ]); |
|
257 | + |
|
258 | + $this->access->expects($this->once()) |
|
259 | + ->method('readAttribute') |
|
260 | + ->with($this->equalTo($this->dn), |
|
261 | + $this->equalTo('myquota')) |
|
262 | + ->willReturn(['27 GB']); |
|
263 | + |
|
264 | + $coreUser = $this->createMock(IUser::class); |
|
265 | + $coreUser->expects($this->once()) |
|
266 | + ->method('setQuota') |
|
267 | + ->with('27 GB'); |
|
268 | + |
|
269 | + $this->userManager->expects($this->once()) |
|
270 | + ->method('get') |
|
271 | + ->with($this->uid) |
|
272 | + ->willReturn($coreUser); |
|
273 | + |
|
274 | + $this->user->updateQuota(); |
|
275 | + } |
|
276 | + |
|
277 | + public function testUpdateQuotaNoneProvided(): void { |
|
278 | + $this->connection->expects($this->exactly(2)) |
|
279 | + ->method('__get') |
|
280 | + ->willReturnMap([ |
|
281 | + ['ldapQuotaAttribute', 'myquota'], |
|
282 | + ['ldapQuotaDefault', ''] |
|
283 | + ]); |
|
284 | + |
|
285 | + $this->access->expects($this->once()) |
|
286 | + ->method('readAttribute') |
|
287 | + ->with($this->equalTo($this->dn), |
|
288 | + $this->equalTo('myquota')) |
|
289 | + ->willReturn(false); |
|
290 | + |
|
291 | + $coreUser = $this->createMock(IUser::class); |
|
292 | + $coreUser->expects($this->never()) |
|
293 | + ->method('setQuota'); |
|
294 | + |
|
295 | + $this->userManager->expects($this->never()) |
|
296 | + ->method('get') |
|
297 | + ->with($this->uid); |
|
298 | + |
|
299 | + $this->config->expects($this->never()) |
|
300 | + ->method('setUserValue'); |
|
301 | + |
|
302 | + $this->user->updateQuota(); |
|
303 | + } |
|
304 | + |
|
305 | + public function testUpdateQuotaNoneConfigured(): void { |
|
306 | + $this->connection->expects($this->exactly(2)) |
|
307 | + ->method('__get') |
|
308 | + ->willReturnMap([ |
|
309 | + ['ldapQuotaAttribute', ''], |
|
310 | + ['ldapQuotaDefault', ''] |
|
311 | + ]); |
|
312 | + |
|
313 | + $coreUser = $this->createMock(IUser::class); |
|
314 | + $coreUser->expects($this->never()) |
|
315 | + ->method('setQuota'); |
|
316 | + |
|
317 | + $this->userManager->expects($this->never()) |
|
318 | + ->method('get'); |
|
319 | + |
|
320 | + $this->access->expects($this->never()) |
|
321 | + ->method('readAttribute'); |
|
322 | + |
|
323 | + $this->config->expects($this->never()) |
|
324 | + ->method('setUserValue'); |
|
325 | + |
|
326 | + $this->user->updateQuota(); |
|
327 | + } |
|
328 | + |
|
329 | + public function testUpdateQuotaFromValue(): void { |
|
330 | + $readQuota = '19 GB'; |
|
331 | + |
|
332 | + $this->connection->expects($this->exactly(2)) |
|
333 | + ->method('__get') |
|
334 | + ->willReturnMap([ |
|
335 | + ['ldapQuotaAttribute', 'myquota'], |
|
336 | + ['ldapQuotaDefault', ''] |
|
337 | + ]); |
|
338 | + |
|
339 | + $this->access->expects($this->never()) |
|
340 | + ->method('readAttribute'); |
|
341 | + |
|
342 | + $user = $this->createMock(IUser::class); |
|
343 | + $user->expects($this->once()) |
|
344 | + ->method('setQuota') |
|
345 | + ->with($readQuota); |
|
346 | + |
|
347 | + $this->userManager->expects($this->once()) |
|
348 | + ->method('get') |
|
349 | + ->with($this->uid) |
|
350 | + ->willReturn($user); |
|
351 | + |
|
352 | + $this->user->updateQuota($readQuota); |
|
353 | + } |
|
354 | + |
|
355 | + /** |
|
356 | + * Unparseable quota will fallback to use the LDAP default |
|
357 | + */ |
|
358 | + public function testUpdateWrongQuotaAllProvided(): void { |
|
359 | + $this->connection->expects($this->exactly(2)) |
|
360 | + ->method('__get') |
|
361 | + ->willReturnMap([ |
|
362 | + ['ldapQuotaAttribute', 'myquota'], |
|
363 | + ['ldapQuotaDefault', '23 GB'] |
|
364 | + ]); |
|
365 | + |
|
366 | + $this->access->expects($this->once()) |
|
367 | + ->method('readAttribute') |
|
368 | + ->with($this->equalTo($this->dn), |
|
369 | + $this->equalTo('myquota')) |
|
370 | + ->willReturn(['42 GBwos']); |
|
371 | + |
|
372 | + $coreUser = $this->createMock(IUser::class); |
|
373 | + $coreUser->expects($this->once()) |
|
374 | + ->method('setQuota') |
|
375 | + ->with('23 GB'); |
|
376 | + |
|
377 | + $this->userManager->expects($this->once()) |
|
378 | + ->method('get') |
|
379 | + ->with($this->uid) |
|
380 | + ->willReturn($coreUser); |
|
381 | + |
|
382 | + $this->user->updateQuota(); |
|
383 | + } |
|
384 | + |
|
385 | + /** |
|
386 | + * No user quota and wrong default will set 'default' as quota |
|
387 | + */ |
|
388 | + public function testUpdateWrongDefaultQuotaProvided(): void { |
|
389 | + $this->connection->expects($this->exactly(2)) |
|
390 | + ->method('__get') |
|
391 | + ->willReturnMap([ |
|
392 | + ['ldapQuotaAttribute', 'myquota'], |
|
393 | + ['ldapQuotaDefault', '23 GBwowowo'] |
|
394 | + ]); |
|
395 | + |
|
396 | + $this->access->expects($this->once()) |
|
397 | + ->method('readAttribute') |
|
398 | + ->with($this->equalTo($this->dn), |
|
399 | + $this->equalTo('myquota')) |
|
400 | + ->willReturn(false); |
|
401 | + |
|
402 | + $coreUser = $this->createMock(IUser::class); |
|
403 | + $coreUser->expects($this->never()) |
|
404 | + ->method('setQuota'); |
|
405 | + |
|
406 | + $this->userManager->expects($this->never()) |
|
407 | + ->method('get'); |
|
408 | + |
|
409 | + $this->user->updateQuota(); |
|
410 | + } |
|
411 | + |
|
412 | + /** |
|
413 | + * Wrong user quota and wrong default will set 'default' as quota |
|
414 | + */ |
|
415 | + public function testUpdateWrongQuotaAndDefaultAllProvided(): void { |
|
416 | + $this->connection->expects($this->exactly(2)) |
|
417 | + ->method('__get') |
|
418 | + ->willReturnMap([ |
|
419 | + ['ldapQuotaAttribute', 'myquota'], |
|
420 | + ['ldapQuotaDefault', '23 GBwowowo'] |
|
421 | + ]); |
|
422 | + |
|
423 | + $this->access->expects($this->once()) |
|
424 | + ->method('readAttribute') |
|
425 | + ->with($this->equalTo($this->dn), |
|
426 | + $this->equalTo('myquota')) |
|
427 | + ->willReturn(['23 flush']); |
|
428 | + |
|
429 | + $coreUser = $this->createMock(IUser::class); |
|
430 | + $coreUser->expects($this->never()) |
|
431 | + ->method('setQuota'); |
|
432 | + |
|
433 | + $this->userManager->expects($this->never()) |
|
434 | + ->method('get'); |
|
435 | + |
|
436 | + $this->user->updateQuota(); |
|
437 | + } |
|
438 | + |
|
439 | + /** |
|
440 | + * No quota attribute set and wrong default will set 'default' as quota |
|
441 | + */ |
|
442 | + public function testUpdateWrongDefaultQuotaNotProvided(): void { |
|
443 | + $this->connection->expects($this->exactly(2)) |
|
444 | + ->method('__get') |
|
445 | + ->willReturnMap([ |
|
446 | + ['ldapQuotaAttribute', ''], |
|
447 | + ['ldapQuotaDefault', '23 GBwowowo'] |
|
448 | + ]); |
|
449 | + |
|
450 | + $this->access->expects($this->never()) |
|
451 | + ->method('readAttribute'); |
|
452 | + |
|
453 | + $coreUser = $this->createMock(IUser::class); |
|
454 | + $coreUser->expects($this->never()) |
|
455 | + ->method('setQuota'); |
|
456 | + |
|
457 | + $this->userManager->expects($this->never()) |
|
458 | + ->method('get'); |
|
459 | + |
|
460 | + $this->user->updateQuota(); |
|
461 | + } |
|
462 | + |
|
463 | + //the testUpdateAvatar series also implicitly tests getAvatarImage |
|
464 | + public function XtestUpdateAvatarJpegPhotoProvided() { |
|
465 | + $this->access->expects($this->once()) |
|
466 | + ->method('readAttribute') |
|
467 | + ->with($this->equalTo($this->dn), |
|
468 | + $this->equalTo('jpegphoto')) |
|
469 | + ->willReturn(['this is a photo']); |
|
470 | + |
|
471 | + $this->image->expects($this->once()) |
|
472 | + ->method('loadFromBase64') |
|
473 | + ->willReturn('imageResource'); |
|
474 | + $this->image->expects($this->once()) |
|
475 | + ->method('valid') |
|
476 | + ->willReturn(true); |
|
477 | + $this->image->expects($this->once()) |
|
478 | + ->method('width') |
|
479 | + ->willReturn(128); |
|
480 | + $this->image->expects($this->once()) |
|
481 | + ->method('height') |
|
482 | + ->willReturn(128); |
|
483 | + $this->image->expects($this->once()) |
|
484 | + ->method('centerCrop') |
|
485 | + ->willReturn(true); |
|
486 | + $this->image->expects($this->once()) |
|
487 | + ->method('data') |
|
488 | + ->willReturn('this is a photo'); |
|
489 | + |
|
490 | + $this->config->expects($this->once()) |
|
491 | + ->method('getUserValue') |
|
492 | + ->with($this->uid, 'user_ldap', 'lastAvatarChecksum', '') |
|
493 | + ->willReturn(''); |
|
494 | + $this->config->expects($this->once()) |
|
495 | + ->method('setUserValue') |
|
496 | + ->with($this->uid, 'user_ldap', 'lastAvatarChecksum', md5('this is a photo')); |
|
497 | + |
|
498 | + $avatar = $this->createMock(IAvatar::class); |
|
499 | + $avatar->expects($this->once()) |
|
500 | + ->method('set') |
|
501 | + ->with($this->image); |
|
502 | + |
|
503 | + $this->avatarManager->expects($this->once()) |
|
504 | + ->method('getAvatar') |
|
505 | + ->with($this->equalTo($this->uid)) |
|
506 | + ->willReturn($avatar); |
|
507 | + |
|
508 | + $this->connection->expects($this->any()) |
|
509 | + ->method('resolveRule') |
|
510 | + ->with('avatar') |
|
511 | + ->willReturn(['jpegphoto', 'thumbnailphoto']); |
|
512 | + |
|
513 | + $this->user->updateAvatar(); |
|
514 | + } |
|
515 | + |
|
516 | + public function testUpdateAvatarKnownJpegPhotoProvided(): void { |
|
517 | + $this->access->expects($this->once()) |
|
518 | + ->method('readAttribute') |
|
519 | + ->with($this->equalTo($this->dn), |
|
520 | + $this->equalTo('jpegphoto')) |
|
521 | + ->willReturn(['this is a photo']); |
|
522 | + |
|
523 | + $this->image->expects($this->once()) |
|
524 | + ->method('loadFromBase64') |
|
525 | + ->willReturn('imageResource'); |
|
526 | + $this->image->expects($this->never()) |
|
527 | + ->method('valid'); |
|
528 | + $this->image->expects($this->never()) |
|
529 | + ->method('width'); |
|
530 | + $this->image->expects($this->never()) |
|
531 | + ->method('height'); |
|
532 | + $this->image->expects($this->never()) |
|
533 | + ->method('centerCrop'); |
|
534 | + $this->image->expects($this->once()) |
|
535 | + ->method('data') |
|
536 | + ->willReturn('this is a photo'); |
|
537 | + |
|
538 | + $this->config->expects($this->once()) |
|
539 | + ->method('getUserValue') |
|
540 | + ->with($this->uid, 'user_ldap', 'lastAvatarChecksum', '') |
|
541 | + ->willReturn(md5('this is a photo')); |
|
542 | + $this->config->expects($this->never()) |
|
543 | + ->method('setUserValue'); |
|
544 | + |
|
545 | + $avatar = $this->createMock(IAvatar::class); |
|
546 | + $avatar->expects($this->never()) |
|
547 | + ->method('set'); |
|
548 | + $avatar->expects($this->any()) |
|
549 | + ->method('exists') |
|
550 | + ->willReturn(true); |
|
551 | + $avatar->expects($this->any()) |
|
552 | + ->method('isCustomAvatar') |
|
553 | + ->willReturn(true); |
|
554 | + |
|
555 | + $this->avatarManager->expects($this->any()) |
|
556 | + ->method('getAvatar') |
|
557 | + ->with($this->uid) |
|
558 | + ->willReturn($avatar); |
|
559 | + |
|
560 | + $this->connection->expects($this->any()) |
|
561 | + ->method('resolveRule') |
|
562 | + ->with('avatar') |
|
563 | + ->willReturn(['jpegphoto', 'thumbnailphoto']); |
|
564 | + |
|
565 | + $this->assertTrue($this->user->updateAvatar()); |
|
566 | + } |
|
567 | + |
|
568 | + public function XtestUpdateAvatarThumbnailPhotoProvided() { |
|
569 | + $this->access->expects($this->any()) |
|
570 | + ->method('readAttribute') |
|
571 | + ->willReturnCallback(function ($dn, $attr) { |
|
572 | + if ($dn === $this->dn |
|
573 | + && $attr === 'jpegphoto') { |
|
574 | + return false; |
|
575 | + } elseif ($dn === $this->dn |
|
576 | + && $attr === 'thumbnailphoto') { |
|
577 | + return ['this is a photo']; |
|
578 | + } |
|
579 | + return null; |
|
580 | + }); |
|
581 | + |
|
582 | + $this->image->expects($this->once()) |
|
583 | + ->method('loadFromBase64') |
|
584 | + ->willReturn('imageResource'); |
|
585 | + $this->image->expects($this->once()) |
|
586 | + ->method('valid') |
|
587 | + ->willReturn(true); |
|
588 | + $this->image->expects($this->once()) |
|
589 | + ->method('width') |
|
590 | + ->willReturn(128); |
|
591 | + $this->image->expects($this->once()) |
|
592 | + ->method('height') |
|
593 | + ->willReturn(128); |
|
594 | + $this->image->expects($this->once()) |
|
595 | + ->method('centerCrop') |
|
596 | + ->willReturn(true); |
|
597 | + $this->image->expects($this->once()) |
|
598 | + ->method('data') |
|
599 | + ->willReturn('this is a photo'); |
|
600 | + |
|
601 | + $this->config->expects($this->once()) |
|
602 | + ->method('getUserValue') |
|
603 | + ->with($this->uid, 'user_ldap', 'lastAvatarChecksum', '') |
|
604 | + ->willReturn(''); |
|
605 | + $this->config->expects($this->once()) |
|
606 | + ->method('setUserValue') |
|
607 | + ->with($this->uid, 'user_ldap', 'lastAvatarChecksum', md5('this is a photo')); |
|
608 | + |
|
609 | + $avatar = $this->createMock(IAvatar::class); |
|
610 | + $avatar->expects($this->once()) |
|
611 | + ->method('set') |
|
612 | + ->with($this->image); |
|
613 | + |
|
614 | + $this->avatarManager->expects($this->once()) |
|
615 | + ->method('getAvatar') |
|
616 | + ->with($this->equalTo($this->uid)) |
|
617 | + ->willReturn($avatar); |
|
618 | + |
|
619 | + $this->connection->expects($this->any()) |
|
620 | + ->method('resolveRule') |
|
621 | + ->with('avatar') |
|
622 | + ->willReturn(['jpegphoto', 'thumbnailphoto']); |
|
623 | + |
|
624 | + $this->user->updateAvatar(); |
|
625 | + } |
|
626 | + |
|
627 | + public function testUpdateAvatarCorruptPhotoProvided(): void { |
|
628 | + $this->access->expects($this->any()) |
|
629 | + ->method('readAttribute') |
|
630 | + ->willReturnCallback(function ($dn, $attr) { |
|
631 | + if ($dn === $this->dn |
|
632 | + && $attr === 'jpegphoto') { |
|
633 | + return false; |
|
634 | + } elseif ($dn === $this->dn |
|
635 | + && $attr === 'thumbnailphoto') { |
|
636 | + return ['this is a photo']; |
|
637 | + } |
|
638 | + return null; |
|
639 | + }); |
|
640 | + |
|
641 | + $this->image->expects($this->once()) |
|
642 | + ->method('loadFromBase64') |
|
643 | + ->willReturn(false); |
|
644 | + $this->image->expects($this->never()) |
|
645 | + ->method('valid'); |
|
646 | + $this->image->expects($this->never()) |
|
647 | + ->method('width'); |
|
648 | + $this->image->expects($this->never()) |
|
649 | + ->method('height'); |
|
650 | + $this->image->expects($this->never()) |
|
651 | + ->method('centerCrop'); |
|
652 | + $this->image->expects($this->never()) |
|
653 | + ->method('data'); |
|
654 | + |
|
655 | + $this->config->expects($this->never()) |
|
656 | + ->method('getUserValue'); |
|
657 | + $this->config->expects($this->never()) |
|
658 | + ->method('setUserValue'); |
|
659 | + |
|
660 | + $avatar = $this->createMock(IAvatar::class); |
|
661 | + $avatar->expects($this->never()) |
|
662 | + ->method('set'); |
|
663 | + |
|
664 | + $this->avatarManager->expects($this->never()) |
|
665 | + ->method('getAvatar'); |
|
666 | + |
|
667 | + $this->connection->expects($this->any()) |
|
668 | + ->method('resolveRule') |
|
669 | + ->with('avatar') |
|
670 | + ->willReturn(['jpegphoto', 'thumbnailphoto']); |
|
671 | + |
|
672 | + $this->user->updateAvatar(); |
|
673 | + } |
|
674 | + |
|
675 | + public function XtestUpdateAvatarUnsupportedThumbnailPhotoProvided() { |
|
676 | + $this->access->expects($this->any()) |
|
677 | + ->method('readAttribute') |
|
678 | + ->willReturnCallback(function ($dn, $attr) { |
|
679 | + if ($dn === $this->dn |
|
680 | + && $attr === 'jpegphoto') { |
|
681 | + return false; |
|
682 | + } elseif ($dn === $this->dn |
|
683 | + && $attr === 'thumbnailphoto') { |
|
684 | + return ['this is a photo']; |
|
685 | + } |
|
686 | + return null; |
|
687 | + }); |
|
688 | + |
|
689 | + $this->image->expects($this->once()) |
|
690 | + ->method('loadFromBase64') |
|
691 | + ->willReturn('imageResource'); |
|
692 | + $this->image->expects($this->once()) |
|
693 | + ->method('valid') |
|
694 | + ->willReturn(true); |
|
695 | + $this->image->expects($this->once()) |
|
696 | + ->method('width') |
|
697 | + ->willReturn(128); |
|
698 | + $this->image->expects($this->once()) |
|
699 | + ->method('height') |
|
700 | + ->willReturn(128); |
|
701 | + $this->image->expects($this->once()) |
|
702 | + ->method('centerCrop') |
|
703 | + ->willReturn(true); |
|
704 | + $this->image->expects($this->once()) |
|
705 | + ->method('data') |
|
706 | + ->willReturn('this is a photo'); |
|
707 | + |
|
708 | + $this->config->expects($this->once()) |
|
709 | + ->method('getUserValue') |
|
710 | + ->with($this->uid, 'user_ldap', 'lastAvatarChecksum', '') |
|
711 | + ->willReturn(''); |
|
712 | + $this->config->expects($this->never()) |
|
713 | + ->method('setUserValue'); |
|
714 | + |
|
715 | + $avatar = $this->createMock(IAvatar::class); |
|
716 | + $avatar->expects($this->once()) |
|
717 | + ->method('set') |
|
718 | + ->with($this->image) |
|
719 | + ->willThrowException(new \Exception()); |
|
720 | + |
|
721 | + $this->avatarManager->expects($this->once()) |
|
722 | + ->method('getAvatar') |
|
723 | + ->with($this->equalTo($this->uid)) |
|
724 | + ->willReturn($avatar); |
|
725 | + |
|
726 | + $this->connection->expects($this->any()) |
|
727 | + ->method('resolveRule') |
|
728 | + ->with('avatar') |
|
729 | + ->willReturn(['jpegphoto', 'thumbnailphoto']); |
|
730 | + |
|
731 | + $this->assertFalse($this->user->updateAvatar()); |
|
732 | + } |
|
733 | + |
|
734 | + public function testUpdateAvatarNotProvided(): void { |
|
735 | + $this->access->expects($this->any()) |
|
736 | + ->method('readAttribute') |
|
737 | + ->willReturnCallback(function ($dn, $attr) { |
|
738 | + if ($dn === $this->dn |
|
739 | + && $attr === 'jpegPhoto') { |
|
740 | + return false; |
|
741 | + } elseif ($dn === $this->dn |
|
742 | + && $attr === 'thumbnailPhoto') { |
|
743 | + return false; |
|
744 | + } |
|
745 | + return null; |
|
746 | + }); |
|
747 | + |
|
748 | + $this->image->expects($this->never()) |
|
749 | + ->method('valid'); |
|
750 | + $this->image->expects($this->never()) |
|
751 | + ->method('width'); |
|
752 | + $this->image->expects($this->never()) |
|
753 | + ->method('height'); |
|
754 | + $this->image->expects($this->never()) |
|
755 | + ->method('centerCrop'); |
|
756 | + $this->image->expects($this->never()) |
|
757 | + ->method('data'); |
|
758 | + |
|
759 | + $this->config->expects($this->never()) |
|
760 | + ->method('getUserValue'); |
|
761 | + $this->config->expects($this->never()) |
|
762 | + ->method('setUserValue'); |
|
763 | + |
|
764 | + $this->avatarManager->expects($this->never()) |
|
765 | + ->method('getAvatar'); |
|
766 | + |
|
767 | + $this->connection->expects($this->any()) |
|
768 | + ->method('resolveRule') |
|
769 | + ->with('avatar') |
|
770 | + ->willReturn(['jpegphoto', 'thumbnailphoto']); |
|
771 | + |
|
772 | + $this->user->updateAvatar(); |
|
773 | + } |
|
774 | + |
|
775 | + public static function extStorageHomeDataProvider(): array { |
|
776 | + return [ |
|
777 | + [ 'myFolder', null ], |
|
778 | + [ '', null, false ], |
|
779 | + [ 'myFolder', 'myFolder' ], |
|
780 | + ]; |
|
781 | + } |
|
782 | + |
|
783 | + #[\PHPUnit\Framework\Attributes\DataProvider('extStorageHomeDataProvider')] |
|
784 | + public function testUpdateExtStorageHome(string $expected, ?string $valueFromLDAP = null, bool $isSet = true): void { |
|
785 | + if ($valueFromLDAP === null) { |
|
786 | + $this->connection->expects($this->once()) |
|
787 | + ->method('__get') |
|
788 | + ->willReturnMap([ |
|
789 | + ['ldapExtStorageHomeAttribute', 'homeDirectory'], |
|
790 | + ]); |
|
791 | + |
|
792 | + $return = []; |
|
793 | + if ($isSet) { |
|
794 | + $return[] = $expected; |
|
795 | + } |
|
796 | + $this->access->expects($this->once()) |
|
797 | + ->method('readAttribute') |
|
798 | + ->with($this->dn, 'homeDirectory') |
|
799 | + ->willReturn($return); |
|
800 | + } |
|
801 | + |
|
802 | + if ($expected !== '') { |
|
803 | + $this->config->expects($this->once()) |
|
804 | + ->method('setUserValue') |
|
805 | + ->with($this->uid, 'user_ldap', 'extStorageHome', $expected); |
|
806 | + } else { |
|
807 | + $this->config->expects($this->once()) |
|
808 | + ->method('deleteUserValue') |
|
809 | + ->with($this->uid, 'user_ldap', 'extStorageHome'); |
|
810 | + } |
|
811 | + |
|
812 | + $actual = $this->user->updateExtStorageHome($valueFromLDAP); |
|
813 | + $this->assertSame($expected, $actual); |
|
814 | + } |
|
815 | + |
|
816 | + public function testMarkLogin(): void { |
|
817 | + $this->config->expects($this->once()) |
|
818 | + ->method('setUserValue') |
|
819 | + ->with($this->equalTo($this->uid), |
|
820 | + $this->equalTo('user_ldap'), |
|
821 | + $this->equalTo(User::USER_PREFKEY_FIRSTLOGIN), |
|
822 | + $this->equalTo(1)) |
|
823 | + ->willReturn(true); |
|
824 | + |
|
825 | + $this->user->markLogin(); |
|
826 | + } |
|
827 | + |
|
828 | + public function testGetAvatarImageProvided(): void { |
|
829 | + $this->access->expects($this->once()) |
|
830 | + ->method('readAttribute') |
|
831 | + ->with($this->equalTo($this->dn), |
|
832 | + $this->equalTo('jpegphoto')) |
|
833 | + ->willReturn(['this is a photo']); |
|
834 | + $this->connection->expects($this->any()) |
|
835 | + ->method('resolveRule') |
|
836 | + ->with('avatar') |
|
837 | + ->willReturn(['jpegphoto', 'thumbnailphoto']); |
|
838 | + |
|
839 | + $photo = $this->user->getAvatarImage(); |
|
840 | + $this->assertSame('this is a photo', $photo); |
|
841 | + //make sure readAttribute is not called again but the already fetched |
|
842 | + //photo is returned |
|
843 | + $this->user->getAvatarImage(); |
|
844 | + } |
|
845 | + |
|
846 | + public function testGetAvatarImageDisabled(): void { |
|
847 | + $this->access->expects($this->never()) |
|
848 | + ->method('readAttribute') |
|
849 | + ->with($this->equalTo($this->dn), $this->anything()); |
|
850 | + $this->connection->expects($this->any()) |
|
851 | + ->method('resolveRule') |
|
852 | + ->with('avatar') |
|
853 | + ->willReturn([]); |
|
854 | + |
|
855 | + $this->assertFalse($this->user->getAvatarImage()); |
|
856 | + } |
|
857 | + |
|
858 | + public static function imageDataProvider(): array { |
|
859 | + return [ |
|
860 | + [ false, false ], |
|
861 | + [ 'corruptData', false ], |
|
862 | + [ 'validData', true ], |
|
863 | + ]; |
|
864 | + } |
|
865 | + |
|
866 | + public function testProcessAttributes(): void { |
|
867 | + $requiredMethods = [ |
|
868 | + 'updateQuota', |
|
869 | + 'updateEmail', |
|
870 | + 'composeAndStoreDisplayName', |
|
871 | + 'storeLDAPUserName', |
|
872 | + 'getHomePath', |
|
873 | + 'updateAvatar', |
|
874 | + 'updateExtStorageHome', |
|
875 | + ]; |
|
876 | + |
|
877 | + /** @var User&MockObject $userMock */ |
|
878 | + $userMock = $this->getMockBuilder(User::class) |
|
879 | + ->setConstructorArgs([ |
|
880 | + $this->uid, |
|
881 | + $this->dn, |
|
882 | + $this->access, |
|
883 | + $this->config, |
|
884 | + $this->image, |
|
885 | + $this->logger, |
|
886 | + $this->avatarManager, |
|
887 | + $this->userManager, |
|
888 | + $this->notificationManager |
|
889 | + ]) |
|
890 | + ->onlyMethods($requiredMethods) |
|
891 | + ->getMock(); |
|
892 | + |
|
893 | + $this->connection->setConfiguration([ |
|
894 | + 'homeFolderNamingRule' => 'homeDirectory' |
|
895 | + ]); |
|
896 | + $this->connection->expects($this->any()) |
|
897 | + ->method('__get') |
|
898 | + ->willReturnCallback(function ($name) { |
|
899 | + if ($name === 'homeFolderNamingRule') { |
|
900 | + return 'attr:homeDirectory'; |
|
901 | + } |
|
902 | + return $name; |
|
903 | + }); |
|
904 | + $this->connection->expects($this->any()) |
|
905 | + ->method('resolveRule') |
|
906 | + ->with('avatar') |
|
907 | + ->willReturn(['jpegphoto', 'thumbnailphoto']); |
|
908 | + |
|
909 | + $record = [ |
|
910 | + strtolower($this->connection->ldapQuotaAttribute) => ['4096'], |
|
911 | + strtolower($this->connection->ldapEmailAttribute) => ['[email protected]'], |
|
912 | + strtolower($this->connection->ldapUserDisplayName) => ['Aaaaalice'], |
|
913 | + strtolower($this->connection->ldapExtStorageHomeAttribute) => ['homeDirectory'], |
|
914 | + 'uid' => [$this->uid], |
|
915 | + 'homedirectory' => ['Alice\'s Folder'], |
|
916 | + 'memberof' => ['cn=groupOne', 'cn=groupTwo'], |
|
917 | + 'jpegphoto' => ['here be an image'] |
|
918 | + ]; |
|
919 | + |
|
920 | + foreach ($requiredMethods as $method) { |
|
921 | + $userMock->expects($this->once()) |
|
922 | + ->method($method); |
|
923 | + } |
|
924 | + \OC_Hook::clear();//disconnect irrelevant hooks |
|
925 | + $userMock->processAttributes($record); |
|
926 | + /** @noinspection PhpUnhandledExceptionInspection */ |
|
927 | + \OC_Hook::emit('OC_User', 'post_login', ['uid' => $this->uid]); |
|
928 | + } |
|
929 | + |
|
930 | + public static function emptyHomeFolderAttributeValueProvider(): array { |
|
931 | + return [ |
|
932 | + 'empty' => [''], |
|
933 | + 'prefixOnly' => ['attr:'], |
|
934 | + ]; |
|
935 | + } |
|
936 | + |
|
937 | + #[\PHPUnit\Framework\Attributes\DataProvider('emptyHomeFolderAttributeValueProvider')] |
|
938 | + public function testGetHomePathNotConfigured(string $attributeValue): void { |
|
939 | + $this->connection->expects($this->any()) |
|
940 | + ->method('__get') |
|
941 | + ->with($this->equalTo('homeFolderNamingRule')) |
|
942 | + ->willReturn($attributeValue); |
|
943 | + |
|
944 | + $this->access->expects($this->never()) |
|
945 | + ->method('readAttribute'); |
|
946 | + |
|
947 | + $this->config->expects($this->never()) |
|
948 | + ->method('getAppValue'); |
|
949 | + |
|
950 | + /** @noinspection PhpUnhandledExceptionInspection */ |
|
951 | + $this->assertFalse($this->user->getHomePath()); |
|
952 | + } |
|
953 | + |
|
954 | + public function testGetHomePathConfiguredNotAvailableAllowed(): void { |
|
955 | + $this->connection->expects($this->any()) |
|
956 | + ->method('__get') |
|
957 | + ->with($this->equalTo('homeFolderNamingRule')) |
|
958 | + ->willReturn('attr:foobar'); |
|
959 | + |
|
960 | + $this->access->expects($this->once()) |
|
961 | + ->method('readAttribute') |
|
962 | + ->willReturn(false); |
|
963 | + |
|
964 | + $this->access->expects($this->once()) |
|
965 | + ->method('username2dn') |
|
966 | + ->willReturn($this->dn); |
|
967 | + |
|
968 | + // asks for "enforce_home_folder_naming_rule" |
|
969 | + $this->config->expects($this->once()) |
|
970 | + ->method('getAppValue') |
|
971 | + ->willReturn(false); |
|
972 | + |
|
973 | + /** @noinspection PhpUnhandledExceptionInspection */ |
|
974 | + $this->assertFalse($this->user->getHomePath()); |
|
975 | + } |
|
976 | + |
|
977 | + |
|
978 | + public function testGetHomePathConfiguredNotAvailableNotAllowed(): void { |
|
979 | + $this->expectException(\Exception::class); |
|
980 | + |
|
981 | + $this->connection->expects($this->any()) |
|
982 | + ->method('__get') |
|
983 | + ->with($this->equalTo('homeFolderNamingRule')) |
|
984 | + ->willReturn('attr:foobar'); |
|
985 | + |
|
986 | + $this->access->expects($this->once()) |
|
987 | + ->method('readAttribute') |
|
988 | + ->willReturn(false); |
|
989 | + |
|
990 | + $this->access->expects($this->once()) |
|
991 | + ->method('username2dn') |
|
992 | + ->willReturn($this->dn); |
|
993 | + |
|
994 | + // asks for "enforce_home_folder_naming_rule" |
|
995 | + $this->config->expects($this->once()) |
|
996 | + ->method('getAppValue') |
|
997 | + ->willReturn(true); |
|
998 | + |
|
999 | + $this->user->getHomePath(); |
|
1000 | + } |
|
1001 | + |
|
1002 | + public static function displayNameProvider(): array { |
|
1003 | + return [ |
|
1004 | + ['Roland Deschain', '', 'Roland Deschain', false], |
|
1005 | + ['Roland Deschain', '', 'Roland Deschain', true], |
|
1006 | + ['Roland Deschain', '[email protected]', 'Roland Deschain ([email protected])', false], |
|
1007 | + ['Roland Deschain', '[email protected]', 'Roland Deschain ([email protected])', true], |
|
1008 | + ]; |
|
1009 | + } |
|
1010 | + |
|
1011 | + #[\PHPUnit\Framework\Attributes\DataProvider('displayNameProvider')] |
|
1012 | + public function testComposeAndStoreDisplayName(string $part1, string $part2, string $expected, bool $expectTriggerChange): void { |
|
1013 | + $this->config->expects($this->once()) |
|
1014 | + ->method('setUserValue'); |
|
1015 | + $oldName = $expectTriggerChange ? 'xxGunslingerxx' : null; |
|
1016 | + $this->config->expects($this->once()) |
|
1017 | + ->method('getUserValue') |
|
1018 | + ->with($this->user->getUsername(), 'user_ldap', 'displayName', null) |
|
1019 | + ->willReturn($oldName); |
|
1020 | + |
|
1021 | + $ncUserObj = $this->createMock(\OC\User\User::class); |
|
1022 | + if ($expectTriggerChange) { |
|
1023 | + $ncUserObj->expects($this->once()) |
|
1024 | + ->method('triggerChange') |
|
1025 | + ->with('displayName', $expected); |
|
1026 | + } else { |
|
1027 | + $ncUserObj->expects($this->never()) |
|
1028 | + ->method('triggerChange'); |
|
1029 | + } |
|
1030 | + $this->userManager->expects($this->once()) |
|
1031 | + ->method('get') |
|
1032 | + ->willReturn($ncUserObj); |
|
1033 | + |
|
1034 | + $displayName = $this->user->composeAndStoreDisplayName($part1, $part2); |
|
1035 | + $this->assertSame($expected, $displayName); |
|
1036 | + } |
|
1037 | + |
|
1038 | + public function testComposeAndStoreDisplayNameNoOverwrite(): void { |
|
1039 | + $displayName = 'Randall Flagg'; |
|
1040 | + $this->config->expects($this->never()) |
|
1041 | + ->method('setUserValue'); |
|
1042 | + $this->config->expects($this->once()) |
|
1043 | + ->method('getUserValue') |
|
1044 | + ->willReturn($displayName); |
|
1045 | + |
|
1046 | + $this->userManager->expects($this->never()) |
|
1047 | + ->method('get'); // Implicit: no triggerChange can be called |
|
1048 | + |
|
1049 | + $composedDisplayName = $this->user->composeAndStoreDisplayName($displayName); |
|
1050 | + $this->assertSame($composedDisplayName, $displayName); |
|
1051 | + } |
|
1052 | + |
|
1053 | + public function testHandlePasswordExpiryWarningDefaultPolicy(): void { |
|
1054 | + $this->connection->expects($this->any()) |
|
1055 | + ->method('__get') |
|
1056 | + ->willReturnCallback(function ($name) { |
|
1057 | + if ($name === 'ldapDefaultPPolicyDN') { |
|
1058 | + return 'cn=default,ou=policies,dc=foo,dc=bar'; |
|
1059 | + } |
|
1060 | + if ($name === 'turnOnPasswordChange') { |
|
1061 | + return '1'; |
|
1062 | + } |
|
1063 | + return $name; |
|
1064 | + }); |
|
1065 | + |
|
1066 | + $this->access->expects($this->any()) |
|
1067 | + ->method('search') |
|
1068 | + ->willReturnCallback(function ($filter, $base) { |
|
1069 | + if ($base === $this->dn) { |
|
1070 | + return [ |
|
1071 | + [ |
|
1072 | + 'pwdchangedtime' => [(new \DateTime())->sub(new \DateInterval('P28D'))->format('Ymdhis') . 'Z'], |
|
1073 | + 'pwdgraceusetime' => [], |
|
1074 | + ], |
|
1075 | + ]; |
|
1076 | + } |
|
1077 | + if ($base === 'cn=default,ou=policies,dc=foo,dc=bar') { |
|
1078 | + return [ |
|
1079 | + [ |
|
1080 | + 'pwdmaxage' => ['2592000'], |
|
1081 | + 'pwdexpirewarning' => ['2591999'], |
|
1082 | + ], |
|
1083 | + ]; |
|
1084 | + } |
|
1085 | + return []; |
|
1086 | + }); |
|
1087 | + |
|
1088 | + $notification = $this->getMockBuilder(INotification::class) |
|
1089 | + ->disableOriginalConstructor() |
|
1090 | + ->getMock(); |
|
1091 | + $notification->expects($this->any()) |
|
1092 | + ->method('setApp') |
|
1093 | + ->willReturn($notification); |
|
1094 | + $notification->expects($this->any()) |
|
1095 | + ->method('setUser') |
|
1096 | + ->willReturn($notification); |
|
1097 | + $notification->expects($this->any()) |
|
1098 | + ->method('setObject') |
|
1099 | + ->willReturn($notification); |
|
1100 | + $notification->expects($this->any()) |
|
1101 | + ->method('setDateTime') |
|
1102 | + ->willReturn($notification); |
|
1103 | + |
|
1104 | + $this->notificationManager->expects($this->exactly(2)) |
|
1105 | + ->method('createNotification') |
|
1106 | + ->willReturn($notification); |
|
1107 | + $this->notificationManager->expects($this->exactly(1)) |
|
1108 | + ->method('notify'); |
|
1109 | + |
|
1110 | + \OC_Hook::clear();//disconnect irrelevant hooks |
|
1111 | + Util::connectHook('OC_User', 'post_login', $this->user, 'handlePasswordExpiry'); |
|
1112 | + /** @noinspection PhpUnhandledExceptionInspection */ |
|
1113 | + \OC_Hook::emit('OC_User', 'post_login', ['uid' => $this->uid]); |
|
1114 | + } |
|
1115 | + |
|
1116 | + public function testHandlePasswordExpiryWarningCustomPolicy(): void { |
|
1117 | + $this->connection->expects($this->any()) |
|
1118 | + ->method('__get') |
|
1119 | + ->willReturnCallback(function ($name) { |
|
1120 | + if ($name === 'ldapDefaultPPolicyDN') { |
|
1121 | + return 'cn=default,ou=policies,dc=foo,dc=bar'; |
|
1122 | + } |
|
1123 | + if ($name === 'turnOnPasswordChange') { |
|
1124 | + return '1'; |
|
1125 | + } |
|
1126 | + return $name; |
|
1127 | + }); |
|
1128 | + |
|
1129 | + $this->access->expects($this->any()) |
|
1130 | + ->method('search') |
|
1131 | + ->willReturnCallback(function ($filter, $base) { |
|
1132 | + if ($base === $this->dn) { |
|
1133 | + return [ |
|
1134 | + [ |
|
1135 | + 'pwdpolicysubentry' => ['cn=custom,ou=policies,dc=foo,dc=bar'], |
|
1136 | + 'pwdchangedtime' => [(new \DateTime())->sub(new \DateInterval('P28D'))->format('Ymdhis') . 'Z'], |
|
1137 | + 'pwdgraceusetime' => [], |
|
1138 | + ] |
|
1139 | + ]; |
|
1140 | + } |
|
1141 | + if ($base === 'cn=custom,ou=policies,dc=foo,dc=bar') { |
|
1142 | + return [ |
|
1143 | + [ |
|
1144 | + 'pwdmaxage' => ['2592000'], |
|
1145 | + 'pwdexpirewarning' => ['2591999'], |
|
1146 | + ] |
|
1147 | + ]; |
|
1148 | + } |
|
1149 | + return []; |
|
1150 | + }); |
|
1151 | + |
|
1152 | + $notification = $this->getMockBuilder(INotification::class) |
|
1153 | + ->disableOriginalConstructor() |
|
1154 | + ->getMock(); |
|
1155 | + $notification->expects($this->any()) |
|
1156 | + ->method('setApp') |
|
1157 | + ->willReturn($notification); |
|
1158 | + $notification->expects($this->any()) |
|
1159 | + ->method('setUser') |
|
1160 | + ->willReturn($notification); |
|
1161 | + $notification->expects($this->any()) |
|
1162 | + ->method('setObject') |
|
1163 | + ->willReturn($notification); |
|
1164 | + $notification->expects($this->any()) |
|
1165 | + ->method('setDateTime') |
|
1166 | + ->willReturn($notification); |
|
1167 | + |
|
1168 | + $this->notificationManager->expects($this->exactly(2)) |
|
1169 | + ->method('createNotification') |
|
1170 | + ->willReturn($notification); |
|
1171 | + $this->notificationManager->expects($this->exactly(1)) |
|
1172 | + ->method('notify'); |
|
1173 | + |
|
1174 | + \OC_Hook::clear();//disconnect irrelevant hooks |
|
1175 | + Util::connectHook('OC_User', 'post_login', $this->user, 'handlePasswordExpiry'); |
|
1176 | + /** @noinspection PhpUnhandledExceptionInspection */ |
|
1177 | + \OC_Hook::emit('OC_User', 'post_login', ['uid' => $this->uid]); |
|
1178 | + } |
|
1179 | 1179 | } |
@@ -17,49 +17,49 @@ |
||
17 | 17 | use Test\TestCase; |
18 | 18 | |
19 | 19 | class OfflineUserTest extends TestCase { |
20 | - protected UserMapping&MockObject $mapping; |
|
21 | - protected string $uid; |
|
22 | - protected IConfig&MockObject $config; |
|
23 | - protected IManager&MockObject $shareManager; |
|
24 | - protected OfflineUser $offlineUser; |
|
20 | + protected UserMapping&MockObject $mapping; |
|
21 | + protected string $uid; |
|
22 | + protected IConfig&MockObject $config; |
|
23 | + protected IManager&MockObject $shareManager; |
|
24 | + protected OfflineUser $offlineUser; |
|
25 | 25 | |
26 | - public function setUp(): void { |
|
27 | - $this->uid = 'deborah'; |
|
28 | - $this->config = $this->createMock(IConfig::class); |
|
29 | - $this->mapping = $this->createMock(UserMapping::class); |
|
30 | - $this->shareManager = $this->createMock(IManager::class); |
|
26 | + public function setUp(): void { |
|
27 | + $this->uid = 'deborah'; |
|
28 | + $this->config = $this->createMock(IConfig::class); |
|
29 | + $this->mapping = $this->createMock(UserMapping::class); |
|
30 | + $this->shareManager = $this->createMock(IManager::class); |
|
31 | 31 | |
32 | - $this->offlineUser = new OfflineUser( |
|
33 | - $this->uid, |
|
34 | - $this->config, |
|
35 | - $this->mapping, |
|
36 | - $this->shareManager |
|
37 | - ); |
|
38 | - } |
|
32 | + $this->offlineUser = new OfflineUser( |
|
33 | + $this->uid, |
|
34 | + $this->config, |
|
35 | + $this->mapping, |
|
36 | + $this->shareManager |
|
37 | + ); |
|
38 | + } |
|
39 | 39 | |
40 | - public static function shareOwnerProvider(): array { |
|
41 | - return [ |
|
42 | - [[], false], |
|
43 | - [[IShare::TYPE_USER], true], |
|
44 | - [[IShare::TYPE_GROUP, IShare::TYPE_LINK], true], |
|
45 | - [[IShare::TYPE_EMAIL, IShare::TYPE_REMOTE, IShare::TYPE_CIRCLE], true], |
|
46 | - [[IShare::TYPE_GUEST, IShare::TYPE_REMOTE_GROUP, IShare::TYPE_ROOM], true], |
|
47 | - ]; |
|
48 | - } |
|
40 | + public static function shareOwnerProvider(): array { |
|
41 | + return [ |
|
42 | + [[], false], |
|
43 | + [[IShare::TYPE_USER], true], |
|
44 | + [[IShare::TYPE_GROUP, IShare::TYPE_LINK], true], |
|
45 | + [[IShare::TYPE_EMAIL, IShare::TYPE_REMOTE, IShare::TYPE_CIRCLE], true], |
|
46 | + [[IShare::TYPE_GUEST, IShare::TYPE_REMOTE_GROUP, IShare::TYPE_ROOM], true], |
|
47 | + ]; |
|
48 | + } |
|
49 | 49 | |
50 | - #[\PHPUnit\Framework\Attributes\DataProvider('shareOwnerProvider')] |
|
51 | - public function testHasActiveShares(array $existingShareTypes, bool $expected): void { |
|
52 | - $shareMock = $this->createMock(IShare::class); |
|
50 | + #[\PHPUnit\Framework\Attributes\DataProvider('shareOwnerProvider')] |
|
51 | + public function testHasActiveShares(array $existingShareTypes, bool $expected): void { |
|
52 | + $shareMock = $this->createMock(IShare::class); |
|
53 | 53 | |
54 | - $this->shareManager->expects($this->atLeastOnce()) |
|
55 | - ->method('getSharesBy') |
|
56 | - ->willReturnCallback(function (string $uid, int $shareType) use ($existingShareTypes, $shareMock) { |
|
57 | - if (in_array($shareType, $existingShareTypes)) { |
|
58 | - return [$shareMock]; |
|
59 | - } |
|
60 | - return []; |
|
61 | - }); |
|
54 | + $this->shareManager->expects($this->atLeastOnce()) |
|
55 | + ->method('getSharesBy') |
|
56 | + ->willReturnCallback(function (string $uid, int $shareType) use ($existingShareTypes, $shareMock) { |
|
57 | + if (in_array($shareType, $existingShareTypes)) { |
|
58 | + return [$shareMock]; |
|
59 | + } |
|
60 | + return []; |
|
61 | + }); |
|
62 | 62 | |
63 | - $this->assertSame($expected, $this->offlineUser->getHasActiveShares()); |
|
64 | - } |
|
63 | + $this->assertSame($expected, $this->offlineUser->getHasActiveShares()); |
|
64 | + } |
|
65 | 65 | } |
@@ -11,108 +11,108 @@ |
||
11 | 11 | use OCA\User_LDAP\Configuration; |
12 | 12 | |
13 | 13 | class ConfigurationTest extends \Test\TestCase { |
14 | - protected Configuration $configuration; |
|
15 | - |
|
16 | - protected function setUp(): void { |
|
17 | - parent::setUp(); |
|
18 | - $this->configuration = new Configuration('t01', false); |
|
19 | - } |
|
20 | - |
|
21 | - public static function configurationDataProvider(): array { |
|
22 | - $inputWithDN = [ |
|
23 | - 'cn=someUsers,dc=example,dc=org', |
|
24 | - ' ', |
|
25 | - ' cn=moreUsers,dc=example,dc=org ' |
|
26 | - ]; |
|
27 | - $expectWithDN = [ |
|
28 | - 'cn=someUsers,dc=example,dc=org', |
|
29 | - 'cn=moreUsers,dc=example,dc=org' |
|
30 | - ]; |
|
31 | - |
|
32 | - $inputNames = [ |
|
33 | - ' uid ', |
|
34 | - 'cn ', |
|
35 | - ' ', |
|
36 | - '', |
|
37 | - ' whats my name', |
|
38 | - ' ' |
|
39 | - ]; |
|
40 | - $expectedNames = ['uid', 'cn', 'whats my name']; |
|
41 | - |
|
42 | - $inputString = ' alea iacta est '; |
|
43 | - $expectedString = 'alea iacta est'; |
|
44 | - |
|
45 | - $inputHomeFolder = [ |
|
46 | - ' homeDirectory ', |
|
47 | - ' attr:homeDirectory ', |
|
48 | - ' ' |
|
49 | - ]; |
|
50 | - |
|
51 | - $expectedHomeFolder = [ |
|
52 | - 'attr:homeDirectory', 'attr:homeDirectory', '' |
|
53 | - ]; |
|
54 | - |
|
55 | - $password = ' such a passw0rd '; |
|
56 | - |
|
57 | - return [ |
|
58 | - 'set general base' => ['ldapBase', $inputWithDN, $expectWithDN], |
|
59 | - 'set user base' => ['ldapBaseUsers', $inputWithDN, $expectWithDN], |
|
60 | - 'set group base' => ['ldapBaseGroups', $inputWithDN, $expectWithDN], |
|
61 | - |
|
62 | - 'set search attributes users' => ['ldapAttributesForUserSearch', $inputNames, $expectedNames], |
|
63 | - 'set search attributes groups' => ['ldapAttributesForGroupSearch', $inputNames, $expectedNames], |
|
64 | - |
|
65 | - 'set user filter objectclasses' => ['ldapUserFilterObjectclass', $inputNames, $expectedNames], |
|
66 | - 'set user filter groups' => ['ldapUserFilterGroups', $inputNames, $expectedNames], |
|
67 | - 'set group filter objectclasses' => ['ldapGroupFilterObjectclass', $inputNames, $expectedNames], |
|
68 | - 'set group filter groups' => ['ldapGroupFilterGroups', $inputNames, $expectedNames], |
|
69 | - 'set login filter attributes' => ['ldapLoginFilterAttributes', $inputNames, $expectedNames], |
|
70 | - |
|
71 | - 'set agent password' => ['ldapAgentPassword', $password, $password], |
|
72 | - |
|
73 | - 'set home folder, variant 1' => ['homeFolderNamingRule', $inputHomeFolder[0], $expectedHomeFolder[0]], |
|
74 | - 'set home folder, variant 2' => ['homeFolderNamingRule', $inputHomeFolder[1], $expectedHomeFolder[1]], |
|
75 | - 'set home folder, empty' => ['homeFolderNamingRule', $inputHomeFolder[2], $expectedHomeFolder[2]], |
|
76 | - |
|
77 | - // default behaviour, one case is enough, special needs must be tested |
|
78 | - // individually |
|
79 | - 'set string value' => ['ldapHost', $inputString, $expectedString], |
|
80 | - |
|
81 | - 'set avatar rule, default' => ['ldapUserAvatarRule', 'default', 'default'], |
|
82 | - 'set avatar rule, none' => ['ldapUserAvatarRule', 'none', 'none'], |
|
83 | - 'set avatar rule, data attribute' => ['ldapUserAvatarRule', 'data:jpegPhoto', 'data:jpegPhoto'], |
|
84 | - |
|
85 | - 'set external storage home attribute' => ['ldapExtStorageHomeAttribute', 'homePath', 'homePath'], |
|
86 | - ]; |
|
87 | - } |
|
88 | - |
|
89 | - #[\PHPUnit\Framework\Attributes\DataProvider('configurationDataProvider')] |
|
90 | - public function testSetValue(string $key, string|array $input, string|array $expected): void { |
|
91 | - $this->configuration->setConfiguration([$key => $input]); |
|
92 | - $this->assertSame($this->configuration->$key, $expected); |
|
93 | - } |
|
94 | - |
|
95 | - public static function avatarRuleValueProvider(): array { |
|
96 | - return [ |
|
97 | - ['none', []], |
|
98 | - ['data:selfie', ['selfie']], |
|
99 | - ['data:sELFie', ['selfie']], |
|
100 | - ['data:', ['jpegphoto', 'thumbnailphoto']], |
|
101 | - ['default', ['jpegphoto', 'thumbnailphoto']], |
|
102 | - ['invalid#', ['jpegphoto', 'thumbnailphoto']], |
|
103 | - ]; |
|
104 | - } |
|
105 | - |
|
106 | - #[\PHPUnit\Framework\Attributes\DataProvider('avatarRuleValueProvider')] |
|
107 | - public function testGetAvatarAttributes(string $setting, array $expected): void { |
|
108 | - $this->configuration->setConfiguration(['ldapUserAvatarRule' => $setting]); |
|
109 | - $this->assertSame($expected, $this->configuration->getAvatarAttributes()); |
|
110 | - } |
|
111 | - |
|
112 | - #[\PHPUnit\Framework\Attributes\DataProvider('avatarRuleValueProvider')] |
|
113 | - public function testResolveRule(string $setting, array $expected): void { |
|
114 | - $this->configuration->setConfiguration(['ldapUserAvatarRule' => $setting]); |
|
115 | - // so far the only thing that can get resolved :) |
|
116 | - $this->assertSame($expected, $this->configuration->resolveRule('avatar')); |
|
117 | - } |
|
14 | + protected Configuration $configuration; |
|
15 | + |
|
16 | + protected function setUp(): void { |
|
17 | + parent::setUp(); |
|
18 | + $this->configuration = new Configuration('t01', false); |
|
19 | + } |
|
20 | + |
|
21 | + public static function configurationDataProvider(): array { |
|
22 | + $inputWithDN = [ |
|
23 | + 'cn=someUsers,dc=example,dc=org', |
|
24 | + ' ', |
|
25 | + ' cn=moreUsers,dc=example,dc=org ' |
|
26 | + ]; |
|
27 | + $expectWithDN = [ |
|
28 | + 'cn=someUsers,dc=example,dc=org', |
|
29 | + 'cn=moreUsers,dc=example,dc=org' |
|
30 | + ]; |
|
31 | + |
|
32 | + $inputNames = [ |
|
33 | + ' uid ', |
|
34 | + 'cn ', |
|
35 | + ' ', |
|
36 | + '', |
|
37 | + ' whats my name', |
|
38 | + ' ' |
|
39 | + ]; |
|
40 | + $expectedNames = ['uid', 'cn', 'whats my name']; |
|
41 | + |
|
42 | + $inputString = ' alea iacta est '; |
|
43 | + $expectedString = 'alea iacta est'; |
|
44 | + |
|
45 | + $inputHomeFolder = [ |
|
46 | + ' homeDirectory ', |
|
47 | + ' attr:homeDirectory ', |
|
48 | + ' ' |
|
49 | + ]; |
|
50 | + |
|
51 | + $expectedHomeFolder = [ |
|
52 | + 'attr:homeDirectory', 'attr:homeDirectory', '' |
|
53 | + ]; |
|
54 | + |
|
55 | + $password = ' such a passw0rd '; |
|
56 | + |
|
57 | + return [ |
|
58 | + 'set general base' => ['ldapBase', $inputWithDN, $expectWithDN], |
|
59 | + 'set user base' => ['ldapBaseUsers', $inputWithDN, $expectWithDN], |
|
60 | + 'set group base' => ['ldapBaseGroups', $inputWithDN, $expectWithDN], |
|
61 | + |
|
62 | + 'set search attributes users' => ['ldapAttributesForUserSearch', $inputNames, $expectedNames], |
|
63 | + 'set search attributes groups' => ['ldapAttributesForGroupSearch', $inputNames, $expectedNames], |
|
64 | + |
|
65 | + 'set user filter objectclasses' => ['ldapUserFilterObjectclass', $inputNames, $expectedNames], |
|
66 | + 'set user filter groups' => ['ldapUserFilterGroups', $inputNames, $expectedNames], |
|
67 | + 'set group filter objectclasses' => ['ldapGroupFilterObjectclass', $inputNames, $expectedNames], |
|
68 | + 'set group filter groups' => ['ldapGroupFilterGroups', $inputNames, $expectedNames], |
|
69 | + 'set login filter attributes' => ['ldapLoginFilterAttributes', $inputNames, $expectedNames], |
|
70 | + |
|
71 | + 'set agent password' => ['ldapAgentPassword', $password, $password], |
|
72 | + |
|
73 | + 'set home folder, variant 1' => ['homeFolderNamingRule', $inputHomeFolder[0], $expectedHomeFolder[0]], |
|
74 | + 'set home folder, variant 2' => ['homeFolderNamingRule', $inputHomeFolder[1], $expectedHomeFolder[1]], |
|
75 | + 'set home folder, empty' => ['homeFolderNamingRule', $inputHomeFolder[2], $expectedHomeFolder[2]], |
|
76 | + |
|
77 | + // default behaviour, one case is enough, special needs must be tested |
|
78 | + // individually |
|
79 | + 'set string value' => ['ldapHost', $inputString, $expectedString], |
|
80 | + |
|
81 | + 'set avatar rule, default' => ['ldapUserAvatarRule', 'default', 'default'], |
|
82 | + 'set avatar rule, none' => ['ldapUserAvatarRule', 'none', 'none'], |
|
83 | + 'set avatar rule, data attribute' => ['ldapUserAvatarRule', 'data:jpegPhoto', 'data:jpegPhoto'], |
|
84 | + |
|
85 | + 'set external storage home attribute' => ['ldapExtStorageHomeAttribute', 'homePath', 'homePath'], |
|
86 | + ]; |
|
87 | + } |
|
88 | + |
|
89 | + #[\PHPUnit\Framework\Attributes\DataProvider('configurationDataProvider')] |
|
90 | + public function testSetValue(string $key, string|array $input, string|array $expected): void { |
|
91 | + $this->configuration->setConfiguration([$key => $input]); |
|
92 | + $this->assertSame($this->configuration->$key, $expected); |
|
93 | + } |
|
94 | + |
|
95 | + public static function avatarRuleValueProvider(): array { |
|
96 | + return [ |
|
97 | + ['none', []], |
|
98 | + ['data:selfie', ['selfie']], |
|
99 | + ['data:sELFie', ['selfie']], |
|
100 | + ['data:', ['jpegphoto', 'thumbnailphoto']], |
|
101 | + ['default', ['jpegphoto', 'thumbnailphoto']], |
|
102 | + ['invalid#', ['jpegphoto', 'thumbnailphoto']], |
|
103 | + ]; |
|
104 | + } |
|
105 | + |
|
106 | + #[\PHPUnit\Framework\Attributes\DataProvider('avatarRuleValueProvider')] |
|
107 | + public function testGetAvatarAttributes(string $setting, array $expected): void { |
|
108 | + $this->configuration->setConfiguration(['ldapUserAvatarRule' => $setting]); |
|
109 | + $this->assertSame($expected, $this->configuration->getAvatarAttributes()); |
|
110 | + } |
|
111 | + |
|
112 | + #[\PHPUnit\Framework\Attributes\DataProvider('avatarRuleValueProvider')] |
|
113 | + public function testResolveRule(string $setting, array $expected): void { |
|
114 | + $this->configuration->setConfiguration(['ldapUserAvatarRule' => $setting]); |
|
115 | + // so far the only thing that can get resolved :) |
|
116 | + $this->assertSame($expected, $this->configuration->resolveRule('avatar')); |
|
117 | + } |
|
118 | 118 | } |
@@ -87,7 +87,7 @@ |
||
87 | 87 | } |
88 | 88 | |
89 | 89 | #[\PHPUnit\Framework\Attributes\DataProvider('configurationDataProvider')] |
90 | - public function testSetValue(string $key, string|array $input, string|array $expected): void { |
|
90 | + public function testSetValue(string $key, string | array $input, string | array $expected): void { |
|
91 | 91 | $this->configuration->setConfiguration([$key => $input]); |
92 | 92 | $this->assertSame($this->configuration->$key, $expected); |
93 | 93 | } |
@@ -13,39 +13,39 @@ |
||
13 | 13 | use PHPUnit\Framework\TestCase; |
14 | 14 | |
15 | 15 | class BirthdateParserServiceTest extends TestCase { |
16 | - private BirthdateParserService $service; |
|
17 | - |
|
18 | - protected function setUp(): void { |
|
19 | - parent::setUp(); |
|
20 | - |
|
21 | - $this->service = new BirthdateParserService(); |
|
22 | - } |
|
23 | - |
|
24 | - public static function parseBirthdateDataProvider(): array { |
|
25 | - return [ |
|
26 | - ['2024-01-01', new DateTimeImmutable('2024-01-01'), false], |
|
27 | - ['20240101', new DateTimeImmutable('2024-01-01'), false], |
|
28 | - ['199412161032Z', new DateTimeImmutable('1994-12-16'), false], // LDAP generalized time |
|
29 | - ['199412160532-0500', new DateTimeImmutable('1994-12-16'), false], // LDAP generalized time |
|
30 | - ['2023-07-31T00:60:59.000Z', null, true], |
|
31 | - ['01.01.2024', null, true], |
|
32 | - ['01/01/2024', null, true], |
|
33 | - ['01 01 2024', null, true], |
|
34 | - ['foobar', null, true], |
|
35 | - ]; |
|
36 | - } |
|
37 | - |
|
38 | - #[\PHPUnit\Framework\Attributes\DataProvider('parseBirthdateDataProvider')] |
|
39 | - public function testParseBirthdate( |
|
40 | - string $value, |
|
41 | - ?DateTimeImmutable $expected, |
|
42 | - bool $shouldThrow, |
|
43 | - ): void { |
|
44 | - if ($shouldThrow) { |
|
45 | - $this->expectException(\InvalidArgumentException::class); |
|
46 | - } |
|
47 | - |
|
48 | - $actual = $this->service->parseBirthdate($value); |
|
49 | - $this->assertEquals($expected, $actual); |
|
50 | - } |
|
16 | + private BirthdateParserService $service; |
|
17 | + |
|
18 | + protected function setUp(): void { |
|
19 | + parent::setUp(); |
|
20 | + |
|
21 | + $this->service = new BirthdateParserService(); |
|
22 | + } |
|
23 | + |
|
24 | + public static function parseBirthdateDataProvider(): array { |
|
25 | + return [ |
|
26 | + ['2024-01-01', new DateTimeImmutable('2024-01-01'), false], |
|
27 | + ['20240101', new DateTimeImmutable('2024-01-01'), false], |
|
28 | + ['199412161032Z', new DateTimeImmutable('1994-12-16'), false], // LDAP generalized time |
|
29 | + ['199412160532-0500', new DateTimeImmutable('1994-12-16'), false], // LDAP generalized time |
|
30 | + ['2023-07-31T00:60:59.000Z', null, true], |
|
31 | + ['01.01.2024', null, true], |
|
32 | + ['01/01/2024', null, true], |
|
33 | + ['01 01 2024', null, true], |
|
34 | + ['foobar', null, true], |
|
35 | + ]; |
|
36 | + } |
|
37 | + |
|
38 | + #[\PHPUnit\Framework\Attributes\DataProvider('parseBirthdateDataProvider')] |
|
39 | + public function testParseBirthdate( |
|
40 | + string $value, |
|
41 | + ?DateTimeImmutable $expected, |
|
42 | + bool $shouldThrow, |
|
43 | + ): void { |
|
44 | + if ($shouldThrow) { |
|
45 | + $this->expectException(\InvalidArgumentException::class); |
|
46 | + } |
|
47 | + |
|
48 | + $actual = $this->service->parseBirthdate($value); |
|
49 | + $this->assertEquals($expected, $actual); |
|
50 | + } |
|
51 | 51 | } |
@@ -40,1428 +40,1428 @@ |
||
40 | 40 | * @package OCA\User_LDAP\Tests |
41 | 41 | */ |
42 | 42 | class User_LDAPTest extends TestCase { |
43 | - protected Access&MockObject $access; |
|
44 | - protected OfflineUser&MockObject $offlineUser; |
|
45 | - protected INotificationManager&MockObject $notificationManager; |
|
46 | - protected UserPluginManager&MockObject $pluginManager; |
|
47 | - protected Connection&MockObject $connection; |
|
48 | - protected Manager&MockObject $userManager; |
|
49 | - protected LoggerInterface&MockObject $logger; |
|
50 | - protected DeletedUsersIndex&MockObject $deletedUsersIndex; |
|
51 | - protected User_LDAP $backend; |
|
52 | - |
|
53 | - protected function setUp(): void { |
|
54 | - parent::setUp(); |
|
55 | - |
|
56 | - Server::get(IUserManager::class)->clearBackends(); |
|
57 | - Server::get(IGroupManager::class)->clearBackends(); |
|
58 | - |
|
59 | - $this->connection = $this->getMockBuilder(Connection::class) |
|
60 | - ->setConstructorArgs([$this->createMock(ILDAPWrapper::class)]) |
|
61 | - ->getMock(); |
|
62 | - $this->userManager = $this->createMock(Manager::class); |
|
63 | - |
|
64 | - $this->access = $this->createMock(Access::class); |
|
65 | - $this->access->connection = $this->connection; |
|
66 | - $this->access->userManager = $this->userManager; |
|
67 | - |
|
68 | - $this->notificationManager = $this->createMock(INotificationManager::class); |
|
69 | - $this->pluginManager = $this->createMock(UserPluginManager::class); |
|
70 | - |
|
71 | - $this->logger = $this->createMock(LoggerInterface::class); |
|
72 | - |
|
73 | - $this->deletedUsersIndex = $this->createMock(DeletedUsersIndex::class); |
|
74 | - |
|
75 | - $this->backend = new User_LDAP( |
|
76 | - $this->access, |
|
77 | - $this->notificationManager, |
|
78 | - $this->pluginManager, |
|
79 | - $this->logger, |
|
80 | - $this->deletedUsersIndex, |
|
81 | - ); |
|
82 | - } |
|
83 | - |
|
84 | - private function prepareMockForUserExists(): void { |
|
85 | - $this->access->expects($this->any()) |
|
86 | - ->method('username2dn') |
|
87 | - ->willReturnCallback(function ($uid) { |
|
88 | - switch ($uid) { |
|
89 | - case 'gunslinger': |
|
90 | - return 'dnOfRoland,dc=test'; |
|
91 | - break; |
|
92 | - case 'formerUser': |
|
93 | - return 'dnOfFormerUser,dc=test'; |
|
94 | - break; |
|
95 | - case 'newyorker': |
|
96 | - return 'dnOfNewYorker,dc=test'; |
|
97 | - break; |
|
98 | - case 'ladyofshadows': |
|
99 | - return 'dnOfLadyOfShadows,dc=test'; |
|
100 | - break; |
|
101 | - default: |
|
102 | - return false; |
|
103 | - } |
|
104 | - }); |
|
105 | - |
|
106 | - $this->access->method('fetchUsersByLoginName') |
|
107 | - ->willReturn([]); |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * Prepares the Access mock for checkPassword tests |
|
112 | - */ |
|
113 | - private function prepareAccessForCheckPassword(bool $noDisplayName = false): void { |
|
114 | - $this->connection->expects($this->any()) |
|
115 | - ->method('__get') |
|
116 | - ->willReturnCallback(function ($name) { |
|
117 | - if ($name === 'ldapLoginFilter') { |
|
118 | - return '%uid'; |
|
119 | - } |
|
120 | - return null; |
|
121 | - }); |
|
122 | - |
|
123 | - $this->access->expects($this->any()) |
|
124 | - ->method('fetchListOfUsers') |
|
125 | - ->willReturnCallback(function ($filter) { |
|
126 | - if ($filter === 'roland') { |
|
127 | - return [['dn' => ['dnOfRoland,dc=test']]]; |
|
128 | - } |
|
129 | - return []; |
|
130 | - }); |
|
131 | - $this->access->expects($this->any()) |
|
132 | - ->method('fetchUsersByLoginName') |
|
133 | - ->willReturnCallback(function ($uid) { |
|
134 | - if ($uid === 'roland') { |
|
135 | - return [['dn' => ['dnOfRoland,dc=test']]]; |
|
136 | - } |
|
137 | - return []; |
|
138 | - }); |
|
139 | - |
|
140 | - $retVal = 'gunslinger'; |
|
141 | - if ($noDisplayName === true) { |
|
142 | - $retVal = false; |
|
143 | - } |
|
144 | - $this->access->expects($this->any()) |
|
145 | - ->method('dn2username') |
|
146 | - ->with($this->equalTo('dnOfRoland,dc=test')) |
|
147 | - ->willReturn($retVal); |
|
148 | - $this->access->expects($this->any()) |
|
149 | - ->method('username2dn') |
|
150 | - ->with($this->equalTo('gunslinger')) |
|
151 | - ->willReturn('dnOfRoland,dc=test'); |
|
152 | - $this->access->expects($this->any()) |
|
153 | - ->method('stringResemblesDN') |
|
154 | - ->with($this->equalTo('dnOfRoland,dc=test')) |
|
155 | - ->willReturn(true); |
|
156 | - $this->access->expects($this->any()) |
|
157 | - ->method('areCredentialsValid') |
|
158 | - ->willReturnCallback(function ($dn, $pwd) { |
|
159 | - if ($pwd === 'dt19') { |
|
160 | - return true; |
|
161 | - } |
|
162 | - return false; |
|
163 | - }); |
|
164 | - |
|
165 | - $this->userManager->expects($this->any()) |
|
166 | - ->method('getAttributes') |
|
167 | - ->willReturn(['dn', 'uid', 'mail', 'displayname']); |
|
168 | - } |
|
169 | - |
|
170 | - public function testCheckPasswordUidReturn(): void { |
|
171 | - $user = $this->createMock(User::class); |
|
172 | - $user->expects($this->any()) |
|
173 | - ->method('getUsername') |
|
174 | - ->willReturn('gunslinger'); |
|
175 | - |
|
176 | - $this->prepareAccessForCheckPassword(); |
|
177 | - $this->userManager->expects($this->any()) |
|
178 | - ->method('get') |
|
179 | - ->willReturn($user); |
|
180 | - |
|
181 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
182 | - |
|
183 | - Server::get(IUserManager::class)->registerBackend($backend); |
|
184 | - |
|
185 | - $result = $backend->checkPassword('roland', 'dt19'); |
|
186 | - $this->assertEquals('gunslinger', $result); |
|
187 | - } |
|
188 | - |
|
189 | - public function testCheckPasswordWrongPassword(): void { |
|
190 | - $this->prepareAccessForCheckPassword(); |
|
191 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
192 | - Server::get(IUserManager::class)->registerBackend($backend); |
|
193 | - |
|
194 | - $result = $backend->checkPassword('roland', 'wrong'); |
|
195 | - $this->assertFalse($result); |
|
196 | - } |
|
197 | - |
|
198 | - public function testCheckPasswordWrongUser(): void { |
|
199 | - $this->prepareAccessForCheckPassword(); |
|
200 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
201 | - Server::get(IUserManager::class)->registerBackend($backend); |
|
202 | - |
|
203 | - $result = $backend->checkPassword('mallory', 'evil'); |
|
204 | - $this->assertFalse($result); |
|
205 | - } |
|
206 | - |
|
207 | - public function testCheckPasswordNoDisplayName(): void { |
|
208 | - $this->prepareAccessForCheckPassword(true); |
|
209 | - |
|
210 | - $this->prepareAccessForCheckPassword(); |
|
211 | - $this->userManager->expects($this->atLeastOnce()) |
|
212 | - ->method('get') |
|
213 | - ->willReturn(null); |
|
214 | - |
|
215 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
216 | - Server::get(IUserManager::class)->registerBackend($backend); |
|
217 | - |
|
218 | - $result = $backend->checkPassword('roland', 'dt19'); |
|
219 | - $this->assertFalse($result); |
|
220 | - } |
|
221 | - |
|
222 | - public function testCheckPasswordPublicAPI(): void { |
|
223 | - $user = $this->createMock(User::class); |
|
224 | - $user->expects($this->any()) |
|
225 | - ->method('getUsername') |
|
226 | - ->willReturn('gunslinger'); |
|
227 | - |
|
228 | - $this->prepareAccessForCheckPassword(); |
|
229 | - $this->userManager->expects($this->any()) |
|
230 | - ->method('get') |
|
231 | - ->willReturn($user); |
|
232 | - |
|
233 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
234 | - Server::get(IUserManager::class)->registerBackend($backend); |
|
235 | - |
|
236 | - $user = Server::get(IUserManager::class)->checkPassword('roland', 'dt19'); |
|
237 | - $result = false; |
|
238 | - if ($user !== false) { |
|
239 | - $result = $user->getUID(); |
|
240 | - } |
|
241 | - $this->assertEquals('gunslinger', $result); |
|
242 | - } |
|
243 | - |
|
244 | - public function testCheckPasswordPublicAPIWrongPassword(): void { |
|
245 | - $this->prepareAccessForCheckPassword(); |
|
246 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
247 | - Server::get(IUserManager::class)->registerBackend($backend); |
|
248 | - |
|
249 | - $user = Server::get(IUserManager::class)->checkPassword('roland', 'wrong'); |
|
250 | - $result = false; |
|
251 | - if ($user !== false) { |
|
252 | - $result = $user->getUID(); |
|
253 | - } |
|
254 | - $this->assertFalse($result); |
|
255 | - } |
|
256 | - |
|
257 | - public function testCheckPasswordPublicAPIWrongUser(): void { |
|
258 | - $this->prepareAccessForCheckPassword(); |
|
259 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
260 | - Server::get(IUserManager::class)->registerBackend($backend); |
|
261 | - |
|
262 | - $user = Server::get(IUserManager::class)->checkPassword('mallory', 'evil'); |
|
263 | - $result = false; |
|
264 | - if ($user !== false) { |
|
265 | - $result = $user->getUID(); |
|
266 | - } |
|
267 | - $this->assertFalse($result); |
|
268 | - } |
|
269 | - |
|
270 | - public function testDeleteUserCancel(): void { |
|
271 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
272 | - $result = $backend->deleteUser('notme'); |
|
273 | - $this->assertFalse($result); |
|
274 | - } |
|
275 | - |
|
276 | - public function testDeleteUserSuccess(): void { |
|
277 | - $uid = 'jeremy'; |
|
278 | - $home = '/var/vhome/jdings/'; |
|
279 | - |
|
280 | - $mapping = $this->createMock(UserMapping::class); |
|
281 | - $mapping->expects($this->once()) |
|
282 | - ->method('unmap') |
|
283 | - ->willReturn(true); |
|
284 | - $this->access->expects($this->once()) |
|
285 | - ->method('getUserMapper') |
|
286 | - ->willReturn($mapping); |
|
287 | - $this->connection->expects($this->any()) |
|
288 | - ->method('getConnectionResource') |
|
289 | - ->willReturn(ldap_connect('ldap://example.com')); |
|
290 | - |
|
291 | - $this->deletedUsersIndex->expects($this->once()) |
|
292 | - ->method('isUserMarked') |
|
293 | - ->with($uid) |
|
294 | - ->willReturn(true); |
|
295 | - |
|
296 | - $offlineUser = $this->createMock(OfflineUser::class); |
|
297 | - $offlineUser->expects($this->once()) |
|
298 | - ->method('getHomePath') |
|
299 | - ->willReturn($home); |
|
300 | - $this->userManager->expects($this->atLeastOnce()) |
|
301 | - ->method('get') |
|
302 | - ->willReturn($offlineUser); |
|
303 | - $this->userManager->expects($this->once()) |
|
304 | - ->method('exists') |
|
305 | - ->with($uid) |
|
306 | - ->willReturn(true); |
|
307 | - |
|
308 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
309 | - |
|
310 | - $result = $backend->deleteUser($uid); |
|
311 | - $this->assertTrue($result); |
|
312 | - /** @noinspection PhpUnhandledExceptionInspection */ |
|
313 | - $this->assertSame($backend->getHome($uid), $home); |
|
314 | - } |
|
315 | - |
|
316 | - public function testDeleteUserWithPlugin(): void { |
|
317 | - $this->pluginManager->expects($this->once()) |
|
318 | - ->method('canDeleteUser') |
|
319 | - ->willReturn(true); |
|
320 | - $this->pluginManager->expects($this->once()) |
|
321 | - ->method('deleteUser') |
|
322 | - ->with('uid') |
|
323 | - ->willReturn(true); |
|
324 | - |
|
325 | - $this->deletedUsersIndex->expects($this->once()) |
|
326 | - ->method('isUserMarked') |
|
327 | - ->with('uid') |
|
328 | - ->willReturn(true); |
|
329 | - |
|
330 | - $mapper = $this->createMock(UserMapping::class); |
|
331 | - $mapper->expects($this->once()) |
|
332 | - ->method('unmap') |
|
333 | - ->with('uid'); |
|
334 | - |
|
335 | - $this->access->expects($this->atLeastOnce()) |
|
336 | - ->method('getUserMapper') |
|
337 | - ->willReturn($mapper); |
|
338 | - |
|
339 | - $this->userManager->expects($this->once()) |
|
340 | - ->method('invalidate') |
|
341 | - ->with('uid'); |
|
342 | - |
|
343 | - $this->assertTrue($this->backend->deleteUser('uid')); |
|
344 | - } |
|
345 | - |
|
346 | - /** |
|
347 | - * Prepares the Access mock for getUsers tests |
|
348 | - */ |
|
349 | - private function prepareAccessForGetUsers() { |
|
350 | - $this->access->expects($this->once()) |
|
351 | - ->method('escapeFilterPart') |
|
352 | - ->willReturnCallback(function ($search) { |
|
353 | - return $search; |
|
354 | - }); |
|
355 | - $this->access->expects($this->any()) |
|
356 | - ->method('getFilterPartForUserSearch') |
|
357 | - ->willReturnCallback(function ($search) { |
|
358 | - return $search; |
|
359 | - }); |
|
360 | - $this->access->expects($this->any()) |
|
361 | - ->method('combineFilterWithAnd') |
|
362 | - ->willReturnCallback(function ($param) { |
|
363 | - return $param[2]; |
|
364 | - }); |
|
365 | - $this->access->expects($this->any()) |
|
366 | - ->method('fetchListOfUsers') |
|
367 | - ->willReturnCallback(function ($search, $a, $l, $o) { |
|
368 | - $users = ['gunslinger', 'newyorker', 'ladyofshadows']; |
|
369 | - if (empty($search)) { |
|
370 | - $result = $users; |
|
371 | - } else { |
|
372 | - $result = []; |
|
373 | - foreach ($users as $user) { |
|
374 | - if (stripos($user, $search) !== false) { |
|
375 | - $result[] = $user; |
|
376 | - } |
|
377 | - } |
|
378 | - } |
|
379 | - if (!is_null($l) || !is_null($o)) { |
|
380 | - $result = array_slice($result, $o, $l); |
|
381 | - } |
|
382 | - return $result; |
|
383 | - }); |
|
384 | - $this->access->expects($this->any()) |
|
385 | - ->method('nextcloudUserNames') |
|
386 | - ->willReturnArgument(0); |
|
387 | - $this->access->method('fetchUsersByLoginName') |
|
388 | - ->willReturn([]); |
|
389 | - |
|
390 | - $this->access->userManager->expects($this->any()) |
|
391 | - ->method('getAttributes') |
|
392 | - ->willReturn(['dn', 'uid', 'mail', 'displayname']); |
|
393 | - } |
|
394 | - |
|
395 | - public function testGetUsersNoParam(): void { |
|
396 | - $this->prepareAccessForGetUsers(); |
|
397 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
398 | - |
|
399 | - $result = $backend->getUsers(); |
|
400 | - $this->assertCount(3, $result); |
|
401 | - } |
|
402 | - |
|
403 | - public function testGetUsersLimitOffset(): void { |
|
404 | - $this->prepareAccessForGetUsers(); |
|
405 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
406 | - |
|
407 | - $result = $backend->getUsers('', 1, 2); |
|
408 | - $this->assertCount(1, $result); |
|
409 | - } |
|
410 | - |
|
411 | - public function testGetUsersLimitOffset2(): void { |
|
412 | - $this->prepareAccessForGetUsers(); |
|
413 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
414 | - |
|
415 | - $result = $backend->getUsers('', 2, 1); |
|
416 | - $this->assertCount(2, $result); |
|
417 | - } |
|
418 | - |
|
419 | - public function testGetUsersSearchWithResult(): void { |
|
420 | - $this->prepareAccessForGetUsers(); |
|
421 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
422 | - |
|
423 | - $result = $backend->getUsers('yo'); |
|
424 | - $this->assertCount(2, $result); |
|
425 | - } |
|
426 | - |
|
427 | - public function testGetUsersSearchEmptyResult(): void { |
|
428 | - $this->prepareAccessForGetUsers(); |
|
429 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
430 | - |
|
431 | - $result = $backend->getUsers('nix'); |
|
432 | - $this->assertCount(0, $result); |
|
433 | - } |
|
434 | - |
|
435 | - private function getUsers($search = '', $limit = null, $offset = null) { |
|
436 | - $users = Server::get(IUserManager::class)->search($search, $limit, $offset); |
|
437 | - $uids = array_map(function (IUser $user) { |
|
438 | - return $user->getUID(); |
|
439 | - }, $users); |
|
440 | - return $uids; |
|
441 | - } |
|
442 | - |
|
443 | - public function testGetUsersViaAPINoParam(): void { |
|
444 | - $this->prepareAccessForGetUsers(); |
|
445 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
446 | - Server::get(IUserManager::class)->registerBackend($backend); |
|
447 | - |
|
448 | - $result = $this->getUsers(); |
|
449 | - $this->assertCount(3, $result); |
|
450 | - } |
|
451 | - |
|
452 | - public function testGetUsersViaAPILimitOffset(): void { |
|
453 | - $this->prepareAccessForGetUsers(); |
|
454 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
455 | - Server::get(IUserManager::class)->registerBackend($backend); |
|
456 | - |
|
457 | - $result = $this->getUsers('', 1, 2); |
|
458 | - $this->assertCount(1, $result); |
|
459 | - } |
|
460 | - |
|
461 | - public function testGetUsersViaAPILimitOffset2(): void { |
|
462 | - $this->prepareAccessForGetUsers(); |
|
463 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
464 | - Server::get(IUserManager::class)->registerBackend($backend); |
|
465 | - |
|
466 | - $result = $this->getUsers('', 2, 1); |
|
467 | - $this->assertCount(2, $result); |
|
468 | - } |
|
469 | - |
|
470 | - public function testGetUsersViaAPISearchWithResult(): void { |
|
471 | - $this->prepareAccessForGetUsers(); |
|
472 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
473 | - Server::get(IUserManager::class)->registerBackend($backend); |
|
474 | - |
|
475 | - $result = $this->getUsers('yo'); |
|
476 | - $this->assertCount(2, $result); |
|
477 | - } |
|
478 | - |
|
479 | - public function testGetUsersViaAPISearchEmptyResult(): void { |
|
480 | - $this->prepareAccessForGetUsers(); |
|
481 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
482 | - Server::get(IUserManager::class)->registerBackend($backend); |
|
483 | - |
|
484 | - $result = $this->getUsers('nix'); |
|
485 | - $this->assertCount(0, $result); |
|
486 | - } |
|
487 | - |
|
488 | - public function testUserExists(): void { |
|
489 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
490 | - $this->prepareMockForUserExists(); |
|
491 | - |
|
492 | - $this->userManager->expects($this->never()) |
|
493 | - ->method('get'); |
|
494 | - $this->userManager->expects($this->once()) |
|
495 | - ->method('exists') |
|
496 | - ->with('gunslinger') |
|
497 | - ->willReturn(true); |
|
498 | - $this->access->expects($this->any()) |
|
499 | - ->method('getUserMapper') |
|
500 | - ->willReturn($this->createMock(UserMapping::class)); |
|
501 | - |
|
502 | - //test for existing user |
|
503 | - /** @noinspection PhpUnhandledExceptionInspection */ |
|
504 | - $result = $backend->userExists('gunslinger'); |
|
505 | - $this->assertTrue($result); |
|
506 | - } |
|
507 | - |
|
508 | - public function testUserExistsForDeleted(): void { |
|
509 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
510 | - $this->prepareMockForUserExists(); |
|
511 | - |
|
512 | - $mapper = $this->createMock(UserMapping::class); |
|
513 | - $mapper->expects($this->any()) |
|
514 | - ->method('getUUIDByDN') |
|
515 | - ->with('dnOfFormerUser,dc=test') |
|
516 | - ->willReturn('45673458748'); |
|
517 | - |
|
518 | - $this->access->expects($this->any()) |
|
519 | - ->method('getUserMapper') |
|
520 | - ->willReturn($mapper); |
|
521 | - |
|
522 | - $this->userManager->expects($this->never()) |
|
523 | - ->method('get'); |
|
524 | - $this->userManager->expects($this->once()) |
|
525 | - ->method('exists') |
|
526 | - ->with('formerUser') |
|
527 | - ->willReturn(true); |
|
528 | - |
|
529 | - //test for deleted user – always returns true as long as we have the user in DB |
|
530 | - $this->assertTrue($backend->userExists('formerUser')); |
|
531 | - } |
|
532 | - |
|
533 | - public function testUserExistsForNeverExisting(): void { |
|
534 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
535 | - $this->prepareMockForUserExists(); |
|
536 | - |
|
537 | - $this->access->expects($this->any()) |
|
538 | - ->method('readAttribute') |
|
539 | - ->willReturnCallback(function ($dn) { |
|
540 | - if ($dn === 'dnOfRoland,dc=test') { |
|
541 | - return []; |
|
542 | - } |
|
543 | - return false; |
|
544 | - }); |
|
545 | - |
|
546 | - //test for never-existing user |
|
547 | - /** @noinspection PhpUnhandledExceptionInspection */ |
|
548 | - $result = $backend->userExists('mallory'); |
|
549 | - $this->assertFalse($result); |
|
550 | - } |
|
551 | - |
|
552 | - public function testUserExistsPublicAPI(): void { |
|
553 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
554 | - $this->prepareMockForUserExists(); |
|
555 | - Server::get(IUserManager::class)->registerBackend($backend); |
|
556 | - |
|
557 | - $user = $this->createMock(User::class); |
|
558 | - $user->expects($this->any()) |
|
559 | - ->method('getDN') |
|
560 | - ->willReturn('dnOfRoland,dc=test'); |
|
561 | - |
|
562 | - $this->access->expects($this->any()) |
|
563 | - ->method('readAttribute') |
|
564 | - ->willReturnCallback(function ($dn) { |
|
565 | - if ($dn === 'dnOfRoland,dc=test') { |
|
566 | - return []; |
|
567 | - } |
|
568 | - return false; |
|
569 | - }); |
|
570 | - $this->userManager->expects($this->never()) |
|
571 | - ->method('get'); |
|
572 | - $this->userManager->expects($this->once()) |
|
573 | - ->method('exists') |
|
574 | - ->with('gunslinger') |
|
575 | - ->willReturn(true); |
|
576 | - $this->access->expects($this->any()) |
|
577 | - ->method('getUserMapper') |
|
578 | - ->willReturn($this->createMock(UserMapping::class)); |
|
579 | - |
|
580 | - //test for existing user |
|
581 | - $result = Server::get(IUserManager::class)->userExists('gunslinger'); |
|
582 | - $this->assertTrue($result); |
|
583 | - } |
|
584 | - |
|
585 | - public function testDeleteUserExisting(): void { |
|
586 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
587 | - |
|
588 | - //we do not support deleting existing users at all |
|
589 | - $result = $backend->deleteUser('gunslinger'); |
|
590 | - $this->assertFalse($result); |
|
591 | - } |
|
592 | - |
|
593 | - public function testGetHomeAbsolutePath(): void { |
|
594 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
595 | - $this->prepareMockForUserExists(); |
|
596 | - |
|
597 | - $this->connection->expects($this->any()) |
|
598 | - ->method('__get') |
|
599 | - ->willReturnCallback(function ($name) { |
|
600 | - if ($name === 'homeFolderNamingRule') { |
|
601 | - return 'attr:testAttribute'; |
|
602 | - } |
|
603 | - return null; |
|
604 | - }); |
|
605 | - |
|
606 | - $this->access->expects($this->any()) |
|
607 | - ->method('readAttribute') |
|
608 | - ->willReturnCallback(function ($dn, $attr) { |
|
609 | - switch ($dn) { |
|
610 | - case 'dnOfRoland,dc=test': |
|
611 | - if ($attr === 'testAttribute') { |
|
612 | - return ['/tmp/rolandshome/']; |
|
613 | - } |
|
614 | - return []; |
|
615 | - break; |
|
616 | - default: |
|
617 | - return false; |
|
618 | - } |
|
619 | - }); |
|
620 | - |
|
621 | - $user = $this->createMock(User::class); |
|
622 | - $user->expects($this->any()) |
|
623 | - ->method('getUsername') |
|
624 | - ->willReturn('gunslinger'); |
|
625 | - $user->expects($this->any()) |
|
626 | - ->method('getDN') |
|
627 | - ->willReturn('dnOfRoland,dc=test'); |
|
628 | - $user->expects($this->any()) |
|
629 | - ->method('getHomePath') |
|
630 | - ->willReturn('/tmp/rolandshome/'); |
|
631 | - |
|
632 | - $this->userManager->expects($this->atLeastOnce()) |
|
633 | - ->method('get') |
|
634 | - ->with('gunslinger') |
|
635 | - ->willReturn($user); |
|
636 | - $this->userManager->expects($this->once()) |
|
637 | - ->method('exists') |
|
638 | - ->with('gunslinger') |
|
639 | - ->willReturn(true); |
|
640 | - |
|
641 | - //absolute path |
|
642 | - /** @noinspection PhpUnhandledExceptionInspection */ |
|
643 | - $result = $backend->getHome('gunslinger'); |
|
644 | - $this->assertEquals('/tmp/rolandshome/', $result); |
|
645 | - } |
|
646 | - |
|
647 | - public function testGetHomeRelative(): void { |
|
648 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
649 | - $this->prepareMockForUserExists(); |
|
650 | - |
|
651 | - $dataDir = Server::get(IConfig::class)->getSystemValue( |
|
652 | - 'datadirectory', \OC::$SERVERROOT . '/data'); |
|
653 | - |
|
654 | - $this->connection->expects($this->any()) |
|
655 | - ->method('__get') |
|
656 | - ->willReturnCallback(function ($name) { |
|
657 | - if ($name === 'homeFolderNamingRule') { |
|
658 | - return 'attr:testAttribute'; |
|
659 | - } |
|
660 | - return null; |
|
661 | - }); |
|
662 | - |
|
663 | - $this->access->expects($this->any()) |
|
664 | - ->method('readAttribute') |
|
665 | - ->willReturnCallback(function ($dn, $attr) { |
|
666 | - switch ($dn) { |
|
667 | - case 'dnOfLadyOfShadows,dc=test': |
|
668 | - if ($attr === 'testAttribute') { |
|
669 | - return ['susannah/']; |
|
670 | - } |
|
671 | - return []; |
|
672 | - break; |
|
673 | - default: |
|
674 | - return false; |
|
675 | - } |
|
676 | - }); |
|
677 | - |
|
678 | - $user = $this->createMock(User::class); |
|
679 | - $user->expects($this->any()) |
|
680 | - ->method('getUsername') |
|
681 | - ->willReturn('ladyofshadows'); |
|
682 | - $user->expects($this->any()) |
|
683 | - ->method('getDN') |
|
684 | - ->willReturn('dnOfLadyOfShadows,dc=test'); |
|
685 | - $user->expects($this->any()) |
|
686 | - ->method('getHomePath') |
|
687 | - ->willReturn($dataDir . '/susannah/'); |
|
688 | - |
|
689 | - $this->userManager->expects($this->atLeastOnce()) |
|
690 | - ->method('get') |
|
691 | - ->willReturn($user); |
|
692 | - $this->userManager->expects($this->once()) |
|
693 | - ->method('exists') |
|
694 | - ->with('ladyofshadows') |
|
695 | - ->willReturn(true); |
|
696 | - |
|
697 | - /** @noinspection PhpUnhandledExceptionInspection */ |
|
698 | - $result = $backend->getHome('ladyofshadows'); |
|
699 | - $this->assertEquals($dataDir . '/susannah/', $result); |
|
700 | - } |
|
701 | - |
|
702 | - |
|
703 | - public function testGetHomeNoPath(): void { |
|
704 | - $this->expectException(\Exception::class); |
|
705 | - |
|
706 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
707 | - $this->prepareMockForUserExists(); |
|
708 | - |
|
709 | - $this->connection->expects($this->any()) |
|
710 | - ->method('__get') |
|
711 | - ->willReturnCallback(function ($name) { |
|
712 | - if ($name === 'homeFolderNamingRule') { |
|
713 | - return 'attr:testAttribute'; |
|
714 | - } |
|
715 | - return null; |
|
716 | - }); |
|
717 | - $this->access->expects($this->any()) |
|
718 | - ->method('readAttribute') |
|
719 | - ->willReturnCallback(function ($dn, $attr) { |
|
720 | - switch ($dn) { |
|
721 | - default: |
|
722 | - return false; |
|
723 | - } |
|
724 | - }); |
|
725 | - |
|
726 | - $user = $this->createMock(User::class); |
|
727 | - $user->expects($this->any()) |
|
728 | - ->method('getUsername') |
|
729 | - ->willReturn('newyorker'); |
|
730 | - $user->expects($this->any()) |
|
731 | - ->method('getHomePath') |
|
732 | - ->willThrowException(new \Exception()); |
|
733 | - |
|
734 | - $this->userManager->expects($this->atLeastOnce()) |
|
735 | - ->method('get') |
|
736 | - ->with('newyorker') |
|
737 | - ->willReturn($user); |
|
738 | - $this->userManager->expects($this->once()) |
|
739 | - ->method('exists') |
|
740 | - ->with('newyorker') |
|
741 | - ->willReturn(true); |
|
742 | - |
|
743 | - //no path at all – triggers OC default behaviour |
|
744 | - $result = $backend->getHome('newyorker'); |
|
745 | - $this->assertFalse($result); |
|
746 | - } |
|
747 | - |
|
748 | - public function testGetHomeDeletedUser(): void { |
|
749 | - $uid = 'newyorker'; |
|
750 | - |
|
751 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
752 | - $this->prepareMockForUserExists(); |
|
753 | - |
|
754 | - $this->connection->expects($this->any()) |
|
755 | - ->method('__get') |
|
756 | - ->willReturnCallback(function ($name) { |
|
757 | - if ($name === 'homeFolderNamingRule') { |
|
758 | - return 'attr:testAttribute'; |
|
759 | - } |
|
760 | - return null; |
|
761 | - }); |
|
762 | - |
|
763 | - $this->access->expects($this->any()) |
|
764 | - ->method('readAttribute') |
|
765 | - ->willReturn([]); |
|
766 | - |
|
767 | - $userMapper = $this->createMock(UserMapping::class); |
|
768 | - |
|
769 | - $this->access->expects($this->any()) |
|
770 | - ->method('getUserMapper') |
|
771 | - ->willReturn($userMapper); |
|
772 | - |
|
773 | - $offlineUser = $this->createMock(OfflineUser::class); |
|
774 | - $offlineUser->expects($this->atLeastOnce()) |
|
775 | - ->method('getHomePath') |
|
776 | - ->willReturn(''); |
|
777 | - |
|
778 | - $this->userManager->expects($this->atLeastOnce()) |
|
779 | - ->method('get') |
|
780 | - ->with($uid) |
|
781 | - ->willReturn($offlineUser); |
|
782 | - $this->userManager->expects($this->once()) |
|
783 | - ->method('exists') |
|
784 | - ->with($uid) |
|
785 | - ->willReturn(true); |
|
786 | - |
|
787 | - $result = $backend->getHome($uid); |
|
788 | - $this->assertFalse($result); |
|
789 | - } |
|
790 | - |
|
791 | - public function testGetHomeWithPlugin(): void { |
|
792 | - $this->pluginManager->expects($this->once()) |
|
793 | - ->method('implementsActions') |
|
794 | - ->with(Backend::GET_HOME) |
|
795 | - ->willReturn(true); |
|
796 | - $this->pluginManager->expects($this->once()) |
|
797 | - ->method('getHome') |
|
798 | - ->with('uid') |
|
799 | - ->willReturn('result'); |
|
800 | - |
|
801 | - $this->connection->expects($this->any()) |
|
802 | - ->method('getFromCache') |
|
803 | - ->willReturnCallback(function ($uid) { |
|
804 | - return true; |
|
805 | - }); |
|
806 | - |
|
807 | - /** @noinspection PhpUnhandledExceptionInspection */ |
|
808 | - $this->assertEquals($this->backend->getHome('uid'), 'result'); |
|
809 | - } |
|
810 | - |
|
811 | - private function prepareAccessForGetDisplayName() { |
|
812 | - $this->connection->expects($this->any()) |
|
813 | - ->method('__get') |
|
814 | - ->willReturnCallback(function ($name) { |
|
815 | - if ($name === 'ldapUserDisplayName') { |
|
816 | - return 'displayname'; |
|
817 | - } elseif ($name === 'ldapUserDisplayName2') { |
|
818 | - return 'displayname2'; |
|
819 | - } |
|
820 | - return null; |
|
821 | - }); |
|
822 | - |
|
823 | - $this->access->expects($this->any()) |
|
824 | - ->method('readAttribute') |
|
825 | - ->willReturnCallback(function ($dn, $attr) { |
|
826 | - switch ($dn) { |
|
827 | - case 'dnOfRoland,dc=test': |
|
828 | - if ($attr === 'displayname') { |
|
829 | - return ['Roland Deschain']; |
|
830 | - } |
|
831 | - return []; |
|
832 | - break; |
|
833 | - |
|
834 | - default: |
|
835 | - return false; |
|
836 | - } |
|
837 | - }); |
|
838 | - $this->access->method('fetchUsersByLoginName') |
|
839 | - ->willReturn([]); |
|
840 | - } |
|
841 | - |
|
842 | - public function testGetDisplayName(): void { |
|
843 | - $this->prepareAccessForGetDisplayName(); |
|
844 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
845 | - $this->prepareMockForUserExists(); |
|
846 | - |
|
847 | - $this->connection->expects($this->any()) |
|
848 | - ->method('getConnectionResource') |
|
849 | - ->willReturnCallback(function () { |
|
850 | - return true; |
|
851 | - }); |
|
852 | - |
|
853 | - $user1 = $this->createMock(User::class); |
|
854 | - $user1->expects($this->once()) |
|
855 | - ->method('composeAndStoreDisplayName') |
|
856 | - ->willReturn('Roland Deschain'); |
|
857 | - $user1->expects($this->any()) |
|
858 | - ->method('getDN') |
|
859 | - ->willReturn('dnOfRoland,dc=test'); |
|
860 | - |
|
861 | - $user2 = $this->createMock(User::class); |
|
862 | - $user2->expects($this->never()) |
|
863 | - ->method('composeAndStoreDisplayName'); |
|
864 | - $user2->expects($this->any()) |
|
865 | - ->method('getDN') |
|
866 | - ->willReturn('another DN'); |
|
867 | - |
|
868 | - $mapper = $this->createMock(UserMapping::class); |
|
869 | - $mapper->expects($this->any()) |
|
870 | - ->method('getUUIDByDN') |
|
871 | - ->willReturnCallback(function ($dn) { |
|
872 | - return $dn; |
|
873 | - }); |
|
874 | - |
|
875 | - $this->userManager->expects($this->any()) |
|
876 | - ->method('get') |
|
877 | - ->willReturnCallback(function ($uid) use ($user1, $user2) { |
|
878 | - if ($uid === 'gunslinger') { |
|
879 | - return $user1; |
|
880 | - } elseif ($uid === 'newyorker') { |
|
881 | - return $user2; |
|
882 | - } |
|
883 | - return null; |
|
884 | - }); |
|
885 | - $this->userManager->expects($this->any()) |
|
886 | - ->method('exists') |
|
887 | - ->willReturnCallback(function ($uid) use ($user1, $user2) { |
|
888 | - if ($uid === 'gunslinger') { |
|
889 | - return true; |
|
890 | - } elseif ($uid === 'newyorker') { |
|
891 | - return true; |
|
892 | - } |
|
893 | - return false; |
|
894 | - }); |
|
895 | - $this->access->expects($this->any()) |
|
896 | - ->method('getUserMapper') |
|
897 | - ->willReturn($mapper); |
|
898 | - $this->access->expects($this->any()) |
|
899 | - ->method('getUserDnByUuid') |
|
900 | - ->willReturnCallback(function ($uuid) { |
|
901 | - return $uuid . '1'; |
|
902 | - }); |
|
903 | - |
|
904 | - //with displayName |
|
905 | - $result = $backend->getDisplayName('gunslinger'); |
|
906 | - $this->assertEquals('Roland Deschain', $result); |
|
907 | - |
|
908 | - //empty displayname retrieved |
|
909 | - $result = $backend->getDisplayName('newyorker'); |
|
910 | - $this->assertEquals(null, $result); |
|
911 | - } |
|
912 | - |
|
913 | - public function testGetDisplayNamePublicAPI(): void { |
|
914 | - $this->access->expects($this->any()) |
|
915 | - ->method('username2dn') |
|
916 | - ->willReturnCallback(function ($uid) { |
|
917 | - switch ($uid) { |
|
918 | - case 'gunslinger': |
|
919 | - return 'dnOfRoland,dc=test'; |
|
920 | - break; |
|
921 | - case 'formerUser': |
|
922 | - return 'dnOfFormerUser,dc=test'; |
|
923 | - break; |
|
924 | - case 'newyorker': |
|
925 | - return 'dnOfNewYorker,dc=test'; |
|
926 | - break; |
|
927 | - case 'ladyofshadows': |
|
928 | - return 'dnOfLadyOfShadows,dc=test'; |
|
929 | - break; |
|
930 | - default: |
|
931 | - return false; |
|
932 | - } |
|
933 | - }); |
|
934 | - $this->prepareAccessForGetDisplayName(); |
|
935 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
936 | - $this->prepareMockForUserExists(); |
|
937 | - |
|
938 | - $this->connection->expects($this->any()) |
|
939 | - ->method('getConnectionResource') |
|
940 | - ->willReturnCallback(function () { |
|
941 | - return true; |
|
942 | - }); |
|
943 | - |
|
944 | - Server::get(IUserManager::class)->registerBackend($backend); |
|
945 | - |
|
946 | - $user1 = $this->createMock(User::class); |
|
947 | - $user1->expects($this->once()) |
|
948 | - ->method('composeAndStoreDisplayName') |
|
949 | - ->willReturn('Roland Deschain'); |
|
950 | - $user1->expects($this->any()) |
|
951 | - ->method('getDN') |
|
952 | - ->willReturn('dnOfRoland,dc=test'); |
|
953 | - |
|
954 | - $user2 = $this->createMock(User::class); |
|
955 | - $user2->expects($this->never()) |
|
956 | - ->method('composeAndStoreDisplayName'); |
|
957 | - $user2->expects($this->any()) |
|
958 | - ->method('getDN') |
|
959 | - ->willReturn('another DN'); |
|
960 | - |
|
961 | - $mapper = $this->createMock(UserMapping::class); |
|
962 | - $mapper->expects($this->any()) |
|
963 | - ->method('getUUIDByDN') |
|
964 | - ->willReturnCallback(function ($dn) { |
|
965 | - return $dn; |
|
966 | - }); |
|
967 | - |
|
968 | - $this->userManager->expects($this->any()) |
|
969 | - ->method('get') |
|
970 | - ->willReturnCallback(function ($uid) use ($user1, $user2) { |
|
971 | - if ($uid === 'gunslinger') { |
|
972 | - return $user1; |
|
973 | - } elseif ($uid === 'newyorker') { |
|
974 | - return $user2; |
|
975 | - } |
|
976 | - return null; |
|
977 | - }); |
|
978 | - $this->userManager->expects($this->any()) |
|
979 | - ->method('exists') |
|
980 | - ->willReturnCallback(function ($uid) use ($user1, $user2) { |
|
981 | - if ($uid === 'gunslinger') { |
|
982 | - return true; |
|
983 | - } elseif ($uid === 'newyorker') { |
|
984 | - return true; |
|
985 | - } |
|
986 | - return false; |
|
987 | - }); |
|
988 | - $this->access->expects($this->any()) |
|
989 | - ->method('getUserMapper') |
|
990 | - ->willReturn($mapper); |
|
991 | - $this->access->expects($this->any()) |
|
992 | - ->method('getUserDnByUuid') |
|
993 | - ->willReturnCallback(function ($uuid) { |
|
994 | - return $uuid . '1'; |
|
995 | - }); |
|
996 | - |
|
997 | - //with displayName |
|
998 | - $result = Server::get(IUserManager::class)->get('gunslinger')?->getDisplayName(); |
|
999 | - $this->assertEquals('Roland Deschain', $result); |
|
1000 | - |
|
1001 | - //empty displayname retrieved |
|
1002 | - $result = Server::get(IUserManager::class)->get('newyorker') === null ? 'newyorker' : Server::get(IUserManager::class)->get('newyorker')->getDisplayName(); |
|
1003 | - $this->assertEquals('newyorker', $result); |
|
1004 | - } |
|
1005 | - |
|
1006 | - public function testGetDisplayNameWithPlugin(): void { |
|
1007 | - $this->pluginManager->expects($this->once()) |
|
1008 | - ->method('implementsActions') |
|
1009 | - ->with(Backend::GET_DISPLAYNAME) |
|
1010 | - ->willReturn(true); |
|
1011 | - $this->pluginManager->expects($this->once()) |
|
1012 | - ->method('getDisplayName') |
|
1013 | - ->with('uid') |
|
1014 | - ->willReturn('result'); |
|
1015 | - |
|
1016 | - $this->assertEquals($this->backend->getDisplayName('uid'), 'result'); |
|
1017 | - } |
|
1018 | - |
|
1019 | - //no test for getDisplayNames, because it just invokes getUsers and |
|
1020 | - //getDisplayName |
|
1021 | - |
|
1022 | - public function testCountUsers(): void { |
|
1023 | - $this->access->expects($this->once()) |
|
1024 | - ->method('countUsers') |
|
1025 | - ->willReturn(5); |
|
1026 | - |
|
1027 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
1028 | - |
|
1029 | - $result = $backend->countUsers(); |
|
1030 | - $this->assertEquals(5, $result); |
|
1031 | - } |
|
1032 | - |
|
1033 | - public function testCountUsersFailing(): void { |
|
1034 | - $this->access->expects($this->once()) |
|
1035 | - ->method('countUsers') |
|
1036 | - ->willReturn(false); |
|
1037 | - |
|
1038 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
1039 | - |
|
1040 | - $result = $backend->countUsers(); |
|
1041 | - $this->assertFalse($result); |
|
1042 | - } |
|
1043 | - |
|
1044 | - public function testCountUsersWithPlugin(): void { |
|
1045 | - $this->pluginManager->expects($this->once()) |
|
1046 | - ->method('implementsActions') |
|
1047 | - ->with(Backend::COUNT_USERS) |
|
1048 | - ->willReturn(true); |
|
1049 | - $this->pluginManager->expects($this->once()) |
|
1050 | - ->method('countUsers') |
|
1051 | - ->willReturn(42); |
|
1052 | - |
|
1053 | - $this->assertEquals($this->backend->countUsers(), 42); |
|
1054 | - } |
|
1055 | - |
|
1056 | - public function testLoginName2UserNameSuccess(): void { |
|
1057 | - $loginName = 'Alice'; |
|
1058 | - $username = 'alice'; |
|
1059 | - $dn = 'uid=alice,dc=what,dc=ever'; |
|
1060 | - |
|
1061 | - $this->access->expects($this->once()) |
|
1062 | - ->method('fetchUsersByLoginName') |
|
1063 | - ->with($this->equalTo($loginName)) |
|
1064 | - ->willReturn([['dn' => [$dn]]]); |
|
1065 | - $this->access->expects($this->any()) |
|
1066 | - ->method('stringResemblesDN') |
|
1067 | - ->with($this->equalTo($dn)) |
|
1068 | - ->willReturn(true); |
|
1069 | - $this->access->expects($this->any()) |
|
1070 | - ->method('dn2username') |
|
1071 | - ->with($this->equalTo($dn)) |
|
1072 | - ->willReturn($username); |
|
1073 | - |
|
1074 | - $this->connection->expects($this->exactly(2)) |
|
1075 | - ->method('getFromCache') |
|
1076 | - ->with($this->equalTo('loginName2UserName-' . $loginName)) |
|
1077 | - ->willReturnOnConsecutiveCalls(null, $username); |
|
1078 | - $this->connection->expects($this->once()) |
|
1079 | - ->method('writeToCache') |
|
1080 | - ->with($this->equalTo('loginName2UserName-' . $loginName), $this->equalTo($username)); |
|
1081 | - |
|
1082 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
1083 | - $user = $this->createMock(User::class); |
|
1084 | - $user->expects($this->any()) |
|
1085 | - ->method('getUsername') |
|
1086 | - ->willReturn('alice'); |
|
1087 | - |
|
1088 | - $this->userManager->expects($this->atLeastOnce()) |
|
1089 | - ->method('get') |
|
1090 | - ->with($dn) |
|
1091 | - ->willReturn($user); |
|
1092 | - $this->userManager->expects($this->never()) |
|
1093 | - ->method('exists'); |
|
1094 | - $this->userManager->expects($this->any()) |
|
1095 | - ->method('getAttributes') |
|
1096 | - ->willReturn(['dn', 'uid', 'mail', 'displayname']); |
|
1097 | - |
|
1098 | - $name = $backend->loginName2UserName($loginName); |
|
1099 | - $this->assertSame($username, $name); |
|
1100 | - |
|
1101 | - // and once again to verify that caching works |
|
1102 | - $backend->loginName2UserName($loginName); |
|
1103 | - } |
|
1104 | - |
|
1105 | - public function testLoginName2UserNameNoUsersOnLDAP(): void { |
|
1106 | - $loginName = 'Loki'; |
|
1107 | - |
|
1108 | - $this->access->expects($this->once()) |
|
1109 | - ->method('fetchUsersByLoginName') |
|
1110 | - ->with($this->equalTo($loginName)) |
|
1111 | - ->willReturn([]); |
|
1112 | - $this->access->expects($this->never()) |
|
1113 | - ->method('stringResemblesDN'); |
|
1114 | - $this->access->expects($this->never()) |
|
1115 | - ->method('dn2username'); |
|
1116 | - |
|
1117 | - $this->connection->expects($this->exactly(2)) |
|
1118 | - ->method('getFromCache') |
|
1119 | - ->with($this->equalTo('loginName2UserName-' . $loginName)) |
|
1120 | - ->willReturnOnConsecutiveCalls(null, false); |
|
1121 | - $this->connection->expects($this->once()) |
|
1122 | - ->method('writeToCache') |
|
1123 | - ->with($this->equalTo('loginName2UserName-' . $loginName), false); |
|
1124 | - |
|
1125 | - $this->userManager->expects($this->any()) |
|
1126 | - ->method('getAttributes') |
|
1127 | - ->willReturn(['dn', 'uid', 'mail', 'displayname']); |
|
1128 | - |
|
1129 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
1130 | - $name = $backend->loginName2UserName($loginName); |
|
1131 | - $this->assertSame(false, $name); |
|
1132 | - |
|
1133 | - // and once again to verify that caching works |
|
1134 | - $backend->loginName2UserName($loginName); |
|
1135 | - } |
|
1136 | - |
|
1137 | - public function testLoginName2UserNameOfflineUser(): void { |
|
1138 | - $loginName = 'Alice'; |
|
1139 | - $dn = 'uid=alice,dc=what,dc=ever'; |
|
1140 | - |
|
1141 | - $offlineUser = $this->getMockBuilder(OfflineUser::class) |
|
1142 | - ->disableOriginalConstructor() |
|
1143 | - ->getMock(); |
|
1144 | - |
|
1145 | - $this->access->expects($this->once()) |
|
1146 | - ->method('fetchUsersByLoginName') |
|
1147 | - ->with($this->equalTo($loginName)) |
|
1148 | - ->willReturn([['dn' => [$dn]]]); |
|
1149 | - |
|
1150 | - $this->connection->expects($this->exactly(2)) |
|
1151 | - ->method('getFromCache') |
|
1152 | - ->with($this->equalTo('loginName2UserName-' . $loginName)) |
|
1153 | - ->willReturnOnConsecutiveCalls(null, false); |
|
1154 | - $this->connection->expects($this->once()) |
|
1155 | - ->method('writeToCache') |
|
1156 | - ->with($this->equalTo('loginName2UserName-' . $loginName), $this->equalTo(false)); |
|
1157 | - |
|
1158 | - $this->userManager->expects($this->any()) |
|
1159 | - ->method('get') |
|
1160 | - ->with($dn) |
|
1161 | - ->willReturn($offlineUser); |
|
1162 | - $this->userManager->expects($this->any()) |
|
1163 | - ->method('getAttributes') |
|
1164 | - ->willReturn(['dn', 'uid', 'mail', 'displayname']); |
|
1165 | - |
|
1166 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
1167 | - $name = $backend->loginName2UserName($loginName); |
|
1168 | - $this->assertSame(false, $name); |
|
1169 | - |
|
1170 | - // and once again to verify that caching works |
|
1171 | - $backend->loginName2UserName($loginName); |
|
1172 | - } |
|
1173 | - |
|
1174 | - /** |
|
1175 | - * Prepares the Access mock for setPassword tests |
|
1176 | - */ |
|
1177 | - private function prepareAccessForSetPassword($enablePasswordChange = true) { |
|
1178 | - $this->connection->expects($this->any()) |
|
1179 | - ->method('__get') |
|
1180 | - ->willReturnCallback(function ($name) use (&$enablePasswordChange) { |
|
1181 | - if ($name === 'ldapLoginFilter') { |
|
1182 | - return '%uid'; |
|
1183 | - } |
|
1184 | - if ($name === 'turnOnPasswordChange') { |
|
1185 | - return $enablePasswordChange?1:0; |
|
1186 | - } |
|
1187 | - return null; |
|
1188 | - }); |
|
1189 | - $this->connection->expects($this->any()) |
|
1190 | - ->method('getFromCache') |
|
1191 | - ->willReturnCallback(function ($uid) { |
|
1192 | - if ($uid === 'userExists' . 'roland') { |
|
1193 | - return true; |
|
1194 | - } |
|
1195 | - return null; |
|
1196 | - }); |
|
1197 | - |
|
1198 | - $this->access->expects($this->any()) |
|
1199 | - ->method('fetchListOfUsers') |
|
1200 | - ->willReturnCallback(function ($filter) { |
|
1201 | - if ($filter === 'roland') { |
|
1202 | - return [['dn' => ['dnOfRoland,dc=test']]]; |
|
1203 | - } |
|
1204 | - return []; |
|
1205 | - }); |
|
1206 | - $this->access->expects($this->any()) |
|
1207 | - ->method('fetchUsersByLoginName') |
|
1208 | - ->willReturnCallback(function ($uid) { |
|
1209 | - if ($uid === 'roland') { |
|
1210 | - return [['dn' => ['dnOfRoland,dc=test']]]; |
|
1211 | - } |
|
1212 | - return []; |
|
1213 | - }); |
|
1214 | - $this->access->expects($this->any()) |
|
1215 | - ->method('dn2username') |
|
1216 | - ->with($this->equalTo('dnOfRoland,dc=test')) |
|
1217 | - ->willReturn('roland'); |
|
1218 | - $this->access->expects($this->any()) |
|
1219 | - ->method('stringResemblesDN') |
|
1220 | - ->with($this->equalTo('dnOfRoland,dc=test')) |
|
1221 | - ->willReturn(true); |
|
1222 | - $this->access->expects($this->any()) |
|
1223 | - ->method('setPassword') |
|
1224 | - ->willReturnCallback(function ($uid, $password) { |
|
1225 | - if (strlen($password) <= 5) { |
|
1226 | - throw new HintException('Password fails quality checking policy', '', 19); |
|
1227 | - } |
|
1228 | - return true; |
|
1229 | - }); |
|
1230 | - } |
|
1231 | - |
|
1232 | - |
|
1233 | - public function testSetPasswordInvalid(): void { |
|
1234 | - $this->expectException(HintException::class); |
|
1235 | - $this->expectExceptionMessage('Password fails quality checking policy'); |
|
1236 | - |
|
1237 | - $this->prepareAccessForSetPassword($this->access); |
|
1238 | - $this->userManager->expects($this->atLeastOnce()) |
|
1239 | - ->method('get') |
|
1240 | - ->willReturn($this->createMock(User::class)); |
|
1241 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
1242 | - Server::get(IUserManager::class)->registerBackend($backend); |
|
1243 | - |
|
1244 | - $this->assertTrue(\OC_User::setPassword('roland', 'dt')); |
|
1245 | - } |
|
1246 | - |
|
1247 | - public function testSetPasswordValid(): void { |
|
1248 | - $this->prepareAccessForSetPassword($this->access); |
|
1249 | - |
|
1250 | - $this->userManager->expects($this->any()) |
|
1251 | - ->method('get') |
|
1252 | - ->willReturn($this->createMock(User::class)); |
|
1253 | - |
|
1254 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
1255 | - $this->userManager->expects($this->any()) |
|
1256 | - ->method('get') |
|
1257 | - ->willReturn($this->createMock(User::class)); |
|
1258 | - |
|
1259 | - Server::get(IUserManager::class)->registerBackend($backend); |
|
1260 | - |
|
1261 | - $this->assertTrue(\OC_User::setPassword('roland', 'dt12234$')); |
|
1262 | - } |
|
1263 | - |
|
1264 | - public function testSetPasswordValidDisabled(): void { |
|
1265 | - $this->userManager->expects($this->any()) |
|
1266 | - ->method('get') |
|
1267 | - ->willReturn($this->createMock(User::class)); |
|
1268 | - |
|
1269 | - $this->prepareAccessForSetPassword(false); |
|
1270 | - $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
1271 | - Server::get(IUserManager::class)->registerBackend($backend); |
|
1272 | - |
|
1273 | - $this->assertFalse(\OC_User::setPassword('roland', 'dt12234$')); |
|
1274 | - } |
|
1275 | - |
|
1276 | - |
|
1277 | - public function testSetPasswordWithInvalidUser(): void { |
|
1278 | - $this->expectException(\Exception::class); |
|
1279 | - $this->expectExceptionMessage('LDAP setPassword: Could not get user object for uid NotExistingUser. Maybe the LDAP entry has no set display name attribute?'); |
|
1280 | - |
|
1281 | - $this->userManager |
|
1282 | - ->expects($this->once()) |
|
1283 | - ->method('get') |
|
1284 | - ->with('NotExistingUser') |
|
1285 | - ->willReturn(null); |
|
1286 | - |
|
1287 | - $this->backend->setPassword('NotExistingUser', 'Password'); |
|
1288 | - } |
|
1289 | - |
|
1290 | - public function testSetPasswordWithUsernameFalse(): void { |
|
1291 | - $user = $this->createMock(User::class); |
|
1292 | - $user |
|
1293 | - ->expects($this->once()) |
|
1294 | - ->method('getUsername') |
|
1295 | - ->willReturn(false); |
|
1296 | - $this->userManager |
|
1297 | - ->expects($this->once()) |
|
1298 | - ->method('get') |
|
1299 | - ->with('NotExistingUser') |
|
1300 | - ->willReturn($user); |
|
1301 | - |
|
1302 | - /** @noinspection PhpUnhandledExceptionInspection */ |
|
1303 | - $this->assertFalse($this->backend->setPassword('NotExistingUser', 'Password')); |
|
1304 | - } |
|
1305 | - |
|
1306 | - public function testSetPasswordWithPlugin(): void { |
|
1307 | - $this->pluginManager->expects($this->once()) |
|
1308 | - ->method('implementsActions') |
|
1309 | - ->with(Backend::SET_PASSWORD) |
|
1310 | - ->willReturn(true); |
|
1311 | - $this->pluginManager->expects($this->once()) |
|
1312 | - ->method('setPassword') |
|
1313 | - ->with('uid', 'password') |
|
1314 | - ->willReturn('result'); |
|
1315 | - |
|
1316 | - /** @noinspection PhpUnhandledExceptionInspection */ |
|
1317 | - $this->assertEquals($this->backend->setPassword('uid', 'password'), 'result'); |
|
1318 | - } |
|
1319 | - |
|
1320 | - public static function avatarDataProvider(): array { |
|
1321 | - return [ |
|
1322 | - [ 'validImageData', false ], |
|
1323 | - [ 'corruptImageData', true ], |
|
1324 | - [ false, true] |
|
1325 | - ]; |
|
1326 | - } |
|
1327 | - |
|
1328 | - #[\PHPUnit\Framework\Attributes\DataProvider('avatarDataProvider')] |
|
1329 | - public function testCanChangeAvatar(string|bool $imageData, bool $expected): void { |
|
1330 | - $isValidImage = str_starts_with((string)$imageData, 'valid'); |
|
1331 | - |
|
1332 | - $user = $this->createMock(User::class); |
|
1333 | - $user->expects($this->once()) |
|
1334 | - ->method('getAvatarImage') |
|
1335 | - ->willReturn($imageData); |
|
1336 | - $user->expects($this->atMost(1)) |
|
1337 | - ->method('updateAvatar') |
|
1338 | - ->willReturn($isValidImage); |
|
1339 | - |
|
1340 | - $this->userManager->expects($this->atLeastOnce()) |
|
1341 | - ->method('get') |
|
1342 | - ->willReturn($user); |
|
1343 | - |
|
1344 | - /** @noinspection PhpUnhandledExceptionInspection */ |
|
1345 | - $this->assertSame($expected, $this->backend->canChangeAvatar('uid')); |
|
1346 | - } |
|
1347 | - |
|
1348 | - public function testCanChangeAvatarWithPlugin(): void { |
|
1349 | - $this->pluginManager->expects($this->once()) |
|
1350 | - ->method('implementsActions') |
|
1351 | - ->with(Backend::PROVIDE_AVATAR) |
|
1352 | - ->willReturn(true); |
|
1353 | - $this->pluginManager->expects($this->once()) |
|
1354 | - ->method('canChangeAvatar') |
|
1355 | - ->with('uid') |
|
1356 | - ->willReturn('result'); |
|
1357 | - |
|
1358 | - $this->assertEquals($this->backend->canChangeAvatar('uid'), 'result'); |
|
1359 | - } |
|
1360 | - |
|
1361 | - public function testSetDisplayNameWithPlugin(): void { |
|
1362 | - $newDisplayName = 'J. Baker'; |
|
1363 | - $this->pluginManager->expects($this->once()) |
|
1364 | - ->method('implementsActions') |
|
1365 | - ->with(Backend::SET_DISPLAYNAME) |
|
1366 | - ->willReturn(true); |
|
1367 | - $this->pluginManager->expects($this->once()) |
|
1368 | - ->method('setDisplayName') |
|
1369 | - ->with('uid', $newDisplayName) |
|
1370 | - ->willReturn($newDisplayName); |
|
1371 | - $this->access->expects($this->once()) |
|
1372 | - ->method('cacheUserDisplayName'); |
|
1373 | - |
|
1374 | - $this->assertEquals($newDisplayName, $this->backend->setDisplayName('uid', $newDisplayName)); |
|
1375 | - } |
|
1376 | - |
|
1377 | - |
|
1378 | - public function testSetDisplayNameErrorWithPlugin(): void { |
|
1379 | - $this->expectException(HintException::class); |
|
1380 | - |
|
1381 | - $newDisplayName = 'J. Baker'; |
|
1382 | - $this->pluginManager->expects($this->once()) |
|
1383 | - ->method('implementsActions') |
|
1384 | - ->with(Backend::SET_DISPLAYNAME) |
|
1385 | - ->willReturn(true); |
|
1386 | - $this->pluginManager->expects($this->once()) |
|
1387 | - ->method('setDisplayName') |
|
1388 | - ->with('uid', $newDisplayName) |
|
1389 | - ->willThrowException(new HintException('something happned')); |
|
1390 | - $this->access->expects($this->never()) |
|
1391 | - ->method('cacheUserDisplayName'); |
|
1392 | - |
|
1393 | - $this->backend->setDisplayName('uid', $newDisplayName); |
|
1394 | - } |
|
1395 | - |
|
1396 | - public function testSetDisplayNameFailing(): void { |
|
1397 | - $this->pluginManager->expects($this->once()) |
|
1398 | - ->method('implementsActions') |
|
1399 | - ->with(Backend::SET_DISPLAYNAME) |
|
1400 | - ->willReturn(false); |
|
1401 | - $this->access->expects($this->never()) |
|
1402 | - ->method('cacheUserDisplayName'); |
|
1403 | - |
|
1404 | - $this->assertFalse($this->backend->setDisplayName('uid', 'displayName')); |
|
1405 | - } |
|
1406 | - |
|
1407 | - public function testCreateUserWithPlugin(): void { |
|
1408 | - $uid = 'alien6372'; |
|
1409 | - $uuid = '123-2345-36756-123-2345234-4431'; |
|
1410 | - $pwd = 'passwørd'; |
|
1411 | - |
|
1412 | - $this->pluginManager->expects($this->once()) |
|
1413 | - ->method('implementsActions') |
|
1414 | - ->with(Backend::CREATE_USER) |
|
1415 | - ->willReturn(true); |
|
1416 | - $this->pluginManager->expects($this->once()) |
|
1417 | - ->method('createUser') |
|
1418 | - ->with($uid, $pwd) |
|
1419 | - ->willReturn('result'); |
|
1420 | - |
|
1421 | - $this->access->expects($this->atLeastOnce()) |
|
1422 | - ->method('getUUID') |
|
1423 | - ->willReturn($uuid); |
|
1424 | - $this->access->expects($this->once()) |
|
1425 | - ->method('mapAndAnnounceIfApplicable') |
|
1426 | - ->with($this->isInstanceOf(AbstractMapping::class), $this->anything(), $uid, $uuid, true); |
|
1427 | - $this->access->expects($this->any()) |
|
1428 | - ->method('getUserMapper') |
|
1429 | - ->willReturn($this->createMock(UserMapping::class)); |
|
1430 | - |
|
1431 | - $this->assertEquals($this->backend->createUser($uid, $pwd), true); |
|
1432 | - } |
|
1433 | - |
|
1434 | - public function testCreateUserFailing(): void { |
|
1435 | - $this->pluginManager->expects($this->once()) |
|
1436 | - ->method('implementsActions') |
|
1437 | - ->with(Backend::CREATE_USER) |
|
1438 | - ->willReturn(false); |
|
1439 | - |
|
1440 | - $this->assertFalse($this->backend->createUser('uid', 'password')); |
|
1441 | - } |
|
1442 | - |
|
1443 | - public static function actionProvider(): array { |
|
1444 | - return [ |
|
1445 | - [ 'ldapUserAvatarRule', 'default', Backend::PROVIDE_AVATAR, true], |
|
1446 | - [ 'ldapUserAvatarRule', 'data:selfiePhoto', Backend::PROVIDE_AVATAR, true], |
|
1447 | - [ 'ldapUserAvatarRule', 'none', Backend::PROVIDE_AVATAR, false], |
|
1448 | - [ 'turnOnPasswordChange', 0, Backend::SET_PASSWORD, false], |
|
1449 | - [ 'turnOnPasswordChange', 1, Backend::SET_PASSWORD, true], |
|
1450 | - ]; |
|
1451 | - } |
|
1452 | - |
|
1453 | - #[\PHPUnit\Framework\Attributes\DataProvider('actionProvider')] |
|
1454 | - public function testImplementsAction(string $configurable, string|int $value, int $actionCode, bool $expected): void { |
|
1455 | - $this->pluginManager->expects($this->once()) |
|
1456 | - ->method('getImplementedActions') |
|
1457 | - ->willReturn(0); |
|
1458 | - |
|
1459 | - $this->connection->expects($this->any()) |
|
1460 | - ->method('__get') |
|
1461 | - ->willReturnMap([ |
|
1462 | - [$configurable, $value], |
|
1463 | - ]); |
|
1464 | - |
|
1465 | - $this->assertSame($expected, $this->backend->implementsActions($actionCode)); |
|
1466 | - } |
|
43 | + protected Access&MockObject $access; |
|
44 | + protected OfflineUser&MockObject $offlineUser; |
|
45 | + protected INotificationManager&MockObject $notificationManager; |
|
46 | + protected UserPluginManager&MockObject $pluginManager; |
|
47 | + protected Connection&MockObject $connection; |
|
48 | + protected Manager&MockObject $userManager; |
|
49 | + protected LoggerInterface&MockObject $logger; |
|
50 | + protected DeletedUsersIndex&MockObject $deletedUsersIndex; |
|
51 | + protected User_LDAP $backend; |
|
52 | + |
|
53 | + protected function setUp(): void { |
|
54 | + parent::setUp(); |
|
55 | + |
|
56 | + Server::get(IUserManager::class)->clearBackends(); |
|
57 | + Server::get(IGroupManager::class)->clearBackends(); |
|
58 | + |
|
59 | + $this->connection = $this->getMockBuilder(Connection::class) |
|
60 | + ->setConstructorArgs([$this->createMock(ILDAPWrapper::class)]) |
|
61 | + ->getMock(); |
|
62 | + $this->userManager = $this->createMock(Manager::class); |
|
63 | + |
|
64 | + $this->access = $this->createMock(Access::class); |
|
65 | + $this->access->connection = $this->connection; |
|
66 | + $this->access->userManager = $this->userManager; |
|
67 | + |
|
68 | + $this->notificationManager = $this->createMock(INotificationManager::class); |
|
69 | + $this->pluginManager = $this->createMock(UserPluginManager::class); |
|
70 | + |
|
71 | + $this->logger = $this->createMock(LoggerInterface::class); |
|
72 | + |
|
73 | + $this->deletedUsersIndex = $this->createMock(DeletedUsersIndex::class); |
|
74 | + |
|
75 | + $this->backend = new User_LDAP( |
|
76 | + $this->access, |
|
77 | + $this->notificationManager, |
|
78 | + $this->pluginManager, |
|
79 | + $this->logger, |
|
80 | + $this->deletedUsersIndex, |
|
81 | + ); |
|
82 | + } |
|
83 | + |
|
84 | + private function prepareMockForUserExists(): void { |
|
85 | + $this->access->expects($this->any()) |
|
86 | + ->method('username2dn') |
|
87 | + ->willReturnCallback(function ($uid) { |
|
88 | + switch ($uid) { |
|
89 | + case 'gunslinger': |
|
90 | + return 'dnOfRoland,dc=test'; |
|
91 | + break; |
|
92 | + case 'formerUser': |
|
93 | + return 'dnOfFormerUser,dc=test'; |
|
94 | + break; |
|
95 | + case 'newyorker': |
|
96 | + return 'dnOfNewYorker,dc=test'; |
|
97 | + break; |
|
98 | + case 'ladyofshadows': |
|
99 | + return 'dnOfLadyOfShadows,dc=test'; |
|
100 | + break; |
|
101 | + default: |
|
102 | + return false; |
|
103 | + } |
|
104 | + }); |
|
105 | + |
|
106 | + $this->access->method('fetchUsersByLoginName') |
|
107 | + ->willReturn([]); |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * Prepares the Access mock for checkPassword tests |
|
112 | + */ |
|
113 | + private function prepareAccessForCheckPassword(bool $noDisplayName = false): void { |
|
114 | + $this->connection->expects($this->any()) |
|
115 | + ->method('__get') |
|
116 | + ->willReturnCallback(function ($name) { |
|
117 | + if ($name === 'ldapLoginFilter') { |
|
118 | + return '%uid'; |
|
119 | + } |
|
120 | + return null; |
|
121 | + }); |
|
122 | + |
|
123 | + $this->access->expects($this->any()) |
|
124 | + ->method('fetchListOfUsers') |
|
125 | + ->willReturnCallback(function ($filter) { |
|
126 | + if ($filter === 'roland') { |
|
127 | + return [['dn' => ['dnOfRoland,dc=test']]]; |
|
128 | + } |
|
129 | + return []; |
|
130 | + }); |
|
131 | + $this->access->expects($this->any()) |
|
132 | + ->method('fetchUsersByLoginName') |
|
133 | + ->willReturnCallback(function ($uid) { |
|
134 | + if ($uid === 'roland') { |
|
135 | + return [['dn' => ['dnOfRoland,dc=test']]]; |
|
136 | + } |
|
137 | + return []; |
|
138 | + }); |
|
139 | + |
|
140 | + $retVal = 'gunslinger'; |
|
141 | + if ($noDisplayName === true) { |
|
142 | + $retVal = false; |
|
143 | + } |
|
144 | + $this->access->expects($this->any()) |
|
145 | + ->method('dn2username') |
|
146 | + ->with($this->equalTo('dnOfRoland,dc=test')) |
|
147 | + ->willReturn($retVal); |
|
148 | + $this->access->expects($this->any()) |
|
149 | + ->method('username2dn') |
|
150 | + ->with($this->equalTo('gunslinger')) |
|
151 | + ->willReturn('dnOfRoland,dc=test'); |
|
152 | + $this->access->expects($this->any()) |
|
153 | + ->method('stringResemblesDN') |
|
154 | + ->with($this->equalTo('dnOfRoland,dc=test')) |
|
155 | + ->willReturn(true); |
|
156 | + $this->access->expects($this->any()) |
|
157 | + ->method('areCredentialsValid') |
|
158 | + ->willReturnCallback(function ($dn, $pwd) { |
|
159 | + if ($pwd === 'dt19') { |
|
160 | + return true; |
|
161 | + } |
|
162 | + return false; |
|
163 | + }); |
|
164 | + |
|
165 | + $this->userManager->expects($this->any()) |
|
166 | + ->method('getAttributes') |
|
167 | + ->willReturn(['dn', 'uid', 'mail', 'displayname']); |
|
168 | + } |
|
169 | + |
|
170 | + public function testCheckPasswordUidReturn(): void { |
|
171 | + $user = $this->createMock(User::class); |
|
172 | + $user->expects($this->any()) |
|
173 | + ->method('getUsername') |
|
174 | + ->willReturn('gunslinger'); |
|
175 | + |
|
176 | + $this->prepareAccessForCheckPassword(); |
|
177 | + $this->userManager->expects($this->any()) |
|
178 | + ->method('get') |
|
179 | + ->willReturn($user); |
|
180 | + |
|
181 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
182 | + |
|
183 | + Server::get(IUserManager::class)->registerBackend($backend); |
|
184 | + |
|
185 | + $result = $backend->checkPassword('roland', 'dt19'); |
|
186 | + $this->assertEquals('gunslinger', $result); |
|
187 | + } |
|
188 | + |
|
189 | + public function testCheckPasswordWrongPassword(): void { |
|
190 | + $this->prepareAccessForCheckPassword(); |
|
191 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
192 | + Server::get(IUserManager::class)->registerBackend($backend); |
|
193 | + |
|
194 | + $result = $backend->checkPassword('roland', 'wrong'); |
|
195 | + $this->assertFalse($result); |
|
196 | + } |
|
197 | + |
|
198 | + public function testCheckPasswordWrongUser(): void { |
|
199 | + $this->prepareAccessForCheckPassword(); |
|
200 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
201 | + Server::get(IUserManager::class)->registerBackend($backend); |
|
202 | + |
|
203 | + $result = $backend->checkPassword('mallory', 'evil'); |
|
204 | + $this->assertFalse($result); |
|
205 | + } |
|
206 | + |
|
207 | + public function testCheckPasswordNoDisplayName(): void { |
|
208 | + $this->prepareAccessForCheckPassword(true); |
|
209 | + |
|
210 | + $this->prepareAccessForCheckPassword(); |
|
211 | + $this->userManager->expects($this->atLeastOnce()) |
|
212 | + ->method('get') |
|
213 | + ->willReturn(null); |
|
214 | + |
|
215 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
216 | + Server::get(IUserManager::class)->registerBackend($backend); |
|
217 | + |
|
218 | + $result = $backend->checkPassword('roland', 'dt19'); |
|
219 | + $this->assertFalse($result); |
|
220 | + } |
|
221 | + |
|
222 | + public function testCheckPasswordPublicAPI(): void { |
|
223 | + $user = $this->createMock(User::class); |
|
224 | + $user->expects($this->any()) |
|
225 | + ->method('getUsername') |
|
226 | + ->willReturn('gunslinger'); |
|
227 | + |
|
228 | + $this->prepareAccessForCheckPassword(); |
|
229 | + $this->userManager->expects($this->any()) |
|
230 | + ->method('get') |
|
231 | + ->willReturn($user); |
|
232 | + |
|
233 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
234 | + Server::get(IUserManager::class)->registerBackend($backend); |
|
235 | + |
|
236 | + $user = Server::get(IUserManager::class)->checkPassword('roland', 'dt19'); |
|
237 | + $result = false; |
|
238 | + if ($user !== false) { |
|
239 | + $result = $user->getUID(); |
|
240 | + } |
|
241 | + $this->assertEquals('gunslinger', $result); |
|
242 | + } |
|
243 | + |
|
244 | + public function testCheckPasswordPublicAPIWrongPassword(): void { |
|
245 | + $this->prepareAccessForCheckPassword(); |
|
246 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
247 | + Server::get(IUserManager::class)->registerBackend($backend); |
|
248 | + |
|
249 | + $user = Server::get(IUserManager::class)->checkPassword('roland', 'wrong'); |
|
250 | + $result = false; |
|
251 | + if ($user !== false) { |
|
252 | + $result = $user->getUID(); |
|
253 | + } |
|
254 | + $this->assertFalse($result); |
|
255 | + } |
|
256 | + |
|
257 | + public function testCheckPasswordPublicAPIWrongUser(): void { |
|
258 | + $this->prepareAccessForCheckPassword(); |
|
259 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
260 | + Server::get(IUserManager::class)->registerBackend($backend); |
|
261 | + |
|
262 | + $user = Server::get(IUserManager::class)->checkPassword('mallory', 'evil'); |
|
263 | + $result = false; |
|
264 | + if ($user !== false) { |
|
265 | + $result = $user->getUID(); |
|
266 | + } |
|
267 | + $this->assertFalse($result); |
|
268 | + } |
|
269 | + |
|
270 | + public function testDeleteUserCancel(): void { |
|
271 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
272 | + $result = $backend->deleteUser('notme'); |
|
273 | + $this->assertFalse($result); |
|
274 | + } |
|
275 | + |
|
276 | + public function testDeleteUserSuccess(): void { |
|
277 | + $uid = 'jeremy'; |
|
278 | + $home = '/var/vhome/jdings/'; |
|
279 | + |
|
280 | + $mapping = $this->createMock(UserMapping::class); |
|
281 | + $mapping->expects($this->once()) |
|
282 | + ->method('unmap') |
|
283 | + ->willReturn(true); |
|
284 | + $this->access->expects($this->once()) |
|
285 | + ->method('getUserMapper') |
|
286 | + ->willReturn($mapping); |
|
287 | + $this->connection->expects($this->any()) |
|
288 | + ->method('getConnectionResource') |
|
289 | + ->willReturn(ldap_connect('ldap://example.com')); |
|
290 | + |
|
291 | + $this->deletedUsersIndex->expects($this->once()) |
|
292 | + ->method('isUserMarked') |
|
293 | + ->with($uid) |
|
294 | + ->willReturn(true); |
|
295 | + |
|
296 | + $offlineUser = $this->createMock(OfflineUser::class); |
|
297 | + $offlineUser->expects($this->once()) |
|
298 | + ->method('getHomePath') |
|
299 | + ->willReturn($home); |
|
300 | + $this->userManager->expects($this->atLeastOnce()) |
|
301 | + ->method('get') |
|
302 | + ->willReturn($offlineUser); |
|
303 | + $this->userManager->expects($this->once()) |
|
304 | + ->method('exists') |
|
305 | + ->with($uid) |
|
306 | + ->willReturn(true); |
|
307 | + |
|
308 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
309 | + |
|
310 | + $result = $backend->deleteUser($uid); |
|
311 | + $this->assertTrue($result); |
|
312 | + /** @noinspection PhpUnhandledExceptionInspection */ |
|
313 | + $this->assertSame($backend->getHome($uid), $home); |
|
314 | + } |
|
315 | + |
|
316 | + public function testDeleteUserWithPlugin(): void { |
|
317 | + $this->pluginManager->expects($this->once()) |
|
318 | + ->method('canDeleteUser') |
|
319 | + ->willReturn(true); |
|
320 | + $this->pluginManager->expects($this->once()) |
|
321 | + ->method('deleteUser') |
|
322 | + ->with('uid') |
|
323 | + ->willReturn(true); |
|
324 | + |
|
325 | + $this->deletedUsersIndex->expects($this->once()) |
|
326 | + ->method('isUserMarked') |
|
327 | + ->with('uid') |
|
328 | + ->willReturn(true); |
|
329 | + |
|
330 | + $mapper = $this->createMock(UserMapping::class); |
|
331 | + $mapper->expects($this->once()) |
|
332 | + ->method('unmap') |
|
333 | + ->with('uid'); |
|
334 | + |
|
335 | + $this->access->expects($this->atLeastOnce()) |
|
336 | + ->method('getUserMapper') |
|
337 | + ->willReturn($mapper); |
|
338 | + |
|
339 | + $this->userManager->expects($this->once()) |
|
340 | + ->method('invalidate') |
|
341 | + ->with('uid'); |
|
342 | + |
|
343 | + $this->assertTrue($this->backend->deleteUser('uid')); |
|
344 | + } |
|
345 | + |
|
346 | + /** |
|
347 | + * Prepares the Access mock for getUsers tests |
|
348 | + */ |
|
349 | + private function prepareAccessForGetUsers() { |
|
350 | + $this->access->expects($this->once()) |
|
351 | + ->method('escapeFilterPart') |
|
352 | + ->willReturnCallback(function ($search) { |
|
353 | + return $search; |
|
354 | + }); |
|
355 | + $this->access->expects($this->any()) |
|
356 | + ->method('getFilterPartForUserSearch') |
|
357 | + ->willReturnCallback(function ($search) { |
|
358 | + return $search; |
|
359 | + }); |
|
360 | + $this->access->expects($this->any()) |
|
361 | + ->method('combineFilterWithAnd') |
|
362 | + ->willReturnCallback(function ($param) { |
|
363 | + return $param[2]; |
|
364 | + }); |
|
365 | + $this->access->expects($this->any()) |
|
366 | + ->method('fetchListOfUsers') |
|
367 | + ->willReturnCallback(function ($search, $a, $l, $o) { |
|
368 | + $users = ['gunslinger', 'newyorker', 'ladyofshadows']; |
|
369 | + if (empty($search)) { |
|
370 | + $result = $users; |
|
371 | + } else { |
|
372 | + $result = []; |
|
373 | + foreach ($users as $user) { |
|
374 | + if (stripos($user, $search) !== false) { |
|
375 | + $result[] = $user; |
|
376 | + } |
|
377 | + } |
|
378 | + } |
|
379 | + if (!is_null($l) || !is_null($o)) { |
|
380 | + $result = array_slice($result, $o, $l); |
|
381 | + } |
|
382 | + return $result; |
|
383 | + }); |
|
384 | + $this->access->expects($this->any()) |
|
385 | + ->method('nextcloudUserNames') |
|
386 | + ->willReturnArgument(0); |
|
387 | + $this->access->method('fetchUsersByLoginName') |
|
388 | + ->willReturn([]); |
|
389 | + |
|
390 | + $this->access->userManager->expects($this->any()) |
|
391 | + ->method('getAttributes') |
|
392 | + ->willReturn(['dn', 'uid', 'mail', 'displayname']); |
|
393 | + } |
|
394 | + |
|
395 | + public function testGetUsersNoParam(): void { |
|
396 | + $this->prepareAccessForGetUsers(); |
|
397 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
398 | + |
|
399 | + $result = $backend->getUsers(); |
|
400 | + $this->assertCount(3, $result); |
|
401 | + } |
|
402 | + |
|
403 | + public function testGetUsersLimitOffset(): void { |
|
404 | + $this->prepareAccessForGetUsers(); |
|
405 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
406 | + |
|
407 | + $result = $backend->getUsers('', 1, 2); |
|
408 | + $this->assertCount(1, $result); |
|
409 | + } |
|
410 | + |
|
411 | + public function testGetUsersLimitOffset2(): void { |
|
412 | + $this->prepareAccessForGetUsers(); |
|
413 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
414 | + |
|
415 | + $result = $backend->getUsers('', 2, 1); |
|
416 | + $this->assertCount(2, $result); |
|
417 | + } |
|
418 | + |
|
419 | + public function testGetUsersSearchWithResult(): void { |
|
420 | + $this->prepareAccessForGetUsers(); |
|
421 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
422 | + |
|
423 | + $result = $backend->getUsers('yo'); |
|
424 | + $this->assertCount(2, $result); |
|
425 | + } |
|
426 | + |
|
427 | + public function testGetUsersSearchEmptyResult(): void { |
|
428 | + $this->prepareAccessForGetUsers(); |
|
429 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
430 | + |
|
431 | + $result = $backend->getUsers('nix'); |
|
432 | + $this->assertCount(0, $result); |
|
433 | + } |
|
434 | + |
|
435 | + private function getUsers($search = '', $limit = null, $offset = null) { |
|
436 | + $users = Server::get(IUserManager::class)->search($search, $limit, $offset); |
|
437 | + $uids = array_map(function (IUser $user) { |
|
438 | + return $user->getUID(); |
|
439 | + }, $users); |
|
440 | + return $uids; |
|
441 | + } |
|
442 | + |
|
443 | + public function testGetUsersViaAPINoParam(): void { |
|
444 | + $this->prepareAccessForGetUsers(); |
|
445 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
446 | + Server::get(IUserManager::class)->registerBackend($backend); |
|
447 | + |
|
448 | + $result = $this->getUsers(); |
|
449 | + $this->assertCount(3, $result); |
|
450 | + } |
|
451 | + |
|
452 | + public function testGetUsersViaAPILimitOffset(): void { |
|
453 | + $this->prepareAccessForGetUsers(); |
|
454 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
455 | + Server::get(IUserManager::class)->registerBackend($backend); |
|
456 | + |
|
457 | + $result = $this->getUsers('', 1, 2); |
|
458 | + $this->assertCount(1, $result); |
|
459 | + } |
|
460 | + |
|
461 | + public function testGetUsersViaAPILimitOffset2(): void { |
|
462 | + $this->prepareAccessForGetUsers(); |
|
463 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
464 | + Server::get(IUserManager::class)->registerBackend($backend); |
|
465 | + |
|
466 | + $result = $this->getUsers('', 2, 1); |
|
467 | + $this->assertCount(2, $result); |
|
468 | + } |
|
469 | + |
|
470 | + public function testGetUsersViaAPISearchWithResult(): void { |
|
471 | + $this->prepareAccessForGetUsers(); |
|
472 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
473 | + Server::get(IUserManager::class)->registerBackend($backend); |
|
474 | + |
|
475 | + $result = $this->getUsers('yo'); |
|
476 | + $this->assertCount(2, $result); |
|
477 | + } |
|
478 | + |
|
479 | + public function testGetUsersViaAPISearchEmptyResult(): void { |
|
480 | + $this->prepareAccessForGetUsers(); |
|
481 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
482 | + Server::get(IUserManager::class)->registerBackend($backend); |
|
483 | + |
|
484 | + $result = $this->getUsers('nix'); |
|
485 | + $this->assertCount(0, $result); |
|
486 | + } |
|
487 | + |
|
488 | + public function testUserExists(): void { |
|
489 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
490 | + $this->prepareMockForUserExists(); |
|
491 | + |
|
492 | + $this->userManager->expects($this->never()) |
|
493 | + ->method('get'); |
|
494 | + $this->userManager->expects($this->once()) |
|
495 | + ->method('exists') |
|
496 | + ->with('gunslinger') |
|
497 | + ->willReturn(true); |
|
498 | + $this->access->expects($this->any()) |
|
499 | + ->method('getUserMapper') |
|
500 | + ->willReturn($this->createMock(UserMapping::class)); |
|
501 | + |
|
502 | + //test for existing user |
|
503 | + /** @noinspection PhpUnhandledExceptionInspection */ |
|
504 | + $result = $backend->userExists('gunslinger'); |
|
505 | + $this->assertTrue($result); |
|
506 | + } |
|
507 | + |
|
508 | + public function testUserExistsForDeleted(): void { |
|
509 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
510 | + $this->prepareMockForUserExists(); |
|
511 | + |
|
512 | + $mapper = $this->createMock(UserMapping::class); |
|
513 | + $mapper->expects($this->any()) |
|
514 | + ->method('getUUIDByDN') |
|
515 | + ->with('dnOfFormerUser,dc=test') |
|
516 | + ->willReturn('45673458748'); |
|
517 | + |
|
518 | + $this->access->expects($this->any()) |
|
519 | + ->method('getUserMapper') |
|
520 | + ->willReturn($mapper); |
|
521 | + |
|
522 | + $this->userManager->expects($this->never()) |
|
523 | + ->method('get'); |
|
524 | + $this->userManager->expects($this->once()) |
|
525 | + ->method('exists') |
|
526 | + ->with('formerUser') |
|
527 | + ->willReturn(true); |
|
528 | + |
|
529 | + //test for deleted user – always returns true as long as we have the user in DB |
|
530 | + $this->assertTrue($backend->userExists('formerUser')); |
|
531 | + } |
|
532 | + |
|
533 | + public function testUserExistsForNeverExisting(): void { |
|
534 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
535 | + $this->prepareMockForUserExists(); |
|
536 | + |
|
537 | + $this->access->expects($this->any()) |
|
538 | + ->method('readAttribute') |
|
539 | + ->willReturnCallback(function ($dn) { |
|
540 | + if ($dn === 'dnOfRoland,dc=test') { |
|
541 | + return []; |
|
542 | + } |
|
543 | + return false; |
|
544 | + }); |
|
545 | + |
|
546 | + //test for never-existing user |
|
547 | + /** @noinspection PhpUnhandledExceptionInspection */ |
|
548 | + $result = $backend->userExists('mallory'); |
|
549 | + $this->assertFalse($result); |
|
550 | + } |
|
551 | + |
|
552 | + public function testUserExistsPublicAPI(): void { |
|
553 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
554 | + $this->prepareMockForUserExists(); |
|
555 | + Server::get(IUserManager::class)->registerBackend($backend); |
|
556 | + |
|
557 | + $user = $this->createMock(User::class); |
|
558 | + $user->expects($this->any()) |
|
559 | + ->method('getDN') |
|
560 | + ->willReturn('dnOfRoland,dc=test'); |
|
561 | + |
|
562 | + $this->access->expects($this->any()) |
|
563 | + ->method('readAttribute') |
|
564 | + ->willReturnCallback(function ($dn) { |
|
565 | + if ($dn === 'dnOfRoland,dc=test') { |
|
566 | + return []; |
|
567 | + } |
|
568 | + return false; |
|
569 | + }); |
|
570 | + $this->userManager->expects($this->never()) |
|
571 | + ->method('get'); |
|
572 | + $this->userManager->expects($this->once()) |
|
573 | + ->method('exists') |
|
574 | + ->with('gunslinger') |
|
575 | + ->willReturn(true); |
|
576 | + $this->access->expects($this->any()) |
|
577 | + ->method('getUserMapper') |
|
578 | + ->willReturn($this->createMock(UserMapping::class)); |
|
579 | + |
|
580 | + //test for existing user |
|
581 | + $result = Server::get(IUserManager::class)->userExists('gunslinger'); |
|
582 | + $this->assertTrue($result); |
|
583 | + } |
|
584 | + |
|
585 | + public function testDeleteUserExisting(): void { |
|
586 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
587 | + |
|
588 | + //we do not support deleting existing users at all |
|
589 | + $result = $backend->deleteUser('gunslinger'); |
|
590 | + $this->assertFalse($result); |
|
591 | + } |
|
592 | + |
|
593 | + public function testGetHomeAbsolutePath(): void { |
|
594 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
595 | + $this->prepareMockForUserExists(); |
|
596 | + |
|
597 | + $this->connection->expects($this->any()) |
|
598 | + ->method('__get') |
|
599 | + ->willReturnCallback(function ($name) { |
|
600 | + if ($name === 'homeFolderNamingRule') { |
|
601 | + return 'attr:testAttribute'; |
|
602 | + } |
|
603 | + return null; |
|
604 | + }); |
|
605 | + |
|
606 | + $this->access->expects($this->any()) |
|
607 | + ->method('readAttribute') |
|
608 | + ->willReturnCallback(function ($dn, $attr) { |
|
609 | + switch ($dn) { |
|
610 | + case 'dnOfRoland,dc=test': |
|
611 | + if ($attr === 'testAttribute') { |
|
612 | + return ['/tmp/rolandshome/']; |
|
613 | + } |
|
614 | + return []; |
|
615 | + break; |
|
616 | + default: |
|
617 | + return false; |
|
618 | + } |
|
619 | + }); |
|
620 | + |
|
621 | + $user = $this->createMock(User::class); |
|
622 | + $user->expects($this->any()) |
|
623 | + ->method('getUsername') |
|
624 | + ->willReturn('gunslinger'); |
|
625 | + $user->expects($this->any()) |
|
626 | + ->method('getDN') |
|
627 | + ->willReturn('dnOfRoland,dc=test'); |
|
628 | + $user->expects($this->any()) |
|
629 | + ->method('getHomePath') |
|
630 | + ->willReturn('/tmp/rolandshome/'); |
|
631 | + |
|
632 | + $this->userManager->expects($this->atLeastOnce()) |
|
633 | + ->method('get') |
|
634 | + ->with('gunslinger') |
|
635 | + ->willReturn($user); |
|
636 | + $this->userManager->expects($this->once()) |
|
637 | + ->method('exists') |
|
638 | + ->with('gunslinger') |
|
639 | + ->willReturn(true); |
|
640 | + |
|
641 | + //absolute path |
|
642 | + /** @noinspection PhpUnhandledExceptionInspection */ |
|
643 | + $result = $backend->getHome('gunslinger'); |
|
644 | + $this->assertEquals('/tmp/rolandshome/', $result); |
|
645 | + } |
|
646 | + |
|
647 | + public function testGetHomeRelative(): void { |
|
648 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
649 | + $this->prepareMockForUserExists(); |
|
650 | + |
|
651 | + $dataDir = Server::get(IConfig::class)->getSystemValue( |
|
652 | + 'datadirectory', \OC::$SERVERROOT . '/data'); |
|
653 | + |
|
654 | + $this->connection->expects($this->any()) |
|
655 | + ->method('__get') |
|
656 | + ->willReturnCallback(function ($name) { |
|
657 | + if ($name === 'homeFolderNamingRule') { |
|
658 | + return 'attr:testAttribute'; |
|
659 | + } |
|
660 | + return null; |
|
661 | + }); |
|
662 | + |
|
663 | + $this->access->expects($this->any()) |
|
664 | + ->method('readAttribute') |
|
665 | + ->willReturnCallback(function ($dn, $attr) { |
|
666 | + switch ($dn) { |
|
667 | + case 'dnOfLadyOfShadows,dc=test': |
|
668 | + if ($attr === 'testAttribute') { |
|
669 | + return ['susannah/']; |
|
670 | + } |
|
671 | + return []; |
|
672 | + break; |
|
673 | + default: |
|
674 | + return false; |
|
675 | + } |
|
676 | + }); |
|
677 | + |
|
678 | + $user = $this->createMock(User::class); |
|
679 | + $user->expects($this->any()) |
|
680 | + ->method('getUsername') |
|
681 | + ->willReturn('ladyofshadows'); |
|
682 | + $user->expects($this->any()) |
|
683 | + ->method('getDN') |
|
684 | + ->willReturn('dnOfLadyOfShadows,dc=test'); |
|
685 | + $user->expects($this->any()) |
|
686 | + ->method('getHomePath') |
|
687 | + ->willReturn($dataDir . '/susannah/'); |
|
688 | + |
|
689 | + $this->userManager->expects($this->atLeastOnce()) |
|
690 | + ->method('get') |
|
691 | + ->willReturn($user); |
|
692 | + $this->userManager->expects($this->once()) |
|
693 | + ->method('exists') |
|
694 | + ->with('ladyofshadows') |
|
695 | + ->willReturn(true); |
|
696 | + |
|
697 | + /** @noinspection PhpUnhandledExceptionInspection */ |
|
698 | + $result = $backend->getHome('ladyofshadows'); |
|
699 | + $this->assertEquals($dataDir . '/susannah/', $result); |
|
700 | + } |
|
701 | + |
|
702 | + |
|
703 | + public function testGetHomeNoPath(): void { |
|
704 | + $this->expectException(\Exception::class); |
|
705 | + |
|
706 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
707 | + $this->prepareMockForUserExists(); |
|
708 | + |
|
709 | + $this->connection->expects($this->any()) |
|
710 | + ->method('__get') |
|
711 | + ->willReturnCallback(function ($name) { |
|
712 | + if ($name === 'homeFolderNamingRule') { |
|
713 | + return 'attr:testAttribute'; |
|
714 | + } |
|
715 | + return null; |
|
716 | + }); |
|
717 | + $this->access->expects($this->any()) |
|
718 | + ->method('readAttribute') |
|
719 | + ->willReturnCallback(function ($dn, $attr) { |
|
720 | + switch ($dn) { |
|
721 | + default: |
|
722 | + return false; |
|
723 | + } |
|
724 | + }); |
|
725 | + |
|
726 | + $user = $this->createMock(User::class); |
|
727 | + $user->expects($this->any()) |
|
728 | + ->method('getUsername') |
|
729 | + ->willReturn('newyorker'); |
|
730 | + $user->expects($this->any()) |
|
731 | + ->method('getHomePath') |
|
732 | + ->willThrowException(new \Exception()); |
|
733 | + |
|
734 | + $this->userManager->expects($this->atLeastOnce()) |
|
735 | + ->method('get') |
|
736 | + ->with('newyorker') |
|
737 | + ->willReturn($user); |
|
738 | + $this->userManager->expects($this->once()) |
|
739 | + ->method('exists') |
|
740 | + ->with('newyorker') |
|
741 | + ->willReturn(true); |
|
742 | + |
|
743 | + //no path at all – triggers OC default behaviour |
|
744 | + $result = $backend->getHome('newyorker'); |
|
745 | + $this->assertFalse($result); |
|
746 | + } |
|
747 | + |
|
748 | + public function testGetHomeDeletedUser(): void { |
|
749 | + $uid = 'newyorker'; |
|
750 | + |
|
751 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
752 | + $this->prepareMockForUserExists(); |
|
753 | + |
|
754 | + $this->connection->expects($this->any()) |
|
755 | + ->method('__get') |
|
756 | + ->willReturnCallback(function ($name) { |
|
757 | + if ($name === 'homeFolderNamingRule') { |
|
758 | + return 'attr:testAttribute'; |
|
759 | + } |
|
760 | + return null; |
|
761 | + }); |
|
762 | + |
|
763 | + $this->access->expects($this->any()) |
|
764 | + ->method('readAttribute') |
|
765 | + ->willReturn([]); |
|
766 | + |
|
767 | + $userMapper = $this->createMock(UserMapping::class); |
|
768 | + |
|
769 | + $this->access->expects($this->any()) |
|
770 | + ->method('getUserMapper') |
|
771 | + ->willReturn($userMapper); |
|
772 | + |
|
773 | + $offlineUser = $this->createMock(OfflineUser::class); |
|
774 | + $offlineUser->expects($this->atLeastOnce()) |
|
775 | + ->method('getHomePath') |
|
776 | + ->willReturn(''); |
|
777 | + |
|
778 | + $this->userManager->expects($this->atLeastOnce()) |
|
779 | + ->method('get') |
|
780 | + ->with($uid) |
|
781 | + ->willReturn($offlineUser); |
|
782 | + $this->userManager->expects($this->once()) |
|
783 | + ->method('exists') |
|
784 | + ->with($uid) |
|
785 | + ->willReturn(true); |
|
786 | + |
|
787 | + $result = $backend->getHome($uid); |
|
788 | + $this->assertFalse($result); |
|
789 | + } |
|
790 | + |
|
791 | + public function testGetHomeWithPlugin(): void { |
|
792 | + $this->pluginManager->expects($this->once()) |
|
793 | + ->method('implementsActions') |
|
794 | + ->with(Backend::GET_HOME) |
|
795 | + ->willReturn(true); |
|
796 | + $this->pluginManager->expects($this->once()) |
|
797 | + ->method('getHome') |
|
798 | + ->with('uid') |
|
799 | + ->willReturn('result'); |
|
800 | + |
|
801 | + $this->connection->expects($this->any()) |
|
802 | + ->method('getFromCache') |
|
803 | + ->willReturnCallback(function ($uid) { |
|
804 | + return true; |
|
805 | + }); |
|
806 | + |
|
807 | + /** @noinspection PhpUnhandledExceptionInspection */ |
|
808 | + $this->assertEquals($this->backend->getHome('uid'), 'result'); |
|
809 | + } |
|
810 | + |
|
811 | + private function prepareAccessForGetDisplayName() { |
|
812 | + $this->connection->expects($this->any()) |
|
813 | + ->method('__get') |
|
814 | + ->willReturnCallback(function ($name) { |
|
815 | + if ($name === 'ldapUserDisplayName') { |
|
816 | + return 'displayname'; |
|
817 | + } elseif ($name === 'ldapUserDisplayName2') { |
|
818 | + return 'displayname2'; |
|
819 | + } |
|
820 | + return null; |
|
821 | + }); |
|
822 | + |
|
823 | + $this->access->expects($this->any()) |
|
824 | + ->method('readAttribute') |
|
825 | + ->willReturnCallback(function ($dn, $attr) { |
|
826 | + switch ($dn) { |
|
827 | + case 'dnOfRoland,dc=test': |
|
828 | + if ($attr === 'displayname') { |
|
829 | + return ['Roland Deschain']; |
|
830 | + } |
|
831 | + return []; |
|
832 | + break; |
|
833 | + |
|
834 | + default: |
|
835 | + return false; |
|
836 | + } |
|
837 | + }); |
|
838 | + $this->access->method('fetchUsersByLoginName') |
|
839 | + ->willReturn([]); |
|
840 | + } |
|
841 | + |
|
842 | + public function testGetDisplayName(): void { |
|
843 | + $this->prepareAccessForGetDisplayName(); |
|
844 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
845 | + $this->prepareMockForUserExists(); |
|
846 | + |
|
847 | + $this->connection->expects($this->any()) |
|
848 | + ->method('getConnectionResource') |
|
849 | + ->willReturnCallback(function () { |
|
850 | + return true; |
|
851 | + }); |
|
852 | + |
|
853 | + $user1 = $this->createMock(User::class); |
|
854 | + $user1->expects($this->once()) |
|
855 | + ->method('composeAndStoreDisplayName') |
|
856 | + ->willReturn('Roland Deschain'); |
|
857 | + $user1->expects($this->any()) |
|
858 | + ->method('getDN') |
|
859 | + ->willReturn('dnOfRoland,dc=test'); |
|
860 | + |
|
861 | + $user2 = $this->createMock(User::class); |
|
862 | + $user2->expects($this->never()) |
|
863 | + ->method('composeAndStoreDisplayName'); |
|
864 | + $user2->expects($this->any()) |
|
865 | + ->method('getDN') |
|
866 | + ->willReturn('another DN'); |
|
867 | + |
|
868 | + $mapper = $this->createMock(UserMapping::class); |
|
869 | + $mapper->expects($this->any()) |
|
870 | + ->method('getUUIDByDN') |
|
871 | + ->willReturnCallback(function ($dn) { |
|
872 | + return $dn; |
|
873 | + }); |
|
874 | + |
|
875 | + $this->userManager->expects($this->any()) |
|
876 | + ->method('get') |
|
877 | + ->willReturnCallback(function ($uid) use ($user1, $user2) { |
|
878 | + if ($uid === 'gunslinger') { |
|
879 | + return $user1; |
|
880 | + } elseif ($uid === 'newyorker') { |
|
881 | + return $user2; |
|
882 | + } |
|
883 | + return null; |
|
884 | + }); |
|
885 | + $this->userManager->expects($this->any()) |
|
886 | + ->method('exists') |
|
887 | + ->willReturnCallback(function ($uid) use ($user1, $user2) { |
|
888 | + if ($uid === 'gunslinger') { |
|
889 | + return true; |
|
890 | + } elseif ($uid === 'newyorker') { |
|
891 | + return true; |
|
892 | + } |
|
893 | + return false; |
|
894 | + }); |
|
895 | + $this->access->expects($this->any()) |
|
896 | + ->method('getUserMapper') |
|
897 | + ->willReturn($mapper); |
|
898 | + $this->access->expects($this->any()) |
|
899 | + ->method('getUserDnByUuid') |
|
900 | + ->willReturnCallback(function ($uuid) { |
|
901 | + return $uuid . '1'; |
|
902 | + }); |
|
903 | + |
|
904 | + //with displayName |
|
905 | + $result = $backend->getDisplayName('gunslinger'); |
|
906 | + $this->assertEquals('Roland Deschain', $result); |
|
907 | + |
|
908 | + //empty displayname retrieved |
|
909 | + $result = $backend->getDisplayName('newyorker'); |
|
910 | + $this->assertEquals(null, $result); |
|
911 | + } |
|
912 | + |
|
913 | + public function testGetDisplayNamePublicAPI(): void { |
|
914 | + $this->access->expects($this->any()) |
|
915 | + ->method('username2dn') |
|
916 | + ->willReturnCallback(function ($uid) { |
|
917 | + switch ($uid) { |
|
918 | + case 'gunslinger': |
|
919 | + return 'dnOfRoland,dc=test'; |
|
920 | + break; |
|
921 | + case 'formerUser': |
|
922 | + return 'dnOfFormerUser,dc=test'; |
|
923 | + break; |
|
924 | + case 'newyorker': |
|
925 | + return 'dnOfNewYorker,dc=test'; |
|
926 | + break; |
|
927 | + case 'ladyofshadows': |
|
928 | + return 'dnOfLadyOfShadows,dc=test'; |
|
929 | + break; |
|
930 | + default: |
|
931 | + return false; |
|
932 | + } |
|
933 | + }); |
|
934 | + $this->prepareAccessForGetDisplayName(); |
|
935 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
936 | + $this->prepareMockForUserExists(); |
|
937 | + |
|
938 | + $this->connection->expects($this->any()) |
|
939 | + ->method('getConnectionResource') |
|
940 | + ->willReturnCallback(function () { |
|
941 | + return true; |
|
942 | + }); |
|
943 | + |
|
944 | + Server::get(IUserManager::class)->registerBackend($backend); |
|
945 | + |
|
946 | + $user1 = $this->createMock(User::class); |
|
947 | + $user1->expects($this->once()) |
|
948 | + ->method('composeAndStoreDisplayName') |
|
949 | + ->willReturn('Roland Deschain'); |
|
950 | + $user1->expects($this->any()) |
|
951 | + ->method('getDN') |
|
952 | + ->willReturn('dnOfRoland,dc=test'); |
|
953 | + |
|
954 | + $user2 = $this->createMock(User::class); |
|
955 | + $user2->expects($this->never()) |
|
956 | + ->method('composeAndStoreDisplayName'); |
|
957 | + $user2->expects($this->any()) |
|
958 | + ->method('getDN') |
|
959 | + ->willReturn('another DN'); |
|
960 | + |
|
961 | + $mapper = $this->createMock(UserMapping::class); |
|
962 | + $mapper->expects($this->any()) |
|
963 | + ->method('getUUIDByDN') |
|
964 | + ->willReturnCallback(function ($dn) { |
|
965 | + return $dn; |
|
966 | + }); |
|
967 | + |
|
968 | + $this->userManager->expects($this->any()) |
|
969 | + ->method('get') |
|
970 | + ->willReturnCallback(function ($uid) use ($user1, $user2) { |
|
971 | + if ($uid === 'gunslinger') { |
|
972 | + return $user1; |
|
973 | + } elseif ($uid === 'newyorker') { |
|
974 | + return $user2; |
|
975 | + } |
|
976 | + return null; |
|
977 | + }); |
|
978 | + $this->userManager->expects($this->any()) |
|
979 | + ->method('exists') |
|
980 | + ->willReturnCallback(function ($uid) use ($user1, $user2) { |
|
981 | + if ($uid === 'gunslinger') { |
|
982 | + return true; |
|
983 | + } elseif ($uid === 'newyorker') { |
|
984 | + return true; |
|
985 | + } |
|
986 | + return false; |
|
987 | + }); |
|
988 | + $this->access->expects($this->any()) |
|
989 | + ->method('getUserMapper') |
|
990 | + ->willReturn($mapper); |
|
991 | + $this->access->expects($this->any()) |
|
992 | + ->method('getUserDnByUuid') |
|
993 | + ->willReturnCallback(function ($uuid) { |
|
994 | + return $uuid . '1'; |
|
995 | + }); |
|
996 | + |
|
997 | + //with displayName |
|
998 | + $result = Server::get(IUserManager::class)->get('gunslinger')?->getDisplayName(); |
|
999 | + $this->assertEquals('Roland Deschain', $result); |
|
1000 | + |
|
1001 | + //empty displayname retrieved |
|
1002 | + $result = Server::get(IUserManager::class)->get('newyorker') === null ? 'newyorker' : Server::get(IUserManager::class)->get('newyorker')->getDisplayName(); |
|
1003 | + $this->assertEquals('newyorker', $result); |
|
1004 | + } |
|
1005 | + |
|
1006 | + public function testGetDisplayNameWithPlugin(): void { |
|
1007 | + $this->pluginManager->expects($this->once()) |
|
1008 | + ->method('implementsActions') |
|
1009 | + ->with(Backend::GET_DISPLAYNAME) |
|
1010 | + ->willReturn(true); |
|
1011 | + $this->pluginManager->expects($this->once()) |
|
1012 | + ->method('getDisplayName') |
|
1013 | + ->with('uid') |
|
1014 | + ->willReturn('result'); |
|
1015 | + |
|
1016 | + $this->assertEquals($this->backend->getDisplayName('uid'), 'result'); |
|
1017 | + } |
|
1018 | + |
|
1019 | + //no test for getDisplayNames, because it just invokes getUsers and |
|
1020 | + //getDisplayName |
|
1021 | + |
|
1022 | + public function testCountUsers(): void { |
|
1023 | + $this->access->expects($this->once()) |
|
1024 | + ->method('countUsers') |
|
1025 | + ->willReturn(5); |
|
1026 | + |
|
1027 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
1028 | + |
|
1029 | + $result = $backend->countUsers(); |
|
1030 | + $this->assertEquals(5, $result); |
|
1031 | + } |
|
1032 | + |
|
1033 | + public function testCountUsersFailing(): void { |
|
1034 | + $this->access->expects($this->once()) |
|
1035 | + ->method('countUsers') |
|
1036 | + ->willReturn(false); |
|
1037 | + |
|
1038 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
1039 | + |
|
1040 | + $result = $backend->countUsers(); |
|
1041 | + $this->assertFalse($result); |
|
1042 | + } |
|
1043 | + |
|
1044 | + public function testCountUsersWithPlugin(): void { |
|
1045 | + $this->pluginManager->expects($this->once()) |
|
1046 | + ->method('implementsActions') |
|
1047 | + ->with(Backend::COUNT_USERS) |
|
1048 | + ->willReturn(true); |
|
1049 | + $this->pluginManager->expects($this->once()) |
|
1050 | + ->method('countUsers') |
|
1051 | + ->willReturn(42); |
|
1052 | + |
|
1053 | + $this->assertEquals($this->backend->countUsers(), 42); |
|
1054 | + } |
|
1055 | + |
|
1056 | + public function testLoginName2UserNameSuccess(): void { |
|
1057 | + $loginName = 'Alice'; |
|
1058 | + $username = 'alice'; |
|
1059 | + $dn = 'uid=alice,dc=what,dc=ever'; |
|
1060 | + |
|
1061 | + $this->access->expects($this->once()) |
|
1062 | + ->method('fetchUsersByLoginName') |
|
1063 | + ->with($this->equalTo($loginName)) |
|
1064 | + ->willReturn([['dn' => [$dn]]]); |
|
1065 | + $this->access->expects($this->any()) |
|
1066 | + ->method('stringResemblesDN') |
|
1067 | + ->with($this->equalTo($dn)) |
|
1068 | + ->willReturn(true); |
|
1069 | + $this->access->expects($this->any()) |
|
1070 | + ->method('dn2username') |
|
1071 | + ->with($this->equalTo($dn)) |
|
1072 | + ->willReturn($username); |
|
1073 | + |
|
1074 | + $this->connection->expects($this->exactly(2)) |
|
1075 | + ->method('getFromCache') |
|
1076 | + ->with($this->equalTo('loginName2UserName-' . $loginName)) |
|
1077 | + ->willReturnOnConsecutiveCalls(null, $username); |
|
1078 | + $this->connection->expects($this->once()) |
|
1079 | + ->method('writeToCache') |
|
1080 | + ->with($this->equalTo('loginName2UserName-' . $loginName), $this->equalTo($username)); |
|
1081 | + |
|
1082 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
1083 | + $user = $this->createMock(User::class); |
|
1084 | + $user->expects($this->any()) |
|
1085 | + ->method('getUsername') |
|
1086 | + ->willReturn('alice'); |
|
1087 | + |
|
1088 | + $this->userManager->expects($this->atLeastOnce()) |
|
1089 | + ->method('get') |
|
1090 | + ->with($dn) |
|
1091 | + ->willReturn($user); |
|
1092 | + $this->userManager->expects($this->never()) |
|
1093 | + ->method('exists'); |
|
1094 | + $this->userManager->expects($this->any()) |
|
1095 | + ->method('getAttributes') |
|
1096 | + ->willReturn(['dn', 'uid', 'mail', 'displayname']); |
|
1097 | + |
|
1098 | + $name = $backend->loginName2UserName($loginName); |
|
1099 | + $this->assertSame($username, $name); |
|
1100 | + |
|
1101 | + // and once again to verify that caching works |
|
1102 | + $backend->loginName2UserName($loginName); |
|
1103 | + } |
|
1104 | + |
|
1105 | + public function testLoginName2UserNameNoUsersOnLDAP(): void { |
|
1106 | + $loginName = 'Loki'; |
|
1107 | + |
|
1108 | + $this->access->expects($this->once()) |
|
1109 | + ->method('fetchUsersByLoginName') |
|
1110 | + ->with($this->equalTo($loginName)) |
|
1111 | + ->willReturn([]); |
|
1112 | + $this->access->expects($this->never()) |
|
1113 | + ->method('stringResemblesDN'); |
|
1114 | + $this->access->expects($this->never()) |
|
1115 | + ->method('dn2username'); |
|
1116 | + |
|
1117 | + $this->connection->expects($this->exactly(2)) |
|
1118 | + ->method('getFromCache') |
|
1119 | + ->with($this->equalTo('loginName2UserName-' . $loginName)) |
|
1120 | + ->willReturnOnConsecutiveCalls(null, false); |
|
1121 | + $this->connection->expects($this->once()) |
|
1122 | + ->method('writeToCache') |
|
1123 | + ->with($this->equalTo('loginName2UserName-' . $loginName), false); |
|
1124 | + |
|
1125 | + $this->userManager->expects($this->any()) |
|
1126 | + ->method('getAttributes') |
|
1127 | + ->willReturn(['dn', 'uid', 'mail', 'displayname']); |
|
1128 | + |
|
1129 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
1130 | + $name = $backend->loginName2UserName($loginName); |
|
1131 | + $this->assertSame(false, $name); |
|
1132 | + |
|
1133 | + // and once again to verify that caching works |
|
1134 | + $backend->loginName2UserName($loginName); |
|
1135 | + } |
|
1136 | + |
|
1137 | + public function testLoginName2UserNameOfflineUser(): void { |
|
1138 | + $loginName = 'Alice'; |
|
1139 | + $dn = 'uid=alice,dc=what,dc=ever'; |
|
1140 | + |
|
1141 | + $offlineUser = $this->getMockBuilder(OfflineUser::class) |
|
1142 | + ->disableOriginalConstructor() |
|
1143 | + ->getMock(); |
|
1144 | + |
|
1145 | + $this->access->expects($this->once()) |
|
1146 | + ->method('fetchUsersByLoginName') |
|
1147 | + ->with($this->equalTo($loginName)) |
|
1148 | + ->willReturn([['dn' => [$dn]]]); |
|
1149 | + |
|
1150 | + $this->connection->expects($this->exactly(2)) |
|
1151 | + ->method('getFromCache') |
|
1152 | + ->with($this->equalTo('loginName2UserName-' . $loginName)) |
|
1153 | + ->willReturnOnConsecutiveCalls(null, false); |
|
1154 | + $this->connection->expects($this->once()) |
|
1155 | + ->method('writeToCache') |
|
1156 | + ->with($this->equalTo('loginName2UserName-' . $loginName), $this->equalTo(false)); |
|
1157 | + |
|
1158 | + $this->userManager->expects($this->any()) |
|
1159 | + ->method('get') |
|
1160 | + ->with($dn) |
|
1161 | + ->willReturn($offlineUser); |
|
1162 | + $this->userManager->expects($this->any()) |
|
1163 | + ->method('getAttributes') |
|
1164 | + ->willReturn(['dn', 'uid', 'mail', 'displayname']); |
|
1165 | + |
|
1166 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
1167 | + $name = $backend->loginName2UserName($loginName); |
|
1168 | + $this->assertSame(false, $name); |
|
1169 | + |
|
1170 | + // and once again to verify that caching works |
|
1171 | + $backend->loginName2UserName($loginName); |
|
1172 | + } |
|
1173 | + |
|
1174 | + /** |
|
1175 | + * Prepares the Access mock for setPassword tests |
|
1176 | + */ |
|
1177 | + private function prepareAccessForSetPassword($enablePasswordChange = true) { |
|
1178 | + $this->connection->expects($this->any()) |
|
1179 | + ->method('__get') |
|
1180 | + ->willReturnCallback(function ($name) use (&$enablePasswordChange) { |
|
1181 | + if ($name === 'ldapLoginFilter') { |
|
1182 | + return '%uid'; |
|
1183 | + } |
|
1184 | + if ($name === 'turnOnPasswordChange') { |
|
1185 | + return $enablePasswordChange?1:0; |
|
1186 | + } |
|
1187 | + return null; |
|
1188 | + }); |
|
1189 | + $this->connection->expects($this->any()) |
|
1190 | + ->method('getFromCache') |
|
1191 | + ->willReturnCallback(function ($uid) { |
|
1192 | + if ($uid === 'userExists' . 'roland') { |
|
1193 | + return true; |
|
1194 | + } |
|
1195 | + return null; |
|
1196 | + }); |
|
1197 | + |
|
1198 | + $this->access->expects($this->any()) |
|
1199 | + ->method('fetchListOfUsers') |
|
1200 | + ->willReturnCallback(function ($filter) { |
|
1201 | + if ($filter === 'roland') { |
|
1202 | + return [['dn' => ['dnOfRoland,dc=test']]]; |
|
1203 | + } |
|
1204 | + return []; |
|
1205 | + }); |
|
1206 | + $this->access->expects($this->any()) |
|
1207 | + ->method('fetchUsersByLoginName') |
|
1208 | + ->willReturnCallback(function ($uid) { |
|
1209 | + if ($uid === 'roland') { |
|
1210 | + return [['dn' => ['dnOfRoland,dc=test']]]; |
|
1211 | + } |
|
1212 | + return []; |
|
1213 | + }); |
|
1214 | + $this->access->expects($this->any()) |
|
1215 | + ->method('dn2username') |
|
1216 | + ->with($this->equalTo('dnOfRoland,dc=test')) |
|
1217 | + ->willReturn('roland'); |
|
1218 | + $this->access->expects($this->any()) |
|
1219 | + ->method('stringResemblesDN') |
|
1220 | + ->with($this->equalTo('dnOfRoland,dc=test')) |
|
1221 | + ->willReturn(true); |
|
1222 | + $this->access->expects($this->any()) |
|
1223 | + ->method('setPassword') |
|
1224 | + ->willReturnCallback(function ($uid, $password) { |
|
1225 | + if (strlen($password) <= 5) { |
|
1226 | + throw new HintException('Password fails quality checking policy', '', 19); |
|
1227 | + } |
|
1228 | + return true; |
|
1229 | + }); |
|
1230 | + } |
|
1231 | + |
|
1232 | + |
|
1233 | + public function testSetPasswordInvalid(): void { |
|
1234 | + $this->expectException(HintException::class); |
|
1235 | + $this->expectExceptionMessage('Password fails quality checking policy'); |
|
1236 | + |
|
1237 | + $this->prepareAccessForSetPassword($this->access); |
|
1238 | + $this->userManager->expects($this->atLeastOnce()) |
|
1239 | + ->method('get') |
|
1240 | + ->willReturn($this->createMock(User::class)); |
|
1241 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
1242 | + Server::get(IUserManager::class)->registerBackend($backend); |
|
1243 | + |
|
1244 | + $this->assertTrue(\OC_User::setPassword('roland', 'dt')); |
|
1245 | + } |
|
1246 | + |
|
1247 | + public function testSetPasswordValid(): void { |
|
1248 | + $this->prepareAccessForSetPassword($this->access); |
|
1249 | + |
|
1250 | + $this->userManager->expects($this->any()) |
|
1251 | + ->method('get') |
|
1252 | + ->willReturn($this->createMock(User::class)); |
|
1253 | + |
|
1254 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
1255 | + $this->userManager->expects($this->any()) |
|
1256 | + ->method('get') |
|
1257 | + ->willReturn($this->createMock(User::class)); |
|
1258 | + |
|
1259 | + Server::get(IUserManager::class)->registerBackend($backend); |
|
1260 | + |
|
1261 | + $this->assertTrue(\OC_User::setPassword('roland', 'dt12234$')); |
|
1262 | + } |
|
1263 | + |
|
1264 | + public function testSetPasswordValidDisabled(): void { |
|
1265 | + $this->userManager->expects($this->any()) |
|
1266 | + ->method('get') |
|
1267 | + ->willReturn($this->createMock(User::class)); |
|
1268 | + |
|
1269 | + $this->prepareAccessForSetPassword(false); |
|
1270 | + $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
|
1271 | + Server::get(IUserManager::class)->registerBackend($backend); |
|
1272 | + |
|
1273 | + $this->assertFalse(\OC_User::setPassword('roland', 'dt12234$')); |
|
1274 | + } |
|
1275 | + |
|
1276 | + |
|
1277 | + public function testSetPasswordWithInvalidUser(): void { |
|
1278 | + $this->expectException(\Exception::class); |
|
1279 | + $this->expectExceptionMessage('LDAP setPassword: Could not get user object for uid NotExistingUser. Maybe the LDAP entry has no set display name attribute?'); |
|
1280 | + |
|
1281 | + $this->userManager |
|
1282 | + ->expects($this->once()) |
|
1283 | + ->method('get') |
|
1284 | + ->with('NotExistingUser') |
|
1285 | + ->willReturn(null); |
|
1286 | + |
|
1287 | + $this->backend->setPassword('NotExistingUser', 'Password'); |
|
1288 | + } |
|
1289 | + |
|
1290 | + public function testSetPasswordWithUsernameFalse(): void { |
|
1291 | + $user = $this->createMock(User::class); |
|
1292 | + $user |
|
1293 | + ->expects($this->once()) |
|
1294 | + ->method('getUsername') |
|
1295 | + ->willReturn(false); |
|
1296 | + $this->userManager |
|
1297 | + ->expects($this->once()) |
|
1298 | + ->method('get') |
|
1299 | + ->with('NotExistingUser') |
|
1300 | + ->willReturn($user); |
|
1301 | + |
|
1302 | + /** @noinspection PhpUnhandledExceptionInspection */ |
|
1303 | + $this->assertFalse($this->backend->setPassword('NotExistingUser', 'Password')); |
|
1304 | + } |
|
1305 | + |
|
1306 | + public function testSetPasswordWithPlugin(): void { |
|
1307 | + $this->pluginManager->expects($this->once()) |
|
1308 | + ->method('implementsActions') |
|
1309 | + ->with(Backend::SET_PASSWORD) |
|
1310 | + ->willReturn(true); |
|
1311 | + $this->pluginManager->expects($this->once()) |
|
1312 | + ->method('setPassword') |
|
1313 | + ->with('uid', 'password') |
|
1314 | + ->willReturn('result'); |
|
1315 | + |
|
1316 | + /** @noinspection PhpUnhandledExceptionInspection */ |
|
1317 | + $this->assertEquals($this->backend->setPassword('uid', 'password'), 'result'); |
|
1318 | + } |
|
1319 | + |
|
1320 | + public static function avatarDataProvider(): array { |
|
1321 | + return [ |
|
1322 | + [ 'validImageData', false ], |
|
1323 | + [ 'corruptImageData', true ], |
|
1324 | + [ false, true] |
|
1325 | + ]; |
|
1326 | + } |
|
1327 | + |
|
1328 | + #[\PHPUnit\Framework\Attributes\DataProvider('avatarDataProvider')] |
|
1329 | + public function testCanChangeAvatar(string|bool $imageData, bool $expected): void { |
|
1330 | + $isValidImage = str_starts_with((string)$imageData, 'valid'); |
|
1331 | + |
|
1332 | + $user = $this->createMock(User::class); |
|
1333 | + $user->expects($this->once()) |
|
1334 | + ->method('getAvatarImage') |
|
1335 | + ->willReturn($imageData); |
|
1336 | + $user->expects($this->atMost(1)) |
|
1337 | + ->method('updateAvatar') |
|
1338 | + ->willReturn($isValidImage); |
|
1339 | + |
|
1340 | + $this->userManager->expects($this->atLeastOnce()) |
|
1341 | + ->method('get') |
|
1342 | + ->willReturn($user); |
|
1343 | + |
|
1344 | + /** @noinspection PhpUnhandledExceptionInspection */ |
|
1345 | + $this->assertSame($expected, $this->backend->canChangeAvatar('uid')); |
|
1346 | + } |
|
1347 | + |
|
1348 | + public function testCanChangeAvatarWithPlugin(): void { |
|
1349 | + $this->pluginManager->expects($this->once()) |
|
1350 | + ->method('implementsActions') |
|
1351 | + ->with(Backend::PROVIDE_AVATAR) |
|
1352 | + ->willReturn(true); |
|
1353 | + $this->pluginManager->expects($this->once()) |
|
1354 | + ->method('canChangeAvatar') |
|
1355 | + ->with('uid') |
|
1356 | + ->willReturn('result'); |
|
1357 | + |
|
1358 | + $this->assertEquals($this->backend->canChangeAvatar('uid'), 'result'); |
|
1359 | + } |
|
1360 | + |
|
1361 | + public function testSetDisplayNameWithPlugin(): void { |
|
1362 | + $newDisplayName = 'J. Baker'; |
|
1363 | + $this->pluginManager->expects($this->once()) |
|
1364 | + ->method('implementsActions') |
|
1365 | + ->with(Backend::SET_DISPLAYNAME) |
|
1366 | + ->willReturn(true); |
|
1367 | + $this->pluginManager->expects($this->once()) |
|
1368 | + ->method('setDisplayName') |
|
1369 | + ->with('uid', $newDisplayName) |
|
1370 | + ->willReturn($newDisplayName); |
|
1371 | + $this->access->expects($this->once()) |
|
1372 | + ->method('cacheUserDisplayName'); |
|
1373 | + |
|
1374 | + $this->assertEquals($newDisplayName, $this->backend->setDisplayName('uid', $newDisplayName)); |
|
1375 | + } |
|
1376 | + |
|
1377 | + |
|
1378 | + public function testSetDisplayNameErrorWithPlugin(): void { |
|
1379 | + $this->expectException(HintException::class); |
|
1380 | + |
|
1381 | + $newDisplayName = 'J. Baker'; |
|
1382 | + $this->pluginManager->expects($this->once()) |
|
1383 | + ->method('implementsActions') |
|
1384 | + ->with(Backend::SET_DISPLAYNAME) |
|
1385 | + ->willReturn(true); |
|
1386 | + $this->pluginManager->expects($this->once()) |
|
1387 | + ->method('setDisplayName') |
|
1388 | + ->with('uid', $newDisplayName) |
|
1389 | + ->willThrowException(new HintException('something happned')); |
|
1390 | + $this->access->expects($this->never()) |
|
1391 | + ->method('cacheUserDisplayName'); |
|
1392 | + |
|
1393 | + $this->backend->setDisplayName('uid', $newDisplayName); |
|
1394 | + } |
|
1395 | + |
|
1396 | + public function testSetDisplayNameFailing(): void { |
|
1397 | + $this->pluginManager->expects($this->once()) |
|
1398 | + ->method('implementsActions') |
|
1399 | + ->with(Backend::SET_DISPLAYNAME) |
|
1400 | + ->willReturn(false); |
|
1401 | + $this->access->expects($this->never()) |
|
1402 | + ->method('cacheUserDisplayName'); |
|
1403 | + |
|
1404 | + $this->assertFalse($this->backend->setDisplayName('uid', 'displayName')); |
|
1405 | + } |
|
1406 | + |
|
1407 | + public function testCreateUserWithPlugin(): void { |
|
1408 | + $uid = 'alien6372'; |
|
1409 | + $uuid = '123-2345-36756-123-2345234-4431'; |
|
1410 | + $pwd = 'passwørd'; |
|
1411 | + |
|
1412 | + $this->pluginManager->expects($this->once()) |
|
1413 | + ->method('implementsActions') |
|
1414 | + ->with(Backend::CREATE_USER) |
|
1415 | + ->willReturn(true); |
|
1416 | + $this->pluginManager->expects($this->once()) |
|
1417 | + ->method('createUser') |
|
1418 | + ->with($uid, $pwd) |
|
1419 | + ->willReturn('result'); |
|
1420 | + |
|
1421 | + $this->access->expects($this->atLeastOnce()) |
|
1422 | + ->method('getUUID') |
|
1423 | + ->willReturn($uuid); |
|
1424 | + $this->access->expects($this->once()) |
|
1425 | + ->method('mapAndAnnounceIfApplicable') |
|
1426 | + ->with($this->isInstanceOf(AbstractMapping::class), $this->anything(), $uid, $uuid, true); |
|
1427 | + $this->access->expects($this->any()) |
|
1428 | + ->method('getUserMapper') |
|
1429 | + ->willReturn($this->createMock(UserMapping::class)); |
|
1430 | + |
|
1431 | + $this->assertEquals($this->backend->createUser($uid, $pwd), true); |
|
1432 | + } |
|
1433 | + |
|
1434 | + public function testCreateUserFailing(): void { |
|
1435 | + $this->pluginManager->expects($this->once()) |
|
1436 | + ->method('implementsActions') |
|
1437 | + ->with(Backend::CREATE_USER) |
|
1438 | + ->willReturn(false); |
|
1439 | + |
|
1440 | + $this->assertFalse($this->backend->createUser('uid', 'password')); |
|
1441 | + } |
|
1442 | + |
|
1443 | + public static function actionProvider(): array { |
|
1444 | + return [ |
|
1445 | + [ 'ldapUserAvatarRule', 'default', Backend::PROVIDE_AVATAR, true], |
|
1446 | + [ 'ldapUserAvatarRule', 'data:selfiePhoto', Backend::PROVIDE_AVATAR, true], |
|
1447 | + [ 'ldapUserAvatarRule', 'none', Backend::PROVIDE_AVATAR, false], |
|
1448 | + [ 'turnOnPasswordChange', 0, Backend::SET_PASSWORD, false], |
|
1449 | + [ 'turnOnPasswordChange', 1, Backend::SET_PASSWORD, true], |
|
1450 | + ]; |
|
1451 | + } |
|
1452 | + |
|
1453 | + #[\PHPUnit\Framework\Attributes\DataProvider('actionProvider')] |
|
1454 | + public function testImplementsAction(string $configurable, string|int $value, int $actionCode, bool $expected): void { |
|
1455 | + $this->pluginManager->expects($this->once()) |
|
1456 | + ->method('getImplementedActions') |
|
1457 | + ->willReturn(0); |
|
1458 | + |
|
1459 | + $this->connection->expects($this->any()) |
|
1460 | + ->method('__get') |
|
1461 | + ->willReturnMap([ |
|
1462 | + [$configurable, $value], |
|
1463 | + ]); |
|
1464 | + |
|
1465 | + $this->assertSame($expected, $this->backend->implementsActions($actionCode)); |
|
1466 | + } |
|
1467 | 1467 | } |
@@ -84,7 +84,7 @@ discard block |
||
84 | 84 | private function prepareMockForUserExists(): void { |
85 | 85 | $this->access->expects($this->any()) |
86 | 86 | ->method('username2dn') |
87 | - ->willReturnCallback(function ($uid) { |
|
87 | + ->willReturnCallback(function($uid) { |
|
88 | 88 | switch ($uid) { |
89 | 89 | case 'gunslinger': |
90 | 90 | return 'dnOfRoland,dc=test'; |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | private function prepareAccessForCheckPassword(bool $noDisplayName = false): void { |
114 | 114 | $this->connection->expects($this->any()) |
115 | 115 | ->method('__get') |
116 | - ->willReturnCallback(function ($name) { |
|
116 | + ->willReturnCallback(function($name) { |
|
117 | 117 | if ($name === 'ldapLoginFilter') { |
118 | 118 | return '%uid'; |
119 | 119 | } |
@@ -122,7 +122,7 @@ discard block |
||
122 | 122 | |
123 | 123 | $this->access->expects($this->any()) |
124 | 124 | ->method('fetchListOfUsers') |
125 | - ->willReturnCallback(function ($filter) { |
|
125 | + ->willReturnCallback(function($filter) { |
|
126 | 126 | if ($filter === 'roland') { |
127 | 127 | return [['dn' => ['dnOfRoland,dc=test']]]; |
128 | 128 | } |
@@ -130,7 +130,7 @@ discard block |
||
130 | 130 | }); |
131 | 131 | $this->access->expects($this->any()) |
132 | 132 | ->method('fetchUsersByLoginName') |
133 | - ->willReturnCallback(function ($uid) { |
|
133 | + ->willReturnCallback(function($uid) { |
|
134 | 134 | if ($uid === 'roland') { |
135 | 135 | return [['dn' => ['dnOfRoland,dc=test']]]; |
136 | 136 | } |
@@ -155,7 +155,7 @@ discard block |
||
155 | 155 | ->willReturn(true); |
156 | 156 | $this->access->expects($this->any()) |
157 | 157 | ->method('areCredentialsValid') |
158 | - ->willReturnCallback(function ($dn, $pwd) { |
|
158 | + ->willReturnCallback(function($dn, $pwd) { |
|
159 | 159 | if ($pwd === 'dt19') { |
160 | 160 | return true; |
161 | 161 | } |
@@ -349,22 +349,22 @@ discard block |
||
349 | 349 | private function prepareAccessForGetUsers() { |
350 | 350 | $this->access->expects($this->once()) |
351 | 351 | ->method('escapeFilterPart') |
352 | - ->willReturnCallback(function ($search) { |
|
352 | + ->willReturnCallback(function($search) { |
|
353 | 353 | return $search; |
354 | 354 | }); |
355 | 355 | $this->access->expects($this->any()) |
356 | 356 | ->method('getFilterPartForUserSearch') |
357 | - ->willReturnCallback(function ($search) { |
|
357 | + ->willReturnCallback(function($search) { |
|
358 | 358 | return $search; |
359 | 359 | }); |
360 | 360 | $this->access->expects($this->any()) |
361 | 361 | ->method('combineFilterWithAnd') |
362 | - ->willReturnCallback(function ($param) { |
|
362 | + ->willReturnCallback(function($param) { |
|
363 | 363 | return $param[2]; |
364 | 364 | }); |
365 | 365 | $this->access->expects($this->any()) |
366 | 366 | ->method('fetchListOfUsers') |
367 | - ->willReturnCallback(function ($search, $a, $l, $o) { |
|
367 | + ->willReturnCallback(function($search, $a, $l, $o) { |
|
368 | 368 | $users = ['gunslinger', 'newyorker', 'ladyofshadows']; |
369 | 369 | if (empty($search)) { |
370 | 370 | $result = $users; |
@@ -434,7 +434,7 @@ discard block |
||
434 | 434 | |
435 | 435 | private function getUsers($search = '', $limit = null, $offset = null) { |
436 | 436 | $users = Server::get(IUserManager::class)->search($search, $limit, $offset); |
437 | - $uids = array_map(function (IUser $user) { |
|
437 | + $uids = array_map(function(IUser $user) { |
|
438 | 438 | return $user->getUID(); |
439 | 439 | }, $users); |
440 | 440 | return $uids; |
@@ -536,7 +536,7 @@ discard block |
||
536 | 536 | |
537 | 537 | $this->access->expects($this->any()) |
538 | 538 | ->method('readAttribute') |
539 | - ->willReturnCallback(function ($dn) { |
|
539 | + ->willReturnCallback(function($dn) { |
|
540 | 540 | if ($dn === 'dnOfRoland,dc=test') { |
541 | 541 | return []; |
542 | 542 | } |
@@ -561,7 +561,7 @@ discard block |
||
561 | 561 | |
562 | 562 | $this->access->expects($this->any()) |
563 | 563 | ->method('readAttribute') |
564 | - ->willReturnCallback(function ($dn) { |
|
564 | + ->willReturnCallback(function($dn) { |
|
565 | 565 | if ($dn === 'dnOfRoland,dc=test') { |
566 | 566 | return []; |
567 | 567 | } |
@@ -596,7 +596,7 @@ discard block |
||
596 | 596 | |
597 | 597 | $this->connection->expects($this->any()) |
598 | 598 | ->method('__get') |
599 | - ->willReturnCallback(function ($name) { |
|
599 | + ->willReturnCallback(function($name) { |
|
600 | 600 | if ($name === 'homeFolderNamingRule') { |
601 | 601 | return 'attr:testAttribute'; |
602 | 602 | } |
@@ -605,7 +605,7 @@ discard block |
||
605 | 605 | |
606 | 606 | $this->access->expects($this->any()) |
607 | 607 | ->method('readAttribute') |
608 | - ->willReturnCallback(function ($dn, $attr) { |
|
608 | + ->willReturnCallback(function($dn, $attr) { |
|
609 | 609 | switch ($dn) { |
610 | 610 | case 'dnOfRoland,dc=test': |
611 | 611 | if ($attr === 'testAttribute') { |
@@ -649,11 +649,11 @@ discard block |
||
649 | 649 | $this->prepareMockForUserExists(); |
650 | 650 | |
651 | 651 | $dataDir = Server::get(IConfig::class)->getSystemValue( |
652 | - 'datadirectory', \OC::$SERVERROOT . '/data'); |
|
652 | + 'datadirectory', \OC::$SERVERROOT.'/data'); |
|
653 | 653 | |
654 | 654 | $this->connection->expects($this->any()) |
655 | 655 | ->method('__get') |
656 | - ->willReturnCallback(function ($name) { |
|
656 | + ->willReturnCallback(function($name) { |
|
657 | 657 | if ($name === 'homeFolderNamingRule') { |
658 | 658 | return 'attr:testAttribute'; |
659 | 659 | } |
@@ -662,7 +662,7 @@ discard block |
||
662 | 662 | |
663 | 663 | $this->access->expects($this->any()) |
664 | 664 | ->method('readAttribute') |
665 | - ->willReturnCallback(function ($dn, $attr) { |
|
665 | + ->willReturnCallback(function($dn, $attr) { |
|
666 | 666 | switch ($dn) { |
667 | 667 | case 'dnOfLadyOfShadows,dc=test': |
668 | 668 | if ($attr === 'testAttribute') { |
@@ -684,7 +684,7 @@ discard block |
||
684 | 684 | ->willReturn('dnOfLadyOfShadows,dc=test'); |
685 | 685 | $user->expects($this->any()) |
686 | 686 | ->method('getHomePath') |
687 | - ->willReturn($dataDir . '/susannah/'); |
|
687 | + ->willReturn($dataDir.'/susannah/'); |
|
688 | 688 | |
689 | 689 | $this->userManager->expects($this->atLeastOnce()) |
690 | 690 | ->method('get') |
@@ -696,7 +696,7 @@ discard block |
||
696 | 696 | |
697 | 697 | /** @noinspection PhpUnhandledExceptionInspection */ |
698 | 698 | $result = $backend->getHome('ladyofshadows'); |
699 | - $this->assertEquals($dataDir . '/susannah/', $result); |
|
699 | + $this->assertEquals($dataDir.'/susannah/', $result); |
|
700 | 700 | } |
701 | 701 | |
702 | 702 | |
@@ -708,7 +708,7 @@ discard block |
||
708 | 708 | |
709 | 709 | $this->connection->expects($this->any()) |
710 | 710 | ->method('__get') |
711 | - ->willReturnCallback(function ($name) { |
|
711 | + ->willReturnCallback(function($name) { |
|
712 | 712 | if ($name === 'homeFolderNamingRule') { |
713 | 713 | return 'attr:testAttribute'; |
714 | 714 | } |
@@ -716,7 +716,7 @@ discard block |
||
716 | 716 | }); |
717 | 717 | $this->access->expects($this->any()) |
718 | 718 | ->method('readAttribute') |
719 | - ->willReturnCallback(function ($dn, $attr) { |
|
719 | + ->willReturnCallback(function($dn, $attr) { |
|
720 | 720 | switch ($dn) { |
721 | 721 | default: |
722 | 722 | return false; |
@@ -753,7 +753,7 @@ discard block |
||
753 | 753 | |
754 | 754 | $this->connection->expects($this->any()) |
755 | 755 | ->method('__get') |
756 | - ->willReturnCallback(function ($name) { |
|
756 | + ->willReturnCallback(function($name) { |
|
757 | 757 | if ($name === 'homeFolderNamingRule') { |
758 | 758 | return 'attr:testAttribute'; |
759 | 759 | } |
@@ -800,7 +800,7 @@ discard block |
||
800 | 800 | |
801 | 801 | $this->connection->expects($this->any()) |
802 | 802 | ->method('getFromCache') |
803 | - ->willReturnCallback(function ($uid) { |
|
803 | + ->willReturnCallback(function($uid) { |
|
804 | 804 | return true; |
805 | 805 | }); |
806 | 806 | |
@@ -811,7 +811,7 @@ discard block |
||
811 | 811 | private function prepareAccessForGetDisplayName() { |
812 | 812 | $this->connection->expects($this->any()) |
813 | 813 | ->method('__get') |
814 | - ->willReturnCallback(function ($name) { |
|
814 | + ->willReturnCallback(function($name) { |
|
815 | 815 | if ($name === 'ldapUserDisplayName') { |
816 | 816 | return 'displayname'; |
817 | 817 | } elseif ($name === 'ldapUserDisplayName2') { |
@@ -822,7 +822,7 @@ discard block |
||
822 | 822 | |
823 | 823 | $this->access->expects($this->any()) |
824 | 824 | ->method('readAttribute') |
825 | - ->willReturnCallback(function ($dn, $attr) { |
|
825 | + ->willReturnCallback(function($dn, $attr) { |
|
826 | 826 | switch ($dn) { |
827 | 827 | case 'dnOfRoland,dc=test': |
828 | 828 | if ($attr === 'displayname') { |
@@ -846,7 +846,7 @@ discard block |
||
846 | 846 | |
847 | 847 | $this->connection->expects($this->any()) |
848 | 848 | ->method('getConnectionResource') |
849 | - ->willReturnCallback(function () { |
|
849 | + ->willReturnCallback(function() { |
|
850 | 850 | return true; |
851 | 851 | }); |
852 | 852 | |
@@ -868,13 +868,13 @@ discard block |
||
868 | 868 | $mapper = $this->createMock(UserMapping::class); |
869 | 869 | $mapper->expects($this->any()) |
870 | 870 | ->method('getUUIDByDN') |
871 | - ->willReturnCallback(function ($dn) { |
|
871 | + ->willReturnCallback(function($dn) { |
|
872 | 872 | return $dn; |
873 | 873 | }); |
874 | 874 | |
875 | 875 | $this->userManager->expects($this->any()) |
876 | 876 | ->method('get') |
877 | - ->willReturnCallback(function ($uid) use ($user1, $user2) { |
|
877 | + ->willReturnCallback(function($uid) use ($user1, $user2) { |
|
878 | 878 | if ($uid === 'gunslinger') { |
879 | 879 | return $user1; |
880 | 880 | } elseif ($uid === 'newyorker') { |
@@ -884,7 +884,7 @@ discard block |
||
884 | 884 | }); |
885 | 885 | $this->userManager->expects($this->any()) |
886 | 886 | ->method('exists') |
887 | - ->willReturnCallback(function ($uid) use ($user1, $user2) { |
|
887 | + ->willReturnCallback(function($uid) use ($user1, $user2) { |
|
888 | 888 | if ($uid === 'gunslinger') { |
889 | 889 | return true; |
890 | 890 | } elseif ($uid === 'newyorker') { |
@@ -897,8 +897,8 @@ discard block |
||
897 | 897 | ->willReturn($mapper); |
898 | 898 | $this->access->expects($this->any()) |
899 | 899 | ->method('getUserDnByUuid') |
900 | - ->willReturnCallback(function ($uuid) { |
|
901 | - return $uuid . '1'; |
|
900 | + ->willReturnCallback(function($uuid) { |
|
901 | + return $uuid.'1'; |
|
902 | 902 | }); |
903 | 903 | |
904 | 904 | //with displayName |
@@ -913,7 +913,7 @@ discard block |
||
913 | 913 | public function testGetDisplayNamePublicAPI(): void { |
914 | 914 | $this->access->expects($this->any()) |
915 | 915 | ->method('username2dn') |
916 | - ->willReturnCallback(function ($uid) { |
|
916 | + ->willReturnCallback(function($uid) { |
|
917 | 917 | switch ($uid) { |
918 | 918 | case 'gunslinger': |
919 | 919 | return 'dnOfRoland,dc=test'; |
@@ -937,7 +937,7 @@ discard block |
||
937 | 937 | |
938 | 938 | $this->connection->expects($this->any()) |
939 | 939 | ->method('getConnectionResource') |
940 | - ->willReturnCallback(function () { |
|
940 | + ->willReturnCallback(function() { |
|
941 | 941 | return true; |
942 | 942 | }); |
943 | 943 | |
@@ -961,13 +961,13 @@ discard block |
||
961 | 961 | $mapper = $this->createMock(UserMapping::class); |
962 | 962 | $mapper->expects($this->any()) |
963 | 963 | ->method('getUUIDByDN') |
964 | - ->willReturnCallback(function ($dn) { |
|
964 | + ->willReturnCallback(function($dn) { |
|
965 | 965 | return $dn; |
966 | 966 | }); |
967 | 967 | |
968 | 968 | $this->userManager->expects($this->any()) |
969 | 969 | ->method('get') |
970 | - ->willReturnCallback(function ($uid) use ($user1, $user2) { |
|
970 | + ->willReturnCallback(function($uid) use ($user1, $user2) { |
|
971 | 971 | if ($uid === 'gunslinger') { |
972 | 972 | return $user1; |
973 | 973 | } elseif ($uid === 'newyorker') { |
@@ -977,7 +977,7 @@ discard block |
||
977 | 977 | }); |
978 | 978 | $this->userManager->expects($this->any()) |
979 | 979 | ->method('exists') |
980 | - ->willReturnCallback(function ($uid) use ($user1, $user2) { |
|
980 | + ->willReturnCallback(function($uid) use ($user1, $user2) { |
|
981 | 981 | if ($uid === 'gunslinger') { |
982 | 982 | return true; |
983 | 983 | } elseif ($uid === 'newyorker') { |
@@ -990,8 +990,8 @@ discard block |
||
990 | 990 | ->willReturn($mapper); |
991 | 991 | $this->access->expects($this->any()) |
992 | 992 | ->method('getUserDnByUuid') |
993 | - ->willReturnCallback(function ($uuid) { |
|
994 | - return $uuid . '1'; |
|
993 | + ->willReturnCallback(function($uuid) { |
|
994 | + return $uuid.'1'; |
|
995 | 995 | }); |
996 | 996 | |
997 | 997 | //with displayName |
@@ -1073,11 +1073,11 @@ discard block |
||
1073 | 1073 | |
1074 | 1074 | $this->connection->expects($this->exactly(2)) |
1075 | 1075 | ->method('getFromCache') |
1076 | - ->with($this->equalTo('loginName2UserName-' . $loginName)) |
|
1076 | + ->with($this->equalTo('loginName2UserName-'.$loginName)) |
|
1077 | 1077 | ->willReturnOnConsecutiveCalls(null, $username); |
1078 | 1078 | $this->connection->expects($this->once()) |
1079 | 1079 | ->method('writeToCache') |
1080 | - ->with($this->equalTo('loginName2UserName-' . $loginName), $this->equalTo($username)); |
|
1080 | + ->with($this->equalTo('loginName2UserName-'.$loginName), $this->equalTo($username)); |
|
1081 | 1081 | |
1082 | 1082 | $backend = new UserLDAP($this->access, $this->notificationManager, $this->pluginManager, $this->logger, $this->deletedUsersIndex); |
1083 | 1083 | $user = $this->createMock(User::class); |
@@ -1116,11 +1116,11 @@ discard block |
||
1116 | 1116 | |
1117 | 1117 | $this->connection->expects($this->exactly(2)) |
1118 | 1118 | ->method('getFromCache') |
1119 | - ->with($this->equalTo('loginName2UserName-' . $loginName)) |
|
1119 | + ->with($this->equalTo('loginName2UserName-'.$loginName)) |
|
1120 | 1120 | ->willReturnOnConsecutiveCalls(null, false); |
1121 | 1121 | $this->connection->expects($this->once()) |
1122 | 1122 | ->method('writeToCache') |
1123 | - ->with($this->equalTo('loginName2UserName-' . $loginName), false); |
|
1123 | + ->with($this->equalTo('loginName2UserName-'.$loginName), false); |
|
1124 | 1124 | |
1125 | 1125 | $this->userManager->expects($this->any()) |
1126 | 1126 | ->method('getAttributes') |
@@ -1149,11 +1149,11 @@ discard block |
||
1149 | 1149 | |
1150 | 1150 | $this->connection->expects($this->exactly(2)) |
1151 | 1151 | ->method('getFromCache') |
1152 | - ->with($this->equalTo('loginName2UserName-' . $loginName)) |
|
1152 | + ->with($this->equalTo('loginName2UserName-'.$loginName)) |
|
1153 | 1153 | ->willReturnOnConsecutiveCalls(null, false); |
1154 | 1154 | $this->connection->expects($this->once()) |
1155 | 1155 | ->method('writeToCache') |
1156 | - ->with($this->equalTo('loginName2UserName-' . $loginName), $this->equalTo(false)); |
|
1156 | + ->with($this->equalTo('loginName2UserName-'.$loginName), $this->equalTo(false)); |
|
1157 | 1157 | |
1158 | 1158 | $this->userManager->expects($this->any()) |
1159 | 1159 | ->method('get') |
@@ -1177,19 +1177,19 @@ discard block |
||
1177 | 1177 | private function prepareAccessForSetPassword($enablePasswordChange = true) { |
1178 | 1178 | $this->connection->expects($this->any()) |
1179 | 1179 | ->method('__get') |
1180 | - ->willReturnCallback(function ($name) use (&$enablePasswordChange) { |
|
1180 | + ->willReturnCallback(function($name) use (&$enablePasswordChange) { |
|
1181 | 1181 | if ($name === 'ldapLoginFilter') { |
1182 | 1182 | return '%uid'; |
1183 | 1183 | } |
1184 | 1184 | if ($name === 'turnOnPasswordChange') { |
1185 | - return $enablePasswordChange?1:0; |
|
1185 | + return $enablePasswordChange ? 1 : 0; |
|
1186 | 1186 | } |
1187 | 1187 | return null; |
1188 | 1188 | }); |
1189 | 1189 | $this->connection->expects($this->any()) |
1190 | 1190 | ->method('getFromCache') |
1191 | - ->willReturnCallback(function ($uid) { |
|
1192 | - if ($uid === 'userExists' . 'roland') { |
|
1191 | + ->willReturnCallback(function($uid) { |
|
1192 | + if ($uid === 'userExists'.'roland') { |
|
1193 | 1193 | return true; |
1194 | 1194 | } |
1195 | 1195 | return null; |
@@ -1197,7 +1197,7 @@ discard block |
||
1197 | 1197 | |
1198 | 1198 | $this->access->expects($this->any()) |
1199 | 1199 | ->method('fetchListOfUsers') |
1200 | - ->willReturnCallback(function ($filter) { |
|
1200 | + ->willReturnCallback(function($filter) { |
|
1201 | 1201 | if ($filter === 'roland') { |
1202 | 1202 | return [['dn' => ['dnOfRoland,dc=test']]]; |
1203 | 1203 | } |
@@ -1205,7 +1205,7 @@ discard block |
||
1205 | 1205 | }); |
1206 | 1206 | $this->access->expects($this->any()) |
1207 | 1207 | ->method('fetchUsersByLoginName') |
1208 | - ->willReturnCallback(function ($uid) { |
|
1208 | + ->willReturnCallback(function($uid) { |
|
1209 | 1209 | if ($uid === 'roland') { |
1210 | 1210 | return [['dn' => ['dnOfRoland,dc=test']]]; |
1211 | 1211 | } |
@@ -1221,7 +1221,7 @@ discard block |
||
1221 | 1221 | ->willReturn(true); |
1222 | 1222 | $this->access->expects($this->any()) |
1223 | 1223 | ->method('setPassword') |
1224 | - ->willReturnCallback(function ($uid, $password) { |
|
1224 | + ->willReturnCallback(function($uid, $password) { |
|
1225 | 1225 | if (strlen($password) <= 5) { |
1226 | 1226 | throw new HintException('Password fails quality checking policy', '', 19); |
1227 | 1227 | } |
@@ -1319,15 +1319,15 @@ discard block |
||
1319 | 1319 | |
1320 | 1320 | public static function avatarDataProvider(): array { |
1321 | 1321 | return [ |
1322 | - [ 'validImageData', false ], |
|
1323 | - [ 'corruptImageData', true ], |
|
1324 | - [ false, true] |
|
1322 | + ['validImageData', false], |
|
1323 | + ['corruptImageData', true], |
|
1324 | + [false, true] |
|
1325 | 1325 | ]; |
1326 | 1326 | } |
1327 | 1327 | |
1328 | 1328 | #[\PHPUnit\Framework\Attributes\DataProvider('avatarDataProvider')] |
1329 | - public function testCanChangeAvatar(string|bool $imageData, bool $expected): void { |
|
1330 | - $isValidImage = str_starts_with((string)$imageData, 'valid'); |
|
1329 | + public function testCanChangeAvatar(string | bool $imageData, bool $expected): void { |
|
1330 | + $isValidImage = str_starts_with((string) $imageData, 'valid'); |
|
1331 | 1331 | |
1332 | 1332 | $user = $this->createMock(User::class); |
1333 | 1333 | $user->expects($this->once()) |
@@ -1442,16 +1442,16 @@ discard block |
||
1442 | 1442 | |
1443 | 1443 | public static function actionProvider(): array { |
1444 | 1444 | return [ |
1445 | - [ 'ldapUserAvatarRule', 'default', Backend::PROVIDE_AVATAR, true], |
|
1446 | - [ 'ldapUserAvatarRule', 'data:selfiePhoto', Backend::PROVIDE_AVATAR, true], |
|
1447 | - [ 'ldapUserAvatarRule', 'none', Backend::PROVIDE_AVATAR, false], |
|
1448 | - [ 'turnOnPasswordChange', 0, Backend::SET_PASSWORD, false], |
|
1449 | - [ 'turnOnPasswordChange', 1, Backend::SET_PASSWORD, true], |
|
1445 | + ['ldapUserAvatarRule', 'default', Backend::PROVIDE_AVATAR, true], |
|
1446 | + ['ldapUserAvatarRule', 'data:selfiePhoto', Backend::PROVIDE_AVATAR, true], |
|
1447 | + ['ldapUserAvatarRule', 'none', Backend::PROVIDE_AVATAR, false], |
|
1448 | + ['turnOnPasswordChange', 0, Backend::SET_PASSWORD, false], |
|
1449 | + ['turnOnPasswordChange', 1, Backend::SET_PASSWORD, true], |
|
1450 | 1450 | ]; |
1451 | 1451 | } |
1452 | 1452 | |
1453 | 1453 | #[\PHPUnit\Framework\Attributes\DataProvider('actionProvider')] |
1454 | - public function testImplementsAction(string $configurable, string|int $value, int $actionCode, bool $expected): void { |
|
1454 | + public function testImplementsAction(string $configurable, string | int $value, int $actionCode, bool $expected): void { |
|
1455 | 1455 | $this->pluginManager->expects($this->once()) |
1456 | 1456 | ->method('getImplementedActions') |
1457 | 1457 | ->willReturn(0); |
@@ -17,154 +17,154 @@ |
||
17 | 17 | use Test\TestCase; |
18 | 18 | |
19 | 19 | class UUIDFixInsertTest extends TestCase { |
20 | - protected IConfig&MockObject $config; |
|
21 | - protected UserMapping&MockObject $userMapper; |
|
22 | - protected GroupMapping&MockObject $groupMapper; |
|
23 | - protected IJobList&MockObject $jobList; |
|
24 | - protected UUIDFixInsert $job; |
|
25 | - |
|
26 | - protected function setUp(): void { |
|
27 | - parent::setUp(); |
|
28 | - |
|
29 | - $this->jobList = $this->createMock(IJobList::class); |
|
30 | - $this->config = $this->createMock(IConfig::class); |
|
31 | - $this->userMapper = $this->createMock(UserMapping::class); |
|
32 | - $this->groupMapper = $this->createMock(GroupMapping::class); |
|
33 | - $this->job = new UUIDFixInsert( |
|
34 | - $this->config, |
|
35 | - $this->userMapper, |
|
36 | - $this->groupMapper, |
|
37 | - $this->jobList |
|
38 | - ); |
|
39 | - } |
|
40 | - |
|
41 | - public function testGetName(): void { |
|
42 | - $this->assertSame('Insert UUIDFix background job for user and group in batches', $this->job->getName()); |
|
43 | - } |
|
44 | - |
|
45 | - public static function recordProvider(): array { |
|
46 | - $record = [ |
|
47 | - 'dn' => 'cn=somerecord,dc=somewhere', |
|
48 | - 'name' => 'Something', |
|
49 | - 'uuid' => 'AB12-3456-CDEF7-8GH9' |
|
50 | - ]; |
|
51 | - |
|
52 | - $userBatches = [ |
|
53 | - 0 => array_fill(0, 50, $record), |
|
54 | - 1 => array_fill(0, 50, $record), |
|
55 | - 2 => array_fill(0, 13, $record), |
|
56 | - ]; |
|
57 | - |
|
58 | - $groupBatches = [ |
|
59 | - 0 => array_fill(0, 7, $record), |
|
60 | - ]; |
|
61 | - |
|
62 | - return [ |
|
63 | - ['userBatches' => $userBatches, 'groupBatches' => $groupBatches] |
|
64 | - ]; |
|
65 | - } |
|
66 | - |
|
67 | - public static function recordProviderTooLongAndNone(): array { |
|
68 | - $record = [ |
|
69 | - 'dn' => 'cn=somerecord,dc=somewhere', |
|
70 | - 'name' => 'Something', |
|
71 | - 'uuid' => 'AB12-3456-CDEF7-8GH9' |
|
72 | - ]; |
|
73 | - |
|
74 | - $userBatches = [ |
|
75 | - 0 => array_fill(0, 50, $record), |
|
76 | - 1 => array_fill(0, 40, $record), |
|
77 | - 2 => array_fill(0, 32, $record), |
|
78 | - 3 => array_fill(0, 32, $record), |
|
79 | - 4 => array_fill(0, 23, $record), |
|
80 | - ]; |
|
81 | - |
|
82 | - $groupBatches = [0 => []]; |
|
83 | - |
|
84 | - return [ |
|
85 | - ['userBatches' => $userBatches, 'groupBatches' => $groupBatches] |
|
86 | - ]; |
|
87 | - } |
|
88 | - |
|
89 | - #[\PHPUnit\Framework\Attributes\DataProvider('recordProvider')] |
|
90 | - public function testRun(array $userBatches, array $groupBatches): void { |
|
91 | - $this->config->expects($this->once()) |
|
92 | - ->method('getAppValue') |
|
93 | - ->with('user_ldap', 'installed_version', '1.2.1') |
|
94 | - ->willReturn('1.2.0'); |
|
95 | - |
|
96 | - $this->userMapper->expects($this->exactly(3)) |
|
97 | - ->method('getList') |
|
98 | - ->willReturnMap([ |
|
99 | - [0, 50, false, $userBatches[0]], |
|
100 | - [50, 50, false, $userBatches[1]], |
|
101 | - [100, 50, false, $userBatches[2]], |
|
102 | - ]); |
|
103 | - |
|
104 | - $this->groupMapper->expects($this->exactly(1)) |
|
105 | - ->method('getList') |
|
106 | - ->with(0, 50) |
|
107 | - ->willReturn($groupBatches[0]); |
|
108 | - |
|
109 | - $this->jobList->expects($this->exactly(4)) |
|
110 | - ->method('add'); |
|
111 | - |
|
112 | - /** @var IOutput $out */ |
|
113 | - $out = $this->createMock(IOutput::class); |
|
114 | - $this->job->run($out); |
|
115 | - } |
|
116 | - |
|
117 | - #[\PHPUnit\Framework\Attributes\DataProvider('recordProviderTooLongAndNone')] |
|
118 | - public function testRunWithManyAndNone(array $userBatches, array $groupBatches): void { |
|
119 | - $this->config->expects($this->once()) |
|
120 | - ->method('getAppValue') |
|
121 | - ->with('user_ldap', 'installed_version', '1.2.1') |
|
122 | - ->willReturn('1.2.0'); |
|
123 | - |
|
124 | - $this->userMapper->expects($this->exactly(5)) |
|
125 | - ->method('getList') |
|
126 | - ->willReturnMap([ |
|
127 | - [0, 50, false, $userBatches[0]], |
|
128 | - [0, 40, false, $userBatches[1]], |
|
129 | - [0, 32, false, $userBatches[2]], |
|
130 | - [32, 32, false, $userBatches[3]], |
|
131 | - [64, 32, false, $userBatches[4]], |
|
132 | - ]); |
|
133 | - |
|
134 | - $this->groupMapper->expects($this->once()) |
|
135 | - ->method('getList') |
|
136 | - ->with(0, 50) |
|
137 | - ->willReturn($groupBatches[0]); |
|
138 | - |
|
139 | - $this->jobList->expects($this->exactly(5)) |
|
140 | - ->method('add') |
|
141 | - ->willReturnOnConsecutiveCalls( |
|
142 | - $this->throwException(new \InvalidArgumentException('Background job arguments can\'t exceed 4000 etc')), |
|
143 | - $this->throwException(new \InvalidArgumentException('Background job arguments can\'t exceed 4000 etc')), |
|
144 | - null, |
|
145 | - null, |
|
146 | - null, |
|
147 | - ); |
|
148 | - |
|
149 | - /** @var IOutput $out */ |
|
150 | - $out = $this->createMock(IOutput::class); |
|
151 | - $this->job->run($out); |
|
152 | - } |
|
153 | - |
|
154 | - public function testDonNotRun(): void { |
|
155 | - $this->config->expects($this->once()) |
|
156 | - ->method('getAppValue') |
|
157 | - ->with('user_ldap', 'installed_version', '1.2.1') |
|
158 | - ->willReturn('1.2.1'); |
|
159 | - $this->userMapper->expects($this->never()) |
|
160 | - ->method('getList'); |
|
161 | - $this->groupMapper->expects($this->never()) |
|
162 | - ->method('getList'); |
|
163 | - $this->jobList->expects($this->never()) |
|
164 | - ->method('add'); |
|
165 | - |
|
166 | - /** @var IOutput $out */ |
|
167 | - $out = $this->createMock(IOutput::class); |
|
168 | - $this->job->run($out); |
|
169 | - } |
|
20 | + protected IConfig&MockObject $config; |
|
21 | + protected UserMapping&MockObject $userMapper; |
|
22 | + protected GroupMapping&MockObject $groupMapper; |
|
23 | + protected IJobList&MockObject $jobList; |
|
24 | + protected UUIDFixInsert $job; |
|
25 | + |
|
26 | + protected function setUp(): void { |
|
27 | + parent::setUp(); |
|
28 | + |
|
29 | + $this->jobList = $this->createMock(IJobList::class); |
|
30 | + $this->config = $this->createMock(IConfig::class); |
|
31 | + $this->userMapper = $this->createMock(UserMapping::class); |
|
32 | + $this->groupMapper = $this->createMock(GroupMapping::class); |
|
33 | + $this->job = new UUIDFixInsert( |
|
34 | + $this->config, |
|
35 | + $this->userMapper, |
|
36 | + $this->groupMapper, |
|
37 | + $this->jobList |
|
38 | + ); |
|
39 | + } |
|
40 | + |
|
41 | + public function testGetName(): void { |
|
42 | + $this->assertSame('Insert UUIDFix background job for user and group in batches', $this->job->getName()); |
|
43 | + } |
|
44 | + |
|
45 | + public static function recordProvider(): array { |
|
46 | + $record = [ |
|
47 | + 'dn' => 'cn=somerecord,dc=somewhere', |
|
48 | + 'name' => 'Something', |
|
49 | + 'uuid' => 'AB12-3456-CDEF7-8GH9' |
|
50 | + ]; |
|
51 | + |
|
52 | + $userBatches = [ |
|
53 | + 0 => array_fill(0, 50, $record), |
|
54 | + 1 => array_fill(0, 50, $record), |
|
55 | + 2 => array_fill(0, 13, $record), |
|
56 | + ]; |
|
57 | + |
|
58 | + $groupBatches = [ |
|
59 | + 0 => array_fill(0, 7, $record), |
|
60 | + ]; |
|
61 | + |
|
62 | + return [ |
|
63 | + ['userBatches' => $userBatches, 'groupBatches' => $groupBatches] |
|
64 | + ]; |
|
65 | + } |
|
66 | + |
|
67 | + public static function recordProviderTooLongAndNone(): array { |
|
68 | + $record = [ |
|
69 | + 'dn' => 'cn=somerecord,dc=somewhere', |
|
70 | + 'name' => 'Something', |
|
71 | + 'uuid' => 'AB12-3456-CDEF7-8GH9' |
|
72 | + ]; |
|
73 | + |
|
74 | + $userBatches = [ |
|
75 | + 0 => array_fill(0, 50, $record), |
|
76 | + 1 => array_fill(0, 40, $record), |
|
77 | + 2 => array_fill(0, 32, $record), |
|
78 | + 3 => array_fill(0, 32, $record), |
|
79 | + 4 => array_fill(0, 23, $record), |
|
80 | + ]; |
|
81 | + |
|
82 | + $groupBatches = [0 => []]; |
|
83 | + |
|
84 | + return [ |
|
85 | + ['userBatches' => $userBatches, 'groupBatches' => $groupBatches] |
|
86 | + ]; |
|
87 | + } |
|
88 | + |
|
89 | + #[\PHPUnit\Framework\Attributes\DataProvider('recordProvider')] |
|
90 | + public function testRun(array $userBatches, array $groupBatches): void { |
|
91 | + $this->config->expects($this->once()) |
|
92 | + ->method('getAppValue') |
|
93 | + ->with('user_ldap', 'installed_version', '1.2.1') |
|
94 | + ->willReturn('1.2.0'); |
|
95 | + |
|
96 | + $this->userMapper->expects($this->exactly(3)) |
|
97 | + ->method('getList') |
|
98 | + ->willReturnMap([ |
|
99 | + [0, 50, false, $userBatches[0]], |
|
100 | + [50, 50, false, $userBatches[1]], |
|
101 | + [100, 50, false, $userBatches[2]], |
|
102 | + ]); |
|
103 | + |
|
104 | + $this->groupMapper->expects($this->exactly(1)) |
|
105 | + ->method('getList') |
|
106 | + ->with(0, 50) |
|
107 | + ->willReturn($groupBatches[0]); |
|
108 | + |
|
109 | + $this->jobList->expects($this->exactly(4)) |
|
110 | + ->method('add'); |
|
111 | + |
|
112 | + /** @var IOutput $out */ |
|
113 | + $out = $this->createMock(IOutput::class); |
|
114 | + $this->job->run($out); |
|
115 | + } |
|
116 | + |
|
117 | + #[\PHPUnit\Framework\Attributes\DataProvider('recordProviderTooLongAndNone')] |
|
118 | + public function testRunWithManyAndNone(array $userBatches, array $groupBatches): void { |
|
119 | + $this->config->expects($this->once()) |
|
120 | + ->method('getAppValue') |
|
121 | + ->with('user_ldap', 'installed_version', '1.2.1') |
|
122 | + ->willReturn('1.2.0'); |
|
123 | + |
|
124 | + $this->userMapper->expects($this->exactly(5)) |
|
125 | + ->method('getList') |
|
126 | + ->willReturnMap([ |
|
127 | + [0, 50, false, $userBatches[0]], |
|
128 | + [0, 40, false, $userBatches[1]], |
|
129 | + [0, 32, false, $userBatches[2]], |
|
130 | + [32, 32, false, $userBatches[3]], |
|
131 | + [64, 32, false, $userBatches[4]], |
|
132 | + ]); |
|
133 | + |
|
134 | + $this->groupMapper->expects($this->once()) |
|
135 | + ->method('getList') |
|
136 | + ->with(0, 50) |
|
137 | + ->willReturn($groupBatches[0]); |
|
138 | + |
|
139 | + $this->jobList->expects($this->exactly(5)) |
|
140 | + ->method('add') |
|
141 | + ->willReturnOnConsecutiveCalls( |
|
142 | + $this->throwException(new \InvalidArgumentException('Background job arguments can\'t exceed 4000 etc')), |
|
143 | + $this->throwException(new \InvalidArgumentException('Background job arguments can\'t exceed 4000 etc')), |
|
144 | + null, |
|
145 | + null, |
|
146 | + null, |
|
147 | + ); |
|
148 | + |
|
149 | + /** @var IOutput $out */ |
|
150 | + $out = $this->createMock(IOutput::class); |
|
151 | + $this->job->run($out); |
|
152 | + } |
|
153 | + |
|
154 | + public function testDonNotRun(): void { |
|
155 | + $this->config->expects($this->once()) |
|
156 | + ->method('getAppValue') |
|
157 | + ->with('user_ldap', 'installed_version', '1.2.1') |
|
158 | + ->willReturn('1.2.1'); |
|
159 | + $this->userMapper->expects($this->never()) |
|
160 | + ->method('getList'); |
|
161 | + $this->groupMapper->expects($this->never()) |
|
162 | + ->method('getList'); |
|
163 | + $this->jobList->expects($this->never()) |
|
164 | + ->method('add'); |
|
165 | + |
|
166 | + /** @var IOutput $out */ |
|
167 | + $out = $this->createMock(IOutput::class); |
|
168 | + $this->job->run($out); |
|
169 | + } |
|
170 | 170 | } |
@@ -19,102 +19,102 @@ |
||
19 | 19 | use Test\TestCase; |
20 | 20 | |
21 | 21 | class CleanUpTest extends TestCase { |
22 | - protected CleanUp $bgJob; |
|
23 | - protected array $mocks; |
|
24 | - |
|
25 | - public function setUp(): void { |
|
26 | - parent::setUp(); |
|
27 | - $this->createMocks(); |
|
28 | - $this->bgJob = new CleanUp($this->mocks['timeFactory'], $this->mocks['userBackend'], $this->mocks['deletedUsersIndex']); |
|
29 | - $this->bgJob->setArguments($this->mocks); |
|
30 | - } |
|
31 | - |
|
32 | - protected function createMocks(): void { |
|
33 | - $this->mocks = []; |
|
34 | - $this->mocks['userBackend'] = $this->createMock(User_Proxy::class); |
|
35 | - $this->mocks['deletedUsersIndex'] = $this->createMock(DeletedUsersIndex::class); |
|
36 | - $this->mocks['ocConfig'] = $this->createMock(IConfig::class); |
|
37 | - $this->mocks['db'] = $this->createMock(IDBConnection::class); |
|
38 | - $this->mocks['helper'] = $this->createMock(Helper::class); |
|
39 | - $this->mocks['timeFactory'] = $this->createMock(ITimeFactory::class); |
|
40 | - } |
|
41 | - |
|
42 | - /** |
|
43 | - * clean up job must not run when there are disabled configurations |
|
44 | - */ |
|
45 | - public function test_runNotAllowedByDisabledConfigurations(): void { |
|
46 | - $this->mocks['helper']->expects($this->once()) |
|
47 | - ->method('haveDisabledConfigurations') |
|
48 | - ->willReturn(true); |
|
49 | - |
|
50 | - $this->mocks['ocConfig']->expects($this->never()) |
|
51 | - ->method('getSystemValue'); |
|
52 | - |
|
53 | - $result = $this->bgJob->isCleanUpAllowed(); |
|
54 | - $this->assertSame(false, $result); |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * clean up job must not run when LDAP Helper is broken i.e. |
|
59 | - * returning unexpected results |
|
60 | - */ |
|
61 | - public function test_runNotAllowedByBrokenHelper(): void { |
|
62 | - $this->mocks['helper']->expects($this->once()) |
|
63 | - ->method('haveDisabledConfigurations') |
|
64 | - ->willThrowException(new Exception()); |
|
65 | - |
|
66 | - $this->mocks['ocConfig']->expects($this->never()) |
|
67 | - ->method('getSystemValue'); |
|
68 | - |
|
69 | - $result = $this->bgJob->isCleanUpAllowed(); |
|
70 | - $this->assertSame(false, $result); |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * clean up job must not run when it is not enabled |
|
75 | - */ |
|
76 | - public function test_runNotAllowedBySysConfig(): void { |
|
77 | - $this->mocks['helper']->expects($this->once()) |
|
78 | - ->method('haveDisabledConfigurations') |
|
79 | - ->willReturn(false); |
|
80 | - |
|
81 | - $this->mocks['ocConfig']->expects($this->once()) |
|
82 | - ->method('getSystemValue') |
|
83 | - ->willReturn(false); |
|
84 | - |
|
85 | - $result = $this->bgJob->isCleanUpAllowed(); |
|
86 | - $this->assertSame(false, $result); |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * clean up job is allowed to run |
|
91 | - */ |
|
92 | - public function test_runIsAllowed(): void { |
|
93 | - $this->mocks['helper']->expects($this->once()) |
|
94 | - ->method('haveDisabledConfigurations') |
|
95 | - ->willReturn(false); |
|
96 | - |
|
97 | - $this->mocks['ocConfig']->expects($this->once()) |
|
98 | - ->method('getSystemValue') |
|
99 | - ->willReturn(true); |
|
100 | - |
|
101 | - $result = $this->bgJob->isCleanUpAllowed(); |
|
102 | - $this->assertSame(true, $result); |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * check whether offset will be reset when it needs to |
|
107 | - */ |
|
108 | - public function test_OffsetResetIsNecessary(): void { |
|
109 | - $result = $this->bgJob->isOffsetResetNecessary($this->bgJob->getChunkSize() - 1); |
|
110 | - $this->assertSame(true, $result); |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * make sure offset is not reset when it is not due |
|
115 | - */ |
|
116 | - public function test_OffsetResetIsNotNecessary(): void { |
|
117 | - $result = $this->bgJob->isOffsetResetNecessary($this->bgJob->getChunkSize()); |
|
118 | - $this->assertSame(false, $result); |
|
119 | - } |
|
22 | + protected CleanUp $bgJob; |
|
23 | + protected array $mocks; |
|
24 | + |
|
25 | + public function setUp(): void { |
|
26 | + parent::setUp(); |
|
27 | + $this->createMocks(); |
|
28 | + $this->bgJob = new CleanUp($this->mocks['timeFactory'], $this->mocks['userBackend'], $this->mocks['deletedUsersIndex']); |
|
29 | + $this->bgJob->setArguments($this->mocks); |
|
30 | + } |
|
31 | + |
|
32 | + protected function createMocks(): void { |
|
33 | + $this->mocks = []; |
|
34 | + $this->mocks['userBackend'] = $this->createMock(User_Proxy::class); |
|
35 | + $this->mocks['deletedUsersIndex'] = $this->createMock(DeletedUsersIndex::class); |
|
36 | + $this->mocks['ocConfig'] = $this->createMock(IConfig::class); |
|
37 | + $this->mocks['db'] = $this->createMock(IDBConnection::class); |
|
38 | + $this->mocks['helper'] = $this->createMock(Helper::class); |
|
39 | + $this->mocks['timeFactory'] = $this->createMock(ITimeFactory::class); |
|
40 | + } |
|
41 | + |
|
42 | + /** |
|
43 | + * clean up job must not run when there are disabled configurations |
|
44 | + */ |
|
45 | + public function test_runNotAllowedByDisabledConfigurations(): void { |
|
46 | + $this->mocks['helper']->expects($this->once()) |
|
47 | + ->method('haveDisabledConfigurations') |
|
48 | + ->willReturn(true); |
|
49 | + |
|
50 | + $this->mocks['ocConfig']->expects($this->never()) |
|
51 | + ->method('getSystemValue'); |
|
52 | + |
|
53 | + $result = $this->bgJob->isCleanUpAllowed(); |
|
54 | + $this->assertSame(false, $result); |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * clean up job must not run when LDAP Helper is broken i.e. |
|
59 | + * returning unexpected results |
|
60 | + */ |
|
61 | + public function test_runNotAllowedByBrokenHelper(): void { |
|
62 | + $this->mocks['helper']->expects($this->once()) |
|
63 | + ->method('haveDisabledConfigurations') |
|
64 | + ->willThrowException(new Exception()); |
|
65 | + |
|
66 | + $this->mocks['ocConfig']->expects($this->never()) |
|
67 | + ->method('getSystemValue'); |
|
68 | + |
|
69 | + $result = $this->bgJob->isCleanUpAllowed(); |
|
70 | + $this->assertSame(false, $result); |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * clean up job must not run when it is not enabled |
|
75 | + */ |
|
76 | + public function test_runNotAllowedBySysConfig(): void { |
|
77 | + $this->mocks['helper']->expects($this->once()) |
|
78 | + ->method('haveDisabledConfigurations') |
|
79 | + ->willReturn(false); |
|
80 | + |
|
81 | + $this->mocks['ocConfig']->expects($this->once()) |
|
82 | + ->method('getSystemValue') |
|
83 | + ->willReturn(false); |
|
84 | + |
|
85 | + $result = $this->bgJob->isCleanUpAllowed(); |
|
86 | + $this->assertSame(false, $result); |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * clean up job is allowed to run |
|
91 | + */ |
|
92 | + public function test_runIsAllowed(): void { |
|
93 | + $this->mocks['helper']->expects($this->once()) |
|
94 | + ->method('haveDisabledConfigurations') |
|
95 | + ->willReturn(false); |
|
96 | + |
|
97 | + $this->mocks['ocConfig']->expects($this->once()) |
|
98 | + ->method('getSystemValue') |
|
99 | + ->willReturn(true); |
|
100 | + |
|
101 | + $result = $this->bgJob->isCleanUpAllowed(); |
|
102 | + $this->assertSame(true, $result); |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * check whether offset will be reset when it needs to |
|
107 | + */ |
|
108 | + public function test_OffsetResetIsNecessary(): void { |
|
109 | + $result = $this->bgJob->isOffsetResetNecessary($this->bgJob->getChunkSize() - 1); |
|
110 | + $this->assertSame(true, $result); |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * make sure offset is not reset when it is not due |
|
115 | + */ |
|
116 | + public function test_OffsetResetIsNotNecessary(): void { |
|
117 | + $result = $this->bgJob->isOffsetResetNecessary($this->bgJob->getChunkSize()); |
|
118 | + $this->assertSame(false, $result); |
|
119 | + } |
|
120 | 120 | } |