PageRenderTime 58ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/tine20/Expressomail/Controller/FolderTest.php

https://gitlab.com/rsilveira1987/Expresso
PHP | 273 lines | 87 code | 30 blank | 156 comment | 4 complexity | 736e1c7c3506486a428928700a036a16 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) 2009-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(__FILE__))) . DIRECTORY_SEPARATOR . 'TestHelper.php';
  14. /**
  15. * Test class for Felamimail_Controller_Folder
  16. */
  17. class Expressomail_Controller_FolderTest extends PHPUnit_Framework_TestCase
  18. {
  19. /**
  20. * @var Felamimail_Controller_Folder
  21. */
  22. protected $_controller = array();
  23. /**
  24. * @var Felamimail_Model_Account
  25. */
  26. protected $_account = NULL;
  27. /**
  28. * @var Felamimail_Backend_Imap
  29. */
  30. protected $_imap = NULL;
  31. /**
  32. * folders to delete in tearDown()
  33. *
  34. * @var array
  35. */
  36. protected $_createdFolders = array();
  37. /**
  38. * Runs the test methods of this class.
  39. *
  40. * @access public
  41. * @static
  42. */
  43. public static function main()
  44. {
  45. $suite = new PHPUnit_Framework_TestSuite('Tine 2.0 Felamimail Folder Controller Tests');
  46. PHPUnit_TextUI_TestRunner::run($suite);
  47. }
  48. /**
  49. * Sets up the fixture.
  50. * This method is called before a test is executed.
  51. *
  52. * @access protected
  53. */
  54. protected function setUp()
  55. {
  56. $this->_account = Expressomail_Controller_Account::getInstance()->search()->getFirstRecord();
  57. $this->_controller = Expressomail_Controller_Folder::getInstance();
  58. $this->_imap = Expressomail_Backend_ImapFactory::factory($this->_account);
  59. // fill folder cache first
  60. $this->_controller->search($this->_getFolderFilter(''));
  61. }
  62. /**
  63. * Tears down the fixture
  64. * This method is called after a test is executed.
  65. *
  66. * @access protected
  67. */
  68. protected function tearDown()
  69. {
  70. foreach ($this->_createdFolders as $foldername) {
  71. $this->_controller->delete($this->_account->getId(), $foldername);
  72. }
  73. // delete all remaining folders from cache of account
  74. $folderBackend = new Expressomail_Backend_Folder();
  75. // TODO delete folders
  76. $folders = array();
  77. foreach ($folders as $folder) {
  78. $folderBackend->delete($folder);
  79. }
  80. }
  81. /**
  82. * get folders from the server
  83. */
  84. // TODO: Fix this test. (task13912)
  85. /*public function testGetFolders()
  86. {
  87. $inboxFolder = $this->_getInbox();
  88. $this->assertFalse($inboxFolder === NULL, 'inbox not found');
  89. $this->assertTrue(($inboxFolder->is_selectable == 1), 'should be selectable');
  90. $this->assertTrue(($inboxFolder->has_children == 0), 'has children');
  91. // check if entry is created/exists in db
  92. $folder = $this->_controller->getByBackendAndGlobalName($this->_account->getId(), 'INBOX');
  93. //print_r($folder->toArray());
  94. $this->assertTrue(!empty($folder->id));
  95. $this->assertEquals('INBOX', $folder->localname);
  96. }*/
  97. /**
  98. * returns inbox
  99. *
  100. * @return Felamimail_Model_Folder
  101. */
  102. protected function _getInbox()
  103. {
  104. $result = $this->_controller->search($this->_getFolderFilter(''));
  105. $this->assertGreaterThan(0, count($result));
  106. // get inbox folder and do more checks
  107. $inboxFolder = $result->filter('localname', 'INBOX')->getFirstRecord();
  108. return $inboxFolder;
  109. }
  110. /**
  111. * create a mail folder on the server
  112. */
  113. public function testCreateFolder()
  114. {
  115. $this->_createdFolders[] = 'INBOX' . $this->_account->delimiter . 'test';
  116. $newFolder = $this->_controller->create($this->_account->getId(), 'test', 'INBOX');
  117. // check returned data (id)
  118. $this->assertTrue(!empty($newFolder->id));
  119. $this->assertEquals('INBOX' . $this->_account->delimiter . 'test', $newFolder->globalname);
  120. // get inbox folder and do more checks -> inbox should have children now
  121. $result = $this->_controller->search($this->_getFolderFilter(''));
  122. $inboxFolder = $result->filter('localname', 'INBOX')->getFirstRecord();
  123. $this->assertTrue($inboxFolder->has_children == 1);
  124. // search for subfolders
  125. $resultInboxSub = $this->_controller->search($this->_getFolderFilter());
  126. $this->assertGreaterThan(0, count($resultInboxSub), 'No subfolders found.');
  127. $testFolder = $resultInboxSub->filter('localname', 'test')->getFirstRecord();
  128. $this->assertFalse($testFolder === NULL, 'No test folder created.');
  129. $this->assertTrue(($testFolder->is_selectable == 1));
  130. }
  131. /**
  132. * rename mail folder
  133. */
  134. public function testRenameFolder()
  135. {
  136. $this->_createdFolders[] = 'INBOX' . $this->_account->delimiter . 'test';
  137. $this->_controller->create($this->_account->getId(), 'test', 'INBOX');
  138. $this->_createdFolders = array('INBOX' . $this->_account->delimiter . 'test_renamed');
  139. $renamedFolder = $this->_controller->rename($this->_account->getId(), 'test_renamed', 'INBOX' . $this->_account->delimiter . 'test');
  140. $this->_checkFolder($renamedFolder);
  141. }
  142. /**
  143. * check folder
  144. *
  145. * @param Felamimail_Model_Folder $_folder
  146. */
  147. protected function _checkFolder($_folder)
  148. {
  149. $this->assertEquals('test_renamed', $_folder->localname);
  150. $resultInboxSub = $this->_controller->search($this->_getFolderFilter());
  151. $this->assertGreaterThan(0, count($resultInboxSub), 'No subfolders found.');
  152. $testFolder = $resultInboxSub->filter('localname', $_folder->localname)->getFirstRecord();
  153. $this->assertFalse($testFolder === NULL, 'No folder found.');
  154. $this->assertTrue(($testFolder->is_selectable == 1));
  155. }
  156. /**
  157. * rename mail folder directly on the server (i.e. another client) and try to rename it with tine
  158. */
  159. //TODO: This test must be uncommented by task16507
  160. /*public function testRenameFolderByAnotherClient()
  161. {
  162. $testFolderName = 'INBOX' . $this->_account->delimiter . 'test';
  163. $this->_controller->create($this->_account->getId(), 'test', 'INBOX');
  164. $this->_imap->renameFolder($testFolderName, $testFolderName . '_renamed');
  165. $this->_createdFolders = array($testFolderName . '_renamed');
  166. $this->setExpectedException('Expressomail_Exception_IMAPFolderNotFound');
  167. $renamedFolder = $this->_controller->rename($this->_account->getId(), $testFolderName, $testFolderName);
  168. }*/
  169. /**
  170. * rename mail folder on the server
  171. */
  172. // TODO: Fix this test. (task13912)
  173. /*public function testRenameFolderWithSubfolder()
  174. {
  175. $this->_controller->create($this->_account->getId(), 'test', 'INBOX');
  176. $this->_controller->create($this->_account->getId(), 'testsub', 'INBOX' . $this->_account->delimiter . 'test');
  177. $renamedFolder = $this->_controller->rename($this->_account->getId(), 'test_renamed', 'INBOX' . $this->_account->delimiter . 'test');
  178. $this->_createdFolders[] = 'INBOX' . $this->_account->delimiter . 'test_renamed' . $this->_account->delimiter . 'testsub';
  179. $this->_createdFolders[] = 'INBOX' . $this->_account->delimiter . 'test_renamed';
  180. $this->assertEquals('test_renamed', $renamedFolder->localname);
  181. $resultTestSub = $this->_controller->search($this->_getFolderFilter('INBOX' . $this->_account->delimiter . 'test'));
  182. $this->assertGreaterThan(0, count($resultTestSub), 'No subfolders found.');
  183. $testFolder = $resultTestSub->filter('localname', 'testsub')->getFirstRecord();
  184. //print_r($testFolder->toArray());
  185. $this->assertFalse($testFolder === NULL, 'No renamed folder found.');
  186. $this->assertTrue(($testFolder->is_selectable == 1));
  187. $this->assertEquals('INBOX' . $this->_account->delimiter . 'test_renamed' . $this->_account->delimiter . 'testsub', $testFolder->globalname);
  188. }*/
  189. /**
  190. * rename mail folder on the server and create a subfolder afterwards
  191. */
  192. //TODO: This test must be uncommented by task16507
  193. /*public function testRenameFolderAndCreateSubfolder()
  194. {
  195. $this->_controller->create($this->_account->getId(), 'test', 'INBOX');
  196. $renamedFolder = $this->_controller->rename($this->_account->getId(), 'test_renamed', 'INBOX' . $this->_account->delimiter . 'test');
  197. $this->_createdFolders[] = 'INBOX' . $this->_account->delimiter . 'test_renamed' . $this->_account->delimiter . 'testsub';
  198. $this->_createdFolders[] = 'INBOX' . $this->_account->delimiter . 'test_renamed';
  199. $subfolder = $this->_controller->create($this->_account->getId(), 'testsub', 'INBOX' . $this->_account->delimiter . 'test_renamed');
  200. $this->assertEquals('INBOX' . $this->_account->delimiter . 'test_renamed' . $this->_account->delimiter . 'testsub', $subfolder->globalname);
  201. }*/
  202. /**
  203. * folder counts test helper
  204. *
  205. * @param Felamimail_Model_Folder $_folder
  206. * @param array $_newCounters
  207. * @param array $_expectedValues
  208. */
  209. protected function _folderCountsTestHelper($_folder, $_newCounters, $_expectedValues)
  210. {
  211. $updatedFolder = $this->_controller->updateFolderCounter($_folder, $_newCounters);
  212. foreach ($_expectedValues as $key => $value) {
  213. $this->assertEquals($value, $updatedFolder->{$key}, $key . ' does not match.');
  214. }
  215. $folderInDb = $this->_controller->get($_folder->getId());
  216. $this->assertTrue($updatedFolder->toArray() == $folderInDb->toArray(), 'folder values do not match');
  217. }
  218. /**
  219. * get folder filter
  220. *
  221. * @return Felamimail_Model_FolderFilter
  222. */
  223. protected function _getFolderFilter($_globalname = 'INBOX')
  224. {
  225. return new Expressomail_Model_FolderFilter(array(
  226. array('field' => 'globalname', 'operator' => 'equals', 'value' => $_globalname),
  227. array('field' => 'account_id', 'operator' => 'equals', 'value' => $this->_account->getId())
  228. ));
  229. }
  230. }