MediaTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\net\http;
  9. use lithium\core\Environment;
  10. use lithium\net\http\Media;
  11. use lithium\action\Request;
  12. use lithium\action\Response;
  13. use lithium\core\Libraries;
  14. use lithium\data\entity\Record;
  15. use lithium\data\collection\RecordSet;
  16. class MediaTest extends \lithium\test\Unit {
  17. /**
  18. * Reset the `Media` class to its default state.
  19. */
  20. public function setUp() {
  21. Media::reset();
  22. }
  23. /**
  24. * Tests setting, getting and removing custom media types.
  25. */
  26. public function testMediaTypes() {
  27. // Get a list of all available media types:
  28. $types = Media::types(); // returns array('html', 'json', 'rss', ...);
  29. $expected = array(
  30. 'html', 'htm', 'form', 'json', 'rss', 'atom', 'css', 'js', 'text', 'txt', 'xml'
  31. );
  32. $this->assertEqual($expected, $types);
  33. $this->assertEqual($expected, Media::formats());
  34. $result = Media::type('json');
  35. $expected = array('application/json');
  36. $this->assertEqual($expected, $result['content']);
  37. $expected = array(
  38. 'cast' => true, 'encode' => 'json_encode', 'decode' => $result['options']['decode']
  39. );
  40. $this->assertEqual($expected, $result['options']);
  41. // Add a custom media type with a custom view class:
  42. Media::type('my', 'text/x-my', array(
  43. 'view' => 'my\custom\View',
  44. 'paths' => array('layout' => false)
  45. ));
  46. $result = Media::types();
  47. $this->assertTrue(in_array('my', $result));
  48. $result = Media::type('my');
  49. $expected = array('text/x-my');
  50. $this->assertEqual($expected, $result['content']);
  51. $expected = array(
  52. 'view' => 'my\custom\View',
  53. 'paths' => array(
  54. 'template' => '{:library}/views/{:controller}/{:template}.{:type}.php',
  55. 'layout' => false,
  56. 'element' => '{:library}/views/elements/{:template}.{:type}.php'
  57. ),
  58. 'encode' => null, 'decode' => null, 'cast' => true, 'conditions' => array()
  59. );
  60. $this->assertEqual($expected, $result['options']);
  61. // Remove a custom media type:
  62. Media::type('my', false);
  63. $result = Media::types();
  64. $this->assertFalse(in_array('my', $result));
  65. }
  66. /**
  67. * Tests that `Media` will return the correct type name of recognized, registered content types.
  68. */
  69. public function testContentTypeDetection() {
  70. $this->assertNull(Media::type('application/foo'));
  71. $this->assertEqual('js', Media::type('application/javascript'));
  72. $this->assertEqual('html', Media::type('*/*'));
  73. $this->assertEqual('json', Media::type('application/json'));
  74. $this->assertEqual('json', Media::type('application/json; charset=UTF-8'));
  75. $result = Media::type('json');
  76. $expected = array('content' => array('application/json'), 'options' => array(
  77. 'cast' => true, 'encode' => 'json_encode', 'decode' => $result['options']['decode']
  78. ));
  79. $this->assertEqual($expected, $result);
  80. }
  81. public function testAssetTypeHandling() {
  82. $result = Media::assets();
  83. $expected = array('js', 'css', 'image', 'generic');
  84. $this->assertEqual($expected, array_keys($result));
  85. $result = Media::assets('css');
  86. $expected = '.css';
  87. $this->assertEqual($expected, $result['suffix']);
  88. $this->assertTrue(isset($result['path']['{:base}/{:library}/css/{:path}']));
  89. $result = Media::assets('my');
  90. $this->assertNull($result);
  91. $result = Media::assets('my', array('suffix' => '.my', 'path' => array(
  92. '{:base}/my/{:path}' => array('base', 'path')
  93. )));
  94. $this->assertNull($result);
  95. $result = Media::assets('my');
  96. $expected = '.my';
  97. $this->assertEqual($expected, $result['suffix']);
  98. $this->assertTrue(isset($result['path']['{:base}/my/{:path}']));
  99. $this->assertNull($result['filter']);
  100. Media::assets('my', array('filter' => array('/my/' => '/your/')));
  101. $result = Media::assets('my');
  102. $expected = array('/my/' => '/your/');
  103. $this->assertEqual($expected, $result['filter']);
  104. $expected = '.my';
  105. $this->assertEqual($expected, $result['suffix']);
  106. Media::assets('my', false);
  107. $result = Media::assets('my');
  108. $this->assertNull($result);
  109. $this->assertEqual('/foo.exe', Media::asset('foo.exe', 'bar'));
  110. }
  111. public function testAssetAbsoluteRelativePaths() {
  112. $result = Media::asset('scheme://host/subpath/file', 'js');
  113. $expected = 'scheme://host/subpath/file';
  114. $this->assertEqual($expected, $result);
  115. $result = Media::asset('//host/subpath/file', 'js', array('base' => '/base'));
  116. $expected = '//host/subpath/file';
  117. $this->assertEqual($expected, $result);
  118. $result = Media::asset('subpath/file', 'js');
  119. $expected = '/js/subpath/file.js';
  120. $this->assertEqual($expected, $result);
  121. }
  122. public function testCustomAssetUrls() {
  123. $env = Environment::get();
  124. $path = Libraries::get(true, 'path');
  125. Libraries::add('cdn_js_test', array(
  126. 'path' => $path,
  127. 'assets' => array(
  128. 'js' => 'http://static.cdn.com'
  129. ),
  130. 'bootstrap' => false
  131. ));
  132. Libraries::add('cdn_env_test', array(
  133. 'path' => $path,
  134. 'assets' => array(
  135. 'js' => 'wrong',
  136. $env => array('js' => 'http://static.cdn.com/myapp')
  137. ),
  138. 'bootstrap' => false
  139. ));
  140. $library = basename($path);
  141. $result = Media::asset('foo', 'js', array('library' => 'cdn_js_test'));
  142. $this->assertEqual("http://static.cdn.com/{$library}/js/foo.js", $result);
  143. $result = Media::asset('foo', 'css', array('library' => 'cdn_js_test'));
  144. $this->assertEqual("/{$library}/css/foo.css", $result);
  145. $result = Media::asset('foo', 'js', array('library' => 'cdn_env_test'));
  146. $this->assertEqual("http://static.cdn.com/myapp/{$library}/js/foo.js", $result);
  147. Libraries::remove('cdn_env_test');
  148. Libraries::remove('cdn_js_test');
  149. }
  150. public function testAssetPathGeneration() {
  151. $resources = Libraries::get(true, 'resources');
  152. $this->skipIf(!is_writable($resources), "Cannot write test app to resources directory.");
  153. $paths = array("{$resources}/media_test/webroot/css", "{$resources}/media_test/webroot/js");
  154. foreach ($paths as $path) {
  155. if (!is_dir($path)) {
  156. mkdir($path, 0777, true);
  157. }
  158. }
  159. touch("{$paths[0]}/debug.css");
  160. Libraries::add('media_test', array('path' => "{$resources}/media_test"));
  161. $result = Media::asset('debug', 'css', array('check' => true, 'library' => 'media_test'));
  162. $this->assertEqual('/media_test/css/debug.css', $result);
  163. $result = Media::asset('debug', 'css', array(
  164. 'timestamp' => true, 'library' => 'media_test'
  165. ));
  166. $this->assertPattern('%^/media_test/css/debug\.css\?\d+$%', $result);
  167. $result = Media::asset('debug.css?type=test', 'css', array(
  168. 'check' => true, 'base' => 'foo', 'library' => 'media_test'
  169. ));
  170. $this->assertEqual('foo/media_test/css/debug.css?type=test', $result);
  171. $result = Media::asset('debug.css?type=test', 'css', array(
  172. 'check' => true, 'base' => 'foo', 'timestamp' => true, 'library' => 'media_test'
  173. ));
  174. $this->assertPattern('%^foo/media_test/css/debug\.css\?type=test&\d+$%', $result);
  175. $file = Media::path('css/debug.css', 'bar', array('library' => 'media_test'));
  176. $this->assertTrue(file_exists($file));
  177. $result = Media::asset('this.file.should.not.exist', 'css', array('check' => true));
  178. $this->assertFalse($result);
  179. unlink("{$paths[0]}/debug.css");
  180. foreach (array_merge($paths, array(dirname($paths[0]))) as $path) {
  181. rmdir($path);
  182. }
  183. }
  184. public function testCustomAssetPathGeneration() {
  185. Media::assets('my', array('suffix' => '.my', 'path' => array(
  186. '{:base}/my/{:path}' => array('base', 'path')
  187. )));
  188. $result = Media::asset('subpath/file', 'my');
  189. $expected = '/my/subpath/file.my';
  190. $this->assertEqual($expected, $result);
  191. Media::assets('my', array('filter' => array('/my/' => '/your/')));
  192. $result = Media::asset('subpath/file', 'my');
  193. $expected = '/your/subpath/file.my';
  194. $this->assertEqual($expected, $result);
  195. $result = Media::asset('subpath/file', 'my', array('base' => '/app/path'));
  196. $expected = '/app/path/your/subpath/file.my';
  197. $this->assertEqual($expected, $result);
  198. $result = Media::asset('subpath/file', 'my', array('base' => '/app/path/'));
  199. $expected = '/app/path//your/subpath/file.my';
  200. $this->assertEqual($expected, $result);
  201. }
  202. public function testMultiLibraryAssetPaths() {
  203. $result = Media::asset('path/file', 'js', array('library' => true, 'base' => '/app/base'));
  204. $expected = '/app/base/js/path/file.js';
  205. $this->assertEqual($expected, $result);
  206. Libraries::add('li3_foo_blog', array(
  207. 'path' => LITHIUM_APP_PATH . '/libraries/plugins/blog',
  208. 'bootstrap' => false,
  209. 'route' => false
  210. ));
  211. $result = Media::asset('path/file', 'js', array(
  212. 'library' => 'li3_foo_blog', 'base' => '/app/base'
  213. ));
  214. $expected = '/app/base/blog/js/path/file.js';
  215. $this->assertEqual($expected, $result);
  216. Libraries::remove('li3_foo_blog');
  217. }
  218. public function testManualAssetPaths() {
  219. $result = Media::asset('/path/file', 'js', array('base' => '/base'));
  220. $expected = '/base/path/file.js';
  221. $this->assertEqual($expected, $result);
  222. $resources = Libraries::get(true, 'resources');
  223. $cssPath = "{$resources}/media_test/webroot/css";
  224. $this->skipIf(!is_writable($resources), "Cannot write test app to resources directory.");
  225. if (!is_dir($cssPath)) {
  226. mkdir($cssPath, 0777, true);
  227. }
  228. Libraries::add('media_test', array('path' => "{$resources}/media_test"));
  229. $result = Media::asset('/foo/bar', 'js', array('base' => '/base', 'check' => true));
  230. $this->assertFalse($result);
  231. file_put_contents("{$cssPath}/debug.css", "html, body { background-color: black; }");
  232. $result = Media::asset('/css/debug', 'css', array(
  233. 'library' => 'media_test', 'base' => '/base', 'check' => true
  234. ));
  235. $expected = '/base/css/debug.css';
  236. $this->assertEqual($expected, $result);
  237. $result = Media::asset('/css/debug.css', 'css', array(
  238. 'library' => 'media_test', 'base' => '/base', 'check' => true
  239. ));
  240. $expected = '/base/css/debug.css';
  241. $this->assertEqual($expected, $result);
  242. $result = Media::asset('/css/debug.css?foo', 'css', array(
  243. 'library' => 'media_test', 'base' => '/base', 'check' => true
  244. ));
  245. $expected = '/base/css/debug.css?foo';
  246. $this->assertEqual($expected, $result);
  247. Libraries::remove('media_test');
  248. unlink("{$cssPath}/debug.css");
  249. foreach (array($cssPath, dirname($cssPath)) as $path) {
  250. rmdir($path);
  251. }
  252. }
  253. public function testRender() {
  254. $response = new Response();
  255. $response->type('json');
  256. $data = array('something');
  257. Media::render($response, $data);
  258. $result = $response->headers();
  259. $this->assertEqual(array('Content-Type: application/json; charset=UTF-8'), $result);
  260. $result = $response->body();
  261. $this->assertEqual($data, $result);
  262. }
  263. /**
  264. * Tests that a decode handler is not called when the Media type has none configured.
  265. */
  266. public function testNoDecode() {
  267. Media::type('my', 'text/x-my', array('decode' => false));
  268. $result = Media::decode('my', 'Hello World');
  269. $this->assertEqual(null, $result);
  270. }
  271. /**
  272. * Tests that types with decode handlers can properly decode content.
  273. */
  274. public function testDecode() {
  275. $data = array('movies' => array(
  276. array('name' => 'Shaun of the Dead', 'year' => 2004),
  277. array('name' => 'V for Vendetta', 'year' => 2005)
  278. ));
  279. $jsonEncoded = '{"movies":[{"name":"Shaun of the Dead","year":2004},';
  280. $jsonEncoded .= '{"name":"V for Vendetta","year":2005}]}';
  281. $result = Media::decode('json', $jsonEncoded);
  282. $this->assertEqual($data, $result);
  283. $formEncoded = 'movies%5B0%5D%5Bname%5D=Shaun+of+the+Dead&movies%5B0%5D%5Byear%5D=2004';
  284. $formEncoded .= '&movies%5B1%5D%5Bname%5D=V+for+Vendetta&movies%5B1%5D%5Byear%5D=2005';
  285. $result = Media::decode('form', $formEncoded);
  286. $this->assertEqual($data, $result);
  287. }
  288. public function testCustomEncodeHandler() {
  289. $response = new Response();
  290. Media::type('csv', 'application/csv', array(
  291. 'encode' => function($data) {
  292. ob_start();
  293. $out = fopen('php://output', 'w');
  294. foreach ($data as $record) {
  295. fputcsv($out, $record);
  296. }
  297. fclose($out);
  298. return ob_get_clean();
  299. }
  300. ));
  301. $data = array(
  302. array('John', 'Doe', '123 Main St.', 'Anytown, CA', '91724'),
  303. array('Jane', 'Doe', '124 Main St.', 'Anytown, CA', '91724')
  304. );
  305. $response->type('csv');
  306. Media::render($response, $data);
  307. $result = $response->body;
  308. $expected = 'John,Doe,"123 Main St.","Anytown, CA",91724' . "\n";
  309. $expected .= 'Jane,Doe,"124 Main St.","Anytown, CA",91724' . "\n";
  310. $this->assertEqual(array($expected), $result);
  311. $result = $response->headers['Content-Type'];
  312. $this->assertEqual('application/csv; charset=UTF-8', $result);
  313. }
  314. public function testEmptyEncode() {
  315. $handler = Media::type('empty', 'empty/encode');
  316. $this->assertNull(Media::encode($handler, array()));
  317. $handler = Media::type('empty', 'empty/encode', array(
  318. 'encode' => null
  319. ));
  320. $this->assertNull(Media::encode($handler, array()));
  321. $handler = Media::type('empty', 'empty/encode', array(
  322. 'encode' => false
  323. ));
  324. $this->assertNull(Media::encode($handler, array()));
  325. $handler = Media::type('empty', 'empty/encode', array(
  326. 'encode' => ""
  327. ));
  328. $this->assertNull(Media::encode($handler, array()));
  329. }
  330. /**
  331. * Tests that rendering plain text correctly returns the render data as-is.
  332. *
  333. * @return void
  334. */
  335. public function testPlainTextOutput() {
  336. $response = new Response();
  337. $response->type('text');
  338. Media::render($response, "Hello, world!");
  339. $result = $response->body;
  340. $this->assertEqual(array("Hello, world!"), $result);
  341. }
  342. /**
  343. * Tests that an exception is thrown for cases where an attempt is made to render content for
  344. * a type which is not registered.
  345. *
  346. * @return void
  347. */
  348. public function testUndhandledContent() {
  349. $response = new Response();
  350. $response->type('bad');
  351. $this->expectException("Unhandled media type `bad`.");
  352. Media::render($response, array('foo' => 'bar'));
  353. $result = $response->body();
  354. $this->assertNull($result);
  355. }
  356. /**
  357. * Tests that attempts to render a media type with no handler registered produces an
  358. * 'unhandled media type' exception, even if the type itself is a registered content type.
  359. *
  360. * @return void
  361. */
  362. public function testUnregisteredContentHandler() {
  363. $response = new Response();
  364. $response->type('xml');
  365. $this->expectException("Unhandled media type `xml`.");
  366. Media::render($response, array('foo' => 'bar'));
  367. $result = $response->body;
  368. $this->assertNull($result);
  369. }
  370. /**
  371. * Tests handling content type manually using parameters to `Media::render()`, for content types
  372. * that are registered but have no default handler.
  373. *
  374. * @return void
  375. */
  376. public function testManualContentHandling() {
  377. Media::type('custom', 'text/x-custom');
  378. $response = new Response();
  379. $response->type('custom');
  380. Media::render($response, 'Hello, world!', array(
  381. 'layout' => false,
  382. 'template' => false,
  383. 'encode' => function($data) { return "Message: {$data}"; }
  384. ));
  385. $result = $response->body;
  386. $expected = array("Message: Hello, world!");
  387. $this->assertEqual($expected, $result);
  388. $this->expectException("/Template not found/");
  389. Media::render($response, 'Hello, world!');
  390. $result = $response->body;
  391. $this->assertNull($result);
  392. }
  393. /**
  394. * Tests that parameters from the `Request` object passed into `render()` via
  395. * `$options['request']` are properly merged into the `$options` array passed to render
  396. * handlers.
  397. *
  398. * @return void
  399. */
  400. public function testRequestOptionMerging() {
  401. Media::type('custom', 'text/x-custom');
  402. $request = new Request();
  403. $request->params['foo'] = 'bar';
  404. $response = new Response();
  405. $response->type('custom');
  406. Media::render($response, null, compact('request') + array(
  407. 'layout' => false,
  408. 'template' => false,
  409. 'encode' => function($data, $handler) { return $handler['request']->foo; }
  410. ));
  411. $this->assertEqual(array('bar'), $response->body);
  412. }
  413. public function testMediaEncoding() {
  414. $data = array('hello', 'goodbye', 'foo' => array('bar', 'baz' => 'dib'));
  415. $expected = json_encode($data);
  416. $result = Media::encode('json', $data);
  417. $this->assertEqual($expected, $result);
  418. $this->assertEqual($result, Media::to('json', $data));
  419. $this->assertNull(Media::encode('badness', $data));
  420. $result = Media::decode('json', $expected);
  421. $this->assertEqual($data, $result);
  422. }
  423. public function testRenderWithOptionsMerging() {
  424. $base = Libraries::get(true, 'resources') . '/tmp';
  425. $this->skipIf(!is_writable($base), "Path `{$base}` is not writable.");
  426. $request = new Request();
  427. $request->params['controller'] = 'pages';
  428. $response = new Response();
  429. $response->type('html');
  430. $this->expectException('/Template not found/');
  431. Media::render($response, null, compact('request'));
  432. $this->_cleanUp();
  433. }
  434. public function testCustomWebroot() {
  435. Libraries::add('defaultStyleApp', array('path' => LITHIUM_APP_PATH, 'bootstrap' => false));
  436. $this->assertEqual(
  437. realpath(LITHIUM_APP_PATH . '/webroot'),
  438. realpath(Media::webroot('defaultStyleApp'))
  439. );
  440. Libraries::add('customWebRootApp', array(
  441. 'path' => LITHIUM_APP_PATH,
  442. 'webroot' => LITHIUM_APP_PATH,
  443. 'bootstrap' => false
  444. ));
  445. $this->assertEqual(LITHIUM_APP_PATH, Media::webroot('customWebRootApp'));
  446. Libraries::remove('defaultStyleApp');
  447. Libraries::remove('customWebRootApp');
  448. $this->assertNull(Media::webroot('defaultStyleApp'));
  449. }
  450. /**
  451. * Tests that the `Media` class' configuration can be reset to its default state.
  452. *
  453. * @return void
  454. */
  455. public function testStateReset() {
  456. $this->assertFalse(in_array('foo', Media::types()));
  457. Media::type('foo', 'text/x-foo');
  458. $this->assertTrue(in_array('foo', Media::types()));
  459. Media::reset();
  460. $this->assertFalse(in_array('foo', Media::types()));
  461. }
  462. public function testEncodeRecordSet() {
  463. $data = new RecordSet(array('data' => array(
  464. 1 => new Record(array('data' => array('id' => 1, 'foo' => 'bar'))),
  465. 2 => new Record(array('data' => array('id' => 2, 'foo' => 'baz'))),
  466. 3 => new Record(array('data' => array('id' => 3, 'baz' => 'dib')))
  467. )));
  468. $json = '{"1":{"id":1,"foo":"bar"},"2":{"id":2,"foo":"baz"},"3":{"id":3,"baz":"dib"}}';
  469. $this->assertEqual($json, Media::encode(array('encode' => 'json_encode'), $data));
  470. }
  471. public function testEncodeNotCallable() {
  472. $data = array('foo' => 'bar');
  473. $result = Media::encode(array('encode' => false), $data);
  474. $this->assertNull($result);
  475. }
  476. /**
  477. * Tests that calling `Media::type()` to retrieve the details of a type that is aliased to
  478. * another type, automatically resolves to the settings of the type being pointed at.
  479. *
  480. * @return void
  481. */
  482. public function testTypeAliasResolution() {
  483. $resolved = Media::type('text');
  484. $this->assertEqual(array('text/plain'), $resolved['content']);
  485. unset($resolved['options']['encode']);
  486. $result = Media::type('txt');
  487. unset($result['options']['encode']);
  488. $this->assertEqual($resolved, $result);
  489. }
  490. public function testQueryUndefinedAssetTypes() {
  491. $base = Media::path('index.php', 'generic');
  492. $result = Media::path('index.php', 'foo');
  493. $this->assertEqual($result, $base);
  494. $base = Media::asset('/bar', 'generic');
  495. $result = Media::asset('/bar', 'foo');
  496. $this->assertEqual($result, $base);
  497. }
  498. public function testGetLibraryWebroot() {
  499. $this->assertNull(Media::webroot('foobar'));
  500. Libraries::add('foobar', array('path' => __DIR__, 'webroot' => __DIR__));
  501. $this->assertEqual(__DIR__, Media::webroot('foobar'));
  502. Libraries::remove('foobar');
  503. $resources = Libraries::get(true, 'resources');
  504. $webroot = "{$resources}/media_test/webroot";
  505. $this->skipIf(!is_writable($resources), "Cannot write test app to resources directory.");
  506. if (!is_dir($webroot)) {
  507. mkdir($webroot, 0777, true);
  508. }
  509. Libraries::add('media_test', array('path' => "{$resources}/media_test"));
  510. $this->assertTrue(is_dir(Media::webroot('media_test')));
  511. Libraries::remove('media_test');
  512. rmdir($webroot);
  513. }
  514. /**
  515. * Tests that the `Response` object can be directly modified from a templating class or encode
  516. * function.
  517. *
  518. * @return void
  519. */
  520. public function testResponseModification() {
  521. Media::type('my', 'text/x-my', array('view' => 'lithium\tests\mocks\net\http\Template'));
  522. $response = new Response();
  523. Media::render($response, null, array('type' => 'my'));
  524. $this->assertEqual('Value', $response->headers('Custom'));
  525. }
  526. /**
  527. * Tests that `Media::asset()` will not prepend path strings with the base application path if
  528. * it has already been prepended.
  529. *
  530. * @return void
  531. */
  532. public function testDuplicateBasePathCheck() {
  533. $result = Media::asset('/foo/bar/image.jpg', 'image', array('base' => '/bar'));
  534. $this->assertEqual('/bar/foo/bar/image.jpg', $result);
  535. $result = Media::asset('/foo/bar/image.jpg', 'image', array('base' => '/foo/bar'));
  536. $this->assertEqual('/foo/bar/image.jpg', $result);
  537. $result = Media::asset('foo/bar/image.jpg', 'image', array('base' => 'foo'));
  538. $this->assertEqual('foo/img/foo/bar/image.jpg', $result);
  539. $result = Media::asset('/foo/bar/image.jpg', 'image', array('base' => ''));
  540. $this->assertEqual('/foo/bar/image.jpg', $result);
  541. }
  542. public function testContentNegotiationByType() {
  543. $this->assertEqual('html', Media::type('text/html'));
  544. Media::type('jsonp', 'text/html', array(
  545. 'conditions' => array('type' => true)
  546. ));
  547. $this->assertEqual(array('jsonp', 'html'), Media::type('text/html'));
  548. $config = array('env' => array('HTTP_ACCEPT' => 'text/html,text/plain;q=0.5'));
  549. $request = new Request($config);
  550. $request->params = array('type' => 'jsonp');
  551. $this->assertEqual('jsonp', Media::negotiate($request));
  552. $request = new Request($config);
  553. $this->assertEqual('html', Media::negotiate($request));
  554. }
  555. public function testContentNegotiationByUserAgent() {
  556. Media::type('iphone', 'application/xhtml+xml', array(
  557. 'conditions' => array('mobile' => true)
  558. ));
  559. $request = new Request(array('env' => array(
  560. 'HTTP_USER_AGENT' => 'Safari',
  561. 'HTTP_ACCEPT' => 'application/xhtml+xml,text/html'
  562. )));
  563. $this->assertEqual('html', Media::negotiate($request));
  564. $request = new Request(array('env' => array(
  565. 'HTTP_USER_AGENT' => 'iPhone',
  566. 'HTTP_ACCEPT' => 'application/xhtml+xml,text/html'
  567. )));
  568. $this->assertEqual('iphone', Media::negotiate($request));
  569. }
  570. /**
  571. * Tests that empty asset paths correctly return the base path for the asset type, and don't
  572. * generate notices or errors.
  573. */
  574. public function testEmptyAssetPaths() {
  575. $this->assertEqual('/img/', Media::asset('', 'image'));
  576. $this->assertEqual('/css/.css', Media::asset('', 'css'));
  577. $this->assertEqual('/js/.js', Media::asset('', 'js'));
  578. $this->assertEqual('/', Media::asset('', 'generic'));
  579. }
  580. }
  581. ?>