浏览代码

Cleanup and more tests

euromark 12 年之前
父节点
当前提交
cf2389b93a
共有 4 个文件被更改,包括 104 次插入108 次删除
  1. 10 74
      Lib/EmailLib.php
  2. 3 3
      Model/MyModel.php
  3. 45 1
      Test/Case/View/Helper/MyHelperTest.php
  4. 46 30
      View/Helper/MyHelper.php

+ 10 - 74
Lib/EmailLib.php

@@ -402,64 +402,6 @@ class EmailLib extends CakeEmail {
 	}
 
 	/**
-	 * Apply the config to an instance
-	 *
-	 * @overwrite
-	 * @param CakeEmail $obj CakeEmail
-	 * @param array $config
-	 * @return void
-	 * @throws ConfigureException When configuration file cannot be found, or is missing
-	 * the named config.
-	 */
-	protected function _applyConfig($config) {
-		if (is_string($config)) {
-			if (!class_exists('EmailConfig') && !config('email')) {
-				throw new ConfigureException(__d('cake_dev', '%s not found.', APP . 'Config' . DS . 'email.php'));
-			}
-			$configs = new EmailConfig();
-			if (!isset($configs->{$config})) {
-				throw new ConfigureException(__d('cake_dev', 'Unknown email configuration "%s".', $config));
-			}
-			$config = $configs->{$config};
-		}
-		$this->_config += $config;
-		if (!empty($config['charset'])) {
-			$this->charset = $config['charset'];
-		}
-		if (!empty($config['headerCharset'])) {
-			$this->headerCharset = $config['headerCharset'];
-		}
-		if (empty($this->headerCharset)) {
-			$this->headerCharset = $this->charset;
-		}
-		$simpleMethods = array(
-			'from', 'sender', 'to', 'replyTo', 'readReceipt', 'returnPath', 'cc', 'bcc',
-			'messageId', 'domain', 'subject', 'viewRender', 'viewVars', 'attachments',
-			'transport', 'emailFormat', 'theme', 'helpers'
-		);
-		foreach ($simpleMethods as $method) {
-			if (isset($config[$method])) {
-				$this->$method($config[$method]);
-				unset($config[$method]);
-			}
-		}
-		if (isset($config['headers'])) {
-			$this->setHeaders($config['headers']);
-			unset($config['headers']);
-		}
-		if (array_key_exists('template', $config)) {
-			$layout = false;
-			if (array_key_exists('layout', $config)) {
-				$layout = $config['layout'];
-				unset($config['layout']);
-			}
-			$this->template($config['template'], $layout);
-			unset($config['template']);
-		}
-		$this->transportClass()->config($config);
-	}
-
-	/**
 	 * Set the body of the mail as we send it.
 	 * Note: the text can be an array, each element will appear as a seperate line in the message body.
 	 *
@@ -490,15 +432,16 @@ class EmailLib extends CakeEmail {
 			$this->_debug = parent::send($message);
 		} catch (Exception $e) {
 			$this->_error = $e->getMessage();
-			$this->_error .= ' (line '.$e->getLine().' in '.$e->getFile().')'.PHP_EOL.$e->getTraceAsString();
+			$this->_error .= ' (line ' . $e->getLine() . ' in ' . $e->getFile() . ')' . PHP_EOL .
+				$e->getTraceAsString();
 
-			if (!empty($this->_config['report'])) {
+			if (!empty($this->_config['logReport'])) {
 				$this->_logEmail();
 			}
 			return false;
 		}
 
-		if (!empty($this->_config['report'])) {
+		if (!empty($this->_config['logReport'])) {
 			$this->_logEmail();
 		}
 		return true;
@@ -580,18 +523,18 @@ class EmailLib extends CakeEmail {
 	 * @return void
 	 */
 	protected function _logEmail($append = null) {
-		$res = $this->_log['transport'].
-			' - '.'TO:'.implode(',', array_keys($this->_log['to'])).
-			'||FROM:'.implode(',', array_keys($this->_log['from'])).
-			'||REPLY:'.implode(',', array_keys($this->_log['replyTo'])).
-			'||S:'.$this->_log['subject'];
+		$res = $this->_log['transport'] .
+			' - ' . 'TO:' . implode(',', array_keys($this->_log['to'])) .
+			'||FROM:' . implode(',', array_keys($this->_log['from'])) .
+			'||REPLY:' . implode(',', array_keys($this->_log['replyTo'])) .
+			'||S:' . $this->_log['subject'];
 		$type = 'email';
 		if (!empty($this->_error)) {
 			$type = 'email_error';
 			$res .= '||ERROR:' . $this->_error;
 		}
 		if ($append) {
-			$res .= '||'.$append;
+			$res .= '||' . $append;
 		}
 		CakeLog::write($type, $res);
 	}
