MboxTest.php 10 KB

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