ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
ilLoggingActivityTest.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2014 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4use org\bovigo\vfs;
5use PHPUnit\Framework\TestCase;
6
17class ilLoggingActivityTest extends TestCase
18{
20 public $test_dir;
21
22 public function setUp() : void
23 {
24 chdir(dirname(__FILE__));
25 chdir('../../../../');
26
27 try {
28 include_once("./Services/PHPUnit/classes/class.ilUnitUtil.php");
29 //ilUnitUtil::performInitialisation();
30 } catch (Exception $exception) {
31 if (!defined('IL_PHPUNIT_TEST')) {
32 define('IL_PHPUNIT_TEST', false);
33 }
34 }
35
36 // Empty workflow.
37 require_once './Services/WorkflowEngine/classes/workflows/class.ilEmptyWorkflow.php';
38 $this->workflow = new ilEmptyWorkflow();
39
40 // Basic node
41 require_once './Services/WorkflowEngine/classes/nodes/class.ilBasicNode.php';
42 $this->node = new ilBasicNode($this->workflow);
43
44 // Wiring up so the node is attached to the workflow.
45 $this->workflow->addNode($this->node);
46
47 require_once './Services/WorkflowEngine/classes/activities/class.ilLoggingActivity.php';
48
49 $this->test_dir = vfs\vfsStream::setup('example');
50 }
51
52 public function tearDown() : void
53 {
54 global $ilSetting;
55 if ($ilSetting != null) {
56 //$ilSetting->delete('IL_PHPUNIT_TEST_TIME');
57 //$ilSetting->delete('IL_PHPUNIT_TEST_MICROTIME');
58 }
59 }
60
62 {
63 // Act
64 $activity = new ilLoggingActivity($this->node);
65
66 // Assert
67 // No exception - good
68 $this->assertTrue(
69 true,
70 'Construction failed with valid context passed to constructor.'
71 );
72 }
73
74 public function testSetGetValidLogFile()
75 {
76 // Arrange
77 $activity = new ilLoggingActivity($this->node);
78 $expected = './Services/WorkflowEngine/test/testlog.txt';
79
80 // Act
81 $activity->setLogFile($expected);
82 $actual = $activity->getLogFile();
83
84 $this->assertEquals(
85 $actual,
86 $expected,
87 'Valid log file was given, returned value differed.'
88 );
89 }
90
95 {
96 $this->expectException(ilWorkflowFilesystemException::class);
97
98 // Arrange
99 $activity = new ilLoggingActivity($this->node);
100 $expected = '/dev/ilias_unit_test_log_file_can_be_deleted_safely.txt';
101
102 // Act
103 $activity->setLogFile($expected);
104 $actual = $activity->getLogFile();
105
106 // Assertion via phpdoc. (Exception)
107 }
108
113 {
114 $this->expectException(ilWorkflowObjectStateException::class);
115
116 // Arrange
117 $activity = new ilLoggingActivity($this->node);
118 $expected = './Services/WorkflowEngine/test/malicious.php';
119 // Is either one of: .log or .txt
120
121 // Act
122 $activity->setLogFile($expected);
123 $actual = $activity->getLogFile();
124
125 // Assertion via phpdoc. (Exception)
126 }
127
128 public function testSetGetLegalMessage()
129 {
130 // Arrange
131 $activity = new ilLoggingActivity($this->node);
132 $expected = 'Hallo Spencer!';
133
134 // Act
135 $activity->setLogMessage($expected);
136 $actual = $activity->getLogMessage();
137
138 // Assert
139 $this->assertEquals(
140 $actual,
141 $expected,
142 'Get/Set corrupted message.'
143 );
144 }
145
149 public function testSetGetNullLogMessage()
150 {
151 $this->expectException(ilWorkflowObjectStateException::class);
152
153 // Arrange
154 $activity = new ilLoggingActivity($this->node);
155
156 // Act
157 $activity->setLogMessage(null);
158 $actual = $activity->getLogMessage();
159
160 // Assertion via phpdoc. (Exception)
161 }
162
167 {
168 $this->expectException(ilWorkflowObjectStateException::class);
169
170 // Arrange
171 $activity = new ilLoggingActivity($this->node);
172
173 // Act
174 $activity->setLogMessage('');
175 $actual = $activity->getLogMessage();
176
177 // Assertion via phpdoc. (Exception)
178 }
179
180 public function testSetGetValidLogLevel()
181 {
182 // Arrange
183 $activity = new ilLoggingActivity($this->node);
184 $expected = "MESSAGE";
185
186 // Act
187 $activity->setLogLevel($expected);
188 $actual = $activity->getLogLevel();
189
190 // Assert
191 $this->assertEquals(
192 $actual,
193 $expected,
194 'Get/Set corrupted log level.'
195 );
196 }
197
202 {
203 $this->expectException(ilWorkflowObjectStateException::class);
204
205 // Arrange
206 $activity = new ilLoggingActivity($this->node);
207 $expected = "guenther";
208
209 // Act
210 $activity->setLogLevel($expected);
211 $actual = $activity->getLogLevel();
212 }
213
214 public function testExecute()
215 {
216 // Arrange
217 $activity = new ilLoggingActivity($this->node);
218 $activity->setLogFile(vfs\vfsStream::url('example/log.txt'));
219 $activity->setLogLevel('MESSAGE');
220 $activity->setLogMessage('TEST');
221
222 // Act
223 $activity->execute();
224
225 // Assert
226 $expected = ' :: MESSAGE :: TEST';
227 $fp = fopen(vfs\vfsStream::url('example/log.txt'), 'r');
228 $line = fgets($fp);
229 $actual = substr($line, 25, strlen($line) - 27);
230
231 $this->assertEquals(
232 $actual,
233 $expected,
234 'Logging Activity did not write expected output.'
235 );
236 }
237
242 {
243 $this->expectException(ilWorkflowFilesystemException::class);
244
245 // Arrange
246 $activity = new ilLoggingActivity($this->node);
247 $activity->setLogFile(vfs\vfsStream::url('example.txt'));
248 }
249
250 public function testGetContext()
251 {
252 // Arrange
253 $activity = new ilLoggingActivity($this->node);
254
255 // Act
256 $actual = $activity->getContext();
257
258 // Assert
259 if ($actual === $this->node) {
260 $this->assertEquals($actual, $this->node);
261 } else {
262 $this->assertTrue(false, 'Context not identical.');
263 }
264 }
265}
An exception for terminatinating execution or to throw for unit testing.
@noinspection PhpIncludeInspection
@noinspection PhpIncludeInspection
ilLoggingActivityTest is part of the petri net based workflow engine.
$test_dir
vfsStream Test Directory, see setup.
@noinspection PhpIncludeInspection
global $ilSetting
Definition: privfeed.php:17