ImapTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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_Imap
  24. */
  25. require_once 'Zend/Mail/Storage/Imap.php';
  26. /**
  27. * Zend_Mail_Protocol_Imap
  28. */
  29. require_once 'Zend/Mail/Protocol/Imap.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_ImapTest extends PHPUnit_Framework_TestCase
  47. {
  48. protected $_params;
  49. public function setUp()
  50. {
  51. $this->_params = array('host' => TESTS_ZEND_MAIL_IMAP_HOST,
  52. 'user' => TESTS_ZEND_MAIL_IMAP_USER,
  53. 'password' => TESTS_ZEND_MAIL_IMAP_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_Imap($this->_params);
  106. } catch (Exception $e) {
  107. $this->fail('exception raised while loading connection to imap server');
  108. }
  109. }
  110. public function testConnectConfig()
  111. {
  112. try {
  113. $mail = new Zend_Mail_Storage_Imap(new Zend_Config($this->_params));
  114. } catch (Exception $e) {
  115. $this->fail('exception raised while loading connection to imap server');
  116. }
  117. }
  118. public function testConnectFailure()
  119. {
  120. $this->_params['host'] = 'example.example';
  121. try {
  122. $mail = new Zend_Mail_Storage_Imap($this->_params);
  123. } catch (Exception $e) {
  124. return; // test ok
  125. }
  126. // I can only hope noone installs a imap 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_Imap(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_IMAP_SSL) {
  141. return;
  142. }
  143. $this->_params['ssl'] = 'SSL';
  144. try {
  145. $mail = new Zend_Mail_Storage_Imap($this->_params);
  146. } catch (Exception $e) {
  147. $this->fail('exception raised while loading connection to imap server with SSL');
  148. }
  149. }
  150. public function testConnectTLS()
  151. {
  152. if (!TESTS_ZEND_MAIL_IMAP_TLS) {
  153. return;
  154. }
  155. $this->_params['ssl'] = 'TLS';
  156. try {
  157. $mail = new Zend_Mail_Storage_Imap($this->_params);
  158. } catch (Exception $e) {
  159. $this->fail('exception raised while loading connection to imap server with TLS');
  160. }
  161. }
  162. public function testInvalidService()
  163. {
  164. $this->_params['port'] = TESTS_ZEND_MAIL_IMAP_INVALID_PORT;
  165. try {
  166. $mail = new Zend_Mail_Storage_Imap($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_IMAP_WRONG_PORT;
  175. try {
  176. $mail = new Zend_Mail_Storage_Imap($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 testWrongUsername()
  183. {
  184. // this also triggers ...{chars}<NL>token for coverage
  185. $this->_params['user'] = "there is no\nnobody";
  186. try {
  187. $mail = new Zend_Mail_Storage_Imap($this->_params);
  188. } catch (Exception $e) {
  189. return; // test ok
  190. }
  191. $this->fail('no exception while using wrong username');
  192. }
  193. public function testWithInstanceConstruction()
  194. {
  195. $protocol = new Zend_Mail_Protocol_Imap($this->_params['host']);
  196. $protocol->login($this->_params['user'], $this->_params['password']);
  197. // if $protocol is invalid the constructor fails while selecting INBOX
  198. $mail = new Zend_Mail_Storage_Imap($protocol);
  199. }
  200. public function testWithNotConnectedInstance()
  201. {
  202. $protocol = new Zend_Mail_Protocol_Imap();
  203. try {
  204. $mail = new Zend_Mail_Storage_Imap($protocol);
  205. } catch (Zend_Mail_Protocol_Exception $e) {
  206. return; // test ok
  207. }
  208. $this->fail('no exception while using not connected low-level class');
  209. }
  210. public function testWithNotLoggedInstance()
  211. {
  212. $protocol = new Zend_Mail_Protocol_Imap($this->_params['host']);
  213. try {
  214. $mail = new Zend_Mail_Storage_Imap($protocol);
  215. } catch (Zend_Mail_Storage_Exception $e) {
  216. return; // test ok
  217. }
  218. $this->fail('no exception while using not logged in low-level class');
  219. }
  220. public function testWrongFolder()
  221. {
  222. $this->_params['folder'] = 'this folder does not exist on your server';
  223. try {
  224. $mail = new Zend_Mail_Storage_Imap($this->_params);
  225. } catch (Exception $e) {
  226. return; // test ok
  227. }
  228. $this->fail('no exception with not existing folder');
  229. }
  230. public function testClose()
  231. {
  232. $mail = new Zend_Mail_Storage_Imap($this->_params);
  233. try {
  234. $mail->close();
  235. } catch (Exception $e) {
  236. $this->fail('exception raised while closing imap connection');
  237. }
  238. }
  239. /*
  240. currently imap has no top
  241. public function testHasTop()
  242. {
  243. $mail = new Zend_Mail_Storage_Imap($this->_params);
  244. $this->assertTrue($mail->hasTop);
  245. }
  246. */
  247. public function testHasCreate()
  248. {
  249. $mail = new Zend_Mail_Storage_Imap($this->_params);
  250. $this->assertFalse($mail->hasCreate);
  251. }
  252. public function testNoop()
  253. {
  254. $mail = new Zend_Mail_Storage_Imap($this->_params);
  255. try {
  256. $mail->noop();
  257. } catch (Exception $e) {
  258. $this->fail('exception raised while doing nothing (noop)');
  259. }
  260. }
  261. public function testCount()
  262. {
  263. $mail = new Zend_Mail_Storage_Imap($this->_params);
  264. $count = $mail->countMessages();
  265. $this->assertEquals(7, $count);
  266. }
  267. public function testSize()
  268. {
  269. $mail = new Zend_Mail_Storage_Imap($this->_params);
  270. $shouldSizes = array(1 => 397, 89, 694, 452, 497, 101, 139);
  271. $sizes = $mail->getSize();
  272. $this->assertEquals($shouldSizes, $sizes);
  273. }
  274. public function testSingleSize()
  275. {
  276. $mail = new Zend_Mail_Storage_Imap($this->_params);
  277. $size = $mail->getSize(2);
  278. $this->assertEquals(89, $size);
  279. }
  280. public function testFetchHeader()
  281. {
  282. $mail = new Zend_Mail_Storage_Imap($this->_params);
  283. $subject = $mail->getMessage(1)->subject;
  284. $this->assertEquals('Simple Message', $subject);
  285. }
  286. /*
  287. currently imap has no top
  288. public function testFetchTopBody()
  289. {
  290. $mail = new Zend_Mail_Storage_Imap($this->_params);
  291. $content = $mail->getHeader(3, 1)->getContent();
  292. $this->assertEquals('Fair river! in thy bright, clear flow', trim($content));
  293. }
  294. */
  295. public function testFetchMessageHeader()
  296. {
  297. $mail = new Zend_Mail_Storage_Imap($this->_params);
  298. $subject = $mail->getMessage(1)->subject;
  299. $this->assertEquals('Simple Message', $subject);
  300. }
  301. public function testFetchMessageBody()
  302. {
  303. $mail = new Zend_Mail_Storage_Imap($this->_params);
  304. $content = $mail->getMessage(3)->getContent();
  305. list($content, ) = explode("\n", $content, 2);
  306. $this->assertEquals('Fair river! in thy bright, clear flow', trim($content));
  307. }
  308. public function testRemove()
  309. {
  310. $mail = new Zend_Mail_Storage_Imap($this->_params);
  311. $count = $mail->countMessages();
  312. $mail->removeMessage(1);
  313. $this->assertEquals($mail->countMessages(), $count - 1);
  314. }
  315. public function testTooLateCount()
  316. {
  317. $mail = new Zend_Mail_Storage_Imap($this->_params);
  318. $mail->close();
  319. // after closing we can't count messages
  320. try {
  321. $mail->countMessages();
  322. } catch (Exception $e) {
  323. return; // test ok
  324. }
  325. $this->fail('no exception raised while counting messages on closed connection');
  326. }
  327. public function testLoadUnkownFolder()
  328. {
  329. $this->_params['folder'] = 'UnknownFolder';
  330. try {
  331. $mail = new Zend_Mail_Storage_Imap($this->_params);
  332. } catch (Exception $e) {
  333. return; // test ok
  334. }
  335. $this->fail('no exception raised while loading unknown folder');
  336. }
  337. public function testChangeFolder()
  338. {
  339. $mail = new Zend_Mail_Storage_Imap($this->_params);
  340. try {
  341. $mail->selectFolder('subfolder/test');
  342. } catch (Exception $e) {
  343. $this->fail('exception raised while selecting existing folder');
  344. }
  345. $this->assertEquals($mail->getCurrentFolder(), 'subfolder/test');
  346. }
  347. public function testUnknownFolder()
  348. {
  349. $mail = new Zend_Mail_Storage_Imap($this->_params);
  350. try {
  351. $mail->selectFolder('/Unknown/Folder/');
  352. } catch (Exception $e) {
  353. return; // test ok
  354. }
  355. $this->fail('no exception raised while selecting unknown folder');
  356. }
  357. public function testGlobalName()
  358. {
  359. $mail = new Zend_Mail_Storage_Imap($this->_params);
  360. try {
  361. // explicit call of __toString() needed for PHP < 5.2
  362. $this->assertEquals($mail->getFolders()->subfolder->__toString(), 'subfolder');
  363. } catch (Exception $e) {
  364. $this->fail('exception raised while selecting existing folder and getting global name');
  365. }
  366. }
  367. public function testLocalName()
  368. {
  369. $mail = new Zend_Mail_Storage_Imap($this->_params);
  370. try {
  371. $this->assertEquals($mail->getFolders()->subfolder->key(), 'test');
  372. } catch (Zend_Mail_Exception $e) {
  373. $this->fail('exception raised while selecting existing folder and getting local name');
  374. }
  375. }
  376. public function testKeyLocalName()
  377. {
  378. $mail = new Zend_Mail_Storage_Imap($this->_params);
  379. $iterator = new RecursiveIteratorIterator($mail->getFolders(), RecursiveIteratorIterator::SELF_FIRST);
  380. // we search for this folder because we can't assume a order while iterating
  381. $search_folders = array('subfolder' => 'subfolder',
  382. 'subfolder/test' => 'test',
  383. 'INBOX' => 'INBOX');
  384. $found_folders = array();
  385. foreach ($iterator as $localName => $folder) {
  386. if (!isset($search_folders[$folder->getGlobalName()])) {
  387. continue;
  388. }
  389. // explicit call of __toString() needed for PHP < 5.2
  390. $found_folders[$folder->__toString()] = $localName;
  391. }
  392. $this->assertEquals($search_folders, $found_folders);
  393. }
  394. public function testSelectable()
  395. {
  396. $mail = new Zend_Mail_Storage_Imap($this->_params);
  397. $iterator = new RecursiveIteratorIterator($mail->getFolders(), RecursiveIteratorIterator::SELF_FIRST);
  398. foreach ($iterator as $localName => $folder) {
  399. $this->assertEquals($localName, $folder->getLocalName());
  400. }
  401. }
  402. public function testCountFolder()
  403. {
  404. $mail = new Zend_Mail_Storage_Imap($this->_params);
  405. $mail->selectFolder('subfolder/test');
  406. $count = $mail->countMessages();
  407. $this->assertEquals(1, $count);
  408. }
  409. public function testSizeFolder()
  410. {
  411. $mail = new Zend_Mail_Storage_Imap($this->_params);
  412. $mail->selectFolder('subfolder/test');
  413. $sizes = $mail->getSize();
  414. $this->assertEquals(array(1 => 410), $sizes);
  415. }
  416. public function testFetchHeaderFolder()
  417. {
  418. $mail = new Zend_Mail_Storage_Imap($this->_params);
  419. $mail->selectFolder('subfolder/test');
  420. $subject = $mail->getMessage(1)->subject;
  421. $this->assertEquals('Message in subfolder', $subject);
  422. }
  423. public function testHasFlag()
  424. {
  425. $mail = new Zend_Mail_Storage_Imap($this->_params);
  426. $this->assertTrue($mail->getMessage(1)->hasFlag(Zend_Mail_Storage::FLAG_RECENT));
  427. }
  428. public function testGetFlags()
  429. {
  430. $mail = new Zend_Mail_Storage_Imap($this->_params);
  431. $flags = $mail->getMessage(1)->getFlags();
  432. $this->assertTrue(isset($flags[Zend_Mail_Storage::FLAG_RECENT]));
  433. $this->assertTrue(in_array(Zend_Mail_Storage::FLAG_RECENT, $flags));
  434. }
  435. public function testRawHeader()
  436. {
  437. $mail = new Zend_Mail_Storage_Imap($this->_params);
  438. $this->assertTrue(strpos($mail->getRawHeader(1), "\r\nSubject: Simple Message\r\n") > 0);
  439. }
  440. public function testUniqueId()
  441. {
  442. $mail = new Zend_Mail_Storage_Imap($this->_params);
  443. $this->assertTrue($mail->hasUniqueId);
  444. $this->assertEquals(1, $mail->getNumberByUniqueId($mail->getUniqueId(1)));
  445. $ids = $mail->getUniqueId();
  446. foreach ($ids as $num => $id) {
  447. foreach ($ids as $inner_num => $inner_id) {
  448. if ($num == $inner_num) {
  449. continue;
  450. }
  451. if ($id == $inner_id) {
  452. $this->fail('not all ids are unique');
  453. }
  454. }
  455. if ($mail->getNumberByUniqueId($id) != $num) {
  456. $this->fail('reverse lookup failed');
  457. }
  458. }
  459. }
  460. public function testWrongUniqueId()
  461. {
  462. $mail = new Zend_Mail_Storage_Imap($this->_params);
  463. try {
  464. $mail->getNumberByUniqueId('this_is_an_invalid_id');
  465. } catch (Exception $e) {
  466. return; // test ok
  467. }
  468. $this->fail('no exception while getting number for invalid id');
  469. }
  470. public function testCreateFolder()
  471. {
  472. $mail = new Zend_Mail_Storage_Imap($this->_params);
  473. $mail->createFolder('subfolder/test1');
  474. $mail->createFolder('test2', 'subfolder');
  475. $mail->createFolder('test3', $mail->getFolders()->subfolder);
  476. try {
  477. $mail->getFolders()->subfolder->test1;
  478. $mail->getFolders()->subfolder->test2;
  479. $mail->getFolders()->subfolder->test3;
  480. } catch (Exception $e) {
  481. $this->fail('could not get new folders');
  482. }
  483. }
  484. public function testCreateExistingFolder()
  485. {
  486. $mail = new Zend_Mail_Storage_Imap($this->_params);
  487. try {
  488. $mail->createFolder('subfolder/test');
  489. } catch (Exception $e) {
  490. return; // ok
  491. }
  492. $this->fail('should not be able to create existing folder');
  493. }
  494. public function testRemoveFolderName()
  495. {
  496. $mail = new Zend_Mail_Storage_Imap($this->_params);
  497. $mail->removeFolder('subfolder/test');
  498. try {
  499. $mail->getFolders()->subfolder->test;
  500. } catch (Exception $e) {
  501. return; // ok
  502. }
  503. $this->fail('folder still exists');
  504. }
  505. public function testRemoveFolderInstance()
  506. {
  507. $mail = new Zend_Mail_Storage_Imap($this->_params);
  508. $mail->removeFolder($mail->getFolders()->subfolder->test);
  509. try {
  510. $mail->getFolders()->subfolder->test;
  511. } catch (Exception $e) {
  512. return; // ok
  513. }
  514. $this->fail('folder still exists');
  515. }
  516. public function testRemoveInvalidFolder()
  517. {
  518. $mail = new Zend_Mail_Storage_Imap($this->_params);
  519. try {
  520. $mail->removeFolder('thisFolderDoestNotExist');
  521. } catch (Exception $e) {
  522. return; // ok
  523. }
  524. $this->fail('no error while removing invalid folder');
  525. }
  526. public function testRenameFolder()
  527. {
  528. $mail = new Zend_Mail_Storage_Imap($this->_params);
  529. try {
  530. $mail->renameFolder('subfolder/test', 'subfolder/test1');
  531. $mail->renameFolder($mail->getFolders()->subfolder->test1, 'subfolder/test');
  532. } catch (Exception $e) {
  533. $this->fail('renaming failed');
  534. }
  535. try {
  536. $mail->renameFolder('subfolder/test', 'INBOX');
  537. } catch (Exception $e) {
  538. return; // ok
  539. }
  540. $this->fail('no error while renaming folder to INBOX');
  541. }
  542. public function testAppend()
  543. {
  544. $mail = new Zend_Mail_Storage_Imap($this->_params);
  545. $count = $mail->countMessages();
  546. $message = '';
  547. $message .= "From: me@example.org\r\n";
  548. $message .= "To: you@example.org\r\n";
  549. $message .= "Subject: append test\r\n";
  550. $message .= "\r\n";
  551. $message .= "This is a test\r\n";
  552. $mail->appendMessage($message);
  553. $this->assertEquals($count + 1, $mail->countMessages());
  554. $this->assertEquals($mail->getMessage($count + 1)->subject, 'append test');
  555. try {
  556. $mail->appendMessage('');
  557. } catch (Exception $e) {
  558. return; // ok
  559. }
  560. $this->fail('no error while appending empty message');
  561. }
  562. public function testCopy()
  563. {
  564. $mail = new Zend_Mail_Storage_Imap($this->_params);
  565. $mail->selectFolder('subfolder/test');
  566. $count = $mail->countMessages();
  567. $mail->selectFolder('INBOX');
  568. $message = $mail->getMessage(1);
  569. $mail->copyMessage(1, 'subfolder/test');
  570. $mail->selectFolder('subfolder/test');
  571. $this->assertEquals($count + 1, $mail->countMessages());
  572. $this->assertEquals($mail->getMessage($count + 1)->subject, $message->subject);
  573. $this->assertEquals($mail->getMessage($count + 1)->from, $message->from);
  574. $this->assertEquals($mail->getMessage($count + 1)->to, $message->to);
  575. try {
  576. $mail->copyMessage(1, 'justARandomFolder');
  577. } catch (Exception $e) {
  578. return; // ok
  579. }
  580. $this->fail('no error while copying to wrong folder');
  581. }
  582. public function testSetFlags()
  583. {
  584. $mail = new Zend_Mail_Storage_Imap($this->_params);
  585. $mail->setFlags(1, array(Zend_Mail_Storage::FLAG_SEEN));
  586. $message = $mail->getMessage(1);
  587. $this->assertTrue($message->hasFlag(Zend_Mail_Storage::FLAG_SEEN));
  588. $this->assertFalse($message->hasFlag(Zend_Mail_Storage::FLAG_FLAGGED));
  589. $mail->setFlags(1, array(Zend_Mail_Storage::FLAG_SEEN, Zend_Mail_Storage::FLAG_FLAGGED));
  590. $message = $mail->getMessage(1);
  591. $this->assertTrue($message->hasFlag(Zend_Mail_Storage::FLAG_SEEN));
  592. $this->assertTrue($message->hasFlag(Zend_Mail_Storage::FLAG_FLAGGED));
  593. $mail->setFlags(1, array(Zend_Mail_Storage::FLAG_FLAGGED));
  594. $message = $mail->getMessage(1);
  595. $this->assertFalse($message->hasFlag(Zend_Mail_Storage::FLAG_SEEN));
  596. $this->assertTrue($message->hasFlag(Zend_Mail_Storage::FLAG_FLAGGED));
  597. $mail->setFlags(1, array('myflag'));
  598. $message = $mail->getMessage(1);
  599. $this->assertFalse($message->hasFlag(Zend_Mail_Storage::FLAG_SEEN));
  600. $this->assertFalse($message->hasFlag(Zend_Mail_Storage::FLAG_FLAGGED));
  601. $this->assertTrue($message->hasFlag('myflag'));
  602. try {
  603. $mail->setFlags(1, array(Zend_Mail_Storage::FLAG_RECENT));
  604. } catch (Exception $e) {
  605. return; // ok
  606. }
  607. $this->fail('should not be able to set recent flag');
  608. }
  609. public function testCapability()
  610. {
  611. $protocol = new Zend_Mail_Protocol_Imap($this->_params['host']);
  612. $protocol->login($this->_params['user'], $this->_params['password']);
  613. $capa = $protocol->capability();
  614. $this->assertTrue(is_array($capa));
  615. $this->assertEquals($capa[0], 'CAPABILITY');
  616. }
  617. public function testSelect()
  618. {
  619. $protocol = new Zend_Mail_Protocol_Imap($this->_params['host']);
  620. $protocol->login($this->_params['user'], $this->_params['password']);
  621. $status = $protocol->select('INBOX');
  622. $this->assertTrue(is_array($status['flags']));
  623. $this->assertEquals($status['exists'], 7);
  624. }
  625. public function testExamine()
  626. {
  627. $protocol = new Zend_Mail_Protocol_Imap($this->_params['host']);
  628. $protocol->login($this->_params['user'], $this->_params['password']);
  629. $status = $protocol->examine('INBOX');
  630. $this->assertTrue(is_array($status['flags']));
  631. $this->assertEquals($status['exists'], 7);
  632. }
  633. public function testClosedSocketNewlineToken()
  634. {
  635. $protocol = new Zend_Mail_Protocol_Imap($this->_params['host']);
  636. $protocol->login($this->_params['user'], $this->_params['password']);
  637. $protocol->logout();
  638. try {
  639. $protocol->select("foo\nbar");
  640. } catch (Exception $e) {
  641. return; // ok
  642. }
  643. $this->fail('no exception while using procol with closed socket');
  644. }
  645. public function testEscaping()
  646. {
  647. $protocol = new Zend_Mail_Protocol_Imap();
  648. $this->assertEquals($protocol->escapeString('foo'), '"foo"');
  649. $this->assertEquals($protocol->escapeString('f\\oo'), '"f\\\\oo"');
  650. $this->assertEquals($protocol->escapeString('f"oo'), '"f\\"oo"');
  651. $this->assertEquals($protocol->escapeString('foo', 'bar'), array('"foo"', '"bar"'));
  652. $this->assertEquals($protocol->escapeString("f\noo"), array('{4}', "f\noo"));
  653. $this->assertEquals($protocol->escapeList(array('foo')), '(foo)');
  654. $this->assertEquals($protocol->escapeList(array(array('foo'))), '((foo))');
  655. $this->assertEquals($protocol->escapeList(array('foo', 'bar')), '(foo bar)');
  656. }
  657. public function testFetch()
  658. {
  659. $protocol = new Zend_Mail_Protocol_Imap($this->_params['host']);
  660. $protocol->login($this->_params['user'], $this->_params['password']);
  661. $protocol->select('INBOX');
  662. $range = array_combine(range(1, 7), range(1, 7));
  663. $this->assertEquals($protocol->fetch('UID', 1, INF), $range);
  664. $this->assertEquals($protocol->fetch('UID', 1, 7), $range);
  665. $this->assertEquals($protocol->fetch('UID', range(1, 7)), $range);
  666. $this->assertTrue(is_numeric($protocol->fetch('UID', 1)));
  667. $result = $protocol->fetch(array('UID', 'FLAGS'), 1, INF);
  668. foreach ($result as $k => $v) {
  669. $this->assertEquals($k, $v['UID']);
  670. $this->assertTrue(is_array($v['FLAGS']));
  671. }
  672. try {
  673. $protocol->fetch('UID', 99);
  674. } catch (Exception $e) {
  675. return; // ok
  676. }
  677. $this->fail('no exception while fetching message');
  678. }
  679. public function testStore()
  680. {
  681. $protocol = new Zend_Mail_Protocol_Imap($this->_params['host']);
  682. $protocol->login($this->_params['user'], $this->_params['password']);
  683. $protocol->select('INBOX');
  684. $this->assertTrue($protocol->store(array('\Flagged'), 1));
  685. $this->assertTrue($protocol->store(array('\Flagged'), 1, null, '-'));
  686. $this->assertTrue($protocol->store(array('\Flagged'), 1, null, '+'));
  687. $result = $protocol->store(array('\Flagged'), 1, null, '', false);
  688. $this->assertTrue(in_array('\Flagged', $result[1]));
  689. $result = $protocol->store(array('\Flagged'), 1, null, '-', false);
  690. $this->assertFalse(in_array('\Flagged', $result[1]));
  691. $result = $protocol->store(array('\Flagged'), 1, null, '+', false);
  692. $this->assertTrue(in_array('\Flagged', $result[1]));
  693. }
  694. public function testMove()
  695. {
  696. $mail = new Zend_Mail_Storage_Imap($this->_params);
  697. $mail->selectFolder('subfolder/test');
  698. $toCount = $mail->countMessages();
  699. $mail->selectFolder('INBOX');
  700. $fromCount = $mail->countMessages();
  701. $mail->moveMessage(1, 'subfolder/test');
  702. $this->assertEquals($fromCount - 1, $mail->countMessages());
  703. $mail->selectFolder('subfolder/test');
  704. $this->assertEquals($toCount + 1, $mail->countMessages());
  705. }
  706. public function testCountFlags()
  707. {
  708. $mail = new Zend_Mail_Storage_Imap($this->_params);
  709. foreach ($mail as $id => $message) {
  710. $mail->setFlags($id, array());
  711. }
  712. $this->assertEquals($mail->countMessages(Zend_Mail_Storage::FLAG_SEEN), 0);
  713. $this->assertEquals($mail->countMessages(Zend_Mail_Storage::FLAG_ANSWERED), 0);
  714. $this->assertEquals($mail->countMessages(Zend_Mail_Storage::FLAG_FLAGGED), 0);
  715. $mail->setFlags(1, array(Zend_Mail_Storage::FLAG_SEEN, Zend_Mail_Storage::FLAG_ANSWERED));
  716. $mail->setFlags(2, array(Zend_Mail_Storage::FLAG_SEEN));
  717. $this->assertEquals($mail->countMessages(Zend_Mail_Storage::FLAG_SEEN), 2);
  718. $this->assertEquals($mail->countMessages(Zend_Mail_Storage::FLAG_ANSWERED), 1);
  719. $this->assertEquals($mail->countMessages(array(Zend_Mail_Storage::FLAG_SEEN, Zend_Mail_Storage::FLAG_ANSWERED)), 1);
  720. $this->assertEquals($mail->countMessages(array(Zend_Mail_Storage::FLAG_SEEN, Zend_Mail_Storage::FLAG_FLAGGED)), 0);
  721. $this->assertEquals($mail->countMessages(Zend_Mail_Storage::FLAG_FLAGGED), 0);
  722. }
  723. }