MboxTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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_Mbox
  28. */
  29. require_once 'Zend/Mail/Storage/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. */
  41. class Zend_Mail_MboxTest extends PHPUnit_Framework_TestCase
  42. {
  43. protected $_mboxOriginalFile;
  44. protected $_mboxFile;
  45. protected $_tmpdir;
  46. public function setUp()
  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 = dirname(__FILE__) . '/_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->_mboxOriginalFile = dirname(__FILE__) . '/_files/test.mbox/INBOX';
  69. $this->_mboxFile = $this->_tmpdir . 'INBOX';
  70. copy($this->_mboxOriginalFile, $this->_mboxFile);
  71. }
  72. public function tearDown()
  73. {
  74. unlink($this->_mboxFile);
  75. }
  76. public function testLoadOk()
  77. {
  78. try {
  79. $mail = new Zend_Mail_Storage_Mbox(array('filename' => $this->_mboxFile));
  80. } catch (Exception $e) {
  81. $this->fail('exception raised while loading mbox file');
  82. }
  83. }
  84. public function testLoadConfig()
  85. {
  86. try {
  87. $mail = new Zend_Mail_Storage_Mbox(new Zend_Config(array('filename' => $this->_mboxFile)));
  88. } catch (Exception $e) {
  89. $this->fail('exception raised while loading mbox folder');
  90. }
  91. }
  92. public function testNoParams()
  93. {
  94. try {
  95. $mail = new Zend_Mail_Storage_Mbox(array());
  96. } catch (Exception $e) {
  97. return; // test ok
  98. }
  99. $this->fail('no exception raised with empty params');
  100. }
  101. public function testLoadFailure()
  102. {
  103. try {
  104. $mail = new Zend_Mail_Storage_Mbox(array('filename' => 'ThisFileDoesNotExist'));
  105. } catch (Exception $e) {
  106. return; // test ok
  107. }
  108. $this->fail('no exception raised while loading unknown file');
  109. }
  110. public function testLoadInvalid()
  111. {
  112. try {
  113. $mail = new Zend_Mail_Storage_Mbox(array('filename' => __FILE__));
  114. } catch (Exception $e) {
  115. return; // test ok
  116. }
  117. $this->fail('no exception while loading invalid file');
  118. }
  119. public function testClose()
  120. {
  121. $mail = new Zend_Mail_Storage_Mbox(array('filename' => $this->_mboxFile));
  122. try {
  123. $mail->close();
  124. } catch (Exception $e) {
  125. $this->fail('exception raised while closing mbox file');
  126. }
  127. }
  128. public function testHasTop()
  129. {
  130. $mail = new Zend_Mail_Storage_Mbox(array('filename' => $this->_mboxFile));
  131. $this->assertTrue($mail->hasTop);
  132. }
  133. public function testHasCreate()
  134. {
  135. $mail = new Zend_Mail_Storage_Mbox(array('filename' => $this->_mboxFile));
  136. $this->assertFalse($mail->hasCreate);
  137. }
  138. public function testNoop()
  139. {
  140. $mail = new Zend_Mail_Storage_Mbox(array('filename' => $this->_mboxFile));
  141. try {
  142. $mail->noop();
  143. } catch (Exception $e) {
  144. $this->fail('exception raised while doing nothing (noop)');
  145. }
  146. }
  147. public function testCount()
  148. {
  149. $mail = new Zend_Mail_Storage_Mbox(array('filename' => $this->_mboxFile));
  150. $count = $mail->countMessages();
  151. $this->assertEquals(7, $count);
  152. }
  153. public function testSize()
  154. {
  155. $mail = new Zend_Mail_Storage_Mbox(array('filename' => $this->_mboxFile));
  156. $shouldSizes = array(1 => 397, 89, 694, 452, 497, 101, 139);
  157. $sizes = $mail->getSize();
  158. $this->assertEquals($shouldSizes, $sizes);
  159. }
  160. public function testSingleSize()
  161. {
  162. $mail = new Zend_Mail_Storage_Mbox(array('filename' => $this->_mboxFile));
  163. $size = $mail->getSize(2);
  164. $this->assertEquals(89, $size);
  165. }
  166. public function testFetchHeader()
  167. {
  168. $mail = new Zend_Mail_Storage_Mbox(array('filename' => $this->_mboxFile));
  169. $subject = $mail->getMessage(1)->subject;
  170. $this->assertEquals('Simple Message', $subject);
  171. }
  172. /*
  173. public function testFetchTopBody()
  174. {
  175. $mail = new Zend_Mail_Storage_Mbox(array('filename' => $this->_mboxFile));
  176. $content = $mail->getHeader(3, 1)->getContent();
  177. $this->assertEquals('Fair river! in thy bright, clear flow', trim($content));
  178. }
  179. */
  180. public function testFetchMessageHeader()
  181. {
  182. $mail = new Zend_Mail_Storage_Mbox(array('filename' => $this->_mboxFile));
  183. $subject = $mail->getMessage(1)->subject;
  184. $this->assertEquals('Simple Message', $subject);
  185. }
  186. public function testFetchMessageBody()
  187. {
  188. $mail = new Zend_Mail_Storage_Mbox(array('filename' => $this->_mboxFile));
  189. $content = $mail->getMessage(3)->getContent();
  190. list($content, ) = explode("\n", $content, 2);
  191. $this->assertEquals('Fair river! in thy bright, clear flow', trim($content));
  192. }
  193. public function testFailedRemove()
  194. {
  195. $mail = new Zend_Mail_Storage_Mbox(array('filename' => $this->_mboxFile));
  196. try {
  197. $mail->removeMessage(1);
  198. } catch (Exception $e) {
  199. return; // test ok
  200. }
  201. $this->fail('no exception raised while deleting message (mbox is read-only)');
  202. }
  203. public function testCapa()
  204. {
  205. $mail = new Zend_Mail_Storage_Mbox(array('filename' => $this->_mboxFile));
  206. $capa = $mail->getCapabilities();
  207. $this->assertTrue(isset($capa['uniqueid']));
  208. }
  209. public function testValid()
  210. {
  211. $mail = new Zend_Mail_Storage_Mbox(array('filename' => $this->_mboxFile));
  212. $this->assertFalse($mail->valid());
  213. $mail->rewind();
  214. $this->assertTrue($mail->valid());
  215. }
  216. public function testOutOfBounds()
  217. {
  218. $mail = new Zend_Mail_Storage_Mbox(array('filename' => $this->_mboxFile));
  219. try {
  220. $mail->seek(INF);
  221. } catch (Exception $e) {
  222. return; // test ok
  223. }
  224. $this->fail('no exception raised while seeking to not invalid id');
  225. }
  226. public function testSleepWake()
  227. {
  228. $mail = new Zend_Mail_Storage_Mbox(array('filename' => $this->_mboxFile));
  229. $count = $mail->countMessages();
  230. $content = $mail->getMessage(1)->getContent();
  231. $serialzed = serialize($mail);
  232. $mail = null;
  233. unlink($this->_mboxFile);
  234. // otherwise this test is to fast for a mtime change
  235. sleep(2);
  236. copy($this->_mboxOriginalFile, $this->_mboxFile);
  237. $mail = unserialize($serialzed);
  238. $this->assertEquals($mail->countMessages(), $count);
  239. $this->assertEquals($mail->getMessage(1)->getContent(), $content);
  240. }
  241. public function testSleepWakeRemoved()
  242. {
  243. $mail = new Zend_Mail_Storage_Mbox(array('filename' => $this->_mboxFile));
  244. $count = $mail->countMessages();
  245. $content = $mail->getMessage(1)->getContent();
  246. $serialzed = serialize($mail);
  247. $mail = null;
  248. $stat = stat($this->_mboxFile);
  249. chmod($this->_mboxFile, 0);
  250. clearstatcache();
  251. $statcheck = stat($this->_mboxFile);
  252. if ($statcheck['mode'] % (8 * 8 * 8) !== 0) {
  253. chmod($this->_mboxFile, $stat['mode']);
  254. $this->markTestSkipped('cannot remove read rights, which makes this test useless (maybe you are using Windows?)');
  255. return;
  256. }
  257. $check = false;
  258. try {
  259. $mail = unserialize($serialzed);
  260. } catch (Exception $e) {
  261. $check = true;
  262. // test ok
  263. }
  264. chmod($this->_mboxFile, $stat['mode']);
  265. if (!$check) {
  266. if (function_exists('posix_getuid') && posix_getuid() === 0) {
  267. $this->markTestSkipped('seems like you are root and we therefore cannot test the error handling');
  268. }
  269. $this->fail('no exception while waking with non readable file');
  270. }
  271. }
  272. public function testUniqueId()
  273. {
  274. $mail = new Zend_Mail_Storage_Mbox(array('filename' => $this->_mboxFile));
  275. $this->assertFalse($mail->hasUniqueId);
  276. $this->assertEquals(1, $mail->getNumberByUniqueId($mail->getUniqueId(1)));
  277. $ids = $mail->getUniqueId();
  278. foreach ($ids as $num => $id) {
  279. $this->assertEquals($num, $id);
  280. if ($mail->getNumberByUniqueId($id) != $num) {
  281. $this->fail('reverse lookup failed');
  282. }
  283. }
  284. }
  285. public function testShortMbox()
  286. {
  287. $fh = fopen($this->_mboxFile, 'w');
  288. fputs($fh, "From \r\nSubject: test\r\nFrom \r\nSubject: test2\r\n");
  289. fclose($fh);
  290. $mail = new Zend_Mail_Storage_Mbox(array('filename' => $this->_mboxFile));
  291. $this->assertEquals($mail->countMessages(), 2);
  292. $this->assertEquals($mail->getMessage(1)->subject, 'test');
  293. $this->assertEquals($mail->getMessage(1)->getContent(), '');
  294. $this->assertEquals($mail->getMessage(2)->subject, 'test2');
  295. $this->assertEquals($mail->getMessage(2)->getContent(), '');
  296. }
  297. }