Browse Source

CakeEmail: create request object before rendering

Closes #2931
Rachman Chavik 14 years ago
parent
commit
9bafc5a3bb

+ 6 - 0
lib/Cake/Network/Email/CakeEmail.php

@@ -1441,6 +1441,12 @@ class CakeEmail {
 		$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;
 
 		list($templatePlugin, $template) = pluginSplit($this->_template);
 		list($layoutPlugin, $layout) = pluginSplit($this->_layout);

+ 32 - 0
lib/Cake/Test/Case/Network/Email/CakeEmailTest.php

@@ -1029,6 +1029,38 @@ class CakeEmailTest extends CakeTestCase {
 	}
 
 /**
+ * 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

+ 23 - 0
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 - 0
lib/Cake/View/Helper.php

@@ -313,6 +313,9 @@ class Helper extends Object {
 			$path = h($this->assetTimestamp($this->webroot($path)));
 
 			if (!empty($options['fullBase'])) {
+				if ($path[0] == '/') {
+					$path = substr($path, 1);
+				}
 				$path = $this->url('/', true) . $path;
 			}
 		}