This repository was archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathMboxTest.php
344 lines (275 loc) · 9.69 KB
/
MboxTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace ZendTest\Mail\Storage;
use PHPUnit\Framework\TestCase;
use Zend\Config;
use Zend\Mail\Storage;
/**
* @group Zend_Mail
*/
class MboxTest extends TestCase
{
protected $mboxOriginalFile;
protected $mboxFile;
protected $mboxFileUnix;
protected $tmpdir;
public function setUp()
{
if ($this->tmpdir == null) {
if (getenv('TESTS_ZEND_MAIL_TEMPDIR') != null) {
$this->tmpdir = getenv('TESTS_ZEND_MAIL_TEMPDIR');
} else {
$this->tmpdir = __DIR__ . '/../_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->mboxOriginalFile = __DIR__ . '/../_files/test.mbox/INBOX';
$this->mboxFile = $this->tmpdir . 'INBOX';
copy($this->mboxOriginalFile, $this->mboxFile);
}
public function tearDown()
{
unlink($this->mboxFile);
if ($this->mboxFileUnix) {
unlink($this->mboxFileUnix);
}
}
public function testLoadOk()
{
new Storage\Mbox(['filename' => $this->mboxFile]);
$this->addToAssertionCount(1);
}
public function testLoadConfig()
{
new Storage\Mbox(new Config\Config(['filename' => $this->mboxFile]));
$this->addToAssertionCount(1);
}
public function testNoParams()
{
$this->expectException('Zend\Mail\Storage\Exception\InvalidArgumentException');
new Storage\Mbox([]);
}
public function testLoadFailure()
{
$this->expectException('Zend\Mail\Storage\Exception\RuntimeException');
new Storage\Mbox(['filename' => 'ThisFileDoesNotExist']);
}
public function testLoadInvalid()
{
$this->expectException('Zend\Mail\Storage\Exception\InvalidArgumentException');
new Storage\Mbox(['filename' => __FILE__]);
}
public function testClose()
{
$mail = new Storage\Mbox(['filename' => $this->mboxFile]);
$mail->close();
$this->addToAssertionCount(1);
}
public function testHasTop()
{
$mail = new Storage\Mbox(['filename' => $this->mboxFile]);
$this->assertTrue($mail->hasTop);
}
public function testHasCreate()
{
$mail = new Storage\Mbox(['filename' => $this->mboxFile]);
$this->assertFalse($mail->hasCreate);
}
public function testNoop()
{
$mail = new Storage\Mbox(['filename' => $this->mboxFile]);
$this->assertTrue($mail->noop());
}
public function testCount()
{
$mail = new Storage\Mbox(['filename' => $this->mboxFile]);
$count = $mail->countMessages();
$this->assertEquals(7, $count);
}
public function testSize()
{
$mail = new Storage\Mbox(['filename' => $this->mboxFile]);
$shouldSizes = [1 => 397, 89, 694, 452, 497, 101, 139];
$sizes = $mail->getSize();
$this->assertEquals($shouldSizes, $sizes);
}
public function testSingleSize()
{
$mail = new Storage\Mbox(['filename' => $this->mboxFile]);
$size = $mail->getSize(2);
$this->assertEquals(89, $size);
}
public function testFetchHeader()
{
$mail = new Storage\Mbox(['filename' => $this->mboxFile]);
$subject = $mail->getMessage(1)->subject;
$this->assertEquals('Simple Message', $subject);
}
/*
public function testFetchTopBody()
{
$mail = new Storage\Mbox(array('filename' => $this->mboxFile));
$content = $mail->getHeader(3, 1)->getContent();
$this->assertEquals('Fair river! in thy bright, clear flow', trim($content));
}
*/
/**
* @group 6775
*/
public function testFetchMessageHeaderUnix()
{
$mail = new Storage\Mbox(['filename' => $this->getUnixMboxFile(), 'messageEOL' => "\n"]);
$subject = $mail->getMessage(1)->subject;
$this->assertEquals('Simple Message', $subject);
}
public function testFetchMessageHeader()
{
$mail = new Storage\Mbox(['filename' => $this->mboxFile]);
$subject = $mail->getMessage(1)->subject;
$this->assertEquals('Simple Message', $subject);
}
public function testFetchMessageBody()
{
$mail = new Storage\Mbox(['filename' => $this->mboxFile]);
$content = $mail->getMessage(3)->getContent();
list($content) = explode("\n", $content, 2);
$this->assertEquals('Fair river! in thy bright, clear flow', trim($content));
}
/**
* @group 6775
*/
public function testFetchMessageBodyUnix()
{
$mail = new Storage\Mbox(['filename' => $this->getUnixMboxFile(), 'messageEOL' => "\n"]);
$content = $mail->getMessage(3)->getContent();
list($content) = explode("\n", $content, 2);
$this->assertEquals('Fair river! in thy bright, clear flow', trim($content));
}
public function testFailedRemove()
{
$mail = new Storage\Mbox(['filename' => $this->mboxFile]);
$this->expectException('Zend\Mail\Storage\Exception\RuntimeException');
$mail->removeMessage(1);
}
public function testCapa()
{
$mail = new Storage\Mbox(['filename' => $this->mboxFile]);
$capa = $mail->getCapabilities();
$this->assertTrue(isset($capa['uniqueid']));
}
public function testValid()
{
$mail = new Storage\Mbox(['filename' => $this->mboxFile]);
$this->assertFalse($mail->valid());
$mail->rewind();
$this->assertTrue($mail->valid());
}
public function testOutOfBounds()
{
$mail = new Storage\Mbox(['filename' => $this->mboxFile]);
$this->expectException('Zend\Mail\Storage\Exception\OutOfBoundsException');
$mail->seek(INF);
}
public function testSleepWake()
{
$mail = new Storage\Mbox(['filename' => $this->mboxFile]);
$count = $mail->countMessages();
$content = $mail->getMessage(1)->getContent();
$serialzed = serialize($mail);
$mail = null;
unlink($this->mboxFile);
// otherwise this test is to fast for a mtime change
sleep(2);
copy($this->mboxOriginalFile, $this->mboxFile);
$mail = unserialize($serialzed);
$this->assertEquals($mail->countMessages(), $count);
$this->assertEquals($mail->getMessage(1)->getContent(), $content);
}
public function testSleepWakeRemoved()
{
$mail = new Storage\Mbox(['filename' => $this->mboxFile]);
$count = $mail->countMessages();
$content = $mail->getMessage(1)->getContent();
$serialzed = serialize($mail);
$mail = null;
$stat = stat($this->mboxFile);
chmod($this->mboxFile, 0);
clearstatcache();
$statcheck = stat($this->mboxFile);
if ($statcheck['mode'] % (8 * 8 * 8) !== 0) {
chmod($this->mboxFile, $stat['mode']);
$this->markTestSkipped(
'cannot remove read rights, which makes this test useless (maybe you are using Windows?)'
);
return;
}
$check = false;
try {
$mail = unserialize($serialzed);
} catch (\Exception $e) {
$check = true;
// test ok
}
chmod($this->mboxFile, $stat['mode']);
if (! $check) {
if (function_exists('posix_getuid') && posix_getuid() === 0) {
$this->markTestSkipped('seems like you are root and we therefore cannot test the error handling');
} elseif (! function_exists('posix_getuid')) {
$this->markTestSkipped('Can\t test if you\'re root and we therefore cannot test the error handling');
}
$this->fail('no exception while waking with non readable file');
}
}
public function testUniqueId()
{
$mail = new Storage\Mbox(['filename' => $this->mboxFile]);
$this->assertFalse($mail->hasUniqueId);
$this->assertEquals(1, $mail->getNumberByUniqueId($mail->getUniqueId(1)));
$ids = $mail->getUniqueId();
foreach ($ids as $num => $id) {
$this->assertEquals($num, $id);
if ($mail->getNumberByUniqueId($id) != $num) {
$this->fail('reverse lookup failed');
}
}
}
public function testShortMbox()
{
$fh = fopen($this->mboxFile, 'w');
fputs($fh, "From \r\nSubject: test\r\nFrom \r\nSubject: test2\r\n");
fclose($fh);
$mail = new Storage\Mbox(['filename' => $this->mboxFile]);
$this->assertEquals($mail->countMessages(), 2);
$this->assertEquals($mail->getMessage(1)->subject, 'test');
$this->assertEquals($mail->getMessage(1)->getContent(), '');
$this->assertEquals($mail->getMessage(2)->subject, 'test2');
$this->assertEquals($mail->getMessage(2)->getContent(), '');
}
/**
* @return string
*/
private function getUnixMboxFile()
{
$this->mboxFileUnix = $this->tmpdir . 'INBOX.unix';
copy(__DIR__ . '/../_files/test.mbox/INBOX.unix', $this->mboxFileUnix);
return $this->mboxFileUnix;
}
}