ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
ilSkinStyleLessFileTest.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/DICMock.php");
8
15
16
21
25 protected $container;
26
30 protected $style;
31
32 protected function setUp()
33 {
34 global $DIC;
35
37
38 $this->system_style_config = new ilSystemStyleConfigMock();
39
40 mkdir($this->system_style_config->test_skin_temp_path);
41 ilSystemStyleSkinContainer::xCopy($this->system_style_config->test_skin_original_path, $this->system_style_config->test_skin_temp_path);
42
43 $this->container = ilSystemStyleSkinContainer::generateFromId("skin1", null, $this->system_style_config);
44 $this->style = $this->container->getSkin()->getStyle("style1");
45 }
46
47 protected function tearDown()
48 {
49 ilSystemStyleSkinContainer::recursiveRemoveDir($this->system_style_config->test_skin_temp_path);
50 }
51
52 public function testConstructAndRead() {
53 $file = new ilSystemStyleLessFile($this->container->getLessVariablesFilePath($this->style->getId()));
54 $this->assertEquals(14,count($file->getItems()));
55 }
56
57 public function testReadCorrectTypes() {
58 $file = new ilSystemStyleLessFile($this->container->getLessVariablesFilePath($this->style->getId()));
59
60 $this->assertEquals(2,count($file->getCategories()));
61 $this->assertEquals(6,count($file->getVariablesIds()));
62 $this->assertEquals(6,count($file->getCommentsIds()));
63 }
64
65
66 public function testGetVariableByName(){
67 $file = new ilSystemStyleLessFile($this->container->getLessVariablesFilePath($this->style->getId()));
68
69 $expected_variable11 = new ilSystemStyleLessVariable("variable11", "value11", "comment variable 11","Category 1", []);
70 $expected_variable12 = new ilSystemStyleLessVariable("variable12", "value12", "comment variable 12","Category 1", []);
71 $expected_variable13 = new ilSystemStyleLessVariable("variable13", "@variable11", "comment variable 13","Category 1", ["variable11"]);
72
73 $expected_variable21 = new ilSystemStyleLessVariable("variable21", "@variable11", "comment variable 21","Category 2", ["variable11"]);
74 $expected_variable22 = new ilSystemStyleLessVariable("variable22", "value21", "comment variable 22","Category 2", []);
75 $expected_variable23 = new ilSystemStyleLessVariable("variable23", "@variable21", "comment variable 23","Category 2", ["variable21"]);
76
77 $this->assertEquals($expected_variable11,$file->getVariableByName("variable11"));
78 $this->assertEquals($expected_variable12,$file->getVariableByName("variable12"));
79 $this->assertEquals($expected_variable13,$file->getVariableByName("variable13"));
80
81 $this->assertEquals($expected_variable21,$file->getVariableByName("variable21"));
82 $this->assertEquals($expected_variable22,$file->getVariableByName("variable22"));
83 $this->assertEquals($expected_variable23,$file->getVariableByName("variable23"));
84 }
85
86 public function testGetCategory(){
87 $file = new ilSystemStyleLessFile($this->container->getLessVariablesFilePath($this->style->getId()));
88
89 $expected_category1 = new ilSystemStyleLessCategory("Category 1", "Comment Category 1");
90 $expected_category2 = new ilSystemStyleLessCategory("Category 2", "Comment Category 2");
91 $expected_categories = [$expected_category1,$expected_category2];
92
93 $this->assertEquals($expected_categories,$file->getCategories());
94 }
95
96 public function testGetItems(){
97 $file = new ilSystemStyleLessFile($this->container->getLessVariablesFilePath($this->style->getId()));
98
99 $expected_category1 = new ilSystemStyleLessCategory("Category 1", "Comment Category 1");
100 $expected_comment2 = new ilSystemStyleLessComment("// Random Section 1");
101 $expected_comment3 = new ilSystemStyleLessComment("");
102 $expected_variable11 = new ilSystemStyleLessVariable("variable11", "value11", "comment variable 11","Category 1", []);
103 $expected_variable12 = new ilSystemStyleLessVariable("variable12", "value12", "comment variable 12","Category 1", []);
104 $expected_variable13 = new ilSystemStyleLessVariable("variable13", "@variable11", "comment variable 13","Category 1", ["variable11"]);
105 $expected_comment4 = new ilSystemStyleLessComment("");
106 $expected_category2 = new ilSystemStyleLessCategory("Category 2", "Comment Category 2");
107 $expected_comment6 = new ilSystemStyleLessComment("/**");
108 $expected_comment7 = new ilSystemStyleLessComment(" Random Section 2 **/");
109 $expected_comment8 = new ilSystemStyleLessComment("");
110 $expected_variable21 = new ilSystemStyleLessVariable("variable21", "@variable11", "comment variable 21","Category 2", ["variable11"]);
111 $expected_variable22 = new ilSystemStyleLessVariable("variable22", "value21", "comment variable 22","Category 2", []);
112 $expected_variable23 = new ilSystemStyleLessVariable("variable23", "@variable21", "comment variable 23","Category 2", ["variable21"]);
113
114 $expected_items = [$expected_category1,
115 $expected_comment2,$expected_comment3,
116 $expected_variable11,$expected_variable12,$expected_variable13,
117 $expected_comment4,
118 $expected_category2,
119 $expected_comment6,$expected_comment7,$expected_comment8,
120 $expected_variable21,$expected_variable22,$expected_variable23];
121
122 $this->assertEquals($expected_items,$file->getItems());
123 }
124
125 public function testGetContent(){
126 $file = new ilSystemStyleLessFile($this->container->getLessVariablesFilePath($this->style->getId()));
127 $expected_content = file_get_contents($this->container->getLessVariablesFilePath($this->style->getId()));
128 $this->assertEquals($expected_content,$file->getContent());
129 }
130
131 public function testReadWriteDouble(){
132 $expected_content = file_get_contents($this->container->getLessVariablesFilePath($this->style->getId()));
133
134 $file = new ilSystemStyleLessFile($this->container->getLessVariablesFilePath($this->style->getId()));
135 $file->write();
136 $file = new ilSystemStyleLessFile($this->container->getLessVariablesFilePath($this->style->getId()));
137 $file->write();
138 $file = new ilSystemStyleLessFile($this->container->getLessVariablesFilePath($this->style->getId()));
139
140 $this->assertEquals($expected_content,$file->getContent());
141 }
142
144 $expected_content = file_get_contents($this->container->getSkinDirectory()."full.less");
145
146 $file = new ilSystemStyleLessFile($this->container->getSkinDirectory()."full.less");
147 $file->write();
148 $file = new ilSystemStyleLessFile($this->container->getSkinDirectory()."full.less");
149 $file->write();
150 $file = new ilSystemStyleLessFile($this->container->getSkinDirectory()."full.less");
151
152 $this->assertEquals($expected_content,$file->getContent());
153 }
154
155 public function testChangeVariable(){
156 $file = new ilSystemStyleLessFile($this->container->getLessVariablesFilePath($this->style->getId()));
157 $variable = $file->getVariableByName("variable11");
158 $variable->setValue("newvalue11");
159
160 $expected_category1 = new ilSystemStyleLessCategory("Category 1", "Comment Category 1");
161 $expected_comment2 = new ilSystemStyleLessComment("// Random Section 1");
162 $expected_comment3 = new ilSystemStyleLessComment("");
163 $expected_variable11 = new ilSystemStyleLessVariable("variable11", "newvalue11", "comment variable 11","Category 1", []);
164 $expected_variable12 = new ilSystemStyleLessVariable("variable12", "value12", "comment variable 12","Category 1", []);
165 $expected_variable13 = new ilSystemStyleLessVariable("variable13", "@variable11", "comment variable 13","Category 1", ["variable11"]);
166 $expected_comment4 = new ilSystemStyleLessComment("");
167 $expected_category2 = new ilSystemStyleLessCategory("Category 2", "Comment Category 2");
168 $expected_comment6 = new ilSystemStyleLessComment("/**");
169 $expected_comment7 = new ilSystemStyleLessComment(" Random Section 2 **/");
170 $expected_comment8 = new ilSystemStyleLessComment("");
171 $expected_variable21 = new ilSystemStyleLessVariable("variable21", "@variable11", "comment variable 21","Category 2", ["variable11"]);
172 $expected_variable22 = new ilSystemStyleLessVariable("variable22", "value21", "comment variable 22","Category 2", []);
173 $expected_variable23 = new ilSystemStyleLessVariable("variable23", "@variable21", "comment variable 23","Category 2", ["variable21"]);
174
175 $expected_items = [$expected_category1,
176 $expected_comment2,$expected_comment3,
177 $expected_variable11,$expected_variable12,$expected_variable13,
178 $expected_comment4,
179 $expected_category2,
180 $expected_comment6,$expected_comment7,$expected_comment8,
181 $expected_variable21,$expected_variable22,$expected_variable23];
182
183 $this->assertEquals($expected_items,$file->getItems());
184
185 }
186
187 public function testAddAndWriteItems(){
188 $empty_less = new ilSystemStyleLessFile($this->container->getSkinDirectory()."empty.less");
189
190 $expected_category1 = new ilSystemStyleLessCategory("Category 1", "Comment Category 1");
191 $expected_comment2 = new ilSystemStyleLessComment("// Random Section 1");
192 $expected_comment3 = new ilSystemStyleLessComment("");
193 $expected_variable11 = new ilSystemStyleLessVariable("variable11", "value11", "comment variable 11","Category 1", []);
194 $expected_variable12 = new ilSystemStyleLessVariable("variable12", "value12", "comment variable 12","Category 1", []);
195 $expected_variable13 = new ilSystemStyleLessVariable("variable13", "@variable11", "comment variable 13","Category 1", ["variable11"]);
196 $expected_comment4 = new ilSystemStyleLessComment("");
197 $expected_category2 = new ilSystemStyleLessCategory("Category 2", "Comment Category 2");
198 $expected_comment6 = new ilSystemStyleLessComment("/**");
199 $expected_comment7 = new ilSystemStyleLessComment(" Random Section 2 **/");
200 $expected_comment8 = new ilSystemStyleLessComment("");
201 $expected_variable21 = new ilSystemStyleLessVariable("variable21", "@variable11", "comment variable 21","Category 2", ["variable11"]);
202 $expected_variable22 = new ilSystemStyleLessVariable("variable22", "value21", "comment variable 22","Category 2", []);
203 $expected_variable23 = new ilSystemStyleLessVariable("variable23", "@variable21", "comment variable 23","Category 2", ["variable21"]);
204
205 $expected_items = [$expected_category1,
206 $expected_comment2,$expected_comment3,
207 $expected_variable11,$expected_variable12,$expected_variable13,
208 $expected_comment4,
209 $expected_category2,
210 $expected_comment6,$expected_comment7,$expected_comment8,
211 $expected_variable21,$expected_variable22,$expected_variable23];
212
213 foreach($expected_items as $item){
214 $empty_less->addItem($item);
215 }
216 $empty_less->write();
217
218 $new_less = new ilSystemStyleLessFile($this->container->getSkinDirectory()."empty.less");
219 $this->assertEquals($expected_items,$new_less->getItems());
220 }
221
222 public function testGetVariableReferences(){
223 $file = new ilSystemStyleLessFile($this->container->getLessVariablesFilePath($this->style->getId()));
224
225 $this->assertEquals(["variable13","variable21"],$file->getReferencesToVariable("variable11"));
226 $this->assertEquals([],$file->getReferencesToVariable("variable12"));
227 $this->assertEquals([],$file->getReferencesToVariable("variable13"));
228
229 $this->assertEquals(["variable23"],$file->getReferencesToVariable("variable21"));
230 $this->assertEquals([],$file->getReferencesToVariable("variable22"));
231 $this->assertEquals([],$file->getReferencesToVariable("variable23"));
232 }
233
235 $file = new ilSystemStyleLessFile($this->container->getLessVariablesFilePath($this->style->getId()));
236
237 $this->assertEquals("variable13; variable21; ",$file->getReferencesToVariableAsString("variable11"));
238 $this->assertEquals("",$file->getReferencesToVariableAsString("variable12"));
239 $this->assertEquals("",$file->getReferencesToVariableAsString("variable13"));
240
241 $this->assertEquals("variable23; ",$file->getReferencesToVariableAsString("variable21"));
242 $this->assertEquals("",$file->getReferencesToVariableAsString("variable22"));
243 $this->assertEquals("",$file->getReferencesToVariableAsString("variable23"));
244 }
245
247 $file = new ilSystemStyleLessFile($this->container->getSkinDirectory()."edge-cases.less");
248
249 $this->assertEquals(3,count($file->getCategories()));
250 $this->assertEquals(7,count($file->getVariablesIds()));
251 $this->assertEquals(4,count($file->getCommentsIds()));
252 }
253
254 public function testGetItemsEdgeCases(){
255 $file = new ilSystemStyleLessFile($this->container->getSkinDirectory()."edge-cases.less");
256
257 $expected_comment1 = new ilSystemStyleLessComment("// No Category to start");
258 $expected_comment2 = new ilSystemStyleLessComment("");
259
260 $expected_variable11 = new ilSystemStyleLessVariable("variableNoCategory1", "value11", "comment variable 11","", []);
261 $expected_variable12 = new ilSystemStyleLessVariable("variableNoCategory1NoComment", "value12", "","", []);
262
263 $expected_category1 = new ilSystemStyleLessCategory("Category 1 no valid section", "");
264
265 $expected_variable21 = new ilSystemStyleLessVariable("variableNoValidSection1", "value21", "","Category 1 no valid section", []);
266 $expected_variable22 = new ilSystemStyleLessVariable("variableNoValidSection2", "value22", "comment","Category 1 no valid section", []);
267
268 $expected_comment3 = new ilSystemStyleLessComment("");
269
270 $expected_category2 = new ilSystemStyleLessCategory("Category 2", "Comment Category 2");
271
272 $expected_variable31 = new ilSystemStyleLessVariable("regular", "value", "Hard references id","Category 2", []);
273 $expected_variable32 = new ilSystemStyleLessVariable("variable21", "floor((@regular * 1.6)) * lighten(@regular, 20%)", "Hard references","Category 2", ["regular"]);
274
275 $expected_comment4 = new ilSystemStyleLessComment("");
276
277 $expected_category3 = new ilSystemStyleLessCategory("Category 3", "No Section Between");
278 $expected_variable41 = new ilSystemStyleLessVariable("variable3", "value3", "","Category 3", []);
279
280 $expected_items = [$expected_comment1,$expected_comment2,
281 $expected_variable11,$expected_variable12,
282 $expected_category1,$expected_variable21,$expected_variable22,
283 $expected_comment3,
284 $expected_category2,$expected_variable31,$expected_variable32,
285 $expected_comment4,
286 $expected_category3,$expected_variable41];
287
288 $this->assertEquals($expected_items,$file->getItems());
289 }
290}
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.
if(!file_exists("$old.txt")) if( $old===$new) if(file_exists("$new.txt")) $file
global $DIC