Completed
Push — 4.2.0 ( f03d5e...24bd0a )
by Daniel
12:22 queued 05:26
created

SessionAuthenticationHandlerTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 48
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testAuthenticateRequestStartsSessionWithSessionIdentifier() 0 19 1
A testAuthenticateRequestDefersSessionStartWithoutSessionIdentifier() 0 15 1
1
<?php
2
namespace SilverStripe\Security\Tests\MemberAuthenticator;
3
4
use SilverStripe\Control\Cookie;
5
use SilverStripe\Control\HTTPRequest;
6
use SilverStripe\Control\Session;
7
use SilverStripe\Dev\SapphireTest;
8
9
use SilverStripe\Security\Member;
10
use SilverStripe\Security\MemberAuthenticator\SessionAuthenticationHandler;
11
12
class SessionAuthenticationHandlerTest extends SapphireTest
13
{
14
    protected $usesDatabase = true;
15
16
    /**
17
     * @runInSeparateProcess
18
     * @preserveGlobalState disabled
19
     */
20
    public function testAuthenticateRequestDefersSessionStartWithoutSessionIdentifier()
21
    {
22
        $member = new Member(['Email' => '[email protected]']);
23
        $member->write();
24
25
        $handler = new SessionAuthenticationHandler();
26
27
        $session = new Session(null); // unstarted, simulates lack of session cookie
28
        $session->set($handler->getSessionVariable(), $member->ID);
29
30
        $req = new HTTPRequest('GET', '/');
31
        $req->setSession($session);
32
33
        $matchedMember = $handler->authenticateRequest($req);
34
        $this->assertNull($matchedMember);
35
    }
36
37
    /**
38
     * @runInSeparateProcess
39
     * @preserveGlobalState disabled
40
     */
41
    public function testAuthenticateRequestStartsSessionWithSessionIdentifier()
42
    {
43
        $member = new Member(['Email' => '[email protected]']);
44
        $member->write();
45
46
        $handler = new SessionAuthenticationHandler();
47
48
        $session = new Session(null); // unstarted
49
        $session->set($handler->getSessionVariable(), $member->ID);
50
51
        $req = new HTTPRequest('GET', '/');
52
        $req->setSession($session);
53
54
        Cookie::set(session_name(), '1234');
55
        $session->start($req); // simulate detection of session cookie
56
57
        $matchedMember = $handler->authenticateRequest($req);
58
        $this->assertNotNull($matchedMember);
59
        $this->assertEquals($matchedMember->Email, $member->Email);
60
    }
61
}
62