Passed
Pull Request — master (#105)
by Robbie
01:55
created

testSetMethodWithVerifiedMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\MFA\Tests\Store;
4
5
use SilverStripe\Dev\SapphireTest;
6
use SilverStripe\MFA\Store\SessionStore;
7
use SilverStripe\Security\Member;
8
9
class SessionStoreTest extends SapphireTest
10
{
11
    /**
12
     * @expectedException \RuntimeException
13
     * @expectedExceptionMessageRegExp /possibly incorrectly encoded/
14
     */
15
    public function testSerializeThrowsExceptionOnFailure()
16
    {
17
        $store = new SessionStore($this->createMock(Member::class));
18
        $store->setState(['some binary' => random_bytes(32)]);
19
        $store->serialize();
20
    }
21
22
    public function testSetState()
23
    {
24
        $store = new SessionStore($this->createMock(Member::class));
25
        $store->setState(['foo' => 'bar']);
26
        $this->assertSame(['foo' => 'bar'], $store->getState());
27
    }
28
29
    public function testAddState()
30
    {
31
        $store = new SessionStore($this->createMock(Member::class));
32
        $store->setState(['foo' => 'bar', 'bar' => 'baz']);
33
        $store->addState(['foo' => 'baz']);
34
        $this->assertSame(['foo' => 'baz', 'bar' => 'baz'], $store->getState());
35
    }
36
37
    /**
38
     * @expectedException \SilverStripe\MFA\Exception\InvalidMethodException
39
     * @expectedExceptionMessage You cannot verify with a method you have already verified
40
     */
41
    public function testSetMethodWithVerifiedMethod()
42
    {
43
        $store = new SessionStore($this->createMock(Member::class));
44
        $store->addVerifiedMethod('foobar');
45
        $store->setMethod('foobar');
46
    }
47
48
    public function testSetMethod()
49
    {
50
        $store = new SessionStore($this->createMock(Member::class));
51
        $store->setMethod('foobar');
52
        $this->assertSame('foobar', $store->getMethod());
53
    }
54
55
    public function testSetMemberDoesNotResetMethodsWhenNoChange()
56
    {
57
        $member1 = $this->createMock(Member::class);
58
59
        $store = new SessionStore($member1);
60
        $store->setMethod('foobar');
61
        $store->addVerifiedMethod('foobar');
62
        $store->setMember($member1);
63
64
        $this->assertSame('foobar', $store->getMethod());
65
    }
66
67
    public function testSetMemberResetsMethodsWhenMemberChanged()
68
    {
69
        $member1 = new Member();
70
        $member1->ID = 25;
71
        $member2 = new Member();
72
        $member2->ID = 50;
73
74
        $store = new SessionStore($member1);
75
        $store->setMethod('foobar');
76
        $store->addVerifiedMethod('foobar');
77
        $store->setMember($member2);
78
79
        $this->assertEmpty($store->getMethod());
80
    }
81
}
82