ILIAS  trunk Revision v11.0_alpha-1713-gd8962da2f67
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
ilUserCertificateRepositoryTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
27 {
28  public function testSaveOfUserCertificateToDatabase(): void
29  {
30  $database = $this->createMock(ilDBInterface::class);
31 
32  $database->method('nextId')
33  ->willReturn(141);
34 
35  $database->method('insert')->with(
36  'il_cert_user_cert',
37  [
38  'id' => ['integer', 141],
39  'pattern_certificate_id' => ['integer', 1],
40  'obj_id' => ['integer', 20],
41  'obj_type' => ['text', 'crs'],
42  'usr_id' => ['integer', 400],
43  'user_name' => ['text', 'Niels Theen'],
44  'acquired_timestamp' => ['integer', 123_456_789],
45  'certificate_content' => ['clob', '<xml>Some Content</xml>'],
46  'template_values' => ['clob', '[]'],
47  'valid_until' => ['integer', null],
48  'version' => ['integer', 1],
49  'ilias_version' => ['text', 'v5.4.0'],
50  'currently_active' => ['integer', true],
51  'background_image_path' => ['text', '/some/where/background.jpg'],
52  'tile_image_path' => ['text', '/some/where/tile_image.svg'],
53  'background_image_ident' => ['text', '-'],
54  'tile_image_ident' => ['text', '-'],
55  'certificate_id' => ['text', '11111111-2222-3333-4444-555555555555'],
56  ]
57  );
58 
59  $database->method('tableColumnExists')->willReturnMap(
60  [
61  ['il_cert_user_cert', 'background_image_path', true],
62  ['il_cert_user_cert', 'tile_image_path', true],
63  ]
64  );
65 
66  $logger = $this->getMockBuilder(ilLogger::class)
67  ->disableOriginalConstructor()
68  ->getMock();
69 
70  $logger->expects($this->atLeastOnce())
71  ->method('debug');
72 
73  $repository = new ilUserCertificateRepository(
74  $database,
75  $logger,
76  'someDefaultTitle'
77  );
78 
79  $userCertificate = new ilUserCertificate(
80  1,
81  20,
82  'crs',
83  400,
84  'Niels Theen',
85  123_456_789,
86  '<xml>Some Content</xml>',
87  '[]',
88  null,
89  1,
90  'v5.4.0',
91  true,
92  new CertificateId('11111111-2222-3333-4444-555555555555'),
93  '/some/where/background.jpg',
94  '/some/where/tile_image.svg',
95  '-',
96  '-',
97  null
98  );
99 
100  $repository->save($userCertificate);
101  }
102 
103  public function testFetchAllActiveCertificateForUser(): void
104  {
105  $database = $this->createMock(ilDBInterface::class);
106 
107  $database->method('nextId')
108  ->willReturn(141);
109 
110  $database->method('fetchAssoc')->willReturnOnConsecutiveCalls(
111  [
112  'id' => 141,
113  'pattern_certificate_id' => 1,
114  'obj_id' => 20,
115  'obj_type' => 'crs',
116  'usr_id' => 400,
117  'user_name' => 'Niels Theen',
118  'acquired_timestamp' => 123_456_789,
119  'certificate_content' => '<xml>Some Content</xml>',
120  'template_values' => '[]',
121  'valid_until' => null,
122  'version' => 1,
123  'ilias_version' => 'v5.4.0',
124  'currently_active' => true,
125  'background_image_path' => '/some/where/background.jpg',
126  'tile_image_path' => '/some/where/tile_image.svg',
127  'background_image_ident' => '-',
128  'tile_image_ident' => '-',
129  'title' => 'Some Title',
130  'certificate_id' => '11111111-2222-3333-4444-555555555555'
131  ],
132  [
133  'id' => 142,
134  'pattern_certificate_id' => 5,
135  'obj_id' => 3123,
136  'obj_type' => 'tst',
137  'usr_id' => 400,
138  'user_name' => 'Niels Theen',
139  'acquired_timestamp' => 987_654_321,
140  'certificate_content' => '<xml>Some Other Content</xml>',
141  'template_values' => '[]',
142  'valid_until' => null,
143  'version' => 1,
144  'ilias_version' => 'v5.3.0',
145  'currently_active' => true,
146  'background_image_path' => '/some/where/else/background.jpg',
147  'tile_image_path' => '/some/where/tile_image.svg',
148  'background_image_ident' => '-',
149  'tile_image_ident' => '-',
150  'title' => 'Someother Title',
151  'certificate_id' => '11111111-2222-3333-4444-555555555555'
152  ],
153  null
154  );
155 
156  $logger = $this->getMockBuilder(ilLogger::class)
157  ->disableOriginalConstructor()
158  ->getMock();
159 
160  $logger->expects($this->atLeastOnce())
161  ->method('debug');
162 
163  $repository = new ilUserCertificateRepository(
164  $database,
165  $logger,
166  'someDefaultTitle'
167  );
168 
169  $results = $repository->fetchActiveCertificates(400);
170 
171  $this->assertSame(141, $results[0]->getUserCertificate()->getId());
172  $this->assertSame(142, $results[1]->getUserCertificate()->getId());
173  }
174 
176  {
177  $database = $this->createMock(ilDBInterface::class);
178 
179  $database->method('nextId')
180  ->willReturn(141);
181 
182  $database->method('fetchAssoc')->willReturnOnConsecutiveCalls(
183  [
184  'id' => 141,
185  'pattern_certificate_id' => 1,
186  'obj_id' => 20,
187  'obj_type' => 'crs',
188  'usr_id' => 400,
189  'user_name' => 'Niels Theen',
190  'acquired_timestamp' => 123_456_789,
191  'certificate_content' => '<xml>Some Content</xml>',
192  'template_values' => '[]',
193  'valid_until' => null,
194  'version' => 1,
195  'ilias_version' => 'v5.4.0',
196  'currently_active' => true,
197  'background_image_path' => '/some/where/background.jpg',
198  'tile_image_path' => '/some/where/tile_image.svg',
199  'background_image_ident' => '-',
200  'tile_image_ident' => '-',
201  'certificate_id' => '11111111-2222-3333-4444-555555555555'
202  ],
203  [
204  'id' => 142,
205  'pattern_certificate_id' => 5,
206  'obj_id' => 20,
207  'obj_type' => 'tst',
208  'usr_id' => 400,
209  'user_name' => 'Niels Theen',
210  'acquired_timestamp' => 987_654_321,
211  'certificate_content' => '<xml>Some Other Content</xml>',
212  'template_values' => '[]',
213  'valid_until' => null,
214  'version' => 1,
215  'ilias_version' => 'v5.3.0',
216  'currently_active' => true,
217  'background_image_path' => '/some/where/else/background.jpg',
218  'tile_image_path' => '/some/where/tile_image.svg',
219  'background_image_ident' => '-',
220  'tile_image_ident' => '-',
221  'certificate_id' => '11111111-2222-3333-4444-555555555555'
222  ]
223  );
224 
225  $logger = $this->getMockBuilder(ilLogger::class)
226  ->disableOriginalConstructor()
227  ->getMock();
228 
229  $logger->expects($this->atLeastOnce())
230  ->method('debug');
231 
232  $repository = new ilUserCertificateRepository(
233  $database,
234  $logger,
235  'someDefaultTitle'
236  );
237 
238  $result = $repository->fetchActiveCertificate(400, 20);
239 
240  $this->assertSame(141, $result->getId());
241  }
242 
247  {
248  $this->expectException(ilException::class);
249 
250  $database = $this->createMock(ilDBInterface::class);
251 
252  $database->method('nextId')
253  ->willReturn(141);
254 
255  $database->method('fetchAssoc')->willReturn([]);
256 
257  $logger = $this->getMockBuilder(ilLogger::class)
258  ->disableOriginalConstructor()
259  ->getMock();
260 
261  $logger->expects($this->atLeastOnce())
262  ->method('debug');
263 
264  $repository = new ilUserCertificateRepository($database, $logger, 'someDefaultTitle');
265 
266  $repository->fetchActiveCertificate(400, 20);
267 
268  $this->fail('Should never happen. Certificate Found?');
269  }
270 
271  public function testFetchActiveCertificatesByType(): void
272  {
273  $database = $this->createMock(ilDBInterface::class);
274 
275  $database->method('nextId')
276  ->willReturn(141);
277 
278  $database->method('fetchAssoc')->willReturnOnConsecutiveCalls(
279  [
280  'id' => 141,
281  'pattern_certificate_id' => 1,
282  'obj_id' => 20,
283  'obj_type' => 'crs',
284  'usr_id' => 400,
285  'user_name' => 'Niels Theen',
286  'acquired_timestamp' => 123_456_789,
287  'certificate_content' => '<xml>Some Content</xml>',
288  'template_values' => '[]',
289  'valid_until' => null,
290  'version' => 1,
291  'ilias_version' => 'v5.4.0',
292  'currently_active' => true,
293  'background_image_path' => '/some/where/background.jpg',
294  'tile_image_path' => '/some/where/else/tile_image.svg',
295  'background_image_ident' => '-',
296  'tile_image_ident' => '-',
297  'title' => 'SomeTitle',
298  'someDescription' => 'SomeDescription',
299  'certificate_id' => '11111111-2222-3333-4444-555555555555'
300  ],
301  [
302  'id' => 142,
303  'pattern_certificate_id' => 5,
304  'obj_id' => 20,
305  'obj_type' => 'crs',
306  'usr_id' => 400,
307  'user_name' => 'Niels Theen',
308  'acquired_timestamp' => 987_654_321,
309  'certificate_content' => '<xml>Some Other Content</xml>',
310  'template_values' => '[]',
311  'valid_until' => null,
312  'version' => 1,
313  'ilias_version' => 'v5.3.0',
314  'currently_active' => true,
315  'background_image_path' => '/some/where/else/background.jpg',
316  'tile_image_path' => '/some/where/else/tile_image.svg',
317  'background_image_ident' => '-',
318  'tile_image_ident' => '-',
319  'title' => 'SomeTitle',
320  'someDescription' => 'SomeDescription',
321  'certificate_id' => '11111111-2222-3333-4444-555555555555'
322  ],
323  null
324  );
325 
326  $logger = $this->getMockBuilder(ilLogger::class)
327  ->disableOriginalConstructor()
328  ->getMock();
329 
330  $logger->expects($this->atLeastOnce())
331  ->method('debug');
332 
333  $repository = new ilUserCertificateRepository($database, $logger, 'someDefaultTitle');
334 
335  $results = $repository->fetchActiveCertificatesByTypeForPresentation(400, 'crs');
336 
337  $this->assertSame(141, $results[0]->getUserCertificate()->getId());
338  $this->assertSame(142, $results[1]->getUserCertificate()->getId());
339  }
340 
341  public function testFetchCertificate(): void
342  {
343  $database = $this->createMock(ilDBInterface::class);
344 
345  $database->method('nextId')
346  ->willReturn(141);
347 
348  $database->method('fetchAssoc')->willReturn(
349  [
350  'id' => 141,
351  'pattern_certificate_id' => 1,
352  'obj_id' => 20,
353  'obj_type' => 'crs',
354  'usr_id' => 400,
355  'user_name' => 'Niels Theen',
356  'acquired_timestamp' => 123_456_789,
357  'certificate_content' => '<xml>Some Content</xml>',
358  'template_values' => '[]',
359  'valid_until' => null,
360  'version' => 1,
361  'ilias_version' => 'v5.4.0',
362  'currently_active' => true,
363  'background_image_path' => '/some/where/background.jpg',
364  'tile_image_path' => '/some/where/else/tile_image.svg',
365  'background_image_ident' => '-',
366  'tile_image_ident' => '-',
367  'title' => 'SomeTitle',
368  'someDescription' => 'SomeDescription',
369  'certificate_id' => '11111111-2222-3333-4444-555555555555'
370  ]
371  );
372 
373  $logger = $this->getMockBuilder(ilLogger::class)
374  ->disableOriginalConstructor()
375  ->getMock();
376 
377  $logger->expects($this->atLeastOnce())
378  ->method('debug');
379 
380  $repository = new ilUserCertificateRepository($database, $logger, 'someTitle');
381 
382  $result = $repository->fetchCertificate(141);
383 
384  $this->assertSame(141, $result->getId());
385  }
386 
388  {
389  $this->expectException(ilException::class);
390 
391  $database = $this->createMock(ilDBInterface::class);
392 
393  $database->method('nextId')
394  ->willReturn(141);
395 
396  $database->method('fetchAssoc')
397  ->willReturn([]);
398 
399  $logger = $this->getMockBuilder(ilLogger::class)
400  ->disableOriginalConstructor()
401  ->getMock();
402 
403  $logger->expects($this->atLeastOnce())
404  ->method('debug');
405 
406  $repository = new ilUserCertificateRepository($database, $logger, 'someTitle');
407 
408  $repository->fetchCertificate(141);
409 
410  $this->fail('Should never happen. Certificate Found?');
411  }
412 
414  {
415  $database = $this->createMock(ilDBInterface::class);
416 
417  $database
418  ->expects($this->once())
419  ->method('query');
420 
421  $database
422  ->expects($this->once())
423  ->method('in');
424 
425  $database
426  ->expects($this->exactly(3))
427  ->method('fetchAssoc')
428  ->willReturnOnConsecutiveCalls(
429  ['obj_id' => 100],
430  ['obj_id' => 300],
431  []
432  );
433 
434  $database->method('fetchAssoc')
435  ->willReturn([]);
436 
437  $logger = $this->getMockBuilder(ilLogger::class)
438  ->disableOriginalConstructor()
439  ->getMock();
440 
441  $logger->expects($this->atLeastOnce())
442  ->method('debug');
443 
444  $repository = new ilUserCertificateRepository($database, $logger, 'someTitle');
445 
446  $userId = 10;
447  $objectIds = [200, 300, 400];
448 
449  $results = $repository->fetchObjectIdsWithCertificateForUser($userId, $objectIds);
450 
451  $this->assertSame([100, 300], $results);
452  }
453 
455  {
456  $database = $this->createMock(ilDBInterface::class);
457 
458  $database
459  ->expects($this->once())
460  ->method('query');
461 
462  $database
463  ->expects($this->exactly(3))
464  ->method('fetchAssoc')
465  ->willReturnOnConsecutiveCalls(
466  ['usr_id' => 100],
467  ['usr_id' => 300],
468  []
469  );
470 
471  $database->method('fetchAssoc')
472  ->willReturn([]);
473 
474  $logger = $this->getMockBuilder(ilLogger::class)
475  ->disableOriginalConstructor()
476  ->getMock();
477 
478  $logger->expects($this->atLeastOnce())
479  ->method('debug');
480 
481  $repository = new ilUserCertificateRepository($database, $logger, 'someTitle');
482 
483  $objectId = 10;
484 
485  $results = $repository->fetchUserIdsWithCertificateForObject($objectId);
486 
487  $this->assertSame([100, 300], $results);
488  }
489 }
Class ilCertificateBaseTestCase.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
$results