Pop3Test.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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_Pop3
  24. */
  25. require_once 'Zend/Mail/Storage/Pop3.php';
  26. /**
  27. * Zend_Mail_Protocol_Pop3
  28. */
  29. require_once 'Zend/Mail/Protocol/Pop3.php';
  30. /**
  31. * Zend_Config
  32. */
  33. require_once 'Zend/Config.php';
  34. /**
  35. * PHPUnit test case
  36. */
  37. require_once 'PHPUnit/Framework/TestCase.php';
  38. /**
  39. * @category Zend
  40. * @package Zend_Mail
  41. * @subpackage UnitTests
  42. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  43. * @license http://framework.zend.com/license/new-bsd New BSD License
  44. * @group Zend_Mail
  45. */
  46. class Zend_Mail_Pop3Test extends PHPUnit_Framework_TestCase
  47. {
  48. protected $_params;
  49. public function setUp()
  50. {
  51. $this->_params = array('host' => TESTS_ZEND_MAIL_POP3_HOST,
  52. 'user' => TESTS_ZEND_MAIL_POP3_USER,
  53. 'password' => TESTS_ZEND_MAIL_POP3_PASSWORD);
  54. if (defined('TESTS_ZEND_MAIL_SERVER_TESTDIR') && TESTS_ZEND_MAIL_SERVER_TESTDIR) {
  55. if (!file_exists(TESTS_ZEND_MAIL_SERVER_TESTDIR . DIRECTORY_SEPARATOR . 'inbox')
  56. && !file_exists(TESTS_ZEND_MAIL_SERVER_TESTDIR . DIRECTORY_SEPARATOR . 'INBOX')) {
  57. $this->markTestSkipped('There is no file name "inbox" or "INBOX" in '
  58. . TESTS_ZEND_MAIL_SERVER_TESTDIR . '. I won\'t use it for testing. '
  59. . 'This is you safety net. If you think it is the right directory just '
  60. . 'create an empty file named INBOX or remove/deactived this message.');
  61. }
  62. $this->_cleanDir(TESTS_ZEND_MAIL_SERVER_TESTDIR);
  63. $this->_copyDir(dirname(__FILE__) . '/_files/test.' . TESTS_ZEND_MAIL_SERVER_FORMAT,
  64. TESTS_ZEND_MAIL_SERVER_TESTDIR);
  65. }
  66. }
  67. protected function _cleanDir($dir)
  68. {
  69. $dh = opendir($dir);
  70. while (($entry = readdir($dh)) !== false) {
  71. if ($entry == '.' || $entry == '..') {
  72. continue;
  73. }
  74. $fullname = $dir . DIRECTORY_SEPARATOR . $entry;
  75. if (is_dir($fullname)) {
  76. $this->_cleanDir($fullname);
  77. rmdir($fullname);
  78. } else {
  79. unlink($fullname);
  80. }
  81. }
  82. closedir($dh);
  83. }
  84. protected function _copyDir($dir, $dest)
  85. {
  86. $dh = opendir($dir);
  87. while (($entry = readdir($dh)) !== false) {
  88. if ($entry == '.' || $entry == '..' || $entry == '.svn') {
  89. continue;
  90. }
  91. $fullname = $dir . DIRECTORY_SEPARATOR . $entry;
  92. $destname = $dest . DIRECTORY_SEPARATOR . $entry;
  93. if (is_dir($fullname)) {
  94. mkdir($destname);
  95. $this->_copyDir($fullname, $destname);
  96. } else {
  97. copy($fullname, $destname);
  98. }
  99. }
  100. closedir($dh);
  101. }
  102. public function testConnectOk()
  103. {
  104. try {
  105. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  106. } catch (Exception $e) {
  107. $this->fail('exception raised while loading connection to pop3 server');
  108. }
  109. }
  110. public function testConnectConfig()
  111. {
  112. try {
  113. $mail = new Zend_Mail_Storage_Pop3(new Zend_Config($this->_params));
  114. } catch (Exception $e) {
  115. $this->fail('exception raised while loading connection to pop3 server');
  116. }
  117. }
  118. public function testConnectFailure()
  119. {
  120. $this->_params['host'] = 'example.example';
  121. try {
  122. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  123. } catch (Exception $e) {
  124. return; // test ok
  125. }
  126. // I can only hope noone installs a POP3 server there
  127. $this->fail('no exception raised while connecting to example.example');
  128. }
  129. public function testNoParams()
  130. {
  131. try {
  132. $mail = new Zend_Mail_Storage_Pop3(array());
  133. } catch (Exception $e) {
  134. return; // test ok
  135. }
  136. $this->fail('no exception raised with empty params');
  137. }
  138. public function testConnectSSL()
  139. {
  140. if (!TESTS_ZEND_MAIL_POP3_SSL) {
  141. return;
  142. }
  143. $this->_params['ssl'] = 'SSL';
  144. try {
  145. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  146. } catch (Exception $e) {
  147. $this->fail('exception raised while loading connection to pop3 server with SSL');
  148. }
  149. }
  150. public function testConnectTLS()
  151. {
  152. if (!TESTS_ZEND_MAIL_POP3_TLS) {
  153. return;
  154. }
  155. $this->_params['ssl'] = 'TLS';
  156. try {
  157. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  158. } catch (Exception $e) {
  159. $this->fail('exception raised while loading connection to pop3 server with TLS');
  160. }
  161. }
  162. public function testInvalidService()
  163. {
  164. $this->_params['port'] = TESTS_ZEND_MAIL_POP3_INVALID_PORT;
  165. try {
  166. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  167. } catch (Exception $e) {
  168. return; // test ok
  169. }
  170. $this->fail('no exception while connection to invalid port');
  171. }
  172. public function testWrongService()
  173. {
  174. $this->_params['port'] = TESTS_ZEND_MAIL_POP3_WRONG_PORT;
  175. try {
  176. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  177. } catch (Exception $e) {
  178. return; // test ok
  179. }
  180. $this->fail('no exception while connection to wrong port');
  181. }
  182. public function testClose()
  183. {
  184. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  185. try {
  186. $mail->close();
  187. } catch (Exception $e) {
  188. $this->fail('exception raised while closing pop3 connection');
  189. }
  190. }
  191. public function testHasTop()
  192. {
  193. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  194. $this->assertTrue($mail->hasTop);
  195. }
  196. public function testHasCreate()
  197. {
  198. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  199. $this->assertFalse($mail->hasCreate);
  200. }
  201. public function testNoop()
  202. {
  203. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  204. try {
  205. $mail->noop();
  206. } catch (Exception $e) {
  207. $this->fail('exception raised while doing nothing (noop)');
  208. }
  209. }
  210. public function testCount()
  211. {
  212. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  213. $count = $mail->countMessages();
  214. $this->assertEquals(7, $count);
  215. }
  216. public function testSize()
  217. {
  218. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  219. $shouldSizes = array(1 => 397, 89, 694, 452, 497, 101, 139);
  220. $sizes = $mail->getSize();
  221. $this->assertEquals($shouldSizes, $sizes);
  222. }
  223. public function testSingleSize()
  224. {
  225. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  226. $size = $mail->getSize(2);
  227. $this->assertEquals(89, $size);
  228. }
  229. public function testFetchHeader()
  230. {
  231. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  232. $subject = $mail->getMessage(1)->subject;
  233. $this->assertEquals('Simple Message', $subject);
  234. }
  235. /*
  236. public function testFetchTopBody()
  237. {
  238. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  239. $content = $mail->getHeader(3, 1)->getContent();
  240. $this->assertEquals('Fair river! in thy bright, clear flow', trim($content));
  241. }
  242. */
  243. public function testFetchMessageHeader()
  244. {
  245. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  246. $subject = $mail->getMessage(1)->subject;
  247. $this->assertEquals('Simple Message', $subject);
  248. }
  249. public function testFetchMessageBody()
  250. {
  251. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  252. $content = $mail->getMessage(3)->getContent();
  253. list($content, ) = explode("\n", $content, 2);
  254. $this->assertEquals('Fair river! in thy bright, clear flow', trim($content));
  255. }
  256. /*
  257. public function testFailedRemove()
  258. {
  259. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  260. try {
  261. $mail->removeMessage(1);
  262. } catch (Exception $e) {
  263. return; // test ok
  264. }
  265. $this->fail('no exception raised while deleting message (mbox is read-only)');
  266. }
  267. */
  268. public function testWithInstanceConstruction()
  269. {
  270. $protocol = new Zend_Mail_Protocol_Pop3($this->_params['host']);
  271. $mail = new Zend_Mail_Storage_Pop3($protocol);
  272. try {
  273. // because we did no login this has to throw an exception
  274. $mail->getMessage(1);
  275. } catch (Exception $e) {
  276. return; // test ok
  277. }
  278. $this->fail('no exception raised while fetching with wrong transport');
  279. }
  280. public function testRequestAfterClose()
  281. {
  282. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  283. $mail->close();
  284. try {
  285. $mail->getMessage(1);
  286. } catch (Exception $e) {
  287. return; // test ok
  288. }
  289. $this->fail('no exception raised while requesting after closing connection');
  290. }
  291. public function testServerCapa()
  292. {
  293. $mail = new Zend_Mail_Protocol_Pop3($this->_params['host']);
  294. $this->assertTrue(is_array($mail->capa()));
  295. }
  296. public function testServerUidl()
  297. {
  298. $mail = new Zend_Mail_Protocol_Pop3($this->_params['host']);
  299. $mail->login($this->_params['user'], $this->_params['password']);
  300. $uids = $mail->uniqueid();
  301. $this->assertEquals(count($uids), 7);
  302. $this->assertEquals($uids[1], $mail->uniqueid(1));
  303. }
  304. public function testRawHeader()
  305. {
  306. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  307. $this->assertTrue(strpos($mail->getRawHeader(1), "\r\nSubject: Simple Message\r\n") > 0);
  308. }
  309. public function testUniqueId()
  310. {
  311. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  312. $this->assertTrue($mail->hasUniqueId);
  313. $this->assertEquals(1, $mail->getNumberByUniqueId($mail->getUniqueId(1)));
  314. $ids = $mail->getUniqueId();
  315. foreach ($ids as $num => $id) {
  316. foreach ($ids as $inner_num => $inner_id) {
  317. if ($num == $inner_num) {
  318. continue;
  319. }
  320. if ($id == $inner_id) {
  321. $this->fail('not all ids are unique');
  322. }
  323. }
  324. if ($mail->getNumberByUniqueId($id) != $num) {
  325. $this->fail('reverse lookup failed');
  326. }
  327. }
  328. }
  329. public function testWrongUniqueId()
  330. {
  331. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  332. try {
  333. $mail->getNumberByUniqueId('this_is_an_invalid_id');
  334. } catch (Exception $e) {
  335. return; // test ok
  336. }
  337. $this->fail('no exception while getting number for invalid id');
  338. }
  339. public function testReadAfterClose()
  340. {
  341. $protocol = new Zend_Mail_Protocol_Pop3($this->_params['host']);
  342. $protocol->logout();
  343. try {
  344. $protocol->readResponse();
  345. } catch (Exception $e) {
  346. return; // test ok
  347. }
  348. $this->fail('no exception while reading from closed socket');
  349. }
  350. public function testRemove()
  351. {
  352. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  353. $count = $mail->countMessages();
  354. $mail->removeMessage(1);
  355. $this->assertEquals($mail->countMessages(), --$count);
  356. unset($mail[2]);
  357. $this->assertEquals($mail->countMessages(), --$count);
  358. }
  359. public function testDotMessage()
  360. {
  361. $mail = new Zend_Mail_Storage_Pop3($this->_params);
  362. $content = '';
  363. $content .= "Before the dot\r\n";
  364. $content .= ".\r\n";
  365. $content .= "is after the dot\r\n";
  366. $this->assertEquals($mail->getMessage(7)->getContent(), $content);
  367. }
  368. }