MaildirFolderTest.php 15 KB

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