]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/SugarTheme/SugarThemeTest.php
Release 6.2.0
[Github/sugarcrm.git] / tests / include / SugarTheme / SugarThemeTest.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
5  * 
6  * This program is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  * 
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
16  * details.
17  * 
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  * 
23  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
25  * 
26  * The interactive user interfaces in modified source and object code versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  * 
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the "Powered by
32  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33  * technical reasons, the Appropriate Legal Notices must display the words
34  * "Powered by SugarCRM".
35  ********************************************************************************/
36
37  
38 require_once 'include/SugarTheme/SugarTheme.php';
39 require_once 'include/dir_inc.php';
40
41 class SugarThemeTest extends Sugar_PHPUnit_Framework_TestCase
42 {
43     private $_themeDef;
44     private $_themeObject;
45     private $_themeDefChild;
46     private $_themeObjectChild;
47     private $_olddeveloperMode;
48     
49     public function setup()
50     {
51         $themedef = array();
52         include('themes/'.SugarTestThemeUtilities::createAnonymousTheme().'/themedef.php');
53         
54         $this->_themeDef = $themedef;
55         SugarThemeRegistry::add($this->_themeDef);
56         $this->_themeObject = SugarThemeRegistry::get($this->_themeDef['dirName']);
57         
58         $themedef = array();
59         include('themes/'.SugarTestThemeUtilities::createAnonymousChildTheme($this->_themeObject->__toString()).'/themedef.php');
60         
61         $this->_themeDefChild = $themedef;
62         SugarThemeRegistry::add($this->_themeDefChild);
63         $this->_themeObjectChild = SugarThemeRegistry::get($this->_themeDefChild['dirName']);
64         
65         // test assumes developerMode is off, so css minifying happens
66         if ( isset($GLOBALS['sugar_config']['developerMode']) )
67             $this->_olddeveloperMode = $GLOBALS['sugar_config']['developerMode'];
68         $GLOBALS['sugar_config']['developerMode'] = false;
69     }
70     
71     public function testMagicIssetWorks()
72     {
73         $this->assertTrue(isset($this->_themeObject->dirName));
74     }
75     
76     public function tearDown()
77     {
78         $themesToRemove = array($this->_themeObject->__toString(),$this->_themeObjectChild->__toString());
79         
80         SugarTestThemeUtilities::removeAllCreatedAnonymousThemes();
81         
82         if ( $this->_olddeveloperMode )
83             $GLOBALS['sugar_config']['developerMode'] = $this->_olddeveloperMode;
84         else
85             unset($GLOBALS['sugar_config']['developerMode']);
86     }
87     
88     public function testCaching()
89     {
90         $this->_themeObject->getCSSURL("style.css");
91         $themename = $this->_themeObject->__toString();
92         $pathname = "cache/themes/{$themename}/css/style.css";
93         
94         // test if it's in the local cache
95         $this->assertTrue(isset($this->_themeObject->_cssCache['style.css']));
96         $this->assertEquals($this->_themeObject->_cssCache['style.css'],$pathname);
97         
98         // destroy object
99         $this->_themeObject->__destruct();
100         unset($this->_themeObject);
101         
102         // now recreate object
103         SugarThemeRegistry::add($this->_themeDef);
104         $this->_themeObject = SugarThemeRegistry::get($this->_themeDef['dirName']);
105         
106         // should still be in local cache
107         $this->assertTrue(isset($this->_themeObject->_cssCache['style.css']));
108         $this->assertEquals($this->_themeObject->_cssCache['style.css'],$pathname);
109         
110         // now, let's tell the theme we want to clear the cache on destroy
111         $this->_themeObject->clearCache();
112         
113         // destroy object
114         $this->_themeObject->__destruct();
115         unset($this->_themeObject);
116         
117         // now recreate object
118         SugarThemeRegistry::add($this->_themeDef);
119         $this->_themeObject = SugarThemeRegistry::get($this->_themeDef['dirName']);
120         
121         // should not be in local cache
122         $this->assertFalse(isset($this->_themeObject->_cssCache['style.css']));
123     }
124     
125     public function testCreateInstance()
126     {
127         foreach ( $this->_themeDef as $key => $value )
128             $this->assertEquals($this->_themeObject->$key,$value);
129     }
130     
131     public function testGetFilePath()
132     {
133         $this->assertEquals($this->_themeObject->getFilePath(),
134             'themes/'.$this->_themeDef['name']);
135     }
136     
137     public function testGetImagePath()
138     {
139         $this->assertEquals($this->_themeObject->getImagePath(),
140             'themes/'.$this->_themeDef['name'].'/images');
141     }
142     
143     public function testGetCSSPath()
144     {
145         $this->assertEquals($this->_themeObject->getCSSPath(),
146             'themes/'.$this->_themeDef['name'].'/css');
147     }
148     
149     public function testGetCSS()
150     {
151         $matches = array();
152         preg_match_all('/href="([^"]+)"/',$this->_themeObject->getCSS(),$matches);
153         $i = 0;
154         
155         $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/css\/yui.css/',$matches[1][$i++]);
156         $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/css\/deprecated.css/',$matches[1][$i++]);
157         $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/css\/style.css/',$matches[1][$i++]);
158         
159         $output = file_get_contents('cache/themes/'.$this->_themeObject->__toString().'/css/style.css');
160         $this->assertRegExp('/h2\{display:inline\}/',$output);
161     }
162     
163     public function testGetCSSWithParams()
164     {
165         $matches = array();
166         preg_match_all('/href="([^"]+)"/',$this->_themeObject->getCSS('blue','small'),$matches);
167         $i = 0;
168         
169         $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/css\/yui.css/',$matches[1][$i++]);
170         $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/css\/deprecated.css/',$matches[1][$i++]);
171         $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/css\/style.css/',$matches[1][$i++]);
172         
173         $output = file_get_contents('cache/themes/'.$this->_themeObject->__toString().'/css/style.css');
174         $this->assertRegExp('/h2\{display:inline\}/',$output);
175     }
176     
177     public function testGetCSSWithCustomStyleCSS()
178     {
179         create_custom_directory('themes/'.$this->_themeObject->__toString().'/css/');
180         sugar_file_put_contents('custom/themes/'.$this->_themeObject->__toString().'/css/style.css','h3 { color: red; }');
181         
182         $matches = array();
183         preg_match_all('/href="([^"]+)"/',$this->_themeObject->getCSS(),$matches);
184         $i = 0;
185         
186         $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/css\/yui.css/',$matches[1][$i++]);
187         $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/css\/deprecated.css/',$matches[1][$i++]);
188         $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/css\/style.css/',$matches[1][$i++]);
189         
190         $output = file_get_contents('cache/themes/'.$this->_themeObject->__toString().'/css/style.css');
191         $this->assertRegExp('/h2\{display:inline\}h3\{color:red\}/',$output);
192     }
193     
194     public function testGetCSSWithParentTheme()
195     {
196         $matches = array();
197         preg_match_all('/href="([^"]+)"/',$this->_themeObjectChild->getCSS(),$matches);
198         $i = 0;
199         
200         $this->assertRegExp('/themes\/'.$this->_themeObjectChild->__toString().'\/css\/yui.css/',$matches[1][$i++]);
201         $this->assertRegExp('/themes\/'.$this->_themeObjectChild->__toString().'\/css\/deprecated.css/',$matches[1][$i++]);
202         $this->assertRegExp('/themes\/'.$this->_themeObjectChild->__toString().'\/css\/style.css/',$matches[1][$i++]);
203         
204         $output = file_get_contents('cache/themes/'.$this->_themeObjectChild->__toString().'/css/style.css');
205         $this->assertRegExp('/h2\{display:inline\}h3\{display:inline\}/',$output);
206     }
207     
208     public function testGetCSSURLWithInvalidFileSpecifed()
209     {
210         $this->assertFalse($this->_themeObject->getCSSURL('ThisFileDoesNotExist.css'));
211     }
212     
213     public function testGetCSSURLAddsJsPathIfSpecified()
214     {
215         // check one may not hit cache
216         $this->assertRegExp('/style\.css\?/',$this->_themeObject->getCSSURL('style.css'));
217         // check two definitely should hit cache
218         $this->assertRegExp('/style\.css\?/',$this->_themeObject->getCSSURL('style.css'));
219         // check three for the jspath not being added
220         $this->assertNotContains('?',$this->_themeObject->getCSSURL('style.css',false));
221     }
222     
223     public function testGetJS()
224     {
225         $matches = array();
226         preg_match_all('/src="([^"]+)"/',$this->_themeObject->getJS(),$matches);
227         $i = 0;
228         
229         $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/js\/style-min.js/',$matches[1][$i++]);
230         
231         $output = file_get_contents('cache/themes/'.$this->_themeObject->__toString().'/js/style-min.js');
232         $this->assertRegExp('/var dog="cat";/',$output);
233     }
234     
235     public function testGetJSCustom()
236     {
237         create_custom_directory('themes/'.$this->_themeObject->__toString().'/js/');
238         sugar_file_put_contents('custom/themes/'.$this->_themeObject->__toString().'/js/style.js','var x = 1;');
239         
240         $matches = array();
241         preg_match_all('/src="([^"]+)"/',$this->_themeObject->getJS(),$matches);
242         $i = 0;
243         
244         $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/js\/style-min.js/',$matches[1][$i++]);
245          
246         $output = file_get_contents('cache/themes/'.$this->_themeObject->__toString().'/js/style-min.js');
247         $this->assertRegExp('/var dog="cat";/',$output);
248         $this->assertRegExp('/var x=1;/',$output);
249     }
250     
251     public function testGetJSWithParentTheme()
252     {
253         $matches = array();
254         preg_match_all('/src="([^"]+)"/',$this->_themeObjectChild->getJS(),$matches);
255         $i = 0;
256         
257         $this->assertRegExp('/themes\/'.$this->_themeObjectChild->__toString().'\/js\/style-min.js/',$matches[1][$i++]);
258         
259         $output = file_get_contents('cache/themes/'.$this->_themeObjectChild->__toString().'/js/style-min.js');
260         $this->assertRegExp('/var dog="cat";var bird="frog";/',$output);
261     }
262     
263     public function testGetJSURLWithInvalidFileSpecifed()
264     {
265         $this->assertFalse($this->_themeObject->getJSURL('ThisFileDoesNotExist.js'));
266     }
267     
268     public function testGetJSURLAddsJsPathIfSpecified()
269     {
270         // check one may not hit cache
271         $this->assertRegExp('/style-min\.js\?/',$this->_themeObject->getJSURL('style.js'));
272         // check two definitely should hit cache
273         $this->assertRegExp('/style-min\.js\?/',$this->_themeObject->getJSURL('style.js'));
274         // check three for the jspath not being added
275         $this->assertNotContains('?',$this->_themeObject->getJSURL('style.js',false));
276     }
277     
278     public function testGetImageURL()
279     {
280         $this->assertEquals('themes/'.$this->_themeObject->__toString().'/images/Accounts.gif',
281             $this->_themeObject->getImageURL('Accounts.gif',false));
282     }
283     
284     public function testGetImageURLWithInvalidFileSpecifed()
285     {
286         $this->assertFalse($this->_themeObject->getImageURL('ThisFileDoesNotExist.gif'));
287     }
288     
289     public function testGetImageURLCustom()
290     {
291         create_custom_directory('themes/'.$this->_themeObject->__toString().'/images/');
292         sugar_touch('custom/themes/'.$this->_themeObject->__toString().'/images/Accounts.gif');
293         
294         $this->assertEquals('custom/themes/'.$this->_themeObject->__toString().'/images/Accounts.gif',
295             $this->_themeObject->getImageURL('Accounts.gif',false));
296     }
297     
298     public function testGetImageURLCustomDifferentExtension()
299     {
300         create_custom_directory('themes/'.$this->_themeObject->__toString().'/images/');
301         sugar_touch('custom/themes/'.$this->_themeObject->__toString().'/images/Accounts.png');
302         
303         $this->assertEquals('custom/themes/'.$this->_themeObject->__toString().'/images/Accounts.png',
304             $this->_themeObject->getImageURL('Accounts.gif',false));
305     }
306     
307     public function testGetImageURLDefault()
308     {
309         $this->assertEquals('themes/default/images/Emails.gif',$this->_themeObject->getImageURL('Emails.gif',false));
310     }
311     
312     public function testGetImageURLDefaultCustom()
313     {
314         create_custom_directory('themes/default/images/');
315         sugar_touch('custom/themes/default/images/Emails.gif');
316         
317         $this->assertEquals('custom/themes/default/images/Emails.gif',
318             $this->_themeObject->getImageURL('Emails.gif',false));
319         
320         unlink('custom/themes/default/images/Emails.gif');
321     }
322     
323     public function testGetImageURLNotFound()
324     {
325         $this->assertEquals('',$this->_themeObject->getImageURL('NoImageByThisName.gif',false));
326     }
327     
328     public function testGetImageURLAddsJsPathIfSpecified()
329     {
330         // check one may not hit cache
331         $this->assertRegExp('/Accounts\.gif\?/',$this->_themeObject->getImageURL('Accounts.gif'));
332         // check two definitely should hit cache
333         $this->assertRegExp('/Accounts\.gif\?/',$this->_themeObject->getImageURL('Accounts.gif'));
334         // check three for the jspath not being added
335         $this->assertNotContains('?',$this->_themeObject->getImageURL('Accounts.gif',false));
336     }
337     
338     public function testGetImageURLWithParentTheme()
339     {
340         $this->assertEquals('themes/'.$this->_themeObject->__toString().'/images/Accounts.gif',
341             $this->_themeObjectChild->getImageURL('Accounts.gif',false));
342     }
343     
344     public function testGetTemplate()
345     {
346         $this->assertEquals('themes/'.$this->_themeObject->__toString().'/tpls/header.tpl',
347             $this->_themeObject->getTemplate('header.tpl'));
348     }
349     
350     public function testGetTemplateCustom()
351     {
352         create_custom_directory('themes/'.$this->_themeObject->__toString().'/tpls/');
353         sugar_touch('custom/themes/'.$this->_themeObject->__toString().'/tpls/header.tpl');
354         
355         $this->assertEquals('custom/themes/'.$this->_themeObject->__toString().'/tpls/header.tpl',
356             $this->_themeObject->getTemplate('header.tpl'));
357     }
358     
359     public function testGetTemplateDefaultCustom()
360     {
361         create_custom_directory('themes/default/tpls/');
362         sugar_touch('custom/themes/default/tpls/SomeDefaultTemplate.tpl');
363         
364         $this->assertEquals('custom/themes/default/tpls/SomeDefaultTemplate.tpl',
365             $this->_themeObject->getTemplate('SomeDefaultTemplate.tpl'));
366         
367         unlink('custom/themes/default/tpls/SomeDefaultTemplate.tpl');
368     }
369     
370     public function testGetTemplateWithParentTheme()
371     {
372         $this->assertEquals('themes/'.$this->_themeObject->__toString().'/tpls/header.tpl',
373             $this->_themeObjectChild->getTemplate('header.tpl'));
374     }
375     
376     public function testGetTemplateNotFound()
377     {
378         $this->assertFalse($this->_themeObject->getTemplate('NoTemplateWithThisName.tpl'));
379     }
380     
381     public function testGetAllImages()
382     {
383         $images = $this->_themeObject->getAllImages();
384         
385         $this->assertEquals(
386             $this->_themeObject->getImageURL('Emails.gif',false),
387             $images['Emails.gif']);
388     }
389     
390     public function testGetAllImagesWhenImageIsInParentTheme()
391     {
392         $images = $this->_themeObjectChild->getAllImages();
393         
394         $this->assertEquals(
395             $this->_themeObjectChild->getImageURL('Accounts.gif',false),
396             $images['Accounts.gif']);
397         
398         $this->assertContains(
399             $this->_themeObject->getImagePath(),
400             $images['Accounts.gif']);
401     }
402     
403     public function testGetImageSpecifyingWidthAndHeightAndOtherAttributes()
404     {
405         $this->assertEquals(
406             $this->_themeObject->getImage('Emails','alt="foo"',20,30),
407             "<img src=\"". $this->_themeObject->getImageURL('Emails.gif') ."\" width=\"20\" height=\"30\" alt=\"foo\" />"
408             );
409         
410         // check again to see if caching of the image size works as expected
411         $this->assertEquals(
412             $this->_themeObject->getImage('Emails','alt="foo"',30,40),
413             "<img src=\"". $this->_themeObject->getImageURL('Emails.gif') ."\" width=\"20\" height=\"30\" alt=\"foo\" />"
414             );
415     }
416     
417     public function testGetImageDetectingImageHeightAndWidth()
418     {
419         $size = getimagesize($this->_themeObject->getImageURL('Contacts.gif',false));
420         
421         $this->assertEquals(
422             $this->_themeObject->getImage('Contacts'),
423             "<img src=\"". $this->_themeObject->getImageURL('Contacts.gif') ."\" width=\"{$size[0]}\" height=\"{$size[1]}\"  />"
424             );
425     }
426     
427     public function testGetImageWithInvalidImage()
428     {
429         $this->assertFalse($this->_themeObject->getImage('ThisImageDoesNotExist'));
430     }
431 }