6
* @subpackage UnitTests
11
* Zend_Mail_Storage_Maildir
13
require_once 'Zend/Mail/Storage/Maildir.php';
18
require_once 'Zend/Config.php';
23
require_once 'PHPUnit/Framework/TestCase.php';
29
* @subpackage UnitTests
31
class Zend_Mail_MaildirTest extends PHPUnit_Framework_TestCase
33
protected $_originalMaildir;
37
public function setUp()
39
$this->_originalMaildir = dirname(__FILE__) . '/_files/test.maildir/';
40
if (!is_dir($this->_originalMaildir . '/cur/')) {
41
$this->markTestSkipped('You have to unpack maildir.tar in Zend/Mail/_files/test.maildir/ '
42
. 'directory before enabling the maildir tests');
46
if ($this->_tmpdir == null) {
47
if (TESTS_ZEND_MAIL_TEMPDIR != null) {
48
$this->_tmpdir = TESTS_ZEND_MAIL_TEMPDIR;
50
$this->_tmpdir = dirname(__FILE__) . '/_files/test.tmp/';
52
if (!file_exists($this->_tmpdir)) {
53
mkdir($this->_tmpdir);
56
$dh = opendir($this->_tmpdir);
57
while (readdir($dh) !== false) {
62
$this->markTestSkipped('Are you sure your tmp dir is a valid empty dir?');
67
$this->_maildir = $this->_tmpdir;
69
foreach (array('cur', 'new') as $dir) {
70
mkdir($this->_tmpdir . $dir);
71
$dh = opendir($this->_originalMaildir . $dir);
72
while (($entry = readdir($dh)) !== false) {
73
$entry = $dir . '/' . $entry;
74
if (!is_file($this->_originalMaildir . $entry)) {
77
copy($this->_originalMaildir . $entry, $this->_tmpdir . $entry);
83
public function tearDown()
85
foreach (array('cur', 'new') as $dir) {
86
$dh = opendir($this->_tmpdir . $dir);
87
while (($entry = readdir($dh)) !== false) {
88
$entry = $this->_tmpdir . $dir . '/' . $entry;
89
if (!is_file($entry)) {
95
rmdir($this->_tmpdir . $dir);
99
public function testLoadOk()
102
$mail = new Zend_Mail_Storage_Maildir(array('dirname' => $this->_maildir));
103
} catch (Exception $e) {
104
$this->fail('exception raised while loading maildir');
108
public function testLoadConfig()
111
$mail = new Zend_Mail_Storage_Maildir(new Zend_Config(array('dirname' => $this->_maildir)));
112
} catch (Exception $e) {
113
$this->fail('exception raised while loading maildir');
117
public function testLoadFailure()
120
$mail = new Zend_Mail_Storage_Maildir(array('dirname' => '/This/Dir/Does/Not/Exist'));
121
} catch (Exception $e) {
125
$this->fail('no exception raised while loading unknown dir');
128
public function testLoadInvalid()
131
$mail = new Zend_Mail_Storage_Maildir(array('dirname' => dirname(__FILE__)));
132
} catch (Exception $e) {
136
$this->fail('no exception while loading invalid dir');
139
public function testClose()
141
$mail = new Zend_Mail_Storage_Maildir(array('dirname' => $this->_maildir));
145
} catch (Exception $e) {
146
$this->fail('exception raised while closing maildir');
150
public function testHasTop()
152
$mail = new Zend_Mail_Storage_Maildir(array('dirname' => $this->_maildir));
154
$this->assertTrue($mail->hasTop);
157
public function testHasCreate()
159
$mail = new Zend_Mail_Storage_Maildir(array('dirname' => $this->_maildir));
161
$this->assertFalse($mail->hasCreate);
164
public function testNoop()
166
$mail = new Zend_Mail_Storage_Maildir(array('dirname' => $this->_maildir));
170
} catch (Exception $e) {
171
$this->fail('exception raised while doing nothing (noop)');
175
public function testCount()
177
$mail = new Zend_Mail_Storage_Maildir(array('dirname' => $this->_maildir));
179
$count = $mail->countMessages();
180
$this->assertEquals(5, $count);
183
public function testSize()
185
$mail = new Zend_Mail_Storage_Maildir(array('dirname' => $this->_maildir));
186
$shouldSizes = array(1 => 397, 89, 694, 452, 497);
189
$sizes = $mail->getSize();
190
$this->assertEquals($shouldSizes, $sizes);
193
public function testSingleSize()
195
$mail = new Zend_Mail_Storage_Maildir(array('dirname' => $this->_maildir));
197
$size = $mail->getSize(2);
198
$this->assertEquals(89, $size);
201
public function testFetchHeader()
203
$mail = new Zend_Mail_Storage_Maildir(array('dirname' => $this->_maildir));
205
$subject = $mail->getMessage(1)->subject;
206
$this->assertEquals('Simple Message', $subject);
210
public function testFetchTopBody()
212
$mail = new Zend_Mail_Storage_Maildir(array('dirname' => $this->_maildir));
214
$content = $mail->getHeader(3, 1)->getContent();
215
$this->assertEquals('Fair river! in thy bright, clear flow', trim($content));
218
public function testFetchMessageHeader()
220
$mail = new Zend_Mail_Storage_Maildir(array('dirname' => $this->_maildir));
222
$subject = $mail->getMessage(1)->subject;
223
$this->assertEquals('Simple Message', $subject);
226
public function testFetchMessageBody()
228
$mail = new Zend_Mail_Storage_Maildir(array('dirname' => $this->_maildir));
230
$content = $mail->getMessage(3)->getContent();
231
list($content, ) = explode("\n", $content, 2);
232
$this->assertEquals('Fair river! in thy bright, clear flow', trim($content));
235
public function testFetchWrongSize()
237
$mail = new Zend_Mail_Storage_Maildir(array('dirname' => $this->_maildir));
241
} catch (Exception $e) {
245
$this->fail('no exception raised while getting size for message 0');
248
public function testFetchWrongMessageBody()
250
$mail = new Zend_Mail_Storage_Maildir(array('dirname' => $this->_maildir));
253
$mail->getMessage(0);
254
} catch (Exception $e) {
258
$this->fail('no exception raised while fetching message 0');
261
public function testFailedRemove()
263
$mail = new Zend_Mail_Storage_Maildir(array('dirname' => $this->_maildir));
266
$mail->removeMessage(1);
267
} catch (Exception $e) {
271
$this->fail('no exception raised while deleting message (maildir is read-only)');
274
public function testHasFlag()
276
$mail = new Zend_Mail_Storage_Maildir(array('dirname' => $this->_maildir));
278
$this->assertFalse($mail->getMessage(5)->hasFlag(Zend_Mail_Storage::FLAG_SEEN));
279
$this->assertTrue($mail->getMessage(5)->hasFlag(Zend_Mail_Storage::FLAG_RECENT));
280
$this->assertTrue($mail->getMessage(2)->hasFlag(Zend_Mail_Storage::FLAG_FLAGGED));
281
$this->assertFalse($mail->getMessage(2)->hasFlag(Zend_Mail_Storage::FLAG_ANSWERED));
284
public function testGetFlags()
286
$mail = new Zend_Mail_Storage_Maildir(array('dirname' => $this->_maildir));
288
$flags = $mail->getMessage(1)->getFlags();
289
$this->assertTrue(isset($flags[Zend_Mail_Storage::FLAG_SEEN]));
290
$this->assertTrue(in_array(Zend_Mail_Storage::FLAG_SEEN, $flags));
293
public function testUniqueId()
295
$mail = new Zend_Mail_Storage_Maildir(array('dirname' => $this->_maildir));
297
$this->assertTrue($mail->hasUniqueId);
298
$this->assertEquals(1, $mail->getNumberByUniqueId($mail->getUniqueId(1)));
300
$ids = $mail->getUniqueId();
301
$should_ids = array(1 => '1000000000.P1.example.org', '1000000001.P1.example.org', '1000000002.P1.example.org',
302
'1000000003.P1.example.org', '1000000004.P1.example.org');
303
foreach ($ids as $num => $id) {
304
$this->assertEquals($id, $should_ids[$num]);
306
if ($mail->getNumberByUniqueId($id) != $num) {
307
$this->fail('reverse lookup failed');
312
public function testWrongUniqueId()
314
$mail = new Zend_Mail_Storage_Maildir(array('dirname' => $this->_maildir));
316
$mail->getNumberByUniqueId('this_is_an_invalid_id');
317
} catch (Exception $e) {
321
$this->fail('no exception while getting number for invalid id');
324
public function isFileTest($dir)
326
if (file_exists($this->_maildir . '/' . $dir)) {
327
rename($this->_maildir . '/' . $dir, $this->_maildir . '/' . $dir . 'bak');
329
touch($this->_maildir . '/' . $dir);
333
$mail = new Zend_Mail_Storage_Maildir(array('dirname' => $this->_maildir));
334
} catch (Exception $e) {
339
unlink($this->_maildir . '/' . $dir);
340
if (file_exists($this->_maildir . '/' . $dir . 'bak')) {
341
rename($this->_maildir . '/' . $dir . 'bak', $this->_maildir . '/' . $dir);
345
$this->fail('no exception while loading invalid dir with ' . $dir . ' as file');
349
public function testCurIsFile()
351
$this->isFileTest('cur');
354
public function testNewIsFile()
356
$this->isFileTest('new');
359
public function testTmpIsFile()
361
$this->isFileTest('tmp');
364
public function notReadableTest($dir)
366
$stat = stat($this->_maildir . '/' . $dir);
367
chmod($this->_maildir . '/' . $dir, 0);
371
$mail = new Zend_Mail_Storage_Maildir(array('dirname' => $this->_maildir));
372
} catch (Exception $e) {
377
chmod($this->_maildir . '/' . $dir, $stat['mode']);
380
$this->fail('no exception while loading invalid dir with ' . $dir . ' not readable');
384
public function testNotReadableCur()
386
$this->notReadableTest('cur');
389
public function testNotReadableNew()
391
$this->notReadableTest('new');
394
public function testCountFlags()
396
$mail = new Zend_Mail_Storage_Maildir(array('dirname' => $this->_maildir));
397
$this->assertEquals($mail->countMessages(Zend_Mail_Storage::FLAG_DELETED), 0);
398
$this->assertEquals($mail->countMessages(Zend_Mail_Storage::FLAG_RECENT), 1);
399
$this->assertEquals($mail->countMessages(Zend_Mail_Storage::FLAG_FLAGGED), 1);
400
$this->assertEquals($mail->countMessages(Zend_Mail_Storage::FLAG_SEEN), 4);
401
$this->assertEquals($mail->countMessages(array(Zend_Mail_Storage::FLAG_SEEN, Zend_Mail_Storage::FLAG_FLAGGED)), 1);
402
$this->assertEquals($mail->countMessages(array(Zend_Mail_Storage::FLAG_SEEN, Zend_Mail_Storage::FLAG_RECENT)), 0);
405
public function testFetchPart()
407
$mail = new Zend_Mail_Storage_Maildir(array('dirname' => $this->_maildir));
408
$this->assertEquals($mail->getMessage(4)->getPart(2)->contentType, 'text/x-vertical');
411
public function testPartSize()
413
$mail = new Zend_Mail_Storage_Maildir(array('dirname' => $this->_maildir));
414
$this->assertEquals($mail->getMessage(4)->getPart(2)->getSize(), 88);
417
public function testSizePlusPlus()
419
rename($this->_maildir . '/cur/1000000000.P1.example.org:2,S', $this->_maildir . '/cur/1000000000.P1.example.org,S=123:2,S');
420
rename($this->_maildir . '/cur/1000000001.P1.example.org:2,FS', $this->_maildir . '/cur/1000000001.P1.example.org,S=456:2,FS');
421
$mail = new Zend_Mail_Storage_Maildir(array('dirname' => $this->_maildir));
422
$shouldSizes = array(1 => 123, 456, 694, 452, 497);
425
$sizes = $mail->getSize();
426
$this->assertEquals($shouldSizes, $sizes);
429
public function testSingleSizePlusPlus()
431
rename($this->_maildir . '/cur/1000000001.P1.example.org:2,FS', $this->_maildir . '/cur/1000000001.P1.example.org,S=456:2,FS');
432
$mail = new Zend_Mail_Storage_Maildir(array('dirname' => $this->_maildir));
434
$size = $mail->getSize(2);
435
$this->assertEquals(456, $size);