CasTest.php 4.95 KB
Newer Older
1
<?php
2

3
4
namespace UnicaenAuthTest\Authentication\Adapter;

5
6
use PHPUnit_Framework_TestCase;
use UnicaenApp\Exception\RuntimeException;
7
use UnicaenAuth\Authentication\Adapter\Cas;
8
9
use UnicaenAuth\Authentication\Adapter\phpCASWrapper;
use UnicaenAuth\Options\ModuleOptions;
10
11
use Zend\EventManager\EventManager;
use ZfcUser\Authentication\Adapter\AdapterChainEvent;
12
use Zend\Authentication\Result;
13
14
15
16
17
18

/**
 * Description of CasTest
 *
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
19
class CasTest extends PHPUnit_Framework_TestCase
20
{
21
22
23
    /**
     * @var Cas
     */
24
    protected $adapter;
25
26
27
28

    /**
     * @var ModuleOptions
     */
29
    protected $moduleOptions;
30

31
32
33
34
35
36
    /**
     * Sets up the fixture, for example, open a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp()
    {
37
        $this->moduleOptions = $moduleOptions = new ModuleOptions([
38
39
40
41
            'cas' => [
                'connection' => [
                    'default' => [
                        'params' => [
42
43
44
45
46
                            'hostname' => 'cas.unicaen.fr',
                            'port' => 443,
                            'version' => "2.0",
                            'uri' => "",
                            'debug' => false,
47
48
49
50
51
52
                        ],
                    ],
                ],
            ],
        ]);

53
        $serviceManager = $this->createMock('Zend\ServiceManager\ServiceManager'/*, ['get']*/);
54
55
56
57
58
59
60
61
62
63
        $serviceManager->expects($this->any())
                       ->method('get')
                       ->will($this->returnCallback(function($serviceName) use ($moduleOptions) {
                           if ('zfcuser_module_options' === $serviceName) {
                               return new \ZfcUser\Options\ModuleOptions();
                           }
                           if ('unicaen-auth_module_options' === $serviceName) {
                               return $moduleOptions;
                           }
                           if ('router' === $serviceName) {
64
                               $router = new \Zend\Mvc\Router\Http\TreeRouteStack();
65
66
67
68
69
                               $router->setBaseUrl('/appli')->setRequestUri(new \Zend\Uri\Http('/request'));
                               return $router;
                           }
                           return null;
                       }));
70

71
72
73
74
        $this->adapter = new Cas();
        $this->adapter->setServiceManager($serviceManager)
                      ->setEventManager(new EventManager());
    }
75

76
77
    public function getInvalidCasOptions()
    {
78
79
80
81
82
83
        return [
            [['other' => []]],
            [['connection' => []]],
            [['connection' => ['default'=> []]]],
            [['connection' => ['default'=> ['params' => []]]]],
        ];
84
    }
85

86
87
    /**
     * @dataProvider getInvalidCasOptions
88
     * @expectedException RuntimeException
89
90
91
92
93
94
     */
    public function testThrowsExceptionIfNoCasParamSpecified($config)
    {
        $this->moduleOptions->setCas($config);
        $this->adapter->authenticate(new AdapterChainEvent());
    }
95

96
97
    public function testAuthenticateReturnsNullIfNoCasConfigSpecified()
    {
98
        $this->moduleOptions->setCas([]);
99
100
101
        $result = $this->adapter->authenticate(new AdapterChainEvent());
        $this->assertNull($result);
    }
102

103
    public function testAuthenticatePopulatesEventWhenAuthenticationSucceeds()
104
    {
105
106
107
        /** @var phpCASWrapper|\PHPUnit_Framework_MockObject_MockObject $casClient */
        $casClient = $this->createMock(phpCASWrapper::class);
        $casClient->expects($this->once())
108
109
                  ->method('getUser')
                  ->will($this->returnValue($username = 'username'));
110

111
        $this->adapter->setCasClient($casClient);
112

113
        $event = new AdapterChainEvent();
114

115
        $this->adapter->authenticate($event);
116

117
        $this->assertTrue($this->adapter->isSatisfied());
118
119
        $this->assertEquals(['is_satisfied' => true, 'identity' => $username], $this->adapter->getStorage()->read());

120
        $this->assertEquals("userAuthenticated", $event->getName());
121
122
        $this->assertEquals(Result::SUCCESS, $event->getCode());
        $this->assertEquals($username, $event->getIdentity());
123
    }
124

125
126
    public function testLogoutReturnsNullIfNoCasConfigSpecified()
    {
127
        $this->moduleOptions->setCas([]);
128
129
130
        $result = $this->adapter->logout(new AdapterChainEvent());
        $this->assertNull($result);
    }
131

132
133
    public function testCanLogoutFromCasWithRedirectService()
    {
134
135
136
        /** @var phpCASWrapper|\PHPUnit_Framework_MockObject_MockObject $casClient */
        $casClient = $this->createMock(phpCASWrapper::class);
        $casClient->expects($this->once())
137
138
                  ->method('isAuthenticated')
                  ->will($this->returnValue(true));
139
        $casClient->expects($this->once())
140
                  ->method('logoutWithRedirectService');
141

142
        $this->adapter->setCasClient($casClient);
143

144
145
146
        $this->adapter->logout(new AdapterChainEvent());
    }
}