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
44 private $_themeObject;
45 private $_themeDefChild;
46 private $_themeObjectChild;
47 private $_olddeveloperMode;
49 public function setup()
52 include('themes/'.SugarTestThemeUtilities::createAnonymousTheme().'/themedef.php');
54 $this->_themeDef = $themedef;
55 SugarThemeRegistry::add($this->_themeDef);
56 $this->_themeObject = SugarThemeRegistry::get($this->_themeDef['dirName']);
59 include('themes/'.SugarTestThemeUtilities::createAnonymousChildTheme($this->_themeObject->__toString()).'/themedef.php');
61 $this->_themeDefChild = $themedef;
62 SugarThemeRegistry::add($this->_themeDefChild);
63 $this->_themeObjectChild = SugarThemeRegistry::get($this->_themeDefChild['dirName']);
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;
71 public function testMagicIssetWorks()
73 $this->assertTrue(isset($this->_themeObject->dirName));
76 public function tearDown()
78 $themesToRemove = array($this->_themeObject->__toString(),$this->_themeObjectChild->__toString());
80 SugarTestThemeUtilities::removeAllCreatedAnonymousThemes();
82 if ( $this->_olddeveloperMode )
83 $GLOBALS['sugar_config']['developerMode'] = $this->_olddeveloperMode;
85 unset($GLOBALS['sugar_config']['developerMode']);
88 public function testCaching()
90 $this->_themeObject->getCSSURL("style.css");
91 $themename = $this->_themeObject->__toString();
92 $pathname = "cache/themes/{$themename}/css/style.css";
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);
99 $this->_themeObject->__destruct();
100 unset($this->_themeObject);
102 // now recreate object
103 SugarThemeRegistry::add($this->_themeDef);
104 $this->_themeObject = SugarThemeRegistry::get($this->_themeDef['dirName']);
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);
110 // now, let's tell the theme we want to clear the cache on destroy
111 $this->_themeObject->clearCache();
114 $this->_themeObject->__destruct();
115 unset($this->_themeObject);
117 // now recreate object
118 SugarThemeRegistry::add($this->_themeDef);
119 $this->_themeObject = SugarThemeRegistry::get($this->_themeDef['dirName']);
121 // should not be in local cache
122 $this->assertFalse(isset($this->_themeObject->_cssCache['style.css']));
125 public function testCreateInstance()
127 foreach ( $this->_themeDef as $key => $value )
128 $this->assertEquals($this->_themeObject->$key,$value);
131 public function testGetFilePath()
133 $this->assertEquals($this->_themeObject->getFilePath(),
134 'themes/'.$this->_themeDef['name']);
137 public function testGetImagePath()
139 $this->assertEquals($this->_themeObject->getImagePath(),
140 'themes/'.$this->_themeDef['name'].'/images');
143 public function testGetCSSPath()
145 $this->assertEquals($this->_themeObject->getCSSPath(),
146 'themes/'.$this->_themeDef['name'].'/css');
149 public function testGetCSS()
152 preg_match_all('/href="([^"]+)"/',$this->_themeObject->getCSS(),$matches);
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++]);
159 $output = file_get_contents('cache/themes/'.$this->_themeObject->__toString().'/css/style.css');
160 $this->assertRegExp('/h2\{display:inline\}/',$output);
163 public function testGetCSSWithParams()
166 preg_match_all('/href="([^"]+)"/',$this->_themeObject->getCSS('blue','small'),$matches);
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++]);
173 $output = file_get_contents('cache/themes/'.$this->_themeObject->__toString().'/css/style.css');
174 $this->assertRegExp('/h2\{display:inline\}/',$output);
177 public function testGetCSSWithCustomStyleCSS()
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; }');
183 preg_match_all('/href="([^"]+)"/',$this->_themeObject->getCSS(),$matches);
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++]);
190 $output = file_get_contents('cache/themes/'.$this->_themeObject->__toString().'/css/style.css');
191 $this->assertRegExp('/h2\{display:inline\}h3\{color:red\}/',$output);
194 public function testGetCSSWithParentTheme()
197 preg_match_all('/href="([^"]+)"/',$this->_themeObjectChild->getCSS(),$matches);
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++]);
204 $output = file_get_contents('cache/themes/'.$this->_themeObjectChild->__toString().'/css/style.css');
205 $this->assertRegExp('/h2\{display:inline\}h3\{display:inline\}/',$output);
208 public function testGetCSSURLWithInvalidFileSpecifed()
210 $this->assertFalse($this->_themeObject->getCSSURL('ThisFileDoesNotExist.css'));
213 public function testGetCSSURLAddsJsPathIfSpecified()
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));
223 public function testGetJS()
226 preg_match_all('/src="([^"]+)"/',$this->_themeObject->getJS(),$matches);
229 $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/js\/style-min.js/',$matches[1][$i++]);
231 $output = file_get_contents('cache/themes/'.$this->_themeObject->__toString().'/js/style-min.js');
232 $this->assertRegExp('/var dog="cat";/',$output);
235 public function testGetJSCustom()
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;');
241 preg_match_all('/src="([^"]+)"/',$this->_themeObject->getJS(),$matches);
244 $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/js\/style-min.js/',$matches[1][$i++]);
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);
251 public function testGetJSWithParentTheme()
254 preg_match_all('/src="([^"]+)"/',$this->_themeObjectChild->getJS(),$matches);
257 $this->assertRegExp('/themes\/'.$this->_themeObjectChild->__toString().'\/js\/style-min.js/',$matches[1][$i++]);
259 $output = file_get_contents('cache/themes/'.$this->_themeObjectChild->__toString().'/js/style-min.js');
260 $this->assertRegExp('/var dog="cat";var bird="frog";/',$output);
263 public function testGetJSURLWithInvalidFileSpecifed()
265 $this->assertFalse($this->_themeObject->getJSURL('ThisFileDoesNotExist.js'));
268 public function testGetJSURLAddsJsPathIfSpecified()
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));
278 public function testGetImageURL()
280 $this->assertEquals('themes/'.$this->_themeObject->__toString().'/images/Accounts.gif',
281 $this->_themeObject->getImageURL('Accounts.gif',false));
284 public function testGetImageURLWithInvalidFileSpecifed()
286 $this->assertFalse($this->_themeObject->getImageURL('ThisFileDoesNotExist.gif'));
289 public function testGetImageURLCustom()
291 create_custom_directory('themes/'.$this->_themeObject->__toString().'/images/');
292 sugar_touch('custom/themes/'.$this->_themeObject->__toString().'/images/Accounts.gif');
294 $this->assertEquals('custom/themes/'.$this->_themeObject->__toString().'/images/Accounts.gif',
295 $this->_themeObject->getImageURL('Accounts.gif',false));
298 public function testGetImageURLCustomDifferentExtension()
300 create_custom_directory('themes/'.$this->_themeObject->__toString().'/images/');
301 sugar_touch('custom/themes/'.$this->_themeObject->__toString().'/images/Accounts.png');
303 $this->assertEquals('custom/themes/'.$this->_themeObject->__toString().'/images/Accounts.png',
304 $this->_themeObject->getImageURL('Accounts.gif',false));
307 public function testGetImageURLDefault()
309 $this->assertEquals('themes/default/images/Emails.gif',$this->_themeObject->getImageURL('Emails.gif',false));
312 public function testGetImageURLDefaultCustom()
314 create_custom_directory('themes/default/images/');
315 sugar_touch('custom/themes/default/images/Emails.gif');
317 $this->assertEquals('custom/themes/default/images/Emails.gif',
318 $this->_themeObject->getImageURL('Emails.gif',false));
320 unlink('custom/themes/default/images/Emails.gif');
323 public function testGetImageURLNotFound()
325 $this->assertEquals('',$this->_themeObject->getImageURL('NoImageByThisName.gif',false));
328 public function testGetImageURLAddsJsPathIfSpecified()
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));
338 public function testGetImageURLWithParentTheme()
340 $this->assertEquals('themes/'.$this->_themeObject->__toString().'/images/Accounts.gif',
341 $this->_themeObjectChild->getImageURL('Accounts.gif',false));
344 public function testGetTemplate()
346 $this->assertEquals('themes/'.$this->_themeObject->__toString().'/tpls/header.tpl',
347 $this->_themeObject->getTemplate('header.tpl'));
350 public function testGetTemplateCustom()
352 create_custom_directory('themes/'.$this->_themeObject->__toString().'/tpls/');
353 sugar_touch('custom/themes/'.$this->_themeObject->__toString().'/tpls/header.tpl');
355 $this->assertEquals('custom/themes/'.$this->_themeObject->__toString().'/tpls/header.tpl',
356 $this->_themeObject->getTemplate('header.tpl'));
359 public function testGetTemplateDefaultCustom()
361 create_custom_directory('themes/default/tpls/');
362 sugar_touch('custom/themes/default/tpls/SomeDefaultTemplate.tpl');
364 $this->assertEquals('custom/themes/default/tpls/SomeDefaultTemplate.tpl',
365 $this->_themeObject->getTemplate('SomeDefaultTemplate.tpl'));
367 unlink('custom/themes/default/tpls/SomeDefaultTemplate.tpl');
370 public function testGetTemplateWithParentTheme()
372 $this->assertEquals('themes/'.$this->_themeObject->__toString().'/tpls/header.tpl',
373 $this->_themeObjectChild->getTemplate('header.tpl'));
376 public function testGetTemplateNotFound()
378 $this->assertFalse($this->_themeObject->getTemplate('NoTemplateWithThisName.tpl'));
381 public function testGetAllImages()
383 $images = $this->_themeObject->getAllImages();
386 $this->_themeObject->getImageURL('Emails.gif',false),
387 $images['Emails.gif']);
390 public function testGetAllImagesWhenImageIsInParentTheme()
392 $images = $this->_themeObjectChild->getAllImages();
395 $this->_themeObjectChild->getImageURL('Accounts.gif',false),
396 $images['Accounts.gif']);
398 $this->assertContains(
399 $this->_themeObject->getImagePath(),
400 $images['Accounts.gif']);
403 public function testGetImageSpecifyingWidthAndHeightAndOtherAttributes()
406 $this->_themeObject->getImage('Emails','alt="foo"',20,30),
407 "<img src=\"". $this->_themeObject->getImageURL('Emails.gif') ."\" width=\"20\" height=\"30\" alt=\"foo\" />"
410 // check again to see if caching of the image size works as expected
412 $this->_themeObject->getImage('Emails','alt="foo"',30,40),
413 "<img src=\"". $this->_themeObject->getImageURL('Emails.gif') ."\" width=\"20\" height=\"30\" alt=\"foo\" />"
417 public function testGetImageDetectingImageHeightAndWidth()
419 $size = getimagesize($this->_themeObject->getImageURL('Contacts.gif',false));
422 $this->_themeObject->getImage('Contacts'),
423 "<img src=\"". $this->_themeObject->getImageURL('Contacts.gif') ."\" width=\"{$size[0]}\" height=\"{$size[1]}\" />"
427 public function testGetImageWithInvalidImage()
429 $this->assertFalse($this->_themeObject->getImage('ThisImageDoesNotExist'));