ImapTest.php 28 KB

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