ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
ilConditionalNodeTest.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  // Empty workflow.
23  require_once './Services/WorkflowEngine/classes/workflows/class.ilEmptyWorkflow.php';
24  $this->workflow = new ilEmptyWorkflow();
25  }
26 
27  public function tearDown()
28  {
29  global $ilSetting;
30  if ($ilSetting != null) {
31  $ilSetting->delete('IL_PHPUNIT_TEST_TIME');
32  $ilSetting->delete('IL_PHPUNIT_TEST_MICROTIME');
33  }
34  }
35 
36  public function testConstructorValidContext()
37  {
38  // Act
39  $node = new ilConditionalNode($this->workflow);
40 
41  // Assert
42  // No exception - good
43  $this->assertTrue(
44  true,
45  'Construction failed with valid context passed to constructor.'
46  );
47  }
48 
49  public function testGetContext()
50  {
51  // Arrange
52  $node = new ilConditionalNode($this->workflow);
53 
54  // Act
55  $actual = $node->getContext();
56 
57  // Assert
58  if ($actual === $this->workflow) {
59  $this->assertEquals($actual, $this->workflow);
60  } else {
61  $this->assertTrue(false, 'Context not identical.');
62  }
63  }
64 
65  public function testIsActiveAndActivate()
66  {
67  // Arrange
68  $node = new ilConditionalNode($this->workflow);
69  require_once './Services/WorkflowEngine/classes/detectors/class.ilSimpleDetector.php';
70  $detector = new ilSimpleDetector($node);
71  $node->addDetector($detector);
72  $evaluation_expression = 'return null;';
73  $node->setEvaluationExpression($evaluation_expression);
74 
75  // Act
76  $node->activate();
77 
78  // Assert
79  $actual = $node->isActive();
80  $this->assertTrue($actual);
81  }
82 
83  public function testDeactivate()
84  {
85  // Arrange
86  $node = new ilConditionalNode($this->workflow);
87  require_once './Services/WorkflowEngine/classes/detectors/class.ilSimpleDetector.php';
88  $detector = new ilSimpleDetector($node);
89  $node->addDetector($detector);
90  $evaluation_expression = 'return null;';
91  $node->setEvaluationExpression($evaluation_expression);
92 
93  // Act
94  $node->activate();
95  $was_activated = $node->isActive();
96  $node->deactivate();
97  $was_deactivated = !$node->isActive();
98 
99  // Assert
100  $this->assertEquals($was_activated, $was_deactivated);
101  }
102 
104  {
105  // Arrange
106  $node = new ilConditionalNode($this->workflow);
107  require_once './Services/WorkflowEngine/classes/detectors/class.ilSimpleDetector.php';
108  $detector1 = new ilSimpleDetector($node);
109  $node->addDetector($detector1);
110  $detector2 = new ilSimpleDetector($node);
111  $node->addDetector($detector2);
112 
113  $evaluation_expression =
114  '
115  $retval = null;
116  if ($detectors[0]->getDetectorState() == false
117  && $detectors[1]->getDetectorState() == true)
118  {
119  $retval = false;
120  }
121 
122  if ($detectors[0]->getDetectorState() == true
123  && $detectors[1]->getDetectorState() == false)
124  {
125  $retval = true;
126  }
127  return $retval;
128  ';
129 
130  $node->setEvaluationExpression($evaluation_expression);
131  $detector1->trigger(null);
132 
133  // Act
134  $preconditions = $node->checkTransitionPreconditions();
135 
136  // Assert
137  $this->assertTrue($preconditions);
138  }
139 
141  {
142  // Arrange
143  $node = new ilConditionalNode($this->workflow);
144  require_once './Services/WorkflowEngine/classes/detectors/class.ilSimpleDetector.php';
145  $detector1 = new ilSimpleDetector($node);
146  $node->addDetector($detector1);
147  $detector2 = new ilSimpleDetector($node);
148  $node->addDetector($detector2);
149 
150  $evaluation_expression =
151  '
152  $retval = null;
153  if ($detectors[0]->getDetectorState() == false
154  && $detectors[1]->getDetectorState() == true)
155  {
156  $retval = false;
157  }
158 
159  if ($detectors[0]->getDetectorState() == true
160  && $detectors[1]->getDetectorState() == false)
161  {
162  $retval = true;
163  }
164  return $retval;
165  ';
166 
167  $node->setEvaluationExpression($evaluation_expression);
168  $detector2->trigger(null);
169 
170  // Act
171  $preconditions = $node->checkTransitionPreconditions();
172 
173  // Assert
174  $this->assertTrue($preconditions);
175  }
176 
178  {
179  // Arrange
180  $node = new ilConditionalNode($this->workflow);
181  require_once './Services/WorkflowEngine/classes/detectors/class.ilSimpleDetector.php';
182  $detector1 = new ilSimpleDetector($node);
183  $node->addDetector($detector1);
184  $detector2 = new ilSimpleDetector($node);
185  $node->addDetector($detector2);
186 
187  $evaluation_expression =
188  '
189  $retval = null;
190  if ($detectors[0]->getDetectorState() == false
191  && $detectors[1]->getDetectorState() == true)
192  {
193  $retval = false;
194  }
195 
196  if ($detectors[0]->getDetectorState() == true
197  && $detectors[1]->getDetectorState() == false)
198  {
199  $retval = true;
200  }
201  return $retval;
202  ';
203 
204  $node->setEvaluationExpression($evaluation_expression);
205 
206  // Act
207  $preconditions = $node->checkTransitionPreconditions();
208 
209  // Assert
210  $this->assertFalse($preconditions);
211  }
212 
213  public function testExecuteTransitionThen()
214  {
215  // Arrange
216  $node = new ilConditionalNode($this->workflow);
217  require_once './Services/WorkflowEngine/classes/detectors/class.ilSimpleDetector.php';
218  $detector = new ilSimpleDetector($node);
219  $node->addDetector($detector);
220  $node->activate();
221 
222  // Act
223  $node->executeTransition();
224  $state = $node->isActive();
225 
226  // Assert
227  $this->assertFalse($state);
228  }
229 
230  public function testExecuteTransitionElse()
231  { // This is test #100 of the WorkflowEngine, written on 9th of May, 2012
232  // @ 14:15
233 
234  // Arrange
235  $node = new ilConditionalNode($this->workflow);
236  require_once './Services/WorkflowEngine/classes/detectors/class.ilSimpleDetector.php';
237  $detector = new ilSimpleDetector($node);
238  $node->addDetector($detector);
239  $node->activate();
240 
241  // Act
242  $node->executeElseTransition();
243  $state = $node->isActive();
244 
245  // Assert
246  $this->assertFalse($state);
247  }
248 
250  {
251  // Arrange
252  $node = new ilConditionalNode($this->workflow);
253  require_once './Services/WorkflowEngine/classes/detectors/class.ilSimpleDetector.php';
254  $detector = new ilSimpleDetector($node);
255  $node->addDetector($detector);
256 
257  require_once './Services/WorkflowEngine/classes/activities/class.ilLoggingActivity.php';
258  $activity = new ilLoggingActivity($node);
259  $activity->setLogFile('ilTransitionLog.txt');
260  $activity->setLogLevel('MESSAGE');
261  $activity->setLogMessage('TEST');
262  $node->addActivity($activity);
263 
264  // Act
265  $node->activate();
266  $node->executeTransition();
267 
268  // Assert
269  $expected = ' :: MESSAGE :: TEST';
270  $fp = fopen('ilTransitionLog.txt', 'r');
271  $line = fgets($fp);
272  $actual = substr($line, 25, strlen($line)-27);
273  @unlink('ilTransitionLog.txt'); // TODO: Use vfsStream
274  $this->assertEquals(
275  $actual,
276  $expected
277  );
278  }
279 
281  {
282  // Arrange
283  $node = new ilConditionalNode($this->workflow);
284  require_once './Services/WorkflowEngine/classes/detectors/class.ilSimpleDetector.php';
285  $detector = new ilSimpleDetector($node);
286  $node->addDetector($detector);
287 
288  require_once './Services/WorkflowEngine/classes/activities/class.ilLoggingActivity.php';
289  $activity = new ilLoggingActivity($node);
290  $activity->setLogFile('ilTransitionLog.txt');
291  $activity->setLogLevel('MESSAGE');
292  $activity->setLogMessage('TEST');
293  $node->addActivity($activity, true);
294 
295  // Act
296  $node->activate();
297  $node->executeElseTransition();
298 
299  // Assert
300  $expected = ' :: MESSAGE :: TEST';
301  $fp = fopen('ilTransitionLog.txt', 'r');
302  $line = fgets($fp);
303  $actual = substr($line, 25, strlen($line)-27);
304  @unlink('ilTransitionLog.txt'); // TODO: Use vfsStream
305  $this->assertEquals(
306  $actual,
307  $expected
308  );
309  }
310 
312  {
313  // Arrange
314  $node = new ilConditionalNode($this->workflow);
315  require_once './Services/WorkflowEngine/classes/detectors/class.ilSimpleDetector.php';
316  $detector = new ilSimpleDetector($node);
317  $node->addDetector($detector);
318 
319  require_once './Services/WorkflowEngine/classes/emitters/class.ilActivationEmitter.php';
320  $t_node = new ilBasicNode($this->workflow);
321  $t_detector = new ilSimpleDetector($t_node);
322  $t_node->addDetector($t_detector);
323  $foo_detector = new ilSimpleDetector($t_node);
324  $t_node->addDetector($foo_detector);
325  // again a foo_detector to keep the t_node from transitting
326 
327  $emitter = new ilActivationEmitter($node);
328  $emitter->setTargetDetector($t_detector);
329  $node->addEmitter($emitter);
330 
331  // Act
332  $node->activate();
333  $node->executeTransition();
334 
335  // Assert
336  $this->assertTrue($t_node->isActive());
337  }
338 
340  {
341  // Arrange
342  $node = new ilConditionalNode($this->workflow);
343  require_once './Services/WorkflowEngine/classes/detectors/class.ilSimpleDetector.php';
344  $detector = new ilSimpleDetector($node);
345  $node->addDetector($detector);
346 
347  require_once './Services/WorkflowEngine/classes/emitters/class.ilActivationEmitter.php';
348  $t_node = new ilBasicNode($this->workflow);
349  $t_detector = new ilSimpleDetector($t_node);
350  $t_node->addDetector($t_detector);
351  $foo_detector = new ilSimpleDetector($t_node);
352  $t_node->addDetector($foo_detector);
353  // again a foo_detector to keep the t_node from transitting
354 
355  $emitter = new ilActivationEmitter($node);
356  $emitter->setTargetDetector($t_detector);
357  $node->addEmitter($emitter, true);
358 
359  // Act
360  $node->activate();
361  $node->executeElseTransition();
362 
363  // Assert
364  $this->assertTrue($t_node->isActive());
365  }
366 
368  {
369  // Arrange
370  $node = new ilConditionalNode($this->workflow);
371  require_once './Services/WorkflowEngine/classes/detectors/class.ilSimpleDetector.php';
372  $detector = new ilSimpleDetector($node);
373  $node->addDetector($detector);
374 
375  require_once './Services/WorkflowEngine/classes/emitters/class.ilActivationEmitter.php';
376  $t_node = new ilBasicNode($this->workflow);
377  $t_detector = new ilSimpleDetector($t_node);
378  $t_node->addDetector($t_detector);
379  $foo_detector = new ilSimpleDetector($t_node);
380  $t_node->addDetector($foo_detector);
381  // again a foo_detector to keep the t_node from transitting
382 
383  $emitter = new ilActivationEmitter($node);
384  $emitter->setTargetDetector($t_detector);
385  $node->addEmitter($emitter, true);
386  $node->setEvaluationExpression("return false;");
387  // Act
388  $node->activate();
389  $node->AttemptTransition();
390 
391  // Assert
392  $this->assertTrue($t_node->isActive());
393  }
394 
395  public function testAddDetectorFirst()
396  {
397  // Arrange
398  $node = new ilConditionalNode($this->workflow);
399  require_once './Services/WorkflowEngine/classes/detectors/class.ilSimpleDetector.php';
400  $detector = new ilSimpleDetector($node);
401 
402  // Act
403  $node->addDetector($detector);
404  $detectors = $node->getDetectors();
405 
406  // Assert
407  $this->assertEquals($detector, $detectors[0]);
408  }
409 
410  public function testAddEmitterFirst()
411  {
412  // Arrange
413  $node = new ilConditionalNode($this->workflow);
414  require_once './Services/WorkflowEngine/classes/emitters/class.ilSimpleEmitter.php';
415  $emitter = new ilSimpleEmitter($node);
416 
417  // Act
418  $node->addEmitter($emitter);
419  $emitters = $node->getEmitters();
420 
421  // Assert
422  $this->assertEquals($emitter, $emitters[0]);
423  }
424 
425  public function testAddElseEmitterFirst()
426  {
427  // Arrange
428  $node = new ilConditionalNode($this->workflow);
429  require_once './Services/WorkflowEngine/classes/emitters/class.ilSimpleEmitter.php';
430  $emitter = new ilSimpleEmitter($node);
431 
432  // Act
433  $node->addEmitter($emitter, true);
434  $emitters = $node->getEmitters(true);
435 
436  // Assert
437  $this->assertEquals($emitter, $emitters[0]);
438  }
439 
440  public function testAddActivityFirst()
441  {
442  // Arrange
443  $node = new ilConditionalNode($this->workflow);
444  require_once './Services/WorkflowEngine/classes/activities/class.ilLoggingActivity.php';
445  $activity = new ilLoggingActivity($node);
446 
447  // Act
448  $node->addActivity($activity);
449  $activities = $node->getActivities();
450 
451  // Assert
452  $this->assertEquals($activity, $activities[0]);
453  }
454 
455  public function testAddElseActivityFirst()
456  {
457  // Arrange
458  $node = new ilConditionalNode($this->workflow);
459  require_once './Services/WorkflowEngine/classes/activities/class.ilLoggingActivity.php';
460  $activity = new ilLoggingActivity($node);
461 
462  // Act
463  $node->addActivity($activity, true);
464  $activities = $node->getActivities(true);
465 
466  // Assert
467  $this->assertEquals($activity, $activities[0]);
468  }
469 
471  {
472  // Arrange
473  $node = new ilConditionalNode($this->workflow);
474 
475  require_once './Services/WorkflowEngine/classes/detectors/class.ilSimpleDetector.php';
476  $detector = new ilSimpleDetector($node);
477  $node->addDetector($detector);
478  $detector->setDetectorState(true);
479  $node->setEvaluationExpression('return true;');
480 
481  $node->activate();
482  /* Setting the detector to true will actually be reported
483  * with notifyDetectorSatisfaction.
484  * To isolate this call, we need to reset the node back
485  * to active prior to evaluating if it successfully executes
486  * the transition and sets itself to inactive.
487  */
488 
489  // Act
490  $node->notifyDetectorSatisfaction($detector);
491 
492  // Assert
493  $this->assertFalse($node->isActive());
494  }
495 
497  {
498  // Arrange
499  $node = new ilConditionalNode($this->workflow);
500  require_once './Services/WorkflowEngine/classes/detectors/class.ilSimpleDetector.php';
501  $detector1 = new ilSimpleDetector($node);
502  $node->addDetector($detector1);
503  $detector2 = new ilSimpleDetector($node);
504  $node->addDetector($detector2);
505  $node->setEvaluationExpression('return $GLOBALS["expr_retval"];');
506  $GLOBALS['expr_retval'] = null;
507 
508  // Act
509  $node->activate();
510  $this->assertTrue($node->isActive());
511  $GLOBALS['expr_retval'] = true;
512 
513  $detector1->trigger(null);
514  $detector2->trigger(null);
515  // TODO: Assert something more meaningful here.
516  $this->assertFalse($node->isActive());
517  }
518 }
PhpIncludeInspection
ilConditionalNodeTest is part of the petri net based workflow engine.
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
PhpIncludeInspection
if(!array_key_exists('stateid', $_REQUEST)) $state
Handle linkback() response from LinkedIn.
Definition: linkback.php:10
PhpIncludeInspection
PhpIncludeInspection
global $ilSetting
Definition: privfeed.php:17
PhpIncludeInspection
PhpIncludeInspection