浏览代码

More tests for EmailLib

euromark 12 年之前
父节点
当前提交
62ed4f067c
共有 2 个文件被更改,包括 169 次插入35 次删除
  1. 11 25
      Lib/EmailLib.php
  2. 158 10
      Test/Case/Lib/EmailLibTest.php

+ 11 - 25
Lib/EmailLib.php

@@ -83,7 +83,8 @@ class EmailLib extends CakeEmail {
 
 	/**
 	 * @param string $file: absolute path
-	 * @param string $filename (optional)
+	 * @param string $filename
+	 * @param array $fileInfo
 	 * @return resource EmailLib
 	 * 2011-11-02 ms
 	 */
@@ -101,19 +102,15 @@ class EmailLib extends CakeEmail {
 	 * @param binary $content: blob data
 	 * @param string $filename to attach it
 	 * @param string $mimeType (leave it empty to get mimetype from $filename)
-	 * @param string $contentId (optional)
-	 * @return mixed ressource EmailLib or string $contentId
+	 * @param array $fileInfo
+	 * @return resource EmailLib
 	 * 2011-11-02 ms
 	 */
 	public function addBlobAttachment($content, $name, $mimeType = null, $fileInfo = array()) {
 		$fileInfo['content'] = $content;
 		$fileInfo['mimetype'] = $mimeType;
-		$file = array($name=>$fileInfo);
-		$res = $this->addAttachments($file);
-		if ($contentId === null) {
-			return $fileInfo['contentId'];
-		}
-		return $res;
+		$file = array($name => $fileInfo);
+		return $this->addAttachments($file);
 	}
 
 	/**
@@ -123,7 +120,7 @@ class EmailLib extends CakeEmail {
 	 * @param string $contentId (optional)
 	 * @param array $options
 	 * - contentDisposition
-	 * @return mixed ressource $EmailLib or string $contentId
+	 * @return mixed resource $EmailLib or string $contentId
 	 * 2011-11-02 ms
 	 */
 	public function addEmbeddedBlobAttachment($content, $name, $mimeType = null, $contentId = null, $options = array()) {
@@ -145,7 +142,7 @@ class EmailLib extends CakeEmail {
 	 * @param array $options
 	 * - mimetype
 	 * - contentDisposition
-	 * @return mixed ressource $EmailLib or string $contentId
+	 * @return mixed resource $EmailLib or string $contentId
 	 * 2011-11-02 ms
 	 */
 	public function addEmbeddedAttachment($file, $name = null, $contentId = null, $options = array()) {
@@ -229,10 +226,12 @@ class EmailLib extends CakeEmail {
 	}
 
 	/**
+	 * Validate if the email has the required fields necessary to make send() work.
+	 *
 	 * @return boolean Success
 	 */
 	public function validates() {
-		if (!empty($this->Email->subject)) {
+		if (!empty($this->_subject) && !empty($this->_to)) {
 			return true;
 		}
 		return false;
@@ -612,19 +611,6 @@ class EmailLib extends CakeEmail {
 	}
 
 	/**
-	 * Fix line length for _renderTemplates()
-	 * @overwrite
-	 */
-	protected function _renderTemplates($content) {
-		$res = parent::_renderTemplates($content);
-		foreach ($res as $type => $content) {
-			$res[$type] = $this->_wrap($content);
-			$res[$type] = implode("\n", $res[$type]);
-		}
-		return $res;
-	}
-
-	/**
 	 * Fix line length
 	 * @overwrite
 	 */

+ 158 - 10
Test/Case/Lib/EmailLibTest.php

@@ -16,13 +16,12 @@ class EmailLibTest extends MyCakeTestCase {
 
 		$this->skipIf(!file_exists(APP . 'Config' . DS . 'email.php'), 'no email.php');
 
-		$this->Email = new EmailLib();
+		$this->Email = new TestEmailLib();
 	}
 
 	public function testObject() {
 		$this->assertTrue(is_object($this->Email));
 		$this->assertInstanceOf('EmailLib', $this->Email);
-;
 	}
 
 	public function testSendDefault() {
@@ -115,7 +114,42 @@ class EmailLibTest extends MyCakeTestCase {
 		die();
 	}
 
+	/**
+	 * EmailLibTest::testAddAttachment()
+	 *
+	 * @return void
+	 */
 	public function testAddAttachment() {
+		$file = CakePlugin::path('Tools').'Test'.DS.'test_files'.DS.'img'.DS.'hotel.png';
+		$this->assertTrue(file_exists($file));
+
+		$this->Email->addAttachment($file);
+
+		$res = $this->Email->getProtected('attachments');
+		$expected = array(
+			'hotel.png' => array(
+				'file' => $file,
+				'mimetype' => 'image/png',
+			)
+		);
+		$this->assertEquals($expected, $res);
+
+		$this->Email->addAttachment($file, 'my_image.jpg');
+
+		$res = $this->Email->getProtected('attachments');
+		$expected = array(
+			'file' => $file,
+			'mimetype' => 'image/jpeg',
+		);
+		$this->assertEquals($expected, $res['my_image.jpg']);
+ 	}
+
+	/**
+	 * EmailLibTest::testAddAttachment()
+	 *
+	 * @return void
+	 */
+	public function testAddAttachmentSend() {
 		$this->skipIf(!$this->sendEmails);
 
 		$file = CakePlugin::path('Tools').'Test'.DS.'test_files'.DS.'img'.DS.'hotel.png';
@@ -142,22 +176,77 @@ class EmailLibTest extends MyCakeTestCase {
 	}
 
 	/**
-	 * html email
+	 * EmailLibTest::testAddBlobAttachment()
+	 *
+	 * @return void
+	 */
+	public function testAddBlobAttachment() {
+		$file = CakePlugin::path('Tools').'Test'.DS.'test_files'.DS.'img'.DS.'hotel.png';
+		$content = file_get_contents($file);
+
+		$this->Email->addBlobAttachment($content, 'hotel.png');
+		$res = $this->Email->getProtected('attachments');
+		$expected = array(
+			'hotel.png' => array(
+				'content' => $content,
+				'mimetype' => 'image/png',
+			)
+		);
+		$this->assertEquals($expected, $res);
+
+		$this->Email->addBlobAttachment($content, 'hotel.gif', 'image/jpeg');
+		$res = $this->Email->getProtected('attachments');
+		$expected = array(
+			'content' => $content,
+			'mimetype' => 'image/jpeg',
+		);
+		$this->assertEquals($expected, $res['hotel.gif']);#
+		$this->assertSame(2, count($res));
+	}
+
+	/**
+	 * EmailLibTest::testAddEmbeddedAttachment()
+	 *
+	 * @return void
 	 */
 	public function testAddEmbeddedAttachment() {
 		$file = CakePlugin::path('Tools').'Test'.DS.'test_files'.DS.'img'.DS.'hotel.png';
 		$this->assertTrue(file_exists($file));
 
+		$this->Email = new TestEmailLib();
+		$this->Email->emailFormat('both');
+
+		$cid = $this->Email->addEmbeddedAttachment($file);
+		$cid2 = $this->Email->addEmbeddedAttachment($file);
+		$this->assertSame($cid, $cid2);
+		$this->assertContains('@' . env('HTTP_HOST'), $cid);
+
+		$res = $this->Email->getProtected('attachments');
+		$expected = array(
+			'hotel.png' => array(
+				'file' => $file,
+				'mimetype' => 'image/png; charset=binary',
+				'contentId' => $cid
+			)
+		);
+		$this->assertSame($expected, $res);
+	}
+
+	/**
+	 * html email
+	 */
+	public function testAddEmbeddedAttachmentSend() {
+		$file = CakePlugin::path('Tools').'Test'.DS.'test_files'.DS.'img'.DS.'hotel.png';
+
 		Configure::write('debug', 0);
-		$this->Email = new EmailLib();
+		$this->Email = new TestEmailLib();
 		$this->Email->emailFormat('both');
 		$this->Email->to(Configure::read('Config.admin_email'));
 		$cid = $this->Email->addEmbeddedAttachment($file);
 
 		$cid2 = $this->Email->addEmbeddedAttachment($file);
 
-		$this->assertContains('@'.env('HTTP_HOST'), $cid);
-
+		$this->assertContains('@' . env('HTTP_HOST'), $cid);
 
 		$html = '<head>
 	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
@@ -189,19 +278,72 @@ html-part
 	}
 
 	/**
-	 * html email
+	 * EmailLibTest::testAddEmbeddedBlobAttachment()
+	 *
+	 * @return void
 	 */
 	public function testAddEmbeddedBlobAttachment() {
 		$file = CakePlugin::path('Tools').'Test'.DS.'test_files'.DS.'img'.DS.'hotel.png';
 		$this->assertTrue(file_exists($file));
 
+		$this->Email = new TestEmailLib();
+		$this->Email->emailFormat('both');
+		$cid = $this->Email->addEmbeddedBlobAttachment(file_get_contents($file), 'my_hotel.png');
+
+		$this->assertContains('@' . env('HTTP_HOST'), $cid);
+
+		$res = $this->Email->getProtected('attachments');
+		$expected = array(
+			'my_hotel.png' => array(
+				'content' => file_get_contents($file),
+				'mimetype' => 'image/png',
+				'contentId' => $cid,
+			)
+		);
+		$this->assertEquals($expected, $res);
+
+		$options = array(
+			'contentDisposition' => true,
+		);
+		$cid = 'abcdef';
+		$this->Email->addEmbeddedBlobAttachment(file_get_contents($file), 'my_other_hotel.png', 'image/jpeg', $cid, $options);
+
+		$res = $this->Email->getProtected('attachments');
+		$expected = array(
+			'contentDisposition' => true,
+			'content' => file_get_contents($file),
+			'mimetype' => 'image/jpeg',
+			'contentId' => $cid,
+		);
+		$this->assertEquals($expected, $res['my_other_hotel.png']);
+	}
+
+	public function testValidates() {
+		$res = $this->Email->validates();
+		$this->assertFalse($res);
+
+		$this->Email->subject('foo');
+		$res = $this->Email->validates();
+		$this->assertFalse($res);
+
+		$this->Email->to('some@web.de');
+		$res = $this->Email->validates();
+		$this->assertTrue($res);
+	}
+
+	/**
+	 * html email
+	 */
+	public function testAddEmbeddedBlobAttachmentSend() {
+		$file = CakePlugin::path('Tools').'Test'.DS.'test_files'.DS.'img'.DS.'hotel.png';
+
 		Configure::write('debug', 0);
-		$this->Email = new EmailLib();
+		$this->Email = new TestEmailLib();
 		$this->Email->emailFormat('both');
 		$this->Email->to(Configure::read('Config.admin_email'));
-		$cid = $this->Email->addEmbeddedBlobAttachment(file_get_contents($file), 'my_hotel.png', 'png');
+		$cid = $this->Email->addEmbeddedBlobAttachment(file_get_contents($file), 'my_hotel.png', 'image/png');
 
-		$this->assertContains('@'.env('HTTP_HOST'), $cid);
+		$this->assertContains('@' . env('HTTP_HOST'), $cid);
 
 
 		$html = '<head>
@@ -236,6 +378,7 @@ html-part
 		$file = CakePlugin::path('Tools').'Test'.DS.'test_files'.DS.'img'.DS.'hotel.png';
 		$this->assertTrue(file_exists($file));
 
+		//TODO
 	}
 
 	public function testWrapLongEmailContent() {
@@ -310,6 +453,11 @@ class TestEmailLib extends EmailLib {
 		return $this->_boundary;
 	}
 
+	public function getProtected($attribute) {
+		$attribute = '_' . $attribute;
+		return $this->$attribute;
+	}
+
 /**
  * Encode to protected method
  *