ConfigTest.php 3.91 KB
Newer Older
1
2
3
<?php
namespace UnicaenAuthTest\Provider\Role;

4
use PHPUnit\Framework\TestCase;
5
use UnicaenAuth\Provider\Role\Config;
6
7
use UnicaenAuth\Acl\NamedRole;
use UnicaenApp\Entity\Ldap\Group;
8
9
10
11
12
13

/**
 * Description of ConfigTest
 *
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
14
class ConfigTest extends TestCase
15
{
16
    protected $mapper;
17

18
19
    protected function setUp()
    {
20
        $this->mapper = $this->getMock('UnicaenApp\Mapper\Ldap\Group', ['findOneByDn']);
21
    }
22

23
    public function testLoadingRolesWithoutLdapDnCreatesNamedRoles()
24
    {
25
26
        $options = [
            'user' => [
27
                'name' => "Profil standard",
28
                'children' => [
29
                    'Gestionnaire', // pas de nom pour celui-là
30
                    'admin' => [
31
                        'name' => "Administrateur",
32
33
34
35
                    ],
                ],
            ],
        ];
36
37
38
39

        $this->mapper
                ->expects($this->never())
                ->method('findOneByDn');
40

41
        $config = new Config($this->mapper, $options);
42

43
        $roles = $config->getRoles();
44
45

        $expected = [
46
47
48
            $guest = new NamedRole('user', null, "Profil standard"),
            new NamedRole('Gestionnaire', $guest, "Gestionnaire"), // le nom attribué par défaut est l'id
            new NamedRole('admin', $guest, "Administrateur"),
49
        ];
50
51
        $this->assertEquals($expected, $roles);
    }
52

53
54
    public function testLoadingRolesWithExistingLdapDnCreatesNamedRoles()
    {
55
56
        $options = [
            'user' => [
57
                'name' => "Profil standard",
58
                'children' => [
59
                    'cn=support_info,ou=groups,dc=unicaen,dc=fr', // pas de nom pour celui-là
60
                    'cn=dsi-infra,ou=groups,dc=unicaen,dc=fr' => [
61
                        'name' => "DSI Infrastructure",
62
63
64
65
66
67
68
69
                    ],
                ],
            ],
        ];

        $map = [
            [$dn = 'cn=support_info,ou=groups,dc=unicaen,dc=fr', new Group(['dn' => $dn, 'description' => "DSI Support"])],
        ];
70
71
72
73
        $this->mapper
                ->expects($this->exactly(1))
                ->method('findOneByDn')
                ->will($this->returnValueMap($map));
74

75
        $config = new Config($this->mapper, $options);
76

77
        $roles = $config->getRoles();
78
79

        $expected = [
80
            $guest = new NamedRole(
81
82
                    'user',
                    null,
83
                    "Profil standard"),
84
            new NamedRole(
85
86
                    'cn=support_info,ou=groups,dc=unicaen,dc=fr',
                    $guest,
87
88
                    "DSI Support"), // le nom attribué est la description LDAP
            new NamedRole(
89
90
                    'cn=dsi-infra,ou=groups,dc=unicaen,dc=fr',
                    $guest,
91
                    "DSI Infrastructure"),
92
        ];
93
94
        $this->assertEquals($expected, $roles);
    }
95

96
97
    public function testLoadingRolesWithUnexistingLdapDnCreatesNamedRoles()
    {
98
99
        $options = [
            'user' => [
100
                'name' => "Profil standard",
101
                'children' => [
102
                    'cn=unknown,ou=groups,dc=unicaen,dc=fr', // groupe introuvable
103
104
105
106
107
108
109
                ],
            ],
        ];

        $map = [
            ['cn=unknown,ou=groups,dc=unicaen,dc=fr', null],
        ];
110
111
112
113
        $this->mapper
                ->expects($this->exactly(1))
                ->method('findOneByDn')
                ->will($this->returnValueMap($map));
114

115
        $config = new Config($this->mapper, $options);
116

117
        $roles = $config->getRoles();
118
119

        $expected = [
120
            $guest = new NamedRole(
121
122
                    'user',
                    null,
123
124
                    "Profil standard"),
            new NamedRole(
125
126
                    'cn=unknown,ou=groups,dc=unicaen,dc=fr',
                    $guest,
127
                    'cn=unknown,ou=groups,dc=unicaen,dc=fr'), // le nom attribué est l'id
128
        ];
129
130
        $this->assertEquals($expected, $roles);
    }
131
}