Completed
Push — master ( 5670a0...79a44e )
by Stig
11s
created

LDAPServiceTest::setUp()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 0 Features 0
Metric Value
c 7
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 1
eloc 18
nc 1
nop 0
1
<?php
2
class LDAPServiceTest extends SapphireTest
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
3
{
4
5
    /**
6
     * @var LDAPService
7
     */
8
    protected $service;
9
10
    protected $usesDatabase = true;
11
12
    public function setUp()
13
    {
14
        parent::setUp();
15
16
        $gateway = new LDAPFakeGateway();
17
        Injector::inst()->registerService($gateway, 'LDAPGateway');
0 ignored issues
show
Documentation introduced by
$gateway is of type object<LDAPFakeGateway>, but the function expects a object<stdClass>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
18
19
        $service = Injector::inst()->create('LDAPService');
20
        $service->setGateway($gateway);
21
        $this->service = $service;
22
23
        Config::inst()->nest();
24
        Config::inst()->update('LDAPGateway', 'options', array('host' => '1.2.3.4'));
25
        Config::inst()->update('LDAPService', 'groups_search_locations', array(
26
            'CN=Users,DC=playpen,DC=local',
27
            'CN=Others,DC=playpen,DC=local'
28
        ));
29
        // Prevent other module extension hooks from executing during write() etc.
30
        Config::inst()->remove('Member', 'extensions');
31
        Config::inst()->remove('Group', 'extensions');
32
        Config::inst()->update('Member', 'update_ldap_from_local', false);
33
        Config::inst()->update('Member', 'create_users_in_ldap', false);
34
        Config::inst()->update('Group', 'extensions', array('LDAPGroupExtension'));
35
        Config::inst()->update('Member', 'extensions', array('LDAPMemberExtension'));
36
    }
37
38
    public function tearDown()
39
    {
40
        parent::tearDown();
41
42
        Config::inst()->unnest();
43
    }
44
45
    public function testGroups()
46
    {
47
        $expected = array(
48
            'CN=Group1,CN=Users,DC=playpen,DC=local' => array('dn' => 'CN=Group1,CN=Users,DC=playpen,DC=local'),
49
            'CN=Group2,CN=Users,DC=playpen,DC=local' => array('dn' => 'CN=Group2,CN=Users,DC=playpen,DC=local'),
50
            'CN=Group3,CN=Users,DC=playpen,DC=local' => array('dn' => 'CN=Group3,CN=Users,DC=playpen,DC=local'),
51
            'CN=Group4,CN=Users,DC=playpen,DC=local' => array('dn' => 'CN=Group4,CN=Users,DC=playpen,DC=local'),
52
            'CN=Group5,CN=Users,DC=playpen,DC=local' => array('dn' => 'CN=Group5,CN=Users,DC=playpen,DC=local'),
53
            'CN=Group6,CN=Others,DC=playpen,DC=local' => array('dn' => 'CN=Group6,CN=Others,DC=playpen,DC=local'),
54
            'CN=Group7,CN=Others,DC=playpen,DC=local' => array('dn' => 'CN=Group7,CN=Others,DC=playpen,DC=local'),
55
            'CN=Group8,CN=Others,DC=playpen,DC=local' => array('dn' => 'CN=Group8,CN=Others,DC=playpen,DC=local')
56
        );
57
58
        $results = $this->service->getGroups();
59
60
        $this->assertEquals($expected, $results);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<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...
61
    }
62
63
    public function testUpdateMemberFromLDAP()
64
    {
65
        Config::inst()->update('Member', 'ldap_field_mappings', array(
66
            'givenname' => 'FirstName',
67
            'sn' => 'Surname',
68
            'mail' => 'Email',
69
        ));
70
71
        $member = new Member();
72
        $member->GUID = '123';
73
74
        $this->service->updateMemberFromLDAP($member);
75
76
        $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<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...
77
        $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<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...
78
        $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<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...
79
        $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<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...
80
        $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<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...
81
    }
82
}
83