2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2011 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 ( isset($GLOBALS['sugar_config']['developerMode']) )
70 $this->_olddeveloperMode = $GLOBALS['sugar_config']['developerMode'];
71 $GLOBALS['sugar_config']['developerMode'] = false;
74 public function testMagicIssetWorks()
76 $this->assertTrue(isset($this->_themeObject->dirName));
79 public function tearDown()
81 $themesToRemove = array($this->_themeObject->__toString(),$this->_themeObjectChild->__toString());
83 SugarTestThemeUtilities::removeAllCreatedAnonymousThemes();
85 if ( $this->_olddeveloperMode )
86 $GLOBALS['sugar_config']['developerMode'] = $this->_olddeveloperMode;
88 unset($GLOBALS['sugar_config']['developerMode']);
91 public function testCaching()
93 $this->_themeObject->getCSSURL("style.css");
94 $themename = $this->_themeObject->__toString();
95 $pathname = "themes/{$themename}/css/style.css";
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']);
102 $this->_themeObject->__destruct();
103 unset($this->_themeObject);
105 // now recreate object
106 SugarThemeRegistry::add($this->_themeDef);
107 $this->_themeObject = SugarThemeRegistry::get($this->_themeDef['dirName']);
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']);
113 // now, let's tell the theme we want to clear the cache on destroy
114 $this->_themeObject->clearCache();
117 $this->_themeObject->__destruct();
118 unset($this->_themeObject);
120 // now recreate object
121 SugarThemeRegistry::add($this->_themeDef);
122 $this->_themeObject = SugarThemeRegistry::get($this->_themeDef['dirName']);
124 // should not be in local cache
125 $this->assertFalse(isset($this->_themeObject->_cssCache['style.css']));
128 public function testCreateInstance()
130 foreach ( $this->_themeDef as $key => $value )
131 $this->assertEquals($this->_themeObject->$key,$value);
134 public function testGetFilePath()
136 $this->assertEquals($this->_themeObject->getFilePath(),
137 'themes/'.$this->_themeDef['name']);
140 public function testGetImagePath()
142 $this->assertEquals($this->_themeObject->getImagePath(),
143 'themes/'.$this->_themeDef['name'].'/images');
146 public function testGetCSSPath()
148 $this->assertEquals($this->_themeObject->getCSSPath(),
149 'themes/'.$this->_themeDef['name'].'/css');
152 public function testGetCSS()
155 preg_match_all('/href="([^"]+)"/',$this->_themeObject->getCSS(),$matches);
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++]);
162 $output = file_get_contents(sugar_cached('themes/').$this->_themeObject->__toString().'/css/style.css');
163 $this->assertRegExp('/h2\{display:inline\}/',$output);
166 public function testGetCSSWithParams()
169 preg_match_all('/href="([^"]+)"/',$this->_themeObject->getCSS('blue','small'),$matches);
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++]);
176 $output = file_get_contents(sugar_cached('themes/').$this->_themeObject->__toString().'/css/style.css');
177 $this->assertRegExp('/h2\{display:inline\}/',$output);
180 public function testGetCSSWithCustomStyleCSS()
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; }');
186 preg_match_all('/href="([^"]+)"/',$this->_themeObject->getCSS(),$matches);
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++]);
193 $output = file_get_contents(sugar_cached('themes/').$this->_themeObject->__toString().'/css/style.css');
194 $this->assertRegExp('/h2\{display:inline\}h3\{color:red\}/',$output);
197 public function testGetCSSWithParentTheme()
200 preg_match_all('/href="([^"]+)"/',$this->_themeObjectChild->getCSS(),$matches);
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++]);
207 $output = file_get_contents(sugar_cached('themes/').$this->_themeObjectChild->__toString().'/css/style.css');
208 $this->assertRegExp('/h2\{display:inline\}h3\{display:inline\}/',$output);
211 public function testGetCSSURLWithInvalidFileSpecifed()
213 $this->assertFalse($this->_themeObject->getCSSURL('ThisFileDoesNotExist.css'));
216 public function testGetCSSURLAddsJsPathIfSpecified()
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));
226 public function testGetJS()
229 preg_match_all('/src="([^"]+)"/',$this->_themeObject->getJS(),$matches);
232 $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/js\/style-min.js/',$matches[1][$i++]);
234 $output = file_get_contents(sugar_cached('themes/').$this->_themeObject->__toString().'/js/style-min.js');
235 $this->assertRegExp('/var dog="cat";/',$output);
238 public function testGetJSCustom()
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;');
244 preg_match_all('/src="([^"]+)"/',$this->_themeObject->getJS(),$matches);
247 $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/js\/style-min.js/',$matches[1][$i++]);
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);
254 public function testGetJSWithParentTheme()
257 preg_match_all('/src="([^"]+)"/',$this->_themeObjectChild->getJS(),$matches);
260 $this->assertRegExp('/themes\/'.$this->_themeObjectChild->__toString().'\/js\/style-min.js/',$matches[1][$i++]);
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);
266 public function testGetJSURLWithInvalidFileSpecifed()
268 $this->assertFalse($this->_themeObject->getJSURL('ThisFileDoesNotExist.js'));
271 public function testGetJSURLAddsJsPathIfSpecified()
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));
281 public function testGetImageURL()
283 $this->assertEquals('themes/'.$this->_themeObject->__toString().'/images/Accounts.gif',
284 $this->_themeObject->getImageURL('Accounts.gif',false));
287 public function testGetImageURLWithInvalidFileSpecifed()
289 $this->assertFalse($this->_themeObject->getImageURL('ThisFileDoesNotExist.gif'));
292 public function testGetImageURLCustom()
294 create_custom_directory('themes/'.$this->_themeObject->__toString().'/images/');
295 sugar_touch('custom/themes/'.$this->_themeObject->__toString().'/images/Accounts.gif');
297 $this->assertEquals('custom/themes/'.$this->_themeObject->__toString().'/images/Accounts.gif',
298 $this->_themeObject->getImageURL('Accounts.gif',false));
301 public function testGetImageURLCustomDifferentExtension()
303 create_custom_directory('themes/'.$this->_themeObject->__toString().'/images/');
304 sugar_touch('custom/themes/'.$this->_themeObject->__toString().'/images/Accounts.png');
306 $this->assertEquals('custom/themes/'.$this->_themeObject->__toString().'/images/Accounts.png',
307 $this->_themeObject->getImageURL('Accounts.gif',false));
310 public function testGetImageURLDefault()
312 $this->assertEquals('themes/default/images/Emails.gif',$this->_themeObject->getImageURL('Emails.gif',false));
315 public function testGetImageURLDefaultCustom()
317 create_custom_directory('themes/default/images/');
318 sugar_touch('custom/themes/default/images/Emails.gif');
320 $this->assertEquals('custom/themes/default/images/Emails.gif',
321 $this->_themeObject->getImageURL('Emails.gif',false));
323 unlink('custom/themes/default/images/Emails.gif');
326 public function testGetImageURLNotFound()
328 $this->assertEquals('',$this->_themeObject->getImageURL('NoImageByThisName.gif',false));
331 public function testGetImageURLAddsJsPathIfSpecified()
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));
341 public function testGetImageURLWithParentTheme()
343 $this->assertEquals('themes/'.$this->_themeObject->__toString().'/images/Accounts.gif',
344 $this->_themeObjectChild->getImageURL('Accounts.gif',false));
347 public function testGetTemplate()
349 $this->assertEquals('themes/'.$this->_themeObject->__toString().'/tpls/header.tpl',
350 $this->_themeObject->getTemplate('header.tpl'));
353 public function testGetTemplateCustom()
355 create_custom_directory('themes/'.$this->_themeObject->__toString().'/tpls/');
356 sugar_touch('custom/themes/'.$this->_themeObject->__toString().'/tpls/header.tpl');
358 $this->assertEquals('custom/themes/'.$this->_themeObject->__toString().'/tpls/header.tpl',
359 $this->_themeObject->getTemplate('header.tpl'));
362 public function testGetTemplateDefaultCustom()
364 create_custom_directory('themes/default/tpls/');
365 sugar_touch('custom/themes/default/tpls/SomeDefaultTemplate.tpl');
367 $this->assertEquals('custom/themes/default/tpls/SomeDefaultTemplate.tpl',
368 $this->_themeObject->getTemplate('SomeDefaultTemplate.tpl'));
370 unlink('custom/themes/default/tpls/SomeDefaultTemplate.tpl');
373 public function testGetTemplateWithParentTheme()
375 $this->assertEquals('themes/'.$this->_themeObject->__toString().'/tpls/header.tpl',
376 $this->_themeObjectChild->getTemplate('header.tpl'));
379 public function testGetTemplateNotFound()
381 $this->assertFalse($this->_themeObject->getTemplate('NoTemplateWithThisName.tpl'));
384 public function testGetAllImages()
386 $images = $this->_themeObject->getAllImages();
389 $this->_themeObject->getImageURL('Emails.gif',false),
390 $images['Emails.gif']);
393 public function testGetAllImagesWhenImageIsInParentTheme()
395 $images = $this->_themeObjectChild->getAllImages();
398 $this->_themeObjectChild->getImageURL('Accounts.gif',false),
399 $images['Accounts.gif']);
401 $this->assertContains(
402 $this->_themeObject->getImagePath(),
403 $images['Accounts.gif']);
406 public function testGetImageSpecifyingWidthAndHeightAndOtherAttributes()
409 $this->_themeObject->getImage('Emails','',20,30,'.gif',"Emails"),
410 "<img src=\"". $this->_themeObject->getImageURL('Emails.gif') ."\" width=\"20\" height=\"30\" alt=\"Emails\" />"
413 // check again to see if caching of the image size works as expected
415 $this->_themeObject->getImage('Emails','',30,40,'.gif',"Emails"),
416 "<img src=\"". $this->_themeObject->getImageURL('Emails.gif') ."\" width=\"30\" height=\"40\" alt=\"Emails\" />"
420 public function testGetImageDetectingImageHeightAndWidth()
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"));
429 public function testGetImageWithInvalidImage()
431 $this->assertFalse($this->_themeObject->getImage('ThisImageDoesNotExist'));