]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/SugarTheme/SugarThemeTest.php
Added unit tests.
[Github/sugarcrm.git] / tests / include / SugarTheme / SugarThemeTest.php
1 <?php
2 require_once 'include/SugarTheme/SugarTheme.php';
3 require_once 'include/dir_inc.php';
4
5 class SugarThemeTest extends Sugar_PHPUnit_Framework_TestCase
6 {
7     private $_themeDef;
8     private $_themeObject;
9     private $_themeDefChild;
10     private $_themeObjectChild;
11     private $_olddeveloperMode;
12     
13     public function setup()
14     {
15         $themedef = array();
16         include('themes/'.SugarTestThemeUtilities::createAnonymousTheme().'/themedef.php');
17         
18         $this->_themeDef = $themedef;
19         SugarThemeRegistry::add($this->_themeDef);
20         $this->_themeObject = SugarThemeRegistry::get($this->_themeDef['dirName']);
21         
22         $themedef = array();
23         include('themes/'.SugarTestThemeUtilities::createAnonymousChildTheme($this->_themeObject->__toString()).'/themedef.php');
24         
25         $this->_themeDefChild = $themedef;
26         SugarThemeRegistry::add($this->_themeDefChild);
27         $this->_themeObjectChild = SugarThemeRegistry::get($this->_themeDefChild['dirName']);
28         
29         // test assumes developerMode is off, so css minifying happens
30         if ( isset($GLOBALS['sugar_config']['developerMode']) )
31             $this->_olddeveloperMode = $GLOBALS['sugar_config']['developerMode'];
32         $GLOBALS['sugar_config']['developerMode'] = false;
33     }
34     
35     public function testMagicIssetWorks()
36     {
37         $this->assertTrue(isset($this->_themeObject->dirName));
38     }
39     
40     public function tearDown()
41     {
42         $themesToRemove = array($this->_themeObject->__toString(),$this->_themeObjectChild->__toString());
43         
44         SugarTestThemeUtilities::removeAllCreatedAnonymousThemes();
45         
46         if ( $this->_olddeveloperMode )
47             $GLOBALS['sugar_config']['developerMode'] = $this->_olddeveloperMode;
48         else
49             unset($GLOBALS['sugar_config']['developerMode']);
50     }
51     
52     public function testCaching()
53     {
54         $this->_themeObject->getCSSURL("style.css");
55         $themename = $this->_themeObject->__toString();
56         $pathname = "cache/themes/{$themename}/css/style.css";
57         
58         // test if it's in the local cache
59         $this->assertTrue(isset($this->_themeObject->_cssCache['style.css']));
60         $this->assertEquals($this->_themeObject->_cssCache['style.css'],$pathname);
61         
62         // destroy object
63         $this->_themeObject->__destruct();
64         unset($this->_themeObject);
65         
66         // now recreate object
67         SugarThemeRegistry::add($this->_themeDef);
68         $this->_themeObject = SugarThemeRegistry::get($this->_themeDef['dirName']);
69         
70         // should still be in local cache
71         $this->assertTrue(isset($this->_themeObject->_cssCache['style.css']));
72         $this->assertEquals($this->_themeObject->_cssCache['style.css'],$pathname);
73         
74         // now, let's tell the theme we want to clear the cache on destroy
75         $this->_themeObject->clearCache();
76         
77         // destroy object
78         $this->_themeObject->__destruct();
79         unset($this->_themeObject);
80         
81         // now recreate object
82         SugarThemeRegistry::add($this->_themeDef);
83         $this->_themeObject = SugarThemeRegistry::get($this->_themeDef['dirName']);
84         
85         // should not be in local cache
86         $this->assertFalse(isset($this->_themeObject->_cssCache['style.css']));
87     }
88     
89     public function testCreateInstance()
90     {
91         foreach ( $this->_themeDef as $key => $value )
92             $this->assertEquals($this->_themeObject->$key,$value);
93     }
94     
95     public function testGetFilePath()
96     {
97         $this->assertEquals($this->_themeObject->getFilePath(),
98             'themes/'.$this->_themeDef['name']);
99     }
100     
101     public function testGetImagePath()
102     {
103         $this->assertEquals($this->_themeObject->getImagePath(),
104             'themes/'.$this->_themeDef['name'].'/images');
105     }
106     
107     public function testGetCSSPath()
108     {
109         $this->assertEquals($this->_themeObject->getCSSPath(),
110             'themes/'.$this->_themeDef['name'].'/css');
111     }
112     
113     public function testGetCSS()
114     {
115         $matches = array();
116         preg_match_all('/href="([^"]+)"/',$this->_themeObject->getCSS(),$matches);
117         $i = 0;
118         
119         $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/css\/yui.css/',$matches[1][$i++]);
120         $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/css\/deprecated.css/',$matches[1][$i++]);
121         $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/css\/style.css/',$matches[1][$i++]);
122         
123         $output = file_get_contents('cache/themes/'.$this->_themeObject->__toString().'/css/style.css');
124         $this->assertRegExp('/h2\{display:inline\}/',$output);
125     }
126     
127     public function testGetCSSWithParams()
128     {
129         $matches = array();
130         preg_match_all('/href="([^"]+)"/',$this->_themeObject->getCSS('blue','small'),$matches);
131         $i = 0;
132         
133         $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/css\/yui.css/',$matches[1][$i++]);
134         $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/css\/deprecated.css/',$matches[1][$i++]);
135         $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/css\/style.css/',$matches[1][$i++]);
136         
137         $output = file_get_contents('cache/themes/'.$this->_themeObject->__toString().'/css/style.css');
138         $this->assertRegExp('/h2\{display:inline\}/',$output);
139     }
140     
141     public function testGetCSSWithCustomStyleCSS()
142     {
143         create_custom_directory('themes/'.$this->_themeObject->__toString().'/css/');
144         sugar_file_put_contents('custom/themes/'.$this->_themeObject->__toString().'/css/style.css','h3 { color: red; }');
145         
146         $matches = array();
147         preg_match_all('/href="([^"]+)"/',$this->_themeObject->getCSS(),$matches);
148         $i = 0;
149         
150         $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/css\/yui.css/',$matches[1][$i++]);
151         $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/css\/deprecated.css/',$matches[1][$i++]);
152         $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/css\/style.css/',$matches[1][$i++]);
153         
154         $output = file_get_contents('cache/themes/'.$this->_themeObject->__toString().'/css/style.css');
155         $this->assertRegExp('/h2\{display:inline\}h3\{color:red\}/',$output);
156     }
157     
158     public function testGetCSSWithParentTheme()
159     {
160         $matches = array();
161         preg_match_all('/href="([^"]+)"/',$this->_themeObjectChild->getCSS(),$matches);
162         $i = 0;
163         
164         $this->assertRegExp('/themes\/'.$this->_themeObjectChild->__toString().'\/css\/yui.css/',$matches[1][$i++]);
165         $this->assertRegExp('/themes\/'.$this->_themeObjectChild->__toString().'\/css\/deprecated.css/',$matches[1][$i++]);
166         $this->assertRegExp('/themes\/'.$this->_themeObjectChild->__toString().'\/css\/style.css/',$matches[1][$i++]);
167         
168         $output = file_get_contents('cache/themes/'.$this->_themeObjectChild->__toString().'/css/style.css');
169         $this->assertRegExp('/h2\{display:inline\}h3\{display:inline\}/',$output);
170     }
171     
172     public function testGetCSSURLWithInvalidFileSpecifed()
173     {
174         $this->assertFalse($this->_themeObject->getCSSURL('ThisFileDoesNotExist.css'));
175     }
176     
177     public function testGetCSSURLAddsJsPathIfSpecified()
178     {
179         // check one may not hit cache
180         $this->assertRegExp('/style\.css\?/',$this->_themeObject->getCSSURL('style.css'));
181         // check two definitely should hit cache
182         $this->assertRegExp('/style\.css\?/',$this->_themeObject->getCSSURL('style.css'));
183         // check three for the jspath not being added
184         $this->assertNotContains('?',$this->_themeObject->getCSSURL('style.css',false));
185     }
186     
187     public function testGetJS()
188     {
189         $matches = array();
190         preg_match_all('/src="([^"]+)"/',$this->_themeObject->getJS(),$matches);
191         $i = 0;
192         
193         $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/js\/style-min.js/',$matches[1][$i++]);
194         
195         $output = file_get_contents('cache/themes/'.$this->_themeObject->__toString().'/js/style-min.js');
196         $this->assertRegExp('/var dog="cat";/',$output);
197     }
198     
199     public function testGetJSCustom()
200     {
201         create_custom_directory('themes/'.$this->_themeObject->__toString().'/js/');
202         sugar_file_put_contents('custom/themes/'.$this->_themeObject->__toString().'/js/style.js','var x = 1;');
203         
204         $matches = array();
205         preg_match_all('/src="([^"]+)"/',$this->_themeObject->getJS(),$matches);
206         $i = 0;
207         
208         $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/js\/style-min.js/',$matches[1][$i++]);
209          
210         $output = file_get_contents('cache/themes/'.$this->_themeObject->__toString().'/js/style-min.js');
211         $this->assertRegExp('/var dog="cat";/',$output);
212         $this->assertRegExp('/var x=1;/',$output);
213     }
214     
215     public function testGetJSWithParentTheme()
216     {
217         $matches = array();
218         preg_match_all('/src="([^"]+)"/',$this->_themeObjectChild->getJS(),$matches);
219         $i = 0;
220         
221         $this->assertRegExp('/themes\/'.$this->_themeObjectChild->__toString().'\/js\/style-min.js/',$matches[1][$i++]);
222         
223         $output = file_get_contents('cache/themes/'.$this->_themeObjectChild->__toString().'/js/style-min.js');
224         $this->assertRegExp('/var dog="cat";var bird="frog";/',$output);
225     }
226     
227     public function testGetJSURLWithInvalidFileSpecifed()
228     {
229         $this->assertFalse($this->_themeObject->getJSURL('ThisFileDoesNotExist.js'));
230     }
231     
232     public function testGetJSURLAddsJsPathIfSpecified()
233     {
234         // check one may not hit cache
235         $this->assertRegExp('/style-min\.js\?/',$this->_themeObject->getJSURL('style.js'));
236         // check two definitely should hit cache
237         $this->assertRegExp('/style-min\.js\?/',$this->_themeObject->getJSURL('style.js'));
238         // check three for the jspath not being added
239         $this->assertNotContains('?',$this->_themeObject->getJSURL('style.js',false));
240     }
241     
242     public function testGetImageURL()
243     {
244         $this->assertEquals('themes/'.$this->_themeObject->__toString().'/images/Accounts.gif',
245             $this->_themeObject->getImageURL('Accounts.gif',false));
246     }
247     
248     public function testGetImageURLWithInvalidFileSpecifed()
249     {
250         $this->assertFalse($this->_themeObject->getImageURL('ThisFileDoesNotExist.gif'));
251     }
252     
253     public function testGetImageURLCustom()
254     {
255         create_custom_directory('themes/'.$this->_themeObject->__toString().'/images/');
256         sugar_touch('custom/themes/'.$this->_themeObject->__toString().'/images/Accounts.gif');
257         
258         $this->assertEquals('custom/themes/'.$this->_themeObject->__toString().'/images/Accounts.gif',
259             $this->_themeObject->getImageURL('Accounts.gif',false));
260     }
261     
262     public function testGetImageURLCustomDifferentExtension()
263     {
264         create_custom_directory('themes/'.$this->_themeObject->__toString().'/images/');
265         sugar_touch('custom/themes/'.$this->_themeObject->__toString().'/images/Accounts.png');
266         
267         $this->assertEquals('custom/themes/'.$this->_themeObject->__toString().'/images/Accounts.png',
268             $this->_themeObject->getImageURL('Accounts.gif',false));
269     }
270     
271     public function testGetImageURLDefault()
272     {
273         $this->assertEquals('themes/default/images/Emails.gif',$this->_themeObject->getImageURL('Emails.gif',false));
274     }
275     
276     public function testGetImageURLDefaultCustom()
277     {
278         create_custom_directory('themes/default/images/');
279         sugar_touch('custom/themes/default/images/Emails.gif');
280         
281         $this->assertEquals('custom/themes/default/images/Emails.gif',
282             $this->_themeObject->getImageURL('Emails.gif',false));
283         
284         unlink('custom/themes/default/images/Emails.gif');
285     }
286     
287     public function testGetImageURLNotFound()
288     {
289         $this->assertEquals('',$this->_themeObject->getImageURL('NoImageByThisName.gif',false));
290     }
291     
292     public function testGetImageURLAddsJsPathIfSpecified()
293     {
294         // check one may not hit cache
295         $this->assertRegExp('/Accounts\.gif\?/',$this->_themeObject->getImageURL('Accounts.gif'));
296         // check two definitely should hit cache
297         $this->assertRegExp('/Accounts\.gif\?/',$this->_themeObject->getImageURL('Accounts.gif'));
298         // check three for the jspath not being added
299         $this->assertNotContains('?',$this->_themeObject->getImageURL('Accounts.gif',false));
300     }
301     
302     public function testGetImageURLWithParentTheme()
303     {
304         $this->assertEquals('themes/'.$this->_themeObject->__toString().'/images/Accounts.gif',
305             $this->_themeObjectChild->getImageURL('Accounts.gif',false));
306     }
307     
308     public function testGetTemplate()
309     {
310         $this->assertEquals('themes/'.$this->_themeObject->__toString().'/tpls/header.tpl',
311             $this->_themeObject->getTemplate('header.tpl'));
312     }
313     
314     public function testGetTemplateCustom()
315     {
316         create_custom_directory('themes/'.$this->_themeObject->__toString().'/tpls/');
317         sugar_touch('custom/themes/'.$this->_themeObject->__toString().'/tpls/header.tpl');
318         
319         $this->assertEquals('custom/themes/'.$this->_themeObject->__toString().'/tpls/header.tpl',
320             $this->_themeObject->getTemplate('header.tpl'));
321     }
322     
323     public function testGetTemplateDefaultCustom()
324     {
325         create_custom_directory('themes/default/tpls/');
326         sugar_touch('custom/themes/default/tpls/SomeDefaultTemplate.tpl');
327         
328         $this->assertEquals('custom/themes/default/tpls/SomeDefaultTemplate.tpl',
329             $this->_themeObject->getTemplate('SomeDefaultTemplate.tpl'));
330         
331         unlink('custom/themes/default/tpls/SomeDefaultTemplate.tpl');
332     }
333     
334     public function testGetTemplateWithParentTheme()
335     {
336         $this->assertEquals('themes/'.$this->_themeObject->__toString().'/tpls/header.tpl',
337             $this->_themeObjectChild->getTemplate('header.tpl'));
338     }
339     
340     public function testGetTemplateNotFound()
341     {
342         $this->assertFalse($this->_themeObject->getTemplate('NoTemplateWithThisName.tpl'));
343     }
344     
345     public function testGetAllImages()
346     {
347         $images = $this->_themeObject->getAllImages();
348         
349         $this->assertEquals(
350             $this->_themeObject->getImageURL('Emails.gif',false),
351             $images['Emails.gif']);
352     }
353     
354     public function testGetAllImagesWhenImageIsInParentTheme()
355     {
356         $images = $this->_themeObjectChild->getAllImages();
357         
358         $this->assertEquals(
359             $this->_themeObjectChild->getImageURL('Accounts.gif',false),
360             $images['Accounts.gif']);
361         
362         $this->assertContains(
363             $this->_themeObject->getImagePath(),
364             $images['Accounts.gif']);
365     }
366     
367     public function testGetImageSpecifyingWidthAndHeightAndOtherAttributes()
368     {
369         $this->assertEquals(
370             $this->_themeObject->getImage('Emails','alt="foo"',20,30),
371             "<img src=\"". $this->_themeObject->getImageURL('Emails.gif') ."\" width=\"20\" height=\"30\" alt=\"foo\" />"
372             );
373         
374         // check again to see if caching of the image size works as expected
375         $this->assertEquals(
376             $this->_themeObject->getImage('Emails','alt="foo"',30,40),
377             "<img src=\"". $this->_themeObject->getImageURL('Emails.gif') ."\" width=\"20\" height=\"30\" alt=\"foo\" />"
378             );
379     }
380     
381     public function testGetImageDetectingImageHeightAndWidth()
382     {
383         $size = getimagesize($this->_themeObject->getImageURL('Contacts.gif',false));
384         
385         $this->assertEquals(
386             $this->_themeObject->getImage('Contacts'),
387             "<img src=\"". $this->_themeObject->getImageURL('Contacts.gif') ."\" width=\"{$size[0]}\" height=\"{$size[1]}\"  />"
388             );
389     }
390     
391     public function testGetImageWithInvalidImage()
392     {
393         $this->assertFalse($this->_themeObject->getImage('ThisImageDoesNotExist'));
394     }
395 }