ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
HandlerTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
24 use ILIAS\Data\URI;
35 
36 class HandlerTest extends TestCase
37 {
38  protected function getURI(string $string): URI
39  {
40  $url = $this->createMock(URI::class);
41  $url->method('__toString')->willReturn($string);
42  return $url;
43  }
44 
45  protected function getInitiator(
46  bool $activated,
47  string $content,
48  bool $processor_throws_exception = false,
49  bool $processor_throws_error = false,
50  bool $processor_triggers_error = false
52  return new class (
53  $activated,
54  $content,
55  $processor_throws_exception,
56  $processor_throws_error,
57  $processor_triggers_error
58  ) extends NullInitiator {
59  protected HTTPWrapperInterface $wrapper;
60 
61  public function __construct(
62  protected bool $activated,
63  protected string $content,
64  protected bool $processor_throws_exception,
65  protected bool $processor_throws_error,
66  protected bool $processor_triggers_error
67  ) {
68  $this->wrapper = new class () extends NullWrapper {
69  public array $exposed_responses = [];
70 
71  public function sendResponseAndClose(
72  int $status_code,
73  string $message = '',
74  \DOMDocument $body = null
75  ): void {
76  $this->exposed_responses[] = [
77  'status' => $status_code,
78  'message' => $message,
79  'body' => $body?->saveXML($body->documentElement)
80  ];
81  }
82  };
83  }
84 
85  public function settings(): SettingsInterface
86  {
87  return new class ($this->activated) extends NullSettings {
88  public function __construct(
89  protected bool $activated
90  ) {
91  }
92 
93  public function isOAIPMHActive(): bool
94  {
95  return $this->activated;
96  }
97  };
98  }
99 
100  public function requestParser(): RequestParserInterface
101  {
102  return new class ($this->content) extends NullParser {
103  public function __construct(protected string $content)
104  {
105  }
106 
107  public function parseFromHTTP(URI $base_url): RequestInterface
108  {
109  return new class ($this->content, $base_url) extends NullRequest {
110  public function __construct(
111  protected string $content,
112  protected URI $base_url
113  ) {
114  }
115 
116  public function baseURL(): URI
117  {
118  return $this->base_url;
119  }
120 
121  public function exposeContent(): string
122  {
123  return $this->content;
124  }
125  };
126  }
127  };
128  }
129 
130  public function requestProcessor(): RequestProcessorInterface
131  {
132  return new class (
133  $this->processor_throws_exception,
134  $this->processor_throws_error,
135  $this->processor_triggers_error
136  ) extends NullRequestProcessor {
137  public function __construct(
138  protected bool $throws_exception,
139  protected bool $throws_error,
140  protected bool $triggers_error
141  ) {
142  }
143 
144  public function getResponseToRequest(RequestInterface $request): \DomDocument
145  {
146  if ($this->throws_exception) {
147  throw new \ilMDOERExposerException('exception message');
148  }
149  if ($this->throws_error) {
150  throw new \Error('thrown error message');
151  }
152  if ($this->triggers_error) {
153  trigger_error('triggered error message', E_USER_ERROR);
154  }
155 
156  $url = (string) $request->baseURL();
157  $content = $request->exposeContent();
158  $doc = new \DOMDocument();
159  $doc->appendChild($doc->createElement('content', $url . '~!~' . $content));
160  return $doc;
161  }
162  };
163  }
164 
165  public function httpWrapper(): HTTPWrapperInterface
166  {
167  // must always be the same instance to make the expose work
168  return $this->wrapper;
169  }
170 
171  public function exposeHTTPResponses(): array
172  {
173  return $this->httpWrapper()->exposed_responses;
174  }
175  };
176  }
177 
178  protected function getHandler(
179  string $base_url,
180  InitiatorInterface $initiator
181  ): Handler {
182  $base_url = $this->getURI($base_url);
183  return new class ($initiator, $base_url) extends Handler {
184  public array $exposed_logged_errors = [];
185 
186  public function __construct(
187  protected InitiatorInterface $initiator,
188  protected readonly URI $base_url
189  ) {
190  }
191 
192  protected function logError(string $message): void
193  {
194  $this->exposed_logged_errors[] = $message;
195  }
196  };
197  }
198 
199  public function testSendResponseToRequestAvailable(): void
200  {
201  $initiator = $this->getInitiator(
202  true,
203  'some content'
204  );
205  $handler = $this->getHandler(
206  'some url',
207  $initiator
208  );
209 
210  $handler->sendResponseToRequest();
211 
212  $this->assertCount(1, $initiator->exposeHTTPResponses());
213  $this->assertEquals(
214  ['status' => 200, 'message' => '', 'body' => '<content>some url~!~some content</content>'],
215  $initiator->exposeHTTPResponses()[0] ?? []
216  );
217  }
218 
220  {
221  $initiator = $this->getInitiator(
222  false,
223  'some content'
224  );
225  $handler = $this->getHandler(
226  'some url',
227  $initiator
228  );
229 
230  $handler->sendResponseToRequest();
231 
232  $this->assertCount(1, $initiator->exposeHTTPResponses());
233  $this->assertEquals(
234  ['status' => 404, 'message' => '', 'body' => null],
235  $initiator->exposeHTTPResponses()[0] ?? []
236  );
237  }
238 
240  {
241  $initiator = $this->getInitiator(
242  true,
243  '',
244  true,
245  false,
246  false
247  );
248  $handler = $this->getHandler(
249  'some url',
250  $initiator
251  );
252 
253  $handler->sendResponseToRequest();
254 
255  $this->assertCount(1, $initiator->exposeHTTPResponses());
256  $this->assertEquals(
257  ['status' => 500, 'message' => 'exception message', 'body' => null],
258  $initiator->exposeHTTPResponses()[0] ?? []
259  );
260  $this->assertEquals(
261  ['exception message'],
262  $handler->exposed_logged_errors
263  );
264  }
265 
267  {
268  $initiator = $this->getInitiator(
269  true,
270  '',
271  false,
272  true,
273  false
274  );
275  $handler = $this->getHandler(
276  'some url',
277  $initiator
278  );
279 
280  $handler->sendResponseToRequest();
281 
282  $this->assertCount(1, $initiator->exposeHTTPResponses());
283  $this->assertEquals(
284  ['status' => 500, 'message' => 'thrown error message', 'body' => null],
285  $initiator->exposeHTTPResponses()[0] ?? []
286  );
287  $this->assertEquals(
288  ['thrown error message'],
289  $handler->exposed_logged_errors
290  );
291  }
292 
294  {
295  $initiator = $this->getInitiator(
296  true,
297  '',
298  false,
299  false,
300  true
301  );
302  $handler = $this->getHandler(
303  'some url',
304  $initiator
305  );
306 
307  $handler->sendResponseToRequest();
308 
309  $this->assertCount(1, $initiator->exposeHTTPResponses());
310  $this->assertEquals(
311  ['status' => 500, 'message' => 'triggered error message', 'body' => null],
312  $initiator->exposeHTTPResponses()[0] ?? []
313  );
314  $this->assertEquals(
315  ['triggered error message'],
316  $handler->exposed_logged_errors
317  );
318  }
319 }
getHandler(string $base_url, InitiatorInterface $initiator)
$url
Definition: shib_logout.php:63
getInitiator(bool $activated, string $content, bool $processor_throws_exception=false, bool $processor_throws_error=false, bool $processor_triggers_error=false)
Definition: HandlerTest.php:45
The OAI PMH interface does not use the usual internal services of the MetaData component, as it should be lightweight and decoupled from everything else as much as possible.
__construct()
Constructor setup ILIAS global object public.
Definition: class.ilias.php:76
The scope of this class is split ilias-conform URI&#39;s into components.
Definition: URI.php:34
Processes OAI PMH requests according to https://www.openarchives.org/OAI/openarchivesprotocol.html.
$handler
Definition: oai.php:14