ILIAS  trunk Revision v11.0_alpha-1702-gfd3ecb7f852
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
ilCtrlStructureTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
28 {
29  public function testStructureBaseClasses(): void
30  {
32  'baseclass1' => [ilCtrlStructureInterface::KEY_CLASS_CID => 'cid1'],
33  'baseclass2' => [ilCtrlStructureInterface::KEY_CLASS_CID => 'cid2'],
34  'baseclass3' => [ilCtrlStructureInterface::KEY_CLASS_CID => 'cid3'],
35  ], [
36  'baseclass1',
37  'baseclass2',
38  'baseclass3',
39  ], []);
40 
41  $this->assertTrue($structure->isBaseClass('baseclass1'));
42  $this->assertTrue($structure->isBaseClass('baseclass2'));
43  $this->assertTrue($structure->isBaseClass('baseclass3'));
44  $this->assertFalse($structure->isBaseClass('baseclass4'));
45  $this->assertFalse($structure->isBaseClass(''));
46  }
47 
48  public function testStructureSecurityInfos(): void
49  {
51  'cmdclass1' => [
54  ],
55  'cmdclass2' => [
58  ],
59  ], [], [
60  'cmdclass1' => [
63  'postCmd1',
64  ],
65  ],
66  'cmdclass2' => [
68  'getCmd1',
69  ],
71  ],
72  ]);
73 
74  $this->assertEmpty($structure->getUnsafeCommandsByCid('cid1'));
75  $this->assertEmpty($structure->getUnsafeCommandsByName('cmdClass1'));
76 
77  $this->assertEquals(['postCmd1'], $structure->getSafeCommandsByCid('cid1'));
78  $this->assertEquals(['postCmd1'], $structure->getSafeCommandsByName('cmdClass1'));
79 
80  $this->assertEquals(['getCmd1'], $structure->getUnsafeCommandsByCid('cid2'));
81  $this->assertEquals(['getCmd1'], $structure->getUnsafeCommandsByName('cmdClass2'));
82 
83  $this->assertEmpty($structure->getSafeCommandsByCid('cid2'));
84  $this->assertEmpty($structure->getSafeCommandsByName('cmdClass2'));
85 
86  $this->assertEmpty($structure->getUnsafeCommandsByName(''));
87  $this->assertEmpty($structure->getUnsafeCommandsByCid(''));
88 
89  $this->assertEmpty($structure->getSafeCommandsByCid(''));
90  $this->assertEmpty($structure->getSafeCommandsByName(''));
91  }
92 
93  public function testStructureCtrlInfos(): void
94  {
96  'class1' => [
102  'class2',
103  ],
104  ],
105  'class2' => [
110  'class1',
111  ],
113  ],
114  ], [], []);
115 
116  $this->assertEquals('Class1', $structure->getObjNameByCid('cid1'));
117  $this->assertEquals('Class1', $structure->getObjNameByName('Class1'));
118  $this->assertEquals('Class2', $structure->getObjNameByCid('cid2'));
119  $this->assertEquals('Class2', $structure->getObjNameByName('Class2'));
120  $this->assertNull($structure->getObjNameByCid('cid3'));
121  $this->assertNull($structure->getObjNameByName('Class3'));
122 
123  $this->assertEquals('class1', $structure->getClassNameByCid('cid1'));
124  $this->assertEquals('cid1', $structure->getClassCidByName('Class1'));
125  $this->assertEquals('class2', $structure->getClassNameByCid('cid2'));
126  $this->assertEquals('cid2', $structure->getClassCidByName('Class2'));
127  $this->assertNull($structure->getClassNameByCid('cid3'));
128  $this->assertNull($structure->getClassCidByName('Class3'));
129 
130  $this->assertEquals('./path/1', $structure->getRelativePathByCid('cid1'));
131  $this->assertEquals('./path/1', $structure->getRelativePathByName('Class1'));
132  $this->assertEquals('./path/2', $structure->getRelativePathByCid('cid2'));
133  $this->assertEquals('./path/2', $structure->getRelativePathByName('Class2'));
134  $this->assertNull($structure->getRelativePathByCid('cid3'));
135  $this->assertNull($structure->getRelativePathByName('Class3'));
136 
137  $this->assertEquals(['class2'], $structure->getChildrenByCid('cid1'));
138  $this->assertEquals(['class2'], $structure->getChildrenByName('Class1'));
139  $this->assertNull($structure->getChildrenByCid('cid2'));
140  $this->assertNull($structure->getChildrenByName('Class2'));
141  $this->assertNull($structure->getChildrenByCid('cid3'));
142  $this->assertNull($structure->getChildrenByName('Class3'));
143 
144  $this->assertNull($structure->getParentsByCid('cid1'));
145  $this->assertNull($structure->getParentsByName('Class1'));
146  $this->assertEquals(['class1'], $structure->getParentsByCid('cid2'));
147  $this->assertEquals(['class1'], $structure->getParentsByName('Class2'));
148  $this->assertNull($structure->getParentsByCid('cid3'));
149  $this->assertNull($structure->getParentsByName('Class3'));
150  }
151 
152  public function testStructureSavedParameters(): void
153  {
154  $structure = new ilCtrlStructure([], [], []);
155 
156  $structure->setPermanentParameterByClass('Class1', 'param1');
157  $structure->setPermanentParameterByClass('Class1', 'param2');
158  $structure->setPermanentParameterByClass('Class2', 'param1');
159 
160  // parameters must be as set above
161  $this->assertEquals(['param1', 'param2'], $structure->getPermanentParametersByClass('Class1'));
162  $this->assertEquals(['param1'], $structure->getPermanentParametersByClass('Class2'));
163 
164  $structure->removeSingleParameterByClass('Class1', 'param1');
165 
166  // parameter 'param1' must no longer be contained and not
167  // affect parameters of 'Class2'.
168  $this->assertEquals(['param2'], $structure->getPermanentParametersByClass('Class1'));
169  $this->assertEquals(['param1'], $structure->getPermanentParametersByClass('Class2'));
170 
171  $structure->removePermanentParametersByClass('Class1');
172 
173  // no parameters must be contained anymore for 'Class1',
174  // but not affect parameters of 'Class2'
175  $this->assertNull($structure->getPermanentParametersByClass('Class1'));
176  $this->assertEquals(['param1'], $structure->getPermanentParametersByClass('Class2'));
177 
178  // parameter must not be set, it contains invalid characters.
179  $parameter_name = '$param';
180  $this->expectException(ilCtrlException::class);
181  $this->expectExceptionMessage("Cannot save parameter '$parameter_name', as it contains invalid characters.");
182  $structure->setPermanentParameterByClass('Class2', $parameter_name);
183  }
184 
188  public function testStructureSavedParametersWithProtectedKey($protected_parameter): void
189  {
190  $structure = new ilCtrlStructure([], [], []);
191  $this->expectException(ilCtrlException::class);
192  $structure->setPermanentParameterByClass('a_class', $protected_parameter);
193  }
194 
195  public function testStructureTemporaryParameters(): void
196  {
197  $structure = new ilCtrlStructure([], [], []);
198 
199  $structure->setTemporaryParameterByClass('Class1', 'param1', 1);
200  $structure->setTemporaryParameterByClass('Class1', 'param2', '2');
201  $structure->setTemporaryParameterByClass('Class2', 'param1', true);
202 
203  // parameters must be as set above
204  $this->assertEquals(['param1' => true], $structure->getTemporaryParametersByClass('Class2'));
205  $this->assertEquals(
206  ['param1' => 1, 'param2' => '2'],
207  $structure->getTemporaryParametersByClass('Class1')
208  );
209 
210  $structure->removeSingleParameterByClass('Class1', 'param1');
211 
212  // parameter 'param1' must no longer be contained and not
213  // affect parameters of 'Class2'.
214  $this->assertEquals(['param1' => true], $structure->getTemporaryParametersByClass('Class2'));
215  $this->assertEquals(
216  ['param2' => '2'],
217  $structure->getTemporaryParametersByClass('Class1')
218  );
219 
220  $structure->removeTemporaryParametersByClass('Class1');
221 
222  // no parameters must be contained anymore for 'Class1',
223  // but not affect parameters of 'Class2'
224  $this->assertEquals(['param1' => true], $structure->getTemporaryParametersByClass('Class2'));
225  $this->assertNull($structure->getTemporaryParametersByClass('Class1'));
226 
227  // parameter must not be set, it contains invalid characters.
228  $parameter_name = '$param';
229  $this->expectException(ilCtrlException::class);
230  $this->expectExceptionMessage("Cannot save parameter '$parameter_name', as it contains invalid characters.");
231  $structure->setTemporaryParameterByClass('Class3', $parameter_name, 0);
232  }
233 
234  public function testStructureReturnTargets(): void
235  {
236  $structure = new ilCtrlStructure([], [], []);
237 
238  $test_url = 'https://domain.com/tests/url';
239  $structure->setReturnTargetByClass('Class1', $test_url);
240  $this->assertEquals($test_url, $structure->getReturnTargetByClass('Class1'));
241  $this->assertNull($structure->getReturnTargetByClass('Class2'));
242  }
243 
244  public static function getProtectedParameters(): array
245  {
246  return [
247  ['baseClass'],
248  ['cmdClass'],
249  ['cmdNode'],
250  ['rtoken'],
251  ];
252  }
253 }
Class ilCtrlStructureTest.
Class ilCtrlStructure holds the currently read control structure.
const KEY_CLASS_CID
array key constants that are used for certain information.
$structure
TOTAL STRUCTURE.
testStructureSavedParametersWithProtectedKey($protected_parameter)
getProtectedParameters