| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- <?php
- /**
- * MediaViewTest file
- *
- * PHP 5
- *
- * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
- * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * Redistributions of files must retain the above copyright notice
- *
- * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
- * @package Cake.Test.Case.View
- * @since CakePHP(tm) v 1.2.0.4206
- * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
- */
- App::uses('Controller', 'Controller');
- App::uses('MediaView', 'View');
- App::uses('CakeResponse', 'Network');
- /**
- * MediaViewTest class
- *
- * @package Cake.Test.Case.View
- */
- class MediaViewTest extends CakeTestCase {
- /**
- * setUp method
- *
- * @return void
- */
- public function setUp() {
- parent::setUp();
- $this->MediaView = new MediaView();
- $this->MediaView->response = $this->getMock('CakeResponse', array(
- '_isActive',
- '_clearBuffer',
- '_flushBuffer',
- 'type',
- 'header',
- 'download'
- ));
- }
- /**
- * tearDown method
- *
- * @return void
- */
- public function tearDown() {
- parent::tearDown();
- unset($this->MediaView);
- }
- /**
- * tests that rendering a file that does not exists throws an exception
- *
- * @expectedException NotFoundException
- * @return void
- */
- public function testRenderNotFound() {
- $this->MediaView->viewVars = array(
- 'path' => '/some/missing/folder',
- 'id' => 'file.jpg'
- );
- $this->MediaView->render();
- }
- /**
- * testRender method
- *
- * @return void
- */
- public function testRender() {
- $this->MediaView->viewVars = array(
- 'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS . 'css' . DS,
- 'id' => 'test_asset.css'
- );
- $this->MediaView->response->expects($this->exactly(1))
- ->method('_isActive')
- ->will($this->returnValue(true));
- $this->MediaView->response->expects($this->exactly(1))
- ->method('type')
- ->with('css')
- ->will($this->returnArgument(0));
- $this->MediaView->response->expects($this->at(0))
- ->method('header')
- ->with(array(
- 'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT',
- 'Cache-Control' => 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0',
- 'Last-Modified' => gmdate('D, d M Y H:i:s', time()) . ' GMT'
- ));
- $this->MediaView->response->expects($this->at(2))
- ->method('header')
- ->with('Content-Length', 38);
- $this->MediaView->response->expects($this->once())->method('_clearBuffer');
- $this->MediaView->response->expects($this->exactly(1))
- ->method('_isActive')
- ->will($this->returnValue(true));
- $this->MediaView->response->expects($this->once())->method('_flushBuffer');
- ob_start();
- $result = $this->MediaView->render();
- $output = ob_get_clean();
- $this->assertEquals("/* this is the test asset css file */\n", $output);
- $this->assertTrue($result !== false);
- }
- /**
- * testRenderWithUnknownFileTypeGeneric method
- *
- * @return void
- */
- public function testRenderWithUnknownFileTypeGeneric() {
- $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
- $_SERVER['HTTP_USER_AGENT'] = 'Some generic browser';
- $this->MediaView->viewVars = array(
- 'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS,
- 'id' => 'no_section.ini'
- );
- $this->MediaView->response->expects($this->exactly(1))
- ->method('type')
- ->with('ini')
- ->will($this->returnValue(false));
- $this->MediaView->response->expects($this->at(0))
- ->method('header')
- ->with($this->logicalAnd(
- $this->arrayHasKey('Last-Modified'),
- $this->arrayHasKey('Expires'),
- $this->arrayHasKey('Cache-Control'),
- $this->contains('Mon, 26 Jul 1997 05:00:00 GMT'),
- $this->contains('no-store, no-cache, must-revalidate, post-check=0, pre-check=0')
- ));
- $this->MediaView->response->expects($this->once())
- ->method('download')
- ->with('no_section.ini');
- $this->MediaView->response->expects($this->at(3))
- ->method('header')
- ->with('Accept-Ranges', 'bytes');
- $this->MediaView->response->expects($this->at(4))
- ->method('header')
- ->with('Content-Length', 35);
- $this->MediaView->response->expects($this->once())->method('_clearBuffer');
- $this->MediaView->response->expects($this->exactly(1))
- ->method('_isActive')
- ->will($this->returnValue(true));
- $this->MediaView->response->expects($this->once())->method('_flushBuffer');
- ob_start();
- $result = $this->MediaView->render();
- $output = ob_get_clean();
- $this->assertEquals("some_key = some_value\nbool_key = 1\n", $output);
- $this->assertTrue($result !== false);
- if ($currentUserAgent !== null) {
- $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent;
- }
- }
- /**
- * testRenderWithUnknownFileTypeOpera method
- *
- * @return void
- */
- public function testRenderWithUnknownFileTypeOpera() {
- $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
- $_SERVER['HTTP_USER_AGENT'] = 'Opera/9.80 (Windows NT 6.0; U; en) Presto/2.8.99 Version/11.10';
- $this->MediaView->viewVars = array(
- 'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS,
- 'id' => 'no_section.ini',
- );
- $this->MediaView->response->expects($this->at(0))
- ->method('header')
- ->with(array(
- 'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT',
- 'Cache-Control' => 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0',
- 'Last-Modified' => gmdate('D, d M Y H:i:s', time()) . ' GMT'
- ));
- $this->MediaView->response->expects($this->at(1))
- ->method('type')
- ->with('ini')
- ->will($this->returnValue(false));
- $this->MediaView->response->expects($this->at(2))
- ->method('type')
- ->with('application/octetstream')
- ->will($this->returnValue(false));
- $this->MediaView->response->expects($this->at(3))
- ->method('download')
- ->with('no_section.ini');
- $this->MediaView->response->expects($this->at(4))
- ->method('header')
- ->with('Accept-Ranges', 'bytes');
- $this->MediaView->response->expects($this->at(5))
- ->method('header')
- ->with('Content-Length', 35);
- $this->MediaView->response->expects($this->once())->method('_clearBuffer');
- $this->MediaView->response->expects($this->exactly(1))
- ->method('_isActive')
- ->will($this->returnValue(true));
- $this->MediaView->response->expects($this->once())->method('_flushBuffer');
- ob_start();
- $result = $this->MediaView->render();
- $output = ob_get_clean();
- $this->assertEquals("some_key = some_value\nbool_key = 1\n", $output);
- $this->assertTrue($result !== false);
- if ($currentUserAgent !== null) {
- $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent;
- }
- }
- /**
- * testRenderWithUnknownFileTypeIE method
- *
- * @return void
- */
- public function testRenderWithUnknownFileTypeIE() {
- $currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
- $_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)';
- $this->MediaView->viewVars = array(
- 'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS,
- 'id' => 'no_section.ini',
- 'name' => 'config'
- );
- $this->MediaView->response->expects($this->at(0))
- ->method('header')
- ->with(array(
- 'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT',
- 'Cache-Control' => 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0',
- 'Last-Modified' => gmdate('D, d M Y H:i:s', time()) . ' GMT'
- ));
- $this->MediaView->response->expects($this->at(1))
- ->method('type')
- ->with('ini')
- ->will($this->returnValue(false));
- $this->MediaView->response->expects($this->at(2))
- ->method('type')
- ->with('application/force-download')
- ->will($this->returnValue(false));
- $this->MediaView->response->expects($this->at(3))
- ->method('download')
- ->with('config.ini');
- $this->MediaView->response->expects($this->at(4))
- ->method('header')
- ->with('Accept-Ranges', 'bytes');
- $this->MediaView->response->expects($this->at(5))
- ->method('header')
- ->with('Content-Length', 35);
- $this->MediaView->response->expects($this->once())->method('_clearBuffer');
- $this->MediaView->response->expects($this->exactly(1))
- ->method('_isActive')
- ->will($this->returnValue(true));
- $this->MediaView->response->expects($this->once())->method('_flushBuffer');
- ob_start();
- $result = $this->MediaView->render();
- $output = ob_get_clean();
- $this->assertEquals("some_key = some_value\nbool_key = 1\n", $output);
- $this->assertTrue($result !== false);
- if ($currentUserAgent !== null) {
- $_SERVER['HTTP_USER_AGENT'] = $currentUserAgent;
- }
- }
- /**
- * testConnectionAbortedOnBuffering method
- *
- * @return void
- */
- public function testConnectionAbortedOnBuffering() {
- $this->MediaView->viewVars = array(
- 'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS . 'css' . DS,
- 'id' => 'test_asset.css'
- );
- $this->MediaView->response->expects($this->any())
- ->method('type')
- ->with('css')
- ->will($this->returnArgument(0));
- $this->MediaView->response->expects($this->at(1))
- ->method('_isActive')
- ->will($this->returnValue(false));
- $this->MediaView->response->expects($this->once())->method('_clearBuffer');
- $this->MediaView->response->expects($this->never())->method('_flushBuffer');
- $this->MediaView->render();
- }
- /**
- * Test downloading files with UPPERCASE extensions.
- *
- * @return void
- */
- public function testRenderUpperExtension() {
- $this->MediaView->viewVars = array(
- 'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS . 'img' . DS,
- 'id' => 'test_2.JPG'
- );
- $this->MediaView->response->expects($this->any())
- ->method('type')
- ->with('jpg')
- ->will($this->returnArgument(0));
- $this->MediaView->response->expects($this->at(0))
- ->method('_isActive')
- ->will($this->returnValue(true));
- $this->MediaView->render();
- }
- }
|