Skip to content

Commit 750ddd8

Browse files
committed
CARD-2527: Increased test coverage, minor improvements, updated README
1 parent 88b415c commit 750ddd8

14 files changed

Lines changed: 786 additions & 16 deletions

File tree

README.md

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

1212
## Installation
1313

src/DTO/User.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
class User
66
{
77
public const PREVIOUS_PASSWORD = 'previousPassword';
8+
public const FIRST_NAME = 'firstName';
9+
public const LAST_NAME = 'lastName';
810

911
/**
1012
* @var string

src/Services/Camunda/CamundaUserMapper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ public function map(array $data): User
1515
null,
1616
null,
1717
[
18-
'firstName' => $data['firstName'] ?? null,
19-
'lastName' => $data['lastName'] ?? null,
18+
User::FIRST_NAME => $data['firstName'] ?? null,
19+
User::LAST_NAME => $data['lastName'] ?? null,
2020
]
2121
);
2222
}

src/Services/Ldap/UserDataMapper.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ public function map(User $user): array
1111
$userInfo = [
1212
'cn' => $user->getUsername(),
1313
'uid' => $user->getUsername(),
14-
'sn' => $user->getProperties()['lastName'] ?? array_pop(explode(' ', (string) $user->getDisplayName())),
14+
'sn' => $user->getProperties()[User::LAST_NAME]
15+
?? array_pop(explode(' ', (string) $user->getDisplayName())),
1516
];
1617

1718
if ($user->getEmail()) {

src/Services/SyncRemover/MattermostSyncRemover.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,4 @@ public function removeNonExists(Organization $organization): void
6868
}
6969
}
7070
}
71-
72-
7371
}

src/SynchronizationAdapter/GroupPush/MattermostGroupPushAdapter.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public function pushGroup(Group $group): GroupPushInterface
3232
throw new BadMethodCallException('Unable to perform action. Try hard remove teams by CLI. See https://docs.mattermost.com/administration/command-line-tools.html');
3333
}
3434

35+
if (empty($group->getMembers())) {
36+
return $this;
37+
}
38+
3539
$teamId = $this->getTeamId($response);
3640
$membersIds = [];
3741
foreach ($group->getMembers() as $member) {
@@ -93,7 +97,7 @@ private function getTeamId(ResponseInterface $response): string
9397
{
9498
$teamData = json_decode((string) $response->getBody(), true);
9599

96-
return $teamData['id'] ?? '';
100+
return $teamData['id'];
97101
}
98102

99103
private function getMemberId(User $user): string
@@ -103,6 +107,6 @@ private function getMemberId(User $user): string
103107
true
104108
);
105109

106-
return $response['id'] ?? '';
110+
return $response['id'];
107111
}
108112
}

src/SynchronizationAdapter/UserPush/CamundaUserPushAdapter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ protected function create(User $user): void
5050
RequestOptions::JSON => [
5151
'profile' => [
5252
'id' => $user->getUsername(),
53-
'firstName' => $user->getProperties()['firstName'] ?? null,
54-
'lastName' => $user->getProperties()['lastName'] ?? null,
53+
'firstName' => $user->getProperties()[User::FIRST_NAME] ?? null,
54+
'lastName' => $user->getProperties()[User::LAST_NAME] ?? null,
5555
'email' => $user->getEmail(),
5656
],
5757
'credentials' => [
@@ -71,8 +71,8 @@ protected function update(User $user): void
7171
[
7272
RequestOptions::JSON => [
7373
'id' => $user->getUsername(),
74-
'firstName' => $user->getProperties()['firstName'] ?? null,
75-
'lastName' => $user->getProperties()['lastName'] ?? null,
74+
'firstName' => $user->getProperties()[User::FIRST_NAME] ?? null,
75+
'lastName' => $user->getProperties()[User::LAST_NAME] ?? null,
7676
'email' => $user->getEmail(),
7777
],
7878
]

src/SynchronizationAdapter/UserPush/MattermostUserPushAdapter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ public function pushUser(User $user): UserPushInterface
3636
$responseBody = json_decode((string) $response->getBody(), true);
3737

3838
if ($responseBody['delete_at'] !== 0) {
39-
$this->driver->getUserModel()->updateUserActive($responseBody['id'] ?? '', ['active' => true]);
39+
$this->driver->getUserModel()->updateUserActive($responseBody['id'], ['active' => true]);
4040
}
4141

42-
$response = $this->driver->getUserModel()->patchUser($responseBody['id'] ?? '', $requestOptions);
42+
$response = $this->driver->getUserModel()->patchUser($responseBody['id'], $requestOptions);
4343
} else {
4444
$requestOptions['password'] = $this->passwordHelper->getDefaultPassword($user->getUsername());
4545

@@ -56,8 +56,8 @@ private function getRequestOptions(User $user): array
5656
return [
5757
'email' => $user->getEmail(),
5858
'username' => $user->getUsername(),
59-
'first_name' => $user->getProperties()['firstName'] ?? null,
60-
'last_name' => $user->getProperties()['lastName'] ?? null,
59+
'first_name' => $user->getProperties()[User::FIRST_NAME] ?? null,
60+
'last_name' => $user->getProperties()[User::LAST_NAME] ?? null,
6161
'nickname' => $user->getDisplayName(),
6262
];
6363
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace LinkORB\OrgSync\Tests\Integrational\Ldap;
4+
5+
use LinkORB\OrgSync\DTO\Group;
6+
use LinkORB\OrgSync\DTO\Organization;
7+
use LinkORB\OrgSync\DTO\Target\Ldap;
8+
use LinkORB\OrgSync\DTO\User;
9+
use LinkORB\OrgSync\SynchronizationAdapter\AdapterFactory\LdapAdapterFactory;
10+
use LinkORB\OrgSync\SynchronizationAdapter\GroupPush\LdapGroupPushAdapter;
11+
use LinkORB\OrgSync\SynchronizationAdapter\SetPassword\LdapSetPasswordAdapter;
12+
use LinkORB\OrgSync\SynchronizationAdapter\UserPush\LdapUserPushAdapter;
13+
use PHPUnit\Framework\TestCase;
14+
15+
class LdapTest extends TestCase
16+
{
17+
/** @var LdapAdapterFactory */
18+
private $factory;
19+
20+
protected function setUp(): void
21+
{
22+
$this->factory = (new LdapAdapterFactory())
23+
->setTarget(new Ldap(
24+
'ldap://ldap:10389',
25+
'test',
26+
'uid=admin,ou=system',
27+
'secret',
28+
['internal', 'com']
29+
));
30+
}
31+
32+
/**
33+
* @depends testSyncRemove
34+
*/
35+
public function testPushUser()
36+
{
37+
list($user1, $user2, $user3) = $this->getUsers();
38+
39+
$this->assertInstanceOf(
40+
LdapUserPushAdapter::class,
41+
$this->factory->createUserPushAdapter()
42+
->pushUser($user1)
43+
->pushUser($user2)
44+
->pushUser($user3)
45+
);
46+
}
47+
48+
/**
49+
* @depends testSyncRemove
50+
*/
51+
public function testPushGroup()
52+
{
53+
list($group1, $group2, $group3) = $this->getOrgGroups();
54+
55+
$this->assertInstanceOf(
56+
LdapGroupPushAdapter::class,
57+
$this->factory->createGroupPushAdapter()
58+
->pushGroup($group1)
59+
->pushGroup($group2)
60+
->pushGroup($group3)
61+
);
62+
}
63+
64+
/**
65+
* @depends testSyncRemove
66+
*/
67+
public function testSetPassword()
68+
{
69+
list($user1) = $this->getUsers();
70+
71+
$user1->setPassword('p@ssword');
72+
73+
$this->assertInstanceOf(
74+
LdapSetPasswordAdapter::class,
75+
$this->factory->createSetPasswordAdapter()->setPassword($user1)
76+
);
77+
}
78+
79+
public function testSyncRemove()
80+
{
81+
$org = new Organization(
82+
'test',
83+
$this->getUsers(),
84+
$this->getOrgGroups()
85+
);
86+
87+
$this->assertNull(
88+
$this->factory->createSyncRemover()->removeNonExists($org)
89+
);
90+
}
91+
92+
/**
93+
* @return User[]
94+
*/
95+
private function getUsers(): array
96+
{
97+
return [
98+
new User('test1', null, 'a@a.com', null, null, ['lastName' => 'Morrison']),
99+
new User('test22', null, 'bb@a.com', null, null, ['lastName' => 'Hank']),
100+
new User('test223', null, 'cc@a.com', null, null, ['lastName' => 'Trent']),
101+
];
102+
}
103+
104+
/**
105+
* @return Group[]
106+
*/
107+
private function getOrgGroups(): array
108+
{
109+
$users = $this->getUsers();
110+
$baseGroup = new Group('test1', 'tempName', null, null, $users);
111+
112+
return [
113+
$baseGroup,
114+
new Group('test123', 'tempName22', null, null, [$users[0], $users[1]]),
115+
new Group('qwerty', 'easy name', null, $baseGroup, [$users[2]]),
116+
];
117+
}
118+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace LinkORB\OrgSync\Tests\Integrational\Mattermost;
4+
5+
use LinkORB\OrgSync\DTO\Group;
6+
use LinkORB\OrgSync\DTO\Organization;
7+
use LinkORB\OrgSync\DTO\Target\Mattermost;
8+
use LinkORB\OrgSync\DTO\User;
9+
use LinkORB\OrgSync\SynchronizationAdapter\AdapterFactory\MattermostAdapterFactory;
10+
use LinkORB\OrgSync\SynchronizationAdapter\GroupPush\MattermostGroupPushAdapter;
11+
use LinkORB\OrgSync\SynchronizationAdapter\SetPassword\MattermostSetPasswordAdapter;
12+
use LinkORB\OrgSync\SynchronizationAdapter\UserPush\MattermostUserPushAdapter;
13+
use PHPUnit\Framework\TestCase;
14+
15+
class MattermostTest extends TestCase
16+
{
17+
/** @var MattermostAdapterFactory */
18+
private $factory;
19+
20+
protected function setUp(): void
21+
{
22+
$this->factory = (new MattermostAdapterFactory('teeest'))
23+
->setTarget(new Mattermost(
24+
'http://mattermost-preview:8065',
25+
'test',
26+
null,
27+
'proskuryakov.r@gmail.com',
28+
'123Qwe'
29+
));
30+
}
31+
32+
public function testSyncRemove()
33+
{
34+
$org = new Organization(
35+
'test',
36+
$this->getUsers(),
37+
$this->getOrgGroups()
38+
);
39+
40+
$this->assertNull(
41+
$this->factory->createSyncRemover()->removeNonExists($org)
42+
);
43+
}
44+
45+
/**
46+
* @depends testSyncRemove
47+
*/
48+
public function testPushUser()
49+
{
50+
list($user1, $user2, $user3, $user4) = $this->getUsers();
51+
52+
$this->assertInstanceOf(
53+
MattermostUserPushAdapter::class,
54+
$this->factory->createUserPushAdapter()
55+
->pushUser($user1)
56+
->pushUser($user2)
57+
->pushUser($user3)
58+
->pushUser($user4)
59+
);
60+
}
61+
62+
/**
63+
* @depends testSyncRemove
64+
*/
65+
public function testSetPassword()
66+
{
67+
list($user1, $user2, $user3, $user4) = $this->getUsers();
68+
69+
$this->assertInstanceOf(
70+
MattermostSetPasswordAdapter::class,
71+
$this->factory->createSetPasswordAdapter()
72+
->setPassword($user1)
73+
->setPassword($user2)
74+
->setPassword($user3)
75+
->setPassword($user4)
76+
);
77+
}
78+
79+
/**
80+
* @depends testSyncRemove
81+
*/
82+
public function testPushGroup()
83+
{
84+
list($group1, $group2, $group3) = $this->getOrgGroups();
85+
86+
$this->assertInstanceOf(
87+
MattermostGroupPushAdapter::class,
88+
$this->factory->createGroupPushAdapter()
89+
->pushGroup($group1)
90+
->pushGroup($group2)
91+
->pushGroup($group3)
92+
);
93+
}
94+
95+
/**
96+
* @return User[]
97+
*/
98+
private function getUsers(): array
99+
{
100+
return [
101+
new User('rost1994', '123Qwe', 'proskuryakov.r@gmail.com', null, null, ['previousPassword' => '123Qwe']),
102+
new User('test1', 'pass1', 'a@a.com', 'qwerty', null, ['lastName' => 'Morrison']),
103+
new User('test22', '00000', 'bb@a.com', 'star', null, ['lastName' => 'Hankey']),
104+
new User('test223', '11111', 'cc@a.com', null, null, ['lastName' => 'Trent']),
105+
];
106+
}
107+
108+
/**
109+
* @return Group[]
110+
*/
111+
private function getOrgGroups(): array
112+
{
113+
$users = $this->getUsers();
114+
$baseGroup = new Group('test111', 'tempName', null, null, $users);
115+
116+
return [
117+
$baseGroup,
118+
new Group('test1234', 'tempName22', null, null, [$users[0], $users[1]]),
119+
new Group('qwerty', 'easy name', null, $baseGroup, [$users[3]]),
120+
];
121+
}
122+
}

0 commit comments

Comments
 (0)