2
0

MaildirFolderTest.php 15 KB

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