diff --git a/cake/libs/view/media.php b/cake/libs/view/media.php index 8e88fb321..b71d6c0bc 100644 --- a/cake/libs/view/media.php +++ b/cake/libs/view/media.php @@ -206,17 +206,16 @@ class MediaView extends View { 'Content-Length: ' . $fileSize)); } $this->_output(); - @ob_end_clean(); + $this->_clearBuffer(); - while (!feof($handle) && connection_status() == 0 && !connection_aborted()) { + while (!feof($handle) && $this->_isActive()) { set_time_limit(0); $buffer = fread($handle, $chunkSize); echo $buffer; - @flush(); - @ob_flush(); + $this->_flushBuffer(); } fclose($handle); - exit(0); + return; } return false; } @@ -252,5 +251,32 @@ class MediaView extends View { header($header, $value[$header]); } } + +/** + * Returns true if connectios is still active + * @return boolean + * @access protected + */ + function _isActive() { + return connection_status() == 0 && !connection_aborted(); + } + +/** + * Clears the contents of the topmost output buffer and discards them + * @return boolean + * @access protected + */ + function _clearBuffer() { + return @ob_end_clean(); + } + +/** + * Flushes the contents of the output buffer + * @access protected + */ + function _flushBuffer() { + @flush(); + @ob_flush(); + } } -?> \ No newline at end of file +?> diff --git a/cake/tests/cases/libs/view/media.test.php b/cake/tests/cases/libs/view/media.test.php new file mode 100644 index 000000000..5e1f382b2 --- /dev/null +++ b/cake/tests/cases/libs/view/media.test.php @@ -0,0 +1,181 @@ + + * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) + * + * Licensed under The Open Group Test Suite License + * Redistributions of files must retain the above copyright notice. + * + * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) + * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests + * @package cake + * @subpackage cake.tests.cases.libs + * @since CakePHP(tm) v 1.2.0.4206 + * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License + */ +App::import('Core', array('Media', 'Controller')); + +if (!class_exists('ErrorHandler')) { + App::import('Core', array('Error')); +} +if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) { + define('CAKEPHP_UNIT_TEST_EXECUTION', 1); +} + +/** + * ThemePostsController class + * + * @package cake + * @subpackage cake.tests.cases.libs.view + */ +class MediaController extends Controller { + +/** + * name property + * + * @var string 'Media' + * @access public + */ + var $name = 'Media'; + +/** + * index download + * + * @access public + * @return void + */ + function download() { + $path = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'vendors' . DS .'css' . DS; + $id = 'test_asset.css'; + $extension = 'css'; + $this->set(compact('path', 'id', 'extension')); + } +} + +/** + * TestMediaView class + * + * @package cake + * @subpackage cake.tests.cases.libs.view + */ +class TestMediaView extends MediaView { + +/** + * headers public property as a copy from protected property _headers + * + * @var array + * @access public + */ + var $headers = array(); + +/** + * active property to mock the status of a remote conenction + * + * @var boolean true + * @access public + */ + var $active = true; + + function _output() { + $this->headers = $this->_headers; + } + +/** + * _isActive method. Usted de $active property to mock an active (true) connection, + * or an aborted (false) one + * + * @access protected + * @return void + */ + function _isActive() { + return $this->active; + } + +/** + * _clearBuffer method + * + * @access protected + * @return void + */ + function _clearBuffer() { + return true; + } + +/** + * _flushBuffer method + * + * @access protected + * @return void + */ + function _flushBuffer() { + } +} + +/** + * ThemeViewTest class + * + * @package cake + * @subpackage cake.tests.cases.libs + */ +class MediaViewTest extends CakeTestCase { + +/** + * startTest method + * + * @access public + * @return void + */ + function startTest() { + Router::reload(); + $this->Controller =& new Controller(); + $this->MediaController =& new MediaController(); + $this->MediaController->viewPath = 'posts'; + $this->MediaController->download(); + $this->MediaView =& new TestMediaView($this->MediaController); + } + +/** + * endTest method + * + * @access public + * @return void + */ + function endTest() { + unset($this->MediaView); + unset($this->MediaController); + unset($this->Controller); + ClassRegistry::flush(); + } + +/** + * testRender method + * + * @access public + * @return void + */ + function testRender() { + ob_start(); + $result = $this->MediaView->render(); + $output = ob_get_clean(); + + $this->assertTrue($result !== false); + $this->assertEqual($output, 'this is the test asset css file'); + } + +/** + * testConnectionAborted method + * + * @access public + * @return void + */ + function testConnectionAborted() { + $this->MediaView->active = false; + $result = $this->MediaView->render(); + $this->assertFalse($result); + } +} +?>