ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
ilTimerDetectorTest.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.ilTimerDetector.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 ilTimerDetector($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 testSetGetTimerStart()
62  {
63  // Arrange
64  $detector = new ilTimerDetector($this->node);
65  $expected = ilWorkflowUtils::time();
66 
67  // Act
68  $detector->setTimerStart($expected);
69  $actual = $detector->getTimerStart();
70 
71  // Assert
72  $this->assertEquals($actual, $expected);
73  }
74 
75  public function testSetGetTimerLimit()
76  {
77  // Arrange
78  $detector = new ilTimerDetector($this->node);
79  $expected = 5*60*60;
80 
81  // Act
82  $detector->setTimerLimit($expected);
83  $actual = $detector->getTimerLimit();
84 
85  // Assert
86  $this->assertEquals($actual, $expected);
87  }
88 
89  public function testTriggerEarly()
90  {
91  // Arrange
92  $detector = new ilTimerDetector($this->node);
93  $timer_start = ilWorkflowUtils::time(); # +5 Minutes from here.
94  $timer_limit = 5*60;
95  $detector->setTimerStart($timer_start);
96  $detector->setTimerLimit($timer_limit);
97 
98  // Act
99  $detector->trigger(null);
100 
101  // Assert
102  $actual = $detector->getDetectorState();
103  $this->assertFalse($actual, 'Early trigger should not satisfy detector');
104  }
105 
106  public function testTriggerValid()
107  {
108  // Arrange
109  $detector = new ilTimerDetector($this->node);
110  $timer_start = ilWorkflowUtils::time(); # +5 Minutes from now.
111  $timer_limit = 0;
112  $detector->setTimerStart($timer_start);
113  $detector->setTimerLimit($timer_limit);
114 
115  // Act
116  $detector->trigger(null);
117 
118  // Assert
119  $actual = $detector->getDetectorState();
120  $this->assertTrue($actual, 'Trigger should not satisfy detector');
121  }
122 
123  public function testTriggerValidTwice()
124  {
125  // Arrange
126  $detector = new ilTimerDetector($this->node);
127  $timer_start = ilWorkflowUtils::time(); # +5 Minutes from now.
128  $timer_limit = 0;
129  $detector->setTimerStart($timer_start);
130  $detector->setTimerLimit($timer_limit);
131 
132  // Act
133  $detector->trigger(null);
134  $actual = $detector->trigger(null);
135 
136  // Assert
137  $this->assertFalse($actual, 'Detector should be satisfied after single trigger');
138  }
139 
141  {
142  // Arrange
143  $detector = new ilTimerDetector($this->node);
144  $timer_start = ilWorkflowUtils::time() + 5*60; # +5 Minutes from here.
145  $timer_end = 0;
146  $detector->setListeningTimeframe($timer_start, $timer_end);
147 
148  // Act
149  $actual = $detector->isListening();
150 
151  // Assert
152  $this->assertFalse($actual, 'Detector should not be listening.');
153  }
154 
156  {
157  // Arrange
158  $detector = new ilTimerDetector($this->node);
159  $timer_start = ilWorkflowUtils::time() + 5*60; # +5 Minutes from here.
160  $timer_end = 0;
161 
162  // Act
163  $actual = $detector->isListening();
164 
165  // Assert
166  $this->assertTrue($actual, 'Detector should be listening.');
167  }
168 
173  {
174  // Arrange
175  $detector = new ilTimerDetector($this->node);
176  $exp_start = 4712;
177  $exp_end = 4711;
178 
179  // Act
180  $detector->setListeningTimeframe($exp_start, $exp_end);
181  $act = $detector->getListeningTimeframe();
182 
183  // Assert
184  $this->assertEquals($exp_start.$exp_end, $act['listening_start'].$act['listening_end']);
185  }
186 
188  {
189  // Arrange
190  $detector = new ilTimerDetector($this->node);
191  $timer_start = ilWorkflowUtils::time() - 5*60; # -5 Minutes from now.
192  $timer_end = ilWorkflowUtils::time() - 1*60; # -1 Minute from now.
193  $detector->setListeningTimeframe($timer_start, $timer_end);
194 
195  // Act
196  $actual = $detector->isListening();
197 
198  // Assert
199  $this->assertFalse($actual, 'Detector should not be listening.');
200  }
201 
203  {
204  // Arrange
205  $detector = new ilTimerDetector($this->node);
206  $timer_start = ilWorkflowUtils::time() - 5*60; # -5 Minutes from now.
207  $timer_end = 0; # Wildcard.
208  $detector->setListeningTimeframe($timer_start, $timer_end);
209 
210  // Act
211  $actual = $detector->isListening();
212 
213  // Assert
214  $this->assertTrue($actual, 'Detector should not be listening.');
215  }
216 
218  {
219  // Arrange
220  $detector = new ilTimerDetector($this->node);
221  $timer_start = 0; # Wildcard.
222  $timer_end = ilWorkflowUtils::time() + 5*60; # +5 Minutes from now.
223  $detector->setListeningTimeframe($timer_start, $timer_end);
224 
225  // Act
226  $actual = $detector->isListening();
227 
228  // Assert
229  $this->assertTrue($actual, 'Detector should not be listening.');
230  }
231 
233  {
234  // Arrange
235  $detector = new ilTimerDetector($this->node);
236  $exp_start = 4711; # +5 Minutes from here.
237  $exp_end = 4712;
238 
239  // Act
240  $detector->setListeningTimeframe($exp_start, $exp_end);
241  $act = $detector->getListeningTimeframe();
242 
243  // Assert
244  $this->assertEquals($exp_start.$exp_end, $act['listening_start'].$act['listening_end']);
245  }
246 
247  public function testSetGetDbId()
248  {
249  // Arrange
250  $detector = new ilTimerDetector($this->node);
251  $expected = '1234';
252 
253  // Act
254  $detector->setDbId($expected);
255  $actual = $detector->getDbId();
256 
257  // Assert
258  $this->assertEquals($expected, $actual);
259  }
260 
261  public function testHasDbIdSet()
262  {
263  // Arrange
264  $detector = new ilTimerDetector($this->node);
265  $expected = '1234';
266 
267  // Act
268  $detector->setDbId($expected);
269  $actual = $detector->hasDbId();
270 
271  // Assert
272  $this->assertTrue($actual);
273  }
274 
278  public function testGetNonExistingDbId()
279  {
280  // Arrange
281  $detector = new ilTimerDetector($this->node);
282  $expected = '1234';
283 
284  // Act
285  $actual = $detector->getDbId();
286 
287  // Assert
288  $this->assertEquals($expected, $actual);
289  }
290 
291  public function testHasDbIdUnset()
292  {
293  // Arrange
294  $detector = new ilTimerDetector($this->node);
295 
296  // Act
297  $actual = $detector->hasDbId();
298 
299  // Assert
300  $this->assertFalse($actual);
301  }
302 
303  public function testWriteDetectorToDb()
304  {
305  $this->markTestIncomplete(
306  '$ilDB throws notices during test.'
307  );
308  return;
309  // Arrange
310  $detector = new ilTimerDetector($this->node);
311 
312  require_once './Services/Database/classes/class.ilDBMySQL.php';
313  $ilDB_mock = $this->getMock('ilDBMySQL',array('nextID','quote','insert'),array(),'', FALSE);
314 
315  $ilDB_mock->expects($this->once())->method('nextID')->with($this->equalTo('wfe_det_listening'))->will($this->returnValue(234));
316  $ilDB_mock->expects($this->any())->method('quote')->will($this->returnArgument(0));
317  $ilDB_mock->expects($this->any())->method('insert')->with(
318  'wfe_det_listening',
319  array(
320  'detector_id' => array ('integer', 234),
321  'workflow_id' => array ('integer', null),
322  'type' => array ('text', 'time_passed' ),
323  'content' => array ('text', 'time_passed'),
324  'subject_type' => array ('text', 'none'),
325  'subject_id' => array ('integer', 0),
326  'context_type' => array ('text', 'none'),
327  'context_id' => array ('integer', 0),
328  'listening_start' => array ('integer', 0),
329  'listening_end' => array ('integer', 0)
330  )
331  )->will($this->returnValue(true));
332 
333  // Act
334  $stashed_real_object = @$GLOBALS['ilDB'];
335  $GLOBALS['ilDB'] = $ilDB_mock;
336  $detector->writeDetectorToDb();
337 
338  $actual = $detector->hasDbId();
339 
340  // Assert
341  $this->assertTrue($actual);
342 
343  $GLOBALS['ilDB'] = $stashed_real_object;
344  }
345 
346  public function testDeleteDetectorFromDb()
347  {
348  $this->markTestIncomplete(
349  '$ilDB throws notices during test.'
350  );
351  return;
352  // Arrange
353  $detector = new ilTimerDetector($this->node);
354 
355  require_once './Services/Database/classes/class.ilDBMySQL.php';
356  $ilDB_mock = $this->getMock('ilDBMySQL',array('quote','manipulate'),array(),'', FALSE);
357 
358  $ilDB_mock->expects($this->any())->method('quote')->will($this->returnValue(234));
359  $ilDB_mock->expects($this->any())->method('manipulate')->with(
360  'DELETE
361  FROM wfe_det_listening
362  WHERE detector_id = 234'
363  )->will($this->returnValue(true));
364 
365  $stashed_real_object = @$GLOBALS['ilDB'];
366  $GLOBALS['ilDB'] = $ilDB_mock;
367 
368  // Act
369  $detector->setDbId(234);
370  $detector->deleteDetectorFromDb();
371  $actual = $detector->hasDbId();
372 
373  // Assert
374  $this->assertFalse($actual);
375  $GLOBALS['ilDB'] = $stashed_real_object;
376  }
377 
378  public function testOnActivate()
379  {
380  $this->markTestIncomplete(
381  '$ilDB throws notices during test.'
382  );
383  return;
384  // Arrange
385  $detector = new ilTimerDetector($this->node);
386 
387  require_once './Services/Database/classes/class.ilDBMySQL.php';
388  $ilDB_mock = $this->getMock('ilDBMySQL',array('nextID','quote','insert'),array(),'', FALSE);
389 
390  $ilDB_mock->expects($this->once())->method('nextID')->with($this->equalTo('wfe_det_listening'))->will($this->returnValue(234));
391  $ilDB_mock->expects($this->any())->method('quote')->will($this->returnArgument(0));
392  $ilDB_mock->expects($this->any())->method('insert')->with(
393  'wfe_det_listening',
394  array(
395  'detector_id' => array ('integer', 234),
396  'workflow_id' => array ('integer', null),
397  'type' => array ('text', 'time_passed' ),
398  'content' => array ('text', 'time_passed'),
399  'subject_type' => array ('text', 'none'),
400  'subject_id' => array ('integer', 0),
401  'context_type' => array ('text', 'none'),
402  'context_id' => array ('integer', 0),
403  'listening_start' => array ('integer', 0),
404  'listening_end' => array ('integer', 0)
405  )
406  )->will($this->returnValue(true));
407 
408  $stashed_real_object = @$GLOBALS['ilDB'];
409  $GLOBALS['ilDB'] = $ilDB_mock;
410 
411  // Act
412  $detector->onActivate();
413  $actual = $detector->hasDbId();
414  //$detector->onDeactivate();
415 
416  // Assert
417  $this->assertTrue($actual);
418  $this->assertEquals(234, $detector->getDbId());
419  $GLOBALS['ilDB'] = $stashed_real_object;
420  }
421 
422  public function testOnDeactivate()
423  {
424  $this->markTestIncomplete(
425  '$ilDB throws notices during test.'
426  );
427  return;
428  // Arrange
429  $detector = new ilTimerDetector($this->node);
430 
431  require_once './Services/Database/classes/class.ilDBMySQL.php';
432  $ilDB_mock = $this->getMock('ilDBMySQL',array('quote','manipulate'),array(),'', FALSE);
433 
434  $ilDB_mock->expects($this->any())->method('quote')->will($this->returnValue(234));
435  $ilDB_mock->expects($this->any())->method('manipulate')->with(
436  'DELETE
437  FROM wfe_det_listening
438  WHERE detector_id = 234'
439  )->will($this->returnValue(true));
440 
441  $stashed_real_object = @$GLOBALS['ilDB'];
442  $GLOBALS['ilDB'] = $ilDB_mock;
443 
444  // Act
445  $detector->setDbId(234);
446  $detector->onDeactivate();
447  $actual = $detector->hasDbId();
448 
449  // Assert
450  $this->assertFalse($actual);
451 
452  $GLOBALS['ilDB'] = $stashed_real_object;
453  }
454 
455  public function testGetEvent()
456  {
457  // Arrange
458  $detector = new ilTimerDetector($this->node);
459  $exp_type = 'time_passed';
460  $exp_content = 'time_passed';
461 
462  // Act
463 
464  // Assert
465  $event = $detector->getEvent();
466  $act_type = $event['type'];
467  $act_content = $event['content'];
468  $this->assertEquals($exp_type.$exp_content, $act_type.$act_content);
469  }
470 
471  public function testGetEventSubject()
472  {
473  // Arrange
474  $detector = new ilTimerDetector($this->node);
475  $exp_type = 'none';
476  $exp_id = '0';
477 
478  // Act
479 
480  // Assert
481  $event = $detector->getEventSubject();
482  $act_type = $event['type'];
483  $act_id = $event['identifier'];
484  $this->assertEquals($exp_type.$exp_id, $act_type.$act_id);
485  }
486 
487  public function testGetEventContext()
488  {
489  // Arrange
490  $detector = new ilTimerDetector($this->node);
491  $exp_type = 'none';
492  $exp_id = '0';
493 
494  // Act
495 
496  // Assert
497  $event = $detector->getEventContext();
498  $act_type = $event['type'];
499  $act_id = $event['identifier'];
500  $this->assertEquals($exp_type.$exp_id, $act_type.$act_id);
501  }
502 
503  public function testGetContext()
504  {
505  // Arrange
506  $detector = new ilTimerDetector($this->node);
507 
508  // Act
509  $actual = $detector->getContext();
510 
511  // Assert
512  if ($actual === $this->node)
513  {
514  $this->assertEquals($actual, $this->node);
515  } else {
516  $this->assertTrue(false, 'Context not identical.');
517  }
518  }
519 }
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
PhpIncludeInspection
PhpIncludeInspection
ilTimerDetectorTest is part of the petri net based workflow engine.
Create styles array
The data for the language used.
global $ilSetting
Definition: privfeed.php:17
PhpIncludeInspection
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
testSetGetIllegalListeningTimeframe()
ilWorkflowInvalidArgumentException
testGetNonExistingDbId()
ilWorkflowObjectStateException