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