MboxFolderTest.php 14 KB

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