| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452 |
- <?php
- namespace Tools\Test\TestCase\Network\Email;
- use Tools\Network\Email\Email;
- use Cake\Core\Configure;
- use Cake\Core\Plugin;
- use Tools\TestSuite\TestCase;
- use Cake\Log\Log;
- /**
- * Help to test Email
- *
- */
- class TestEmail extends Email {
- /**
- * Wrap to protected method
- *
- * @return array
- */
- public function formatAddress($address) {
- return parent::_formatAddress($address);
- }
- /**
- * Wrap to protected method
- *
- * @return array
- */
- public function wrap($text, $length = Email::LINE_LENGTH_MUST) {
- return parent::_wrap($text, $length);
- }
- /**
- * Get the boundary attribute
- *
- * @return string
- */
- public function getBoundary() {
- return $this->_boundary;
- }
- /**
- * Encode to protected method
- *
- * @return string
- */
- public function encode($text) {
- return $this->_encode($text);
- }
- /**
- * Render to protected method
- *
- * @return array
- */
- public function render($content) {
- return $this->_render($content);
- }
- /**
- * TestEmail::getProtected()
- *
- * @param string $attribute
- * @return mixed
- */
- public function getProtected($attribute) {
- $attribute = '_' . $attribute;
- return $this->$attribute;
- }
- }
- /**
- * EmailTest class
- */
- class EmailTest extends TestCase {
- /**
- * setUp
- *
- * @return void
- */
- public function setUp() {
- parent::setUp();
- $this->Email = new TestEmail();
- Email::configTransport('debug', [
- 'className' => 'Debug'
- ]);
- }
- /**
- * tearDown method
- *
- * @return void
- */
- public function tearDown() {
- parent::tearDown();
- Log::drop('email');
- Email::drop('test');
- Email::dropTransport('debug');
- Email::dropTransport('test_smtp');
- }
- /**
- * testFrom method
- *
- * @return void
- */
- public function testFrom() {
- $this->assertSame(array('test@example.com' => 'Mark'), $this->Email->from());
- $this->Email->from('cake@cakephp.org');
- $expected = array('cake@cakephp.org' => 'cake@cakephp.org');
- $this->assertSame($expected, $this->Email->from());
- $this->Email->from(array('cake@cakephp.org'));
- $this->assertSame($expected, $this->Email->from());
- $this->Email->from('cake@cakephp.org', 'CakePHP');
- $expected = array('cake@cakephp.org' => 'CakePHP');
- $this->assertSame($expected, $this->Email->from());
- $result = $this->Email->from(array('cake@cakephp.org' => 'CakePHP'));
- $this->assertSame($expected, $this->Email->from());
- $this->assertSame($this->Email, $result);
- $this->setExpectedException('InvalidArgumentException');
- $result = $this->Email->from(array('cake@cakephp.org' => 'CakePHP', 'fail@cakephp.org' => 'From can only be one address'));
- }
- /**
- * EmailTest::testAddAttachment()
- *
- * @return void
- */
- public function testAddAttachment() {
- $file = Plugin::path('Tools') . 'tests' . 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']);
- }
- /**
- * 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);
- $res = $this->Email->send('test_default', 'default');
- if ($error = $this->Email->getError()) {
- $this->out($error);
- }
- $this->assertEquals('', $this->Email->getError());
- $this->assertTrue((bool)$res);
- $this->Email->reset();
- $this->Email->to(Configure::read('Config.adminEmail'));
- $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);
- }
- /**
- * EmailTest::testAddBlobAttachment()
- *
- * @return void
- */
- public function testAddBlobAttachment() {
- $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
- $content = file_get_contents($file);
- $this->Email->addBlobAttachment($content, 'hotel.png');
- $res = $this->Email->getProtected('attachments');
- $this->assertTrue(!empty($res['hotel.png']['data']));
- unset($res['hotel.png']['data']);
- $expected = array(
- 'hotel.png' => array(
- //'data' => $content,
- 'mimetype' => 'image/png',
- )
- );
- $this->assertEquals($expected, $res);
- $this->Email->addBlobAttachment($content, 'hotel.gif', 'image/jpeg');
- $res = $this->Email->getProtected('attachments');
- $this->assertTrue(!empty($res['hotel.gif']['data']));
- unset($res['hotel.gif']['data']);
- $expected = array(
- //'data' => $content,
- 'mimetype' => 'image/jpeg',
- );
- $this->assertEquals($expected, $res['hotel.gif']);#
- $this->assertSame(2, count($res));
- }
- /**
- * EmailTest::testAddEmbeddedAttachment()
- *
- * @return void
- */
- public function testAddEmbeddedAttachment() {
- $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
- $this->assertTrue(file_exists($file));
- $this->Email = new TestEmail();
- $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');
- $this->assertTrue(!empty($res['hotel.png']['file']));
- unset($res['hotel.png']['file']);
- $expected = array(
- 'hotel.png' => array(
- //'file' => $file,
- 'mimetype' => 'image/png',
- 'contentId' => $cid
- )
- );
- $this->assertSame($expected, $res);
- }
- /**
- * Html email
- *
- * @return void
- */
- public function testAddEmbeddedAttachmentSend() {
- $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
- Configure::write('debug', 0);
- $this->Email = new TestEmail();
- $this->Email->emailFormat('both');
- $this->Email->to(Configure::read('Config.adminEmail'));
- $cid = $this->Email->addEmbeddedAttachment($file);
- $cid2 = $this->Email->addEmbeddedAttachment($file);
- $this->assertContains('@' . env('HTTP_HOST'), $cid);
- $html = '<head>
- <meta http-equiv="content-type" content="text/html; charset=utf-8" />
- <meta name="author" content="ohyeah" />
- <title>Untitled 6</title>
- </head>
- <body>
- test_embedded_default äöü <img src="cid:' . $cid . '" /> end
- another image <img src="cid:' . $cid2 . '" /> end
- html-part
- </body>
- </html>';
- $text = trim(strip_tags($html));
- $this->Email->viewVars(compact('text', 'html'));
- $res = $this->Email->send();
- Configure::write('debug', 2);
- if ($error = $this->Email->getError()) {
- $this->out($error);
- }
- $this->assertEquals('', $this->Email->getError());
- $this->assertTrue((bool)$res);
- }
- /**
- * EmailTest::testAddEmbeddedBlobAttachment()
- *
- * @return void
- */
- public function testAddEmbeddedBlobAttachment() {
- $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
- $this->assertTrue(file_exists($file));
- $this->Email = new TestEmail();
- $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');
- $this->assertTrue(!empty($res['my_hotel.png']['data']));
- unset($res['my_hotel.png']['data']);
- $expected = array(
- 'my_hotel.png' => array(
- //'data' => 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');
- $this->assertTrue(!empty($res['my_other_hotel.png']['data']));
- unset($res['my_other_hotel.png']['data']);
- $expected = array(
- 'contentDisposition' => true,
- //'data' => file_get_contents($file),
- 'mimetype' => 'image/jpeg',
- 'contentId' => $cid,
- );
- $this->assertEquals($expected, $res['my_other_hotel.png']);
- }
- /**
- * EmailTest::testValidates()
- *
- * @return void
- */
- public function testValidates() {
- $this->Email = new TestEmail();
- $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);
- }
- /**
- * Html email
- *
- * @return void
- */
- public function testAddEmbeddedBlobAttachmentSend() {
- $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
- $this->Email = new TestEmail();
- $this->Email->emailFormat('both');
- $this->Email->to(Configure::read('Config.adminEmail'));
- $cid = $this->Email->addEmbeddedBlobAttachment(file_get_contents($file), 'my_hotel.png', 'image/png');
- $this->assertContains('@' . env('HTTP_HOST'), $cid);
- $html = '<head>
- <meta http-equiv="content-type" content="text/html; charset=utf-8" />
- <meta name="author" content="ohyeah" />
- <title>Untitled 6</title>
- </head>
- <body>
- test_embedded_blob_default äöü <img src="cid:' . $cid . '" /> end
- html-part
- </body>
- </html>';
- $text = trim(strip_tags($html));
- $this->Email->viewVars(compact('text', 'html'));
- $res = $this->Email->send();
- if ($error = $this->Email->getError()) {
- $this->out($error);
- }
- $this->assertEquals('', $this->Email->getError());
- $this->assertTrue((bool)$res);
- }
- public function _testComplexeHtmlWithEmbeddedImages() {
- $file = Plugin::path('Tools') . 'tests' . DS . 'test_files' . DS . 'img' . DS . 'hotel.png';
- $this->assertTrue(file_exists($file));
- //TODO
- }
- public function testWrapLongEmailContent() {
- $this->Email = new TestEmail();
- $html = <<<HTML
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html><head></head><body style="color: #000000; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 16px; text-align: left; vertical-align: top; margin: 0;">
- sjdf ojdshfdsf odsfh dsfhodsf hodshfhdsjdshfjdshfjdshfj dsjfh jdsfh ojds hfposjdf pohpojds fojds hfojds fpojds foijds fpodsij fojdsnhfojdshf dsufhpodsufds fuds foudshf ouds hfoudshf udsofhuds hfouds hfouds hfoudshf udsh fouhds fluds hflsdu hflsud hfuldsuhf dsfsjdf ojdshfdsf odsfh dsfhodsf hodshfhdsjdshfjdshfjdshfj dsjfh jdsfh ojds hfposjdf pohpojds fojds hfojds fpojds foijds fpodsij fojdsnhfojdshf dsufhpodsufds fuds foudshf ouds hfoudshf udsofhuds hfouds hfouds hfoudshf udsh fouhds fluds hflsdu hflsud hfuldsuhf dsfsjdf ojdshfdsf odsfh dsfhodsf hodshfhdsjdshfjdshfjdshfj dsjfh jdsfh ojds hfposjdf pohpojds fojds hfojds fpojds foijds fpodsij fojdsnhfojdshf dsufhpodsufds fuds foudshf ouds hfoudshf udsofhuds hfouds hfouds hfoudshf udsh fouhds fluds hflsdu hflsud hfuldsuhf dsfsjdf ojdshfdsf odsfh dsfhodsf hodshfhdsjdshfjdshfjdshfj dsjfh jdsfh ojds hfposjdf pohpojds fojds hfojds fpojds foijds fpodsij fojdsnhfojdshf dsufhpodsufds fuds foudshf ouds hfoudshf udsofhuds hfouds hfouds hfoudshf udsh fouhds fluds hflsdu hflsud hfuldsuhf dsf
- </body></html>
- HTML;
- //$html = str_replace(array("\r\n", "\n", "\r"), "", $html);
- $is = $this->Email->wrap($html);
- foreach ($is as $line => $content) {
- $this->assertTrue(strlen($content) <= Email::LINE_LENGTH_MUST);
- }
- $this->debug($is);
- $this->assertTrue(count($is) >= 5);
- }
- public function testWrapCustomized() {
- $this->Email = new TestEmail();
- $html = <<<HTML
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html><head></head><body style="color: #000000; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 16px; text-align: left; vertical-align: top; margin: 0;">
- sjdf ojdshfdsf odsfh dsfhodsf hodshfhdsjdshfjdshfjdshfj dsjfh jdsfh ojds hfposjdf pohpojds fojds hfojds fpojds foijds fpodsij fojdsnhfojdshf dsufhpodsufds fuds foudshf ouds hfoudshf udsofhuds hfouds hfouds hfoudshf udsh fouhds fluds hflsdu hflsud hfuldsuhf dsfsjdf ojdshfdsf odsfh dsfhodsf hodshfhdsjdshfjdshfjdshfj dsjfh jdsfh ojds hfposjdf pohpojds fojds hfojds fpojds foijds fpodsij fojdsnhfojdshf dsufhpodsufds fuds foudshf ouds hfoudshf udsofhuds hfouds hfouds hfoudshf udsh fouhds fluds hflsdu hflsud hfuldsuhf dsfsjdf ojdshfdsf odsfh dsfhodsf hodshfhdsjdshfjdshfjdshfj dsjfh jdsfh ojds hfposjdf pohpojds fojds hfojds fpojds foijds fpodsij fojdsnhfojdshf dsufhpodsufds fuds foudshf ouds hfoudshf udsofhuds hfouds hfouds hfoudshf udsh fouhds fluds hflsdu hflsud hfuldsuhf dsfsjdf ojdshfdsf odsfh dsfhodsf hodshfhdsjdshfjdshfjdshfj dsjfh jdsfh ojds hfposjdf pohpojds fojds hfojds fpojds foijds fpodsij fojdsnhfojdshf dsufhpodsufds fuds foudshf ouds hfoudshf udsofhuds hfouds hfouds hfoudshf udsh fouhds fluds hflsdu hflsud hfuldsuhf dsf
- </body></html>
- HTML;
- //$html = str_replace(array("\r\n", "\n", "\r"), "", $html);
- $this->Email->wrapLength(100);
- $is = $this->Email->wrap($html);
- foreach ($is as $line => $content) {
- $this->assertTrue(strlen($content) <= Email::LINE_LENGTH_MUST);
- }
- $this->debug($is);
- $this->assertTrue(count($is) >= 16);
- }
- }
|