Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
CakeEmail: create request object before rendering
Closes #2931
  • Loading branch information
rchavik committed Jun 4, 2012
1 parent d1819dc commit 9bafc5a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/Cake/Network/Email/CakeEmail.php
Expand Up @@ -1441,6 +1441,12 @@ protected function _renderTemplates($content) {
$View = new $viewClass(null);
$View->viewVars = $this->_viewVars;
$View->helpers = $this->_helpers;
if (!$request = Router::getRequest(true)) {
$request = new CakeRequest('/', false);
$request->base = '';
$request->here = $request->webroot = '/';
}
$View->request = $request;

This comment has been minimized.

Copy link
@tigrang

tigrang Jun 4, 2012

Contributor

I was wondering if it would be better to have this check inside the View class's constructor. Helper methods depend on the View having a request object and there may be other places in the code that don't pass a controller object to the View's constructor.

This comment has been minimized.

Copy link
@SimonEast

SimonEast Jun 4, 2012

Yes, decent point tigrang. I would probably concur.

This comment has been minimized.

Copy link
@ADmad

ADmad Jun 4, 2012

Member

Even greater folly is assuming the webroot for the app is "/". If for example the app is deployed in a sub folder of document root then the webroot will not be "/"

This comment has been minimized.

Copy link
@markstory

markstory Jun 4, 2012

Member

If the false is left off, it will munge through global state and guess at the base/here values. However, you're still screwed in a non-http request state.

This comment has been minimized.

Copy link
@rchavik

rchavik Jun 4, 2012

Author Member

Moved to View class in 8966f1b. Will get back on this tomorrow.


list($templatePlugin, $template) = pluginSplit($this->_template);
list($layoutPlugin, $layout) = pluginSplit($this->_layout);
Expand Down
32 changes: 32 additions & 0 deletions lib/Cake/Test/Case/Network/Email/CakeEmailTest.php
Expand Up @@ -1028,6 +1028,38 @@ public function testSendRenderWithHelpers() {
$this->assertEquals(array('Time'), $result);
}

/**
* testSendRenderWithImage method
*
* @return void
*/
public function testSendRenderWithImage() {
$this->CakeEmail->reset();
$this->CakeEmail->transport('Debug');

$this->CakeEmail->from('cake@cakephp.org');
$this->CakeEmail->to(array('you@cakephp.org' => 'You'));
$this->CakeEmail->subject('My title');
$this->CakeEmail->config(array('empty'));
$this->CakeEmail->template('image');
$this->CakeEmail->emailFormat('html');

$View = new View();
$View->request = new CakeRequest('/', true);
$View->request->base = '';
$View->request->webroot = '/';
$View->request->here = '/';
$View->Helpers->load('Html');

$expected = $View->Html->image('image.gif', array(
'fullBase' => true, 'alt' => 'cool image',
'width' => 100, 'height' => 100,
));

$result = $this->CakeEmail->send();
$this->assertContains($expected, $result['message']);
}

/**
* testSendRenderPlugin method
*
Expand Down
23 changes: 23 additions & 0 deletions lib/Cake/Test/test_app/View/Emails/html/image.ctp
@@ -0,0 +1,23 @@
<?php
/**
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://www.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://www.cakefoundation.org)
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
* @since CakePHP(tm) v 2.1
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/

echo $this->Html->image('image.gif', array(
'alt' => 'cool image',
'width' => 100,
'height' => 100,
'fullBase' => true,
));
3 changes: 3 additions & 0 deletions lib/Cake/View/Helper.php
Expand Up @@ -313,6 +313,9 @@ public function assetUrl($path, $options = array()) {
$path = h($this->assetTimestamp($this->webroot($path)));

if (!empty($options['fullBase'])) {
if ($path[0] == '/') {
$path = substr($path, 1);
}
$path = $this->url('/', true) . $path;
}
}
Expand Down

0 comments on commit 9bafc5a

Please sign in to comment.