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