Skip to content
Browse files

CakeEmail: create request object before rendering

Closes #2931
  • Loading branch information...
1 parent d1819dc commit 9bafc5a3bbfa4e588397082620c2f9dab3cde548 @rchavik rchavik committed Jun 3, 2012
View
6 lib/Cake/Network/Email/CakeEmail.php
@@ -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;
@tigrang
tigrang added a note Jun 4, 2012

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.

@SimonEast
SimonEast added a note Jun 4, 2012

Yes, decent point tigrang. I would probably concur.

@ADmad
CakePHP member
ADmad added a note Jun 4, 2012

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 "/"

@markstory
CakePHP member
markstory added a note Jun 4, 2012

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.

@rchavik
CakePHP member
rchavik added a note Jun 4, 2012

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
list($templatePlugin, $template) = pluginSplit($this->_template);
list($layoutPlugin, $layout) = pluginSplit($this->_layout);
View
32 lib/Cake/Test/Case/Network/Email/CakeEmailTest.php
@@ -1029,6 +1029,38 @@ public function testSendRenderWithHelpers() {
}
/**
+ * 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
*
* @return void
View
23 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,
+ ));
View
3 lib/Cake/View/Helper.php
@@ -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;
}
}

0 comments on commit 9bafc5a

Please sign in to comment.
Something went wrong with that request. Please try again.