ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
ilCtrlStructureTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /* Copyright (c) 2021 Thibeau Fuhrer <thf@studer-raimann.ch> Extended GPL, see docs/LICENSE */
6 
8 
13 class ilCtrlStructureTest extends TestCase
14 {
15  public function testStructureBaseClasses(): void
16  {
17  $structure = new ilCtrlStructure([
18  'baseclass1' => [ilCtrlStructureInterface::KEY_CLASS_CID => 'cid1'],
19  'baseclass2' => [ilCtrlStructureInterface::KEY_CLASS_CID => 'cid2'],
20  'baseclass3' => [ilCtrlStructureInterface::KEY_CLASS_CID => 'cid3'],
21  ], [
22  'baseclass1',
23  'baseclass2',
24  'baseclass3',
25  ], []);
26 
27  $this->assertTrue($structure->isBaseClass('baseclass1'));
28  $this->assertTrue($structure->isBaseClass('baseclass2'));
29  $this->assertTrue($structure->isBaseClass('baseclass3'));
30  $this->assertFalse($structure->isBaseClass('baseclass4'));
31  $this->assertFalse($structure->isBaseClass(''));
32  }
33 
34  public function testStructureSecurityInfos(): void
35  {
36  $structure = new ilCtrlStructure([
37  'cmdclass1' => [
40  ],
41  'cmdclass2' => [
44  ],
45  ], [], [
46  'cmdclass1' => [
49  'postCmd1',
50  ],
51  ],
52  'cmdclass2' => [
54  'getCmd1',
55  ],
57  ],
58  ]);
59 
60  $this->assertEmpty($structure->getUnsafeCommandsByCid('cid1'));
61  $this->assertEmpty($structure->getUnsafeCommandsByName('cmdClass1'));
62 
63  $this->assertEquals(['postCmd1'], $structure->getSafeCommandsByCid('cid1'));
64  $this->assertEquals(['postCmd1'], $structure->getSafeCommandsByName('cmdClass1'));
65 
66  $this->assertEquals(['getCmd1'], $structure->getUnsafeCommandsByCid('cid2'));
67  $this->assertEquals(['getCmd1'], $structure->getUnsafeCommandsByName('cmdClass2'));
68 
69  $this->assertEmpty($structure->getSafeCommandsByCid('cid2'));
70  $this->assertEmpty($structure->getSafeCommandsByName('cmdClass2'));
71 
72  $this->assertEmpty($structure->getUnsafeCommandsByName(''));
73  $this->assertEmpty($structure->getUnsafeCommandsByCid(''));
74 
75  $this->assertEmpty($structure->getSafeCommandsByCid(''));
76  $this->assertEmpty($structure->getSafeCommandsByName(''));
77  }
78 
79  public function testStructureCtrlInfos(): void
80  {
81  $structure = new ilCtrlStructure([
82  'class1' => [
88  'class2',
89  ],
90  ],
91  'class2' => [
96  'class1',
97  ],
99  ],
100  ], [], []);
101 
102  $this->assertEquals('Class1', $structure->getObjNameByCid('cid1'));
103  $this->assertEquals('Class1', $structure->getObjNameByName('Class1'));
104  $this->assertEquals('Class2', $structure->getObjNameByCid('cid2'));
105  $this->assertEquals('Class2', $structure->getObjNameByName('Class2'));
106  $this->assertNull($structure->getObjNameByCid('cid3'));
107  $this->assertNull($structure->getObjNameByName('Class3'));
108 
109  $this->assertEquals('class1', $structure->getClassNameByCid('cid1'));
110  $this->assertEquals('cid1', $structure->getClassCidByName('Class1'));
111  $this->assertEquals('class2', $structure->getClassNameByCid('cid2'));
112  $this->assertEquals('cid2', $structure->getClassCidByName('Class2'));
113  $this->assertNull($structure->getClassNameByCid('cid3'));
114  $this->assertNull($structure->getClassCidByName('Class3'));
115 
116  $this->assertEquals('./path/1', $structure->getRelativePathByCid('cid1'));
117  $this->assertEquals('./path/1', $structure->getRelativePathByName('Class1'));
118  $this->assertEquals('./path/2', $structure->getRelativePathByCid('cid2'));
119  $this->assertEquals('./path/2', $structure->getRelativePathByName('Class2'));
120  $this->assertNull($structure->getRelativePathByCid('cid3'));
121  $this->assertNull($structure->getRelativePathByName('Class3'));
122 
123  $this->assertEquals(['class2'], $structure->getChildrenByCid('cid1'));
124  $this->assertEquals(['class2'], $structure->getChildrenByName('Class1'));
125  $this->assertNull($structure->getChildrenByCid('cid2'));
126  $this->assertNull($structure->getChildrenByName('Class2'));
127  $this->assertNull($structure->getChildrenByCid('cid3'));
128  $this->assertNull($structure->getChildrenByName('Class3'));
129 
130  $this->assertNull($structure->getParentsByCid('cid1'));
131  $this->assertNull($structure->getParentsByName('Class1'));
132  $this->assertEquals(['class1'], $structure->getParentsByCid('cid2'));
133  $this->assertEquals(['class1'], $structure->getParentsByName('Class2'));
134  $this->assertNull($structure->getParentsByCid('cid3'));
135  $this->assertNull($structure->getParentsByName('Class3'));
136  }
137 
138  public function testStructureSavedParameters(): void
139  {
140  $structure = new ilCtrlStructure([], [], []);
141 
142  $structure->setPermanentParameterByClass('Class1', 'param1');
143  $structure->setPermanentParameterByClass('Class1', 'param2');
144  $structure->setPermanentParameterByClass('Class2', 'param1');
145 
146  // parameters must be as set above
147  $this->assertEquals(['param1', 'param2'], $structure->getPermanentParametersByClass('Class1'));
148  $this->assertEquals(['param1'], $structure->getPermanentParametersByClass('Class2'));
149 
150  $structure->removeSingleParameterByClass('Class1', 'param1');
151 
152  // parameter 'param1' must no longer be contained and not
153  // affect parameters of 'Class2'.
154  $this->assertEquals(['param2'], $structure->getPermanentParametersByClass('Class1'));
155  $this->assertEquals(['param1'], $structure->getPermanentParametersByClass('Class2'));
156 
157  $structure->removePermanentParametersByClass('Class1');
158 
159  // no parameters must be contained anymore for 'Class1',
160  // but not affect parameters of 'Class2'
161  $this->assertNull($structure->getPermanentParametersByClass('Class1'));
162  $this->assertEquals(['param1'], $structure->getPermanentParametersByClass('Class2'));
163 
164  // parameter must not be set, it contains invalid characters.
165  $parameter_name = '$param';
166  $this->expectException(ilCtrlException::class);
167  $this->expectExceptionMessage("Cannot save parameter '$parameter_name', as it contains invalid characters.");
168  $structure->setPermanentParameterByClass('Class2', $parameter_name);
169  }
170 
174  public function testStructureSavedParametersWithProtectedKey($protected_parameter): void
175  {
176  $structure = new ilCtrlStructure([], [], []);
177  $this->expectException(ilCtrlException::class);
178  $structure->setPermanentParameterByClass('a_class', $protected_parameter);
179  }
180 
181  public function testStructureTemporaryParameters(): void
182  {
183  $structure = new ilCtrlStructure([], [], []);
184 
185  $structure->setTemporaryParameterByClass('Class1', 'param1', 1);
186  $structure->setTemporaryParameterByClass('Class1', 'param2', '2');
187  $structure->setTemporaryParameterByClass('Class2', 'param1', true);
188 
189  // parameters must be as set above
190  $this->assertEquals(['param1' => true], $structure->getTemporaryParametersByClass('Class2'));
191  $this->assertEquals(
192  ['param1' => 1, 'param2' => '2'],
193  $structure->getTemporaryParametersByClass('Class1')
194  );
195 
196  $structure->removeSingleParameterByClass('Class1', 'param1');
197 
198  // parameter 'param1' must no longer be contained and not
199  // affect parameters of 'Class2'.
200  $this->assertEquals(['param1' => true], $structure->getTemporaryParametersByClass('Class2'));
201  $this->assertEquals(
202  ['param2' => '2'],
203  $structure->getTemporaryParametersByClass('Class1')
204  );
205 
206  $structure->removeTemporaryParametersByClass('Class1');
207 
208  // no parameters must be contained anymore for 'Class1',
209  // but not affect parameters of 'Class2'
210  $this->assertEquals(['param1' => true], $structure->getTemporaryParametersByClass('Class2'));
211  $this->assertNull($structure->getTemporaryParametersByClass('Class1'));
212 
213  // parameter must not be set, it contains invalid characters.
214  $parameter_name = '$param';
215  $this->expectException(ilCtrlException::class);
216  $this->expectExceptionMessage("Cannot save parameter '$parameter_name', as it contains invalid characters.");
217  $structure->setTemporaryParameterByClass('Class3', $parameter_name, 0);
218  }
219 
220  public function testStructureReturnTargets(): void
221  {
222  $structure = new ilCtrlStructure([], [], []);
223 
224  $test_url = 'https://domain.com/test/url';
225  $structure->setReturnTargetByClass('Class1', $test_url);
226  $this->assertEquals($test_url, $structure->getReturnTargetByClass('Class1'));
227  $this->assertNull($structure->getReturnTargetByClass('Class2'));
228  }
229 
230  public function getProtectedParameters(): array
231  {
232  return [
233  ['baseClass'],
234  ['cmdClass'],
235  ['cmdNode'],
236  ['rtoken'],
237  ];
238  }
239 }
Class ilCtrlStructureTest.
Class ilCtrlStructure holds the currently read control structure.
const KEY_CLASS_CID
array key constants that are used for certain information.
testStructureSavedParametersWithProtectedKey($protected_parameter)
getProtectedParameters