File: AttributeCopyTest.php

package info (click to toggle)
simplesamlphp 1.19.7-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 42,920 kB
  • sloc: php: 202,044; javascript: 14,867; xml: 2,700; sh: 225; perl: 82; makefile: 70; python: 5
file content (179 lines) | stat: -rw-r--r-- 4,948 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php

declare(strict_types=1);

namespace SimpleSAML\Test\Module\core\Auth\Process;

use Exception;
use PHPUnit\Framework\TestCase;
use SimpleSAML\Module\core\Auth\Process\AttributeCopy;

/**
 * Test for the core:AttributeCopy filter.
 */
class AttributeCopyTest extends TestCase
{
    /**
     * Helper function to run the filter with a given configuration.
     *
     * @param array $config  The filter configuration.
     * @param array $request  The request state.
     * @return array  The state array after processing.
     */
    private static function processFilter(array $config, array $request): array
    {
        $filter = new AttributeCopy($config, null);
        $filter->process($request);
        return $request;
    }


    /**
     * Test the most basic functionality.
     * @return void
     */
    public function testBasic(): void
    {
        $config = [
            'test' => 'testnew',
        ];
        $request = [
            'Attributes' => ['test' => ['AAP']],
        ];
        $result = self::processFilter($config, $request);
        $attributes = $result['Attributes'];
        $this->assertArrayHasKey('test', $attributes);
        $this->assertArrayHasKey('testnew', $attributes);
        $this->assertEquals($attributes['testnew'], ['AAP']);
    }


    /**
     * Test the most basic functionality.
     * @return void
     */
    public function testArray(): void
    {
        $config = [
            'test' => ['new1', 'new2'],
        ];
        $request = [
            'Attributes' => ['test' => ['AAP']],
        ];
        $result = self::processFilter($config, $request);
        $attributes = $result['Attributes'];
        $this->assertArrayHasKey('test', $attributes);
        $this->assertArrayHasKey('new1', $attributes);
        $this->assertArrayHasKey('new2', $attributes);
        $this->assertEquals($attributes['new1'], ['AAP']);
        $this->assertEquals($attributes['new2'], ['AAP']);
    }


    /**
     * Test that existing attributes are left unmodified.
     * @return void
     */
    public function testExistingNotModified(): void
    {
        $config = [
            'test' => 'testnew',
        ];
        $request = [
            'Attributes' => [
                'test' => ['AAP'],
                'original1' => ['original_value1'],
                'original2' => ['original_value2'],
            ],
        ];
        $result = self::processFilter($config, $request);
        $attributes = $result['Attributes'];
        $this->assertArrayHasKey('testnew', $attributes);
        $this->assertEquals($attributes['test'], ['AAP']);
        $this->assertArrayHasKey('original1', $attributes);
        $this->assertEquals($attributes['original1'], ['original_value1']);
        $this->assertArrayHasKey('original2', $attributes);
        $this->assertEquals($attributes['original2'], ['original_value2']);
    }


    /**
     * Test copying multiple attributes
     * @return void
     */
    public function testCopyMultiple(): void
    {
        $config = [
            'test1' => 'new1',
            'test2' => 'new2',
        ];
        $request = [
            'Attributes' => ['test1' => ['val1'], 'test2' => ['val2.1', 'val2.2']],
        ];
        $result = self::processFilter($config, $request);
        $attributes = $result['Attributes'];
        $this->assertArrayHasKey('new1', $attributes);
        $this->assertEquals($attributes['new1'], ['val1']);
        $this->assertArrayHasKey('new2', $attributes);
        $this->assertEquals($attributes['new2'], ['val2.1', 'val2.2']);
    }


    /**
     * Test behaviour when target attribute exists (should be replaced).
     * @return void
     */
    public function testCopyClash(): void
    {
        $config = [
            'test' => 'new1',
        ];
        $request = [
            'Attributes' => [
                'test' => ['testvalue1'],
                'new1' => ['newvalue1'],
            ],
        ];
        $result = self::processFilter($config, $request);
        $attributes = $result['Attributes'];
        $this->assertEquals($attributes['new1'], ['testvalue1']);
    }


    /**
     * Test wrong attribute name
     * @return void
     */
    public function testWrongAttributeName(): void
    {
        $this->expectException(Exception::class);
        $config = [
            ['value2'],
        ];
        $request = [
            'Attributes' => [
                'test' => ['value1'],
            ],
        ];
        self::processFilter($config, $request);
    }


    /**
     * Test wrong attribute value
     * @return void
     */
    public function testWrongAttributeValue(): void
    {
        $this->expectException(Exception::class);
        $config = [
            'test' => 100,
        ];
        $request = [
            'Attributes' => [
                'test' => ['value1'],
            ],
        ];
        self::processFilter($config, $request);
    }
}