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 pathMboxFolderTest.php
347 lines (288 loc) · 11.3 KB
/
MboxFolderTest.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
345
346
347
<?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 RecursiveIteratorIterator;
use Zend\Config;
use Zend\Mail\Storage\Folder;
/**
* @group Zend_Mail
*/
class MboxFolderTest extends TestCase
{
protected $params;
protected $originalDir;
protected $tmpdir;
protected $subdirs = ['.', 'subfolder'];
public function setUp()
{
$this->originalDir = __DIR__ . '/../_files/test.mbox/';
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->params = [];
$this->params['dirname'] = $this->tmpdir;
$this->params['folder'] = 'INBOX';
foreach ($this->subdirs as $dir) {
if ($dir != '.') {
mkdir($this->tmpdir . $dir);
}
$dh = opendir($this->originalDir . $dir);
while (($entry = readdir($dh)) !== false) {
$entry = $dir . '/' . $entry;
if (! is_file($this->originalDir . $entry)) {
continue;
}
copy($this->originalDir . $entry, $this->tmpdir . $entry);
}
closedir($dh);
}
}
public function tearDown()
{
foreach (array_reverse($this->subdirs) as $dir) {
$dh = opendir($this->tmpdir . $dir);
while (($entry = readdir($dh)) !== false) {
$entry = $this->tmpdir . $dir . '/' . $entry;
if (! is_file($entry)) {
continue;
}
unlink($entry);
}
closedir($dh);
if ($dir != '.') {
rmdir($this->tmpdir . $dir);
}
}
}
public function testLoadOk()
{
new Folder\Mbox($this->params);
$this->addToAssertionCount(1);
}
public function testLoadConfig()
{
new Folder\Mbox(new Config\Config($this->params));
$this->addToAssertionCount(1);
}
public function testNoParams()
{
$this->expectException('Zend\Mail\Storage\Exception\InvalidArgumentException');
new Folder\Mbox([]);
}
public function testFilenameParam()
{
$this->expectException('Zend\Mail\Storage\Exception\InvalidArgumentException');
// filename is not allowed in this subclass
new Folder\Mbox(['filename' => 'foobar']);
}
public function testLoadFailure()
{
$this->expectException('Zend\Mail\Storage\Exception\InvalidArgumentException');
new Folder\Mbox(['dirname' => 'This/Folder/Does/Not/Exist']);
}
public function testLoadUnkownFolder()
{
$this->params['folder'] = 'UnknownFolder';
$this->expectException('Zend\Mail\Storage\Exception\InvalidArgumentException');
new Folder\Mbox($this->params);
}
public function testChangeFolder()
{
$mail = new Folder\Mbox($this->params);
$mail->selectFolder(DIRECTORY_SEPARATOR . 'subfolder' . DIRECTORY_SEPARATOR . 'test');
$this->assertEquals(
$mail->getCurrentFolder(),
DIRECTORY_SEPARATOR . 'subfolder' . DIRECTORY_SEPARATOR . 'test'
);
}
public function testChangeFolderUnselectable()
{
$mail = new Folder\Mbox($this->params);
$this->expectException('Zend\Mail\Storage\Exception\RuntimeException');
$mail->selectFolder(DIRECTORY_SEPARATOR . 'subfolder');
}
public function testUnknownFolder()
{
$mail = new Folder\Mbox($this->params);
$this->expectException('Zend\Mail\Storage\Exception\InvalidArgumentException');
$mail->selectFolder('/Unknown/Folder/');
}
public function testGlobalName()
{
$mail = new Folder\Mbox($this->params);
$this->assertEquals($mail->getFolders()->subfolder->__toString(), DIRECTORY_SEPARATOR . 'subfolder');
}
public function testLocalName()
{
$mail = new Folder\Mbox($this->params);
$this->assertEquals($mail->getFolders()->subfolder->key(), 'test');
}
public function testIterator()
{
$mail = new Folder\Mbox($this->params);
$iterator = new RecursiveIteratorIterator($mail->getFolders(), RecursiveIteratorIterator::SELF_FIRST);
// we search for this folder because we cannot assume an order while iterating
$search_folders = [
DIRECTORY_SEPARATOR . 'subfolder' => 'subfolder',
DIRECTORY_SEPARATOR . 'subfolder' . DIRECTORY_SEPARATOR . 'test' => 'test',
DIRECTORY_SEPARATOR . 'INBOX' => 'INBOX'
];
$found_folders = [];
foreach ($iterator as $localName => $folder) {
if (! isset($search_folders[$folder->getGlobalName()])) {
continue;
}
// explicit call of __toString() needed for PHP < 5.2
$found_folders[$folder->__toString()] = $localName;
}
$this->assertEquals($search_folders, $found_folders);
}
public function testKeyLocalName()
{
$mail = new Folder\Mbox($this->params);
$iterator = new RecursiveIteratorIterator($mail->getFolders(), RecursiveIteratorIterator::SELF_FIRST);
// we search for this folder because we cannot assume an order while iterating
$search_folders = [
DIRECTORY_SEPARATOR . 'subfolder' => 'subfolder',
DIRECTORY_SEPARATOR . 'subfolder' . DIRECTORY_SEPARATOR . 'test' => 'test',
DIRECTORY_SEPARATOR . 'INBOX' => 'INBOX'
];
$found_folders = [];
foreach ($iterator as $localName => $folder) {
if (! isset($search_folders[$folder->getGlobalName()])) {
continue;
}
// explicit call of __toString() needed for PHP < 5.2
$found_folders[$folder->__toString()] = $localName;
}
$this->assertEquals($search_folders, $found_folders);
}
public function testSelectable()
{
$mail = new Folder\Mbox($this->params);
$iterator = new RecursiveIteratorIterator($mail->getFolders(), RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $localName => $folder) {
$this->assertEquals($localName, $folder->getLocalName());
}
}
public function testCount()
{
$mail = new Folder\Mbox($this->params);
$count = $mail->countMessages();
$this->assertEquals(7, $count);
$mail->selectFolder(DIRECTORY_SEPARATOR . 'subfolder' . DIRECTORY_SEPARATOR . 'test');
$count = $mail->countMessages();
$this->assertEquals(1, $count);
}
public function testSize()
{
$mail = new Folder\Mbox($this->params);
$shouldSizes = [1 => 397, 89, 694, 452, 497, 101, 139];
$sizes = $mail->getSize();
$this->assertEquals($shouldSizes, $sizes);
$mail->selectFolder(DIRECTORY_SEPARATOR . 'subfolder' . DIRECTORY_SEPARATOR . 'test');
$sizes = $mail->getSize();
$this->assertEquals([1 => 410], $sizes);
}
public function testFetchHeader()
{
$mail = new Folder\Mbox($this->params);
$subject = $mail->getMessage(1)->subject;
$this->assertEquals('Simple Message', $subject);
$mail->selectFolder(DIRECTORY_SEPARATOR . 'subfolder' . DIRECTORY_SEPARATOR . 'test');
$subject = $mail->getMessage(1)->subject;
$this->assertEquals('Message in subfolder', $subject);
}
public function testSleepWake()
{
$mail = new Folder\Mbox($this->params);
$mail->selectFolder(DIRECTORY_SEPARATOR . 'subfolder' . DIRECTORY_SEPARATOR . 'test');
$count = $mail->countMessages();
$content = $mail->getMessage(1)->getContent();
$serialzed = serialize($mail);
$mail = null;
$mail = unserialize($serialzed);
$this->assertEquals($mail->countMessages(), $count);
$this->assertEquals($mail->getMessage(1)->getContent(), $content);
$mail->selectFolder(DIRECTORY_SEPARATOR . 'subfolder' . DIRECTORY_SEPARATOR . 'test');
$this->assertEquals($mail->countMessages(), $count);
$this->assertEquals($mail->getMessage(1)->getContent(), $content);
}
public function testNotMboxFile()
{
touch($this->params['dirname'] . 'foobar');
$mail = new Folder\Mbox($this->params);
$this->expectException('Zend\Mail\Storage\Exception\InvalidArgumentException');
$mail->getFolders()->foobar;
}
public function testNotReadableFolder()
{
$stat = stat($this->params['dirname'] . 'subfolder');
chmod($this->params['dirname'] . 'subfolder', 0);
clearstatcache();
$statcheck = stat($this->params['dirname'] . 'subfolder');
if ($statcheck['mode'] % (8 * 8 * 8) !== 0) {
chmod($this->params['dirname'] . 'subfolder', $stat['mode']);
$this->markTestSkipped(
'cannot remove read rights, which makes this test useless (maybe you are using Windows?)'
);
return;
}
$check = false;
try {
$mail = new Folder\Mbox($this->params);
} catch (\Exception $e) {
$check = true;
// test ok
}
chmod($this->params['dirname'] . 'subfolder', $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 loading invalid dir with subfolder not readable');
}
}
public function testGetInvalidFolder()
{
$mail = new Folder\Mbox($this->params);
$root = $mail->getFolders();
$root->foobar = new Folder('x', 'x');
$this->expectException('Zend\Mail\Storage\Exception\InvalidArgumentException');
$mail->getFolders('foobar');
}
public function testGetVanishedFolder()
{
$mail = new Folder\Mbox($this->params);
$root = $mail->getFolders();
$root->foobar = new Folder('foobar', DIRECTORY_SEPARATOR . 'foobar');
$this->expectException('Zend\Mail\Storage\Exception\RuntimeException');
$mail->selectFolder('foobar');
}
}