ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ilCertificateCourseLearningProgressEvaluationTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use PDO;
24use ilLogger;
25use ilSetting;
26use ilLPStatus;
32use PHPUnit\Framework\TestCase;
38
43{
45 {
46 $templateRepository = $this->getMockBuilder(ilCertificateTemplateRepository::class)->getMock();
47
48 $templateRepository->method('fetchActiveCertificateTemplatesForCoursesWithDisabledLearningProgress')
49 ->willReturn(
50 [
52 5,
53 'crs',
54 '<xml>Some Content</xml>',
55 md5('<xml>Some Content</xml>'),
56 '[]',
57 1,
58 'v5.4.0',
59 123_456_789,
60 true,
61 '/some/where/background.jpg',
62 '/some/where/thumbnail.svg',
63 '-',
64 '-',
65 1
66 ),
68 6,
69 'crs',
70 '<xml>Some Content</xml>',
71 md5('<xml>Some Content</xml>'),
72 '[]',
73 1,
74 'v5.4.0',
75 123_456_789,
76 true,
77 '/some/where/background.jpg',
78 '/some/where/thumbnail.svg',
79 '-',
80 '-',
81 5
82 ),
83 ]
84 );
85
86 $setting = $this->getMockBuilder(ilSetting::class)
87 ->disableOriginalConstructor()
88 ->getMock();
89
90 $consecutive_get = [
91 ['cert_subitems_5', '[10,20]'],
92 ['cert_subitems_6', '[10,50]'],
93 ];
95 ->method('get')
96 ->willReturnCallback(
97 function (string $k) use (&$consecutive_get): string {
98 list($expected, $ret) = array_shift($consecutive_get);
99 $this->assertEquals($expected, $k);
100 return $ret;
101 }
102 );
103
104 $objectHelper = $this->getMockBuilder(ilCertificateObjectHelper::class)
105 ->getMock();
106
107 $consecutive_lookup = [10, 20, 10, 50];
108 $objectHelper
109 ->method('lookupObjId')
110 ->willReturnCallback(
111 function (int $id) use (&$consecutive_lookup): int {
112 $expected = array_shift($consecutive_lookup);
113 $this->assertEquals($expected, $id);
114
115 return $id * 10;
116 }
117 );
118
119 $statusHelper = $this->getMockBuilder(ilCertificateLPStatusHelper::class)
120 ->getMock();
121
122 $consecutive_status = [
127 ];
128 $statusHelper
129 ->method('lookUpStatus')
130 ->willReturnCallback(
131 function (int $id) use (&$consecutive_status): int {
132 list($expected, $ret) = array_shift($consecutive_status);
133 $this->assertEquals($expected, $id);
134
135 return $ret;
136 }
137 );
138
139 $trackingHelper = $this->getMockBuilder(ilCertificateObjUserTrackingHelper::class)
140 ->getMock();
141 $trackingHelper->method('enabledLearningProgress')->willReturn(true);
142
144 $templateRepository,
145 $setting,
146 $objectHelper,
147 $statusHelper,
148 $trackingHelper
149 );
150
151 $completedCourses = $evaluation->evaluate(10, 200);
152
153 $this->assertSame(5, $completedCourses[0]->getObjId());
154 }
155
157 {
158 $templateRepository = $this->getMockBuilder(ilCertificateTemplateRepository::class)->getMock();
159
160 $templateRepository->method('fetchActiveCertificateTemplatesForCoursesWithDisabledLearningProgress')
161 ->willReturn(
162 [
164 5,
165 'crs',
166 '<xml>Some Content</xml>',
167 md5('<xml>Some Content</xml>'),
168 '[]',
169 1,
170 'v5.4.0',
171 123_456_789,
172 true,
173 '/some/where/background.jpg',
174 '/some/where/thumbnail.svg',
175 '-',
176 '-',
177 1
178 ),
180 6,
181 'crs',
182 '<xml>Some Content</xml>',
183 md5('<xml>Some Content</xml>'),
184 '[]',
185 1,
186 'v5.4.0',
187 123_456_789,
188 true,
189 '/some/where/background.jpg',
190 '/some/where/thumbnail.svg',
191 '-',
192 '-',
193 5
194 ),
195 ]
196 );
197
198 $setting = $this->getMockBuilder(ilSetting::class)
199 ->disableOriginalConstructor()
200 ->getMock();
201
202 $setting = $this->getMockBuilder(ilSetting::class)
203 ->disableOriginalConstructor()
204 ->getMock();
205
206 $consecutive_get = [
207 ['cert_subitems_5', '[10,20]'],
208 ['cert_subitems_6', '[10,500]'],
209 ];
211 ->method('get')
212 ->willReturnCallback(
213 function (string $k) use (&$consecutive_get): string {
214 list($expected, $ret) = array_shift($consecutive_get);
215 $this->assertEquals($expected, $k);
216 return $ret;
217 }
218 );
219
220 $objectHelper = $this->getMockBuilder(ilCertificateObjectHelper::class)
221 ->getMock();
222
223 $consecutive_lookup = [
224 [10, 100],
225 [20, 200],
226 [10, 100],
227 [500, 500],
228 ];
229 $objectHelper
230 ->method('lookupObjId')
231 ->willReturnCallback(
232 function (int $id) use (&$consecutive_lookup): int {
233 list($expected, $ret) = array_shift($consecutive_lookup);
234 $this->assertEquals($expected, $id);
235
236 return $ret;
237 }
238 );
239
240 $statusHelper = $this->getMockBuilder(ilCertificateLPStatusHelper::class)
241 ->getMock();
242
243 $consecutive_status = [
248 ];
249
250 $statusHelper
251 ->method('lookUpStatus')
252 ->willReturnCallback(
253 function (int $id) use (&$consecutive_status): int {
254 list($expected, $ret) = array_shift($consecutive_status);
255 $this->assertEquals($expected, $id);
256
257 return $ret;
258 }
259 );
260
261 $trackingHelper = $this->getMockBuilder(ilCertificateObjUserTrackingHelper::class)
262 ->getMock();
263 $trackingHelper->method('enabledLearningProgress')->willReturn(false);
264
266 $templateRepository,
267 $setting,
268 $objectHelper,
269 $statusHelper,
270 $trackingHelper
271 );
272
273 $completedCourses = $evaluation->evaluate(10, 200);
274
275 $this->assertSame(5, $completedCourses[0]->getObjId());
276 $this->assertSame(6, $completedCourses[1]->getObjId());
277 }
278
279 public function testNoSubitemDefinedForEvaluation(): void
280 {
281 $templateRepository = $this->getMockBuilder(ilCertificateTemplateRepository::class)->getMock();
282
283 $templateRepository->method('fetchActiveCertificateTemplatesForCoursesWithDisabledLearningProgress')
284 ->willReturn(
285 [
287 5,
288 'crs',
289 '<xml>Some Content</xml>',
290 md5('<xml>Some Content</xml>'),
291 '[]',
292 1,
293 'v5.4.0',
294 123_456_789,
295 true,
296 '/some/where/background.jpg',
297 '/some/where/thumbnail.svg',
298 '-',
299 '-',
300 1
301 ),
303 6,
304 'crs',
305 '<xml>Some Content</xml>',
306 md5('<xml>Some Content</xml>'),
307 '[]',
308 1,
309 'v5.4.0',
310 123_456_789,
311 true,
312 '/some/where/background.jpg',
313 '/some/where/thumbnail.svg',
314 '-',
315 '-',
316 5
317 ),
318 ]
319 );
320
321 $setting = $this->getMockBuilder(ilSetting::class)
322 ->disableOriginalConstructor()
323 ->getMock();
324
325 $consecutive_get = [
326 'cert_subitems_5',
327 'cert_subitems_6',
328 ];
330 ->method('get')
331 ->willReturnCallback(
332 function (string $k) use (&$consecutive_get) {
333 $expected = array_shift($consecutive_get);
334 $this->assertEquals($expected, $k);
335
336 return null;
337 }
338 );
339
340 $objectHelper = $this->getMockBuilder(ilCertificateObjectHelper::class)
341 ->getMock();
342
343 $statusHelper = $this->getMockBuilder(ilCertificateLPStatusHelper::class)
344 ->getMock();
345
346 $trackingHelper = $this->getMockBuilder(ilCertificateObjUserTrackingHelper::class)
347 ->getMock();
348 $trackingHelper->method('enabledLearningProgress')->willReturn(false);
349
351 $templateRepository,
352 $setting,
353 $objectHelper,
354 $statusHelper,
355 $trackingHelper
356 );
357
358 $completedCourses = $evaluation->evaluate(10, 200);
359
360 $this->assertSame([], $completedCourses);
361 }
362
363 public static function globalLearningProgressStateProvder(): array
364 {
365 return [
366 'LP globally enabled' => [true, []],
367 'LP globally disabled' => [true, [
368 [
369 'id' => 1,
370 'obj_id' => 10,
371 'obj_type' => 'crs',
372 'certificate_content' => '<xml>Some Content</xml>',
373 'certificate_hash' => md5('<xml>Some Content</xml>'),
374 'template_values' => '[]',
375 'version' => 1,
376 'ilias_version' => 'v5.4.0',
377 'created_timestamp' => 123_456_789,
378 'currently_active' => true,
379 'background_image_path' => '/some/where/background.jpg',
380 'thumbnail_image_path' => 'some/path/test.svg',
381 'background_image_ident' => '-',
382 'thumbnail_image_ident' => '-'
383 ],
384 [
385 'id' => 30,
386 'obj_id' => 10,
387 'obj_type' => 'tst',
388 'certificate_content' => '<xml>Some Other Content</xml>',
389 'certificate_hash' => md5('<xml>Some Content</xml>'),
390 'template_values' => '[]',
391 'version' => 55,
392 'ilias_version' => 'v5.3.0',
393 'created_timestamp' => 123_456_789,
394 'currently_active' => false,
395 'background_image_path' => '/some/where/else/background.jpg',
396 'thumbnail_image_path' => 'some/path/test.svg',
397 'background_image_ident' => '-',
398 'thumbnail_image_ident' => '-'
399 ]
400 ]],
401 ];
402 }
403
407 #[\PHPUnit\Framework\Attributes\DataProvider('globalLearningProgressStateProvder')]
409 bool $isGlobalLpEnabled,
410 array $template_recods
411 ): void {
412 $statement = $database = $this->getMockBuilder(ilDBStatement::class)->getMock();
413 $i = 0;
414 $database->method('fetch')->willReturnCallback(static function () use (&$i, $template_recods): ?array {
415 $result = $template_recods[$i] ?? null;
416 ++$i;
417
418 return $result;
419 });
420
421 $database = $this->createMock(ilDBInterface::class);
422 $database->expects($this->once())->method('queryF')->with(
423 $isGlobalLpEnabled
424 ? $this->logicalAnd(
425 $this->stringContains('LEFT JOIN ut_lp_settings', false),
426 $this->stringContains('uls.u_mode', false),
427 ) : $this->logicalAnd(
428 $this->logicalNot($this->stringContains('LEFT JOIN ut_lp_settings', false)),
429 $this->logicalNot($this->stringContains('uls.u_mode', false)),
430 )
431 )->willReturn($statement);
432 $database->expects($this->exactly(count($template_recods) + 1))
433 ->method('fetchAssoc')
434 ->with($statement)
435 ->willReturnCallback(static function (ilDBStatement $statement) {
436 return $statement->fetch(PDO::FETCH_ASSOC);
437 });
438
439 $logger = $this->getMockBuilder(ilLogger::class)
440 ->disableOriginalConstructor()
441 ->getMock();
442
443 $objectDataCache = $this->getMockBuilder(ilObjectDataCache::class)
444 ->disableOriginalConstructor()
445 ->getMock();
446
448 $database,
449 $logger,
450 $objectDataCache
451 );
452
453 $templates = $repository->fetchActiveCertificateTemplatesForCoursesWithDisabledLearningProgress(
454 $isGlobalLpEnabled
455 );
456 $this->assertCount(count($template_recods), $templates);
457 }
458
460 {
461 $wrappedTemplateRepository = $this->getMockBuilder(ilCertificateTemplateRepository::class)->getMock();
462 $wrappedTemplateRepository
463 ->expects($this->exactly(2))
464 ->method('fetchActiveCertificateTemplatesForCoursesWithDisabledLearningProgress')
465 ->willReturnCallback(static function (bool $isGlobalLpEnabled): array {
466 if ($isGlobalLpEnabled) {
467 return [];
468 }
469
470 return [
472 5,
473 'crs',
474 '<xml>Some Content</xml>',
475 md5('<xml>Some Content</xml>'),
476 '[]',
477 1,
478 'v5.4.0',
479 123_456_789,
480 true,
481 '/some/where/background.jpg',
482 '/some/where/thumbnail.svg',
483 '-',
484 '-',
485 1
486 ),
488 6,
489 'crs',
490 '<xml>Some Content</xml>',
491 md5('<xml>Some Content</xml>'),
492 '[]',
493 1,
494 'v5.4.0',
495 123_456_789,
496 true,
497 '/some/where/background.jpg',
498 '/some/where/thumbnail.svg',
499 '-',
500 '-',
501 5
502 ),
503 ];
504 });
505 $templateRepository = new ilCachedCertificateTemplateRepository($wrappedTemplateRepository);
506
507 $result1 = $templateRepository->fetchActiveCertificateTemplatesForCoursesWithDisabledLearningProgress(true);
508 // Do not delegate to wrapped repository again
509 $result2 = $templateRepository->fetchActiveCertificateTemplatesForCoursesWithDisabledLearningProgress(true);
510 // Should be delegated
511 $result3 = $templateRepository->fetchActiveCertificateTemplatesForCoursesWithDisabledLearningProgress(false);
512 // Do not delegate to wrapped repository again
513 $result4 = $templateRepository->fetchActiveCertificateTemplatesForCoursesWithDisabledLearningProgress(false);
514 // Do not delegate to wrapped repository again
515 $result5 = $templateRepository->fetchActiveCertificateTemplatesForCoursesWithDisabledLearningProgress(true);
516
517 $this->assertSame($result1, $result2);
518 $this->assertSame($result1, $result5);
519 $this->assertCount(0, $result1);
520
521 $this->assertNotSame($result1, $result3);
522 $this->assertSame($result3, $result4);
523 $this->assertCount(2, $result3);
524 }
525}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
testRetrievingCertificateTemplatesForCoursesWorksAsExpectedWhenUsingNonCachingRepository(bool $isGlobalLpEnabled, array $template_recods)
ilSetting $setting
Definition: class.ilias.php:68
A certicate template repository which caches results of query commands List of cached results (other ...
Abstract class ilLPStatus for all learning progress modes E.g ilLPStatusManual, ilLPStatusObjectives ...
const LP_STATUS_COMPLETED_NUM
const LP_STATUS_IN_PROGRESS_NUM
Component logger with individual log levels by component id.
class ilObjectDataCache
ILIAS Setting Class.
Interface ilDBInterface.
Interface ilDBStatement.
fetch(int $fetch_mode=ilDBConstants::FETCHMODE_ASSOC)