Skip to content

Commit efc84ba

Browse files
committed
Fix inconsistencies, increase test coverage, update README
1 parent 4bd287b commit efc84ba

12 files changed

Lines changed: 643 additions & 7 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
List of adapters:
77
* [GitHub](https://github.com/) (WIP)
88
* [Camunda](https://docs.camunda.org/manual/7.9/)
9-
* LDAP (Planned)
9+
* [LDAP](https://ldap.com/)
1010
* [MatterMost](https://mattermost.com/) (Planned)
1111

1212
## Installation

src/Services/Ldap/Client.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,22 @@ public function remove(string $dn): bool
121121

122122
public function generateDn(array $rdn = []): string
123123
{
124-
$dc = '';
124+
$dn = '';
125125

126126
foreach (array_merge($rdn, $this->target->getDomain()) as $key => $domainComponent) {
127127
$dnKey = is_string($key) ? $key : 'dc';
128128

129129
$domainComponent = is_array($domainComponent) ? $domainComponent : [$domainComponent];
130130

131131
foreach ($domainComponent as $domainComponentElement) {
132-
$dc .= sprintf('%s=%s,', $dnKey, $domainComponentElement);
132+
$dn .= sprintf('%s=%s,', $dnKey, $domainComponentElement);
133133
}
134134
}
135135

136-
return substr($dc, 0, -1);
136+
if (empty($dn)) {
137+
return $dn;
138+
}
139+
140+
return substr($dn, 0, -1);
137141
}
138142
}

src/SynchronizationAdapter/SetPassword/LdapSetPasswordAdapter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use LinkORB\OrgSync\Services\Ldap\LdapAssertionAwareTrait;
88
use LinkORB\OrgSync\SynchronizationAdapter\UserPush\LdapUserPushAdapter;
99

10-
final class LdapSetPasswordAdapter implements SetPasswordInterface
10+
class LdapSetPasswordAdapter implements SetPasswordInterface
1111
{
1212
use LdapAssertionAwareTrait;
1313

@@ -30,7 +30,7 @@ public function setPassword(User $user): SetPasswordInterface
3030
$userSearchFirstDn = $this->client->getDn(
3131
$this->client->first(
3232
$this->client->search(
33-
sprintf('(cn=%s)', $user->getUsername()),
33+
sprintf('(uid=%s)', $user->getUsername()),
3434
['ou' => LdapUserPushAdapter::USERS_ORG_UNIT]
3535
)
3636
)
@@ -46,7 +46,7 @@ public function setPassword(User $user): SetPasswordInterface
4646
return $this;
4747
}
4848

49-
private function encodePassword(string $password): string
49+
protected function encodePassword(string $password): string
5050
{
5151
$salt = substr(str_shuffle(
5252
str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', 4)

tests/Unit/DTO/UserTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,22 @@ public function getDtoClassName(): string
4242
{
4343
return User::class;
4444
}
45+
46+
public function testSetPassword()
47+
{
48+
$somePass = '0000000';
49+
50+
$dto = (new User('user1', 'differentPass'))->setPassword($somePass);
51+
52+
$this->assertSame($dto->getPassword(), $somePass);
53+
}
54+
55+
public function testSetPreviousPassword()
56+
{
57+
$somePass = '0000000';
58+
59+
$dto = (new User('user1', 'differentPass'))->setPreviousPassword($somePass);
60+
61+
$this->assertSame($dto->getProperties()[User::PREVIOUS_PASSWORD], $somePass);
62+
}
4563
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace LinkORB\OrgSync\Tests\Unit\Services\Ldap;
4+
5+
use LinkORB\OrgSync\DTO\Target\Ldap;
6+
use LinkORB\OrgSync\Services\Ldap\Client;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class ClientTest extends TestCase
10+
{
11+
/**
12+
* @dataProvider getGenerateDnData
13+
*/
14+
public function testGenerateDn(array $domain, array $rdn, string $expectedDn)
15+
{
16+
$ldap = new Ldap('', '', '', '', $domain);
17+
18+
$client = $this->createPartialMock(Client::class, ['__destruct']);
19+
$client->__construct($ldap);
20+
21+
$this->assertEquals($expectedDn, $client->generateDn($rdn));
22+
}
23+
24+
/**
25+
* @return array
26+
*/
27+
public function getGenerateDnData(): array
28+
{
29+
return [
30+
[
31+
['internal', 'com'],
32+
['cn' => ['test', 'somewhere'], 'ou' => 'org'],
33+
'cn=test,cn=somewhere,ou=org,dc=internal,dc=com'
34+
],
35+
[
36+
['com', 'internal'],
37+
['cn' => ['somewhere', 'test']],
38+
'cn=somewhere,cn=test,dc=com,dc=internal'
39+
],
40+
[
41+
[],
42+
['uid' => 111222,'ou' => 'org'],
43+
'uid=111222,ou=org'
44+
],
45+
[
46+
['abc', 'xyz'],
47+
[],
48+
'dc=abc,dc=xyz'
49+
],
50+
[
51+
[],
52+
[],
53+
''
54+
],
55+
];
56+
}
57+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace LinkORB\OrgSync\Tests\Unit\Services\Ldap;
4+
5+
use LinkORB\OrgSync\DTO\Group;
6+
use LinkORB\OrgSync\Services\Ldap\LdapParentHelper;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class LdapParentHelperTest extends TestCase
10+
{
11+
/** @var LdapParentHelper */
12+
private $parentHelper;
13+
14+
protected function setUp(): void
15+
{
16+
$this->parentHelper = new LdapParentHelper();
17+
}
18+
19+
/**
20+
* @dataProvider getParentData
21+
*/
22+
public function testGetParentGroups(Group $group, array $expectedParents, array $initialParents = [])
23+
{
24+
$this->assertEquals($expectedParents, $this->parentHelper->getParentGroups($initialParents, $group));
25+
}
26+
27+
public function testGetParentsCircularReference()
28+
{
29+
$this->expectExceptionMessage('Circular reference detected');
30+
$this->expectException(\UnexpectedValueException::class);
31+
32+
$group1 = new Group('test1', '');
33+
$group2 = new Group('test2', '');
34+
$group1->setParent($group2);
35+
$group2->setParent($group1);
36+
37+
$this->parentHelper->getParentGroups([], $group1);
38+
}
39+
40+
public function getParentData(): array
41+
{
42+
return [
43+
[
44+
new Group('hello', '', '', new Group('to', '', '', new Group('all', '', '', new Group('world', '')))),
45+
['to', 'all', 'world'],
46+
],
47+
[
48+
new Group('empty', ''),
49+
[],
50+
],
51+
[
52+
new Group('empty', ''),
53+
['initial', 'set'],
54+
['initial', 'set']
55+
],
56+
[
57+
new Group('Earth', '', '', new Group('Eurasia', '', '', new Group('Europe', '',))),
58+
['space', 'MilkyWay', 'Eurasia', 'Europe'],
59+
['space', 'MilkyWay']
60+
],
61+
[
62+
new Group('Earth', ''),
63+
[],
64+
[]
65+
],
66+
];
67+
}
68+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace LinkORB\OrgSync\Tests\Unit\Services\Ldap;
4+
5+
use LinkORB\OrgSync\DTO\User;
6+
use LinkORB\OrgSync\Services\Ldap\UserDataMapper;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class UserDataMapperTest extends TestCase
10+
{
11+
/** @var UserDataMapper */
12+
private $mapper;
13+
14+
protected function setUp(): void
15+
{
16+
$this->mapper = new UserDataMapper();
17+
}
18+
19+
/**
20+
* @dataProvider getMapData
21+
*/
22+
public function testMap(User $user, array $expectedUserData)
23+
{
24+
$this->assertEquals($expectedUserData, $this->mapper->map($user));
25+
}
26+
27+
public function getMapData(): array
28+
{
29+
return [
30+
[
31+
new User(''),
32+
[
33+
'cn' => '',
34+
'uid' => '',
35+
'sn' => '',
36+
'objectClass' => ['inetOrgPerson', 'organizationalPerson', 'person', 'top']
37+
],
38+
],
39+
[
40+
new User('uadmin', 'p@ss', 'admin@example.com', 'superadm', '1.jpg', ['lastName' => 'Jong']),
41+
[
42+
'cn' => 'uadmin',
43+
'uid' => 'uadmin',
44+
'sn' => 'Jong',
45+
'objectClass' => ['inetOrgPerson', 'organizationalPerson', 'person', 'top'],
46+
'mail' => 'admin@example.com',
47+
'displayName' => 'superadm',
48+
'Photo' => '1.jpg',
49+
],
50+
],
51+
[
52+
new User('uadmin', 'p@ss', '', 'superadm'),
53+
[
54+
'cn' => 'uadmin',
55+
'uid' => 'uadmin',
56+
'sn' => 'superadm',
57+
'objectClass' => ['inetOrgPerson', 'organizationalPerson', 'person', 'top'],
58+
'displayName' => 'superadm',
59+
],
60+
],
61+
];
62+
}
63+
}

tests/Unit/Services/SyncRemover/LdapSyncRemoverTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use LinkORB\OrgSync\SynchronizationAdapter\UserPush\LdapUserPushAdapter;
1313
use PHPUnit\Framework\MockObject\MockObject;
1414
use PHPUnit\Framework\TestCase;
15+
use UnexpectedValueException;
1516

1617
class LdapSyncRemoverTest extends TestCase
1718
{
@@ -124,6 +125,28 @@ public function testRemoveNonExistsGroups(array $orgGroups, array $existingGroup
124125
$this->assertNull($this->syncRemover->removeNonExists($organization));
125126
}
126127

128+
public function testRemoveException()
129+
{
130+
$this->client
131+
->expects($this->exactly(2))
132+
->method('search')
133+
->withConsecutive(
134+
[sprintf('(ou=%s)', LdapUserPushAdapter::USERS_ORG_UNIT)],
135+
[sprintf('(ou=%s)', LdapGroupPushAdapter::GROUPS_ORG_UNIT)]
136+
)
137+
->willReturnOnConsecutiveCalls(null, null);
138+
$this->client
139+
->expects($this->exactly(2))
140+
->method('count')
141+
->withConsecutive([null], [null])
142+
->willReturnOnConsecutiveCalls(null, null);
143+
144+
$this->expectException(UnexpectedValueException::class);
145+
$this->expectExceptionMessage('Error during search!');
146+
147+
$this->syncRemover->removeNonExists(new Organization('test'));
148+
}
149+
127150
public function getRemoveUsersData(): array
128151
{
129152
return [

tests/Unit/SynchronizationAdapter/AdapterFactory/LdapAdapterFactoryTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use LinkORB\OrgSync\Services\Ldap\Client;
88
use LinkORB\OrgSync\Services\SyncRemover\LdapSyncRemover;
99
use LinkORB\OrgSync\SynchronizationAdapter\AdapterFactory\LdapAdapterFactory;
10+
use LinkORB\OrgSync\SynchronizationAdapter\GroupPush\LdapGroupPushAdapter;
11+
use LinkORB\OrgSync\SynchronizationAdapter\SetPassword\LdapSetPasswordAdapter;
1012
use LinkORB\OrgSync\SynchronizationAdapter\UserPush\LdapUserPushAdapter;
1113
use PHPUnit\Framework\MockObject\MockObject;
1214
use PHPUnit\Framework\TestCase;
@@ -35,6 +37,16 @@ public function testCreateUserPushAdapter()
3537
$this->assertInstanceOf(LdapUserPushAdapter::class, $this->factory->createUserPushAdapter());
3638
}
3739

40+
public function testCreateGroupPushAdapter()
41+
{
42+
$this->assertInstanceOf(LdapGroupPushAdapter::class, $this->factory->createGroupPushAdapter());
43+
}
44+
45+
public function testCreateSetPasswordAdapter()
46+
{
47+
$this->assertInstanceOf(LdapSetPasswordAdapter::class, $this->factory->createSetPasswordAdapter());
48+
}
49+
3850
/**
3951
* @dataProvider getSupportsData
4052
*/

0 commit comments

Comments
 (0)