ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
ilSystemStyleStyleLessFileTest.php
Go to the documentation of this file.
1<?php
2include_once("Services/Style/System/classes/Utilities/class.ilSkinStyleXML.php");
3include_once("Services/Style/System/classes/Utilities/class.ilSkinXML.php");
4include_once("Services/Style/System/classes/Utilities/class.ilSystemStyleSkinContainer.php");
5include_once("./Services/Style/System/classes/Less/class.ilSystemStyleLessFile.php");
6include_once("Services/Style/System/test/fixtures/mocks/ilSystemStyleConfigMock.php");
7include_once("Services/Style/System/test/fixtures/mocks/ilSystemStyleDICMock.php");
8
9use PHPUnit\Framework\TestCase;
10
16class ilSystemStyleStyleLessFileTest extends TestCase
17{
18
19
24
28 protected $container;
29
33 protected $style;
34
35 protected $save_dic = null;
36
37 protected function setUp() : void
38 {
39 global $DIC;
40
41 $this->save_dic = $DIC;
43
44 $this->system_style_config = new ilSystemStyleConfigMock();
45
46 mkdir($this->system_style_config->test_skin_temp_path);
47 ilSystemStyleSkinContainer::xCopy($this->system_style_config->test_skin_original_path, $this->system_style_config->test_skin_temp_path);
48
49 $this->container = ilSystemStyleSkinContainer::generateFromId("skin1", null, $this->system_style_config);
50 $this->style = $this->container->getSkin()->getStyle("style1");
51 }
52
53 protected function tearDown() : void
54 {
55 global $DIC;
57
58 ilSystemStyleSkinContainer::recursiveRemoveDir($this->system_style_config->test_skin_temp_path);
59 }
60
61 public function testConstructAndRead()
62 {
63 $file = new ilSystemStyleLessFile($this->container->getLessVariablesFilePath($this->style->getId()));
64 $this->assertEquals(14, count($file->getItems()));
65 }
66
67 public function testReadCorrectTypes()
68 {
69 $file = new ilSystemStyleLessFile($this->container->getLessVariablesFilePath($this->style->getId()));
70
71 $this->assertEquals(2, count($file->getCategories()));
72 $this->assertEquals(6, count($file->getVariablesIds()));
73 $this->assertEquals(6, count($file->getCommentsIds()));
74 }
75
76
77 public function testGetVariableByName()
78 {
79 $file = new ilSystemStyleLessFile($this->container->getLessVariablesFilePath($this->style->getId()));
80
81 $expected_variable11 = new ilSystemStyleLessVariable("variable11", "value11", "comment variable 11", "Category 1", []);
82 $expected_variable12 = new ilSystemStyleLessVariable("variable12", "value12", "comment variable 12", "Category 1", []);
83 $expected_variable13 = new ilSystemStyleLessVariable("variable13", "@variable11", "comment variable 13", "Category 1", ["variable11"]);
84
85 $expected_variable21 = new ilSystemStyleLessVariable("variable21", "@variable11", "comment variable 21", "Category 2", ["variable11"]);
86 $expected_variable22 = new ilSystemStyleLessVariable("variable22", "value21", "comment variable 22", "Category 2", []);
87 $expected_variable23 = new ilSystemStyleLessVariable("variable23", "@variable21", "comment variable 23", "Category 2", ["variable21"]);
88
89 $this->assertEquals($expected_variable11, $file->getVariableByName("variable11"));
90 $this->assertEquals($expected_variable12, $file->getVariableByName("variable12"));
91 $this->assertEquals($expected_variable13, $file->getVariableByName("variable13"));
92
93 $this->assertEquals($expected_variable21, $file->getVariableByName("variable21"));
94 $this->assertEquals($expected_variable22, $file->getVariableByName("variable22"));
95 $this->assertEquals($expected_variable23, $file->getVariableByName("variable23"));
96 }
97
98 public function testGetCategory()
99 {
100 $file = new ilSystemStyleLessFile($this->container->getLessVariablesFilePath($this->style->getId()));
101
102 $expected_category1 = new ilSystemStyleLessCategory("Category 1", "Comment Category 1");
103 $expected_category2 = new ilSystemStyleLessCategory("Category 2", "Comment Category 2");
104 $expected_categories = [$expected_category1,$expected_category2];
105
106 $this->assertEquals($expected_categories, $file->getCategories());
107 }
108
109 public function testGetItems()
110 {
111 $file = new ilSystemStyleLessFile($this->container->getLessVariablesFilePath($this->style->getId()));
112
113 $expected_category1 = new ilSystemStyleLessCategory("Category 1", "Comment Category 1");
114 $expected_comment2 = new ilSystemStyleLessComment("// Random Section 1");
115 $expected_comment3 = new ilSystemStyleLessComment("");
116 $expected_variable11 = new ilSystemStyleLessVariable("variable11", "value11", "comment variable 11", "Category 1", []);
117 $expected_variable12 = new ilSystemStyleLessVariable("variable12", "value12", "comment variable 12", "Category 1", []);
118 $expected_variable13 = new ilSystemStyleLessVariable("variable13", "@variable11", "comment variable 13", "Category 1", ["variable11"]);
119 $expected_comment4 = new ilSystemStyleLessComment("");
120 $expected_category2 = new ilSystemStyleLessCategory("Category 2", "Comment Category 2");
121 $expected_comment6 = new ilSystemStyleLessComment("/**");
122 $expected_comment7 = new ilSystemStyleLessComment(" Random Section 2 **/");
123 $expected_comment8 = new ilSystemStyleLessComment("");
124 $expected_variable21 = new ilSystemStyleLessVariable("variable21", "@variable11", "comment variable 21", "Category 2", ["variable11"]);
125 $expected_variable22 = new ilSystemStyleLessVariable("variable22", "value21", "comment variable 22", "Category 2", []);
126 $expected_variable23 = new ilSystemStyleLessVariable("variable23", "@variable21", "comment variable 23", "Category 2", ["variable21"]);
127
128 $expected_items = [$expected_category1,
129 $expected_comment2,$expected_comment3,
130 $expected_variable11,$expected_variable12,$expected_variable13,
131 $expected_comment4,
132 $expected_category2,
133 $expected_comment6,$expected_comment7,$expected_comment8,
134 $expected_variable21,$expected_variable22,$expected_variable23];
135
136 $this->assertEquals($expected_items, $file->getItems());
137 }
138
139 public function testGetContent()
140 {
141 $file = new ilSystemStyleLessFile($this->container->getLessVariablesFilePath($this->style->getId()));
142 $expected_content = file_get_contents($this->container->getLessVariablesFilePath($this->style->getId()));
143 $this->assertEquals($expected_content, $file->getContent());
144 }
145
146 public function testReadWriteDouble()
147 {
148 $expected_content = file_get_contents($this->container->getLessVariablesFilePath($this->style->getId()));
149
150 $file = new ilSystemStyleLessFile($this->container->getLessVariablesFilePath($this->style->getId()));
151 $file->write();
152 $file = new ilSystemStyleLessFile($this->container->getLessVariablesFilePath($this->style->getId()));
153 $file->write();
154 $file = new ilSystemStyleLessFile($this->container->getLessVariablesFilePath($this->style->getId()));
155
156 $this->assertEquals($expected_content, $file->getContent());
157 }
158
160 {
161 $expected_content = file_get_contents($this->container->getSkinDirectory() . "full.less");
162
163 $file = new ilSystemStyleLessFile($this->container->getSkinDirectory() . "full.less");
164 $file->write();
165 $file = new ilSystemStyleLessFile($this->container->getSkinDirectory() . "full.less");
166 $file->write();
167 $file = new ilSystemStyleLessFile($this->container->getSkinDirectory() . "full.less");
168
169 $this->assertEquals($expected_content, $file->getContent());
170 }
171
172 public function testChangeVariable()
173 {
174 $file = new ilSystemStyleLessFile($this->container->getLessVariablesFilePath($this->style->getId()));
175 $variable = $file->getVariableByName("variable11");
176 $variable->setValue("newvalue11");
177
178 $expected_category1 = new ilSystemStyleLessCategory("Category 1", "Comment Category 1");
179 $expected_comment2 = new ilSystemStyleLessComment("// Random Section 1");
180 $expected_comment3 = new ilSystemStyleLessComment("");
181 $expected_variable11 = new ilSystemStyleLessVariable("variable11", "newvalue11", "comment variable 11", "Category 1", []);
182 $expected_variable12 = new ilSystemStyleLessVariable("variable12", "value12", "comment variable 12", "Category 1", []);
183 $expected_variable13 = new ilSystemStyleLessVariable("variable13", "@variable11", "comment variable 13", "Category 1", ["variable11"]);
184 $expected_comment4 = new ilSystemStyleLessComment("");
185 $expected_category2 = new ilSystemStyleLessCategory("Category 2", "Comment Category 2");
186 $expected_comment6 = new ilSystemStyleLessComment("/**");
187 $expected_comment7 = new ilSystemStyleLessComment(" Random Section 2 **/");
188 $expected_comment8 = new ilSystemStyleLessComment("");
189 $expected_variable21 = new ilSystemStyleLessVariable("variable21", "@variable11", "comment variable 21", "Category 2", ["variable11"]);
190 $expected_variable22 = new ilSystemStyleLessVariable("variable22", "value21", "comment variable 22", "Category 2", []);
191 $expected_variable23 = new ilSystemStyleLessVariable("variable23", "@variable21", "comment variable 23", "Category 2", ["variable21"]);
192
193 $expected_items = [$expected_category1,
194 $expected_comment2,$expected_comment3,
195 $expected_variable11,$expected_variable12,$expected_variable13,
196 $expected_comment4,
197 $expected_category2,
198 $expected_comment6,$expected_comment7,$expected_comment8,
199 $expected_variable21,$expected_variable22,$expected_variable23];
200
201 $this->assertEquals($expected_items, $file->getItems());
202 }
203
204 public function testAddAndWriteItems()
205 {
206 $empty_less = new ilSystemStyleLessFile($this->container->getSkinDirectory() . "empty.less");
207
208 $expected_category1 = new ilSystemStyleLessCategory("Category 1", "Comment Category 1");
209 $expected_comment2 = new ilSystemStyleLessComment("// Random Section 1");
210 $expected_comment3 = new ilSystemStyleLessComment("");
211 $expected_variable11 = new ilSystemStyleLessVariable("variable11", "value11", "comment variable 11", "Category 1", []);
212 $expected_variable12 = new ilSystemStyleLessVariable("variable12", "value12", "comment variable 12", "Category 1", []);
213 $expected_variable13 = new ilSystemStyleLessVariable("variable13", "@variable11", "comment variable 13", "Category 1", ["variable11"]);
214 $expected_comment4 = new ilSystemStyleLessComment("");
215 $expected_category2 = new ilSystemStyleLessCategory("Category 2", "Comment Category 2");
216 $expected_comment6 = new ilSystemStyleLessComment("/**");
217 $expected_comment7 = new ilSystemStyleLessComment(" Random Section 2 **/");
218 $expected_comment8 = new ilSystemStyleLessComment("");
219 $expected_variable21 = new ilSystemStyleLessVariable("variable21", "@variable11", "comment variable 21", "Category 2", ["variable11"]);
220 $expected_variable22 = new ilSystemStyleLessVariable("variable22", "value21", "comment variable 22", "Category 2", []);
221 $expected_variable23 = new ilSystemStyleLessVariable("variable23", "@variable21", "comment variable 23", "Category 2", ["variable21"]);
222
223 $expected_items = [$expected_category1,
224 $expected_comment2,$expected_comment3,
225 $expected_variable11,$expected_variable12,$expected_variable13,
226 $expected_comment4,
227 $expected_category2,
228 $expected_comment6,$expected_comment7,$expected_comment8,
229 $expected_variable21,$expected_variable22,$expected_variable23];
230
231 foreach ($expected_items as $item) {
232 $empty_less->addItem($item);
233 }
234 $empty_less->write();
235
236 $new_less = new ilSystemStyleLessFile($this->container->getSkinDirectory() . "empty.less");
237 $this->assertEquals($expected_items, $new_less->getItems());
238 }
239
241 {
242 $file = new ilSystemStyleLessFile($this->container->getLessVariablesFilePath($this->style->getId()));
243
244 $this->assertEquals(["variable13","variable21"], $file->getReferencesToVariable("variable11"));
245 $this->assertEquals([], $file->getReferencesToVariable("variable12"));
246 $this->assertEquals([], $file->getReferencesToVariable("variable13"));
247
248 $this->assertEquals(["variable23"], $file->getReferencesToVariable("variable21"));
249 $this->assertEquals([], $file->getReferencesToVariable("variable22"));
250 $this->assertEquals([], $file->getReferencesToVariable("variable23"));
251 }
252
254 {
255 $file = new ilSystemStyleLessFile($this->container->getLessVariablesFilePath($this->style->getId()));
256
257 $this->assertEquals("variable13; variable21; ", $file->getReferencesToVariableAsString("variable11"));
258 $this->assertEquals("", $file->getReferencesToVariableAsString("variable12"));
259 $this->assertEquals("", $file->getReferencesToVariableAsString("variable13"));
260
261 $this->assertEquals("variable23; ", $file->getReferencesToVariableAsString("variable21"));
262 $this->assertEquals("", $file->getReferencesToVariableAsString("variable22"));
263 $this->assertEquals("", $file->getReferencesToVariableAsString("variable23"));
264 }
265
267 {
268 $file = new ilSystemStyleLessFile($this->container->getSkinDirectory() . "edge-cases.less");
269
270 $this->assertEquals(3, count($file->getCategories()));
271 $this->assertEquals(7, count($file->getVariablesIds()));
272 $this->assertEquals(4, count($file->getCommentsIds()));
273 }
274
275 public function testGetItemsEdgeCases()
276 {
277 $file = new ilSystemStyleLessFile($this->container->getSkinDirectory() . "edge-cases.less");
278
279 $expected_comment1 = new ilSystemStyleLessComment("// No Category to start");
280 $expected_comment2 = new ilSystemStyleLessComment("");
281
282 $expected_variable11 = new ilSystemStyleLessVariable("variableNoCategory1", "value11", "comment variable 11", "", []);
283 $expected_variable12 = new ilSystemStyleLessVariable("variableNoCategory1NoComment", "value12", "", "", []);
284
285 $expected_category1 = new ilSystemStyleLessCategory("Category 1 no valid section", "");
286
287 $expected_variable21 = new ilSystemStyleLessVariable("variableNoValidSection1", "value21", "", "Category 1 no valid section", []);
288 $expected_variable22 = new ilSystemStyleLessVariable("variableNoValidSection2", "value22", "comment", "Category 1 no valid section", []);
289
290 $expected_comment3 = new ilSystemStyleLessComment("");
291
292 $expected_category2 = new ilSystemStyleLessCategory("Category 2", "Comment Category 2");
293
294 $expected_variable31 = new ilSystemStyleLessVariable("regular", "value", "Hard references id", "Category 2", []);
295 $expected_variable32 = new ilSystemStyleLessVariable("variable21", "floor((@regular * 1.6)) * lighten(@regular, 20%)", "Hard references", "Category 2", ["regular"]);
296
297 $expected_comment4 = new ilSystemStyleLessComment("");
298
299 $expected_category3 = new ilSystemStyleLessCategory("Category 3", "No Section Between");
300 $expected_variable41 = new ilSystemStyleLessVariable("variable3", "value3", "", "Category 3", []);
301
302 $expected_items = [$expected_comment1,$expected_comment2,
303 $expected_variable11,$expected_variable12,
304 $expected_category1,$expected_variable21,$expected_variable22,
305 $expected_comment3,
306 $expected_category2,$expected_variable31,$expected_variable32,
307 $expected_comment4,
308 $expected_category3,$expected_variable41];
309
310 $this->assertEquals($expected_items, $file->getItems());
311 }
312}
An exception for terminatinating execution or to throw for unit testing.
ilSystemStyleConfig wraps all 'constants' to ensure the testability of all classes using those 'const...
Class ilLanguageMock.
Capsules data of a less category in the variables to less file.
Capsules all data which is neither part of a variable or category structure in the less file.
static recursiveRemoveDir($dir)
Recursive delete of a folder.
static generateFromId($skin_id, ilSystemStyleMessageStack $message_stack=null, ilSystemStyleConfig $system_styles_conf=null)
Generate the container class by parsing the corresponding XML.
static xCopy($src, $dest)
Recursive copy of a folder.
$DIC
Definition: xapitoken.php:46