ソースを参照

Adjust Mailer to new core Mailer enhancements of 3.4.

dereuromark 9 年 前
コミット
599e23a89e
2 ファイル変更218 行追加100 行削除
  1. 156 27
      src/Mailer/Email.php
  2. 62 73
      tests/TestCase/Mailer/EmailTest.php

+ 156 - 27
src/Mailer/Email.php

@@ -20,6 +20,8 @@ class Email extends CakeEmail {
 	protected $_wrapLength = null;
 
 	/**
+	 * @deprecated Since CakePHP 3.4.0-RC4 in core
+	 *
 	 * @var int|null
 	 */
 	protected $_priority = null;
@@ -52,6 +54,8 @@ class Email extends CakeEmail {
 	/**
 	 * Change the layout
 	 *
+	 * @deprecated Since CakePHP 3.4.0-RC4 in core as getLayout()/setLayout()
+	 *
 	 * @param string|bool $layout Layout to use (or false to use none)
 	 * @return $this
 	 */
@@ -63,22 +67,46 @@ class Email extends CakeEmail {
 	}
 
 	/**
+	 * Sets wrap length.
+	 *
+	 * @param int $length Must not be more than CakeEmail::LINE_LENGTH_MUST
+	 * @return $this
+	 */
+	public function setWrapLength($length) {
+		$this->_wrapLength = $length;
+		return $this;
+	}
+
+	/**
+	 * Gets wrap length.
+	 *
+	 * @return int
+	 */
+	public function getWrapLength() {
+		return $this->_wrapLength;
+	}
+
+	/**
 	 * Set/Get wrapLength
 	 *
+	 * @deprecated Use setWrapLenght()/getWrapLength() instead.
+	 *
 	 * @param int|null $length Must not be more than CakeEmail::LINE_LENGTH_MUST
 	 * @return int|$this
 	 */
 	public function wrapLength($length = null) {
 		if ($length === null) {
-			return $this->_wrapLength;
+			return $this->getWrapLength();
 		}
-		$this->_wrapLength = $length;
-		return $this;
+
+		return $this->setWrapLength($length);
 	}
 
 	/**
 	 * Set/Get priority
 	 *
+	 * @deprecated Since CakePHP 3.4.0-RC4 in core as setPriority()/getPriority()
+	 *
 	 * @param int|null $priority 1 (highest) to 5 (lowest)
 	 * @return int|$this
 	 */
@@ -123,13 +151,10 @@ class Email extends CakeEmail {
 	/**
 	 * Ovewrite to allow custom enhancements
 	 *
-	 * @param mixed $config
-	 * @return string|null|$this
+	 * @param array|string $config
+	 * @return $this
 	 */
-	public function profile($config = null) {
-		if ($config === null) {
-			return $this->_profile;
-		}
+	public function setProfile($config) {
 		if (!is_array($config)) {
 			$config = (string)$config;
 		}
@@ -155,15 +180,27 @@ class Email extends CakeEmail {
 	}
 
 	/**
-	 * Overwrite to allow mimetype detection
+	 * @deprecated Since CakePHP 3.4.0 - use setProfile()/getProfile() instead.
 	 *
-	 * @param mixed|null $attachments
-	 * @return array|$this
+	 * @param mixed $config
+	 * @return string|null|$this
 	 */
-	public function attachments($attachments = null) {
-		if ($attachments === null) {
-			return $this->_attachments;
+	public function profile($config = null) {
+		if ($config === null) {
+			return $this->_profile;
 		}
+
+		return $this->setProfile($config);
+	}
+
+	/**
+	 * Overwrite to allow mimetype detection
+	 +
+	 * @param string|array $attachments String with the filename or array with filenames
+	 * @return $this
+	 * @throws \InvalidArgumentException
+	 */
+	public function setAttachments($attachments) {
 		$attach = [];
 		foreach ((array)$attachments as $name => $fileInfo) {
 			if (!is_array($fileInfo)) {
@@ -194,10 +231,25 @@ class Email extends CakeEmail {
 			$attach[$name] = $fileInfo;
 		}
 		$this->_attachments = $attach;
+
 		return $this;
 	}
 
 	/**
+	 * @deprecated Since CakePHP 3.4.0 - use setAttachments()/getAttachments() instead.
+	 *
+	 * @param mixed|null $attachments
+	 * @return array|$this
+	 */
+	public function attachments($attachments = null) {
+		if ($attachments === null) {
+			return $this->_attachments;
+		}
+
+		return $this->setAttachments($attachments);
+	}
+
+	/**
 	 * Add an attachment from file
 	 *
 	 * @param string $file Absolute path
@@ -236,7 +288,35 @@ class Email extends CakeEmail {
 	}
 
 	/**
-	 * Add an inline attachment from file
+	 * Adds an inline attachment from file.
+	 *
+	 * Options:
+	 * - mimetype
+	 * - contentDisposition
+	 *
+	 * @param string $contentId
+	 * @param string $file
+	 * @param string|null $name
+	 * @param array $options
+	 * @return $this
+	 */
+	public function addEmbeddedAttachmentByContentId($contentId, $file, $name = null, array $options = []) {
+		if (empty($name)) {
+			$name = basename($file);
+		}
+		$name = pathinfo($name, PATHINFO_FILENAME) . '_' . md5($file) . '.' . pathinfo($name, PATHINFO_EXTENSION);
+
+		$options['file'] = $file;
+		if (empty($options['mimetype'])) {
+			$options['mimetype'] = $this->_getMime($file);
+		}
+		$options['contentId'] = $contentId;
+		$file = [$name => $options];
+		return $this->addAttachments($file);
+	}
+
+	/**
+	 * Adds an inline attachment from file.
 	 *
 	 * Options:
 	 * - mimetype
@@ -244,15 +324,22 @@ class Email extends CakeEmail {
 	 *
 	 * @param string $file Absolute path
 	 * @param string|null $name (optional)
-	 * @param string|null $contentId (optional)
-	 * @param array $options Options
-	 * @return string|$this CID or $this
+	 * @param array|string|null $options Options - string CID is deprecated
+	 * @param array $notUsed Former Options @deprecated 4th param is now 3rd since CakePHP 3.4.0 - Use addEmbeddedAttachmentByContentId() otherwise.
+	 * @return string CID ($this is deprecated!)
 	 */
-	public function addEmbeddedAttachment($file, $name = null, $contentId = null, array $options = []) {
+	public function addEmbeddedAttachment($file, $name = null, $options = null, array $notUsed = []) {
 		if (empty($name)) {
 			$name = basename($file);
 		}
 
+		$contentId = null;
+		// Deprecated $contentId here
+		if (!is_array($options)) {
+			$contentId = $options;
+			$options = $notUsed;
+		}
+
 		$name = pathinfo($name, PATHINFO_FILENAME) . '_' . md5($file) . '.' . pathinfo($name, PATHINFO_EXTENSION);
 		if ($contentId === null && ($cid = $this->_isEmbeddedAttachment($file, $name))) {
 			return $cid;
@@ -262,12 +349,45 @@ class Email extends CakeEmail {
 		if (empty($options['mimetype'])) {
 			$options['mimetype'] = $this->_getMime($file);
 		}
-		$options['contentId'] = $contentId ? $contentId : str_replace('-', '', Text::uuid()) . '@' . $this->_domain;
+		$options['contentId'] = $contentId ?: str_replace('-', '', Text::uuid()) . '@' . $this->_domain;
 		$file = [$name => $options];
 		$res = $this->addAttachments($file);
 		if ($contentId === null) {
 			return $options['contentId'];
 		}
+
+		// Deprecated
+		return $res;
+	}
+
+	/**
+	 * Adds an inline attachment from file.
+	 *
+	 * Options:
+	 * - mimetype
+	 * - contentDisposition
+	 *
+	 * @param string $contentId
+	 * @param string $content Blob data
+	 * @param string $file File File path to file
+	 * @param string|null $mimeType (leave it empty to get mimetype from $filename)
+	 * @param array $options
+	 * @return $this
+	 */
+	public function addEmbeddedBlobAttachmentByContentId($contentId, $content, $file, $mimeType = null, array $options = []) {
+		if ($mimeType === null) {
+			$ext = pathinfo($file, PATHINFO_EXTENSION);
+			$mimeType = $this->_getMimeByExtension($ext);
+		}
+
+		$filename = pathinfo($file, PATHINFO_FILENAME) . '_' . md5($content) . '.' . pathinfo($file, PATHINFO_EXTENSION);
+
+		$options['data'] = $content;
+		$options['mimetype'] = $mimeType;
+		$options['contentId'] = $contentId;
+		$file = [$filename => $options];
+		$res = $this->addAttachments($file);
+
 		return $res;
 	}
 
@@ -280,16 +400,23 @@ class Email extends CakeEmail {
 	 * @param string $content Blob data
 	 * @param string $filename to attach it
 	 * @param string|null $mimeType (leave it empty to get mimetype from $filename)
-	 * @param string|null $contentId (optional)
-	 * @param array $options Options
-	 * @return string|$this $contentId or $this
+	 * @param array|string|null $options Options - string CID is deprecated
+	 * @param array $notUsed
+	 * @return string CID CcontentId ($this is deprecated)
 	 */
-	public function addEmbeddedBlobAttachment($content, $filename, $mimeType = null, $contentId = null, array $options = []) {
+	public function addEmbeddedBlobAttachment($content, $filename, $mimeType = null, $options = null, array $notUsed = []) {
 		if ($mimeType === null) {
 			$ext = pathinfo($filename, PATHINFO_EXTENSION);
 			$mimeType = $this->_getMimeByExtension($ext);
 		}
 
+		$contentId = null;
+		// Deprecated $contentId here
+		if (!is_array($options)) {
+			$contentId = $options;
+			$options = $notUsed;
+		}
+
 		$filename = pathinfo($filename, PATHINFO_FILENAME) . '_' . md5($content) . '.' . pathinfo($filename, PATHINFO_EXTENSION);
 		if ($contentId === null && ($cid = $this->_isEmbeddedBlobAttachment($content, $filename))) {
 			return $cid;
@@ -303,6 +430,8 @@ class Email extends CakeEmail {
 		if ($contentId === null) {
 			return $options['contentId'];
 		}
+
+		// Deprecated
 		return $res;
 	}
 
@@ -434,10 +563,10 @@ class Email extends CakeEmail {
 			'bcc' => $this->_bcc,
 			'transport' => get_class($this->_transport),
 		];
+
+		/** @deprecated Since CakePHP 3.4.0-RC4 in core **/
 		if ($this->_priority) {
 			$this->_headers['X-Priority'] = $this->_priority;
-			//$this->_headers['X-MSMail-Priority'] = 'High';
-			//$this->_headers['Importance'] = 'High';
 		}
 
 		// if not live, just log but do not send any mails //TODO: remove and use Debug Transport!

+ 62 - 73
tests/TestCase/Mailer/EmailTest.php

@@ -14,6 +14,11 @@ use Tools\TestSuite\TestCase;
 class EmailTest extends TestCase {
 
 	/**
+	 * @var TestEmail
+	 */
+	protected $Email;
+
+	/**
 	 * setUp
 	 *
 	 * @return void
@@ -25,6 +30,8 @@ class EmailTest extends TestCase {
 		Email::configTransport('debug', [
 			'className' => 'Debug'
 		]);
+
+		Configure::delete('Config.xMailer');
 	}
 
 	/**
@@ -38,11 +45,23 @@ class EmailTest extends TestCase {
 		Email::drop('test');
 		Email::dropTransport('debug');
 		Email::dropTransport('test_smtp');
+
+		Configure::delete('Config.xMailer');
+	}
+
+	/**
+	 * @return void
+	 */
+	public function testSetProfile() {
+		Configure::write('Config.xMailer', 'foobar');
+
+		$this->Email->setProfile('default');
+
+		$result = $this->Email->getProtected('headers');
+		$this->assertSame(['X-Mailer' => 'foobar'], $result);
 	}
 
 	/**
-	 * testFrom method
-	 *
 	 * @return void
 	 */
 	public function testFrom() {
@@ -63,13 +82,11 @@ class EmailTest extends TestCase {
 		$this->assertSame($expected, $this->Email->from());
 		$this->assertSame($this->Email, $result);
 
-		$this->setExpectedException('InvalidArgumentException');
-		$result = $this->Email->from(['cake@cakephp.org' => 'CakePHP', 'fail@cakephp.org' => 'From can only be one address']);
+		$this->expectException('InvalidArgumentException');
+		$this->Email->from(['cake@cakephp.org' => 'CakePHP', 'fail@cakephp.org' => 'From can only be one address']);
 	}
 
 	/**
-	 * EmailTest::testAddAttachment()
-	 *
 	 * @return void
 	 */
 	public function testAddAttachment() {
@@ -97,14 +114,11 @@ class EmailTest extends TestCase {
 	}
 
 	/**
-	 * EmailTest::testAddAttachment()
-	 *
 	 * @return void
 	 */
 	public function testAddAttachmentSend() {
 		$file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
 		$this->assertTrue(file_exists($file));
-		//Configure::write('debug', 0);
 
 		$this->Email->to(Configure::read('Config.adminEmail'));
 		$this->Email->addAttachment($file);
@@ -121,14 +135,46 @@ class EmailTest extends TestCase {
 		$this->Email->addAttachment($file, 'x.jpg');
 		$res = $this->Email->send('test_custom_filename');
 
-		//Configure::write('debug', 2);
-		//$this->assertEquals('', $this->Email->getError());
-		//$this->assertTrue($res);
+		$this->assertTrue((bool)$res);
+	}
+
+	/**
+	 * @return void
+	 */
+	public function testAddEmbeddedAttachmentByContentId() {
+		$file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
+
+		$this->Email->addEmbeddedAttachmentByContentId('123', $file);
+
+		$attachments = $this->Email->getProtected('attachments');
+		$attachment = array_shift($attachments);
+		$this->assertSame('image/png', $attachment['mimetype']);
+		$this->assertSame('123', $attachment['contentId']);
+	}
+
+	/**
+	 * @return void
+	 */
+	public function testAddEmbeddedBlobAttachmentByContentId() {
+		$file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
+		$content = file_get_contents($file);
+
+		$this->Email->addEmbeddedBlobAttachmentByContentId('123', $content, $file);
+
+		$attachments = $this->Email->getProtected('attachments');
+		$attachment = array_shift($attachments);
+		$this->assertNotEmpty($attachment['data']);
+		$this->assertSame('image/png', $attachment['mimetype']);
+		$this->assertSame('123', $attachment['contentId']);
+
+		$this->Email->addEmbeddedBlobAttachmentByContentId('123', $content, $file, 'png');
+
+		$attachments = $this->Email->getProtected('attachments');
+		$attachment = array_shift($attachments);
+		$this->assertSame('png', $attachment['mimetype']);
 	}
 
 	/**
-	 * EmailTest::testAddBlobAttachment()
-	 *
 	 * @return void
 	 */
 	public function testAddBlobAttachment() {
@@ -160,8 +206,6 @@ class EmailTest extends TestCase {
 	}
 
 	/**
-	 * EmailTest::testAddEmbeddedAttachment()
-	 *
 	 * @return void
 	 */
 	public function testAddEmbeddedAttachment() {
@@ -224,16 +268,12 @@ html-part
 		$res = $this->Email->send();
 		Configure::write('debug', 2);
 		$error = $this->Email->getError();
-		if ($error) {
-			$this->out($error);
-		}
+
 		$this->assertEquals('', $this->Email->getError());
 		$this->assertTrue((bool)$res);
 	}
 
 	/**
-	 * EmailTest::testAddEmbeddedBlobAttachment()
-	 *
 	 * @return void
 	 */
 	public function testAddEmbeddedBlobAttachment() {
@@ -281,8 +321,6 @@ html-part
 	}
 
 	/**
-	 * EmailTest::testValidates()
-	 *
 	 * @return void
 	 */
 	public function testValidates() {
@@ -290,20 +328,14 @@ html-part
 		$this->Email->transport('debug');
 		$res = $this->Email->validates();
 		$this->assertFalse($res);
-		//$res = $this->Email->send();
-		//$this->assertFalse($res);
 
 		$this->Email->subject('foo');
 		$res = $this->Email->validates();
 		$this->assertFalse($res);
-		//$res = $this->Email->send();
-		//$this->assertFalse($res);
 
 		$this->Email->to('some@web.de');
 		$res = $this->Email->validates();
 		$this->assertTrue($res);
-		//$res = $this->Email->send();
-		//$this->assertTrue($res);
 	}
 
 	/**
@@ -338,9 +370,7 @@ html-part
 		$res = $this->Email->send();
 
 		$error = $this->Email->getError();
-		if ($error) {
-			$this->out($error);
-		}
+
 		$this->assertEquals('', $this->Email->getError());
 		$this->assertTrue((bool)$res);
 	}
@@ -411,16 +441,6 @@ class TestEmail extends Email {
 	/**
 	 * Wrap to protected method
 	 *
-	 * @param array $address
-	 * @return array
-	 */
-	public function formatAddress($address) {
-		return parent::_formatAddress($address);
-	}
-
-	/**
-	 * Wrap to protected method
-	 *
 	 * @param string $text
 	 * @param int $length
 	 * @return array
@@ -430,37 +450,6 @@ class TestEmail extends Email {
 	}
 
 	/**
-	 * Get the boundary attribute
-	 *
-	 * @return string
-	 */
-	public function getBoundary() {
-		return $this->_boundary;
-	}
-
-	/**
-	 * Encode to protected method
-	 *
-	 * @param string $text
-	 * @return string
-	 */
-	public function encode($text) {
-		return $this->_encode($text);
-	}
-
-	/**
-	 * Render to protected method
-	 *
-	 * @param string $content
-	 * @return array
-	 */
-	public function render($content) {
-		return $this->_render($content);
-	}
-
-	/**
-	 * TestEmail::getProtected()
-	 *
 	 * @param string $attribute
 	 * @return mixed
 	 */