ILIAS  release_7 Revision v7.30-3-g800a261c036
ilCtrlStructureReaderTest.php
Go to the documentation of this file.
1<?php
18use PHPUnit\Framework\TestCase;
19
20class ilCtrlStructureReaderTest extends TestCase
21{
22 protected function setUp() : void
23 {
24 $this->db = $this->createMock(\ilDBInterface::class);
25 $this->reader = (new class() extends ilCtrlStructureReader {
26 public function _shouldDescendToDirectory(string $dir)
27 {
28 return $this->shouldDescendToDirectory($dir);
29 }
30 public function _getFilesIn(string $dir)
31 {
32 return $this->getFilesIn($dir);
33 }
34 public function _isInterestingFile(string $file)
35 {
36 return $this->isInterestingFile($file);
37 }
38 public function _getGUIClassNameFromClassPath(string $file)
39 {
40 return $this->getGUIClassNameFromClassPath($file);
41 }
42 public function _getIlCtrlCalls(string $content)
43 {
44 return $this->getIlCtrlCalls($content);
45 }
46 public function _getIlCtrlIsCalledBy(string $content)
47 {
48 return $this->getIlCtrlIsCalledBy($content);
49 }
50 public function _containsClassDefinitionFor(string $class, string $content)
51 {
52 return $this->containsClassDefinitionFor($class, $content);
53 }
54 protected function getILIASAbsolutePath() : string
55 {
56 return "";
57 }
58 public function _readDirTo(string $a_cdir, \ilCtrlStructure $cs = null) : \ilCtrlStructure
59 {
60 return $this->readDirTo($a_cdir, $cs ?? new \ilCtrlStructure());
61 }
62 protected function normalizePath(string $path) : string
63 {
64 return $path;
65 }
66 })
67 ->withDB($this->db);
68 }
69
70 public function testSmoke()
71 {
72 $this->assertInstanceOf(ilCtrlStructureReader::class, $this->reader);
73 }
74
75 public function testReadSmoke()
76 {
77 $dir = __DIR__ . "/test_dir";
78 $result = $this->reader->_readDirTo($dir);
79 $this->assertInstanceOf(\ilCtrlStructure::class, $result);
80 }
81
82 public function testReadClassScriptIsAsExpected()
83 {
84 $dir = __DIR__ . "/test_dir";
85 $result = $this->reader->_readDirTo($dir);
86
87 $expected_class_script = [
88 "ilmytestinggui" => "$dir/class.ilMyTestingGUI.php"
89 ];
90 $this->assertEquals(
91 $expected_class_script,
92 iterator_to_array($result->getClassScripts())
93 );
94 }
95
96 public function testReadClassChildsIsAsExpected()
97 {
98 $dir = __DIR__ . "/test_dir/";
99 $result = $this->reader->_readDirTo($dir);
100
101 $expected_class_childs = [
102 "ilmytestinggui" => [
103 "ilmyothertestinggui"
104 ],
105 "ilmythirdtestinggui" => [
106 "ilmytestinggui"
107 ]
108 ];
109 $this->assertEquals(
110 $expected_class_childs,
111 iterator_to_array($result->getClassChildren())
112 );
113 }
114
115 public function testReadRemovesDuplicateCallsInDatabase()
116 {
117 $this->expectException(\Exception::class);
118
119 $dir = __DIR__ . "/test_dir/";
120
121 $this->reader->comp_prefix = "";
122 $ctrl_structure = new \ilCtrlStructure([
123 "ilmytestinggui" => "/some/other/dir/class.ilMyTestingGUI.php"
124 ]);
125 $this->db
126 ->method("quote")
127 ->will($this->returnCallback(function ($v, $_) {
128 return "\"$v\"";
129 }));
130 $this->db
131 ->method("equals")
132 ->will($this->returnCallback(function ($f, $v, $_, $__) {
133 return "$f = \"$v\"";
134 }));
135 $this->db->expects($this->exactly(4))
136 ->method("manipulate")
137 ->withConsecutive(
138 ["DELETE FROM ctrl_classfile WHERE comp_prefix = \"\""],
139 ["DELETE FROM ctrl_classfile WHERE comp_prefix = \"\""],
140 ["DELETE FROM ctrl_calls WHERE comp_prefix = \"\""],
141 ["DELETE FROM ctrl_calls WHERE comp_prefix IS NULL"]
142 );
143
144 $result = $this->reader->_readDirTo($dir, $ctrl_structure);
145 }
146
147 public function testReadRemovesDuplicateFilesInDatabaseIfCompPrefixIsSet()
148 {
149 $this->expectException(\Exception::class);
150
151 $dir = __DIR__ . "/test_dir/";
152 $my_comp_prefix = "mcp";
153
154 $this->reader->comp_prefix = $my_comp_prefix;
155 $ctrl_structure = new \ilCtrlStructure([
156 "ilmytestinggui" => "/some/other/dir/class.ilMyTestingGUI.php"
157 ]);
158 $this->db
159 ->method("quote")
160 ->will($this->returnCallback(function ($v, $_) {
161 return "\"$v\"";
162 }));
163 $this->db
164 ->method("equals")
165 ->will($this->returnCallback(function ($f, $v, $_, $__) {
166 return "$f = \"$v\"";
167 }));
168 $this->db->expects($this->exactly(2))
169 ->method("manipulate")
170 ->withConsecutive(
171 ["DELETE FROM ctrl_classfile WHERE comp_prefix = \"$my_comp_prefix\""],
172 ["DELETE FROM ctrl_calls WHERE comp_prefix = \"$my_comp_prefix\""]
173 );
174
175 $result = $this->reader->_readDirTo($dir, $ctrl_structure);
176 }
177
178 public function testShouldDescendToDirectory()
179 {
180 $this->assertTrue($this->reader->_shouldDescendToDirectory("/foo"));
181 $this->assertTrue($this->reader->_shouldDescendToDirectory("/bar"));
182 $this->assertFalse($this->reader->_shouldDescendToDirectory("/data"));
183 $this->assertFalse($this->reader->_shouldDescendToDirectory("/Customizing"));
184 }
185
186 public function testFilesInDir()
187 {
188 $dir = __DIR__ . "/test_dir";
189 $expected = [
190 ["class.ilMyTestingGUI.php", "$dir/class.ilMyTestingGUI.php"],
191 ["test_file", "$dir/sub_test_dir/test_file"]
192 ];
193 $result = iterator_to_array($this->reader->_getFilesIn($dir));
194 sort($expected);
195 sort($result);
196 $this->assertEquals($expected, $result);
197 }
198
199 public function testIsInterestingFile()
200 {
201 $this->assertFalse($this->reader->_isInterestingFile("ilSCORM13Player.php"));
202 $this->assertTrue($this->reader->_isInterestingFile("class.ilSCORM13PlayerGUI.php"));
203 $this->assertTrue($this->reader->_isInterestingFile("class.ilMyTestingGUI.php"));
204 $this->assertFalse($this->reader->_isInterestingFile("foo.php"));
205 $this->assertFalse($this->reader->_isInterestingFile("picture.png"));
206 $this->assertFalse($this->reader->_isInterestingFile("icon.svg"));
207 $this->assertFalse($this->reader->_isInterestingFile("data.json"));
208 }
209
210 public function testGetGUIClassNameFromClassPath()
211 {
212 $this->assertNull($this->reader->_getGUIClassNameFromClassPath("/my/dir/ilSCORM13Player.php"));
213 $this->assertNull($this->reader->_getGUIClassNameFromClassPath("\\my\\dir\\ilSCORM13Player.php"));
214 $this->assertNull($this->reader->_getGUIClassNameFromClassPath("c:\\my\\dir\\ilSCORM13Player.php"));
215 $this->assertEquals("ilscorm13playergui", $this->reader->_getGUIClassNameFromClassPath("/my/dir/class.ilSCORM13PlayerGUI.php"));
216 $this->assertEquals("ilscorm13playergui", $this->reader->_getGUIClassNameFromClassPath("\\my\\dir\\class.ilSCORM13PlayerGUI.php"));
217 $this->assertEquals("ilscorm13playergui", $this->reader->_getGUIClassNameFromClassPath("c:\\my\\dir\\class.ilSCORM13PlayerGUI.php"));
218 $this->assertEquals("ilmytestinggui", $this->reader->_getGUIClassNameFromClassPath("/my/dir/class.ilMyTestingGUI.php"));
219 $this->assertEquals("ilmytestinggui", $this->reader->_getGUIClassNameFromClassPath("\\my\\dir\\class.ilMyTestingGUI.php"));
220 $this->assertEquals("ilmytestinggui", $this->reader->_getGUIClassNameFromClassPath("c:\\my\\dir\\class.ilMyTestingGUI.php"));
221 $this->assertNull($this->reader->_getGUIClassNameFromClassPath("/my/dir/foo.php"));
222 $this->assertNull($this->reader->_getGUIClassNameFromClassPath("\\my\\dir\\foo.php"));
223 $this->assertNull($this->reader->_getGUIClassNameFromClassPath("c:\\my\\dir\\foo.php"));
224 $this->assertNull($this->reader->_getGUIClassNameFromClassPath("/my/dir/picture.png"));
225 $this->assertNull($this->reader->_getGUIClassNameFromClassPath("\\my\\dir\\picture.png"));
226 $this->assertNull($this->reader->_getGUIClassNameFromClassPath("c:\\my\\dir\\picture.png"));
227 $this->assertNull($this->reader->_getGUIClassNameFromClassPath("/my/dir/icon.svg"));
228 $this->assertNull($this->reader->_getGUIClassNameFromClassPath("\\my\\dir\\icon.svg"));
229 $this->assertNull($this->reader->_getGUIClassNameFromClassPath("c:\\my\\dir\\icon.svg"));
230 $this->assertNull($this->reader->_getGUIClassNameFromClassPath("/my/dir/data.json"));
231 $this->assertNull($this->reader->_getGUIClassNameFromClassPath("\\my\\dir\\data.json"));
232 $this->assertNull($this->reader->_getGUIClassNameFromClassPath("c:\\my\\dir\\data.json"));
233 }
234
235 public function testGetIlCtrlCallsNoContent()
236 {
237 $gen = $this->reader->_getIlCtrlCalls(
238 <<<"PHP"
239class SomeRandomClass {
240}
241PHP
242 );
243 $this->assertNull($gen);
244 }
245
246 public function testGetIlCtrlCallsWithContent()
247 {
248 list($parent, $children) = $this->reader->_getIlCtrlCalls(
249 <<<"PHP"
250<?php
251/* Copyright (c) 2020 ILIAS open source, Extended GPL, see docs/LICENSE */
252
253
254require_once "./Services/Container/classes/class.ilContainerGUI.php";
255
267class ilObjCourseGUI extends ilContainerGUI
268{
269}
270PHP
271 );
272 $expected = [
273 "ilcourseregistrationgui",
274 "ilcourseobjectivesgui",
275 "ilobjcoursegroupinggui",
276 "ilinfoscreengui",
277 "illearningprogressgui",
278 "ilpermissiongui",
279 "ilrepositorysearchgui"
280 ];
281
282 sort($expected);
283 sort($children);
284
285 $this->assertEquals("ilobjcoursegui", $parent);
286 $this->assertEquals($expected, $children);
287 }
288
289 public function testGetIlCtrlIsCalledByNoContent()
290 {
291 $gen = $this->reader->_getIlCtrlIsCalledBy(
292 <<<"PHP"
293class SomeRandomClass {
294}
295PHP
296 );
297 $this->assertNull($gen);
298 }
299
300 public function testGetIlCtrlIsCalledByWithContent()
301 {
302 list($parent, $children) = $this->reader->_getIlCtrlIsCalledBy(
303 <<<"PHP"
304<?php
305/* Copyright (c) 2020 ILIAS open source, Extended GPL, see docs/LICENSE */
306
307
308require_once "./Services/Container/classes/class.ilContainerGUI.php";
309
321class ilObjCourseGUI extends ilContainerGUI
322{
323}
324PHP
325 );
326 $expected = [
327 "ilcourseregistrationgui",
328 "ilcourseobjectivesgui",
329 "ilobjcoursegroupinggui",
330 "ilinfoscreengui",
331 "illearningprogressgui",
332 "ilpermissiongui",
333 "ilrepositorysearchgui"
334 ];
335
336 sort($expected);
337 sort($children);
338
339 $this->assertEquals("ilobjcoursegui", $parent);
340 $this->assertEquals($expected, $children);
341 }
342
343 public function testContainsClassDefinitionFor()
344 {
345 $res = $this->reader->_containsClassDefinitionFor(
346 "SomeRandomClass",
347 <<<"PHP"
348class SomeRandomClass {
349}
350PHP
351 );
352 $this->assertTrue($res);
353 }
354
355 public function testDoesNotContainClassDefinitionFor()
356 {
357 $res = $this->reader->_containsClassDefinitionFor(
358 "SomeRandomClass",
359 <<<"PHP"
360class fooSomeRandomClass {
361}
362PHP
363 );
364 $this->assertFalse($res);
365 }
366
367
368 public function testGetIlCtrlCallsWithNamespaces() : void
369 {
370 list($parent, $children) = $this->reader->_getIlCtrlCalls(
371 <<<"PHP"
372<?php
373namespace ILIAS\UICore;
374
379class TestGUI
380}
381PHP
382 );
383 $expected = array_map("strtolower", [
384 "ilFormPropertyDispatchGUI",
385 "ILIAS\UICore\Test2GUI"
386 ]);
387
388 sort($expected);
389 sort($children);
390
391 $this->assertEquals(strtolower("ILIAS\UICore\TestGUI"), $parent);
392 $this->assertEquals($expected, $children);
393 }
394
395
396 public function testGetIlCtrlIsCalledByWithNamespaces() : void
397 {
398 list($parent, $children) = $this->reader->_getIlCtrlIsCalledBy(
399 <<<"PHP"
400<?php
401namespace ILIAS\UICore;
402
407class TestGUI
408}
409PHP
410 );
411 $expected = array_map("strtolower", [
412 "ilUIPluginRouterGUI",
413 "ILIAS\UICore\Test3GUI"
414 ]);
415
416 sort($expected);
417 sort($children);
418
419 $this->assertEquals(strtolower("ILIAS\UICore\TestGUI"), $parent);
420 $this->assertEquals($expected, $children);
421 }
422}
$result
An exception for terminatinating execution or to throw for unit testing.
Class ilContainerGUI.
Class ilCtrlStructureReader.
The CtrlStructure knows how GUIs call each other and where the code of the guis is located.
Class ilObjCourseGUI.
Class ChatMainBarProvider \MainMenu\Provider.
PHP
Definition: index.php:3
foreach($_POST as $key=> $value) $res