ILIAS  trunk Revision v11.0_alpha-2662-g519ff7d528f
RepositoryAndDataTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
24 
25 class RepositoryAndDataTest extends TestCase
26 {
27  protected function getRepository(
28  int $current_datestamp,
29  array $returns_on_query
31  return new class ($current_datestamp, $returns_on_query) extends DatabaseRepository {
32  public array $exposed_queries = [];
33 
34  public function __construct(
35  protected int $current_datestamp,
36  protected array $returns_on_query
37  ) {
38  }
39 
40  protected function query(string $query): \Generator
41  {
42  $this->exposed_queries[] = $query;
43  yield from $this->returns_on_query;
44  }
45 
46  protected function manipulate(string $query): void
47  {
48  $this->exposed_queries[] = $query;
49  }
50 
51  protected function quoteInteger(int $integer): string
52  {
53  return '~int:' . $integer . '~';
54  }
55 
56  protected function quoteString(string $string): string
57  {
58  return '~string:' . $string . '~';
59  }
60 
61  protected function quoteClob(string $string): string
62  {
63  return '~clob:' . $string . '~';
64  }
65 
66  protected function getCurrentDatestamp(): int
67  {
68  return $this->current_datestamp;
69  }
70  };
71  }
72 
73  protected function assertRecordMatchesArray(RecordInterface $record, array $data): void
74  {
75  $this->assertRecordInfosMatchesArray($record->infos(), $data);
76  $this->assertXmlStringEqualsXmlString(
77  $data['metadata'],
78  $record->metadata()->saveXML()
79  );
80  }
81 
82  protected function assertRecordInfosMatchesArray(RecordInfosInterface $infos, array $data): void
83  {
84  $this->assertSame((int) $data['obj_id'], $infos->objID());
85  $this->assertSame($data['identifier'], $infos->identfifier());
86  $this->assertSame((int) $data['datestamp'], $infos->datestamp()->getTimestamp());
87  }
88 
89  public function testGetRecords(): void
90  {
91  $record_1 = [
92  'obj_id' => '32',
93  'identifier' => 'id32',
94  'datestamp' => '123456',
95  'metadata' => '<content>something</content>'
96  ];
97  $record_2 = [
98  'obj_id' => '456',
99  'identifier' => 'id456',
100  'datestamp' => '9345653',
101  'metadata' => '<content><sub1>hello</sub1><sub2>world</sub2></content>'
102  ];
103  $record_3 = [
104  'obj_id' => '1',
105  'identifier' => 'id1',
106  'datestamp' => '65656565',
107  'metadata' => '<content>something else</content>'
108  ];
109  $repo = $this->getRepository(0, [$record_1, $record_2, $record_3]);
110 
111  $records = iterator_to_array($repo->getRecords());
112 
113  $this->assertSame(
114  ['SELECT * FROM il_meta_oer_exposed ORDER BY obj_id'],
115  $repo->exposed_queries
116  );
117  $this->assertCount(3, $records);
118  $this->assertRecordMatchesArray($records[0], $record_1);
119  $this->assertRecordMatchesArray($records[1], $record_2);
120  $this->assertRecordMatchesArray($records[2], $record_3);
121  }
122 
123  public function testGetRecordsWithFromDate(): void
124  {
125  $record_1 = [
126  'obj_id' => '32',
127  'identifier' => 'id32',
128  'datestamp' => '123456',
129  'metadata' => '<content>something</content>'
130  ];
131  $record_2 = [
132  'obj_id' => '456',
133  'identifier' => 'id456',
134  'datestamp' => '9345653',
135  'metadata' => '<content><sub1>hello</sub1><sub2>world</sub2></content>'
136  ];
137  $repo = $this->getRepository(0, [$record_1, $record_2]);
138 
139  $records = iterator_to_array($repo->getRecords(
140  new \DateTimeImmutable('@1723994')
141  ));
142 
143  $this->assertSame(
144  ['SELECT *' . ' FROM il_meta_oer_exposed WHERE datestamp >= ~int:1723994~ ORDER BY obj_id'],
145  $repo->exposed_queries
146  );
147  $this->assertCount(2, $records);
148  $this->assertRecordMatchesArray($records[0], $record_1);
149  $this->assertRecordMatchesArray($records[1], $record_2);
150  }
151 
152  public function testGetRecordsWithUntilDate(): void
153  {
154  $record_2 = [
155  'obj_id' => '456',
156  'identifier' => 'id456',
157  'datestamp' => '9345653',
158  'metadata' => '<content><sub1>hello</sub1><sub2>world</sub2></content>'
159  ];
160  $record_3 = [
161  'obj_id' => '1',
162  'identifier' => 'id1',
163  'datestamp' => '65656565',
164  'metadata' => '<content>something else</content>'
165  ];
166  $repo = $this->getRepository(0, [$record_2, $record_3]);
167 
168  $records = iterator_to_array($repo->getRecords(
169  null,
170  new \DateTimeImmutable('@1763994')
171  ));
172 
173  $this->assertSame(
174  ['SELECT *' . ' FROM il_meta_oer_exposed WHERE datestamp <= ~int:1763994~ ORDER BY obj_id'],
175  $repo->exposed_queries
176  );
177  $this->assertCount(2, $records);
178  $this->assertRecordMatchesArray($records[0], $record_2);
179  $this->assertRecordMatchesArray($records[1], $record_3);
180  }
181 
182  public function testGetRecordsWithFromAndUntilDates(): void
183  {
184  $record_2 = [
185  'obj_id' => '456',
186  'identifier' => 'id456',
187  'datestamp' => '9345653',
188  'metadata' => '<content><sub1>hello</sub1><sub2>world</sub2></content>'
189  ];
190  $repo = $this->getRepository(0, [$record_2]);
191 
192  $records = iterator_to_array($repo->getRecords(
193  new \DateTimeImmutable('@1723994'),
194  new \DateTimeImmutable('@1763994')
195  ));
196 
197  $this->assertSame(
198  ['SELECT *' . ' FROM il_meta_oer_exposed WHERE datestamp >= ~int:1723994~ AND datestamp <= ~int:1763994~ ORDER BY obj_id'],
199  $repo->exposed_queries
200  );
201  $this->assertCount(1, $records);
202  $this->assertRecordMatchesArray($records[0], $record_2);
203  }
204 
205  public function testGetRecordsWithLimit(): void
206  {
207  $record = [
208  'obj_id' => '456',
209  'identifier' => 'id456',
210  'datestamp' => '9345653',
211  'metadata' => '<content><sub1>hello</sub1><sub2>world</sub2></content>'
212  ];
213  $repo = $this->getRepository(0, [$record]);
214 
215  $records = iterator_to_array($repo->getRecords(
216  null,
217  null,
218  5
219  ));
220 
221  $this->assertSame(
222  ['SELECT *' . ' FROM il_meta_oer_exposed ORDER BY obj_id LIMIT ~int:5~'],
223  $repo->exposed_queries
224  );
225  $this->assertCount(1, $records);
226  $this->assertRecordMatchesArray($records[0], $record);
227  }
228 
229  public function testGetRecordsWithOffset(): void
230  {
231  $record = [
232  'obj_id' => '456',
233  'identifier' => 'id456',
234  'datestamp' => '9345653',
235  'metadata' => '<content><sub1>hello</sub1><sub2>world</sub2></content>'
236  ];
237  $repo = $this->getRepository(0, [$record]);
238 
239  $records = iterator_to_array($repo->getRecords(
240  null,
241  null,
242  null,
243  5
244  ));
245 
246  $this->assertSame(
247  ['SELECT *' . ' FROM il_meta_oer_exposed ORDER BY obj_id LIMIT ~int:' . PHP_INT_MAX . '~ OFFSET ~int:5~'],
248  $repo->exposed_queries
249  );
250  $this->assertCount(1, $records);
251  $this->assertRecordMatchesArray($records[0], $record);
252  }
253 
254  public function testGetRecordsWithLimitAndOffset(): void
255  {
256  $record = [
257  'obj_id' => '456',
258  'identifier' => 'id456',
259  'datestamp' => '9345653',
260  'metadata' => '<content><sub1>hello</sub1><sub2>world</sub2></content>'
261  ];
262  $repo = $this->getRepository(0, [$record]);
263 
264  $records = iterator_to_array($repo->getRecords(
265  null,
266  null,
267  5,
268  10
269  ));
270 
271  $this->assertSame(
272  ['SELECT *' . ' FROM il_meta_oer_exposed ORDER BY obj_id LIMIT ~int:5~ OFFSET ~int:10~'],
273  $repo->exposed_queries
274  );
275  $this->assertCount(1, $records);
276  $this->assertRecordMatchesArray($records[0], $record);
277  }
278 
279  public function testGetRecordInfos(): void
280  {
281  $record_1 = [
282  'obj_id' => '32',
283  'identifier' => 'id32',
284  'datestamp' => '123456'
285  ];
286  $record_2 = [
287  'obj_id' => '456',
288  'identifier' => 'id456',
289  'datestamp' => '9345653'
290  ];
291  $record_3 = [
292  'obj_id' => '1',
293  'identifier' => 'id1',
294  'datestamp' => '65656565'
295  ];
296  $repo = $this->getRepository(0, [$record_1, $record_2, $record_3]);
297 
298  $records = iterator_to_array($repo->getRecordInfos());
299 
300  $this->assertSame(
301  ['SELECT obj_id, identifier, datestamp FROM il_meta_oer_exposed ORDER BY obj_id'],
302  $repo->exposed_queries
303  );
304  $this->assertCount(3, $records);
305  $this->assertRecordInfosMatchesArray($records[0], $record_1);
306  $this->assertRecordInfosMatchesArray($records[1], $record_2);
307  $this->assertRecordInfosMatchesArray($records[2], $record_3);
308  }
309 
310  public function testGetRecordInfosWithFromDate(): void
311  {
312  $record_1 = [
313  'obj_id' => '32',
314  'identifier' => 'id32',
315  'datestamp' => '123456'
316  ];
317  $record_2 = [
318  'obj_id' => '456',
319  'identifier' => 'id456',
320  'datestamp' => '9345653'
321  ];
322  $repo = $this->getRepository(0, [$record_1, $record_2]);
323 
324  $records = iterator_to_array($repo->getRecordInfos(
325  new \DateTimeImmutable('@1723994')
326  ));
327 
328  $this->assertSame(
329  ['SELECT obj_id, identifier, datestamp' . ' FROM il_meta_oer_exposed WHERE datestamp >= ~int:1723994~ ORDER BY obj_id'],
330  $repo->exposed_queries
331  );
332  $this->assertCount(2, $records);
333  $this->assertRecordInfosMatchesArray($records[0], $record_1);
334  $this->assertRecordInfosMatchesArray($records[1], $record_2);
335  }
336 
337  public function testGetRecordInfosWithUntilDate(): void
338  {
339  $record_2 = [
340  'obj_id' => '456',
341  'identifier' => 'id456',
342  'datestamp' => '9345653'
343  ];
344  $record_3 = [
345  'obj_id' => '1',
346  'identifier' => 'id1',
347  'datestamp' => '65656565'
348  ];
349  $repo = $this->getRepository(0, [$record_2, $record_3]);
350 
351  $records = iterator_to_array($repo->getRecordInfos(
352  null,
353  new \DateTimeImmutable('@1763994')
354  ));
355 
356  $this->assertSame(
357  ['SELECT obj_id, identifier, datestamp' . ' FROM il_meta_oer_exposed WHERE datestamp <= ~int:1763994~ ORDER BY obj_id'],
358  $repo->exposed_queries
359  );
360  $this->assertCount(2, $records);
361  $this->assertRecordInfosMatchesArray($records[0], $record_2);
362  $this->assertRecordInfosMatchesArray($records[1], $record_3);
363  }
364 
366  {
367  $record_2 = [
368  'obj_id' => '456',
369  'identifier' => 'id456',
370  'datestamp' => '9345653'
371  ];
372  $repo = $this->getRepository(0, [$record_2]);
373 
374  $records = iterator_to_array($repo->getRecordInfos(
375  new \DateTimeImmutable('@1723994'),
376  new \DateTimeImmutable('@1763994')
377  ));
378 
379  $this->assertSame(
380  ['SELECT obj_id, identifier, datestamp' . ' FROM il_meta_oer_exposed WHERE datestamp >= ~int:1723994~ AND datestamp <= ~int:1763994~ ORDER BY obj_id'],
381  $repo->exposed_queries
382  );
383  $this->assertCount(1, $records);
384  $this->assertRecordInfosMatchesArray($records[0], $record_2);
385  }
386 
387  public function testGetRecordInfosWithLimit(): void
388  {
389  $record = [
390  'obj_id' => '456',
391  'identifier' => 'id456',
392  'datestamp' => '9345653'
393  ];
394  $repo = $this->getRepository(0, [$record]);
395 
396  $records = iterator_to_array($repo->getRecordInfos(
397  null,
398  null,
399  5
400  ));
401 
402  $this->assertSame(
403  ['SELECT obj_id, identifier, datestamp' . ' FROM il_meta_oer_exposed ORDER BY obj_id LIMIT ~int:5~'],
404  $repo->exposed_queries
405  );
406  $this->assertCount(1, $records);
407  $this->assertRecordInfosMatchesArray($records[0], $record);
408  }
409 
410  public function testGetRecordInfosWithOffset(): void
411  {
412  $record = [
413  'obj_id' => '456',
414  'identifier' => 'id456',
415  'datestamp' => '9345653'
416  ];
417  $repo = $this->getRepository(0, [$record]);
418 
419  $records = iterator_to_array($repo->getRecordInfos(
420  null,
421  null,
422  null,
423  5
424  ));
425 
426  $this->assertSame(
427  ['SELECT obj_id, identifier, datestamp' . ' FROM il_meta_oer_exposed ORDER BY obj_id LIMIT ~int:' . PHP_INT_MAX . '~ OFFSET ~int:5~'],
428  $repo->exposed_queries
429  );
430  $this->assertCount(1, $records);
431  $this->assertRecordInfosMatchesArray($records[0], $record);
432  }
433 
434  public function testGetRecordInfosWithLimitAndOffset(): void
435  {
436  $record = [
437  'obj_id' => '456',
438  'identifier' => 'id456',
439  'datestamp' => '9345653'
440  ];
441  $repo = $this->getRepository(0, [$record]);
442 
443  $records = iterator_to_array($repo->getRecordInfos(
444  null,
445  null,
446  5,
447  10
448  ));
449 
450  $this->assertSame(
451  ['SELECT obj_id, identifier, datestamp' . ' FROM il_meta_oer_exposed ORDER BY obj_id LIMIT ~int:5~ OFFSET ~int:10~'],
452  $repo->exposed_queries
453  );
454  $this->assertCount(1, $records);
455  $this->assertRecordInfosMatchesArray($records[0], $record);
456  }
457 
458  public function testGetRecordCount(): void
459  {
460  $repo = $this->getRepository(0, [['num' => 4]]);
461 
462  $count = $repo->getRecordCount();
463 
464  $this->assertSame(
465  ['SELECT COUNT(*) AS num FROM il_meta_oer_exposed'],
466  $repo->exposed_queries
467  );
468  $this->assertSame(4, $count);
469  }
470 
471  public function testGetRecordCountWithFromDate(): void
472  {
473  $repo = $this->getRepository(0, [['num' => 3]]);
474 
475  $count = $repo->getRecordCount(
476  new \DateTimeImmutable('@1723994')
477  );
478 
479  $this->assertSame(
480  ['SELECT' . ' COUNT(*) AS num FROM il_meta_oer_exposed WHERE datestamp >= ~int:1723994~'],
481  $repo->exposed_queries
482  );
483  $this->assertSame(3, $count);
484  }
485 
486  public function testGetRecordCountWithUntilDate(): void
487  {
488  $repo = $this->getRepository(0, [['num' => 3]]);
489 
490  $count = $repo->getRecordCount(
491  null,
492  new \DateTimeImmutable('@1763994')
493  );
494 
495  $this->assertSame(
496  ['SELECT' . ' COUNT(*) AS num FROM il_meta_oer_exposed WHERE datestamp <= ~int:1763994~'],
497  $repo->exposed_queries
498  );
499  $this->assertSame(3, $count);
500  }
501 
503  {
504  $repo = $this->getRepository(0, [['num' => 2]]);
505 
506  $count = $repo->getRecordCount(
507  new \DateTimeImmutable('@1723994'),
508  new \DateTimeImmutable('@1763994')
509  );
510 
511  $this->assertSame(
512  ['SELECT' . ' COUNT(*) AS num FROM il_meta_oer_exposed WHERE datestamp >= ~int:1723994~ AND datestamp <= ~int:1763994~'],
513  $repo->exposed_queries
514  );
515  $this->assertSame(2, $count);
516  }
517 
518  public function testGetEarliestDatestamp(): void
519  {
520  $repo = $this->getRepository(0, [['earliest' => '1795857']]);
521 
522  $earliest = $repo->getEarliestDatestamp();
523 
524  $this->assertSame(
525  ['SELECT MIN(datestamp) AS earliest FROM il_meta_oer_exposed'],
526  $repo->exposed_queries
527  );
528  $this->assertSame(1795857, $earliest->getTimestamp());
529  }
530 
531  public function testGetRecordByIdentifier(): void
532  {
533  $record = [
534  'obj_id' => '456',
535  'identifier' => 'id456',
536  'datestamp' => '9345653',
537  'metadata' => '<content><sub1>hello</sub1><sub2>world</sub2></content>'
538  ];
539  $repo = $this->getRepository(0, [$record]);
540 
541  $res = $repo->getRecordByIdentifier('id456');
542 
543  $this->assertSame(
544  ['SELECT * FROM il_meta_oer_exposed WHERE identifier = ~string:id456~'],
545  $repo->exposed_queries
546  );
547  $this->assertNotNull($res);
548  $this->assertRecordMatchesArray($res, $record);
549  }
550 
551  public function testGetRecordByIdentifierNotFound(): void
552  {
553  $repo = $this->getRepository(0, []);
554 
555  $res = $repo->getRecordByIdentifier('id456');
556 
557  $this->assertSame(
558  ['SELECT * FROM il_meta_oer_exposed WHERE identifier = ~string:id456~'],
559  $repo->exposed_queries
560  );
561  $this->assertNull($res);
562  }
563 
565  {
566  $repo = $this->getRepository(0, [['num' => 1]]);
567 
568  $exists = $repo->doesRecordWithIdentifierExist('some id');
569 
570  $this->assertSame(
571  ['SELECT COUNT(*) AS num FROM il_meta_oer_exposed WHERE identifier = ~string:some id~'],
572  $repo->exposed_queries
573  );
574  $this->assertTrue($exists);
575  }
576 
578  {
579  $repo = $this->getRepository(0, [['num' => 0]]);
580 
581  $exists = $repo->doesRecordWithIdentifierExist('some id');
582 
583  $this->assertSame(
584  ['SELECT COUNT(*) AS num FROM il_meta_oer_exposed WHERE identifier = ~string:some id~'],
585  $repo->exposed_queries
586  );
587  $this->assertFalse($exists);
588  }
589 
590  public function testDoesRecordExistForObjIDTrue(): void
591  {
592  $repo = $this->getRepository(0, [['num' => 1]]);
593 
594  $exists = $repo->doesRecordExistForObjID(43);
595 
596  $this->assertSame(
597  ['SELECT ' . 'COUNT(*) AS num FROM il_meta_oer_exposed WHERE obj_id = ~int:43~'],
598  $repo->exposed_queries
599  );
600  $this->assertTrue($exists);
601  }
602 
603  public function testDoesRecordExistForObjIDFalse(): void
604  {
605  $repo = $this->getRepository(0, [['num' => 0]]);
606 
607  $exists = $repo->doesRecordExistForObjID(43);
608 
609  $this->assertSame(
610  ['SELECT ' . 'COUNT(*) AS num FROM il_meta_oer_exposed WHERE obj_id = ~int:43~'],
611  $repo->exposed_queries
612  );
613  $this->assertFalse($exists);
614  }
615 
616  public function testCreateRecord(): void
617  {
618  $xml = new \DOMDocument();
619  $xml->loadXML('<content><sub1>hello</sub1><sub2>world</sub2></content>');
620 
621  $repo = $this->getRepository(17646362, []);
622 
623  $repo->createRecord(
624  32,
625  'id32',
626  $xml
627  );
628 
629  $this->assertSame(
630  [
631  'INSERT INTO il_meta_oer_exposed (obj_id, identifier, datestamp, metadata) VALUES (' .
632  '~int:32~, ~string:id32~, ~int:17646362~, ~clob:' . $xml->saveXML() . '~)'
633  ],
634  $repo->exposed_queries
635  );
636  }
637 
638  public function testUpdateRecord(): void
639  {
640  $xml = new \DOMDocument();
641  $xml->loadXML('<content><sub1>hello</sub1><sub2>world</sub2></content>');
642 
643  $repo = $this->getRepository(17646362, []);
644 
645  $repo->updateRecord(
646  32,
647  $xml
648  );
649 
650  $this->assertSame(
651  [
652  'UPDATE il_meta_oer_exposed SET metadata = ~clob:' . $xml->saveXML() . '~, ' .
653  'datestamp = ~int:17646362~ WHERE obj_id = ~int:32~'
654  ],
655  $repo->exposed_queries
656  );
657  }
658 
659  public function testDeleteRecord(): void
660  {
661  $repo = $this->getRepository(0, []);
662 
663  $repo->deleteRecord(32);
664 
665  $this->assertSame(
666  ['DELETE ' . 'FROM il_meta_oer_exposed WHERE obj_id = ~int:32~'],
667  $repo->exposed_queries
668  );
669  }
670 }
assertRecordInfosMatchesArray(RecordInfosInterface $infos, array $data)
$res
Definition: ltiservices.php:66
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
__construct()
Constructor setup ILIAS global object public.
Definition: class.ilias.php:76
getRepository(int $current_datestamp, array $returns_on_query)