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