MboxTest.php 10 KB

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