SchemaGeneratorTest   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 23
c 5
b 0
f 0
dl 0
loc 45
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 9 1
A testGetSchema() 0 25 1
1
<?php
2
3
namespace SilverStripe\MFA\Tests\Service;
4
5
use SilverStripe\Dev\SapphireTest;
6
use SilverStripe\MFA\Service\MethodRegistry;
7
use SilverStripe\MFA\Service\SchemaGenerator;
8
use SilverStripe\MFA\State\RegisteredMethodDetailsInterface;
9
use SilverStripe\MFA\Tests\Stub\BasicMath\Method as BasicMathMethod;
10
use SilverStripe\Security\Member;
11
12
class SchemaGeneratorTest extends SapphireTest
13
{
14
    protected static $fixture_file = 'SchemaGeneratorTest.yml';
15
16
    /**
17
     * @var SchemaGenerator
18
     */
19
    protected $generator;
20
21
    protected function setUp()
22
    {
23
        parent::setUp();
24
25
        MethodRegistry::config()->set('methods', [
26
            BasicMathMethod::class,
27
        ]);
28
29
        $this->generator = new SchemaGenerator();
30
    }
31
32
    public function testGetSchema()
33
    {
34
        /** @var Member $member */
35
        $member = $this->objFromFixture(Member::class, 'sally_smith');
36
        $this->logInAs($member);
37
38
        $schema = $this->generator->getSchema($member);
39
40
        $this->assertArrayHasKey('registeredMethods', $schema);
41
        $this->assertContainsOnlyInstancesOf(RegisteredMethodDetailsInterface::class, $schema['registeredMethods']);
42
        $this->assertSame('backup-codes', $schema['registeredMethods'][0]->jsonSerialize()['urlSegment']);
43
44
        $this->assertArrayHasKey('availableMethods', $schema);
45
        $this->assertContainsOnlyInstancesOf(RegisteredMethodDetailsInterface::class, $schema['registeredMethods']);
46
        $this->assertSame('basic-math', $schema['availableMethods'][0]->jsonSerialize()['urlSegment']);
47
48
        $this->assertArrayHasKey('allMethods', $schema);
49
        $this->assertCount(1, $schema['allMethods'], 'Only BasicMath is registered; allMethods was wrong');
50
51
        $this->assertArrayHasKey('defaultMethod', $schema);
52
        $this->assertNotEmpty($schema['defaultMethod']);
53
        $this->assertSame('backup-codes', $schema['defaultMethod']);
54
55
        $this->assertArrayHasKey('canSkip', $schema);
56
        $this->assertArrayHasKey('shouldRedirect', $schema);
57
    }
58
}
59