2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
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.
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
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
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.
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.
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 ********************************************************************************/
38 require_once 'include/SugarTheme/SugarTheme.php';
39 require_once 'include/dir_inc.php';
41 class SugarThemeTest extends Sugar_PHPUnit_Framework_TestCase
47 private $_themeObject;
48 private $_themeDefChild;
49 private $_themeObjectChild;
50 private $_olddeveloperMode;
52 public function setup()
55 include('themes/'.SugarTestThemeUtilities::createAnonymousTheme().'/themedef.php');
57 $this->_themeDef = $themedef;
58 SugarThemeRegistry::add($this->_themeDef);
59 $this->_themeObject = SugarThemeRegistry::get($this->_themeDef['dirName']);
62 include('themes/'.SugarTestThemeUtilities::createAnonymousChildTheme($this->_themeObject->__toString()).'/themedef.php');
64 $this->_themeDefChild = $themedef;
65 SugarThemeRegistry::add($this->_themeDefChild);
66 $this->_themeObjectChild = SugarThemeRegistry::get($this->_themeDefChild['dirName']);
68 // test assumes developerMode is off, so css minifying happens
69 if (inDeveloperMode())
71 $this->_olddeveloperMode = $GLOBALS['sugar_config']['developerMode'];
73 $GLOBALS['sugar_config']['developerMode'] = false;
76 public function testMagicIssetWorks()
78 $this->assertTrue(isset($this->_themeObject->dirName));
81 public function tearDown()
83 $themesToRemove = array($this->_themeObject->__toString(),$this->_themeObjectChild->__toString());
85 SugarTestThemeUtilities::removeAllCreatedAnonymousThemes();
87 if ( $this->_olddeveloperMode )
88 $GLOBALS['sugar_config']['developerMode'] = $this->_olddeveloperMode;
90 unset($GLOBALS['sugar_config']['developerMode']);
93 public function testCaching()
95 $this->_themeObject->getCSSURL("style.css");
96 $themename = $this->_themeObject->__toString();
97 $pathname = "themes/{$themename}/css/style.css";
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']);
104 $this->_themeObject->__destruct();
105 unset($this->_themeObject);
107 // now recreate object
108 SugarThemeRegistry::add($this->_themeDef);
109 $this->_themeObject = SugarThemeRegistry::get($this->_themeDef['dirName']);
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']);
115 // now, let's tell the theme we want to clear the cache on destroy
116 $this->_themeObject->clearCache();
119 $this->_themeObject->__destruct();
120 unset($this->_themeObject);
122 // now recreate object
123 SugarThemeRegistry::add($this->_themeDef);
124 $this->_themeObject = SugarThemeRegistry::get($this->_themeDef['dirName']);
126 // should not be in local cache
127 $this->assertFalse(isset($this->_themeObject->_cssCache['style.css']));
130 public function testCreateInstance()
132 foreach ( $this->_themeDef as $key => $value )
133 $this->assertEquals($this->_themeObject->$key,$value);
136 public function testGetFilePath()
138 $this->assertEquals($this->_themeObject->getFilePath(),
139 'themes/'.$this->_themeDef['name']);
142 public function testGetImagePath()
144 $this->assertEquals($this->_themeObject->getImagePath(),
145 'themes/'.$this->_themeDef['name'].'/images');
148 public function testGetCSSPath()
150 $this->assertEquals($this->_themeObject->getCSSPath(),
151 'themes/'.$this->_themeDef['name'].'/css');
154 public function testGetCSS()
157 preg_match_all('/href="([^"]+)"/',$this->_themeObject->getCSS(),$matches);
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++]);
164 $output = file_get_contents(sugar_cached('themes/').$this->_themeObject->__toString().'/css/style.css');
165 $this->assertRegExp('/h2\{display:inline\}/',$output);
168 public function testGetCSSWithParams()
171 preg_match_all('/href="([^"]+)"/',$this->_themeObject->getCSS('blue','small'),$matches);
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++]);
178 $output = file_get_contents(sugar_cached('themes/').$this->_themeObject->__toString().'/css/style.css');
179 $this->assertRegExp('/h2\{display:inline\}/',$output);
182 public function testGetCSSWithCustomStyleCSS()
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; }');
188 preg_match_all('/href="([^"]+)"/',$this->_themeObject->getCSS(),$matches);
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++]);
195 $output = file_get_contents(sugar_cached('themes/').$this->_themeObject->__toString().'/css/style.css');
196 $this->assertRegExp('/h2\{display:inline\}h3\{color:red\}/',$output);
199 public function testGetCSSWithParentTheme()
202 preg_match_all('/href="([^"]+)"/',$this->_themeObjectChild->getCSS(),$matches);
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++]);
209 $output = file_get_contents(sugar_cached('themes/').$this->_themeObjectChild->__toString().'/css/style.css');
210 $this->assertRegExp('/h2\{display:inline\}h3\{display:inline\}/',$output);
213 public function testGetCSSURLWithInvalidFileSpecifed()
215 $this->assertFalse($this->_themeObject->getCSSURL('ThisFileDoesNotExist.css'));
218 public function testGetCSSURLAddsJsPathIfSpecified()
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));
228 public function testGetJS()
231 preg_match_all('/src="([^"]+)"/',$this->_themeObject->getJS(),$matches);
234 $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/js\/style-min.js/',$matches[1][$i++]);
236 $output = file_get_contents(sugar_cached('themes/').$this->_themeObject->__toString().'/js/style-min.js');
237 $this->assertRegExp('/var dog="cat";/',$output);
240 public function testGetJSCustom()
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;');
246 preg_match_all('/src="([^"]+)"/',$this->_themeObject->getJS(),$matches);
249 $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/js\/style-min.js/',$matches[1][$i++]);
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);
256 public function testGetJSWithParentTheme()
259 preg_match_all('/src="([^"]+)"/',$this->_themeObjectChild->getJS(),$matches);
262 $this->assertRegExp('/themes\/'.$this->_themeObjectChild->__toString().'\/js\/style-min.js/',$matches[1][$i++]);
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);
268 public function testGetJSURLWithInvalidFileSpecifed()
270 $this->assertFalse($this->_themeObject->getJSURL('ThisFileDoesNotExist.js'));
273 public function testGetJSURLAddsJsPathIfSpecified()
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));
283 public function testGetImageURL()
285 $this->assertEquals('themes/'.$this->_themeObject->__toString().'/images/Accounts.gif',
286 $this->_themeObject->getImageURL('Accounts.gif',false));
289 public function testGetImageURLWithInvalidFileSpecifed()
291 $this->assertFalse($this->_themeObject->getImageURL('ThisFileDoesNotExist.gif'));
294 public function testGetImageURLCustom()
296 create_custom_directory('themes/'.$this->_themeObject->__toString().'/images/');
297 sugar_touch('custom/themes/'.$this->_themeObject->__toString().'/images/Accounts.gif');
299 $this->assertEquals('custom/themes/'.$this->_themeObject->__toString().'/images/Accounts.gif',
300 $this->_themeObject->getImageURL('Accounts.gif',false));
303 public function testGetImageURLCustomDifferentExtension()
305 create_custom_directory('themes/'.$this->_themeObject->__toString().'/images/');
306 sugar_touch('custom/themes/'.$this->_themeObject->__toString().'/images/Accounts.png');
308 $this->assertEquals('custom/themes/'.$this->_themeObject->__toString().'/images/Accounts.png',
309 $this->_themeObject->getImageURL('Accounts.gif',false));
312 public function testGetImageURLDefault()
314 $this->assertEquals('themes/default/images/Emails.gif',$this->_themeObject->getImageURL('Emails.gif',false));
317 public function testGetImageURLDefaultCustom()
319 create_custom_directory('themes/default/images/');
320 sugar_touch('custom/themes/default/images/Emails.gif');
322 $this->assertEquals('custom/themes/default/images/Emails.gif',
323 $this->_themeObject->getImageURL('Emails.gif',false));
325 unlink('custom/themes/default/images/Emails.gif');
328 public function testGetImageURLNotFound()
330 $this->assertEquals('',$this->_themeObject->getImageURL('NoImageByThisName.gif',false));
333 public function testGetImageURLAddsJsPathIfSpecified()
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));
343 public function testGetImageURLWithParentTheme()
345 $this->assertEquals('themes/'.$this->_themeObject->__toString().'/images/Accounts.gif',
346 $this->_themeObjectChild->getImageURL('Accounts.gif',false));
349 public function testGetTemplate()
351 $this->assertEquals('themes/'.$this->_themeObject->__toString().'/tpls/header.tpl',
352 $this->_themeObject->getTemplate('header.tpl'));
355 public function testGetTemplateCustom()
357 create_custom_directory('themes/'.$this->_themeObject->__toString().'/tpls/');
358 sugar_touch('custom/themes/'.$this->_themeObject->__toString().'/tpls/header.tpl');
360 $this->assertEquals('custom/themes/'.$this->_themeObject->__toString().'/tpls/header.tpl',
361 $this->_themeObject->getTemplate('header.tpl'));
364 public function testGetTemplateDefaultCustom()
366 create_custom_directory('themes/default/tpls/');
367 sugar_touch('custom/themes/default/tpls/SomeDefaultTemplate.tpl');
369 $this->assertEquals('custom/themes/default/tpls/SomeDefaultTemplate.tpl',
370 $this->_themeObject->getTemplate('SomeDefaultTemplate.tpl'));
372 unlink('custom/themes/default/tpls/SomeDefaultTemplate.tpl');
375 public function testGetTemplateWithParentTheme()
377 $this->assertEquals('themes/'.$this->_themeObject->__toString().'/tpls/header.tpl',
378 $this->_themeObjectChild->getTemplate('header.tpl'));
381 public function testGetTemplateNotFound()
383 $this->assertFalse($this->_themeObject->getTemplate('NoTemplateWithThisName.tpl'));
386 public function testGetAllImages()
388 $images = $this->_themeObject->getAllImages();
391 $this->_themeObject->getImageURL('Emails.gif',false),
392 $images['Emails.gif']);
395 public function testGetAllImagesWhenImageIsInParentTheme()
397 $images = $this->_themeObjectChild->getAllImages();
400 $this->_themeObjectChild->getImageURL('Accounts.gif',false),
401 $images['Accounts.gif']);
403 $this->assertContains(
404 $this->_themeObject->getImagePath(),
405 $images['Accounts.gif']);
408 public function testGetImageSpecifyingWidthAndHeightAndOtherAttributes()
411 $this->_themeObject->getImage('Emails','',20,30,'.gif',"Emails"),
412 "<img src=\"". $this->_themeObject->getImageURL('Emails.gif') ."\" width=\"20\" height=\"30\" alt=\"Emails\" />"
415 // check again to see if caching of the image size works as expected
417 $this->_themeObject->getImage('Emails','',30,40,'.gif',"Emails"),
418 "<img src=\"". $this->_themeObject->getImageURL('Emails.gif') ."\" width=\"30\" height=\"40\" alt=\"Emails\" />"
422 public function testGetImageDetectingImageHeightAndWidth()
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"));
431 public function testGetImageWithInvalidImage()
433 $this->assertFalse($this->_themeObject->getImage('ThisImageDoesNotExist'));