Passed
Pull Request — 4.2 (#8269)
by Ingo
07:01
created

testAuthenticateRequestDefersSessionStartWithoutSessionIdentifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 0
dl 0
loc 15
rs 9.9666
c 0
b 0
f 0
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