2010-04-13 20:04:13 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2011-10-10 21:18:48 +00:00
|
|
|
* MediaViewTest file
|
2010-04-13 20:04:13 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* PHP 5
|
2010-04-13 20:04:13 +00:00
|
|
|
*
|
2010-05-19 01:15:13 +00:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
|
2012-03-13 02:46:07 +00:00
|
|
|
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-04-13 20:04:13 +00:00
|
|
|
*
|
2010-10-03 16:31:21 +00:00
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice
|
2010-04-13 20:04:13 +00:00
|
|
|
*
|
2012-03-13 02:46:07 +00:00
|
|
|
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-05-19 01:15:13 +00:00
|
|
|
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.View
|
2010-04-13 20:04:13 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.4206
|
2010-10-03 16:27:27 +00:00
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
2010-04-13 20:04:13 +00:00
|
|
|
*/
|
2010-12-10 06:23:27 +00:00
|
|
|
|
|
|
|
App::uses('Controller', 'Controller');
|
|
|
|
App::uses('MediaView', 'View');
|
|
|
|
App::uses('CakeResponse', 'Network');
|
2010-04-13 20:04:13 +00:00
|
|
|
|
|
|
|
/**
|
2010-10-04 04:00:02 +00:00
|
|
|
* MediaViewTest class
|
2010-04-13 20:04:13 +00:00
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.View
|
2010-04-13 20:04:13 +00:00
|
|
|
*/
|
|
|
|
class MediaViewTest extends CakeTestCase {
|
|
|
|
|
|
|
|
/**
|
2010-09-26 01:36:49 +00:00
|
|
|
* setUp method
|
2010-04-13 20:04:13 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function setUp() {
|
2010-09-26 01:36:49 +00:00
|
|
|
parent::setUp();
|
2010-10-04 04:00:02 +00:00
|
|
|
$this->MediaView = $this->getMock('MediaView', array('_isActive', '_clearBuffer', '_flushBuffer'));
|
|
|
|
$this->MediaView->response = $this->getMock('CakeResponse');
|
2010-04-13 20:04:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* endTest method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function tearDown() {
|
2010-09-26 01:36:49 +00:00
|
|
|
parent::tearDown();
|
2010-04-13 20:04:13 +00:00
|
|
|
unset($this->MediaView);
|
2010-10-04 04:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* tests that rendering a file that does not exists throws an exception
|
|
|
|
*
|
2010-10-04 04:00:47 +00:00
|
|
|
* @expectedException NotFoundException
|
2010-10-04 04:00:02 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testRenderNotFound() {
|
|
|
|
$this->MediaView->viewVars = array(
|
|
|
|
'path' => '/some/missing/folder',
|
|
|
|
'id' => 'file.jpg'
|
|
|
|
);
|
|
|
|
$this->MediaView->render();
|
2010-04-13 20:04:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testRender method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testRender() {
|
2010-10-04 04:00:02 +00:00
|
|
|
$this->MediaView->viewVars = array(
|
2012-03-16 02:50:05 +00:00
|
|
|
'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS . 'css' . DS,
|
2010-10-04 04:00:02 +00:00
|
|
|
'id' => 'test_asset.css',
|
2010-10-04 04:00:47 +00:00
|
|
|
'extension' => 'css',
|
2010-10-04 04:00:02 +00:00
|
|
|
);
|
|
|
|
$this->MediaView->expects($this->exactly(2))
|
|
|
|
->method('_isActive')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
2011-02-27 01:01:15 +00:00
|
|
|
$this->MediaView->response->expects($this->exactly(1))
|
2010-10-04 04:00:02 +00:00
|
|
|
->method('type')
|
|
|
|
->with('css')
|
|
|
|
->will($this->returnArgument(0));
|
|
|
|
|
|
|
|
$this->MediaView->response->expects($this->at(1))
|
|
|
|
->method('header')
|
|
|
|
->with(array(
|
|
|
|
'Date' => gmdate('D, d M Y H:i:s', time()) . ' GMT',
|
|
|
|
'Expires' => '0',
|
|
|
|
'Cache-Control' => 'private, must-revalidate, post-check=0, pre-check=0',
|
|
|
|
'Pragma' => 'no-cache'
|
|
|
|
));
|
|
|
|
|
2011-02-27 01:01:15 +00:00
|
|
|
$this->MediaView->response->expects($this->at(2))
|
2010-10-04 04:00:02 +00:00
|
|
|
->method('header')
|
|
|
|
->with(array(
|
|
|
|
'Content-Length' => 31
|
|
|
|
));
|
|
|
|
$this->MediaView->response->expects($this->once())->method('send');
|
|
|
|
$this->MediaView->expects($this->once())->method('_clearBuffer');
|
|
|
|
$this->MediaView->expects($this->once())->method('_flushBuffer');
|
|
|
|
|
2010-04-13 20:04:13 +00:00
|
|
|
ob_start();
|
|
|
|
$result = $this->MediaView->render();
|
|
|
|
$output = ob_get_clean();
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals('this is the test asset css file', $output);
|
2010-04-13 20:04:13 +00:00
|
|
|
$this->assertTrue($result !== false);
|
|
|
|
}
|
|
|
|
|
2011-02-27 01:01:15 +00:00
|
|
|
/**
|
2011-04-25 03:31:44 +00:00
|
|
|
* testRenderWithUnknownFileTypeGeneric method
|
2011-02-27 01:01:15 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testRenderWithUnknownFileTypeGeneric() {
|
2011-04-27 04:26:42 +00:00
|
|
|
$currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
|
2011-04-25 03:31:44 +00:00
|
|
|
$_SERVER['HTTP_USER_AGENT'] = 'Some generic browser';
|
2011-02-27 01:01:15 +00:00
|
|
|
$this->MediaView->viewVars = array(
|
2012-03-16 02:50:05 +00:00
|
|
|
'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS,
|
2011-02-27 01:01:15 +00:00
|
|
|
'id' => 'no_section.ini',
|
|
|
|
'extension' => 'ini',
|
|
|
|
);
|
|
|
|
$this->MediaView->expects($this->exactly(2))
|
|
|
|
->method('_isActive')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$this->MediaView->response->expects($this->exactly(1))
|
|
|
|
->method('type')
|
|
|
|
->with('ini')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
|
|
|
$this->MediaView->response->expects($this->at(1))
|
|
|
|
->method('header')
|
|
|
|
->with(array(
|
|
|
|
'Date' => gmdate('D, d M Y H:i:s', time()) . ' GMT',
|
|
|
|
'Expires' => '0',
|
|
|
|
'Cache-Control' => 'private, must-revalidate, post-check=0, pre-check=0',
|
|
|
|
'Pragma' => 'no-cache'
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->MediaView->response->expects($this->once())
|
|
|
|
->method('download')
|
|
|
|
->with('no_section.ini');
|
|
|
|
|
|
|
|
$this->MediaView->response->expects($this->at(3))
|
|
|
|
->method('header')
|
|
|
|
->with(array(
|
|
|
|
'Accept-Ranges' => 'bytes'
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->MediaView->response->expects($this->at(4))
|
|
|
|
->method('header')
|
|
|
|
->with('Content-Length', 35);
|
|
|
|
|
|
|
|
$this->MediaView->response->expects($this->once())->method('send');
|
|
|
|
$this->MediaView->expects($this->once())->method('_clearBuffer');
|
|
|
|
$this->MediaView->expects($this->once())->method('_flushBuffer');
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$result = $this->MediaView->render();
|
|
|
|
$output = ob_get_clean();
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals("some_key = some_value\nbool_key = 1\n", $output);
|
2011-02-27 01:01:15 +00:00
|
|
|
$this->assertTrue($result !== false);
|
2011-04-27 04:26:42 +00:00
|
|
|
if ($currentUserAgent !== null) {
|
|
|
|
$_SERVER['HTTP_USER_AGENT'] = $currentUserAgent;
|
|
|
|
}
|
2011-04-25 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testRenderWithUnknownFileTypeOpera method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testRenderWithUnknownFileTypeOpera() {
|
2011-04-27 04:26:42 +00:00
|
|
|
$currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
|
2011-04-25 03:31:44 +00:00
|
|
|
$_SERVER['HTTP_USER_AGENT'] = 'Opera/9.80 (Windows NT 6.0; U; en) Presto/2.8.99 Version/11.10';
|
|
|
|
$this->MediaView->viewVars = array(
|
2012-03-16 02:50:05 +00:00
|
|
|
'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS,
|
2011-04-25 03:31:44 +00:00
|
|
|
'id' => 'no_section.ini',
|
|
|
|
'extension' => 'ini',
|
|
|
|
);
|
|
|
|
$this->MediaView->expects($this->exactly(2))
|
|
|
|
->method('_isActive')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$this->MediaView->response->expects($this->at(0))
|
|
|
|
->method('type')
|
|
|
|
->with('ini')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
|
|
|
$this->MediaView->response->expects($this->at(1))
|
|
|
|
->method('header')
|
|
|
|
->with(array(
|
|
|
|
'Date' => gmdate('D, d M Y H:i:s', time()) . ' GMT',
|
|
|
|
'Expires' => '0',
|
|
|
|
'Cache-Control' => 'private, must-revalidate, post-check=0, pre-check=0',
|
|
|
|
'Pragma' => 'no-cache'
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->MediaView->response->expects($this->at(2))
|
|
|
|
->method('type')
|
|
|
|
->with('application/octetstream')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
|
|
|
$this->MediaView->response->expects($this->once())
|
|
|
|
->method('download')
|
|
|
|
->with('no_section.ini');
|
|
|
|
|
|
|
|
$this->MediaView->response->expects($this->at(4))
|
|
|
|
->method('header')
|
|
|
|
->with(array(
|
|
|
|
'Accept-Ranges' => 'bytes'
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->MediaView->response->expects($this->at(5))
|
|
|
|
->method('header')
|
|
|
|
->with('Content-Length', 35);
|
|
|
|
|
|
|
|
$this->MediaView->response->expects($this->once())->method('send');
|
|
|
|
$this->MediaView->expects($this->once())->method('_clearBuffer');
|
|
|
|
$this->MediaView->expects($this->once())->method('_flushBuffer');
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$result = $this->MediaView->render();
|
|
|
|
$output = ob_get_clean();
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals("some_key = some_value\nbool_key = 1\n", $output);
|
2011-04-25 03:31:44 +00:00
|
|
|
$this->assertTrue($result !== false);
|
2011-04-27 04:26:42 +00:00
|
|
|
if ($currentUserAgent !== null) {
|
|
|
|
$_SERVER['HTTP_USER_AGENT'] = $currentUserAgent;
|
|
|
|
}
|
2011-04-25 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testRenderWithUnknownFileTypeIE method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testRenderWithUnknownFileTypeIE() {
|
2011-04-27 04:26:42 +00:00
|
|
|
$currentUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null;
|
2011-04-25 03:31:44 +00:00
|
|
|
$_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(
|
2012-03-16 02:50:05 +00:00
|
|
|
'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS,
|
2011-04-25 03:31:44 +00:00
|
|
|
'id' => 'no_section.ini',
|
|
|
|
'extension' => 'ini',
|
2012-02-07 19:38:04 +00:00
|
|
|
'name' => 'config'
|
2011-04-25 03:31:44 +00:00
|
|
|
);
|
|
|
|
$this->MediaView->expects($this->exactly(2))
|
|
|
|
->method('_isActive')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$this->MediaView->response->expects($this->at(0))
|
|
|
|
->method('type')
|
|
|
|
->with('ini')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
|
|
|
$this->MediaView->response->expects($this->at(1))
|
|
|
|
->method('header')
|
|
|
|
->with(array(
|
|
|
|
'Date' => gmdate('D, d M Y H:i:s', time()) . ' GMT',
|
|
|
|
'Expires' => '0',
|
|
|
|
'Cache-Control' => 'private, must-revalidate, post-check=0, pre-check=0',
|
|
|
|
'Pragma' => 'no-cache'
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->MediaView->response->expects($this->at(2))
|
|
|
|
->method('type')
|
|
|
|
->with('application/force-download')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
|
|
|
$this->MediaView->response->expects($this->once())
|
|
|
|
->method('download')
|
2012-02-07 19:38:04 +00:00
|
|
|
->with('config.ini');
|
2011-04-25 03:31:44 +00:00
|
|
|
|
|
|
|
$this->MediaView->response->expects($this->at(4))
|
|
|
|
->method('header')
|
|
|
|
->with(array(
|
|
|
|
'Accept-Ranges' => 'bytes'
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->MediaView->response->expects($this->at(5))
|
|
|
|
->method('header')
|
|
|
|
->with('Content-Length', 35);
|
|
|
|
|
|
|
|
$this->MediaView->response->expects($this->once())->method('send');
|
|
|
|
$this->MediaView->expects($this->once())->method('_clearBuffer');
|
|
|
|
$this->MediaView->expects($this->once())->method('_flushBuffer');
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$result = $this->MediaView->render();
|
|
|
|
$output = ob_get_clean();
|
2011-11-16 00:07:56 +00:00
|
|
|
$this->assertEquals("some_key = some_value\nbool_key = 1\n", $output);
|
2011-04-25 03:31:44 +00:00
|
|
|
$this->assertTrue($result !== false);
|
2011-04-27 04:26:42 +00:00
|
|
|
if ($currentUserAgent !== null) {
|
|
|
|
$_SERVER['HTTP_USER_AGENT'] = $currentUserAgent;
|
|
|
|
}
|
2011-02-27 01:01:15 +00:00
|
|
|
}
|
|
|
|
|
2010-04-13 20:04:13 +00:00
|
|
|
/**
|
|
|
|
* testConnectionAborted method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testConnectionAborted() {
|
2010-10-04 04:00:47 +00:00
|
|
|
$this->MediaView->viewVars = array(
|
2012-03-16 02:50:05 +00:00
|
|
|
'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS . 'css' . DS,
|
2010-10-04 04:00:47 +00:00
|
|
|
'id' => 'test_asset.css',
|
|
|
|
'extension' => 'css',
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->MediaView->expects($this->once())
|
|
|
|
->method('_isActive')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
2011-02-27 01:01:15 +00:00
|
|
|
$this->MediaView->response->expects($this->never())
|
|
|
|
->method('type');
|
2010-10-04 04:00:47 +00:00
|
|
|
|
|
|
|
$result = $this->MediaView->render();
|
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* testConnectionAbortedOnBuffering method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testConnectionAbortedOnBuffering() {
|
2010-10-04 04:00:47 +00:00
|
|
|
$this->MediaView->viewVars = array(
|
2012-03-16 02:50:05 +00:00
|
|
|
'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS . 'css' . DS,
|
2010-10-04 04:00:47 +00:00
|
|
|
'id' => 'test_asset.css',
|
|
|
|
'extension' => 'css',
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->MediaView->expects($this->at(0))
|
|
|
|
->method('_isActive')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$this->MediaView->response->expects($this->any())
|
|
|
|
->method('type')
|
|
|
|
->with('css')
|
|
|
|
->will($this->returnArgument(0));
|
|
|
|
|
|
|
|
$this->MediaView->expects($this->at(1))
|
|
|
|
->method('_isActive')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
|
|
|
$this->MediaView->response->expects($this->once())->method('send');
|
|
|
|
$this->MediaView->expects($this->once())->method('_clearBuffer');
|
|
|
|
$this->MediaView->expects($this->never())->method('_flushBuffer');
|
|
|
|
|
2010-04-13 20:04:13 +00:00
|
|
|
$result = $this->MediaView->render();
|
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
2011-06-23 19:48:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test downloading files with UPPERCASE extensions.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2012-02-07 19:38:04 +00:00
|
|
|
public function testRenderUpperExtension() {
|
2011-06-23 19:48:06 +00:00
|
|
|
$this->MediaView->viewVars = array(
|
2012-03-16 02:50:05 +00:00
|
|
|
'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS . 'img' . DS,
|
2011-06-23 19:48:06 +00:00
|
|
|
'id' => 'test_2.JPG',
|
|
|
|
'extension' => 'JPG',
|
|
|
|
);
|
|
|
|
|
2011-06-23 20:22:08 +00:00
|
|
|
$this->MediaView->response->expects($this->any())
|
|
|
|
->method('type')
|
|
|
|
->with('jpg')
|
|
|
|
->will($this->returnArgument(0));
|
2011-06-23 19:48:06 +00:00
|
|
|
|
2011-06-23 20:22:08 +00:00
|
|
|
$this->MediaView->expects($this->at(0))
|
|
|
|
->method('_isActive')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$this->MediaView->render();
|
2011-06-23 19:48:06 +00:00
|
|
|
}
|
|
|
|
|
2012-02-07 19:38:04 +00:00
|
|
|
/**
|
|
|
|
* Test downloading files with extension not explicitly set.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testRenderExtensionNotSet() {
|
|
|
|
$this->MediaView->viewVars = array(
|
2012-03-16 02:50:05 +00:00
|
|
|
'path' => CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor' . DS . 'img' . DS,
|
2012-02-07 19:38:04 +00:00
|
|
|
'id' => 'test_2.JPG',
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->MediaView->response->expects($this->any())
|
|
|
|
->method('type')
|
|
|
|
->with('jpg')
|
|
|
|
->will($this->returnArgument(0));
|
|
|
|
|
|
|
|
$this->MediaView->expects($this->at(0))
|
|
|
|
->method('_isActive')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$this->MediaView->render();
|
|
|
|
}
|
|
|
|
|
2010-04-13 20:04:13 +00:00
|
|
|
}
|