2 require_once 'include/SugarTheme/SugarTheme.php';
3 require_once 'include/dir_inc.php';
5 class SugarThemeTest extends Sugar_PHPUnit_Framework_TestCase
9 private $_themeDefChild;
10 private $_themeObjectChild;
11 private $_olddeveloperMode;
13 public function setup()
16 include('themes/'.SugarTestThemeUtilities::createAnonymousTheme().'/themedef.php');
18 $this->_themeDef = $themedef;
19 SugarThemeRegistry::add($this->_themeDef);
20 $this->_themeObject = SugarThemeRegistry::get($this->_themeDef['dirName']);
23 include('themes/'.SugarTestThemeUtilities::createAnonymousChildTheme($this->_themeObject->__toString()).'/themedef.php');
25 $this->_themeDefChild = $themedef;
26 SugarThemeRegistry::add($this->_themeDefChild);
27 $this->_themeObjectChild = SugarThemeRegistry::get($this->_themeDefChild['dirName']);
29 // test assumes developerMode is off, so css minifying happens
30 if ( isset($GLOBALS['sugar_config']['developerMode']) )
31 $this->_olddeveloperMode = $GLOBALS['sugar_config']['developerMode'];
32 $GLOBALS['sugar_config']['developerMode'] = false;
35 public function testMagicIssetWorks()
37 $this->assertTrue(isset($this->_themeObject->dirName));
40 public function tearDown()
42 $themesToRemove = array($this->_themeObject->__toString(),$this->_themeObjectChild->__toString());
44 SugarTestThemeUtilities::removeAllCreatedAnonymousThemes();
46 if ( $this->_olddeveloperMode )
47 $GLOBALS['sugar_config']['developerMode'] = $this->_olddeveloperMode;
49 unset($GLOBALS['sugar_config']['developerMode']);
52 public function testCaching()
54 $this->_themeObject->getCSSURL("style.css");
55 $themename = $this->_themeObject->__toString();
56 $pathname = "cache/themes/{$themename}/css/style.css";
58 // test if it's in the local cache
59 $this->assertTrue(isset($this->_themeObject->_cssCache['style.css']));
60 $this->assertEquals($this->_themeObject->_cssCache['style.css'],$pathname);
63 $this->_themeObject->__destruct();
64 unset($this->_themeObject);
66 // now recreate object
67 SugarThemeRegistry::add($this->_themeDef);
68 $this->_themeObject = SugarThemeRegistry::get($this->_themeDef['dirName']);
70 // should still be in local cache
71 $this->assertTrue(isset($this->_themeObject->_cssCache['style.css']));
72 $this->assertEquals($this->_themeObject->_cssCache['style.css'],$pathname);
74 // now, let's tell the theme we want to clear the cache on destroy
75 $this->_themeObject->clearCache();
78 $this->_themeObject->__destruct();
79 unset($this->_themeObject);
81 // now recreate object
82 SugarThemeRegistry::add($this->_themeDef);
83 $this->_themeObject = SugarThemeRegistry::get($this->_themeDef['dirName']);
85 // should not be in local cache
86 $this->assertFalse(isset($this->_themeObject->_cssCache['style.css']));
89 public function testCreateInstance()
91 foreach ( $this->_themeDef as $key => $value )
92 $this->assertEquals($this->_themeObject->$key,$value);
95 public function testGetFilePath()
97 $this->assertEquals($this->_themeObject->getFilePath(),
98 'themes/'.$this->_themeDef['name']);
101 public function testGetImagePath()
103 $this->assertEquals($this->_themeObject->getImagePath(),
104 'themes/'.$this->_themeDef['name'].'/images');
107 public function testGetCSSPath()
109 $this->assertEquals($this->_themeObject->getCSSPath(),
110 'themes/'.$this->_themeDef['name'].'/css');
113 public function testGetCSS()
116 preg_match_all('/href="([^"]+)"/',$this->_themeObject->getCSS(),$matches);
119 $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/css\/yui.css/',$matches[1][$i++]);
120 $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/css\/deprecated.css/',$matches[1][$i++]);
121 $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/css\/style.css/',$matches[1][$i++]);
123 $output = file_get_contents('cache/themes/'.$this->_themeObject->__toString().'/css/style.css');
124 $this->assertRegExp('/h2\{display:inline\}/',$output);
127 public function testGetCSSWithParams()
130 preg_match_all('/href="([^"]+)"/',$this->_themeObject->getCSS('blue','small'),$matches);
133 $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/css\/yui.css/',$matches[1][$i++]);
134 $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/css\/deprecated.css/',$matches[1][$i++]);
135 $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/css\/style.css/',$matches[1][$i++]);
137 $output = file_get_contents('cache/themes/'.$this->_themeObject->__toString().'/css/style.css');
138 $this->assertRegExp('/h2\{display:inline\}/',$output);
141 public function testGetCSSWithCustomStyleCSS()
143 create_custom_directory('themes/'.$this->_themeObject->__toString().'/css/');
144 sugar_file_put_contents('custom/themes/'.$this->_themeObject->__toString().'/css/style.css','h3 { color: red; }');
147 preg_match_all('/href="([^"]+)"/',$this->_themeObject->getCSS(),$matches);
150 $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/css\/yui.css/',$matches[1][$i++]);
151 $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/css\/deprecated.css/',$matches[1][$i++]);
152 $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/css\/style.css/',$matches[1][$i++]);
154 $output = file_get_contents('cache/themes/'.$this->_themeObject->__toString().'/css/style.css');
155 $this->assertRegExp('/h2\{display:inline\}h3\{color:red\}/',$output);
158 public function testGetCSSWithParentTheme()
161 preg_match_all('/href="([^"]+)"/',$this->_themeObjectChild->getCSS(),$matches);
164 $this->assertRegExp('/themes\/'.$this->_themeObjectChild->__toString().'\/css\/yui.css/',$matches[1][$i++]);
165 $this->assertRegExp('/themes\/'.$this->_themeObjectChild->__toString().'\/css\/deprecated.css/',$matches[1][$i++]);
166 $this->assertRegExp('/themes\/'.$this->_themeObjectChild->__toString().'\/css\/style.css/',$matches[1][$i++]);
168 $output = file_get_contents('cache/themes/'.$this->_themeObjectChild->__toString().'/css/style.css');
169 $this->assertRegExp('/h2\{display:inline\}h3\{display:inline\}/',$output);
172 public function testGetCSSURLWithInvalidFileSpecifed()
174 $this->assertFalse($this->_themeObject->getCSSURL('ThisFileDoesNotExist.css'));
177 public function testGetCSSURLAddsJsPathIfSpecified()
179 // check one may not hit cache
180 $this->assertRegExp('/style\.css\?/',$this->_themeObject->getCSSURL('style.css'));
181 // check two definitely should hit cache
182 $this->assertRegExp('/style\.css\?/',$this->_themeObject->getCSSURL('style.css'));
183 // check three for the jspath not being added
184 $this->assertNotContains('?',$this->_themeObject->getCSSURL('style.css',false));
187 public function testGetJS()
190 preg_match_all('/src="([^"]+)"/',$this->_themeObject->getJS(),$matches);
193 $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/js\/style-min.js/',$matches[1][$i++]);
195 $output = file_get_contents('cache/themes/'.$this->_themeObject->__toString().'/js/style-min.js');
196 $this->assertRegExp('/var dog="cat";/',$output);
199 public function testGetJSCustom()
201 create_custom_directory('themes/'.$this->_themeObject->__toString().'/js/');
202 sugar_file_put_contents('custom/themes/'.$this->_themeObject->__toString().'/js/style.js','var x = 1;');
205 preg_match_all('/src="([^"]+)"/',$this->_themeObject->getJS(),$matches);
208 $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/js\/style-min.js/',$matches[1][$i++]);
210 $output = file_get_contents('cache/themes/'.$this->_themeObject->__toString().'/js/style-min.js');
211 $this->assertRegExp('/var dog="cat";/',$output);
212 $this->assertRegExp('/var x=1;/',$output);
215 public function testGetJSWithParentTheme()
218 preg_match_all('/src="([^"]+)"/',$this->_themeObjectChild->getJS(),$matches);
221 $this->assertRegExp('/themes\/'.$this->_themeObjectChild->__toString().'\/js\/style-min.js/',$matches[1][$i++]);
223 $output = file_get_contents('cache/themes/'.$this->_themeObjectChild->__toString().'/js/style-min.js');
224 $this->assertRegExp('/var dog="cat";var bird="frog";/',$output);
227 public function testGetJSURLWithInvalidFileSpecifed()
229 $this->assertFalse($this->_themeObject->getJSURL('ThisFileDoesNotExist.js'));
232 public function testGetJSURLAddsJsPathIfSpecified()
234 // check one may not hit cache
235 $this->assertRegExp('/style-min\.js\?/',$this->_themeObject->getJSURL('style.js'));
236 // check two definitely should hit cache
237 $this->assertRegExp('/style-min\.js\?/',$this->_themeObject->getJSURL('style.js'));
238 // check three for the jspath not being added
239 $this->assertNotContains('?',$this->_themeObject->getJSURL('style.js',false));
242 public function testGetImageURL()
244 $this->assertEquals('themes/'.$this->_themeObject->__toString().'/images/Accounts.gif',
245 $this->_themeObject->getImageURL('Accounts.gif',false));
248 public function testGetImageURLWithInvalidFileSpecifed()
250 $this->assertFalse($this->_themeObject->getImageURL('ThisFileDoesNotExist.gif'));
253 public function testGetImageURLCustom()
255 create_custom_directory('themes/'.$this->_themeObject->__toString().'/images/');
256 sugar_touch('custom/themes/'.$this->_themeObject->__toString().'/images/Accounts.gif');
258 $this->assertEquals('custom/themes/'.$this->_themeObject->__toString().'/images/Accounts.gif',
259 $this->_themeObject->getImageURL('Accounts.gif',false));
262 public function testGetImageURLCustomDifferentExtension()
264 create_custom_directory('themes/'.$this->_themeObject->__toString().'/images/');
265 sugar_touch('custom/themes/'.$this->_themeObject->__toString().'/images/Accounts.png');
267 $this->assertEquals('custom/themes/'.$this->_themeObject->__toString().'/images/Accounts.png',
268 $this->_themeObject->getImageURL('Accounts.gif',false));
271 public function testGetImageURLDefault()
273 $this->assertEquals('themes/default/images/Emails.gif',$this->_themeObject->getImageURL('Emails.gif',false));
276 public function testGetImageURLDefaultCustom()
278 create_custom_directory('themes/default/images/');
279 sugar_touch('custom/themes/default/images/Emails.gif');
281 $this->assertEquals('custom/themes/default/images/Emails.gif',
282 $this->_themeObject->getImageURL('Emails.gif',false));
284 unlink('custom/themes/default/images/Emails.gif');
287 public function testGetImageURLNotFound()
289 $this->assertEquals('',$this->_themeObject->getImageURL('NoImageByThisName.gif',false));
292 public function testGetImageURLAddsJsPathIfSpecified()
294 // check one may not hit cache
295 $this->assertRegExp('/Accounts\.gif\?/',$this->_themeObject->getImageURL('Accounts.gif'));
296 // check two definitely should hit cache
297 $this->assertRegExp('/Accounts\.gif\?/',$this->_themeObject->getImageURL('Accounts.gif'));
298 // check three for the jspath not being added
299 $this->assertNotContains('?',$this->_themeObject->getImageURL('Accounts.gif',false));
302 public function testGetImageURLWithParentTheme()
304 $this->assertEquals('themes/'.$this->_themeObject->__toString().'/images/Accounts.gif',
305 $this->_themeObjectChild->getImageURL('Accounts.gif',false));
308 public function testGetTemplate()
310 $this->assertEquals('themes/'.$this->_themeObject->__toString().'/tpls/header.tpl',
311 $this->_themeObject->getTemplate('header.tpl'));
314 public function testGetTemplateCustom()
316 create_custom_directory('themes/'.$this->_themeObject->__toString().'/tpls/');
317 sugar_touch('custom/themes/'.$this->_themeObject->__toString().'/tpls/header.tpl');
319 $this->assertEquals('custom/themes/'.$this->_themeObject->__toString().'/tpls/header.tpl',
320 $this->_themeObject->getTemplate('header.tpl'));
323 public function testGetTemplateDefaultCustom()
325 create_custom_directory('themes/default/tpls/');
326 sugar_touch('custom/themes/default/tpls/SomeDefaultTemplate.tpl');
328 $this->assertEquals('custom/themes/default/tpls/SomeDefaultTemplate.tpl',
329 $this->_themeObject->getTemplate('SomeDefaultTemplate.tpl'));
331 unlink('custom/themes/default/tpls/SomeDefaultTemplate.tpl');
334 public function testGetTemplateWithParentTheme()
336 $this->assertEquals('themes/'.$this->_themeObject->__toString().'/tpls/header.tpl',
337 $this->_themeObjectChild->getTemplate('header.tpl'));
340 public function testGetTemplateNotFound()
342 $this->assertFalse($this->_themeObject->getTemplate('NoTemplateWithThisName.tpl'));
345 public function testGetAllImages()
347 $images = $this->_themeObject->getAllImages();
350 $this->_themeObject->getImageURL('Emails.gif',false),
351 $images['Emails.gif']);
354 public function testGetAllImagesWhenImageIsInParentTheme()
356 $images = $this->_themeObjectChild->getAllImages();
359 $this->_themeObjectChild->getImageURL('Accounts.gif',false),
360 $images['Accounts.gif']);
362 $this->assertContains(
363 $this->_themeObject->getImagePath(),
364 $images['Accounts.gif']);
367 public function testGetImageSpecifyingWidthAndHeightAndOtherAttributes()
370 $this->_themeObject->getImage('Emails','alt="foo"',20,30),
371 "<img src=\"". $this->_themeObject->getImageURL('Emails.gif') ."\" width=\"20\" height=\"30\" alt=\"foo\" />"
374 // check again to see if caching of the image size works as expected
376 $this->_themeObject->getImage('Emails','alt="foo"',30,40),
377 "<img src=\"". $this->_themeObject->getImageURL('Emails.gif') ."\" width=\"20\" height=\"30\" alt=\"foo\" />"
381 public function testGetImageDetectingImageHeightAndWidth()
383 $size = getimagesize($this->_themeObject->getImageURL('Contacts.gif',false));
386 $this->_themeObject->getImage('Contacts'),
387 "<img src=\"". $this->_themeObject->getImageURL('Contacts.gif') ."\" width=\"{$size[0]}\" height=\"{$size[1]}\" />"
391 public function testGetImageWithInvalidImage()
393 $this->assertFalse($this->_themeObject->getImage('ThisImageDoesNotExist'));