ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
TokenHandlerTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use PHPUnit\Framework\TestCase;
27
28class TokenHandlerTest extends TestCase
29{
30 protected function getDate(string $string): \DateTimeImmutable
31 {
32 return new \DateTimeImmutable($string, new \DateTimeZone('UTC'));
33 }
34
35 protected function getTokenHandler(\DateTimeImmutable $current_date): TokenHandler
36 {
37 return new class ($current_date) extends TokenHandler {
38 public function __construct(protected \DateTimeImmutable $current_date)
39 {
40 }
41
42 protected function getCurrentDate(): \DateTimeImmutable
43 {
44 return $this->current_date;
45 }
46 };
47 }
48
49 protected function getRequest(): RequestInterface
50 {
51 return new class () extends NullRequest {
52 public ?string $exposed_from_date = null;
53 public ?string $exposed_until_date = null;
54
55 public function withArgument(
56 Argument $key,
57 string $value
59 $clone = clone $this;
60 switch ($key) {
61 case Argument::FROM_DATE:
62 $clone->exposed_from_date = $value;
63 return $clone;
64
65 case Argument::UNTIL_DATE:
66 $clone->exposed_until_date = $value;
67 return $clone;
68
69 default:
70 throw new \ilMDOERExposerException('Argument not covered in mock.');
71 }
72 }
73 };
74 }
75
77 {
78 $handler = $this->getTokenHandler($this->getDate('2022-10-30'));
79
80 $token = $handler->generateToken(32, null, null);
81 $offset_from_token = $handler->getOffsetFromToken($token);
82 $request_from_token = $handler->appendArgumentsFromTokenToRequest($this->getRequest(), $token);
83
84 $this->assertTrue($handler->isTokenValid($token));
85 $this->assertSame(32, $offset_from_token);
86 $this->assertNull($request_from_token->exposed_from_date);
87 $this->assertSame('2022-10-30', $request_from_token->exposed_until_date);
88 }
89
91 {
92 $handler = $this->getTokenHandler($this->getDate('2022-10-30'));
93
94 $token = $handler->generateToken(
95 32,
96 $this->getDate('2021-10-30'),
97 null
98 );
99 $offset_from_token = $handler->getOffsetFromToken($token);
100 $request_from_token = $handler->appendArgumentsFromTokenToRequest($this->getRequest(), $token);
101
102 $this->assertTrue($handler->isTokenValid($token));
103 $this->assertSame(32, $offset_from_token);
104 $this->assertSame('2021-10-30', $request_from_token->exposed_from_date);
105 $this->assertSame('2022-10-30', $request_from_token->exposed_until_date);
106 }
107
109 {
110 $handler = $this->getTokenHandler($this->getDate('2022-10-30'));
111
112 $token = $handler->generateToken(
113 32,
114 null,
115 $this->getDate('2022-09-30')
116 );
117 $offset_from_token = $handler->getOffsetFromToken($token);
118 $request_from_token = $handler->appendArgumentsFromTokenToRequest($this->getRequest(), $token);
119
120 $this->assertTrue($handler->isTokenValid($token));
121 $this->assertSame(32, $offset_from_token);
122 $this->assertNull($request_from_token->exposed_from_date);
123 $this->assertSame('2022-09-30', $request_from_token->exposed_until_date);
124 }
125
127 {
128 $handler = $this->getTokenHandler($this->getDate('2022-10-30'));
129
130 $token = $handler->generateToken(
131 32,
132 null,
133 $this->getDate('2022-11-30')
134 );
135 $offset_from_token = $handler->getOffsetFromToken($token);
136 $request_from_token = $handler->appendArgumentsFromTokenToRequest($this->getRequest(), $token);
137
138 $this->assertTrue($handler->isTokenValid($token));
139 $this->assertSame(32, $offset_from_token);
140 $this->assertNull($request_from_token->exposed_from_date);
141 $this->assertSame('2022-10-30', $request_from_token->exposed_until_date);
142 }
143
145 {
146 $handler = $this->getTokenHandler($this->getDate('2022-10-30'));
147
148 $token = $handler->generateToken(
149 32,
150 $this->getDate('2023-10-30'),
151 $this->getDate('2021-10-30'),
152 );
153 $offset_from_token = $handler->getOffsetFromToken($token);
154 $request_from_token = $handler->appendArgumentsFromTokenToRequest($this->getRequest(), $token);
155
156 $this->assertTrue($handler->isTokenValid($token));
157 $this->assertSame(32, $offset_from_token);
158 $this->assertSame('2023-10-30', $request_from_token->exposed_from_date);
159 $this->assertSame('2021-10-30', $request_from_token->exposed_until_date);
160 }
161
162 public function testIsTokenValidNonsenseString(): void
163 {
164 $handler = $this->getTokenHandler($this->getDate('2022-10-30'));
165
166 $this->assertFalse($handler->isTokenValid('sadadsadsgr'));
167 }
168
169 public function testIsTokenValidNonsenseAppended(): void
170 {
171 $handler = $this->getTokenHandler($this->getDate('2022-10-30'));
172 $token = $handler->generateToken(
173 32,
174 $this->getDate('2023-10-30'),
175 $this->getDate('2021-10-30'),
176 );
177
178 $this->assertFalse($handler->isTokenValid($token . 'sadadsadsgr'));
179 }
180
181 public function testIsTokenValidEmptyArray(): void
182 {
183 $handler = $this->getTokenHandler($this->getDate('2022-10-30'));
184
185 $this->assertFalse($handler->isTokenValid(base64_encode(json_encode([]))));
186 }
187
188 public function testIsTokenValidNoUntilDate(): void
189 {
190 $handler = $this->getTokenHandler($this->getDate('2022-10-30'));
191
192 $this->assertFalse($handler->isTokenValid(base64_encode(json_encode([5]))));
193 }
194
195 public function testIsTokenValidTooManyDates(): void
196 {
197 $handler = $this->getTokenHandler($this->getDate('2022-10-30'));
198
199 $this->assertFalse(
200 $handler->isTokenValid(
201 base64_encode(json_encode([5, '2023-10-30', '2021-10-30', '2051-10-30']))
202 )
203 );
204 }
205
207 {
208 $handler = $this->getTokenHandler($this->getDate('2022-10-30'));
209
210 $this->assertFalse(
211 $handler->isTokenValid(
212 base64_encode(json_encode(['asdsasd', '2023-10-30']))
213 )
214 );
215 }
216
217 public function testIsTokenValidInvalidDate(): void
218 {
219 $handler = $this->getTokenHandler($this->getDate('2022-10-30'));
220
221 $this->assertFalse(
222 $handler->isTokenValid(
223 base64_encode(json_encode([5, '2023-99-99']))
224 )
225 );
226 }
227}
__construct()
Constructor setup ILIAS global object @access public.
Definition: class.ilias.php:76
$handler
Definition: oai.php:29
$token
Definition: xapitoken.php:70