PageRenderTime 48ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/tine20/Felamimail/Controller/Cache/MessageTest.php

https://github.com/pschuele/Tine-2.0-Open-Source-Groupware-and-CRM
PHP | 308 lines | 159 code | 42 blank | 107 comment | 15 complexity | 9d272849af9de02854ee5675b96600f0 MD5 | raw file
  1. <?php
  2. /**
  3. * Tine 2.0 - http://www.tine20.org
  4. *
  5. * @package Felamimail
  6. * @license http://www.gnu.org/licenses/agpl.html
  7. * @copyright Copyright (c) 2010-2011 Metaways Infosystems GmbH (http://www.metaways.de)
  8. * @author Philipp Schüle <p.schuele@metaways.de>
  9. */
  10. /**
  11. * Test helper
  12. */
  13. require_once dirname(dirname(dirname(dirname(__FILE__)))) . DIRECTORY_SEPARATOR . 'TestHelper.php';
  14. /**
  15. * Test class for Felamimail_Controller_Cache_*
  16. */
  17. class Felamimail_Controller_Cache_MessageTest extends PHPUnit_Framework_TestCase
  18. {
  19. /**
  20. * @var Felamimail_Controller_Cache_Message
  21. */
  22. protected $_controller = NULL;
  23. /**
  24. * @var Felamimail_Backend_Imap
  25. */
  26. protected $_imap = NULL;
  27. /**
  28. * @var Felamimail_Model_Account
  29. */
  30. protected $_account = NULL;
  31. /**
  32. * @var Felamimail_Model_Folder
  33. */
  34. protected $_folder = NULL;
  35. /**
  36. *
  37. * @var Felamimail_Controller_MessageTest
  38. */
  39. protected $_emailTestClass;
  40. /**
  41. * name of the folder to use for tests
  42. * @var string
  43. */
  44. protected $_testFolderName = 'Junk';
  45. /**
  46. * delete messages with this header in tearDown
  47. *
  48. * @var string
  49. */
  50. protected $_headerValueToDelete = NULL;
  51. /**
  52. * Runs the test methods of this class.
  53. *
  54. * @access public
  55. * @static
  56. */
  57. public static function main()
  58. {
  59. $suite = new PHPUnit_Framework_TestSuite('Tine 2.0 Felamimail Message Cache Controller Tests');
  60. PHPUnit_TextUI_TestRunner::run($suite);
  61. }
  62. /**
  63. * Sets up the fixture.
  64. * This method is called before a test is executed.
  65. *
  66. * @access protected
  67. */
  68. protected function setUp()
  69. {
  70. // get (or create) test accout
  71. $this->_account = Felamimail_Controller_Account::getInstance()->search()->getFirstRecord();
  72. // init controller and imap backend
  73. $this->_controller = Felamimail_Controller_Cache_Message::getInstance();
  74. $this->_imap = Felamimail_Backend_ImapFactory::factory($this->_account);
  75. try {
  76. $this->_imap->createFolder($this->_testFolderName, '', $this->_account->delimiter);
  77. } catch (Zend_Mail_Storage_Exception $zmse) {
  78. // exists
  79. }
  80. $this->_imap->selectFolder($this->_testFolderName);
  81. // init folder cache and get INBOX
  82. Felamimail_Controller_Cache_Folder::getInstance()->update($this->_account->getId());
  83. $this->_folder = $this->_getFolder($this->_testFolderName);
  84. $this->_emailTestClass = new Felamimail_Controller_MessageTest();
  85. $this->_emailTestClass->setup();
  86. }
  87. /**
  88. * Tears down the fixture
  89. * This method is called after a test is executed.
  90. *
  91. * @access protected
  92. */
  93. protected function tearDown()
  94. {
  95. if ($this->_emailTestClass instanceof Felamimail_Controller_MessageTest) {
  96. $this->_emailTestClass->tearDown();
  97. }
  98. if ($this->_headerValueToDelete !== NULL) {
  99. $result = $this->_imap->search(array(
  100. $this->_headerValueToDelete
  101. ));
  102. foreach($result as $messageUid) {
  103. $this->_imap->removeMessage($messageUid);
  104. }
  105. }
  106. }
  107. /**
  108. * test clear message cache
  109. *
  110. */
  111. public function testClear()
  112. {
  113. $this->_controller->clear($this->_folder);
  114. $messageCacheBackend = new Felamimail_Backend_Cache_Sql_Message();
  115. $count = $messageCacheBackend->searchCountByFolderId($this->_folder->getId());
  116. // check if empty
  117. $this->assertEquals(0, $count);
  118. $this->assertEquals(Felamimail_Model_Folder::CACHE_STATUS_EMPTY, $this->_folder->cache_status);
  119. $this->assertEquals(0, $this->_folder->cache_job_actions_estimate);
  120. }
  121. /**
  122. * test update message cache
  123. *
  124. */
  125. public function testUpdateCache()
  126. {
  127. // update message cache
  128. $updatedFolder = $this->_controller->updateCache($this->_folder, 30);
  129. // check folder status after update
  130. if ($updatedFolder->cache_status == Felamimail_Model_Folder::CACHE_STATUS_COMPLETE) {
  131. $this->assertEquals($updatedFolder->imap_totalcount, $updatedFolder->cache_totalcount, 'totalcounts should be equal');
  132. $this->assertGreaterThan(-1, Tinebase_DateTime::now()->compare($updatedFolder->cache_timestamp), 'timestamp incorrect'); // later or equals
  133. $this->assertEquals(0, $updatedFolder->cache_job_actions_done, 'done/estimate wrong');
  134. $this->assertEquals(0, $updatedFolder->cache_job_actions_estimate, 'done/estimate wrong');
  135. } else if ($updatedFolder->cache_status == Felamimail_Model_Folder::CACHE_STATUS_EMPTY) {
  136. $this->assertEquals(0, $updatedFolder->cache_totalcount, 'cache should be empty');
  137. } else {
  138. $this->assertNotEquals($updatedFolder->imap_totalcount, $updatedFolder->cache_totalcount, 'totalcounts should not be equal: ' . print_r($updatedFolder->toArray(), TRUE));
  139. $this->assertGreaterThan(-1, Tinebase_DateTime::now()->compare($updatedFolder->cache_timestamp), 'timestamp incorrect'); // later or equals
  140. $this->assertNotEquals(0, $updatedFolder->cache_job_actions_done, 'done wrong');
  141. $this->assertNotEquals(0, $updatedFolder->cache_job_actions_estimate, 'estimate wrong');
  142. }
  143. }
  144. /**
  145. * test update message cache
  146. *
  147. */
  148. public function testUpdateCacheAgain()
  149. {
  150. // add three messages to folder
  151. for($i = 0; $i < 3; $i++) {
  152. $this->_appendMessage('multipart_alternative.eml', $this->_testFolderName);
  153. }
  154. $this->_headerValueToDelete = 'HEADER X-Tine20TestMessage multipart/alternative';
  155. // update message cache
  156. $loopCount = 0;
  157. do {
  158. $updatedFolder = $this->_controller->updateCache($this->_folder, 10);
  159. $loopCount++;
  160. } while ($updatedFolder->cache_status != Felamimail_Model_Folder::CACHE_STATUS_COMPLETE && $loopCount < 10);
  161. $this->assertGreaterThan(0, $updatedFolder->cache_totalcount);
  162. $this->assertNotEquals(10, $loopCount, 'should complete cache update with < 10 iterations.');
  163. // now lets delete one message from folder and add another one
  164. $result = $this->_imap->search(array(
  165. $this->_headerValueToDelete
  166. ));
  167. $this->_imap->removeMessage($result[0]);
  168. $this->_appendMessage('multipart_alternative.eml', $this->_testFolderName);
  169. $updatedFolderAgain = $this->_controller->updateCache($this->_folder, 30);
  170. $this->assertEquals($updatedFolder->cache_totalcount, $updatedFolderAgain->cache_totalcount);
  171. }
  172. /**
  173. * test update of message cache counters only
  174. */
  175. public function testUpdateCountersOnly()
  176. {
  177. // update message cache
  178. $updatedFolder = $this->_controller->updateCache($this->_folder, 30);
  179. $this->_appendMessage('multipart_alternative.eml', $this->_testFolderName);
  180. $this->_headerValueToDelete = 'HEADER X-Tine20TestMessage multipart/alternative';
  181. // update message cache + check folder status after update
  182. $updatedFolder = $this->_controller->updateCache($this->_folder, 0);
  183. $this->assertEquals(Felamimail_Model_Folder::CACHE_STATUS_INCOMPLETE, $updatedFolder->cache_status);
  184. $this->assertNotEquals($updatedFolder->imap_totalcount, $updatedFolder->cache_totalcount, 'totalcounts should not be equal');
  185. $this->assertGreaterThan(-1, Tinebase_DateTime::now()->compare($updatedFolder->cache_timestamp), 'timestamp incorrect'); // later or equals
  186. $this->assertEquals(0, $updatedFolder->cache_job_actions_done, 'done wrong');
  187. $this->assertNotEquals(0, $updatedFolder->cache_job_actions_estimate, 'estimate wrong');
  188. }
  189. /**
  190. * test message cache unread counter sanitizing
  191. */
  192. public function testFolderCounterSanitizing()
  193. {
  194. $updatedFolder = $this->_controller->updateCache($this->_folder, 30);
  195. $unreadcount = $updatedFolder->cache_unreadcount;
  196. // change unreadcount of folder
  197. Felamimail_Controller_Folder::getInstance()->updateFolderCounter($updatedFolder, array('cache_unreadcount' => '+1'));
  198. $updatedFolder = $this->_controller->updateCache($this->_folder, 30);
  199. $this->assertEquals($unreadcount, $updatedFolder->cache_unreadcount, 'unreadcount should have been sanitized');
  200. // add new unread message
  201. $message = $this->_emailTestClass->messageTestHelper('multipart_mixed.eml', 'multipart/mixed');
  202. $this->_imap->clearFlags($message->messageuid, array(Zend_Mail_Storage::FLAG_SEEN));
  203. $updatedFolder = $this->_controller->updateCache($this->_folder, 30);
  204. $this->assertEquals($unreadcount+1, $updatedFolder->cache_unreadcount, 'unreadcount should have been increased by 1');
  205. // mark message as seen twice
  206. Felamimail_Controller_Message::getInstance()->addFlags($message, array(Zend_Mail_Storage::FLAG_SEEN));
  207. Felamimail_Controller_Message::getInstance()->addFlags($message, array(Zend_Mail_Storage::FLAG_SEEN));
  208. $updatedFolder = $this->_controller->updateCache($this->_folder, 30);
  209. $this->assertEquals($unreadcount, $updatedFolder->cache_unreadcount, 'unreadcount should be the same as before');
  210. }
  211. /**
  212. * get folder
  213. *
  214. * @return Felamimail_Model_Folder
  215. */
  216. protected function _getFolder($_folderName = null)
  217. {
  218. $folderName = ($_folderName !== null) ? $_folderName : $this->_testFolderName;
  219. $filter = new Felamimail_Model_FolderFilter(array(
  220. array('field' => 'globalname', 'operator' => 'equals', 'value' => '',),
  221. array('field' => 'account_id', 'operator' => 'equals', 'value' => $this->_account->getId())
  222. ));
  223. $result = Felamimail_Controller_Folder::getInstance()->search($filter);
  224. $folder = $result->filter('localname', $folderName)->getFirstRecord();
  225. if (empty($folder)) {
  226. print_r($result->toArray());
  227. throw new Exception('folder not found');
  228. }
  229. return $folder;
  230. }
  231. /**
  232. * append message (from given filename) to folder
  233. *
  234. * @param string $_filename
  235. * @param string $_folder
  236. */
  237. protected function _appendMessage($_filename, $_folder)
  238. {
  239. $mailAsString = file_get_contents(dirname(dirname(dirname(__FILE__))) . '/files/' . $_filename);
  240. Felamimail_Backend_ImapFactory::factory($this->_account->getId())
  241. ->appendMessage($mailAsString, $_folder);
  242. }
  243. /**
  244. * test flag update
  245. */
  246. public function testUpdateFlags()
  247. {
  248. $message = $this->_emailTestClass->messageTestHelper('multipart_mixed.eml', 'multipart/mixed');
  249. // appended messages already have the SEEN flag
  250. $this->assertTrue(in_array(Zend_Mail_Storage::FLAG_SEEN, $message->flags), 'SEEN flag not found: ' . print_r($message->flags, TRUE));
  251. // add another flag
  252. Felamimail_Controller_Message::getInstance()->addFlags($message, Zend_Mail_Storage::FLAG_ANSWERED);
  253. while (! isset($updatedFolder) || $updatedFolder->cache_status === Felamimail_Model_Folder::CACHE_STATUS_INCOMPLETE) {
  254. $updatedFolder = $this->_controller->updateCache($this->_folder, 30);
  255. }
  256. // clear/add flag on imap
  257. $this->_imap->clearFlags($message->messageuid, array(Zend_Mail_Storage::FLAG_SEEN));
  258. $this->_imap->addFlags($message->messageuid, array(Zend_Mail_Storage::FLAG_FLAGGED, Zend_Mail_Storage::FLAG_DRAFT));
  259. $this->_controller->updateFlags($updatedFolder);
  260. $cachedMessage = Felamimail_Controller_Message::getInstance()->get($message->getId());
  261. $this->assertTrue(! in_array(Zend_Mail_Storage::FLAG_SEEN, $cachedMessage->flags), 'SEEN flag found: ' . print_r($cachedMessage->flags, TRUE));
  262. $this->assertTrue(in_array(Zend_Mail_Storage::FLAG_FLAGGED, $cachedMessage->flags), 'FLAGGED flag not found: ' . print_r($cachedMessage->flags, TRUE));
  263. $this->assertTrue(in_array(Zend_Mail_Storage::FLAG_DRAFT, $cachedMessage->flags), 'DRAFT flag not found: ' . print_r($cachedMessage->flags, TRUE));
  264. $this->assertTrue(in_array(Zend_Mail_Storage::FLAG_ANSWERED, $cachedMessage->flags),'ANSWERED flag not found: ' . print_r($cachedMessage->flags, TRUE));
  265. }
  266. }