@@ -602,8 +545,6 @@ class EmailLib extends CakeEmail {
 	 * @return void
 	 */
 	public function resetAndSet() {
-		//$this->reset();
-
 		$this->_to = array();
 		$this->_cc = array();
 		$this->_bcc = array();
@@ -630,11 +571,6 @@ class EmailLib extends CakeEmail {
 		if ($xMailer = Configure::read('Config.x-mailer')) {
 			$this->addHeaders(array('X-Mailer' => $xMailer));
 		}
-		//$this->_errors = array();
-		//$this->charset($this->charset);
-		//$this->sendAs($this->sendAs);
-		//$this->layout($this->_layout);
-		//$this->delivery($this->deliveryMethod);
 	}
 
 }

+ 3 - 3
Model/MyModel.php

@@ -1649,9 +1649,9 @@ class MyModel extends Model {
 	 */
 	public function generateNestedList($conditions = null, $indent = '--') {
 		$cats = $this->find('threaded', array('conditions' => $conditions, 'fields' => array(
-				$this->name . '.id',
-				$this->name . '.name',
-				$this->name . '.parent_id')));
+				$this->alias . '.id',
+				$this->alias . '.' . $this->displayField,
+				$this->alias . '.parent_id')));
 		$glist = $this->_generateNestedList($cats, $indent);
 		return $glist;
 	}

+ 45 - 1
Test/Case/View/Helper/MyHelperTest.php

@@ -12,13 +12,24 @@ class MyHelperTest extends MyCakeTestCase {
 		parent::setUp();
 
 		$this->MyHelper = new MyHelper(new View(null));
+		$this->Html = new MyHtmlHelper(new View(null));
 	}
 
+	/**
+	 * MyHelperTest::testObject()
+	 *
+	 * @return void
+	 */
 	public function testObject() {
 		$this->assertTrue(is_object($this->MyHelper));
 		$this->assertInstanceOf('MyHelper', $this->MyHelper);
 	}
 
+	/**
+	 * MyHelperTest::testLoadHelpers()
+	 *
+	 * @return void
+	 */
 	public function testLoadHelpers() {
 		$this->skipIf(class_exists('QrCodeHelper'), 'Already loaded');
 
@@ -29,5 +40,38 @@ class MyHelperTest extends MyCakeTestCase {
 		$this->assertTrue(class_exists('QrCodeHelper'));
 	}
 
-	//TODO
+	/**
+	 * MyHelperTest::testTime()
+	 *
+	 * @return void
+	 */
+	public function testTime() {
+		$time = time();
+		$is = $this->MyHelper->time($time);
+
+		$time = CakeTime::i18nFormat($time, '%Y-%m-%d %T');
+		$expected = '<time datetime="' . $time . '">' . $time . '</time>';
+		$this->assertEquals($expected, $is);
+	}
+
+	/**
+	 * MyHelperTest::testImageFromBlob()
+	 *
+	 * @return void
+	 */
+	public function testImageFromBlob() {
+		$folder = CakePlugin::path('Tools') . 'Test' . DS . 'test_files' . DS . 'img' . DS;
+		$content = file_get_contents($folder . 'hotel.png');
+		$is = $this->Html->imageFromBlob($content);
+		$this->assertTrue(!empty($is));
+	}
+
 }
+
+class MyHtmlHelper extends MyHelper {
+
+	protected $_tags = array(
+		'image' => '<img src="%s" %s/>',
+	);
+
+}

+ 46 - 30
View/Helper/MyHelper.php

