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

/Test/Case/View/Helper/AssetCompressHelperTest.php

https://github.com/renan/asset_compress
PHP | 491 lines | 363 code | 50 blank | 78 comment | 0 complexity | 76ff108bc975443be3129aa94db08440 MD5 | raw file
  1. <?php
  2. App::uses('AssetConfig', 'AssetCompress.Lib');
  3. App::uses('AssetCompressHelper', 'AssetCompress.View/Helper');
  4. App::uses('HtmlHelper', 'View/Helper');
  5. App::uses('View', 'View');
  6. class AssetCompressHelperTest extends CakeTestCase {
  7. /**
  8. * start a test
  9. *
  10. * @return void
  11. */
  12. public function setUp() {
  13. parent::setUp();
  14. $this->_pluginPath = App::pluginPath('AssetCompress');
  15. $this->_testFiles = $this->_pluginPath . 'Test' . DS . 'test_files' . DS;
  16. $testFile = $this->_testFiles . 'Config' . DS . 'config.ini';
  17. AssetConfig::clearAllCachedKeys();
  18. Cache::drop(AssetConfig::CACHE_CONFIG);
  19. Cache::config(AssetConfig::CACHE_CONFIG, array(
  20. 'path' => TMP,
  21. 'prefix' => 'asset_compress_test_',
  22. 'engine' => 'File'
  23. ));
  24. $controller = null;
  25. $request = new CakeRequest(null, false);
  26. $request->webroot = '';
  27. $view = new View($controller);
  28. $view->request = $request;
  29. $this->Helper = new AssetCompressHelper($view, array('noconfig' => true));
  30. $Config = AssetConfig::buildFromIniFile($testFile);
  31. $this->Helper->config($Config);
  32. $this->Helper->Html = new HtmlHelper($view);
  33. Router::reload();
  34. Configure::write('debug', 2);
  35. }
  36. /**
  37. * end a test
  38. *
  39. * @return void
  40. */
  41. public function tearDown() {
  42. parent::tearDown();
  43. unset($this->Helper);
  44. Cache::delete(AssetConfig::CACHE_BUILD_TIME_KEY, AssetConfig::CACHE_CONFIG);
  45. Cache::drop(AssetConfig::CACHE_CONFIG);
  46. // @codingStandardsIgnoreStart
  47. @unlink(TMP . AssetConfig::BUILD_TIME_FILE);
  48. // @codingStandardsIgnoreEnd
  49. }
  50. /**
  51. * test that assets only have one base path attached
  52. *
  53. * @return void
  54. */
  55. public function testIncludeAssets() {
  56. Router::setRequestInfo(array(
  57. array('controller' => 'posts', 'action' => 'index', 'plugin' => null),
  58. array('base' => '/some/dir', 'webroot' => '/some/dir/', 'here' => '/some/dir/posts')
  59. ));
  60. $this->Helper->Html->webroot = '/some/dir/';
  61. $this->Helper->addScript('one.js');
  62. $result = $this->Helper->includeAssets();
  63. $this->assertRegExp('#"/some/dir/cache_js/*#', $result, 'double dir set %s');
  64. }
  65. /**
  66. * test that setting $compress = false echos original scripts
  67. *
  68. * @return void
  69. */
  70. public function testNoCompression() {
  71. $this->Helper->addCss('one', 'lib');
  72. $this->Helper->addCss('two');
  73. $this->Helper->addScript('one');
  74. $this->Helper->addScript('dir/two');
  75. $result = $this->Helper->includeAssets(true);
  76. $expected = array(
  77. array(
  78. 'link' => array(
  79. 'type' => 'text/css',
  80. 'rel' => 'stylesheet',
  81. 'href' => 'preg:/.*css\/one\.css/'
  82. )
  83. ),
  84. array(
  85. 'link' => array(
  86. 'type' => 'text/css',
  87. 'rel' => 'stylesheet',
  88. 'href' => 'preg:/.*css\/two\.css/'
  89. )
  90. ),
  91. array(
  92. 'script' => array(
  93. 'type' => 'text/javascript',
  94. 'src' => 'preg:/.*js\/one\.js/'
  95. )
  96. ),
  97. '/script',
  98. array(
  99. 'script' => array(
  100. 'type' => 'text/javascript',
  101. 'src' => 'preg:/.*js\/dir\/two\.js/'
  102. )
  103. ),
  104. '/script'
  105. );
  106. $this->assertTags($result, $expected, true);
  107. }
  108. /**
  109. * test css addition
  110. *
  111. * @return void
  112. */
  113. public function testCssOrderPreserving() {
  114. $this->Helper->addCss('base');
  115. $this->Helper->addCss('reset');
  116. $hash = md5('base_reset');
  117. $result = $this->Helper->includeAssets();
  118. $expected = array(
  119. 'link' => array(
  120. 'type' => 'text/css',
  121. 'rel' => 'stylesheet',
  122. 'href' => '/cache_css/' . $hash . '.css?file%5B0%5D=base&amp;file%5B1%5D=reset'
  123. )
  124. );
  125. $this->assertTags($result, $expected);
  126. }
  127. /**
  128. * test script addition
  129. *
  130. * @return void
  131. */
  132. public function testScriptOrderPreserving() {
  133. $this->Helper->addScript('libraries');
  134. $this->Helper->addScript('thing');
  135. $hash = md5('libraries_thing');
  136. $result = $this->Helper->includeAssets();
  137. $expected = array(
  138. 'script' => array(
  139. 'type' => 'text/javascript',
  140. 'src' => '/cache_js/' . $hash . '.js?file%5B0%5D=libraries&amp;file%5B1%5D=thing'
  141. ),
  142. '/script'
  143. );
  144. $this->assertTags($result, $expected);
  145. }
  146. /**
  147. * test that magic slug builds work.
  148. *
  149. * @return void
  150. */
  151. public function testScriptMagicSlugs() {
  152. $this->Helper->addScript('libraries', ':hash-default');
  153. $this->Helper->addScript('thing', ':hash-default');
  154. $this->Helper->addScript('jquery.js', ':hash-jquery');
  155. $this->Helper->addScript('jquery-ui.js', ':hash-jquery');
  156. $hash1 = md5('libraries_thing');
  157. $hash2 = md5('jquery.js_jquery-ui.js');
  158. $result = $this->Helper->includeAssets();
  159. $expected = array(
  160. array('script' => array(
  161. 'type' => 'text/javascript',
  162. 'src' => '/cache_js/' . $hash1 . '.js?file%5B0%5D=libraries&amp;file%5B1%5D=thing'
  163. )),
  164. '/script',
  165. array('script' => array(
  166. 'type' => 'text/javascript',
  167. 'src' => '/cache_js/' . $hash2 . '.js?file%5B0%5D=jquery.js&amp;file%5B1%5D=jquery-ui.js'
  168. )),
  169. '/script'
  170. );
  171. $this->assertTags($result, $expected);
  172. }
  173. /**
  174. * test generating two script files.
  175. *
  176. * @return void
  177. */
  178. public function testMultipleScriptFiles() {
  179. $this->Helper->addScript('libraries', 'default');
  180. $this->Helper->addScript('thing', 'second');
  181. $result = $this->Helper->includeAssets();
  182. $expected = array(
  183. array('script' => array(
  184. 'type' => 'text/javascript',
  185. 'src' => '/cache_js/default.js?file%5B0%5D=libraries'
  186. )),
  187. '/script',
  188. array('script' => array(
  189. 'type' => 'text/javascript',
  190. 'src' => '/cache_js/second.js?file%5B0%5D=thing'
  191. )),
  192. '/script'
  193. );
  194. $this->assertTags($result, $expected);
  195. }
  196. /**
  197. * test includeJs() with multiple destination files.
  198. *
  199. * @return void
  200. */
  201. public function testIncludeJsMultipleDestination() {
  202. $this->Helper->addScript('libraries', 'default');
  203. $this->Helper->addScript('thing', 'second');
  204. $this->Helper->addScript('other', 'third');
  205. $result = $this->Helper->includeJs('default');
  206. $expected = array(
  207. array('script' => array(
  208. 'type' => 'text/javascript',
  209. 'src' => '/cache_js/default.js?file%5B0%5D=libraries'
  210. )),
  211. );
  212. $this->assertTags($result, $expected);
  213. $result = $this->Helper->includeJs('second', 'third');
  214. $expected = array(
  215. array('script' => array(
  216. 'type' => 'text/javascript',
  217. 'src' => '/cache_js/second.js?file%5B0%5D=thing'
  218. )),
  219. '/script',
  220. array('script' => array(
  221. 'type' => 'text/javascript',
  222. 'src' => '/cache_js/third.js?file%5B0%5D=other'
  223. )),
  224. '/script'
  225. );
  226. $this->assertTags($result, $expected);
  227. }
  228. /**
  229. * test includeCss() with multiple destination files.
  230. *
  231. * @return void
  232. */
  233. public function testIncludeCssMultipleDestination() {
  234. $this->Helper->addCss('libraries', 'default');
  235. $this->Helper->addCss('thing', 'second');
  236. $this->Helper->addCss('other', 'third');
  237. $result = $this->Helper->includeCss('second', 'default');
  238. $expected = array(
  239. array('link' => array(
  240. 'type' => 'text/css',
  241. 'rel' => 'stylesheet',
  242. 'href' => '/cache_css/second.css?file%5B0%5D=thing'
  243. )),
  244. array('link' => array(
  245. 'type' => 'text/css',
  246. 'rel' => 'stylesheet',
  247. 'href' => '/cache_css/default.css?file%5B0%5D=libraries'
  248. )),
  249. );
  250. $this->assertTags($result, $expected);
  251. }
  252. /**
  253. * test that including assets removes them from the list of files to be included.
  254. *
  255. * @return void
  256. */
  257. public function testIncludingFilesRemovesFromQueue() {
  258. $this->Helper->addCss('libraries', 'default');
  259. $result = $this->Helper->includeCss('default');
  260. $expected = array(
  261. 'link' => array(
  262. 'type' => 'text/css',
  263. 'rel' => 'stylesheet',
  264. 'href' => '/cache_css/default.css?file%5B0%5D=libraries'
  265. )
  266. );
  267. $this->assertTags($result, $expected);
  268. $result = $this->Helper->includeCss('default');
  269. $this->assertEquals('', $result);
  270. }
  271. /**
  272. * Test that generated elements can have attributes added.
  273. *
  274. */
  275. public function testAttributesOnElements() {
  276. $result = $this->Helper->script('libs.js', array('defer' => true));
  277. $expected = array(
  278. array('script' => array(
  279. 'defer' => 'defer',
  280. 'type' => 'text/javascript',
  281. 'src' => '/cache_js/libs.js'
  282. ))
  283. );
  284. $this->assertTags($result, $expected);
  285. $result = $this->Helper->css('all.css', array('test' => 'value'));
  286. $expected = array(
  287. 'link' => array(
  288. 'type' => 'text/css',
  289. 'test' => 'value',
  290. 'rel' => 'stylesheet',
  291. 'href' => '/cache_css/all.css'
  292. )
  293. );
  294. $this->assertTags($result, $expected);
  295. }
  296. /**
  297. * test that a baseurl configuration works well.
  298. *
  299. * @return void
  300. */
  301. public function testBaseUrl() {
  302. Configure::write('debug', 0);
  303. $config = $this->Helper->config();
  304. $config->set('js.baseUrl', 'http://cdn.example.com/js/');
  305. $config->set('js.timestamp', false);
  306. $result = $this->Helper->script('libs.js');
  307. $expected = array(
  308. array('script' => array(
  309. 'type' => 'text/javascript',
  310. 'src' => 'http://cdn.example.com/js/libs.js'
  311. ))
  312. );
  313. $this->assertTags($result, $expected);
  314. Configure::write('debug', 1);
  315. $result = $this->Helper->script('libs.js');
  316. $expected = array(
  317. array('script' => array(
  318. 'type' => 'text/javascript',
  319. 'src' => '/cache_js/libs.js'
  320. ))
  321. );
  322. $this->assertTags($result, $expected);
  323. }
  324. /**
  325. * test that baseurl and timestamps play nice.
  326. *
  327. * @return void
  328. */
  329. public function testBaseUrlAndTimestamp() {
  330. Configure::write('debug', 0);
  331. $config = $this->Helper->config();
  332. $config->set('js.baseUrl', 'http://cdn.example.com/js/');
  333. $config->set('js.timestamp', true);
  334. $config->general('cacheConfig', true);
  335. // populate the cache.
  336. Cache::write(AssetConfig::CACHE_BUILD_TIME_KEY, array('libs.js' => 1234), AssetConfig::CACHE_CONFIG);
  337. $result = $this->Helper->script('libs.js');
  338. $expected = array(
  339. array('script' => array(
  340. 'type' => 'text/javascript',
  341. 'src' => 'http://cdn.example.com/js/libs.v1234.js'
  342. ))
  343. );
  344. $this->assertTags($result, $expected);
  345. }
  346. /**
  347. * Test that builds using themes defined in the ini file work
  348. * with themes.
  349. *
  350. * @return void
  351. */
  352. public function testDefinedBuildWithThemeNoBuiltAsset() {
  353. $this->Helper->theme = 'blue';
  354. $config = $this->Helper->config();
  355. $config->addTarget('themed.js', array(
  356. 'theme' => true,
  357. 'files' => array('libraries.js')
  358. ));
  359. $result = $this->Helper->script('themed.js');
  360. $expected = array(
  361. array('script' => array(
  362. 'type' => 'text/javascript',
  363. 'src' => '/cache_js/themed.js?theme=blue'
  364. ))
  365. );
  366. $this->assertTags($result, $expected);
  367. }
  368. public function testRawAssets() {
  369. $result = $this->Helper->script('new_file.js', array('raw' => true));
  370. $expected = array(
  371. array(
  372. 'script' => array(
  373. 'type' => 'text/javascript',
  374. 'src' => 'js/prototype.js'
  375. ),
  376. ),
  377. '/script',
  378. array(
  379. 'script' => array(
  380. 'type' => 'text/javascript',
  381. 'src' => 'js/scriptaculous.js'
  382. ),
  383. ),
  384. '/script',
  385. );
  386. $this->assertTags($result, $expected);
  387. }
  388. public function testRawAssetsPlugin() {
  389. App::build(array(
  390. 'Plugin' => array($this->_testFiles . 'Plugin' . DS)
  391. ));
  392. CakePlugin::load('TestAsset');
  393. $config = AssetConfig::buildFromIniFile($this->_testFiles . 'Config/plugins.ini');
  394. $this->Helper->config($config);
  395. $result = $this->Helper->css('plugins.css', array('raw' => true));
  396. $expected = array(
  397. array(
  398. 'link' => array(
  399. 'type' => 'text/css',
  400. 'rel' => 'stylesheet',
  401. 'href' => 'css/nav.css'
  402. )
  403. ),
  404. array(
  405. 'link' => array(
  406. 'type' => 'text/css',
  407. 'rel' => 'stylesheet',
  408. 'href' => '/test_asset/plugin.css'
  409. )
  410. ),
  411. );
  412. $this->assertTags($result, $expected);
  413. }
  414. public function testCompiledBuildWithThemes() {
  415. Configure::write('debug', 0);
  416. $config = $this->Helper->config();
  417. $config->general('writeCache', true);
  418. $config->set('js.timestamp', false);
  419. $config->cachePath('js', TMP);
  420. $config->addTarget('asset_test.js', array(
  421. 'files' => array('one.js'),
  422. 'theme' => true
  423. ));
  424. $this->Helper->theme = 'blue';
  425. $result = $this->Helper->script('asset_test.js');
  426. $result = str_replace('/', DS, $result);
  427. $this->assertContains('blue-asset_test.js', $result);
  428. }
  429. public function testUrlGenerationProductionMode() {
  430. Configure::write('debug', 0);
  431. $this->Helper->config()->set('js.timestamp', false);
  432. $result = $this->Helper->script('libs.js');
  433. $expected = array(
  434. array('script' => array(
  435. 'type' => 'text/javascript',
  436. 'src' => '/cache_js/libs.js'
  437. ))
  438. );
  439. $this->assertTags($result, $expected);
  440. }
  441. }