ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
DatabaseRepositoryTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\MetaData\Copyright;
22 
25 use ILIAS\Data\URI;
29 
30 class DatabaseRepositoryTest extends TestCase
31 {
32  protected const int NEXT_ID = 123;
33 
34  protected const array ROW_DEFAULT = [
35  'entry_id' => 25,
36  'title' => 'entry default',
37  'description' => 'description default',
38  'is_default' => 1,
39  'outdated' => 0,
40  'position' => 0,
41  'full_name' => 'full name default',
42  'link' => 'link default',
43  'image_link' => 'image link default',
44  'image_file' => '',
45  'alt_text' => 'alt text default'
46  ];
47 
48  protected const array ROW_1 = [
49  'entry_id' => 5,
50  'title' => 'entry 1',
51  'description' => 'description 1',
52  'is_default' => 0,
53  'outdated' => 1,
54  'position' => 1,
55  'full_name' => 'full name 1',
56  'link' => 'link 1',
57  'image_link' => 'image link 1',
58  'image_file' => '',
59  'alt_text' => 'alt text 1'
60  ];
61 
62  protected const array ROW_2 = [
63  'entry_id' => 67,
64  'title' => 'entry 2',
65  'description' => '',
66  'is_default' => 0,
67  'outdated' => 0,
68  'position' => 0,
69  'full_name' => 'full name 2',
70  'link' => '',
71  'image_link' => '',
72  'image_file' => 'image file 2',
73  'alt_text' => 'alt text 2'
74  ];
75 
76  protected function getDBWrapper(array ...$query_results): WrapperInterface
77  {
78  return new class (self::NEXT_ID, $query_results) extends NullWrapper {
79  public array $exposed_tables;
80  public array $exposed_queries;
81  public array $exposed_manipulates;
82  public array $exposed_values;
83  public array $exposed_wheres;
84 
85  public function __construct(
86  protected int $next_id,
87  protected array $query_results
88  ) {
89  }
90 
91  public function nextID(string $table): int
92  {
93  $this->exposed_tables[] = $table;
94  return $this->next_id;
95  }
96 
97  public function quoteInteger(int $integer): string
98  {
99  return '~' . $integer . '~';
100  }
101 
102  public function query(string $query): \Generator
103  {
104  $this->exposed_queries[] = $query;
105  yield from $this->query_results;
106  }
107 
108  public function manipulate(string $query): void
109  {
110  $this->exposed_manipulates[] = $query;
111  }
112 
113  public function update(string $table, array $values, array $where): void
114  {
115  $this->exposed_tables[] = $table;
116  $this->exposed_values[] = $values;
117  $this->exposed_wheres[] = $where;
118  }
119 
120  public function insert(string $table, array $values): void
121  {
122  $this->exposed_tables[] = $table;
123  $this->exposed_values[] = $values;
124  }
125  };
126  }
127 
128  protected function getMockURI(): URI|MockObject
129  {
130  return $this->createMock(URI::class);
131  }
132 
133  protected function getRepo(WrapperInterface $wrapper): DatabaseRepository
134  {
135  $uri = $this->getMockURI();
136 
137  return new class ($wrapper, $uri) extends DatabaseRepository {
138  public function __construct(
139  WrapperInterface $wrapper,
140  protected URI|MockObject $uri
141  ) {
142  parent::__construct($wrapper);
143  }
144 
145  protected function getURI(string $uri): URI
146  {
147  $clone = clone $this->uri;
148  $clone->method('__toString')->willReturn($uri);
149  return $clone;
150  }
151  };
152  }
153 
154  public function testGetEntry(): void
155  {
156  $db = $this->getDBWrapper(self::ROW_1);
157  $repo = $this->getRepo($db);
158 
159  $entry = $repo->getEntry(5);
160  $this->assertSame(
161  ['SELECT * ' . 'FROM il_md_cpr_selections WHERE entry_id = ~5~'],
162  $db->exposed_queries
163  );
164  $this->assertSame(self::ROW_1['entry_id'], $entry->id());
165  $this->assertSame(self::ROW_1['title'], $entry->title());
166  $this->assertSame(self::ROW_1['description'], $entry->description());
167  $this->assertSame((bool) self::ROW_1['is_default'], $entry->isDefault());
168  $this->assertSame((bool) self::ROW_1['outdated'], $entry->isOutdated());
169  $this->assertSame(self::ROW_1['position'], $entry->position());
170  $this->assertSame(self::ROW_1['full_name'], $entry->copyrightData()->fullName());
171  $this->assertSame(self::ROW_1['link'], (string) $entry->copyrightData()->link());
172  $this->assertSame(self::ROW_1['image_link'], (string) $entry->copyrightData()->imageLink());
173  $this->assertSame(self::ROW_1['image_file'], $entry->copyrightData()->imageFile());
174  $this->assertSame(self::ROW_1['alt_text'], $entry->copyrightData()->altText());
175  $this->assertSame((bool) self::ROW_1['is_default'], $entry->copyrightData()->fallBackToDefaultImage());
176  }
177 
178  public function testGetEntryNoLinks(): void
179  {
180  $db = $this->getDBWrapper(self::ROW_2);
181  $repo = $this->getRepo($db);
182 
183  $entry = $repo->getEntry(67);
184  $this->assertSame(
185  ['SELECT * ' . 'FROM il_md_cpr_selections WHERE entry_id = ~67~'],
186  $db->exposed_queries
187  );
188  $this->assertNull($entry->copyrightData()->link());
189  $this->assertNull($entry->copyrightData()->imageLink());
190  }
191 
192  public function testGetEntryNoneFound(): void
193  {
194  $db = $this->getDBWrapper();
195  $repo = $this->getRepo($db);
196 
197  $this->assertEquals(new NullEntry(), $repo->getEntry(5));
198  $this->assertSame(
199  ['SELECT * ' . 'FROM il_md_cpr_selections WHERE entry_id = ~5~'],
200  $db->exposed_queries
201  );
202  }
203 
204  public function testGetDefaultEntry(): void
205  {
206  $db = $this->getDBWrapper(self::ROW_DEFAULT);
207  $repo = $this->getRepo($db);
208 
209  $this->assertInstanceOf(EntryInterface::class, $repo->getDefaultEntry());
210  $this->assertSame(
211  ['SELECT * FROM il_md_cpr_selections WHERE is_default = 1'],
212  $db->exposed_queries
213  );
214  }
215 
216  public function testGetAllEntries(): void
217  {
218  $db = $this->getDBWrapper(
219  self::ROW_DEFAULT,
220  self::ROW_2,
221  self::ROW_1
222  );
223  $repo = $this->getRepo($db);
224 
225  $res = iterator_to_array($repo->getAllEntries());
226  $this->assertSame(
227  ['SELECT * FROM il_md_cpr_selections
228  ORDER BY is_default DESC, position ASC'],
229  $db->exposed_queries
230  );
231  $this->assertCount(3, $res);
232  $this->assertInstanceOf(EntryInterface::class, $res[0]);
233  $this->assertInstanceOf(EntryInterface::class, $res[1]);
234  $this->assertInstanceOf(EntryInterface::class, $res[2]);
235  }
236 
237  public function testGetActiveEntries(): void
238  {
239  $db = $this->getDBWrapper(
240  self::ROW_DEFAULT,
241  self::ROW_2
242  );
243  $repo = $this->getRepo($db);
244 
245  $res = iterator_to_array($repo->getActiveEntries());
246  $this->assertSame(
247  ['SELECT * FROM il_md_cpr_selections WHERE outdated = 0
248  ORDER BY is_default DESC, position ASC'],
249  $db->exposed_queries
250  );
251  $this->assertCount(2, $res);
252  $this->assertInstanceOf(EntryInterface::class, $res[0]);
253  $this->assertInstanceOf(EntryInterface::class, $res[1]);
254  }
255 
256  public function testDeleteEntry(): void
257  {
258  $db = $this->getDBWrapper();
259  $repo = $this->getRepo($db);
260 
261  $repo->deleteEntry(5);
262  $this->assertSame(
263  ['DELETE ' . 'FROM il_md_cpr_selections WHERE entry_id = ~5~'],
264  $db->exposed_manipulates
265  );
266  }
267 
268  public function testCreateWithLinkImage(): void
269  {
270  $db = $this->getDBWrapper(['max' => 5]);
271  $uri = $this->getMockURI();
272  $uri->method('__toString')->willReturn('new link');
273  $img_uri = $this->getMockURI();
274  $img_uri->method('__toString')->willReturn('new image link');
275  $repo = $this->getRepo($db);
276 
277  $repo->createEntry(
278  'new title',
279  'new description',
280  true,
281  'new full name',
282  $uri,
283  $img_uri,
284  'new alt text'
285  );
286  $this->assertSame(
287  ['SELECT MAX(position) AS max FROM il_md_cpr_selections WHERE is_default = 0'],
288  $db->exposed_queries
289  );
290  $this->assertSame(
291  [
292  'il_md_cpr_selections',
293  'il_md_cpr_selections'
294  ],
295  $db->exposed_tables
296  );
297  $this->assertSame(
298  [[
299  'entry_id' => [\ilDBConstants::T_INTEGER, 123],
300  'title' => [\ilDBConstants::T_TEXT, 'new title'],
301  'description' => [\ilDBConstants::T_TEXT, 'new description'],
302  'is_default' => [\ilDBConstants::T_INTEGER, 0],
303  'outdated' => [\ilDBConstants::T_INTEGER, 1],
304  'position' => [\ilDBConstants::T_INTEGER, 6],
305  'full_name' => [\ilDBConstants::T_TEXT, 'new full name'],
306  'link' => [\ilDBConstants::T_TEXT, 'new link'],
307  'image_link' => [\ilDBConstants::T_TEXT, 'new image link'],
308  'image_file' => [\ilDBConstants::T_TEXT, ''],
309  'alt_text' => [\ilDBConstants::T_TEXT, 'new alt text'],
310  'migrated' => [\ilDBConstants::T_INTEGER, 1]
311  ]],
312  $db->exposed_values
313  );
314  }
315 
316  public function testCreateWithFileImage(): void
317  {
318  $db = $this->getDBWrapper(['max' => 5]);
319  $uri = $this->getMockURI();
320  $uri->method('__toString')->willReturn('new link');
321  $repo = $this->getRepo($db);
322 
323  $repo->createEntry(
324  'new title',
325  'new description',
326  false,
327  'new full name',
328  null,
329  'new image file',
330  'new alt text'
331  );
332  $this->assertSame(
333  ['SELECT MAX(position) AS max FROM il_md_cpr_selections WHERE is_default = 0'],
334  $db->exposed_queries
335  );
336  $this->assertSame(
337  [
338  'il_md_cpr_selections',
339  'il_md_cpr_selections'
340  ],
341  $db->exposed_tables
342  );
343  $this->assertSame(
344  [[
345  'entry_id' => [\ilDBConstants::T_INTEGER, 123],
346  'title' => [\ilDBConstants::T_TEXT, 'new title'],
347  'description' => [\ilDBConstants::T_TEXT, 'new description'],
348  'is_default' => [\ilDBConstants::T_INTEGER, 0],
349  'outdated' => [\ilDBConstants::T_INTEGER, 0],
350  'position' => [\ilDBConstants::T_INTEGER, 6],
351  'full_name' => [\ilDBConstants::T_TEXT, 'new full name'],
352  'link' => [\ilDBConstants::T_TEXT, ''],
353  'image_link' => [\ilDBConstants::T_TEXT, ''],
354  'image_file' => [\ilDBConstants::T_TEXT, 'new image file'],
355  'alt_text' => [\ilDBConstants::T_TEXT, 'new alt text'],
356  'migrated' => [\ilDBConstants::T_INTEGER, 1]
357  ]],
358  $db->exposed_values
359  );
360  }
361 
362  public function testCreateEmptyTitleException(): void
363  {
364  $repo = $this->getRepo($this->getDBWrapper());
365 
366  $this->expectException(\ilMDCopyrightException::class);
367  $repo->createEntry('');
368  }
369 
370  public function testUpdateWithLinkImage(): void
371  {
372  $db = $this->getDBWrapper();
373  $uri = $this->getMockURI();
374  $uri->method('__toString')->willReturn('new link');
375  $img_uri = $this->getMockURI();
376  $img_uri->method('__toString')->willReturn('new image link');
377  $repo = $this->getRepo($db);
378 
379  $repo->updateEntry(
380  9,
381  'new title',
382  'new description',
383  true,
384  'new full name',
385  $uri,
386  $img_uri,
387  'new alt text'
388  );
389  $this->assertSame(
390  ['il_md_cpr_selections'],
391  $db->exposed_tables
392  );
393  $this->assertSame(
394  [[
395  'title' => [\ilDBConstants::T_TEXT, 'new title'],
396  'description' => [\ilDBConstants::T_TEXT, 'new description'],
397  'outdated' => [\ilDBConstants::T_INTEGER, 1],
398  'full_name' => [\ilDBConstants::T_TEXT, 'new full name'],
399  'link' => [\ilDBConstants::T_TEXT, 'new link'],
400  'image_link' => [\ilDBConstants::T_TEXT, 'new image link'],
401  'image_file' => [\ilDBConstants::T_TEXT, ''],
402  'alt_text' => [\ilDBConstants::T_TEXT, 'new alt text']
403  ]],
404  $db->exposed_values
405  );
406  $this->assertSame(
407  [['entry_id' => [\ilDBConstants::T_INTEGER, 9]]],
408  $db->exposed_wheres
409  );
410  }
411 
412  public function testUpdateWithFileImage(): void
413  {
414  $db = $this->getDBWrapper();
415  $uri = $this->getMockURI();
416  $uri->method('__toString')->willReturn('new link');
417  $img_uri = $this->getMockURI();
418  $img_uri->method('__toString')->willReturn('new image link');
419 
420  $repo = $this->getRepo($db);
421  $repo->updateEntry(
422  9,
423  'new title',
424  'new description',
425  false,
426  'new full name',
427  $uri,
428  'new image file',
429  'new alt text'
430  );
431  $this->assertSame(
432  ['il_md_cpr_selections'],
433  $db->exposed_tables
434  );
435  $this->assertSame(
436  [[
437  'title' => [\ilDBConstants::T_TEXT, 'new title'],
438  'description' => [\ilDBConstants::T_TEXT, 'new description'],
439  'outdated' => [\ilDBConstants::T_INTEGER, 0],
440  'full_name' => [\ilDBConstants::T_TEXT, 'new full name'],
441  'link' => [\ilDBConstants::T_TEXT, 'new link'],
442  'image_link' => [\ilDBConstants::T_TEXT, ''],
443  'image_file' => [\ilDBConstants::T_TEXT, 'new image file'],
444  'alt_text' => [\ilDBConstants::T_TEXT, 'new alt text']
445  ]],
446  $db->exposed_values
447  );
448  $this->assertSame(
449  [['entry_id' => [\ilDBConstants::T_INTEGER, 9]]],
450  $db->exposed_wheres
451  );
452  }
453 
454  public function testUpdateEmptyTitleException(): void
455  {
456  $repo = $this->getRepo($this->getDBWrapper());
457 
458  $this->expectException(\ilMDCopyrightException::class);
459  $repo->updateEntry(21, '');
460  }
461 
462  public function testReorderEntries(): void
463  {
464  $db = $this->getDBWrapper(['entry_id' => 5]);
465  $repo = $this->getRepo($db);
466 
467  $repo->reorderEntries(7, 5, 99, 1);
468  $this->assertSame(
469  ['SELECT entry_id FROM il_md_cpr_selections WHERE is_default = 1'],
470  $db->exposed_queries
471  );
472  $this->assertSame(
473  [
474  'il_md_cpr_selections',
475  'il_md_cpr_selections',
476  'il_md_cpr_selections'
477  ],
478  $db->exposed_tables,
479  );
480  $this->assertSame(
481  [
482  ['position' => [\ilDBConstants::T_INTEGER, 0]],
483  ['position' => [\ilDBConstants::T_INTEGER, 1]],
484  ['position' => [\ilDBConstants::T_INTEGER, 2]]
485  ],
486  $db->exposed_values
487  );
488  $this->assertSame(
489  [
490  ['entry_id' => [\ilDBConstants::T_INTEGER, 7]],
491  ['entry_id' => [\ilDBConstants::T_INTEGER, 99]],
492  ['entry_id' => [\ilDBConstants::T_INTEGER, 1]]
493  ],
494  $db->exposed_wheres
495  );
496  }
497 }
$res
Definition: ltiservices.php:66
const array const array const array getDBWrapper(array ... $query_results)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
if($request_wrapper->has('ui_mainbar')) getURI()
Definition: ui_mainbar.php:301
__construct()
Constructor setup ILIAS global object public.
Definition: class.ilias.php:76
__construct(Container $dic, ilPlugin $plugin)