PageRenderTime 26ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Mail/MaildirFolderTest.php

https://github.com/agelang/zf2
PHP | 444 lines | 338 code | 67 blank | 39 comment | 27 complexity | 24678fea46d7c3a80bace2a6e02b4684 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Mail
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace ZendTest\Mail;
  25. use Zend\Mail\Storage\Folder;
  26. /**
  27. * @category Zend
  28. * @package Zend_Mail
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @group Zend_Mail
  33. */
  34. class MaildirFolderTest extends \PHPUnit_Framework_TestCase
  35. {
  36. protected $_params;
  37. protected $_originalDir;
  38. protected $_tmpdir;
  39. protected $_subdirs = array('.', '.subfolder', '.subfolder.test');
  40. public function setUp()
  41. {
  42. $this->_originalDir = __DIR__ . '/_files/test.maildir/';
  43. if (!is_dir($this->_originalDir . '/cur/')) {
  44. $this->markTestSkipped('You have to unpack maildir.tar in Zend/Mail/_files/test.maildir/ '
  45. . 'directory before enabling the maildir tests');
  46. return;
  47. }
  48. if ($this->_tmpdir == null) {
  49. if (TESTS_ZEND_MAIL_TEMPDIR != null) {
  50. $this->_tmpdir = TESTS_ZEND_MAIL_TEMPDIR;
  51. } else {
  52. $this->_tmpdir = __DIR__ . '/_files/test.tmp/';
  53. }
  54. if (!file_exists($this->_tmpdir)) {
  55. mkdir($this->_tmpdir);
  56. }
  57. $count = 0;
  58. $dh = opendir($this->_tmpdir);
  59. while (readdir($dh) !== false) {
  60. ++$count;
  61. }
  62. closedir($dh);
  63. if ($count != 2) {
  64. $this->markTestSkipped('Are you sure your tmp dir is a valid empty dir?');
  65. return;
  66. }
  67. }
  68. $this->_params = array();
  69. $this->_params['dirname'] = $this->_tmpdir;
  70. foreach ($this->_subdirs as $dir) {
  71. if ($dir != '.') {
  72. mkdir($this->_tmpdir . $dir);
  73. }
  74. foreach (array('cur', 'new') as $subdir) {
  75. if (!file_exists($this->_originalDir . $dir . '/' . $subdir)) {
  76. continue;
  77. }
  78. mkdir($this->_tmpdir . $dir . '/' . $subdir);
  79. $dh = opendir($this->_originalDir . $dir . '/' . $subdir);
  80. while (($entry = readdir($dh)) !== false) {
  81. $entry = $dir . '/' . $subdir . '/' . $entry;
  82. if (!is_file($this->_originalDir . $entry)) {
  83. continue;
  84. }
  85. copy($this->_originalDir . $entry, $this->_tmpdir . $entry);
  86. }
  87. closedir($dh);
  88. }
  89. }
  90. }
  91. public function tearDown()
  92. {
  93. foreach (array_reverse($this->_subdirs) as $dir) {
  94. foreach (array('cur', 'new') as $subdir) {
  95. if (!file_exists($this->_tmpdir . $dir . '/' . $subdir)) {
  96. continue;
  97. }
  98. $dh = opendir($this->_tmpdir . $dir . '/' . $subdir);
  99. while (($entry = readdir($dh)) !== false) {
  100. $entry = $this->_tmpdir . $dir . '/' . $subdir . '/' . $entry;
  101. if (!is_file($entry)) {
  102. continue;
  103. }
  104. unlink($entry);
  105. }
  106. closedir($dh);
  107. rmdir($this->_tmpdir . $dir . '/' . $subdir);
  108. }
  109. if ($dir != '.') {
  110. rmdir($this->_tmpdir . $dir);
  111. }
  112. }
  113. }
  114. public function testLoadOk()
  115. {
  116. try {
  117. $mail = new Folder\Maildir($this->_params);
  118. } catch (\Exception $e) {
  119. $this->fail('exception raised while loading Maildir folder');
  120. }
  121. }
  122. public function testLoadConfig()
  123. {
  124. try {
  125. $mail = new Folder\Maildir(new \Zend\Config\Config($this->_params));
  126. } catch (\Exception $e) {
  127. $this->fail('exception raised while loading Maildir folder');
  128. }
  129. }
  130. public function testNoParams()
  131. {
  132. try {
  133. $mail = new Folder\Maildir(array());
  134. } catch (\Exception $e) {
  135. return; // test ok
  136. }
  137. $this->fail('no exception raised with empty params');
  138. }
  139. public function testLoadFailure()
  140. {
  141. try {
  142. $mail = new Folder\Maildir(array('dirname' => 'This/Folder/Does/Not/Exist'));
  143. } catch (\Exception $e) {
  144. return; // test ok
  145. }
  146. $this->fail('no exception raised while loading unknown dirname');
  147. }
  148. public function testLoadUnkownFolder()
  149. {
  150. $this->_params['folder'] = 'UnknownFolder';
  151. try {
  152. $mail = new Folder\Maildir($this->_params);
  153. } catch (\Exception $e) {
  154. return; // test ok
  155. }
  156. $this->fail('no exception raised while loading unknown folder');
  157. }
  158. public function testChangeFolder()
  159. {
  160. $mail = new Folder\Maildir($this->_params);
  161. try {
  162. $mail->selectFolder('subfolder.test');
  163. } catch (\Exception $e) {
  164. $this->fail('exception raised while selecting existing folder');
  165. }
  166. $this->assertEquals($mail->getCurrentFolder(), 'subfolder.test');
  167. }
  168. public function testUnknownFolder()
  169. {
  170. $mail = new Folder\Maildir($this->_params);
  171. try {
  172. $mail->selectFolder('/Unknown/Folder/');
  173. } catch (\Exception $e) {
  174. return; // test ok
  175. }
  176. $this->fail('no exception raised while selecting unknown folder');
  177. }
  178. public function testGlobalName()
  179. {
  180. $mail = new Folder\Maildir($this->_params);
  181. try {
  182. // explicit call of __toString() needed for PHP < 5.2
  183. $this->assertEquals($mail->getFolders()->subfolder->__toString(), 'subfolder');
  184. } catch (\Exception $e) {
  185. $this->fail('exception raised while selecting existing folder and getting global name');
  186. }
  187. }
  188. public function testLocalName()
  189. {
  190. $mail = new Folder\Maildir($this->_params);
  191. try {
  192. $this->assertEquals($mail->getFolders()->subfolder->key(), 'test');
  193. } catch (\Exception $e) {
  194. $this->fail('exception raised while selecting existing folder and getting local name');
  195. }
  196. }
  197. public function testIterator()
  198. {
  199. $mail = new Folder\Maildir($this->_params);
  200. $iterator = new \RecursiveIteratorIterator($mail->getFolders(), \RecursiveIteratorIterator::SELF_FIRST);
  201. // we search for this folder because we can't assume a order while iterating
  202. $search_folders = array('subfolder' => 'subfolder',
  203. 'subfolder.test' => 'test',
  204. 'INBOX' => 'INBOX');
  205. $found_folders = array();
  206. foreach ($iterator as $localName => $folder) {
  207. if (!isset($search_folders[$folder->getGlobalName()])) {
  208. continue;
  209. }
  210. // explicit call of __toString() needed for PHP < 5.2
  211. $found_folders[$folder->__toString()] = $localName;
  212. }
  213. $this->assertEquals($search_folders, $found_folders);
  214. }
  215. public function testKeyLocalName()
  216. {
  217. $mail = new Folder\Maildir($this->_params);
  218. $iterator = new \RecursiveIteratorIterator($mail->getFolders(), \RecursiveIteratorIterator::SELF_FIRST);
  219. // we search for this folder because we can't assume a order while iterating
  220. $search_folders = array('subfolder' => 'subfolder',
  221. 'subfolder.test' => 'test',
  222. 'INBOX' => 'INBOX');
  223. $found_folders = array();
  224. foreach ($iterator as $localName => $folder) {
  225. if (!isset($search_folders[$folder->getGlobalName()])) {
  226. continue;
  227. }
  228. // explicit call of __toString() needed for PHP < 5.2
  229. $found_folders[$folder->__toString()] = $localName;
  230. }
  231. $this->assertEquals($search_folders, $found_folders);
  232. }
  233. public function testInboxEquals()
  234. {
  235. $mail = new Folder\Maildir($this->_params);
  236. $iterator = new \RecursiveIteratorIterator($mail->getFolders('INBOX.subfolder'), \RecursiveIteratorIterator::SELF_FIRST);
  237. // we search for this folder because we can't assume a order while iterating
  238. $search_folders = array('subfolder.test' => 'test');
  239. $found_folders = array();
  240. foreach ($iterator as $localName => $folder) {
  241. if (!isset($search_folders[$folder->getGlobalName()])) {
  242. continue;
  243. }
  244. // explicit call of __toString() needed for PHP < 5.2
  245. $found_folders[$folder->__toString()] = $localName;
  246. }
  247. $this->assertEquals($search_folders, $found_folders);
  248. }
  249. public function testSelectable()
  250. {
  251. $mail = new Folder\Maildir($this->_params);
  252. $iterator = new \RecursiveIteratorIterator($mail->getFolders(), \RecursiveIteratorIterator::SELF_FIRST);
  253. foreach ($iterator as $localName => $folder) {
  254. $this->assertEquals($localName, $folder->getLocalName());
  255. }
  256. }
  257. public function testCount()
  258. {
  259. $mail = new Folder\Maildir($this->_params);
  260. $count = $mail->countMessages();
  261. $this->assertEquals(5, $count);
  262. $mail->selectFolder('subfolder.test');
  263. $count = $mail->countMessages();
  264. $this->assertEquals(1, $count);
  265. }
  266. public function testSize()
  267. {
  268. $mail = new Folder\Maildir($this->_params);
  269. $shouldSizes = array(1 => 397, 89, 694, 452, 497);
  270. $sizes = $mail->getSize();
  271. $this->assertEquals($shouldSizes, $sizes);
  272. $mail->selectFolder('subfolder.test');
  273. $sizes = $mail->getSize();
  274. $this->assertEquals(array(1 => 467), $sizes);
  275. }
  276. public function testFetchHeader()
  277. {
  278. $mail = new Folder\Maildir($this->_params);
  279. $subject = $mail->getMessage(1)->subject;
  280. $this->assertEquals('Simple Message', $subject);
  281. $mail->selectFolder('subfolder.test');
  282. $subject = $mail->getMessage(1)->subject;
  283. $this->assertEquals('Message in subfolder', $subject);
  284. }
  285. public function testNotReadableFolder()
  286. {
  287. $stat = stat($this->_params['dirname'] . '.subfolder');
  288. chmod($this->_params['dirname'] . '.subfolder', 0);
  289. clearstatcache();
  290. $statcheck = stat($this->_params['dirname'] . '.subfolder');
  291. if ($statcheck['mode'] % (8 * 8 * 8) !== 0) {
  292. chmod($this->_params['dirname'] . '.subfolder', $stat['mode']);
  293. $this->markTestSkipped('cannot remove read rights, which makes this test useless (maybe you are using Windows?)');
  294. return;
  295. }
  296. $check = false;
  297. try {
  298. $mail = new Folder\Maildir($this->_params);
  299. } catch (\Exception $e) {
  300. $check = true;
  301. // test ok
  302. }
  303. chmod($this->_params['dirname'] . '.subfolder', $stat['mode']);
  304. if (!$check) {
  305. $this->fail('no exception while loading invalid dir with subfolder not readable');
  306. }
  307. }
  308. public function testNotReadableMaildir()
  309. {
  310. $stat = stat($this->_params['dirname']);
  311. chmod($this->_params['dirname'], 0);
  312. clearstatcache();
  313. $statcheck = stat($this->_params['dirname']);
  314. if ($statcheck['mode'] % (8 * 8 * 8) !== 0) {
  315. chmod($this->_params['dirname'], $stat['mode']);
  316. $this->markTestSkipped('cannot remove read rights, which makes this test useless (maybe you are using Windows?)');
  317. return;
  318. }
  319. $check = false;
  320. try {
  321. $mail = new Folder\Maildir($this->_params);
  322. } catch (\Exception $e) {
  323. $check = true;
  324. // test ok
  325. }
  326. chmod($this->_params['dirname'], $stat['mode']);
  327. if (!$check) {
  328. $this->fail('no exception while loading not readable maildir');
  329. }
  330. }
  331. public function testGetInvalidFolder()
  332. {
  333. $mail = new Folder\Maildir($this->_params);
  334. $root = $mail->getFolders();
  335. $root->foobar = new Folder('foobar', DIRECTORY_SEPARATOR . 'foobar');
  336. try {
  337. $mail->selectFolder('foobar');
  338. } catch (\Exception $e) {
  339. return; // ok
  340. }
  341. $this->fail('no error while getting invalid folder');
  342. }
  343. public function testGetVanishedFolder()
  344. {
  345. $mail = new Folder\Maildir($this->_params);
  346. $root = $mail->getFolders();
  347. $root->foobar = new Folder('foobar', 'foobar');
  348. try {
  349. $mail->selectFolder('foobar');
  350. } catch (\Exception $e) {
  351. return; // ok
  352. }
  353. $this->fail('no error while getting vanished folder');
  354. }
  355. public function testGetNotSelectableFolder()
  356. {
  357. $mail = new Folder\Maildir($this->_params);
  358. $root = $mail->getFolders();
  359. $root->foobar = new Folder('foobar', 'foobar', false);
  360. try {
  361. $mail->selectFolder('foobar');
  362. } catch (\Exception $e) {
  363. return; // ok
  364. }
  365. $this->fail('no error while getting not selectable folder');
  366. }
  367. public function testWithAdditionalFolder()
  368. {
  369. mkdir($this->_params['dirname'] . '.xyyx');
  370. mkdir($this->_params['dirname'] . '.xyyx/cur');
  371. $mail = new Folder\Maildir($this->_params);
  372. $mail->selectFolder('xyyx');
  373. $this->assertEquals($mail->countMessages(), 0);
  374. rmdir($this->_params['dirname'] . '.xyyx/cur');
  375. rmdir($this->_params['dirname'] . '.xyyx');
  376. }
  377. }