Completed
Push — master ( 232009...3c190c )
by Robbie
14s
created

LDAPServiceTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 6
dl 0
loc 82
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 0 28 1
A testGroups() 0 17 1
A testUpdateMemberFromLDAP() 0 23 1
1
<?php
2
3
namespace SilverStripe\ActiveDirectory\Tests\Services;
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\ActiveDirectory\Tests\FakeGatewayTest;
10
use SilverStripe\Core\Config\Config;
11
use SilverStripe\Security\Group;
12
use SilverStripe\Security\Member;
13
14
/**
15
 * @coversDefaultClass \SilverStripe\ActiveDirectory\Services\LDAPService
16
 * @package activedirectory
17
 */
18
class LDAPServiceTest extends FakeGatewayTest
19
{
20
    /**
21
     * {@inheritDoc}
22
     * @var bool
23
     */
24
    protected $usesDatabase = true;
25
26
    /**
27
     * {@inheritDoc}
28
     */
29
    public function setUp()
30
    {
31
        parent::setUp();
32
33
        Config::modify()->set(LDAPGateway::class, 'options', ['host' => '1.2.3.4']);
34
        Config::modify()->set(LDAPService::class, 'groups_search_locations', [
35
            'CN=Users,DC=playpen,DC=local',
36
            'CN=Others,DC=playpen,DC=local'
37
        ]);
38
        // Prevent other module extension hooks from executing during write() etc.
39
        Config::modify()->remove(Member::class, 'extensions');
40
        Config::modify()->remove(Group::class, 'extensions');
41
        Config::modify()->set(Member::class, 'update_ldap_from_local', false);
42
        Config::modify()->set(Member::class, 'create_users_in_ldap', false);
43
        Config::modify()->set(
44
            Group::class,
45
            'extensions',
46
            [LDAPGroupExtension::class]
47
        );
48
        Config::modify()->set(
49
            Member::class,
50
            'extensions',
51
            [LDAPMemberExtension::class]
52
        );
53
54
        // Disable Monolog logging to stderr by default if you don't give it a handler
55
        $this->service->getLogger()->pushHandler(new \Monolog\Handler\NullHandler);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Psr\Log\LoggerInterface as the method pushHandler() does only exist in the following implementations of said interface: Monolog\Logger.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
56
    }
57
58
    public function testGroups()
59
    {
60
        $expected = [
61
            'CN=Group1,CN=Users,DC=playpen,DC=local' => ['dn' => 'CN=Group1,CN=Users,DC=playpen,DC=local'],
62
            'CN=Group2,CN=Users,DC=playpen,DC=local' => ['dn' => 'CN=Group2,CN=Users,DC=playpen,DC=local'],
63
            'CN=Group3,CN=Users,DC=playpen,DC=local' => ['dn' => 'CN=Group3,CN=Users,DC=playpen,DC=local'],
64
            'CN=Group4,CN=Users,DC=playpen,DC=local' => ['dn' => 'CN=Group4,CN=Users,DC=playpen,DC=local'],
65
            'CN=Group5,CN=Users,DC=playpen,DC=local' => ['dn' => 'CN=Group5,CN=Users,DC=playpen,DC=local'],
66
            'CN=Group6,CN=Others,DC=playpen,DC=local' => ['dn' => 'CN=Group6,CN=Others,DC=playpen,DC=local'],
67
            'CN=Group7,CN=Others,DC=playpen,DC=local' => ['dn' => 'CN=Group7,CN=Others,DC=playpen,DC=local'],
68
            'CN=Group8,CN=Others,DC=playpen,DC=local' => ['dn' => 'CN=Group8,CN=Others,DC=playpen,DC=local']
69
        ];
70
71
        $results = $this->service->getGroups();
72
73
        $this->assertEquals($expected, $results);
74
    }
75
76
    public function testUpdateMemberFromLDAP()
77
    {
78
        Config::modify()->set(
79
            Member::class,
80
            'ldap_field_mappings',
81
            [
82
                'givenname' => 'FirstName',
83
                'sn' => 'Surname',
84
                'mail' => 'Email',
85
            ]
86
        );
87
88
        $member = new Member();
89
        $member->GUID = '123';
90
91
        $this->service->updateMemberFromLDAP($member);
92
93
        $this->assertTrue($member->ID > 0, 'updateMemberFromLDAP writes the member');
94
        $this->assertEquals('123', $member->GUID, 'GUID remains the same');
95
        $this->assertEquals('Joe', $member->FirstName, 'FirstName updated from LDAP');
96
        $this->assertEquals('Bloggs', $member->Surname, 'Surname updated from LDAP');
97
        $this->assertEquals('[email protected]', $member->Email, 'Email updated from LDAP');
98
    }
99
}
100