PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Test/Case/View/MediaViewTest.php

http://github.com/cakephp/cakephp
PHP | 379 lines | 255 code | 51 blank | 73 comment | 3 complexity | 3e177687d853d6df885e754df5b14d60 MD5 | raw file
Possible License(s): JSON
  1. <?php
  2. /**
  3. * MediaViewTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2011, 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-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing 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. $controller = new Controller();
  36. $this->MediaView = $this->getMock('MediaView', array('_isActive', '_clearBuffer', '_flushBuffer'));
  37. $this->MediaView->response = $this->getMock('CakeResponse');
  38. }
  39. /**
  40. * endTest method
  41. *
  42. * @return void
  43. */
  44. public function tearDown() {
  45. parent::tearDown();
  46. unset($this->MediaView);
  47. }
  48. /**
  49. * tests that rendering a file that does not exists throws an exception
  50. *
  51. * @expectedException NotFoundException
  52. * @return void
  53. */
  54. public function testRenderNotFound() {
  55. $this->MediaView->viewVars = array(
  56. 'path' => '/some/missing/folder',
  57. 'id' => 'file.jpg'
  58. );
  59. $this->MediaView->render();
  60. }
  61. /**
  62. * testRender method
  63. *
  64. * @return void
  65. */
  66. public function testRender() {
  67. $this->MediaView->viewVars = array(
  68. 'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS .'css' . DS,
  69. 'id' => 'test_asset.css',
  70. 'extension' => 'css',
  71. );
  72. $this->MediaView->expects($this->exactly(2))
  73. ->method('_isActive')
  74. ->will($this->returnValue(true));
  75. $this->MediaView->response->expects($this->exactly(1))
  76. ->method('type')
  77. ->with('css')
  78. ->will($this->returnArgument(0));
  79. $this->MediaView->response->expects($this->at(1))
  80. ->method('header')
  81. ->with(array(
  82. 'Date' => gmdate('D, d M Y H:i:s', time()) . ' GMT',
  83. 'Expires' => '0',
  84. 'Cache-Control' => 'private, must-revalidate, post-check=0, pre-check=0',
  85. 'Pragma' => 'no-cache'
  86. ));
  87. $this->MediaView->response->expects($this->at(2))
  88. ->method('header')
  89. ->with(array(
  90. 'Content-Length' => 31
  91. ));
  92. $this->MediaView->response->expects($this->once())->method('send');
  93. $this->MediaView->expects($this->once())->method('_clearBuffer');
  94. $this->MediaView->expects($this->once())->method('_flushBuffer');
  95. ob_start();
  96. $result = $this->MediaView->render();
  97. $output = ob_get_clean();
  98. $this->assertEquals('this is the test asset css file', $output);
  99. $this->assertTrue($result !== false);
  100. }
  101. /**
  102. * testRenderWithUnknownFileTypeGeneric method
  103. *
  104. * @return void
  105. */
  106. public function testRenderWithUnknownFileTypeGeneric() {
  107. $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
  108. $_SERVER['HTTP_USER_AGENT'] = 'Some generic browser';
  109. $this->MediaView->viewVars = array(
  110. 'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS,
  111. 'id' => 'no_section.ini',
  112. 'extension' => 'ini',
  113. );
  114. $this->MediaView->expects($this->exactly(2))
  115. ->method('_isActive')
  116. ->will($this->returnValue(true));
  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(1))
  122. ->method('header')
  123. ->with(array(
  124. 'Date' => gmdate('D, d M Y H:i:s', time()) . ' GMT',
  125. 'Expires' => '0',
  126. 'Cache-Control' => 'private, must-revalidate, post-check=0, pre-check=0',
  127. 'Pragma' => 'no-cache'
  128. ));
  129. $this->MediaView->response->expects($this->once())
  130. ->method('download')
  131. ->with('no_section.ini');
  132. $this->MediaView->response->expects($this->at(3))
  133. ->method('header')
  134. ->with(array(
  135. 'Accept-Ranges' => 'bytes'
  136. ));
  137. $this->MediaView->response->expects($this->at(4))
  138. ->method('header')
  139. ->with('Content-Length', 35);
  140. $this->MediaView->response->expects($this->once())->method('send');
  141. $this->MediaView->expects($this->once())->method('_clearBuffer');
  142. $this->MediaView->expects($this->once())->method('_flushBuffer');
  143. ob_start();
  144. $result = $this->MediaView->render();
  145. $output = ob_get_clean();
  146. $this->assertEquals("some_key = some_value\nbool_key = 1\n", $output);
  147. $this->assertTrue($result !== false);
  148. if ($currentUserAgent !== null) {
  149. $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent;
  150. }
  151. }
  152. /**
  153. * testRenderWithUnknownFileTypeOpera method
  154. *
  155. * @return void
  156. */
  157. public function testRenderWithUnknownFileTypeOpera() {
  158. $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
  159. $_SERVER['HTTP_USER_AGENT'] = 'Opera/9.80 (Windows NT 6.0; U; en) Presto/2.8.99 Version/11.10';
  160. $this->MediaView->viewVars = array(
  161. 'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS,
  162. 'id' => 'no_section.ini',
  163. 'extension' => 'ini',
  164. );
  165. $this->MediaView->expects($this->exactly(2))
  166. ->method('_isActive')
  167. ->will($this->returnValue(true));
  168. $this->MediaView->response->expects($this->at(0))
  169. ->method('type')
  170. ->with('ini')
  171. ->will($this->returnValue(false));
  172. $this->MediaView->response->expects($this->at(1))
  173. ->method('header')
  174. ->with(array(
  175. 'Date' => gmdate('D, d M Y H:i:s', time()) . ' GMT',
  176. 'Expires' => '0',
  177. 'Cache-Control' => 'private, must-revalidate, post-check=0, pre-check=0',
  178. 'Pragma' => 'no-cache'
  179. ));
  180. $this->MediaView->response->expects($this->at(2))
  181. ->method('type')
  182. ->with('application/octetstream')
  183. ->will($this->returnValue(false));
  184. $this->MediaView->response->expects($this->once())
  185. ->method('download')
  186. ->with('no_section.ini');
  187. $this->MediaView->response->expects($this->at(4))
  188. ->method('header')
  189. ->with(array(
  190. 'Accept-Ranges' => 'bytes'
  191. ));
  192. $this->MediaView->response->expects($this->at(5))
  193. ->method('header')
  194. ->with('Content-Length', 35);
  195. $this->MediaView->response->expects($this->once())->method('send');
  196. $this->MediaView->expects($this->once())->method('_clearBuffer');
  197. $this->MediaView->expects($this->once())->method('_flushBuffer');
  198. ob_start();
  199. $result = $this->MediaView->render();
  200. $output = ob_get_clean();
  201. $this->assertEquals("some_key = some_value\nbool_key = 1\n", $output);
  202. $this->assertTrue($result !== false);
  203. if ($currentUserAgent !== null) {
  204. $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent;
  205. }
  206. }
  207. /**
  208. * testRenderWithUnknownFileTypeIE method
  209. *
  210. * @return void
  211. */
  212. public function testRenderWithUnknownFileTypeIE() {
  213. $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
  214. $_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)';
  215. $this->MediaView->viewVars = array(
  216. 'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS,
  217. 'id' => 'no_section.ini',
  218. 'extension' => 'ini',
  219. );
  220. $this->MediaView->expects($this->exactly(2))
  221. ->method('_isActive')
  222. ->will($this->returnValue(true));
  223. $this->MediaView->response->expects($this->at(0))
  224. ->method('type')
  225. ->with('ini')
  226. ->will($this->returnValue(false));
  227. $this->MediaView->response->expects($this->at(1))
  228. ->method('header')
  229. ->with(array(
  230. 'Date' => gmdate('D, d M Y H:i:s', time()) . ' GMT',
  231. 'Expires' => '0',
  232. 'Cache-Control' => 'private, must-revalidate, post-check=0, pre-check=0',
  233. 'Pragma' => 'no-cache'
  234. ));
  235. $this->MediaView->response->expects($this->at(2))
  236. ->method('type')
  237. ->with('application/force-download')
  238. ->will($this->returnValue(false));
  239. $this->MediaView->response->expects($this->once())
  240. ->method('download')
  241. ->with('no_section.ini');
  242. $this->MediaView->response->expects($this->at(4))
  243. ->method('header')
  244. ->with(array(
  245. 'Accept-Ranges' => 'bytes'
  246. ));
  247. $this->MediaView->response->expects($this->at(5))
  248. ->method('header')
  249. ->with('Content-Length', 35);
  250. $this->MediaView->response->expects($this->once())->method('send');
  251. $this->MediaView->expects($this->once())->method('_clearBuffer');
  252. $this->MediaView->expects($this->once())->method('_flushBuffer');
  253. ob_start();
  254. $result = $this->MediaView->render();
  255. $output = ob_get_clean();
  256. $this->assertEquals("some_key = some_value\nbool_key = 1\n", $output);
  257. $this->assertTrue($result !== false);
  258. if ($currentUserAgent !== null) {
  259. $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent;
  260. }
  261. }
  262. /**
  263. * testConnectionAborted method
  264. *
  265. * @return void
  266. */
  267. public function testConnectionAborted() {
  268. $this->MediaView->viewVars = array(
  269. 'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS .'css' . DS,
  270. 'id' => 'test_asset.css',
  271. 'extension' => 'css',
  272. );
  273. $this->MediaView->expects($this->once())
  274. ->method('_isActive')
  275. ->will($this->returnValue(false));
  276. $this->MediaView->response->expects($this->never())
  277. ->method('type');
  278. $result = $this->MediaView->render();
  279. $this->assertFalse($result);
  280. }
  281. /**
  282. * testConnectionAbortedOnBuffering method
  283. *
  284. * @return void
  285. */
  286. public function testConnectionAbortedOnBuffering() {
  287. $this->MediaView->viewVars = array(
  288. 'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS .'css' . DS,
  289. 'id' => 'test_asset.css',
  290. 'extension' => 'css',
  291. );
  292. $this->MediaView->expects($this->at(0))
  293. ->method('_isActive')
  294. ->will($this->returnValue(true));
  295. $this->MediaView->response->expects($this->any())
  296. ->method('type')
  297. ->with('css')
  298. ->will($this->returnArgument(0));
  299. $this->MediaView->expects($this->at(1))
  300. ->method('_isActive')
  301. ->will($this->returnValue(false));
  302. $this->MediaView->response->expects($this->once())->method('send');
  303. $this->MediaView->expects($this->once())->method('_clearBuffer');
  304. $this->MediaView->expects($this->never())->method('_flushBuffer');
  305. $result = $this->MediaView->render();
  306. $this->assertFalse($result);
  307. }
  308. /**
  309. * Test downloading files with UPPERCASE extensions.
  310. *
  311. * @return void
  312. */
  313. function testRenderUpperExtesnion() {
  314. $this->MediaView->viewVars = array(
  315. 'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS .'img' . DS,
  316. 'id' => 'test_2.JPG',
  317. 'extension' => 'JPG',
  318. );
  319. $this->MediaView->response->expects($this->any())
  320. ->method('type')
  321. ->with('jpg')
  322. ->will($this->returnArgument(0));
  323. $this->MediaView->expects($this->at(0))
  324. ->method('_isActive')
  325. ->will($this->returnValue(true));
  326. $this->MediaView->render();
  327. }
  328. }