Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace core\session\utility;
18
 
19
/**
20
 * Tests for the cookie_helper utility class.
21
 *
22
 * @package    core
23
 * @copyright  2024 Jake Dallimore <jrhdallimore@gmail.com>
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 * @covers \core\session\utility\cookie_helper
26
 */
27
class cookie_helper_test extends \advanced_testcase {
28
 
29
    /**
30
     * Testing cookie_response_headers_add_attributes().
31
     *
32
     * @dataProvider cookie_response_headers_provider
33
     *
34
     * @param array $headers the headers to search
35
     * @param array $cookienames the cookienames to match
36
     * @param array $attributes the attributes to add
37
     * @param bool $casesensitive whether to do a case-sensitive lookup for the attribute
38
     * @param array $expectedheaders the expected, updated headers
39
     * @return void
40
     */
41
    public function test_cookie_response_headers_add_attributes(array $headers, array $cookienames, array $attributes,
42
            bool $casesensitive, array $expectedheaders): void {
43
 
44
        $updated = cookie_helper::cookie_response_headers_add_attributes($headers, $cookienames, $attributes, $casesensitive);
45
        $this->assertEquals($expectedheaders, $updated);
46
    }
47
 
48
    /**
49
     * Data provider for testing cookie_response_headers_add_attributes().
50
     *
51
     * @return array the inputs and expected outputs.
52
     */
53
    public static function cookie_response_headers_provider(): array {
54
        return [
55
            'Only one matching cookie header, without any of the attributes' => [
56
                'headers' => [
57
                    'Set-Cookie: testcookie=value; path=/test/; HttpOnly;',
58
                ],
59
                'cookienames' => [
60
                    'testcookie',
61
                ],
62
                'attributes' => [
63
                    'Partitioned',
64
                    'SameSite=None',
65
                    'Secure',
66
                ],
67
                'casesensitive' => false,
68
                'output' => [
69
                    'Set-Cookie: testcookie=value; path=/test/; HttpOnly; Partitioned; SameSite=None; Secure;',
70
                ],
71
            ],
72
            'Several matching cookie headers, without attributes' => [
73
                'headers' => [
74
                    'Set-Cookie: testcookie=value; path=/test/; HttpOnly;',
75
                    'Set-Cookie: mytestcookie=value; path=/test/; HttpOnly;',
76
                ],
77
                'cookienames' => [
78
                    'testcookie',
79
                    'mytestcookie',
80
                ],
81
                'attributes' => [
82
                    'Partitioned',
83
                    'SameSite=None',
84
                    'Secure',
85
                ],
86
                'casesensitive' => false,
87
                'output' => [
88
                    'Set-Cookie: testcookie=value; path=/test/; HttpOnly; Partitioned; SameSite=None; Secure;',
89
                    'Set-Cookie: mytestcookie=value; path=/test/; HttpOnly; Partitioned; SameSite=None; Secure;',
90
                ],
91
            ],
92
            'Several matching cookie headers, several non-matching, all missing all attributes' => [
93
                'headers' => [
94
                    'Set-Cookie: testcookie=value; path=/test/; HttpOnly;',
95
                    'Set-Cookie: mytestcookie=value; path=/test/; HttpOnly;',
96
                    'Set-Cookie: anothertestcookie=value; path=/test/; HttpOnly;',
97
                ],
98
                'cookienames' => [
99
                    'testcookie',
100
                    'mytestcookie',
101
                    'blah',
102
                    'etc',
103
                ],
104
                'attributes' => [
105
                    'Partitioned',
106
                    'SameSite=None',
107
                    'Secure',
108
                ],
109
                'casesensitive' => false,
110
                'output' => [
111
                    'Set-Cookie: testcookie=value; path=/test/; HttpOnly; Partitioned; SameSite=None; Secure;',
112
                    'Set-Cookie: mytestcookie=value; path=/test/; HttpOnly; Partitioned; SameSite=None; Secure;',
113
                    'Set-Cookie: anothertestcookie=value; path=/test/; HttpOnly;',
114
                ],
115
            ],
116
            'Matching cookie headers, some with existing attributes' => [
117
                'headers' => [
118
                    'Set-Cookie: testcookie=value; path=/test/; secure; HttpOnly; Partitioned; SameSite=None',
119
                    'Set-Cookie: mytestcookie=value; path=/test/; secure; HttpOnly; SameSite=None',
120
                ],
121
                'cookienames' => [
122
                    'testcookie',
123
                    'mytestcookie',
124
                    'etc',
125
                ],
126
                'attributes' => [
127
                    'Partitioned',
128
                    'SameSite=None',
129
                    'Secure',
130
                ],
131
                'casesensitive' => false,
132
                'output' => [
133
                    'Set-Cookie: testcookie=value; path=/test/; secure; HttpOnly; Partitioned; SameSite=None',
134
                    'Set-Cookie: mytestcookie=value; path=/test/; secure; HttpOnly; SameSite=None; Partitioned;',
135
                ],
136
            ],
137
            'Matching headers, some with existing attributes, case sensitive' => [
138
                'headers' => [
139
                    'Set-Cookie: testcookie=value; path=/test/; secure; HttpOnly; SameSite=None; partitioned',
140
                    'Set-Cookie: mytestcookie=value; path=/test/; secure; HttpOnly; SameSite=None',
141
                ],
142
                'cookienames' => [
143
                    'testcookie',
144
                    'mytestcookie',
145
                    'etc',
146
                ],
147
                'attributes' => [
148
                    'Partitioned',
149
                    'SameSite=None',
150
                    'Secure',
151
                ],
152
                'casesensitive' => true,
153
                'output' => [
154
                    'Set-Cookie: testcookie=value; path=/test/; secure; HttpOnly; SameSite=None; partitioned; Partitioned; Secure;',
155
                    'Set-Cookie: mytestcookie=value; path=/test/; secure; HttpOnly; SameSite=None; Partitioned; Secure;',
156
                ],
157
            ],
158
            'Empty list of cookie names to match, so unmodified inputs' => [
159
                'headers' => [
160
                    'Set-Cookie: testcookie=value; path=/test/; secure; HttpOnly; SameSite=None; partitioned',
161
                    'Set-Cookie: mytestcookie=value; path=/test/; secure; HttpOnly; SameSite=None',
162
                ],
163
                'cookienames' => [],
164
                'attributes' => [
165
                    'Partitioned',
166
                    'SameSite=None',
167
                    'Secure',
168
                ],
169
                'casesensitive' => false,
170
                'output' => [
171
                    'Set-Cookie: testcookie=value; path=/test/; secure; HttpOnly; SameSite=None; partitioned',
172
                    'Set-Cookie: mytestcookie=value; path=/test/; secure; HttpOnly; SameSite=None',
173
                ],
174
            ],
175
            'Empty list of attributes to set, so unmodified inputs' => [
176
                'headers' => [
177
                    'Set-Cookie: testcookie=value; path=/test/; secure; HttpOnly; SameSite=None; partitioned',
178
                    'Set-Cookie: mytestcookie=value; path=/test/; secure; HttpOnly; SameSite=None',
179
                ],
180
                'cookienames' => [
181
                    'testcookie',
182
                    'mycookie',
183
                ],
184
                'attributes' => [],
185
                'casesensitive' => false,
186
                'output' => [
187
                    'Set-Cookie: testcookie=value; path=/test/; secure; HttpOnly; SameSite=None; partitioned',
188
                    'Set-Cookie: mytestcookie=value; path=/test/; secure; HttpOnly; SameSite=None',
189
                ],
190
            ],
191
            'Other HTTP headers, some matching Set-Cookie, some not' => [
192
                'headers' => [
193
                    'Authorization: blah',
194
                    'Set-Cookie: testcookie=value; path=/test/; secure; HttpOnly; SameSite=None; Partitioned',
195
                    'Set-Cookie: mytestcookie=value; path=/test/; secure; HttpOnly; SameSite=None',
196
                ],
197
                'cookienames' => [
198
                    'testcookie',
199
                    'mytestcookie',
200
                ],
201
                'attributes' => [
202
                    'Partitioned',
203
                    'SameSite=None',
204
                    'Secure',
205
                ],
206
                'casesensitive' => false,
207
                'output' => [
208
                    'Authorization: blah',
209
                    'Set-Cookie: testcookie=value; path=/test/; secure; HttpOnly; SameSite=None; Partitioned',
210
                    'Set-Cookie: mytestcookie=value; path=/test/; secure; HttpOnly; SameSite=None; Partitioned;',
211
                ],
212
            ],
213
        ];
214
    }
215
}