Passed
Branch master (0b08c9)
by Tim
15:40
created

PowerIdPDiscoTest::testmcmp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 41
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 25
nc 1
nop 0
dl 0
loc 41
rs 9.52
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Test\Module\discopower;
6
7
use PHPUnit\Framework\TestCase;
8
use ReflectionClass;
9
use SAML2\Constants;
10
use SimpleSAML\Configuration;
11
use SimpleSAML\Metadata\MetaDataStorageHandler;
12
use SimpleSAML\Module\discopower\PowerIdPDisco;
13
14
class PowerIdPDiscoTest extends TestCase
15
{
16
    /** @var \SimpleSAML\Module\discopower\PowerIdPDisco */
17
    private PowerIdPDisco $discoHandler;
18
19
    /** @var \SimpleSAML\Configuration */
20
    private Configuration $config;
21
22
    /** @var \SimpleSAML\Configuration */
23
    private Configuration $discoConfig;
24
25
    /** @var array */
26
    private array $idpList;
27
28
    /**
29
     */
30
    protected function setUp(): void
31
    {
32
        $this->config = Configuration::loadFromArray([
33
            'module.enable' => ['discopower' => true],
34
            'metadata.sources' => [
35
                ['type' => 'flatfile', 'directory' => __DIR__ . '/test-metadata'],
36
            ],
37
        ], '[ARRAY]', 'simplesaml');
38
        Configuration::setPreLoadedConfig($this->config, 'config.php');
39
40
        $this->discoConfig = Configuration::loadFromArray([
41
            'defaulttab' => 0,
42
            'taborder' => ['B', 'A'],
43
        ], '[ARRAY]', 'module_discopower');
44
        Configuration::setPreLoadedConfig($this->discoConfig, 'module_discopower.php');
45
46
        /* spoof the request*/
47
        $_GET['entityID'] = 'https://sp01.example.net/sp';
48
        $_GET['return'] = 'https://sp01.example.net/simplesaml/module.php/saml/sp/discoresp.php';
49
        $_GET['returnIDParam'] = 'idpentityid';
50
        $_SERVER['SERVER_NAME'] = 'sp01.example.net';
51
        $_SERVER['REQUEST_URI'] = '/simplesaml/module.php/discopower/disco.php';
52
53
        $this->discoHandler = new PowerIdPDisco(
54
            ['saml20-idp-remote'],
55
            'poweridpdisco'
56
        );
57
58
        $this->idpList = MetaDataStorageHandler::getMetadataHandler()->getList('saml20-idp-remote');
59
    }
60
61
    /**
62
     * @covers \SimpleSAML\Module\discopower\PowerIdPDisco
63
     * @covers \SimpleSAML\Module\discopower\PowerIdPDisco::__construct
64
     */
65
    public function testPowerIdPDisco(): void
66
    {
67
        $this->assertInstanceOf(PowerIdPDisco::class, $this->discoHandler);
68
    }
69
70
    /**
71
     * @covers \SimpleSAML\Module\discopower\PowerIdPDisco::getIdPList
72
     */
73
    public function testGetIdPList(): void
74
    {
75
        $refl = new ReflectionClass($this->discoHandler);
76
        $getIdPList = $refl->getMethod('getIdPList');
77
        $getIdPList->setAccessible(true);
78
        $idpList = $getIdPList->invoke($this->discoHandler);
79
80
        $this->assertEquals($this->idpList, $idpList);
81
    }
82
83
    /**
84
     * @covers \SimpleSAML\Module\discopower\PowerIdPDisco::idplistStructured
85
     * @covers \SimpleSAML\Module\discopower\PowerIdPDisco::getIdPList
86
     * @covers \SimpleSAML\Module\discopower\PowerIdPDisco::mcmp
87
     */
88
    public function testIdplistStructured(): void
89
    {
90
        $refl = new ReflectionClass($this->discoHandler);
91
        $idplistStructured = $refl->getMethod('idplistStructured');
92
        $idplistStructured->setAccessible(true);
93
        $idpList = $idplistStructured->invokeArgs($this->discoHandler, [$this->idpList]);
94
95
        $expected = [
96
            'B' => [
97
                'https://idp04.example.org' => [
98
                    'name' => ['en' => 'IdP 04'],
99
                    'tags' => ['A', 'B'],
100
                    'entityid' => 'https://idp04.example.org',
101
                    'UIInfo' => ['Keywords' => ['en' => ['aap','noot','mies']]],
102
                ],
103
                'https://idp06.example.org' => [
104
                    'name' => ['en' => 'IdP 06'],
105
                    'tags' => ['B'],
106
                    'entityid' => 'https://idp06.example.org',
107
                    'UIInfo' => ['Keywords' => ['fr' => ['singue','noix','mies'], 'de' => ['Affe', 'Nuss', 'mies']]],
108
                ],
109
                'https://idp05.example.org' => [
110
                    'tags' => ['B'],
111
                    'entityid' => 'https://idp05.example.org'
112
                ],
113
            ],
114
            'A' => [
115
                'https://idp03.example.org' => [
116
                    'name' => ['en' => 'IdP 03'],
117
                    'discopower.weight' => 100,
118
                    'tags' => ['A'],
119
                    'entityid' => 'https://idp03.example.org'
120
                ],
121
                'https://idp02.example.org' => [
122
                    'name' => ['en' => 'IdP 02'],
123
                    'tags' => ['A'],
124
                    'entityid' => 'https://idp02.example.org'
125
                ],
126
                'https://idp04.example.org' => [
127
                    'name' => ['en' => 'IdP 04'],
128
                    'tags' => ['A','B',],
129
                    'entityid' => 'https://idp04.example.org',
130
                    'UIInfo' => ['Keywords' => ['en' => ['aap','noot','mies']]],
131
                ],
132
                'https://idp01.example.org' => [
133
                    'name' => ['en' => 'IdP 01'],
134
                    'discopower.weight' => 1,
135
                    'tags' => ['A'],
136
                    'entityid' => 'https://idp01.example.org'
137
                ],
138
            ],
139
        ];
140
        $this->assertEquals($expected, $idpList);
141
        $this->assertEquals($expected['A'], $idpList['A']);
142
        $this->assertEquals($expected['B'], $idpList['B']);
143
    }
144
145
    /**
146
     * @covers \SimpleSAML\Module\discopower\PowerIdPDisco::mcmp
147
     */
148
    public function testmcmp(): void
149
    {
150
        $this->assertEquals(
151
            -1,
152
            PowerIdPDisco::mcmp(
153
                ['name' => 'B', 'entityid' => '1'],
154
                ['name' => 'A', 'entityid' => '2']
155
            ),
156
            'name,name'
157
        );
158
        $this->assertEquals(
159
            -1,
160
            PowerIdPDisco::mcmp(
161
                ['entityid' => '1'],
162
                ['name' => 'A', 'entityid' => '2']
163
            ),
164
            'entityid,name'
165
        );
166
        $this->assertEquals(
167
            1,
168
            PowerIdPDisco::mcmp(
169
                ['entityid' => '2'],
170
                ['entityid' => '1']
171
            ),
172
            'entityid,entityid'
173
        );
174
        $this->assertEquals(
175
            -1,
176
            PowerIdPDisco::mcmp(
177
                ['name' => 'B', 'entityid' => '1', 'discopower.weight' => 100],
178
                ['name' => 'A', 'entityid' => '2']
179
            ),
180
            'weight,name'
181
        );
182
        $this->assertEquals(
183
            1,
184
            PowerIdPDisco::mcmp(
185
                ['name' => 'B', 'entityid' => '1', 'discopower.weight' => 100],
186
                ['name' => 'A', 'entityid' => '2', 'discopower.weight' => 200]
187
            ),
188
            'weight,weight'
189
        );
190
    }
191
}
192