Pop3Test.php 12 KB

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