AssetsTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. <?php
  2. use Codeception\Util\Fixtures;
  3. use Grav\Common\Grav;
  4. use Grav\Common\Assets;
  5. /**
  6. * Class AssetsTest
  7. */
  8. class AssetsTest extends \Codeception\TestCase\Test
  9. {
  10. /** @var Grav $grav */
  11. protected $grav;
  12. /** @var Assets $assets */
  13. protected $assets;
  14. protected function _before()
  15. {
  16. $grav = Fixtures::get('grav');
  17. $this->grav = $grav();
  18. $this->assets = $this->grav['assets'];
  19. }
  20. protected function _after()
  21. {
  22. }
  23. public function testAddingAssets()
  24. {
  25. //test add()
  26. $this->assets->add('test.css');
  27. $css = $this->assets->css();
  28. $this->assertSame('<link href="/test.css" type="text/css" rel="stylesheet">' . PHP_EOL, $css);
  29. $array = $this->assets->getCss();
  30. /** @var Assets\BaseAsset $item */
  31. $item = reset($array);
  32. $actual = json_encode($item);
  33. $expected = '
  34. {
  35. "type":"css",
  36. "elements":{
  37. "asset":"\/test.css",
  38. "asset_type":"css",
  39. "order":0,
  40. "group":"head",
  41. "position":"pipeline",
  42. "priority":10,
  43. "attributes":{
  44. "type":"text\/css",
  45. "rel":"stylesheet"
  46. },
  47. "modified":false,
  48. "query":""
  49. }
  50. }';
  51. $this->assertJsonStringEqualsJsonString($expected, $actual);
  52. $this->assets->add('test.js');
  53. $js = $this->assets->js();
  54. $this->assertSame('<script src="/test.js"></script>' . PHP_EOL, $js);
  55. $array = $this->assets->getJs();
  56. /** @var Assets\BaseAsset $item */
  57. $item = reset($array);
  58. $actual = json_encode($item);
  59. $expected = '
  60. {
  61. "type":"js",
  62. "elements":{
  63. "asset":"\/test.js",
  64. "asset_type":"js",
  65. "order":0,
  66. "group":"head",
  67. "position":"pipeline",
  68. "priority":10,
  69. "attributes":[
  70. ],
  71. "modified":false,
  72. "query":""
  73. }
  74. }';
  75. $this->assertJsonStringEqualsJsonString($expected, $actual);
  76. //test addCss(). Test adding asset to a separate group
  77. $this->assets->reset();
  78. $this->assets->addCSS('test.css');
  79. $css = $this->assets->css();
  80. $this->assertSame('<link href="/test.css" type="text/css" rel="stylesheet">' . PHP_EOL, $css);
  81. $array = $this->assets->getCss();
  82. /** @var Assets\BaseAsset $item */
  83. $item = reset($array);
  84. $actual = json_encode($item);
  85. $expected = '
  86. {
  87. "type":"css",
  88. "elements":{
  89. "asset":"\/test.css",
  90. "asset_type":"css",
  91. "order":0,
  92. "group":"head",
  93. "position":"pipeline",
  94. "priority":10,
  95. "attributes":{
  96. "type":"text\/css",
  97. "rel":"stylesheet"
  98. },
  99. "modified":false,
  100. "query":""
  101. }
  102. }';
  103. $this->assertJsonStringEqualsJsonString($expected, $actual);
  104. //test addCss(). Testing with remote URL
  105. $this->assets->reset();
  106. $this->assets->addCSS('http://www.somesite.com/test.css');
  107. $css = $this->assets->css();
  108. $this->assertSame('<link href="http://www.somesite.com/test.css" type="text/css" rel="stylesheet">' . PHP_EOL, $css);
  109. $array = $this->assets->getCss();
  110. /** @var Assets\BaseAsset $item */
  111. $item = reset($array);
  112. $actual = json_encode($item);
  113. $expected = '
  114. {
  115. "type":"css",
  116. "elements":{
  117. "asset":"http:\/\/www.somesite.com\/test.css",
  118. "asset_type":"css",
  119. "order":0,
  120. "group":"head",
  121. "position":"pipeline",
  122. "priority":10,
  123. "attributes":{
  124. "type":"text\/css",
  125. "rel":"stylesheet"
  126. },
  127. "query":""
  128. }
  129. }';
  130. $this->assertJsonStringEqualsJsonString($expected, $actual);
  131. //test addCss() adding asset to a separate group, and with an alternate rel attribute
  132. $this->assets->reset();
  133. $this->assets->addCSS('test.css', ['group' => 'alternate', 'rel' => 'alternate']);
  134. $css = $this->assets->css('alternate');
  135. $this->assertSame('<link href="/test.css" type="text/css" rel="alternate">' . PHP_EOL, $css);
  136. //test addJs()
  137. $this->assets->reset();
  138. $this->assets->addJs('test.js');
  139. $js = $this->assets->js();
  140. $this->assertSame('<script src="/test.js"></script>' . PHP_EOL, $js);
  141. $array = $this->assets->getJs();
  142. /** @var Assets\BaseAsset $item */
  143. $item = reset($array);
  144. $actual = json_encode($item);
  145. $expected = '
  146. {
  147. "type":"js",
  148. "elements":{
  149. "asset":"\/test.js",
  150. "asset_type":"js",
  151. "order":0,
  152. "group":"head",
  153. "position":"pipeline",
  154. "priority":10,
  155. "attributes":[],
  156. "modified":false,
  157. "query":""
  158. }
  159. }';
  160. $this->assertJsonStringEqualsJsonString($expected, $actual);
  161. //Test CSS Groups
  162. $this->assets->reset();
  163. $this->assets->addCSS('test.css', ['group' => 'footer']);
  164. $css = $this->assets->css();
  165. $this->assertEmpty($css);
  166. $css = $this->assets->css('footer');
  167. $this->assertSame('<link href="/test.css" type="text/css" rel="stylesheet">' . PHP_EOL, $css);
  168. $array = $this->assets->getCss();
  169. /** @var Assets\BaseAsset $item */
  170. $item = reset($array);
  171. $actual = json_encode($item);
  172. $expected = '
  173. {
  174. "type": "css",
  175. "elements": {
  176. "asset": "/test.css",
  177. "asset_type": "css",
  178. "order": 0,
  179. "group": "footer",
  180. "position": "pipeline",
  181. "priority": 10,
  182. "attributes": {
  183. "type": "text/css",
  184. "rel": "stylesheet"
  185. },
  186. "modified": false,
  187. "query": ""
  188. }
  189. }
  190. ';
  191. $this->assertJsonStringEqualsJsonString($expected, $actual);
  192. //Test JS Groups
  193. $this->assets->reset();
  194. $this->assets->addJs('test.js', ['group' => 'footer']);
  195. $js = $this->assets->js();
  196. $this->assertEmpty($js);
  197. $js = $this->assets->js('footer');
  198. $this->assertSame('<script src="/test.js"></script>' . PHP_EOL, $js);
  199. $array = $this->assets->getJs();
  200. /** @var Assets\BaseAsset $item */
  201. $item = reset($array);
  202. $actual = json_encode($item);
  203. $expected = '
  204. {
  205. "type": "js",
  206. "elements": {
  207. "asset": "/test.js",
  208. "asset_type": "js",
  209. "order": 0,
  210. "group": "footer",
  211. "position": "pipeline",
  212. "priority": 10,
  213. "attributes": [],
  214. "modified": false,
  215. "query": ""
  216. }
  217. }';
  218. $this->assertJsonStringEqualsJsonString($expected, $actual);
  219. //Test async / defer
  220. $this->assets->reset();
  221. $this->assets->addJs('test.js', ['loading' => 'async']);
  222. $js = $this->assets->js();
  223. $this->assertSame('<script src="/test.js" async></script>' . PHP_EOL, $js);
  224. $array = $this->assets->getJs();
  225. /** @var Assets\BaseAsset $item */
  226. $item = reset($array);
  227. $actual = json_encode($item);
  228. $expected = '
  229. {
  230. "type": "js",
  231. "elements": {
  232. "asset": "/test.js",
  233. "asset_type": "js",
  234. "order": 0,
  235. "group": "head",
  236. "position": "pipeline",
  237. "priority": 10,
  238. "attributes": {
  239. "loading": "async"
  240. },
  241. "modified": false,
  242. "query": ""
  243. }
  244. }';
  245. $this->assertJsonStringEqualsJsonString($expected, $actual);
  246. $this->assets->reset();
  247. $this->assets->addJs('test.js', ['loading' => 'defer']);
  248. $js = $this->assets->js();
  249. $this->assertSame('<script src="/test.js" defer></script>' . PHP_EOL, $js);
  250. $array = $this->assets->getJs();
  251. /** @var Assets\BaseAsset $item */
  252. $item = reset($array);
  253. $actual = json_encode($item);
  254. $expected = '
  255. {
  256. "type": "js",
  257. "elements": {
  258. "asset": "/test.js",
  259. "asset_type": "js",
  260. "order": 0,
  261. "group": "head",
  262. "position": "pipeline",
  263. "priority": 10,
  264. "attributes": {
  265. "loading": "defer"
  266. },
  267. "modified": false,
  268. "query": ""
  269. }
  270. }';
  271. $this->assertJsonStringEqualsJsonString($expected, $actual);
  272. //Test inline
  273. $this->assets->reset();
  274. $this->assets->setJsPipeline(true);
  275. $this->assets->addJs('/system/assets/jquery/jquery-3.x.min.js');
  276. $js = $this->assets->js('head', ['loading' => 'inline']);
  277. $this->assertContains('"jquery",[],function()', $js);
  278. $this->assets->reset();
  279. $this->assets->setCssPipeline(true);
  280. $this->assets->addCss('/system/assets/debugger.css');
  281. $css = $this->assets->css('head', ['loading' => 'inline']);
  282. $this->assertContains('div.phpdebugbar', $css);
  283. $this->assets->reset();
  284. $this->assets->setCssPipeline(true);
  285. $this->assets->addCss('https://fonts.googleapis.com/css?family=Roboto');
  286. $css = $this->assets->css('head', ['loading' => 'inline']);
  287. $this->assertContains('font-family:\'Roboto\';', $css);
  288. //Test adding media queries
  289. $this->assets->reset();
  290. $this->assets->add('test.css', ['media' => 'only screen and (min-width: 640px)']);
  291. $css = $this->assets->css();
  292. $this->assertSame('<link href="/test.css" type="text/css" rel="stylesheet" media="only screen and (min-width: 640px)">' . PHP_EOL, $css);
  293. }
  294. public function testAddingAssetPropertiesWithArray()
  295. {
  296. //Test adding assets with object to define properties
  297. $this->assets->reset();
  298. $this->assets->addJs('test.js', ['loading' => 'async']);
  299. $js = $this->assets->js();
  300. $this->assertSame('<script src="/test.js" async></script>' . PHP_EOL, $js);
  301. $this->assets->reset();
  302. }
  303. public function testAddingJSAssetPropertiesWithArrayFromCollection()
  304. {
  305. //Test adding properties with array
  306. $this->assets->reset();
  307. $this->assets->addJs('jquery', ['loading' => 'async']);
  308. $js = $this->assets->js();
  309. $this->assertSame('<script src="/system/assets/jquery/jquery-2.x.min.js" async></script>' . PHP_EOL, $js);
  310. //Test priority too
  311. $this->assets->reset();
  312. $this->assets->addJs('jquery', ['loading' => 'async', 'priority' => 1]);
  313. $this->assets->addJs('test.js', ['loading' => 'async', 'priority' => 2]);
  314. $js = $this->assets->js();
  315. $this->assertSame('<script src="/test.js" async></script>' . PHP_EOL .
  316. '<script src="/system/assets/jquery/jquery-2.x.min.js" async></script>' . PHP_EOL, $js);
  317. //Test multiple groups
  318. $this->assets->reset();
  319. $this->assets->addJs('jquery', ['loading' => 'async', 'priority' => 1, 'group' => 'footer']);
  320. $this->assets->addJs('test.js', ['loading' => 'async', 'priority' => 2]);
  321. $js = $this->assets->js();
  322. $this->assertSame('<script src="/test.js" async></script>' . PHP_EOL, $js);
  323. $js = $this->assets->js('footer');
  324. $this->assertSame('<script src="/system/assets/jquery/jquery-2.x.min.js" async></script>' . PHP_EOL, $js);
  325. //Test adding array of assets
  326. //Test priority too
  327. $this->assets->reset();
  328. $this->assets->addJs(['jquery', 'test.js'], ['loading' => 'async']);
  329. $js = $this->assets->js();
  330. $this->assertSame('<script src="/system/assets/jquery/jquery-2.x.min.js" async></script>' . PHP_EOL .
  331. '<script src="/test.js" async></script>' . PHP_EOL, $js);
  332. }
  333. public function testAddingLegacyFormat()
  334. {
  335. // regular CSS add
  336. //test addCss(). Test adding asset to a separate group
  337. $this->assets->reset();
  338. $this->assets->addCSS('test.css', 15, true, 'bottom', 'async');
  339. $css = $this->assets->css('bottom');
  340. $this->assertSame('<link href="/test.css" type="text/css" rel="stylesheet" async>' . PHP_EOL, $css);
  341. $array = $this->assets->getCss();
  342. /** @var Assets\BaseAsset $item */
  343. $item = reset($array);
  344. $actual = json_encode($item);
  345. $expected = '
  346. {
  347. "type":"css",
  348. "elements":{
  349. "asset":"\/test.css",
  350. "asset_type":"css",
  351. "order":0,
  352. "group":"bottom",
  353. "position":"pipeline",
  354. "priority":15,
  355. "attributes":{
  356. "type":"text\/css",
  357. "rel":"stylesheet",
  358. "loading":"async"
  359. },
  360. "modified":false,
  361. "query":""
  362. }
  363. }';
  364. $this->assertJsonStringEqualsJsonString($expected, $actual);
  365. $this->assets->reset();
  366. $this->assets->addJs('test.js', 15, false, 'defer', 'bottom');
  367. $js = $this->assets->js('bottom');
  368. $this->assertSame('<script src="/test.js" defer></script>' . PHP_EOL, $js);
  369. $array = $this->assets->getJs();
  370. /** @var Assets\BaseAsset $item */
  371. $item = reset($array);
  372. $actual = json_encode($item);
  373. $expected = '
  374. {
  375. "type": "js",
  376. "elements": {
  377. "asset": "/test.js",
  378. "asset_type": "js",
  379. "order": 0,
  380. "group": "bottom",
  381. "position": "after",
  382. "priority": 15,
  383. "attributes": {
  384. "loading": "defer"
  385. },
  386. "modified": false,
  387. "query": ""
  388. }
  389. }';
  390. $this->assertJsonStringEqualsJsonString($expected, $actual);
  391. $this->assets->reset();
  392. $this->assets->addInlineCss('body { color: black }', 15, 'bottom');
  393. $css = $this->assets->css('bottom');
  394. $this->assertSame('<style>' . PHP_EOL . 'body { color: black }' . PHP_EOL . '</style>' . PHP_EOL, $css);
  395. $this->assets->reset();
  396. $this->assets->addInlineJs('alert("test")', 15, 'bottom', ['id' => 'foo']);
  397. $js = $this->assets->js('bottom');
  398. $this->assertSame('<script id="foo">' . PHP_EOL . 'alert("test")' . PHP_EOL . '</script>' . PHP_EOL, $js);
  399. }
  400. public function testAddingCSSAssetPropertiesWithArrayFromCollection()
  401. {
  402. $this->assets->registerCollection('test', ['/system/assets/whoops.css']);
  403. //Test priority too
  404. $this->assets->reset();
  405. $this->assets->addCss('test', ['priority' => 1]);
  406. $this->assets->addCss('test.css', ['priority' => 2]);
  407. $css = $this->assets->css();
  408. $this->assertSame('<link href="/test.css" type="text/css" rel="stylesheet">' . PHP_EOL .
  409. '<link href="/system/assets/whoops.css" type="text/css" rel="stylesheet">' . PHP_EOL, $css);
  410. //Test multiple groups
  411. $this->assets->reset();
  412. $this->assets->addCss('test', ['priority' => 1, 'group' => 'footer']);
  413. $this->assets->addCss('test.css', ['priority' => 2]);
  414. $css = $this->assets->css();
  415. $this->assertSame('<link href="/test.css" type="text/css" rel="stylesheet">' . PHP_EOL, $css);
  416. $css = $this->assets->css('footer');
  417. $this->assertSame('<link href="/system/assets/whoops.css" type="text/css" rel="stylesheet">' . PHP_EOL, $css);
  418. //Test adding array of assets
  419. //Test priority too
  420. $this->assets->reset();
  421. $this->assets->addCss(['test', 'test.css'], ['loading' => 'async']);
  422. $css = $this->assets->css();
  423. $this->assertSame('<link href="/system/assets/whoops.css" type="text/css" rel="stylesheet" async>' . PHP_EOL .
  424. '<link href="/test.css" type="text/css" rel="stylesheet" async>' . PHP_EOL, $css);
  425. }
  426. public function testPriorityOfAssets()
  427. {
  428. $this->assets->reset();
  429. $this->assets->add('test.css');
  430. $this->assets->add('test-after.css');
  431. $css = $this->assets->css();
  432. $this->assertSame('<link href="/test.css" type="text/css" rel="stylesheet">' . PHP_EOL .
  433. '<link href="/test-after.css" type="text/css" rel="stylesheet">' . PHP_EOL, $css);
  434. //----------------
  435. $this->assets->reset();
  436. $this->assets->add('test-after.css', 1);
  437. $this->assets->add('test.css', 2);
  438. $css = $this->assets->css();
  439. $this->assertSame('<link href="/test.css" type="text/css" rel="stylesheet">' . PHP_EOL .
  440. '<link href="/test-after.css" type="text/css" rel="stylesheet">' . PHP_EOL, $css);
  441. //----------------
  442. $this->assets->reset();
  443. $this->assets->add('test-after.css', 1);
  444. $this->assets->add('test.css', 2);
  445. $this->assets->add('test-before.css', 3);
  446. $css = $this->assets->css();
  447. $this->assertSame('<link href="/test-before.css" type="text/css" rel="stylesheet">' . PHP_EOL .
  448. '<link href="/test.css" type="text/css" rel="stylesheet">' . PHP_EOL .
  449. '<link href="/test-after.css" type="text/css" rel="stylesheet">' . PHP_EOL, $css);
  450. }
  451. public function testPipeline()
  452. {
  453. $this->assets->reset();
  454. //File not existing. Pipeline searches for that file without reaching it. Output is empty.
  455. $this->assets->add('test.css', null, true);
  456. $this->assets->setCssPipeline(true);
  457. $css = $this->assets->css();
  458. $this->assertRegExp('#<link href=\"\/assets\/(.*).css\" type=\"text\/css\" rel=\"stylesheet\">#', $css);
  459. //Add a core Grav CSS file, which is found. Pipeline will now return a file
  460. $this->assets->add('/system/assets/debugger.css', null, true);
  461. $css = $this->assets->css();
  462. $this->assertRegExp('#<link href=\"\/assets\/(.*).css\" type=\"text\/css\" rel=\"stylesheet\">#', $css);
  463. }
  464. public function testPipelineWithTimestamp()
  465. {
  466. $this->assets->reset();
  467. $this->assets->setTimestamp('foo');
  468. $this->assets->setCssPipeline(true);
  469. //Add a core Grav CSS file, which is found. Pipeline will now return a file
  470. $this->assets->add('/system/assets/debugger.css', null, true);
  471. $css = $this->assets->css();
  472. $this->assertRegExp('#<link href=\"\/assets\/(.*).css\?foo\" type=\"text\/css\" rel=\"stylesheet\">#', $css);
  473. }
  474. public function testInline()
  475. {
  476. $this->assets->reset();
  477. //File not existing. Pipeline searches for that file without reaching it. Output is empty.
  478. $this->assets->add('test.css', ['loading' => 'inline']);
  479. $css = $this->assets->css();
  480. $this->assertSame("<style>\n\n</style>\n", $css);
  481. $this->assets->reset();
  482. //Add a core Grav CSS file, which is found. Pipeline will now return its content.
  483. $this->assets->addCss('https://fonts.googleapis.com/css?family=Roboto', ['loading' => 'inline']);
  484. $this->assets->addCss('/system/assets/debugger.css', ['loading' => 'inline']);
  485. $css = $this->assets->css();
  486. $this->assertContains('font-family: \'Roboto\';', $css);
  487. $this->assertContains('div.phpdebugbar-header', $css);
  488. }
  489. public function testInlinePipeline()
  490. {
  491. $this->assets->reset();
  492. $this->assets->setCssPipeline(true);
  493. //File not existing. Pipeline searches for that file without reaching it. Output is empty.
  494. $this->assets->add('test.css');
  495. $css = $this->assets->css('head', ['loading' => 'inline']);
  496. $this->assertSame("<style>\n\n</style>\n", $css);
  497. //Add a core Grav CSS file, which is found. Pipeline will now return its content.
  498. $this->assets->addCss('https://fonts.googleapis.com/css?family=Roboto', null, true);
  499. $this->assets->add('/system/assets/debugger.css', null, true);
  500. $css = $this->assets->css('head', ['loading' => 'inline']);
  501. $this->assertContains('font-family:\'Roboto\';', $css);
  502. $this->assertContains('div.phpdebugbar', $css);
  503. }
  504. public function testAddAsyncJs()
  505. {
  506. $this->assets->reset();
  507. $this->assets->addAsyncJs('jquery');
  508. $js = $this->assets->js();
  509. $this->assertSame('<script src="/system/assets/jquery/jquery-2.x.min.js" async></script>' . PHP_EOL, $js);
  510. }
  511. public function testAddDeferJs()
  512. {
  513. $this->assets->reset();
  514. $this->assets->addDeferJs('jquery');
  515. $js = $this->assets->js();
  516. $this->assertSame('<script src="/system/assets/jquery/jquery-2.x.min.js" defer></script>' . PHP_EOL, $js);
  517. }
  518. public function testTimestamps()
  519. {
  520. // local CSS nothing extra
  521. $this->assets->reset();
  522. $this->assets->setTimestamp('foo');
  523. $this->assets->addCSS('test.css');
  524. $css = $this->assets->css();
  525. $this->assertSame('<link href="/test.css?foo" type="text/css" rel="stylesheet">' . PHP_EOL, $css);
  526. // local CSS already with param
  527. $this->assets->reset();
  528. $this->assets->setTimestamp('foo');
  529. $this->assets->addCSS('test.css?bar');
  530. $css = $this->assets->css();
  531. $this->assertSame('<link href="/test.css?bar&foo" type="text/css" rel="stylesheet">' . PHP_EOL, $css);
  532. // external CSS already
  533. $this->assets->reset();
  534. $this->assets->setTimestamp('foo');
  535. $this->assets->addCSS('http://somesite.com/test.css');
  536. $css = $this->assets->css();
  537. $this->assertSame('<link href="http://somesite.com/test.css?foo" type="text/css" rel="stylesheet">' . PHP_EOL, $css);
  538. // external CSS already with param
  539. $this->assets->reset();
  540. $this->assets->setTimestamp('foo');
  541. $this->assets->addCSS('http://somesite.com/test.css?bar');
  542. $css = $this->assets->css();
  543. $this->assertSame('<link href="http://somesite.com/test.css?bar&foo" type="text/css" rel="stylesheet">' . PHP_EOL, $css);
  544. // local JS nothing extra
  545. $this->assets->reset();
  546. $this->assets->setTimestamp('foo');
  547. $this->assets->addJs('test.js');
  548. $css = $this->assets->js();
  549. $this->assertSame('<script src="/test.js?foo"></script>' . PHP_EOL, $css);
  550. // local JS already with param
  551. $this->assets->reset();
  552. $this->assets->setTimestamp('foo');
  553. $this->assets->addJs('test.js?bar');
  554. $css = $this->assets->js();
  555. $this->assertSame('<script src="/test.js?bar&foo"></script>' . PHP_EOL, $css);
  556. // external JS already
  557. $this->assets->reset();
  558. $this->assets->setTimestamp('foo');
  559. $this->assets->addJs('http://somesite.com/test.js');
  560. $css = $this->assets->js();
  561. $this->assertSame('<script src="http://somesite.com/test.js?foo"></script>' . PHP_EOL, $css);
  562. // external JS already with param
  563. $this->assets->reset();
  564. $this->assets->setTimestamp('foo');
  565. $this->assets->addJs('http://somesite.com/test.js?bar');
  566. $css = $this->assets->js();
  567. $this->assertSame('<script src="http://somesite.com/test.js?bar&foo"></script>' . PHP_EOL, $css);
  568. }
  569. public function testAddInlineCss()
  570. {
  571. $this->assets->reset();
  572. $this->assets->addInlineCss('body { color: black }');
  573. $css = $this->assets->css();
  574. $this->assertSame('<style>' . PHP_EOL . 'body { color: black }' . PHP_EOL . '</style>' . PHP_EOL, $css);
  575. }
  576. public function testAddInlineJs()
  577. {
  578. $this->assets->reset();
  579. $this->assets->addInlineJs('alert("test")');
  580. $js = $this->assets->js();
  581. $this->assertSame('<script>' . PHP_EOL . 'alert("test")' . PHP_EOL . '</script>' . PHP_EOL, $js);
  582. }
  583. public function testGetCollections()
  584. {
  585. $this->assertInternalType('array', $this->assets->getCollections());
  586. $this->assertContains('jquery', array_keys($this->assets->getCollections()));
  587. $this->assertContains('system://assets/jquery/jquery-2.x.min.js', $this->assets->getCollections());
  588. }
  589. public function testExists()
  590. {
  591. $this->assertTrue($this->assets->exists('jquery'));
  592. $this->assertFalse($this->assets->exists('another-unexisting-library'));
  593. }
  594. public function testRegisterCollection()
  595. {
  596. $this->assets->registerCollection('debugger', ['/system/assets/debugger.css']);
  597. $this->assertTrue($this->assets->exists('debugger'));
  598. $this->assertContains('debugger', array_keys($this->assets->getCollections()));
  599. }
  600. public function testReset()
  601. {
  602. $this->assets->addInlineJs('alert("test")');
  603. $this->assets->reset();
  604. $this->assertCount(0, (array) $this->assets->getJs());
  605. $this->assets->addAsyncJs('jquery');
  606. $this->assets->reset();
  607. $this->assertCount(0, (array) $this->assets->getJs());
  608. $this->assets->addInlineCss('body { color: black }');
  609. $this->assets->reset();
  610. $this->assertCount(0, (array) $this->assets->getCss());
  611. $this->assets->add('/system/assets/debugger.css', null, true);
  612. $this->assets->reset();
  613. $this->assertCount(0, (array) $this->assets->getCss());
  614. }
  615. public function testResetJs()
  616. {
  617. $this->assets->addInlineJs('alert("test")');
  618. $this->assets->resetJs();
  619. $this->assertCount(0, (array) $this->assets->getJs());
  620. $this->assets->addAsyncJs('jquery');
  621. $this->assets->resetJs();
  622. $this->assertCount(0, (array) $this->assets->getJs());
  623. }
  624. public function testResetCss()
  625. {
  626. $this->assets->addInlineCss('body { color: black }');
  627. $this->assets->resetCss();
  628. $this->assertCount(0, (array) $this->assets->getCss());
  629. $this->assets->add('/system/assets/debugger.css', null, true);
  630. $this->assets->resetCss();
  631. $this->assertCount(0, (array) $this->assets->getCss());
  632. }
  633. public function testAddDirCss()
  634. {
  635. $this->assets->addDirCss('/system');
  636. $this->assertInternalType('array', $this->assets->getCss());
  637. $this->assertGreaterThan(0, (array) $this->assets->getCss());
  638. $this->assertInternalType('array', $this->assets->getJs());
  639. $this->assertCount(0, (array) $this->assets->getJs());
  640. $this->assets->reset();
  641. $this->assets->addDirCss('/system/assets');
  642. $this->assertInternalType('array', $this->assets->getCss());
  643. $this->assertGreaterThan(0, (array) $this->assets->getCss());
  644. $this->assertInternalType('array', $this->assets->getJs());
  645. $this->assertCount(0, (array) $this->assets->getJs());
  646. $this->assets->reset();
  647. $this->assets->addDirJs('/system');
  648. $this->assertInternalType('array', $this->assets->getCss());
  649. $this->assertCount(0, (array) $this->assets->getCss());
  650. $this->assertInternalType('array', $this->assets->getJs());
  651. $this->assertGreaterThan(0, (array) $this->assets->getJs());
  652. $this->assets->reset();
  653. $this->assets->addDirJs('/system/assets');
  654. $this->assertInternalType('array', $this->assets->getCss());
  655. $this->assertCount(0, (array) $this->assets->getCss());
  656. $this->assertInternalType('array', $this->assets->getJs());
  657. $this->assertGreaterThan(0, (array) $this->assets->getJs());
  658. $this->assets->reset();
  659. $this->assets->addDir('/system/assets');
  660. $this->assertInternalType('array', $this->assets->getCss());
  661. $this->assertGreaterThan(0, (array) $this->assets->getCss());
  662. $this->assertInternalType('array', $this->assets->getJs());
  663. $this->assertGreaterThan(0, (array) $this->assets->getJs());
  664. //Use streams
  665. $this->assets->reset();
  666. $this->assets->addDir('system://assets');
  667. $this->assertInternalType('array', $this->assets->getCss());
  668. $this->assertGreaterThan(0, (array) $this->assets->getCss());
  669. $this->assertInternalType('array', $this->assets->getJs());
  670. $this->assertGreaterThan(0, (array) $this->assets->getJs());
  671. }
  672. }