| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664 |
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_Mail
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id$
- */
- /**
- * Zend_Mail_Storage_Folder_Maildir
- */
- require_once 'Zend/Mail/Storage/Writable/Maildir.php';
- /**
- * @category Zend
- * @package Zend_Mail
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @group Zend_Mail
- */
- class Zend_Mail_MaildirWritableTest extends PHPUnit_Framework_TestCase
- {
- protected $_params;
- protected $_originalDir;
- protected $_tmpdir;
- protected $_subdirs = array('.', '.subfolder', '.subfolder.test');
- public function setUp()
- {
- $this->_originalDir = dirname(__FILE__) . '/_files/test.maildir/';
- if (!is_dir($this->_originalDir . '/cur/')) {
- $this->markTestSkipped('You have to unpack maildir.tar in Zend/Mail/_files/test.maildir/ '
- . 'directory before enabling the maildir tests');
- return;
- }
- if ($this->_tmpdir == null) {
- if (TESTS_ZEND_MAIL_TEMPDIR != null) {
- $this->_tmpdir = TESTS_ZEND_MAIL_TEMPDIR;
- } else {
- $this->_tmpdir = dirname(__FILE__) . '/_files/test.tmp/';
- }
- if (!file_exists($this->_tmpdir)) {
- mkdir($this->_tmpdir);
- }
- $count = 0;
- $dh = opendir($this->_tmpdir);
- while (readdir($dh) !== false) {
- ++$count;
- }
- closedir($dh);
- if ($count != 2) {
- $this->markTestSkipped('Are you sure your tmp dir is a valid empty dir?');
- return;
- }
- }
- $this->_params = array();
- $this->_params['dirname'] = $this->_tmpdir;
- foreach ($this->_subdirs as $dir) {
- if ($dir != '.') {
- mkdir($this->_tmpdir . $dir);
- }
- foreach (array('cur', 'new') as $subdir) {
- if (!file_exists($this->_originalDir . $dir . '/' . $subdir)) {
- continue;
- }
- mkdir($this->_tmpdir . $dir . '/' . $subdir);
- $dh = opendir($this->_originalDir . $dir . '/' . $subdir);
- while (($entry = readdir($dh)) !== false) {
- $entry = $dir . '/' . $subdir . '/' . $entry;
- if (!is_file($this->_originalDir . $entry)) {
- continue;
- }
- copy($this->_originalDir . $entry, $this->_tmpdir . $entry);
- }
- closedir($dh);
- }
- copy($this->_originalDir . 'maildirsize', $this->_tmpdir . 'maildirsize');
- }
- }
- public function tearDown()
- {
- foreach (array_reverse($this->_subdirs) as $dir) {
- if (!file_exists($this->_tmpdir . $dir)) {
- continue;
- }
- foreach (array('cur', 'new', 'tmp') as $subdir) {
- if (!file_exists($this->_tmpdir . $dir . '/' . $subdir)) {
- continue;
- }
- $dh = opendir($this->_tmpdir . $dir . '/' . $subdir);
- while (($entry = readdir($dh)) !== false) {
- $entry = $this->_tmpdir . $dir . '/' . $subdir . '/' . $entry;
- if (!is_file($entry)) {
- continue;
- }
- unlink($entry);
- }
- closedir($dh);
- rmdir($this->_tmpdir . $dir . '/' . $subdir);
- }
- if ($dir != '.') {
- rmdir($this->_tmpdir . $dir);
- }
- }
- @unlink($this->_tmpdir . 'maildirsize');
- }
- public function testCreateFolder()
- {
- $mail = new Zend_Mail_Storage_Writable_Maildir($this->_params);
- $mail->createFolder('subfolder.test1');
- $mail->createFolder('test2', 'INBOX.subfolder');
- $mail->createFolder('test3', $mail->getFolders()->subfolder);
- $mail->createFolder('foo.bar');
- try {
- $mail->selectFolder($mail->getFolders()->subfolder->test1);
- $mail->selectFolder($mail->getFolders()->subfolder->test2);
- $mail->selectFolder($mail->getFolders()->subfolder->test3);
- $mail->selectFolder($mail->getFolders()->foo->bar);
- } catch (Exception $e) {
- $this->fail('could not get new folders');
- }
- // to tear down
- $this->_subdirs[] = '.subfolder.test1';
- $this->_subdirs[] = '.subfolder.test2';
- $this->_subdirs[] = '.subfolder.test3';
- $this->_subdirs[] = '.foo';
- $this->_subdirs[] = '.foo.bar';
- }
- public function testCreateFolderEmtpyPart()
- {
- $mail = new Zend_Mail_Storage_Writable_Maildir($this->_params);
- try {
- $mail->createFolder('foo..bar');
- } catch (Exception $e) {
- return; //ok
- }
- $this->fail('no exception while creating folder with empty part name');
- }
- public function testCreateFolderSlash()
- {
- $mail = new Zend_Mail_Storage_Writable_Maildir($this->_params);
- try {
- $mail->createFolder('foo/bar');
- } catch (Exception $e) {
- return; //ok
- }
- $this->fail('no exception while creating folder with slash');
- }
- public function testCreateFolderDirectorySeparator()
- {
- $mail = new Zend_Mail_Storage_Writable_Maildir($this->_params);
- try {
- $mail->createFolder('foo' . DIRECTORY_SEPARATOR . 'bar');
- } catch (Exception $e) {
- return; //ok
- }
- $this->fail('no exception while creating folder with DIRECTORY_SEPARATOR');
- }
- public function testCreateFolderExistingDir()
- {
- $mail = new Zend_Mail_Storage_Writable_Maildir($this->_params);
- unset($mail->getFolders()->subfolder->test);
- try {
- $mail->createFolder('subfolder.test');
- } catch (Exception $e) {
- return; // ok
- }
- $this->fail('should not be able to create existing folder');
- }
- public function testCreateExistingFolder()
- {
- $mail = new Zend_Mail_Storage_Writable_Maildir($this->_params);
- try {
- $mail->createFolder('subfolder.test');
- } catch (Exception $e) {
- return; // ok
- }
- $this->fail('should not be able to create existing folder');
- }
- public function testRemoveFolderName()
- {
- $mail = new Zend_Mail_Storage_Writable_Maildir($this->_params);
- $mail->removeFolder('INBOX.subfolder.test');
- try {
- $mail->selectFolder($mail->getFolders()->subfolder->test);
- } catch (Exception $e) {
- return; // ok
- }
- $this->fail('folder still exists');
- }
- public function testRemoveFolderInstance()
- {
- $mail = new Zend_Mail_Storage_Writable_Maildir($this->_params);
- $mail->removeFolder($mail->getFolders()->subfolder->test);
- try {
- $mail->selectFolder($mail->getFolders()->subfolder->test);
- } catch (Exception $e) {
- return; // ok
- }
- $this->fail('folder still exists');
- }
- public function testRemoveFolderWithChildren()
- {
- $mail = new Zend_Mail_Storage_Writable_Maildir($this->_params);
- try {
- $mail->removeFolder($mail->getFolders()->subfolder);
- } catch (Exception $e) {
- return; // ok
- }
- $this->fail('should not be able to remove a folder with children');
- }
- public function testRemoveSelectedFolder()
- {
- $mail = new Zend_Mail_Storage_Writable_Maildir($this->_params);
- $mail->selectFolder('subfolder.test');
- try {
- $mail->removeFolder('subfolder.test');
- } catch (Exception $e) {
- return; // ok
- }
- $this->fail('no error while removing selected folder');
- }
- public function testRemoveInvalidFolder()
- {
- $mail = new Zend_Mail_Storage_Writable_Maildir($this->_params);
- try {
- $mail->removeFolder('thisFolderDoestNotExist');
- } catch (Exception $e) {
- return; // ok
- }
- $this->fail('no error while removing invalid folder');
- }
- public function testRenameFolder()
- {
- $mail = new Zend_Mail_Storage_Writable_Maildir($this->_params);
- try {
- $mail->renameFolder('INBOX.subfolder', 'INBOX.foo');
- $mail->renameFolder($mail->getFolders()->foo, 'subfolder');
- } catch (Exception $e) {
- $this->fail('renaming failed');
- }
- try {
- $mail->renameFolder('INBOX', 'foo');
- } catch (Exception $e) {
- return; // ok
- }
- $this->fail('no error while renaming INBOX');
- }
- public function testRenameSelectedFolder()
- {
- $mail = new Zend_Mail_Storage_Writable_Maildir($this->_params);
- $mail->selectFolder('subfolder.test');
- try {
- $mail->renameFolder('subfolder.test', 'foo');
- } catch (Exception $e) {
- return; // ok
- }
- $this->fail('no error while renaming selected folder');
- }
- public function testRenameToChild()
- {
- $mail = new Zend_Mail_Storage_Writable_Maildir($this->_params);
- try {
- $mail->renameFolder('subfolder.test', 'subfolder.test.foo');
- } catch (Exception $e) {
- return; // ok
- }
- $this->fail('no error while renaming folder to child of old');
- }
- public function testAppend()
- {
- $mail = new Zend_Mail_Storage_Writable_Maildir($this->_params);
- $count = $mail->countMessages();
- $message = '';
- $message .= "From: me@example.org\r\n";
- $message .= "To: you@example.org\r\n";
- $message .= "Subject: append test\r\n";
- $message .= "\r\n";
- $message .= "This is a test\r\n";
- $mail->appendMessage($message);
- $this->assertEquals($count + 1, $mail->countMessages());
- $this->assertEquals($mail->getMessage($count + 1)->subject, 'append test');
- }
- public function testCopy()
- {
- $mail = new Zend_Mail_Storage_Writable_Maildir($this->_params);
- $mail->selectFolder('subfolder.test');
- $count = $mail->countMessages();
- $mail->selectFolder('INBOX');
- $message = $mail->getMessage(1);
- $mail->copyMessage(1, 'subfolder.test');
- $mail->selectFolder('subfolder.test');
- $this->assertEquals($count + 1, $mail->countMessages());
- $this->assertEquals($mail->getMessage($count + 1)->subject, $message->subject);
- $this->assertEquals($mail->getMessage($count + 1)->from, $message->from);
- $this->assertEquals($mail->getMessage($count + 1)->to, $message->to);
- try {
- $mail->copyMessage(1, 'justARandomFolder');
- } catch (Exception $e) {
- return; // ok
- }
- $this->fail('no error while copying to wrong folder');
- }
- public function testSetFlags()
- {
- $mail = new Zend_Mail_Storage_Writable_Maildir($this->_params);
- $mail->setFlags(1, array(Zend_Mail_Storage::FLAG_SEEN));
- $message = $mail->getMessage(1);
- $this->assertTrue($message->hasFlag(Zend_Mail_Storage::FLAG_SEEN));
- $this->assertFalse($message->hasFlag(Zend_Mail_Storage::FLAG_FLAGGED));
- $mail->setFlags(1, array(Zend_Mail_Storage::FLAG_SEEN, Zend_Mail_Storage::FLAG_FLAGGED));
- $message = $mail->getMessage(1);
- $this->assertTrue($message->hasFlag(Zend_Mail_Storage::FLAG_SEEN));
- $this->assertTrue($message->hasFlag(Zend_Mail_Storage::FLAG_FLAGGED));
- $mail->setFlags(1, array(Zend_Mail_Storage::FLAG_FLAGGED));
- $message = $mail->getMessage(1);
- $this->assertFalse($message->hasFlag(Zend_Mail_Storage::FLAG_SEEN));
- $this->assertTrue($message->hasFlag(Zend_Mail_Storage::FLAG_FLAGGED));
- try {
- $mail->setFlags(1, array(Zend_Mail_Storage::FLAG_RECENT));
- } catch (Exception $e) {
- return; // ok
- }
- $this->fail('should not be able to set recent flag');
- }
- public function testSetFlagsRemovedFile()
- {
- $mail = new Zend_Mail_Storage_Writable_Maildir($this->_params);
- unlink($this->_params['dirname'] . 'cur/1000000000.P1.example.org:2,S');
- try {
- $mail->setFlags(1, array(Zend_Mail_Storage::FLAG_FLAGGED));
- } catch (Exception $e) {
- return; // ok
- }
- $this->fail('should not be able to set flags with removed file');
- }
- public function testRemove()
- {
- $mail = new Zend_Mail_Storage_Writable_Maildir($this->_params);
- $count = $mail->countMessages();
- $mail->removeMessage(1);
- $this->assertEquals($mail->countMessages(), --$count);
- unset($mail[2]);
- $this->assertEquals($mail->countMessages(), --$count);
- }
- public function testRemoveRemovedFile()
- {
- $mail = new Zend_Mail_Storage_Writable_Maildir($this->_params);
- unlink($this->_params['dirname'] . 'cur/1000000000.P1.example.org:2,S');
- try {
- $mail->removeMessage(1);
- } catch (Exception $e) {
- return; // ok
- }
- $this->fail('should not be able to remove message which is already removed in fs');
- }
- public function testCheckQuota()
- {
- $mail = new Zend_Mail_Storage_Writable_Maildir($this->_params);
- $this->assertFalse($mail->checkQuota());
- }
- public function testCheckQuotaDetailed()
- {
- $mail = new Zend_Mail_Storage_Writable_Maildir($this->_params);
- $quotaResult = array(
- 'size' => 2596,
- 'count' => 6,
- 'quota' => array(
- 'count' => 10,
- 'L' => 1,
- 'size' => 3000
- ),
- 'over_quota' => false
- );
- $this->assertEquals($mail->checkQuota(true), $quotaResult);
- }
- public function testSetQuota()
- {
- $mail = new Zend_Mail_Storage_Writable_Maildir($this->_params);
- $this->assertNull($mail->getQuota());
- $mail->setQuota(true);
- $this->assertTrue($mail->getQuota());
- $mail->setQuota(false);
- $this->assertFalse($mail->getQuota());
- $mail->setQuota(array('size' => 100, 'count' => 2, 'X' => 0));
- $this->assertEquals($mail->getQuota(), array('size' => 100, 'count' => 2, 'X' => 0));
- $this->assertEquals($mail->getQuota(true), array('size' => 3000, 'L' => 1, 'count' => 10));
- $quotaResult = array(
- 'size' => 2596,
- 'count' => 6,
- 'quota' => array(
- 'size' => 100,
- 'count' => 2,
- 'X' => 0
- ),
- 'over_quota' => true
- );
- $this->assertEquals($mail->checkQuota(true, true), $quotaResult);
- $this->assertEquals($mail->getQuota(true), array('size' => 100, 'count' => 2, 'X' => 0));
- }
- public function testMissingMaildirsize()
- {
- $mail = new Zend_Mail_Storage_Writable_Maildir($this->_params);
- $this->assertEquals($mail->getQuota(true), array('size' => 3000, 'L' => 1, 'count' => 10));
- unlink($this->_tmpdir . 'maildirsize');
- $this->assertNull($mail->getQuota());
- try {
- $mail->getQuota(true);
- } catch(Zend_Mail_Exception $e) {
- // ok
- return;
- }
- $this->fail('get quota from file should fail if file is missing');
- }
- public function testMissingMaildirsizeWithFixedQuota()
- {
- $mail = new Zend_Mail_Storage_Writable_Maildir($this->_params);
- unlink($this->_tmpdir . 'maildirsize');
- $mail->setQuota(array('size' => 100, 'count' => 2, 'X' => 0));
- $quotaResult = array(
- 'size' => 2596,
- 'count' => 6,
- 'quota' => array(
- 'size' => 100,
- 'count' => 2,
- 'X' => 0
- ),
- 'over_quota' => true
- );
- $this->assertEquals($mail->checkQuota(true), $quotaResult);
- $this->assertEquals($mail->getQuota(true), $quotaResult['quota']);
- }
- public function testAppendMessage()
- {
- $mail = new Zend_Mail_Storage_Writable_Maildir($this->_params);
- $mail->setQuota(array('size' => 3000, 'count' => 6, 'X' => 0));
- $this->assertFalse($mail->checkQuota(false, true));
- $mail->appendMessage("Subject: test\r\n\r\n");
- $quotaResult = array(
- 'size' => 2613,
- 'count' => 7,
- 'quota' => array(
- 'size' => 3000,
- 'count' => 6,
- 'X' => 0
- ),
- 'over_quota' => true
- );
- $this->assertEquals($mail->checkQuota(true), $quotaResult);
- $mail->setQuota(false);
- $this->assertTrue($mail->checkQuota());
- try {
- $mail->appendMessage("Subject: test\r\n\r\n");
- } catch(Zend_Mail_Exception $e) {
- $this->fail('appending should not fail if quota check is not active');
- }
- $mail->setQuota(true);
- $this->assertTrue($mail->checkQuota());
- try {
- $mail->appendMessage("Subject: test\r\n\r\n");
- } catch(Zend_Mail_Exception $e) {
- // ok
- return;
- }
- $this->fail('appending after being over quota should fail');
- }
- public function testRemoveMessage()
- {
- $mail = new Zend_Mail_Storage_Writable_Maildir($this->_params);
- $mail->setQuota(array('size' => 3000, 'count' => 5, 'X' => 0));
- $this->assertTrue($mail->checkQuota(false, true));
- $mail->removeMessage(1);
- $this->assertFalse($mail->checkQuota());
- }
- public function testCopyMessage()
- {
- $mail = new Zend_Mail_Storage_Writable_Maildir($this->_params);
- $mail->setQuota(array('size' => 3000, 'count' => 6, 'X' => 0));
- $this->assertFalse($mail->checkQuota(false, true));
- $mail->copyMessage(1, 'subfolder');
- $quotaResult = array(
- 'size' => 2993,
- 'count' => 7,
- 'quota' => array(
- 'size' => 3000,
- 'count' => 6,
- 'X' => 0
- ),
- 'over_quota' => true
- );
- $this->assertEquals($mail->checkQuota(true), $quotaResult);
- }
- public function testAppendStream()
- {
- $mail = new Zend_Mail_Storage_Writable_Maildir($this->_params);
- $fh = fopen('php://memory', 'rw');
- fputs($fh, "Subject: test\r\n\r\n");
- fseek($fh, 0);
- $mail->appendMessage($fh);
- fclose($fh);
- $this->assertEquals($mail->getMessage($mail->countMessages())->subject, 'test');
- }
- public function testMove()
- {
- $mail = new Zend_Mail_Storage_Writable_Maildir($this->_params);
- $target = $mail->getFolders()->subfolder->test;
- $mail->selectFolder($target);
- $toCount = $mail->countMessages();
- $mail->selectFolder('INBOX');
- $fromCount = $mail->countMessages();
- $mail->moveMessage(1, $target);
- $this->assertEquals($fromCount - 1, $mail->countMessages());
- $mail->selectFolder($target);
- $this->assertEquals($toCount + 1, $mail->countMessages());
- }
- public function testInitExisting()
- {
- // this should be a noop
- Zend_Mail_Storage_Writable_Maildir::initMaildir($this->_params['dirname']);
- $mail = new Zend_Mail_Storage_Writable_Maildir($this->_params);
- $this->assertEquals($mail->countMessages(), 5);
- }
- public function testInit()
- {
- $this->tearDown();
- // should fail now
- $e = null;
- try {
- $mail = new Zend_Mail_Storage_Writable_Maildir($this->_params);
- } catch (Exception $e) {
- }
- if ($e === null) {
- $this->fail('empty maildir should not be accepted');
- }
- Zend_Mail_Storage_Writable_Maildir::initMaildir($this->_params['dirname']);
- $mail = new Zend_Mail_Storage_Writable_Maildir($this->_params);
- $this->assertEquals($mail->countMessages(), 0);
- }
- public function testCreate()
- {
- $this->tearDown();
- // should fail now
- $e = null;
- try {
- $mail = new Zend_Mail_Storage_Writable_Maildir($this->_params);
- } catch (Exception $e) {
- }
- if ($e === null) {
- $this->fail('empty maildir should not be accepted');
- }
- $this->_params['create'] = true;
- $mail = new Zend_Mail_Storage_Writable_Maildir($this->_params);
- $this->assertEquals($mail->countMessages(), 0);
- }
- }
|