PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/include/SugarTheme/SugarThemeTest.php

https://github.com/item/sugarcrm_dev
PHP | 395 lines | 295 code | 81 blank | 19 comment | 2 complexity | 89967ed2fd44ed2821bc757cb80b9594 MD5 | raw file
Possible License(s): AGPL-3.0, LGPL-2.1
  1. <?php
  2. require_once 'include/SugarTheme/SugarTheme.php';
  3. require_once 'include/dir_inc.php';
  4. class SugarThemeTest extends Sugar_PHPUnit_Framework_TestCase
  5. {
  6. private $_themeDef;
  7. private $_themeObject;
  8. private $_themeDefChild;
  9. private $_themeObjectChild;
  10. private $_olddeveloperMode;
  11. public function setup()
  12. {
  13. $themedef = array();
  14. include('themes/'.SugarTestThemeUtilities::createAnonymousTheme().'/themedef.php');
  15. $this->_themeDef = $themedef;
  16. SugarThemeRegistry::add($this->_themeDef);
  17. $this->_themeObject = SugarThemeRegistry::get($this->_themeDef['dirName']);
  18. $themedef = array();
  19. include('themes/'.SugarTestThemeUtilities::createAnonymousChildTheme($this->_themeObject->__toString()).'/themedef.php');
  20. $this->_themeDefChild = $themedef;
  21. SugarThemeRegistry::add($this->_themeDefChild);
  22. $this->_themeObjectChild = SugarThemeRegistry::get($this->_themeDefChild['dirName']);
  23. // test assumes developerMode is off, so css minifying happens
  24. if ( isset($GLOBALS['sugar_config']['developerMode']) )
  25. $this->_olddeveloperMode = $GLOBALS['sugar_config']['developerMode'];
  26. $GLOBALS['sugar_config']['developerMode'] = false;
  27. }
  28. public function testMagicIssetWorks()
  29. {
  30. $this->assertTrue(isset($this->_themeObject->dirName));
  31. }
  32. public function tearDown()
  33. {
  34. $themesToRemove = array($this->_themeObject->__toString(),$this->_themeObjectChild->__toString());
  35. SugarTestThemeUtilities::removeAllCreatedAnonymousThemes();
  36. if ( $this->_olddeveloperMode )
  37. $GLOBALS['sugar_config']['developerMode'] = $this->_olddeveloperMode;
  38. else
  39. unset($GLOBALS['sugar_config']['developerMode']);
  40. }
  41. public function testCaching()
  42. {
  43. $this->_themeObject->getCSSURL("style.css");
  44. $themename = $this->_themeObject->__toString();
  45. $pathname = "cache/themes/{$themename}/css/style.css";
  46. // test if it's in the local cache
  47. $this->assertTrue(isset($this->_themeObject->_cssCache['style.css']));
  48. $this->assertEquals($this->_themeObject->_cssCache['style.css'],$pathname);
  49. // destroy object
  50. $this->_themeObject->__destruct();
  51. unset($this->_themeObject);
  52. // now recreate object
  53. SugarThemeRegistry::add($this->_themeDef);
  54. $this->_themeObject = SugarThemeRegistry::get($this->_themeDef['dirName']);
  55. // should still be in local cache
  56. $this->assertTrue(isset($this->_themeObject->_cssCache['style.css']));
  57. $this->assertEquals($this->_themeObject->_cssCache['style.css'],$pathname);
  58. // now, let's tell the theme we want to clear the cache on destroy
  59. $this->_themeObject->clearCache();
  60. // destroy object
  61. $this->_themeObject->__destruct();
  62. unset($this->_themeObject);
  63. // now recreate object
  64. SugarThemeRegistry::add($this->_themeDef);
  65. $this->_themeObject = SugarThemeRegistry::get($this->_themeDef['dirName']);
  66. // should not be in local cache
  67. $this->assertFalse(isset($this->_themeObject->_cssCache['style.css']));
  68. }
  69. public function testCreateInstance()
  70. {
  71. foreach ( $this->_themeDef as $key => $value )
  72. $this->assertEquals($this->_themeObject->$key,$value);
  73. }
  74. public function testGetFilePath()
  75. {
  76. $this->assertEquals($this->_themeObject->getFilePath(),
  77. 'themes/'.$this->_themeDef['name']);
  78. }
  79. public function testGetImagePath()
  80. {
  81. $this->assertEquals($this->_themeObject->getImagePath(),
  82. 'themes/'.$this->_themeDef['name'].'/images');
  83. }
  84. public function testGetCSSPath()
  85. {
  86. $this->assertEquals($this->_themeObject->getCSSPath(),
  87. 'themes/'.$this->_themeDef['name'].'/css');
  88. }
  89. public function testGetCSS()
  90. {
  91. $matches = array();
  92. preg_match_all('/href="([^"]+)"/',$this->_themeObject->getCSS(),$matches);
  93. $i = 0;
  94. $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/css\/yui.css/',$matches[1][$i++]);
  95. $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/css\/deprecated.css/',$matches[1][$i++]);
  96. $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/css\/style.css/',$matches[1][$i++]);
  97. $output = file_get_contents('cache/themes/'.$this->_themeObject->__toString().'/css/style.css');
  98. $this->assertRegExp('/h2\{display:inline\}/',$output);
  99. }
  100. public function testGetCSSWithParams()
  101. {
  102. $matches = array();
  103. preg_match_all('/href="([^"]+)"/',$this->_themeObject->getCSS('blue','small'),$matches);
  104. $i = 0;
  105. $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/css\/yui.css/',$matches[1][$i++]);
  106. $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/css\/deprecated.css/',$matches[1][$i++]);
  107. $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/css\/style.css/',$matches[1][$i++]);
  108. $output = file_get_contents('cache/themes/'.$this->_themeObject->__toString().'/css/style.css');
  109. $this->assertRegExp('/h2\{display:inline\}/',$output);
  110. }
  111. public function testGetCSSWithCustomStyleCSS()
  112. {
  113. create_custom_directory('themes/'.$this->_themeObject->__toString().'/css/');
  114. sugar_file_put_contents('custom/themes/'.$this->_themeObject->__toString().'/css/style.css','h3 { color: red; }');
  115. $matches = array();
  116. preg_match_all('/href="([^"]+)"/',$this->_themeObject->getCSS(),$matches);
  117. $i = 0;
  118. $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/css\/yui.css/',$matches[1][$i++]);
  119. $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/css\/deprecated.css/',$matches[1][$i++]);
  120. $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/css\/style.css/',$matches[1][$i++]);
  121. $output = file_get_contents('cache/themes/'.$this->_themeObject->__toString().'/css/style.css');
  122. $this->assertRegExp('/h2\{display:inline\}h3\{color:red\}/',$output);
  123. }
  124. public function testGetCSSWithParentTheme()
  125. {
  126. $matches = array();
  127. preg_match_all('/href="([^"]+)"/',$this->_themeObjectChild->getCSS(),$matches);
  128. $i = 0;
  129. $this->assertRegExp('/themes\/'.$this->_themeObjectChild->__toString().'\/css\/yui.css/',$matches[1][$i++]);
  130. $this->assertRegExp('/themes\/'.$this->_themeObjectChild->__toString().'\/css\/deprecated.css/',$matches[1][$i++]);
  131. $this->assertRegExp('/themes\/'.$this->_themeObjectChild->__toString().'\/css\/style.css/',$matches[1][$i++]);
  132. $output = file_get_contents('cache/themes/'.$this->_themeObjectChild->__toString().'/css/style.css');
  133. $this->assertRegExp('/h2\{display:inline\}h3\{display:inline\}/',$output);
  134. }
  135. public function testGetCSSURLWithInvalidFileSpecifed()
  136. {
  137. $this->assertFalse($this->_themeObject->getCSSURL('ThisFileDoesNotExist.css'));
  138. }
  139. public function testGetCSSURLAddsJsPathIfSpecified()
  140. {
  141. // check one may not hit cache
  142. $this->assertRegExp('/style\.css\?/',$this->_themeObject->getCSSURL('style.css'));
  143. // check two definitely should hit cache
  144. $this->assertRegExp('/style\.css\?/',$this->_themeObject->getCSSURL('style.css'));
  145. // check three for the jspath not being added
  146. $this->assertNotContains('?',$this->_themeObject->getCSSURL('style.css',false));
  147. }
  148. public function testGetJS()
  149. {
  150. $matches = array();
  151. preg_match_all('/src="([^"]+)"/',$this->_themeObject->getJS(),$matches);
  152. $i = 0;
  153. $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/js\/style-min.js/',$matches[1][$i++]);
  154. $output = file_get_contents('cache/themes/'.$this->_themeObject->__toString().'/js/style-min.js');
  155. $this->assertRegExp('/var dog="cat";/',$output);
  156. }
  157. public function testGetJSCustom()
  158. {
  159. create_custom_directory('themes/'.$this->_themeObject->__toString().'/js/');
  160. sugar_file_put_contents('custom/themes/'.$this->_themeObject->__toString().'/js/style.js','var x = 1;');
  161. $matches = array();
  162. preg_match_all('/src="([^"]+)"/',$this->_themeObject->getJS(),$matches);
  163. $i = 0;
  164. $this->assertRegExp('/themes\/'.$this->_themeObject->__toString().'\/js\/style-min.js/',$matches[1][$i++]);
  165. $output = file_get_contents('cache/themes/'.$this->_themeObject->__toString().'/js/style-min.js');
  166. $this->assertRegExp('/var dog="cat";/',$output);
  167. $this->assertRegExp('/var x=1;/',$output);
  168. }
  169. public function testGetJSWithParentTheme()
  170. {
  171. $matches = array();
  172. preg_match_all('/src="([^"]+)"/',$this->_themeObjectChild->getJS(),$matches);
  173. $i = 0;
  174. $this->assertRegExp('/themes\/'.$this->_themeObjectChild->__toString().'\/js\/style-min.js/',$matches[1][$i++]);
  175. $output = file_get_contents('cache/themes/'.$this->_themeObjectChild->__toString().'/js/style-min.js');
  176. $this->assertRegExp('/var dog="cat";var bird="frog";/',$output);
  177. }
  178. public function testGetJSURLWithInvalidFileSpecifed()
  179. {
  180. $this->assertFalse($this->_themeObject->getJSURL('ThisFileDoesNotExist.js'));
  181. }
  182. public function testGetJSURLAddsJsPathIfSpecified()
  183. {
  184. // check one may not hit cache
  185. $this->assertRegExp('/style-min\.js\?/',$this->_themeObject->getJSURL('style.js'));
  186. // check two definitely should hit cache
  187. $this->assertRegExp('/style-min\.js\?/',$this->_themeObject->getJSURL('style.js'));
  188. // check three for the jspath not being added
  189. $this->assertNotContains('?',$this->_themeObject->getJSURL('style.js',false));
  190. }
  191. public function testGetImageURL()
  192. {
  193. $this->assertEquals('themes/'.$this->_themeObject->__toString().'/images/Accounts.gif',
  194. $this->_themeObject->getImageURL('Accounts.gif',false));
  195. }
  196. public function testGetImageURLWithInvalidFileSpecifed()
  197. {
  198. $this->assertFalse($this->_themeObject->getImageURL('ThisFileDoesNotExist.gif'));
  199. }
  200. public function testGetImageURLCustom()
  201. {
  202. create_custom_directory('themes/'.$this->_themeObject->__toString().'/images/');
  203. sugar_touch('custom/themes/'.$this->_themeObject->__toString().'/images/Accounts.gif');
  204. $this->assertEquals('custom/themes/'.$this->_themeObject->__toString().'/images/Accounts.gif',
  205. $this->_themeObject->getImageURL('Accounts.gif',false));
  206. }
  207. public function testGetImageURLCustomDifferentExtension()
  208. {
  209. create_custom_directory('themes/'.$this->_themeObject->__toString().'/images/');
  210. sugar_touch('custom/themes/'.$this->_themeObject->__toString().'/images/Accounts.png');
  211. $this->assertEquals('custom/themes/'.$this->_themeObject->__toString().'/images/Accounts.png',
  212. $this->_themeObject->getImageURL('Accounts.gif',false));
  213. }
  214. public function testGetImageURLDefault()
  215. {
  216. $this->assertEquals('themes/default/images/Emails.gif',$this->_themeObject->getImageURL('Emails.gif',false));
  217. }
  218. public function testGetImageURLDefaultCustom()
  219. {
  220. create_custom_directory('themes/default/images/');
  221. sugar_touch('custom/themes/default/images/Emails.gif');
  222. $this->assertEquals('custom/themes/default/images/Emails.gif',
  223. $this->_themeObject->getImageURL('Emails.gif',false));
  224. unlink('custom/themes/default/images/Emails.gif');
  225. }
  226. public function testGetImageURLNotFound()
  227. {
  228. $this->assertEquals('',$this->_themeObject->getImageURL('NoImageByThisName.gif',false));
  229. }
  230. public function testGetImageURLAddsJsPathIfSpecified()
  231. {
  232. // check one may not hit cache
  233. $this->assertRegExp('/Accounts\.gif\?/',$this->_themeObject->getImageURL('Accounts.gif'));
  234. // check two definitely should hit cache
  235. $this->assertRegExp('/Accounts\.gif\?/',$this->_themeObject->getImageURL('Accounts.gif'));
  236. // check three for the jspath not being added
  237. $this->assertNotContains('?',$this->_themeObject->getImageURL('Accounts.gif',false));
  238. }
  239. public function testGetImageURLWithParentTheme()
  240. {
  241. $this->assertEquals('themes/'.$this->_themeObject->__toString().'/images/Accounts.gif',
  242. $this->_themeObjectChild->getImageURL('Accounts.gif',false));
  243. }
  244. public function testGetTemplate()
  245. {
  246. $this->assertEquals('themes/'.$this->_themeObject->__toString().'/tpls/header.tpl',
  247. $this->_themeObject->getTemplate('header.tpl'));
  248. }
  249. public function testGetTemplateCustom()
  250. {
  251. create_custom_directory('themes/'.$this->_themeObject->__toString().'/tpls/');
  252. sugar_touch('custom/themes/'.$this->_themeObject->__toString().'/tpls/header.tpl');
  253. $this->assertEquals('custom/themes/'.$this->_themeObject->__toString().'/tpls/header.tpl',
  254. $this->_themeObject->getTemplate('header.tpl'));
  255. }
  256. public function testGetTemplateDefaultCustom()
  257. {
  258. create_custom_directory('themes/default/tpls/');
  259. sugar_touch('custom/themes/default/tpls/SomeDefaultTemplate.tpl');
  260. $this->assertEquals('custom/themes/default/tpls/SomeDefaultTemplate.tpl',
  261. $this->_themeObject->getTemplate('SomeDefaultTemplate.tpl'));
  262. unlink('custom/themes/default/tpls/SomeDefaultTemplate.tpl');
  263. }
  264. public function testGetTemplateWithParentTheme()
  265. {
  266. $this->assertEquals('themes/'.$this->_themeObject->__toString().'/tpls/header.tpl',
  267. $this->_themeObjectChild->getTemplate('header.tpl'));
  268. }
  269. public function testGetTemplateNotFound()
  270. {
  271. $this->assertFalse($this->_themeObject->getTemplate('NoTemplateWithThisName.tpl'));
  272. }
  273. public function testGetAllImages()
  274. {
  275. $images = $this->_themeObject->getAllImages();
  276. $this->assertEquals(
  277. $this->_themeObject->getImageURL('Emails.gif',false),
  278. $images['Emails.gif']);
  279. }
  280. public function testGetAllImagesWhenImageIsInParentTheme()
  281. {
  282. $images = $this->_themeObjectChild->getAllImages();
  283. $this->assertEquals(
  284. $this->_themeObjectChild->getImageURL('Accounts.gif',false),
  285. $images['Accounts.gif']);
  286. $this->assertContains(
  287. $this->_themeObject->getImagePath(),
  288. $images['Accounts.gif']);
  289. }
  290. public function testGetImageSpecifyingWidthAndHeightAndOtherAttributes()
  291. {
  292. $this->assertEquals(
  293. $this->_themeObject->getImage('Emails','alt="foo"',20,30),
  294. "<img src=\"". $this->_themeObject->getImageURL('Emails.gif') ."\" width=\"20\" height=\"30\" alt=\"foo\" />"
  295. );
  296. // check again to see if caching of the image size works as expected
  297. $this->assertEquals(
  298. $this->_themeObject->getImage('Emails','alt="foo"',30,40),
  299. "<img src=\"". $this->_themeObject->getImageURL('Emails.gif') ."\" width=\"20\" height=\"30\" alt=\"foo\" />"
  300. );
  301. }
  302. public function testGetImageDetectingImageHeightAndWidth()
  303. {
  304. $size = getimagesize($this->_themeObject->getImageURL('Contacts.gif',false));
  305. $this->assertEquals(
  306. $this->_themeObject->getImage('Contacts'),
  307. "<img src=\"". $this->_themeObject->getImageURL('Contacts.gif') ."\" width=\"{$size[0]}\" height=\"{$size[1]}\" />"
  308. );
  309. }
  310. public function testGetImageWithInvalidImage()
  311. {
  312. $this->assertFalse($this->_themeObject->getImage('ThisImageDoesNotExist'));
  313. }
  314. }