CasTest.php 5.67 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
use UnicaenAuth\Options\ModuleOptions;
9
use Zend\EventManager\EventManager;
10
11
use Zend\Router\Http\TreeRouteStack;
use Zend\Uri\Uri;
12
use ZfcUser\Authentication\Adapter\AdapterChainEvent;
13
use Zend\Authentication\Result;
14
15
16
17
18
19

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

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

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

54
        $serviceManager = $this->createMock('Zend\ServiceManager\ServiceManager'/*, ['get']*/);
55
56
57
58
59
60
61
62
63
64
        $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) {
65
                               $router = new \Zend\Mvc\Router\Http\TreeRouteStack();
66
67
68
69
70
                               $router->setBaseUrl('/appli')->setRequestUri(new \Zend\Uri\Http('/request'));
                               return $router;
                           }
                           return null;
                       }));
71

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

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

87
88
    /**
     * @dataProvider getInvalidCasOptions
89
90
     * @expectedException \Exception
     * @param array $config
91
     */
92
    public function testThrowsExceptionIfNoCasParamSpecified(array $config)
93
94
    {
        $this->moduleOptions->setCas($config);
95
        $this->adapter->setOptions($this->moduleOptions);
96
97
        $this->adapter->authenticate(new AdapterChainEvent());
    }
98

99
100
    public function testAuthenticateReturnsNullIfNoCasConfigSpecified()
    {
101
        $this->moduleOptions->setCas([]);
102
        $this->adapter->setOptions($this->moduleOptions);
103
104
105
        $result = $this->adapter->authenticate(new AdapterChainEvent());
        $this->assertNull($result);
    }
106

107
    public function testAuthenticatePopulatesEventWhenAuthenticationSucceeds()
108
    {
109
110
        /** @var \phpCAS|\PHPUnit_Framework_MockObject_MockObject $casClient */
        $casClient = $this->createMock(\phpCAS::class);
111
        $casClient->expects($this->once())
112
113
                  ->method('getUser')
                  ->will($this->returnValue($username = 'username'));
114

115
        $this->adapter->setCasClient($casClient);
116

117
        $event = new AdapterChainEvent();
118

119
        $this->adapter->setOptions($this->moduleOptions);
120
        $this->adapter->authenticate($event);
121

122
        $this->assertTrue($this->adapter->isSatisfied());
123
124
        $this->assertEquals(['is_satisfied' => true, 'identity' => $username], $this->adapter->getStorage()->read());

125
        $this->assertEquals("userAuthenticated", $event->getName());
126
127
        $this->assertEquals(Result::SUCCESS, $event->getCode());
        $this->assertEquals($username, $event->getIdentity());
128
    }
129

130
131
    public function testLogoutReturnsNullIfNoCasConfigSpecified()
    {
132
        $this->moduleOptions->setCas([]);
133
        $this->adapter->setOptions($this->moduleOptions);
134
135
136
        $result = $this->adapter->logout(new AdapterChainEvent());
        $this->assertNull($result);
    }
137

138
139
    public function testCanLogoutFromCasWithRedirectService()
    {
140
141
        /** @var \phpCAS|\PHPUnit_Framework_MockObject_MockObject $casClient */
        $casClient = $this->createMock(\phpCAS::class);
142
        $casClient->expects($this->once())
143
144
                  ->method('isAuthenticated')
                  ->will($this->returnValue(true));
145
        $casClient->expects($this->once())
146
                  ->method('logoutWithRedirectService');
147

148
149
150
151
152
153
154
155
156
157
158
        /** @var TreeRouteStack|\PHPUnit_Framework_MockObject_MockObject $router */
        $router = $this->createMock(TreeRouteStack::class);
        $router
            ->expects($this->once())
            ->method('getRequestUri')
            ->willReturn(new Uri());
        $router
            ->expects($this->once())
            ->method('getBaseUrl')
            ->willReturn('path');

159
        $this->adapter->setCasClient($casClient);
160

161
162
        $this->adapter->setOptions($this->moduleOptions);
        $this->adapter->setRouter($router);
163
164
165
        $this->adapter->logout(new AdapterChainEvent());
    }
}