Skip to content

Commit

Permalink
Applying patch from 'jeremyharris' to make RequestHandler::renderAs()…
Browse files Browse the repository at this point in the history
… accept an array of options that can be used to send a file as a download. Fixes #950
  • Loading branch information
markstory committed Sep 10, 2010
1 parent d5e906f commit 4a0e34a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
8 changes: 5 additions & 3 deletions cake/libs/controller/components/request_handler.php
Expand Up @@ -490,16 +490,18 @@ public function prefers($type = null) {
*
* @param object $controller A reference to a controller object
* @param string $type Type of response to send (e.g: 'ajax')
* @param array $options Array of options to use
* @return void
* @see RequestHandlerComponent::setContent()
* @see RequestHandlerComponent::respondAs()
*/
public function renderAs(&$controller, $type) {
$options = array('charset' => 'UTF-8');
public function renderAs(&$controller, $type, $options = array()) {
$defaults = array('charset' => 'UTF-8');

if (Configure::read('App.encoding') !== null) {
$options = array('charset' => Configure::read('App.encoding'));
$defaults['charset'] = Configure::read('App.encoding');
}
$options = array_merge($defaults, $options);

if ($type == 'ajax') {
$controller->layout = $this->ajaxLayout;
Expand Down
Expand Up @@ -319,6 +319,34 @@ function testRenderAs() {
$this->assertEqual($this->Controller->viewPath, 'request_handler_test' . DS . 'js');
}

/**
* test that attachment headers work with renderAs
*
* @return void
*/
function testRenderAsWithAttachment() {
$this->RequestHandler->request = $this->getMock('CakeRequest');
$this->RequestHandler->request->expects($this->any())
->method('accepts')
->will($this->returnValue(array('application/xml')));

$this->RequestHandler->response = $this->getMock('CakeResponse', array('type', 'download', 'charset'));
$this->RequestHandler->response->expects($this->at(0))
->method('type')
->with('application/xml');
$this->RequestHandler->response->expects($this->at(1))
->method('charset')
->with('UTF-8');
$this->RequestHandler->response->expects($this->at(2))
->method('download')
->with('myfile.xml');

$this->RequestHandler->renderAs($this->Controller, 'xml', array('attachment' => 'myfile.xml'));

$expected = 'request_handler_test' . DS . 'xml';
$this->assertEquals($expected, $this->Controller->viewPath);
}

/**
* test that respondAs works as expected.
*
Expand All @@ -344,15 +372,23 @@ function testRespondAs() {
* @return void
*/
function testRespondAsWithAttachment() {
$this->RequestHandler = $this->getMock('RequestHandlerComponent', array('_header'), array(&$this->Controller->Components));
$this->RequestHandler = $this->getMock(
'RequestHandlerComponent',
array('_header'),
array(&$this->Controller->Components)
);
$this->RequestHandler->response = $this->getMock('CakeResponse', array('type', 'download'));
$this->RequestHandler->request = $this->getMock('CakeRequest');

$this->RequestHandler->request->expects($this->once())
->method('accepts')
->will($this->returnValue(array('application/xml')));

$this->RequestHandler->response->expects($this->once())->method('download')
->with('myfile.xml');
$this->RequestHandler->response->expects($this->once())->method('type')
->with('application/xml');


$result = $this->RequestHandler->respondAs('xml', array('attachment' => 'myfile.xml'));
$this->assertTrue($result);
}
Expand Down

0 comments on commit 4a0e34a

Please sign in to comment.