@@ -4,7 +4,7 @@ App::uses('Router', 'Routing');
 App::uses('UrlCacheManager', 'Tools.Routing');
 
 /**
- * Helper enhancements for Cake2
+ * Helper enhancements for CakePHP
  *
  * @author Mark Scherer
  * @license MIT
@@ -20,6 +20,7 @@ class MyHelper extends Helper {
 
 	/**
 	 * Manually load helpers
+	 *
 	 * @param array $helpers (either strings, or [string => array(config...)])
 	 * @param boolean $callbacks - trigger missed callbacks
 	 * @return void
@@ -46,9 +47,11 @@ class MyHelper extends Helper {
 		}
 	}
 
-	//TODO
 	/**
-	 * problems: what if inside plugin webroot? not easy to do...
+	 * //TODO: remove
+	 * Not a very good solution, as it takes a lot of resources, file lookups.
+	 *
+	 * Problems: what if inside plugin webroot? not easy to do...
 	 */
 	public function imageIfExists($path, $options = array(), $default = '---') {
 		if (startsWith($path, '/')) {
@@ -71,16 +74,22 @@ class MyHelper extends Helper {
 	}
 
 	/**
-	 * Display image tag from blob content
-	 * enhancement for HtmlHelper
+	 * Display image tag from blob content.
+	 * Enhancement for HtmlHelper. Defaults to png image
+	 *
+	 * Options:
+	 * - type: png, gif, jpg, ...
+	 *
 	 * @param binary $content
 	 * @param array $options
 	 * @return string html imageTag
 	 */
 	public function imageFromBlob($content, $options = array()) {
-		$text = 'data:image/png;base64,' . base64_encode($content);
-		$image = sprintf($this->_tags['image'], $text, $this->_parseAttributes($options, null, '', ' '));
-		return $image;
+		$options += array('type' => 'png');
+		$mimeType = 'image/' . $options['type'];
+
+		$text = 'data:' . $mimeType . ';base64,' . base64_encode($content);
+		return sprintf($this->_tags['image'], $text, $this->_parseAttributes($options, null, '', ' '));
 	}
 
 	/**
@@ -89,12 +98,15 @@ class MyHelper extends Helper {
 	 * or a precise date in the proleptic Gregorian calendar,
 	 * optionally with a time and a time-zone offset.
 	 *
-	 * @param $content string
-	 * @param $options array
-	 * - 'format' STRING: Use the specified TimeHelper method (or format()). FALSE: Generate the datetime. NULL: Do nothing.
-	 * - 'datetime' STRING: If 'format' is STRING use as the formatting string. FALSE: Don't generate attribute
+	 * Options:
+	 * - 'format' STRING: Use the specified TimeHelper method (or format()).
+	 *   FALSE: Generate the datetime. NULL: Do nothing.
+	 * - 'datetime' STRING: If 'format' is STRING use as the formatting string.
+	 *   FALSE: Don't generate attribute
 	 *
-	 * //TODO: fixme
+	 * @param $content string Time
+	 * @param $options array Options
+	 * @return string HTML time tag.
 	 */
 	public function time($content, $options = array()) {
 		if (!isset($this->tags['time'])) {
@@ -107,18 +119,20 @@ class MyHelper extends Helper {
 		), $options);
 
 		if ($options['format'] !== null) {
-			App::uses('TimeHelper', 'View/Helper');
-			$TimeHelper = new TimeHelper($this->_View);
+			if (!isset($this->Time)) {
+				App::uses('TimeHelper', 'View/Helper');
+				$this->Time = new TimeHelper($this->_View);
+			}
 		}
 		if ($options['format']) {
-			if (method_exists($t, $options['format'])) {
-				$content = $TimeHelper->$options['format']($content);
+			if (method_exists($this->Time, $options['format'])) {
+				$content = $this->Time->$options['format']($content);
 			} else {
-				$content = $TimeHelper->i18nFormat($content, $options['format']);
+				$content = $this->Time->i18nFormat($content, $options['format']);
 			}
-			$options['datetime'] = $TimeHelper->i18nFormat(strtotime($content), $options['datetime']);
+			$options['datetime'] = $this->Time->i18nFormat(strtotime($content), $options['datetime']);
 		} elseif ($options['format'] === false && $options['datetime']) {
-			$options['datetime'] = $TimeHelper->i18nFormat(strtotime($content), $options['datetime']);
+			$options['datetime'] = $this->Time->i18nFormat(strtotime($content), $options['datetime']);
 		}
 
 		if ($options['pubdate']) {
@@ -135,14 +149,14 @@ class MyHelper extends Helper {
 	}
 
 	/**
-	 * For convienience function Html::defaultLink().
+	 * For convenience functions Html::defaultLink() and defaultUrl().
 	 *
 	 * @var array
 	 */
 	protected $_linkDefaults = null;
 
 	/**
-	 * Keep named and query params for pagination/filter after edit etc
+	 * Keep named and query params for pagination/filter after edit etc.
 	 *
 	 * @params same as Html::link($title, $url, $options, $confirmMessage)
 	 * @return string Link
@@ -155,7 +169,7 @@ class MyHelper extends Helper {
 	}
 
 	/**
-	 * Keep named and query params for pagination/filter after edit etc
+	 * Keep named and query params for pagination/filter after edit etc.
 	 *
 	 * @params same as Html::url($url, $options, $escape)
 	 * @return string Link
@@ -168,11 +182,12 @@ class MyHelper extends Helper {
 	}
 
 	/**
-	 * Convenience function for normal links
-	 * useful for layout links and links inside elements etc
+	 * Convenience function for normal links.
+	 * Useful for layout links and links inside elements etc if you don't want to
+	 * verbosely reset all parts of it (prefix, plugin, ...).
 	 *
 	 * @params same as Html::link($title, $url, $options, $confirmMessage)
-	 * @return string Link
+	 * @return string HTML Link
 	 */
 	public function defaultLink($title, $url = null, $options = array(), $confirmMessage = false) {
 		if ($this->_linkDefaults === null) {
@@ -200,9 +215,12 @@ class MyHelper extends Helper {
 	}
 
 	/**
-	 * Convenience function for normal urls
-	 * useful for layout urls and urls inside elements etc
+	 * Convenience function for normal urls.
+	 * Useful for layout links and links inside elements etc if you don't want to
+	 * verbosely reset all parts of it (prefix, plugin, ...).
+	 *
 	 * @params same as Html::url($url, $full)
+	 * @return string URL
 	 */
 	public function defaultUrl($url = null, $full = false) {
 		if ($this->_linkDefaults === null) {
@@ -217,8 +235,6 @@ class MyHelper extends Helper {
 		return $this->url($url, $full);
 	}
 
-	public $urlHere = null;
-
 	/**
 	 * Enhancement to htmlHelper which allows the crumbs protected array
 	 * to be cleared so that more than one set of crumbs can be generated in the same view.