ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
ilUserCertificateRepositoryTest.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
8 {
10  {
11  $database = $this->getMockBuilder('ilDBInterface')
12  ->getMock();
13 
14  $database->method('nextId')
15  ->willReturn(141);
16 
17  $database->method('insert')->with(
18  'il_cert_user_cert',
19  array(
20  'id' => array('integer', 141),
21  'pattern_certificate_id' => array('integer', 1),
22  'obj_id' => array('integer', 20),
23  'obj_type' => array('text', 'crs'),
24  'user_id' => array('integer', 400),
25  'user_name' => array('text', 'Niels Theen'),
26  'acquired_timestamp' => array('integer', 123456789),
27  'certificate_content' => array('clob', '<xml>Some Content</xml>'),
28  'template_values' => array('clob', '[]'),
29  'valid_until' => array('integer', null),
30  'version' => array('integer', 1),
31  'ilias_version' => array('text', 'v5.4.0'),
32  'currently_active' => array('integer', true),
33  'background_image_path' => array('text', '/some/where/background.jpg'),
34  'thumbnail_image_path' => array('text', '/some/where/thumbnail.svg'),
35  )
36  );
37 
38  $logger = $this->getMockBuilder('ilLogger')
39  ->disableOriginalConstructor()
40  ->getMock();
41 
42  $logger->expects($this->atLeastOnce())
43  ->method('info');
44 
45  $repository = new ilUserCertificateRepository(
46  $database,
47  $logger,
48  'someDefaultTitle'
49  );
50 
51  $userCertificate = new ilUserCertificate(
52  1,
53  20,
54  'crs',
55  400,
56  'Niels Theen',
57  123456789,
58  '<xml>Some Content</xml>',
59  '[]',
60  null,
61  1,
62  'v5.4.0',
63  true,
64  '/some/where/background.jpg',
65  '/some/where/thumbnail.svg'
66  );
67 
68  $repository->save($userCertificate);
69  }
70 
72  {
73  $database = $this->getMockBuilder('ilDBInterface')
74  ->getMock();
75 
76  $database->method('nextId')
77  ->willReturn(141);
78 
79  $database->method('fetchAssoc')->willReturnOnConsecutiveCalls(
80  array(
81  'id' => 141,
82  'pattern_certificate_id' => 1,
83  'obj_id' => 20,
84  'obj_type' => 'crs',
85  'user_id' => 400,
86  'user_name' => 'Niels Theen',
87  'acquired_timestamp' => 123456789,
88  'certificate_content' => '<xml>Some Content</xml>',
89  'template_values' => '[]',
90  'valid_until' => null,
91  'version' => 1,
92  'ilias_version' => 'v5.4.0',
93  'currently_active' => true,
94  'background_image_path' => '/some/where/background.jpg',
95  'thumbnail_image_path' => array('clob', '/some/where/thumbnail.svg'),
96  'title' => 'Some Title'
97  ),
98  array(
99  'id' => 142,
100  'pattern_certificate_id' => 5,
101  'obj_id' => 3123,
102  'obj_type' => 'tst',
103  'user_id' => 400,
104  'user_name' => 'Niels Theen',
105  'acquired_timestamp' => 987654321,
106  'certificate_content' => '<xml>Some Other Content</xml>',
107  'template_values' => '[]',
108  'valid_until' => null,
109  'version' => 1,
110  'ilias_version' => 'v5.3.0',
111  'currently_active' => true,
112  'background_image_path' => '/some/where/else/background.jpg',
113  'thumbnail_image_path' => array('clob', '/some/where/thumbnail.svg'),
114  'title' => 'Someother Title'
115  )
116  );
117 
118  $logger = $this->getMockBuilder('ilLogger')
119  ->disableOriginalConstructor()
120  ->getMock();
121 
122  $logger->expects($this->atLeastOnce())
123  ->method('info');
124 
125  $repository = new ilUserCertificateRepository(
126  $database,
127  $logger,
128  'someDefaultTitle',
129  'someDefaultDescription'
130  );
131 
132  $results = $repository->fetchActiveCertificates(400);
133 
134  $this->assertEquals(141, $results[0]->getUserCertificate()->getId());
135  $this->assertEquals(142, $results[1]->getUserCertificate()->getId());
136  }
137 
139  {
140  $database = $this->getMockBuilder('ilDBInterface')
141  ->getMock();
142 
143  $database->method('nextId')
144  ->willReturn(141);
145 
146  $database->method('fetchAssoc')->willReturnOnConsecutiveCalls(
147  array(
148  'id' => 141,
149  'pattern_certificate_id' => 1,
150  'obj_id' => 20,
151  'obj_type' => 'crs',
152  'user_id' => 400,
153  'user_name' => 'Niels Theen',
154  'acquired_timestamp' => 123456789,
155  'certificate_content' => '<xml>Some Content</xml>',
156  'template_values' => '[]',
157  'valid_until' => null,
158  'version' => 1,
159  'ilias_version' => 'v5.4.0',
160  'currently_active' => true,
161  'background_image_path' => '/some/where/background.jpg',
162  'thumbnail_image_path' => array('clob', '/some/where/thumbnail.svg'),
163  ),
164  array(
165  'id' => 142,
166  'pattern_certificate_id' => 5,
167  'obj_id' => 20,
168  'obj_type' => 'tst',
169  'user_id' => 400,
170  'user_name' => 'Niels Theen',
171  'acquired_timestamp' => 987654321,
172  'certificate_content' => '<xml>Some Other Content</xml>',
173  'template_values' => '[]',
174  'valid_until' => null,
175  'version' => 1,
176  'ilias_version' => 'v5.3.0',
177  'currently_active' => true,
178  'background_image_path' => '/some/where/else/background.jpg',
179  'thumbnail_image_path' => array('clob', '/some/where/thumbnail.svg'),
180  )
181  );
182 
183  $logger = $this->getMockBuilder('ilLogger')
184  ->disableOriginalConstructor()
185  ->getMock();
186 
187  $logger->expects($this->atLeastOnce())
188  ->method('info');
189 
190  $repository = new ilUserCertificateRepository(
191  $database,
192  $logger,
193  'someDefaultTitle'
194  );
195 
196  $result = $repository->fetchActiveCertificate(400, 20);
197 
198  $this->assertEquals(141, $result->getId());
199  }
200 
205  {
206  $this->expectException(\ilException::class);
207 
208  $database = $this->getMockBuilder('ilDBInterface')
209  ->getMock();
210 
211  $database->method('nextId')
212  ->willReturn(141);
213 
214  $database->method('fetchAssoc')->willReturn(array());
215 
216  $logger = $this->getMockBuilder('ilLogger')
217  ->disableOriginalConstructor()
218  ->getMock();
219 
220  $logger->expects($this->atLeastOnce())
221  ->method('info');
222 
223  $repository = new ilUserCertificateRepository($database, $logger, 'someDefaultTitle');
224 
225  $repository->fetchActiveCertificate(400, 20);
226 
227  $this->fail('Should never happen. Certificate Found?');
228  }
229 
231  {
232  $database = $this->getMockBuilder('ilDBInterface')
233  ->getMock();
234 
235  $database->method('nextId')
236  ->willReturn(141);
237 
238  $database->method('fetchAssoc')->willReturnOnConsecutiveCalls(
239  array(
240  'id' => 141,
241  'pattern_certificate_id' => 1,
242  'obj_id' => 20,
243  'obj_type' => 'crs',
244  'user_id' => 400,
245  'user_name' => 'Niels Theen',
246  'acquired_timestamp' => 123456789,
247  'certificate_content' => '<xml>Some Content</xml>',
248  'template_values' => '[]',
249  'valid_until' => null,
250  'version' => 1,
251  'ilias_version' => 'v5.4.0',
252  'currently_active' => true,
253  'background_image_path' => '/some/where/background.jpg',
254  'thumbnail_image_path' => '/some/where/else/thumbnail.svg',
255  'title' => 'SomeTitle',
256  'someDescription' => 'SomeDescription'
257  ),
258  array(
259  'id' => 142,
260  'pattern_certificate_id' => 5,
261  'obj_id' => 20,
262  'obj_type' => 'crs',
263  'user_id' => 400,
264  'user_name' => 'Niels Theen',
265  'acquired_timestamp' => 987654321,
266  'certificate_content' => '<xml>Some Other Content</xml>',
267  'template_values' => '[]',
268  'valid_until' => null,
269  'version' => 1,
270  'ilias_version' => 'v5.3.0',
271  'currently_active' => true,
272  'background_image_path' => '/some/where/else/background.jpg',
273  'thumbnail_image_path' => '/some/where/else/thumbnail.svg',
274  'title' => 'SomeTitle',
275  'someDescription' => 'SomeDescription'
276  )
277  );
278 
279  $logger = $this->getMockBuilder('ilLogger')
280  ->disableOriginalConstructor()
281  ->getMock();
282 
283  $logger->expects($this->atLeastOnce())
284  ->method('info');
285 
286  $repository = new ilUserCertificateRepository($database, $logger, 'someDefaultTitle');
287 
288  $results = $repository->fetchActiveCertificatesByTypeForPresentation(400, 'crs');
289 
290  $this->assertEquals(141, $results[0]->getUserCertificate()->getId());
291  $this->assertEquals(142, $results[1]->getUserCertificate()->getId());
292  }
293 
294  public function testFetchCertificate()
295  {
296  $database = $this->getMockBuilder('ilDBInterface')
297  ->getMock();
298 
299  $database->method('nextId')
300  ->willReturn(141);
301 
302  $database->method('fetchAssoc')->willReturn(
303  array(
304  'id' => 141,
305  'pattern_certificate_id' => 1,
306  'obj_id' => 20,
307  'obj_type' => 'crs',
308  'user_id' => 400,
309  'user_name' => 'Niels Theen',
310  'acquired_timestamp' => 123456789,
311  'certificate_content' => '<xml>Some Content</xml>',
312  'template_values' => '[]',
313  'valid_until' => null,
314  'version' => 1,
315  'ilias_version' => 'v5.4.0',
316  'currently_active' => true,
317  'background_image_path' => '/some/where/background.jpg',
318  'thumbnail_image_path' => '/some/where/else/thumbnail.svg',
319  'title' => 'SomeTitle',
320  'someDescription' => 'SomeDescription'
321  )
322  );
323 
324  $logger = $this->getMockBuilder('ilLogger')
325  ->disableOriginalConstructor()
326  ->getMock();
327 
328  $logger->expects($this->atLeastOnce())
329  ->method('info');
330 
331  $repository = new ilUserCertificateRepository($database, $logger, 'someTitle');
332 
333  $result = $repository->fetchCertificate(141);
334 
335  $this->assertEquals(141, $result->getId());
336  }
337 
342  {
343  $this->expectException(\ilException::class);
344 
345  $database = $this->getMockBuilder('ilDBInterface')
346  ->getMock();
347 
348  $database->method('nextId')
349  ->willReturn(141);
350 
351  $database->method('fetchAssoc')
352  ->willReturn(array());
353 
354  $logger = $this->getMockBuilder('ilLogger')
355  ->disableOriginalConstructor()
356  ->getMock();
357 
358  $logger->expects($this->atLeastOnce())
359  ->method('info');
360 
361  $repository = new ilUserCertificateRepository($database, $logger, 'someTitle');
362 
363  $repository->fetchCertificate(141);
364 
365  $this->fail('Should never happen. Certificate Found?');
366  }
367 
369  {
370  $database = $this->getMockBuilder('ilDBInterface')
371  ->getMock();
372 
373  $database
374  ->expects($this->once())
375  ->method('query');
376 
377  $database
378  ->expects($this->once())
379  ->method('in');
380 
381  $database
382  ->expects($this->exactly(3))
383  ->method('fetchAssoc')
384  ->willReturnOnConsecutiveCalls(
385  array('obj_id' => 100),
386  array('obj_id' => 300),
387  array()
388  );
389 
390  $database->method('fetchAssoc')
391  ->willReturn(array());
392 
393  $logger = $this->getMockBuilder('ilLogger')
394  ->disableOriginalConstructor()
395  ->getMock();
396 
397  $logger->expects($this->atLeastOnce())
398  ->method('info');
399 
400  $repository = new ilUserCertificateRepository($database, $logger, 'someTitle');
401 
402  $userId = 10;
403  $objectIds = array(200, 300, 400);
404 
405  $results = $repository->fetchObjectIdsWithCertificateForUser($userId, $objectIds);
406 
407  $this->assertEquals(array(100, 300), $results);
408  }
409 
411  {
412  $database = $this->getMockBuilder('ilDBInterface')
413  ->getMock();
414 
415  $database
416  ->expects($this->once())
417  ->method('query');
418 
419  $database
420  ->expects($this->exactly(3))
421  ->method('fetchAssoc')
422  ->willReturnOnConsecutiveCalls(
423  array('user_id' => 100),
424  array('user_id' => 300),
425  array()
426  );
427 
428  $database->method('fetchAssoc')
429  ->willReturn(array());
430 
431  $logger = $this->getMockBuilder('ilLogger')
432  ->disableOriginalConstructor()
433  ->getMock();
434 
435  $logger->expects($this->atLeastOnce())
436  ->method('info');
437 
438  $repository = new ilUserCertificateRepository($database, $logger, 'someTitle');
439 
440  $objectId = 10;
441 
442  $results = $repository->fetchUserIdsWithCertificateForObject($objectId);
443 
444  $this->assertEquals(array(100, 300), $results);
445  }
446 }
$result
Class ilCertificateBaseTestCase.
$results