MediaViewTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. /**
  3. * MediaViewTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.View
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('Controller', 'Controller');
  20. App::uses('MediaView', 'View');
  21. App::uses('CakeResponse', 'Network');
  22. /**
  23. * MediaViewTest class
  24. *
  25. * @package Cake.Test.Case.View
  26. */
  27. class MediaViewTest extends CakeTestCase {
  28. /**
  29. * setUp method
  30. *
  31. * @return void
  32. */
  33. public function setUp() {
  34. parent::setUp();
  35. $this->MediaView = new MediaView();
  36. $this->MediaView->response = $this->getMock('CakeResponse', array(
  37. '_isActive',
  38. '_clearBuffer',
  39. '_flushBuffer',
  40. 'type',
  41. 'header',
  42. 'download'
  43. ));
  44. }
  45. /**
  46. * tearDown method
  47. *
  48. * @return void
  49. */
  50. public function tearDown() {
  51. parent::tearDown();
  52. unset($this->MediaView);
  53. }
  54. /**
  55. * tests that rendering a file that does not exists throws an exception
  56. *
  57. * @expectedException NotFoundException
  58. * @return void
  59. */
  60. public function testRenderNotFound() {
  61. $this->MediaView->viewVars = array(
  62. 'path' => '/some/missing/folder',
  63. 'id' => 'file.jpg'
  64. );
  65. $this->MediaView->render();
  66. }
  67. /**
  68. * testRender method
  69. *
  70. * @return void
  71. */
  72. public function testRender() {
  73. $this->MediaView->viewVars = array(
  74. 'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS . 'css' . DS,
  75. 'id' => 'test_asset.css'
  76. );
  77. $this->MediaView->response->expects($this->exactly(1))
  78. ->method('_isActive')
  79. ->will($this->returnValue(true));
  80. $this->MediaView->response->expects($this->exactly(1))
  81. ->method('type')
  82. ->with('css')
  83. ->will($this->returnArgument(0));
  84. $this->MediaView->response->expects($this->at(0))
  85. ->method('header')
  86. ->with(array(
  87. 'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT',
  88. 'Cache-Control' => 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0',
  89. 'Last-Modified' => gmdate('D, d M Y H:i:s', time()) . ' GMT'
  90. ));
  91. $this->MediaView->response->expects($this->at(2))
  92. ->method('header')
  93. ->with('Content-Length', 38);
  94. $this->MediaView->response->expects($this->once())->method('_clearBuffer');
  95. $this->MediaView->response->expects($this->exactly(1))
  96. ->method('_isActive')
  97. ->will($this->returnValue(true));
  98. $this->MediaView->response->expects($this->once())->method('_flushBuffer');
  99. ob_start();
  100. $result = $this->MediaView->render();
  101. $output = ob_get_clean();
  102. $this->assertEquals("/* this is the test asset css file */\n", $output);
  103. $this->assertTrue($result !== false);
  104. }
  105. /**
  106. * testRenderWithUnknownFileTypeGeneric method
  107. *
  108. * @return void
  109. */
  110. public function testRenderWithUnknownFileTypeGeneric() {
  111. $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
  112. $_SERVER['HTTP_USER_AGENT'] = 'Some generic browser';
  113. $this->MediaView->viewVars = array(
  114. 'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS,
  115. 'id' => 'no_section.ini'
  116. );
  117. $this->MediaView->response->expects($this->exactly(1))
  118. ->method('type')
  119. ->with('ini')
  120. ->will($this->returnValue(false));
  121. $this->MediaView->response->expects($this->at(0))
  122. ->method('header')
  123. ->with($this->logicalAnd(
  124. $this->arrayHasKey('Last-Modified'),
  125. $this->arrayHasKey('Expires'),
  126. $this->arrayHasKey('Cache-Control'),
  127. $this->contains('Mon, 26 Jul 1997 05:00:00 GMT'),
  128. $this->contains('no-store, no-cache, must-revalidate, post-check=0, pre-check=0')
  129. ));
  130. $this->MediaView->response->expects($this->once())
  131. ->method('download')
  132. ->with('no_section.ini');
  133. $this->MediaView->response->expects($this->at(3))
  134. ->method('header')
  135. ->with('Accept-Ranges', 'bytes');
  136. $this->MediaView->response->expects($this->at(4))
  137. ->method('header')
  138. ->with('Content-Length', 35);
  139. $this->MediaView->response->expects($this->once())->method('_clearBuffer');
  140. $this->MediaView->response->expects($this->exactly(1))
  141. ->method('_isActive')
  142. ->will($this->returnValue(true));
  143. $this->MediaView->response->expects($this->once())->method('_flushBuffer');
  144. ob_start();
  145. $result = $this->MediaView->render();
  146. $output = ob_get_clean();
  147. $this->assertEquals("some_key = some_value\nbool_key = 1\n", $output);
  148. $this->assertTrue($result !== false);
  149. if ($currentUserAgent !== null) {
  150. $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent;
  151. }
  152. }
  153. /**
  154. * testRenderWithUnknownFileTypeOpera method
  155. *
  156. * @return void
  157. */
  158. public function testRenderWithUnknownFileTypeOpera() {
  159. $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
  160. $_SERVER['HTTP_USER_AGENT'] = 'Opera/9.80 (Windows NT 6.0; U; en) Presto/2.8.99 Version/11.10';
  161. $this->MediaView->viewVars = array(
  162. 'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS,
  163. 'id' => 'no_section.ini',
  164. );
  165. $this->MediaView->response->expects($this->at(0))
  166. ->method('header')
  167. ->with(array(
  168. 'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT',
  169. 'Cache-Control' => 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0',
  170. 'Last-Modified' => gmdate('D, d M Y H:i:s', time()) . ' GMT'
  171. ));
  172. $this->MediaView->response->expects($this->at(1))
  173. ->method('type')
  174. ->with('ini')
  175. ->will($this->returnValue(false));
  176. $this->MediaView->response->expects($this->at(2))
  177. ->method('type')
  178. ->with('application/octetstream')
  179. ->will($this->returnValue(false));
  180. $this->MediaView->response->expects($this->at(3))
  181. ->method('download')
  182. ->with('no_section.ini');
  183. $this->MediaView->response->expects($this->at(4))
  184. ->method('header')
  185. ->with('Accept-Ranges', 'bytes');
  186. $this->MediaView->response->expects($this->at(5))
  187. ->method('header')
  188. ->with('Content-Length', 35);
  189. $this->MediaView->response->expects($this->once())->method('_clearBuffer');
  190. $this->MediaView->response->expects($this->exactly(1))
  191. ->method('_isActive')
  192. ->will($this->returnValue(true));
  193. $this->MediaView->response->expects($this->once())->method('_flushBuffer');
  194. ob_start();
  195. $result = $this->MediaView->render();
  196. $output = ob_get_clean();
  197. $this->assertEquals("some_key = some_value\nbool_key = 1\n", $output);
  198. $this->assertTrue($result !== false);
  199. if ($currentUserAgent !== null) {
  200. $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent;
  201. }
  202. }
  203. /**
  204. * testRenderWithUnknownFileTypeIE method
  205. *
  206. * @return void
  207. */
  208. public function testRenderWithUnknownFileTypeIE() {
  209. $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
  210. $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; Media Center PC 4.0; SLCC1; .NET CLR 3.0.04320)';
  211. $this->MediaView->viewVars = array(
  212. 'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS,
  213. 'id' => 'no_section.ini',
  214. 'name' => 'config'
  215. );
  216. $this->MediaView->response->expects($this->at(0))
  217. ->method('header')
  218. ->with(array(
  219. 'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT',
  220. 'Cache-Control' => 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0',
  221. 'Last-Modified' => gmdate('D, d M Y H:i:s', time()) . ' GMT'
  222. ));
  223. $this->MediaView->response->expects($this->at(1))
  224. ->method('type')
  225. ->with('ini')
  226. ->will($this->returnValue(false));
  227. $this->MediaView->response->expects($this->at(2))
  228. ->method('type')
  229. ->with('application/force-download')
  230. ->will($this->returnValue(false));
  231. $this->MediaView->response->expects($this->at(3))
  232. ->method('download')
  233. ->with('config.ini');
  234. $this->MediaView->response->expects($this->at(4))
  235. ->method('header')
  236. ->with('Accept-Ranges', 'bytes');
  237. $this->MediaView->response->expects($this->at(5))
  238. ->method('header')
  239. ->with('Content-Length', 35);
  240. $this->MediaView->response->expects($this->once())->method('_clearBuffer');
  241. $this->MediaView->response->expects($this->exactly(1))
  242. ->method('_isActive')
  243. ->will($this->returnValue(true));
  244. $this->MediaView->response->expects($this->once())->method('_flushBuffer');
  245. ob_start();
  246. $result = $this->MediaView->render();
  247. $output = ob_get_clean();
  248. $this->assertEquals("some_key = some_value\nbool_key = 1\n", $output);
  249. $this->assertTrue($result !== false);
  250. if ($currentUserAgent !== null) {
  251. $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent;
  252. }
  253. }
  254. /**
  255. * testConnectionAbortedOnBuffering method
  256. *
  257. * @return void
  258. */
  259. public function testConnectionAbortedOnBuffering() {
  260. $this->MediaView->viewVars = array(
  261. 'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS . 'css' . DS,
  262. 'id' => 'test_asset.css'
  263. );
  264. $this->MediaView->response->expects($this->any())
  265. ->method('type')
  266. ->with('css')
  267. ->will($this->returnArgument(0));
  268. $this->MediaView->response->expects($this->at(1))
  269. ->method('_isActive')
  270. ->will($this->returnValue(false));
  271. $this->MediaView->response->expects($this->once())->method('_clearBuffer');
  272. $this->MediaView->response->expects($this->never())->method('_flushBuffer');
  273. $this->MediaView->render();
  274. }
  275. /**
  276. * Test downloading files with UPPERCASE extensions.
  277. *
  278. * @return void
  279. */
  280. public function testRenderUpperExtension() {
  281. $this->MediaView->viewVars = array(
  282. 'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS . 'img' . DS,
  283. 'id' => 'test_2.JPG'
  284. );
  285. $this->MediaView->response->expects($this->any())
  286. ->method('type')
  287. ->with('jpg')
  288. ->will($this->returnArgument(0));
  289. $this->MediaView->response->expects($this->at(0))
  290. ->method('_isActive')
  291. ->will($this->returnValue(true));
  292. $this->MediaView->render();
  293. }
  294. }