flattenInstitutionResults()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupMiddleware\Test\Database;
20
21
use Surfnet\Stepup\Identity\Collection\InstitutionCollection;
22
use Surfnet\Stepup\Identity\Value\IdentityId;
23
use Surfnet\Stepup\Identity\Value\Institution;
24
use Surfnet\Stepup\Identity\Value\RegistrationAuthorityRole;
0 ignored issues
show
Bug introduced by
The type Surfnet\Stepup\Identity\...gistrationAuthorityRole was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\AuthorizationRepository;
26
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
27
28
/**
29
 * Test the AuthorizationRepository.
30
 *
31
 * This repo is responsible for determining great portions of the FGA authorizations.
32
 * Having that repository code (DQL) under test will greatly decrease the chance of
33
 * regressions in that area.
34
 *
35
 * Tests in this file are based of a database dump from test2, a quite representative
36
 * data set was captured there with many FGA scenarios covered.
37
 */
38
class AuthorizationRepositoryMatrixTest extends KernelTestCase
39
{
40
    /**
41
     * @var AuthorizationRepository
42
     */
43
    private ?object $authzRepository;
44
45
    public static function authorizationMatrix(): array
46
    {
47
        $ra = RegistrationAuthorityRole::ra();
48
        $raa = RegistrationAuthorityRole::raa();
49
        // The uuids match those of the `Fixtures/test2.sql` data
50
        $aRa = new IdentityId('eff4d3bc-bbe9-45d4-b80d-080bc7e06615'); // pieter-a-ra
51
        $aRaa = new IdentityId('947da709-185b-4d9a-ba49-0a22d99dceb3'); // michiel-a-raa (only raa in institution-a)
52
        $avRaa = new IdentityId('cccfece4-e5e5-40b7-9aa4-a800d7cd3633'); // pieter-a-raa (raa in inst-a and inst-v)
53
        return [
54
            'RA from inst-a should have RA rights in inst-a+f' => [$ra, $aRa, ['institution-a.nl', 'institution-f.nl']],
55
            'RA from inst-a should not have RAA rights in inst-a+f' => [$raa, $aRa, []],
56
            'RAA from inst-a should have RA rights in inst-a+f' => [$ra, $aRaa, ['institution-a.nl', 'institution-f.nl']],
57
            'RAA from inst-a should have RAA rights in inst-a+f' => [$raa, $aRaa, ['institution-a.nl', 'institution-f.nl']],
58
            'RAA from inst-a+v should have RA rights in inst-a+f+i' => [$ra, $avRaa, ['institution-a.nl', 'institution-f.nl', 'institution-i.nl']],
59
            'RAA from inst-a+v should have RAA rights in inst-a+f+i' => [$raa, $avRaa, ['institution-a.nl', 'institution-f.nl', 'institution-i.nl']],
60
        ];
61
    }
62
63
    public static function selectRaaMatrix(): array
64
    {
65
        $aRaa = new IdentityId('cccfece4-e5e5-40b7-9aa4-a800d7cd3633'); // Raa @ institution A
66
        $ghRaa = new IdentityId('02b70719-243f-4c7d-8649-48952a816ddf'); // RAA @ institution H
67
68
        return [
69
            'RAA inst-a => select_raa @ inst-a' => [$aRaa, ['institution-a.nl']],
70
            'RAA inst-h => select_raa @ inst-h+g' => [$ghRaa, ['institution-g.nl', 'institution-h.nl']],
71
        ];
72
    }
73
74
    protected function setUp(): void
75
    {
76
        $kernel = self::bootKernel();
77
        $manager = $kernel->getContainer()
78
            ->get('doctrine')
79
            ->getManager();
80
81
        $this->authzRepository = $kernel->getContainer()->get(AuthorizationRepository::class);
82
        $fixture = file_get_contents(__DIR__ . '/Fixture/test2.sql');
83
        $manager->getConnection()->executeStatement($fixture);
84
        $manager->flush();
85
    }
86
87
    /**
88
     * A test matrix to verify the correct institutions are selected for a given identity for a
89
     * specific institution role.
90
     */
91
    #[\PHPUnit\Framework\Attributes\DataProvider('authorizationMatrix')]
92
    public function test_get_institutions_for_role_matrix(
93
        RegistrationAuthorityRole $requiredRole,
94
        IdentityId $identity,
95
        array $expectedInstitutions
96
    ): void {
97
        $institutions = $this->authzRepository->getInstitutionsForRole($requiredRole, $identity);
0 ignored issues
show
Bug introduced by
The method getInstitutionsForRole() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

97
        /** @scrutinizer ignore-call */ 
98
        $institutions = $this->authzRepository->getInstitutionsForRole($requiredRole, $identity);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
98
        $results = $this->flattenInstitutionResults($institutions);
99
100
        $this->assertEquals(
101
            $results,
102
            $expectedInstitutions,
103
            sprintf(
104
                'The results do not match the expected results. Actual "%s" versus expected: "%s"',
105
                implode(',', $results),
106
                implode(',', $expectedInstitutions)
107
            )
108
        );
109
    }
110
111
    #[\PHPUnit\Framework\Attributes\DataProvider('selectRaaMatrix')]
112
    public function test_select_raa_authorization(IdentityId $identityId, array $expected): void
113
    {
114
        $institutions = $this->authzRepository->getInstitutionsForSelectRaaRole($identityId);
115
        $this->assertEquals($expected, $this->flattenInstitutionResults($institutions));
116
117
    }
118
119
    /**
120
     * @return mixed[]
121
     */
122
    private function flattenInstitutionResults(InstitutionCollection $collection): array
123
    {
124
        $institutions = [];
125
        /** @var Institution $institution */
126
        foreach($collection->jsonSerialize()['institutions'] as $institution)
127
        {
128
            $institutions[] = $institution->getInstitution();
129
        }
130
        return $institutions;
131
    }
132
}
133