ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
ilEventDetectorTest.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2014 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
16 {
17  public function setUp()
18  {
19  include_once("./Services/PHPUnit/classes/class.ilUnitUtil.php");
20  //ilUnitUtil::performInitialisation();
21 
22  require_once './Services/WorkflowEngine/classes/utils/class.ilWorkflowUtils.php';
23 
24  // Empty workflow.
25  require_once './Services/WorkflowEngine/classes/workflows/class.ilEmptyWorkflow.php';
26  $this->workflow = new ilEmptyWorkflow();
27 
28  // Basic node
29  require_once './Services/WorkflowEngine/classes/nodes/class.ilBasicNode.php';
30  $this->node = new ilBasicNode($this->workflow);
31 
32  // Wiring up so the node is attached to the workflow.
33  $this->workflow->addNode($this->node);
34 
35  require_once './Services/WorkflowEngine/classes/detectors/class.ilEventDetector.php';
36  }
37 
38  public function tearDown()
39  {
40  global $ilSetting;
41  if ($ilSetting != null) {
42  $ilSetting->delete('IL_PHPUNIT_TEST_TIME');
43  $ilSetting->delete('IL_PHPUNIT_TEST_MICROTIME');
44  }
45  }
46 
47  public function testConstructorValidContext()
48  {
49  // Act
50  $detector = new ilEventDetector($this->node);
51 
52  // Assert
53  // No exception - good
54  $this->assertTrue(
55  true,
56  'Construction failed with valid context passed to constructor.'
57  );
58  }
59 
60  public function testIsListeningWithTimeFrame()
61  {
62  // Arrange
63  $detector = new ilEventDetector($this->node);
64  $timer_start = ilWorkflowUtils::time() + 5*60; # +5 Minutes from here.
65  $timer_end = 0;
66  $detector->setListeningTimeframe($timer_start, $timer_end);
67 
68  // Act
69  $actual = $detector->isListening();
70 
71  // Assert
72  $this->assertFalse($actual, 'Detector should not be listening.');
73  }
74 
76  {
77  // Arrange
78  $detector = new ilEventDetector($this->node);
79  $timer_start = ilWorkflowUtils::time() - 5*60; # -5 Minutes from now.
80  $timer_end = ilWorkflowUtils::time() - 1*60; # -1 Minute from now.
81  $detector->setListeningTimeframe($timer_start, $timer_end);
82 
83  // Act
84  $actual = $detector->isListening();
85 
86  // Assert
87  $this->assertFalse($actual, 'Detector should not be listening.');
88  }
89 
91  {
92  // Arrange
93  $detector = new ilEventDetector($this->node);
94  $timer_start = ilWorkflowUtils::time() - 5*60; # -5 Minutes from now.
95  $timer_end = 0; # Wildcard.
96  $detector->setListeningTimeframe($timer_start, $timer_end);
97 
98  // Act
99  $actual = $detector->isListening();
100 
101  // Assert
102  $this->assertTrue($actual, 'Detector should not be listening.');
103  }
104 
106  {
107  // Arrange
108  $detector = new ilEventDetector($this->node);
109  $timer_start = 0; # Wildcard.
110  $timer_end = ilWorkflowUtils::time() + 5*60; # +5 Minutes from now.
111  $detector->setListeningTimeframe($timer_start, $timer_end);
112 
113  // Act
114  $actual = $detector->isListening();
115 
116  // Assert
117  $this->assertTrue($actual, 'Detector should not be listening.');
118  }
119 
121  {
122  // Arrange
123  $detector = new ilEventDetector($this->node);
124 
125  // Act
126  $actual = $detector->isListening();
127 
128  // Assert
129  $this->assertTrue($actual, 'Detector should be listening.');
130  }
131 
133  {
134  // Arrange
135  $detector = new ilEventDetector($this->node);
136  $exp_start = 4711;
137  $exp_end = 4712;
138 
139  // Act
140  $detector->setListeningTimeframe($exp_start, $exp_end);
141  $act = $detector->getListeningTimeframe();
142 
143  // Assert
144  $this->assertEquals($exp_start . $exp_end, $act['listening_start'] . $act['listening_end']);
145  }
146 
151  {
152  // Arrange
153  $detector = new ilEventDetector($this->node);
154  $exp_start = 4712; # +5 Minutes from here.
155  $exp_end = 4711;
156 
157  // Act
158  $detector->setListeningTimeframe($exp_start, $exp_end);
159  $act = $detector->getListeningTimeframe();
160 
161  // Assert
162  $this->assertEquals($exp_start . $exp_end, $act['listening_start'] . $act['listening_end']);
163  }
164 
165  public function testSetGetDbId()
166  {
167  // Arrange
168  $detector = new ilEventDetector($this->node);
169  $expected = '1234';
170 
171  // Act
172  $detector->setDbId($expected);
173  $actual = $detector->getDbId();
174 
175  // Assert
176  $this->assertEquals($expected, $actual);
177  }
178 
182  public function testGetNonExistingDbId()
183  {
184  // Arrange
185  $detector = new ilEventDetector($this->node);
186  $expected = '1234';
187 
188  // Act
189  $actual = $detector->getDbId();
190 
191  // Assert
192  $this->assertEquals($expected, $actual);
193  }
194 
195  public function testHasDbIdSet()
196  {
197  // Arrange
198  $detector = new ilEventDetector($this->node);
199  $expected = '1234';
200 
201  // Act
202  $detector->setDbId($expected);
203  $actual = $detector->hasDbId();
204 
205  // Assert
206  $this->assertTrue($actual);
207  }
208 
209  public function testHasDbIdUnset()
210  {
211  // Arrange
212  $detector = new ilEventDetector($this->node);
213 
214  // Act
215  $actual = $detector->hasDbId();
216 
217  // Assert
218  $this->assertFalse($actual);
219  }
220 
221  public function testWriteDetectorToDb()
222  {
223  $this->markTestIncomplete(
224  '$ilDB throws notices during test.'
225  );
226  // Arrange
227  $detector = new ilEventDetector($this->node);
228 
229  require_once './Services/Database/classes/class.ilDBMySQL.php';
230  $ilDB_mock = $this->createMock('ilDBMySQL', array('nextID','quote','insert'), array(), '', false);
231 
232  $ilDB_mock->expects($this->once())->method('nextID')->with($this->equalTo('wfe_det_listening'))->will($this->returnValue(234));
233  $ilDB_mock->expects($this->any())->method('quote')->will($this->returnArgument(0));
234  $ilDB_mock->expects($this->any())->method('insert')->with(
235  'wfe_det_listening',
236  array(
237  'detector_id' => array('integer', 234),
238  'workflow_id' => array('integer', null),
239  'type' => array('text', '' ),
240  'content' => array('text', ''),
241  'subject_type' => array('text', ''),
242  'subject_id' => array('integer', ''),
243  'context_type' => array('text', ''),
244  'context_id' => array('integer', ''),
245  'listening_start' => array('integer', '0'),
246  'listening_end' => array('integer', '0')
247  )
248  )->will($this->returnValue(true));
249 
250  // Act
251  $stashed_real_object = @$GLOBALS['ilDB'];
252  $GLOBALS['ilDB'] = $ilDB_mock;
253 
254  // Act
255  $detector->writeDetectorToDb();
256  $actual = $detector->hasDbId();
257 
258  // Assert
259  $this->assertTrue($actual);
260 
261  $GLOBALS['ilDB'] = $stashed_real_object;
262  }
263 
264  public function testDeleteDetectorFromDb()
265  {
266  $this->markTestIncomplete(
267  '$ilDB throws notices during test.'
268  );
269 
270  // Arrange
271  $detector = new ilEventDetector($this->node);
272  require_once './Services/Database/classes/class.ilDBMySQL.php';
273  $ilDB_mock = $this->createMock('ilDBMySQL', array('quote','manipulate'), array(), '', false);
274 
275  $ilDB_mock->expects($this->any())->method('quote')->will($this->returnValue(234));
276  $ilDB_mock->expects($this->any())->method('manipulate')->with(
277  'DELETE
278  FROM wfe_det_listening
279  WHERE detector_id = 234'
280  )->will($this->returnValue(true));
281 
282  $stashed_real_object = @$GLOBALS['ilDB'];
283  $GLOBALS['ilDB'] = $ilDB_mock;
284 
285  // Act
286  $detector->setDbId(234);
287  $detector->deleteDetectorFromDb();
288  $actual = $detector->hasDbId();
289 
290  // Assert
291  $this->assertFalse($actual);
292  $GLOBALS['ilDB'] = $stashed_real_object;
293  }
294 
295  public function testOnActivate()
296  {
297  $this->markTestIncomplete(
298  '$ilDB throws notices during test.'
299  );
300  // Arrange
301  $detector = new ilEventDetector($this->node);
302 
303  require_once './Services/Database/classes/class.ilDBMySQL.php';
304  $ilDB_mock = $this->createMock('ilDBMySQL', array('nextID','quote','insert'), array(), '', false);
305 
306  $ilDB_mock->expects($this->once())->method('nextID')->with($this->equalTo('wfe_det_listening'))->will($this->returnValue(234));
307  $ilDB_mock->expects($this->any())->method('quote')->will($this->returnArgument(0));
308  $ilDB_mock->expects($this->any())->method('insert')->with(
309  'wfe_det_listening',
310  array(
311  'detector_id' => array('integer', 234),
312  'workflow_id' => array('integer', null),
313  'type' => array('text', '' ),
314  'content' => array('text', ''),
315  'subject_type' => array('text', ''),
316  'subject_id' => array('integer', ''),
317  'context_type' => array('text', ''),
318  'context_id' => array('integer', ''),
319  'listening_start' => array('integer', 0),
320  'listening_end' => array('integer', 0)
321  )
322  )->will($this->returnValue(true));
323 
324  $stashed_real_object = @$GLOBALS['ilDB'];
325  $GLOBALS['ilDB'] = $ilDB_mock;
326 
327  // Act
328  $detector->onActivate();
329  $actual = $detector->hasDbId();
330 
331  // Assert
332  $this->assertTrue($actual);
333  $this->assertEquals(234, $detector->getDbId());
334  $GLOBALS['ilDB'] = $stashed_real_object;
335  }
336 
337  public function testOnDeactivate()
338  {
339  $this->markTestIncomplete(
340  '$ilDB throws notices during test.'
341  );
342 
343  // Arrange
344  $detector = new ilEventDetector($this->node);
345 
346  require_once './Services/Database/classes/class.ilDBMySQL.php';
347  $ilDB_mock = $this->createMock('ilDBMySQL', array('quote','manipulate'), array(), '', false);
348 
349  $ilDB_mock->expects($this->any())->method('quote')->will($this->returnValue(234));
350  $ilDB_mock->expects($this->any())->method('manipulate')->with(
351  'DELETE
352  FROM wfe_det_listening
353  WHERE detector_id = 234'
354  )->will($this->returnValue(true));
355 
356  $stashed_real_object = @$GLOBALS['ilDB'];
357  $GLOBALS['ilDB'] = $ilDB_mock;
358 
359  // Act
360  $detector->setDbId(234);
361  $detector->onDeactivate();
362  $actual = $detector->hasDbId();
363 
364  // Assert
365  $this->assertFalse($actual);
366 
367  $GLOBALS['ilDB'] = $stashed_real_object;
368  }
369 
370  public function testSetGetEvent()
371  {
372  // Arrange INC
373  $detector = new ilEventDetector($this->node);
374  $exp_type = 'time_passed';
375  $exp_content = 'time_passed';
376 
377  // Act
378  $detector->setEvent($exp_type, $exp_content);
379 
380  // Assert
381  $event = $detector->getEvent();
382  $act_type = $event['type'];
383  $act_content = $event['content'];
384  $this->assertEquals($exp_type . $exp_content, $act_type . $act_content);
385  }
386 
387  public function testSetGetEventSubject()
388  {
389  // Arrange INC
390  $detector = new ilEventDetector($this->node);
391  $exp_type = 'none';
392  $exp_id = '0';
393 
394  // Act
395  $detector->setEventSubject($exp_type, $exp_id);
396 
397  // Assert
398  $event = $detector->getEventSubject();
399  $act_type = $event['type'];
400  $act_id = $event['identifier'];
401  $this->assertEquals($exp_type . $exp_id, $act_type . $act_id);
402  }
403 
404  public function testSetGetEventContext()
405  {
406  // Arrange INC
407  $detector = new ilEventDetector($this->node);
408  $exp_type = 'none';
409  $exp_id = '0';
410 
411  // Act
412  $detector->setEventContext($exp_type, $exp_id);
413 
414  // Assert
415  $event = $detector->getEventContext();
416  $act_type = $event['type'];
417  $act_id = $event['identifier'];
418  $this->assertEquals($exp_type . $exp_id, $act_type . $act_id);
419  }
420 
421  public function testGetContext()
422  {
423  // Arrange
424  $detector = new ilEventDetector($this->node);
425 
426  // Act
427  $actual = $detector->getContext();
428 
429  // Assert
430  if ($actual === $this->node) {
431  $this->assertEquals($actual, $this->node);
432  } else {
433  $this->assertTrue(false, 'Context not identical.');
434  }
435  }
436 
437  public function testTriggerValid()
438  {
439  // Arrange
440  $detector = new ilEventDetector($this->node);
441  $evt_type = 'testEvent';
442  $evt_content = 'content';
443  $detector->setEvent($evt_type, $evt_content);
444  $subj_type = 'usr';
445  $subj_id = 6;
446  $detector->setEventSubject($subj_type, $subj_id);
447  $ctx_type = 'crs';
448  $ctx_id = 48;
449  $detector->setEventContext($ctx_type, $ctx_id);
450  $params = array(
451  $evt_type, $evt_content,
452  $subj_type, $subj_id,
453  $ctx_type, $ctx_id
454  );
455 
456  // Act
457  $detector->trigger($params);
458 
459  // Assert
460  $actual = $detector->getDetectorState();
461  $this->assertTrue($actual);
462  }
463 
464  public function testTriggerValidTwice()
465  {
466  // Arrange
467  $detector = new ilEventDetector($this->node);
468  $evt_type = 'testEvent';
469  $evt_content = 'content';
470  $detector->setEvent($evt_type, $evt_content);
471  $subj_type = 'usr';
472  $subj_id = 6;
473  $detector->setEventSubject($subj_type, $subj_id);
474  $ctx_type = 'crs';
475  $ctx_id = 48;
476  $detector->setEventContext($ctx_type, $ctx_id);
477  $params = array(
478  $evt_type, $evt_content,
479  $subj_type, $subj_id,
480  $ctx_type, $ctx_id
481  );
482 
483  // Act
484  $this->assertTrue($detector->trigger($params), 'First trigger should receive a true state.');
485  $this->assertFalse($detector->trigger($params), 'Second trigger should receive a false state.');
486 
487  // Assert
488  $actual = $detector->getDetectorState();
489  $this->assertTrue($actual, 'After satisfaction of the trigger, detectorstate should be true.');
490  }
491 
492  // TODO Test wildcards!
493 
494  public function testTriggerInvalidContent()
495  {
496  // Arrange
497  $detector = new ilEventDetector($this->node);
498  $evt_type = 'testEvent';
499  $evt_content = 'content';
500  $detector->setEvent($evt_type, $evt_content);
501  $subj_type = 'usr';
502  $subj_id = 6;
503  $detector->setEventSubject($subj_type, $subj_id);
504  $ctx_type = 'crs';
505  $ctx_id = 48;
506  $detector->setEventContext($ctx_type, $ctx_id);
507  $params = array(
508  $evt_type, $evt_content . 'INVALIDATE',
509  $subj_type, $subj_id,
510  $ctx_type, $ctx_id
511  );
512 
513  // Act
514  $detector->trigger($params);
515 
516  // Assert
517  $actual = $detector->getDetectorState();
518  $this->assertFalse($actual);
519  }
520 
521  public function testTriggerInvalidType()
522  {
523  // Arrange
524  $detector = new ilEventDetector($this->node);
525  $evt_type = 'testEvent';
526  $evt_content = 'content';
527  $detector->setEvent($evt_type, $evt_content);
528  $subj_type = 'usr';
529  $subj_id = 6;
530  $detector->setEventSubject($subj_type, $subj_id);
531  $ctx_type = 'crs';
532  $ctx_id = 48;
533  $detector->setEventContext($ctx_type, $ctx_id);
534  $params = array(
535  $evt_type . 'INVALIDATE', $evt_content,
536  $subj_type, $subj_id,
537  $ctx_type, $ctx_id
538  );
539 
540  // Act
541  $detector->trigger($params);
542 
543  // Assert
544  $actual = $detector->getDetectorState();
545  $this->assertFalse($actual);
546  }
547 
549  {
550  // Arrange
551  $detector = new ilEventDetector($this->node);
552  $evt_type = 'testEvent';
553  $evt_content = 'content';
554  $detector->setEvent($evt_type, $evt_content);
555  $subj_type = 'usr';
556  $subj_id = 6;
557  $detector->setEventSubject($subj_type, $subj_id);
558  $ctx_type = 'crs';
559  $ctx_id = 48;
560  $detector->setEventContext($ctx_type, $ctx_id);
561  $params = array(
562  $evt_type, $evt_content,
563  $subj_type . 'INVALIDATE', $subj_id,
564  $ctx_type, $ctx_id
565  );
566 
567  // Act
568  $detector->trigger($params);
569 
570  // Assert
571  $actual = $detector->getDetectorState();
572  $this->assertFalse($actual);
573  }
574 
575  public function testTriggerInvalidSubjectId()
576  {
577  // Arrange
578  $detector = new ilEventDetector($this->node);
579  $evt_type = 'testEvent';
580  $evt_content = 'content';
581  $detector->setEvent($evt_type, $evt_content);
582  $subj_type = 'usr';
583  $subj_id = 6;
584  $detector->setEventSubject($subj_type, $subj_id);
585  $ctx_type = 'crs';
586  $ctx_id = 48;
587  $detector->setEventContext($ctx_type, $ctx_id);
588  $params = array(
589  $evt_type, $evt_content,
590  $subj_type, $subj_id . 'INVALIDATE',
591  $ctx_type, $ctx_id
592  );
593 
594  // Act
595  $detector->trigger($params);
596 
597  // Assert
598  $actual = $detector->getDetectorState();
599  $this->assertFalse($actual);
600  }
601 
603  {
604  // Arrange
605  $detector = new ilEventDetector($this->node);
606  $evt_type = 'testEvent';
607  $evt_content = 'content';
608  $detector->setEvent($evt_type, $evt_content);
609  $subj_type = 'usr';
610  $subj_id = 6;
611  $detector->setEventSubject($subj_type, $subj_id);
612  $ctx_type = 'crs';
613  $ctx_id = 48;
614  $detector->setEventContext($ctx_type, $ctx_id);
615  $params = array(
616  $evt_type, $evt_content,
617  $subj_type, $subj_id,
618  $ctx_type . 'INVALIDATE', $ctx_id
619  );
620 
621  // Act
622  $detector->trigger($params);
623 
624  // Assert
625  $actual = $detector->getDetectorState();
626  $this->assertFalse($actual);
627  }
628 
629  public function testTriggerInvalidContextId()
630  {
631  // Arrange
632  $detector = new ilEventDetector($this->node);
633  $evt_type = 'testEvent';
634  $evt_content = 'content';
635  $detector->setEvent($evt_type, $evt_content);
636  $subj_type = 'usr';
637  $subj_id = 6;
638  $detector->setEventSubject($subj_type, $subj_id);
639  $ctx_type = 'crs';
640  $ctx_id = 48;
641  $detector->setEventContext($ctx_type, $ctx_id);
642  $params = array(
643  $evt_type, $evt_content,
644  $subj_type, $subj_id,
645  $ctx_type, $ctx_id . 'INVALIDATE'
646  );
647 
648  // Act
649  $detector->trigger($params);
650 
651  // Assert
652  $actual = $detector->getDetectorState();
653  $this->assertFalse($actual);
654  }
655 }
$params
Definition: disable.php:11
testGetNonExistingDbId()
ilWorkflowObjectStateException
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
ilEventDetectorTest is part of the petri net based workflow engine.
PhpIncludeInspection
testSetGetIllegalListeningTimeframe()
ilWorkflowInvalidArgumentException
Create styles array
The data for the language used.
global $ilSetting
Definition: privfeed.php:17
PhpIncludeInspection
PhpIncludeInspection
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.