BaseTest.php 4.52 KB
Newer Older
1
2
3
<?php
namespace UnicaenAuthTest\Provider\Identity;

4
use PHPUnit\Framework\TestCase;
5
6
7
8
9
10
11
use UnicaenAuth\Acl\NamedRole;

/**
 * Description of LdapTest
 *
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
12
abstract class BaseTest extends TestCase
13
14
15
16
17
{
    protected $providerClass;
    protected $provider;
    protected $authService;
    protected $serviceManager;
18

19
20
21
22
23
24
    /**
     * Sets up the fixture, for example, open a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp()
    {
25
26
        $this->serviceManager    = $this->getMock('Zend\ServiceManager\ServiceManager', ['get']);
        $this->authService       = $this->getMock('Zend\Authentication\AuthenticationService', ['getIdentity']);
27
28
        $this->provider          = new $this->providerClass($this->authService);
    }
29

30
31
    public function getInvalidDefaultRole()
    {
32
33
34
35
36
        return [
            [12],
            [new \stdClass()],
            [['value']],
        ];
37
    }
38

39
40
41
42
43
44
    public function testGettingIdentityRolesReturnsDefaultRoleWhenEmptyIdentityAvailable()
    {
        // fournisseur de l'identité de l'utilisateur connecté
        $this->authService->expects($this->once())
                          ->method('getIdentity')
                          ->will($this->returnValue(null));
45

46
        $roles = $this->provider->getIdentityRoles();
47
        $this->assertEquals([$this->provider->getDefaultRole()], $roles);
48
    }
49

50
51
52
53
54
55
    public function testGettingIdentityRolesReturnsAuthenticatedRoleWhenUnexpectedIdentityAvailable()
    {
        // fournisseur de l'identité de l'utilisateur connecté
        $this->authService->expects($this->once())
                          ->method('getIdentity')
                          ->will($this->returnValue(new \DateTime()));
56

57
        $roles = $this->provider->getIdentityRoles();
58
        $this->assertEquals([$this->provider->getAuthenticatedRole()], $roles);
59
    }
60

61
62
63
64
65
66
    /**
     * @depends testGettingIdentityRolesReturnsPeopleRoles
     */
    public function testGettingIdentityRolesTrigger()
    {
        $event    = new \UnicaenAuth\Provider\Identity\ChainEvent();
67
68
        $roles    = [new NamedRole('role id')];
        $provider = $this->getMock($this->providerClass, ['getIdentityRoles'], [$this->authService]);
69
70
71
72

        $provider->expects($this->once())
                ->method('getIdentityRoles')
                ->will($this->returnValue($roles));
73

74
        $provider->getIdentityRolesTrigger($event);
75

76
77
        $this->assertEquals($roles, $event->getRoles());
    }
78

79
80
81
82
83
84
//    public function testGettingIdentityRolesReturnsDefaultRoleWhenIdentityLdapGroupDoesNotExistInAcl()
//    {
//        // fournisseur de l'identité de l'utilisateur connecté
//        $this->authService->expects($this->once())
//                          ->method('getIdentity')
//                          ->will($this->returnValue($identity = new LdapPeopleEntity(LdapPeopleTestAsset::$data1)));
85
//
86
//        $this->assertNotEmpty($identity->getMemberOf(), "Pré-requis non respecté : memberOf vide.");
87
//
88
89
90
91
//        // fournisseur des ACL
//        $this->authorize->expects($this->any())
//                        ->method('getAcl')
//                        ->will($this->returnValue($acl = new \Zend\Permissions\Acl\Acl()));
92
//
93
94
//        $roles = $this->provider->getIdentityRoles();
//        $this->assertEquals(array($this->defaultRole), $roles);
95
//
96
97
98
99
//        // NB: le rôle par défaut n'a pas besoin d'être connu des ACL
//        $this->setExpectedException('Zend\Permissions\Acl\Exception\InvalidArgumentException');
//        $acl->getRole($this->defaultRole->getRoleId());
//    }
100
//
101
102
103
104
105
106
//    public function testGettingIdentityRolesReturnsIdentityLdapGroupsWhichExistInAcl()
//    {
//        // fournisseur de l'identité de l'utilisateur connecté
//        $this->authService->expects($this->once())
//                          ->method('getIdentity')
//                          ->will($this->returnValue($identity = new LdapPeopleEntity(LdapPeopleTestAsset::$data1)));
107
//
108
//        $this->assertNotEmpty($identity->getMemberOf(), "Pré-requis non respecté : memberOf vide.");
109
//
110
111
112
113
114
115
//        // fournisseur des ACL
//        $acl = new \Zend\Permissions\Acl\Acl();
//        $acl->addRole($role = new GenericRole('cn=admin_reseau,ou=groups,dc=unicaen,dc=fr'));
//        $this->authorize->expects($this->any())
//                        ->method('getAcl')
//                        ->will($this->returnValue($acl));
116
//
117
118
119
120
//        $roles = $this->provider->getIdentityRoles();
//        $this->assertEquals(array($role), $roles);
//    }
}