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