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