ILIAS  trunk Revision v12.0_alpha-1540-g00f839d5fa1
ilCertificateTemplateRepositoryTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22{
24 {
25 $database = $this->createMock(ilDBInterface::class);
26
27 $logger = $this->getMockBuilder(ilLogger::class)
28 ->disableOriginalConstructor()
29 ->getMock();
30
31 $objectDataCache = $this->getMockBuilder(ilObjectDataCache::class)
32 ->disableOriginalConstructor()
33 ->getMock();
34
35 $objectDataCache->method('lookUpType')->willReturn('crs');
36
37 $database->method('nextId')
38 ->willReturn(10);
39
40 $database
41 ->expects($this->once())
42 ->method('insert')
43 ->with(
44 'il_cert_template',
45 [
46 'id' => ['integer', 10],
47 'obj_id' => ['integer', 100],
48 'obj_type' => ['text', 'crs'],
49 'certificate_content' => ['clob', '<xml>Some Content</xml>'],
50 'certificate_hash' => ['text', md5('<xml>Some Content</xml>')],
51 'template_values' => ['clob', '[]'],
52 'version' => ['integer', 1],
53 'ilias_version' => ['text', 'v5.4.0'],
54 'created_timestamp' => ['integer', 123_456_789],
55 'currently_active' => ['integer', true],
56 'deleted' => ['integer', 0],
57 'background_image_ident' => ['text', '-'],
58 'tile_image_ident' => ['text', '-']
59 ]
60 );
61
62 $logger->expects($this->atLeastOnce())
63 ->method('debug');
64
65 $template = new ilCertificateTemplate(
66 100,
67 'crs',
68 '<xml>Some Content</xml>',
69 md5('<xml>Some Content</xml>'),
70 '[]',
71 1,
72 'v5.4.0',
73 123_456_789,
74 true,
75 '-',
76 '-'
77 );
78
79 $repository = new ilCertificateTemplateDatabaseRepository($database, $logger, $objectDataCache);
80
81 $repository->save($template);
82 }
83
85 {
86 $logger = $this->getMockBuilder(ilLogger::class)
87 ->disableOriginalConstructor()
88 ->getMock();
89
90 $database = $this->createMock(ilDBInterface::class);
91 $consecutive = [
92 [
93 'id' => 1,
94 'obj_id' => 10,
95 'obj_type' => 'crs',
96 'certificate_content' => '<xml>Some Content</xml>',
97 'certificate_hash' => md5('<xml>Some Content</xml>'),
98 'template_values' => '[]',
99 'version' => 1,
100 'ilias_version' => 'v5.4.0',
101 'created_timestamp' => 123_456_789,
102 'currently_active' => true,
103 'background_image_ident' => '-',
104 'tile_image_ident' => '-'
105 ],
106 [
107 'id' => 30,
108 'obj_id' => 10,
109 'obj_type' => 'tst',
110 'certificate_content' => '<xml>Some Other Content</xml>',
111 'certificate_hash' => md5('<xml>Some Content</xml>'),
112 'template_values' => '[]',
113 'version' => 55,
114 'ilias_version' => 'v5.3.0',
115 'created_timestamp' => 123_456_789,
116 'currently_active' => false,
117 'background_image_ident' => '-',
118 'tile_image_ident' => '-'
119 ]
120 ];
121 $database->method('fetchAssoc')->willReturnCallback(
122 function () use (&$consecutive) {
123 return array_shift($consecutive);
124 }
125 );
126
127 $objectDataCache = $this->getMockBuilder(ilObjectDataCache::class)
128 ->disableOriginalConstructor()
129 ->getMock();
130
131 $objectDataCache->method('lookUpType')->willReturn('crs');
132
133 $repository = new ilCertificateTemplateDatabaseRepository($database, $logger, $objectDataCache);
134
135 $templates = $repository->fetchCertificateTemplatesByObjId(10);
136
137 $this->assertSame(1, $templates[0]->getId());
138 $this->assertSame(30, $templates[1]->getId());
139 }
140
142 {
143 $logger = $this->getMockBuilder(ilLogger::class)
144 ->disableOriginalConstructor()
145 ->getMock();
146
147 $database = $this->createMock(ilDBInterface::class);
148 $consecutive = [
149 [
150 'id' => 1,
151 'obj_id' => 10,
152 'obj_type' => 'crs',
153 'certificate_content' => '<xml>Some Content</xml>',
154 'certificate_hash' => md5('<xml>Some Content</xml>'),
155 'template_values' => '[]',
156 'version' => 1,
157 'ilias_version' => 'v5.4.0',
158 'created_timestamp' => 123_456_789,
159 'currently_active' => true,
160 'background_image_ident' => '-',
161 'tile_image_ident' => '-'
162 ],
163 [
164 'id' => 30,
165 'obj_id' => 10,
166 'obj_type' => 'tst',
167 'certificate_content' => '<xml>Some Other Content</xml>',
168 'certificate_hash' => md5('<xml>Some Content</xml>'),
169 'template_values' => '[]',
170 'version' => 55,
171 'ilias_version' => 'v5.3.0',
172 'created_timestamp' => 123_456_789,
173 'currently_active' => false,
174 'background_image_ident' => '-',
175 'tile_image_ident' => '-'
176 ]
177 ];
178 $database->method('fetchAssoc')->willReturnCallback(
179 function () use (&$consecutive) {
180 return array_shift($consecutive);
181 }
182 );
183
184 $objectDataCache = $this->getMockBuilder(ilObjectDataCache::class)
185 ->disableOriginalConstructor()
186 ->getMock();
187
188 $objectDataCache->method('lookUpType')->willReturn('crs');
189
190 $repository = new ilCertificateTemplateDatabaseRepository($database, $logger, $objectDataCache);
191
192 $template = $repository->fetchCurrentlyActiveCertificate(10);
193
194 $this->assertSame(1, $template->getId());
195 }
196
197 public function testFetchPreviousCertificate(): void
198 {
199 $logger = $this->getMockBuilder(ilLogger::class)
200 ->disableOriginalConstructor()
201 ->getMock();
202
203 $database = $this->createMock(ilDBInterface::class);
204 $consecutive = [
205 [
206 'id' => 1,
207 'obj_id' => 10,
208 'obj_type' => 'crs',
209 'certificate_content' => '<xml>Some Content</xml>',
210 'certificate_hash' => md5('<xml>Some Content</xml>'),
211 'template_values' => '[]',
212 'version' => 1,
213 'ilias_version' => 'v5.4.0',
214 'created_timestamp' => 123_456_789,
215 'currently_active' => true,
216 'background_image_ident' => '-',
217 'tile_image_ident' => '-'
218 ],
219 [
220 'id' => 30,
221 'obj_id' => 10,
222 'obj_type' => 'tst',
223 'certificate_content' => '<xml>Some Other Content</xml>',
224 'certificate_hash' => md5('<xml>Some Content</xml>'),
225 'template_values' => '[]',
226 'version' => 55,
227 'ilias_version' => 'v5.3.0',
228 'created_timestamp' => 123_456_789,
229 'currently_active' => false,
230 'background_image_ident' => '-',
231 'tile_image_ident' => '-'
232 ]
233 ];
234 $database->method('fetchAssoc')->willReturnCallback(
235 function () use (&$consecutive) {
236 return array_shift($consecutive);
237 }
238 );
239
240 $objectDataCache = $this->getMockBuilder(ilObjectDataCache::class)
241 ->disableOriginalConstructor()
242 ->getMock();
243
244 $objectDataCache->method('lookUpType')->willReturn('crs');
245
246 $repository = new ilCertificateTemplateDatabaseRepository($database, $logger, $objectDataCache);
247
248 $template = $repository->fetchPreviousCertificate(10);
249
250 $this->assertSame(30, $template->getId());
251 }
252
253 public function testDeleteTemplateFromDatabase(): void
254 {
255 $database = $this->createMock(ilDBInterface::class);
256 $logger = $this->createStub(ilLogger::class);
257 $object_data_cache = $this->createStub(ilObjectDataCache::class);
258
259 $database->method('quote')->willReturnCallback(
260 static fn(int $value, string $type): string => (string) $value
261 );
262
263 $captured_sql_string = null;
264 $database
265 ->expects($this->once())
266 ->method('manipulate')
267 ->willReturnCallback(static function (string $sql) use (&$captured_sql_string): int {
268 $captured_sql_string = $sql;
269 return 1;
270 });
271
272 $repository = new ilCertificateTemplateDatabaseRepository($database, $logger, $object_data_cache);
273
274 $repository->deleteTemplate(10, 200);
275
276 $this->assertSame(
277 'UPDATE il_cert_template SET deleted = 1, currently_active = 0 WHERE id = 10 AND obj_id = 200',
278 trim(preg_replace('/\s+/', ' ', $captured_sql_string))
279 );
280 }
281
282 public function testActivatePreviousCertificate(): void
283 {
284 $logger = $this->getMockBuilder(ilLogger::class)
285 ->disableOriginalConstructor()
286 ->getMock();
287
288 $database = $this->createMock(ilDBInterface::class);
289
290 $quote_consecutive = [
291 [10, 'integer'],
292 [30, 'integer']
293 ];
294 $database->method('quote')->willReturnCallback(
295 function (int $v, string $type) use (&$quote_consecutive) {
296 [$expected, $type] = array_shift($quote_consecutive);
297 $this->assertEquals('integer', $type);
298 $this->assertEquals($expected, $v);
299 return (string) ($v);
300 }
301 );
302
303 $consecutive = [
304 [
305 'id' => 1,
306 'obj_id' => 10,
307 'obj_type' => 'crs',
308 'certificate_content' => '<xml>Some Content</xml>',
309 'certificate_hash' => md5('<xml>Some Content</xml>'),
310 'template_values' => '[]',
311 'version' => 1,
312 'ilias_version' => 'v5.4.0',
313 'created_timestamp' => 123_456_789,
314 'currently_active' => true,
315 'background_image_ident' => '-',
316 'tile_image_ident' => '-'
317 ],
318 [
319 'id' => 30,
320 'obj_id' => 10,
321 'obj_type' => 'tst',
322 'certificate_content' => '<xml>Some Other Content</xml>',
323 'certificate_hash' => md5('<xml>Some Content</xml>'),
324 'template_values' => '[]',
325 'version' => 55,
326 'ilias_version' => 'v5.3.0',
327 'created_timestamp' => 123_456_789,
328 'currently_active' => false,
329 'background_image_ident' => '-',
330 'tile_image_ident' => '-'
331 ]
332 ];
333 $database->method('fetchAssoc')->willReturnCallback(
334 function () use (&$consecutive) {
335 return array_shift($consecutive);
336 }
337 );
338
339 $query_consecutive = [
340 "SELECT * FROM il_cert_template WHERE obj_id = 10 AND deleted = 0 ORDER BY version ASC",
341 'UPDATE il_cert_template SET currently_active = 1 WHERE id = 30'
342 ];
343 $database->method('query')->willReturnCallback(
344 function (string $v) use (&$query_consecutive) {
345 $expected = array_shift($query_consecutive);
346 $v = trim(str_replace(array("\n", "\r"), ' ', $v)) ;
347 $this->assertEquals($expected, $v);
348 return $this->createMock(ilDBStatement::class);
349 },
350 );
351
352 $objectDataCache = $this->getMockBuilder(ilObjectDataCache::class)
353 ->disableOriginalConstructor()
354 ->getMock();
355
356 $objectDataCache->method('lookUpType')->willReturn('crs');
357
358 $repository = new ilCertificateTemplateDatabaseRepository($database, $logger, $objectDataCache);
359
360 $template = $repository->activatePreviousCertificate(10);
361
362 $this->assertSame(30, $template->getId());
363 }
364
365 public function testFetchAllObjectIdsByType(): void
366 {
367 $logger = $this->getMockBuilder(ilLogger::class)
368 ->disableOriginalConstructor()
369 ->getMock();
370
371 $objectDataCache = $this->getMockBuilder(ilObjectDataCache::class)
372 ->disableOriginalConstructor()
373 ->getMock();
374
375
376 $database = $this->createMock(ilDBInterface::class);
377 $consecutive = [
378 [
379 'id' => 1,
380 'obj_id' => 10,
381 'obj_type' => 'crs',
382 'certificate_content' => '<xml>Some Content</xml>',
383 'certificate_hash' => md5('<xml>Some Content</xml>'),
384 'template_values' => '[]',
385 'version' => 1,
386 'ilias_version' => 'v5.4.0',
387 'created_timestamp' => 123_456_789,
388 'currently_active' => true,
389 'background_image_ident' => '-',
390 'tile_image_ident' => '-'
391 ],
392 [
393 'id' => 30,
394 'obj_id' => 30,
395 'obj_type' => 'crs',
396 'certificate_content' => '<xml>Some Other Content</xml>',
397 'certificate_hash' => md5('<xml>Some Content</xml>'),
398 'template_values' => '[]',
399 'version' => 55,
400 'ilias_version' => 'v5.3.0',
401 'created_timestamp' => 123_456_789,
402 'currently_active' => false,
403 'background_image_ident' => '-',
404 'tile_image_ident' => '-'
405 ]
406 ];
407 $database->method('fetchAssoc')->willReturnCallback(
408 function () use (&$consecutive) {
409 return array_shift($consecutive);
410 }
411 );
412
413 $repository = new ilCertificateTemplateDatabaseRepository($database, $logger, $objectDataCache);
414
415 $templates = $repository->fetchActiveCertificateTemplatesForCoursesWithDisabledLearningProgress(true);
416
417 $this->assertSame(10, $templates[0]->getObjId());
418 $this->assertSame(30, $templates[1]->getObjId());
419 }
420
422 {
423 $this->expectException(ilException::class);
424
425 $database = $this->createMock(ilDBInterface::class);
426
427 $logger = $this->getMockBuilder(ilLogger::class)
428 ->disableOriginalConstructor()
429 ->getMock();
430
431 $database
432 ->expects($this->once())
433 ->method('quote')
434 ->with(10, 'integer')
435 ->willReturn('10');
436
437 $objectDataCache = $this->getMockBuilder(ilObjectDataCache::class)
438 ->disableOriginalConstructor()
439 ->getMock();
440
441 $database->method('fetchAssoc')
442 ->willReturn([]);
443
444 $database->method('fetchAssoc')
445 ->willReturn([]);
446
447 $repository = new ilCertificateTemplateDatabaseRepository($database, $logger, $objectDataCache);
448
449 $repository->fetchFirstCreatedTemplate(10);
450
451 $this->fail();
452 }
453
454 public function fetchFirstCreateTemplate(): void
455 {
456 $database = $this->createMock(ilDBInterface::class);
457
458 $logger = $this->getMockBuilder(ilLogger::class)
459 ->disableOriginalConstructor()
460 ->getMock();
461
462 $database
463 ->expects($this->once())
464 ->method('quote')
465 ->with(10, 'integer')
466 ->willReturn(10);
467
468 $objectDataCache = $this->getMockBuilder(ilObjectDataCache::class)
469 ->disableOriginalConstructor()
470 ->getMock();
471
472 $database->method('fetchAssoc')
473 ->willReturn([]);
474
475 $database->method('fetchAssoc')->willReturn(
476 [
477 'id' => 1,
478 'obj_id' => 10,
479 'obj_type' => 'crs',
480 'certificate_content' => '<xml>Some Content</xml>',
481 'certificate_hash' => md5('<xml>Some Content</xml>'),
482 'template_values' => '[]',
483 'version' => 1,
484 'ilias_version' => 'v5.4.0',
485 'created_timestamp' => 123_456_789,
486 'currently_active' => true,
487 'background_image_ident' => '/some/where/background.jpg'
488 ]
489 );
490
491 $repository = new ilCertificateTemplateDatabaseRepository($database, $logger, $objectDataCache);
492
493 $firstTemplate = $repository->fetchFirstCreatedTemplate(10);
494
495 $this->assertSame(1, $firstTemplate->getId());
496 }
497}