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