Completed
Push — master ( 430cef...2cb4fe )
by Sam
02:01
created

LDAPServiceTest::setUp()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 24
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\ActiveDirectory\Tests;
4
5
use SilverStripe\ActiveDirectory\Extensions\LDAPGroupExtension;
6
use SilverStripe\ActiveDirectory\Extensions\LDAPMemberExtension;
7
use SilverStripe\ActiveDirectory\Model\LDAPGateway;
8
use SilverStripe\ActiveDirectory\Services\LDAPService;
9
use SilverStripe\Dev\SapphireTest;
10
use SilverStripe\Core\Config\Config;
11
use SilverStripe\Core\Injector\Injector;
12
use SilverStripe\Security\Group;
13
use SilverStripe\Security\Member;
14
15
/**
16
 * @coversDefaultClass \SilverStripe\ActiveDirectory\Services\LDAPService
17
 * @package activedirectory
18
 */
19
class LDAPServiceTest extends SapphireTest
20
{
21
    /**
22
     * @var LDAPService
23
     */
24
    protected $service;
25
26
    /**
27
     * {@inheritDoc}
28
     * @var bool
29
     */
30
    protected $usesDatabase = true;
31
32
    /**
33
     * {@inheritDoc}
34
     */
35
    public function setUp()
36
    {
37
        parent::setUp();
38
39
        $gateway = new Model\LDAPFakeGateway();
40
        Injector::inst()->registerService($gateway, LDAPGateway::class);
41
42
        $service = Injector::inst()->create(LDAPService::class);
43
        $service->setGateway($gateway);
44
        $this->service = $service;
45
46
        Config::modify()->set(LDAPGateway::class, 'options', ['host' => '1.2.3.4']);
47
        Config::modify()->set(LDAPService::class, 'groups_search_locations', [
48
            'CN=Users,DC=playpen,DC=local',
49
            'CN=Others,DC=playpen,DC=local'
50
        ]);
51
        // Prevent other module extension hooks from executing during write() etc.
52
        Config::modify()->remove(Member::class, 'extensions');
53
        Config::modify()->remove(Group::class, 'extensions');
54
        Config::modify()->set(Member::class, 'update_ldap_from_local', false);
55
        Config::modify()->set(Member::class, 'create_users_in_ldap', false);
56
        Config::modify()->set(
57
            Group::class,
58
            'extensions',
59
            [LDAPGroupExtension::class]
60
        );
61
        Config::modify()->set(
62
            Member::class,
63
            'extensions',
64
            [LDAPMemberExtension::class]
65
        );
66
67
        // Disable Monolog logging to stderr by default if you don't give it a handler
68
        $this->service->getLogger()->pushHandler(new \Monolog\Handler\NullHandler);
69
    }
70
71
    public function testGroups()
72
    {
73
        $expected = [
74
            'CN=Group1,CN=Users,DC=playpen,DC=local' => ['dn' => 'CN=Group1,CN=Users,DC=playpen,DC=local'],
75
            'CN=Group2,CN=Users,DC=playpen,DC=local' => ['dn' => 'CN=Group2,CN=Users,DC=playpen,DC=local'],
76
            'CN=Group3,CN=Users,DC=playpen,DC=local' => ['dn' => 'CN=Group3,CN=Users,DC=playpen,DC=local'],
77
            'CN=Group4,CN=Users,DC=playpen,DC=local' => ['dn' => 'CN=Group4,CN=Users,DC=playpen,DC=local'],
78
            'CN=Group5,CN=Users,DC=playpen,DC=local' => ['dn' => 'CN=Group5,CN=Users,DC=playpen,DC=local'],
79
            'CN=Group6,CN=Others,DC=playpen,DC=local' => ['dn' => 'CN=Group6,CN=Others,DC=playpen,DC=local'],
80
            'CN=Group7,CN=Others,DC=playpen,DC=local' => ['dn' => 'CN=Group7,CN=Others,DC=playpen,DC=local'],
81
            'CN=Group8,CN=Others,DC=playpen,DC=local' => ['dn' => 'CN=Group8,CN=Others,DC=playpen,DC=local']
82
        ];
83
84
        $results = $this->service->getGroups();
85
86
        $this->assertEquals($expected, $results);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SilverStripe\Acti...\Tests\LDAPServiceTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
87
    }
88
89
    public function testUpdateMemberFromLDAP()
90
    {
91
        Config::modify()->set(
92
            Member::class,
93
            'ldap_field_mappings',
94
            [
95
                'givenname' => 'FirstName',
96
                'sn' => 'Surname',
97
                'mail' => 'Email',
98
            ]
99
        );
100
101
        $member = new Member();
102
        $member->GUID = '123';
103
104
        $this->service->updateMemberFromLDAP($member);
105
106
        $this->assertTrue($member->ID > 0, 'updateMemberFromLDAP writes the member');
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<SilverStripe\Acti...\Tests\LDAPServiceTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
107
        $this->assertEquals('123', $member->GUID, 'GUID remains the same');
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SilverStripe\Acti...\Tests\LDAPServiceTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
108
        $this->assertEquals('Joe', $member->FirstName, 'FirstName updated from LDAP');
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SilverStripe\Acti...\Tests\LDAPServiceTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
109
        $this->assertEquals('Bloggs', $member->Surname, 'Surname updated from LDAP');
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SilverStripe\Acti...\Tests\LDAPServiceTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
110
        $this->assertEquals('[email protected]', $member->Email, 'Email updated from LDAP');
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<SilverStripe\Acti...\Tests\LDAPServiceTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
111
    }
112
}
113