PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/standard/branches/release-1.5/tests/Zend/Mail/MaildirFolderTest.php

https://github.com/bhaumik25/zend-framework
PHP | 438 lines | 339 code | 71 blank | 28 comment | 27 complexity | c6d310760bac21ec4995cf9cd1412cb0 MD5 | raw file
  1. <?php
  2. /**
  3. * @category Zend
  4. * @package Zend_Mail
  5. * @subpackage UnitTests
  6. */
  7. /**
  8. * Zend_Mail_Storage_Folder_Maildir
  9. */
  10. require_once 'Zend/Mail/Storage/Folder/Maildir.php';
  11. /**
  12. * Zend_Config
  13. */
  14. require_once 'Zend/Config.php';
  15. /**
  16. * PHPUnit test case
  17. */
  18. require_once 'PHPUnit/Framework/TestCase.php';
  19. /**
  20. * @category Zend
  21. * @package Zend_Mail
  22. * @subpackage UnitTests
  23. */
  24. class Zend_Mail_MaildirFolderTest extends PHPUnit_Framework_TestCase
  25. {
  26. protected $_params;
  27. protected $_originalDir;
  28. protected $_tmpdir;
  29. protected $_subdirs = array('.', '.subfolder', '.subfolder.test');
  30. public function setUp()
  31. {
  32. $this->_originalDir = dirname(__FILE__) . '/_files/test.maildir/';
  33. if (!is_dir($this->_originalDir . '/cur/')) {
  34. $this->markTestSkipped('You have to unpack maildir.tar in Zend/Mail/_files/test.maildir/ '
  35. . 'directory before enabling the maildir tests');
  36. return;
  37. }
  38. if ($this->_tmpdir == null) {
  39. if (TESTS_ZEND_MAIL_TEMPDIR != null) {
  40. $this->_tmpdir = TESTS_ZEND_MAIL_TEMPDIR;
  41. } else {
  42. $this->_tmpdir = dirname(__FILE__) . '/_files/test.tmp/';
  43. }
  44. if (!file_exists($this->_tmpdir)) {
  45. mkdir($this->_tmpdir);
  46. }
  47. $count = 0;
  48. $dh = opendir($this->_tmpdir);
  49. while (readdir($dh) !== false) {
  50. ++$count;
  51. }
  52. closedir($dh);
  53. if ($count != 2) {
  54. $this->markTestSkipped('Are you sure your tmp dir is a valid empty dir?');
  55. return;
  56. }
  57. }
  58. $this->_params = array();
  59. $this->_params['dirname'] = $this->_tmpdir;
  60. foreach ($this->_subdirs as $dir) {
  61. if ($dir != '.') {
  62. mkdir($this->_tmpdir . $dir);
  63. }
  64. foreach (array('cur', 'new') as $subdir) {
  65. if (!file_exists($this->_originalDir . $dir . '/' . $subdir)) {
  66. continue;
  67. }
  68. mkdir($this->_tmpdir . $dir . '/' . $subdir);
  69. $dh = opendir($this->_originalDir . $dir . '/' . $subdir);
  70. while (($entry = readdir($dh)) !== false) {
  71. $entry = $dir . '/' . $subdir . '/' . $entry;
  72. if (!is_file($this->_originalDir . $entry)) {
  73. continue;
  74. }
  75. copy($this->_originalDir . $entry, $this->_tmpdir . $entry);
  76. }
  77. closedir($dh);
  78. }
  79. }
  80. }
  81. public function tearDown()
  82. {
  83. foreach (array_reverse($this->_subdirs) as $dir) {
  84. foreach (array('cur', 'new') as $subdir) {
  85. if (!file_exists($this->_tmpdir . $dir . '/' . $subdir)) {
  86. continue;
  87. }
  88. $dh = opendir($this->_tmpdir . $dir . '/' . $subdir);
  89. while (($entry = readdir($dh)) !== false) {
  90. $entry = $this->_tmpdir . $dir . '/' . $subdir . '/' . $entry;
  91. if (!is_file($entry)) {
  92. continue;
  93. }
  94. unlink($entry);
  95. }
  96. closedir($dh);
  97. rmdir($this->_tmpdir . $dir . '/' . $subdir);
  98. }
  99. if ($dir != '.') {
  100. rmdir($this->_tmpdir . $dir);
  101. }
  102. }
  103. }
  104. public function testLoadOk()
  105. {
  106. try {
  107. $mail = new Zend_Mail_Storage_Folder_Maildir($this->_params);
  108. } catch (Exception $e) {
  109. $this->fail('exception raised while loading Maildir folder');
  110. }
  111. }
  112. public function testLoadConfig()
  113. {
  114. try {
  115. $mail = new Zend_Mail_Storage_Folder_Maildir(new Zend_Config($this->_params));
  116. } catch (Exception $e) {
  117. $this->fail('exception raised while loading Maildir folder');
  118. }
  119. }
  120. public function testNoParams()
  121. {
  122. try {
  123. $mail = new Zend_Mail_Storage_Folder_Maildir(array());
  124. } catch (Exception $e) {
  125. return; // test ok
  126. }
  127. $this->fail('no exception raised with empty params');
  128. }
  129. public function testLoadFailure()
  130. {
  131. try {
  132. $mail = new Zend_Mail_Storage_Folder_Maildir(array('dirname' => 'This/Folder/Does/Not/Exist'));
  133. } catch (Exception $e) {
  134. return; // test ok
  135. }
  136. $this->fail('no exception raised while loading unknown dirname');
  137. }
  138. public function testLoadUnkownFolder()
  139. {
  140. $this->_params['folder'] = 'UnknownFolder';
  141. try {
  142. $mail = new Zend_Mail_Storage_Folder_Maildir($this->_params);
  143. } catch (Exception $e) {
  144. return; // test ok
  145. }
  146. $this->fail('no exception raised while loading unknown folder');
  147. }
  148. public function testChangeFolder()
  149. {
  150. $mail = new Zend_Mail_Storage_Folder_Maildir($this->_params);
  151. try {
  152. $mail->selectFolder('subfolder.test');
  153. } catch (Exception $e) {
  154. $this->fail('exception raised while selecting existing folder');
  155. }
  156. $this->assertEquals($mail->getCurrentFolder(), 'subfolder.test');
  157. }
  158. public function testUnknownFolder()
  159. {
  160. $mail = new Zend_Mail_Storage_Folder_Maildir($this->_params);
  161. try {
  162. $mail->selectFolder('/Unknown/Folder/');
  163. } catch (Exception $e) {
  164. return; // test ok
  165. }
  166. $this->fail('no exception raised while selecting unknown folder');
  167. }
  168. public function testGlobalName()
  169. {
  170. $mail = new Zend_Mail_Storage_Folder_Maildir($this->_params);
  171. try {
  172. // explicit call of __toString() needed for PHP < 5.2
  173. $this->assertEquals($mail->getFolders()->subfolder->__toString(), 'subfolder');
  174. } catch (Exception $e) {
  175. $this->fail('exception raised while selecting existing folder and getting global name');
  176. }
  177. }
  178. public function testLocalName()
  179. {
  180. $mail = new Zend_Mail_Storage_Folder_Maildir($this->_params);
  181. try {
  182. $this->assertEquals($mail->getFolders()->subfolder->key(), 'test');
  183. } catch (Exception $e) {
  184. $this->fail('exception raised while selecting existing folder and getting local name');
  185. }
  186. }
  187. public function testIterator()
  188. {
  189. $mail = new Zend_Mail_Storage_Folder_Maildir($this->_params);
  190. $iterator = new RecursiveIteratorIterator($mail->getFolders(), RecursiveIteratorIterator::SELF_FIRST);
  191. // we search for this folder because we can't assume a order while iterating
  192. $search_folders = array('subfolder' => 'subfolder',
  193. 'subfolder.test' => 'test',
  194. 'INBOX' => 'INBOX');
  195. $found_folders = array();
  196. foreach ($iterator as $localName => $folder) {
  197. if (!isset($search_folders[$folder->getGlobalName()])) {
  198. continue;
  199. }
  200. // explicit call of __toString() needed for PHP < 5.2
  201. $found_folders[$folder->__toString()] = $localName;
  202. }
  203. $this->assertEquals($search_folders, $found_folders);
  204. }
  205. public function testKeyLocalName()
  206. {
  207. $mail = new Zend_Mail_Storage_Folder_Maildir($this->_params);
  208. $iterator = new RecursiveIteratorIterator($mail->getFolders(), RecursiveIteratorIterator::SELF_FIRST);
  209. // we search for this folder because we can't assume a order while iterating
  210. $search_folders = array('subfolder' => 'subfolder',
  211. 'subfolder.test' => 'test',
  212. 'INBOX' => 'INBOX');
  213. $found_folders = array();
  214. foreach ($iterator as $localName => $folder) {
  215. if (!isset($search_folders[$folder->getGlobalName()])) {
  216. continue;
  217. }
  218. // explicit call of __toString() needed for PHP < 5.2
  219. $found_folders[$folder->__toString()] = $localName;
  220. }
  221. $this->assertEquals($search_folders, $found_folders);
  222. }
  223. public function testInboxEquals()
  224. {
  225. $mail = new Zend_Mail_Storage_Folder_Maildir($this->_params);
  226. $iterator = new RecursiveIteratorIterator($mail->getFolders('INBOX.subfolder'), RecursiveIteratorIterator::SELF_FIRST);
  227. // we search for this folder because we can't assume a order while iterating
  228. $search_folders = array('subfolder.test' => 'test');
  229. $found_folders = array();
  230. foreach ($iterator as $localName => $folder) {
  231. if (!isset($search_folders[$folder->getGlobalName()])) {
  232. continue;
  233. }
  234. // explicit call of __toString() needed for PHP < 5.2
  235. $found_folders[$folder->__toString()] = $localName;
  236. }
  237. $this->assertEquals($search_folders, $found_folders);
  238. }
  239. public function testSelectable()
  240. {
  241. $mail = new Zend_Mail_Storage_Folder_Maildir($this->_params);
  242. $iterator = new RecursiveIteratorIterator($mail->getFolders(), RecursiveIteratorIterator::SELF_FIRST);
  243. foreach ($iterator as $localName => $folder) {
  244. $this->assertEquals($localName, $folder->getLocalName());
  245. }
  246. }
  247. public function testCount()
  248. {
  249. $mail = new Zend_Mail_Storage_Folder_Maildir($this->_params);
  250. $count = $mail->countMessages();
  251. $this->assertEquals(5, $count);
  252. $mail->selectFolder('subfolder.test');
  253. $count = $mail->countMessages();
  254. $this->assertEquals(1, $count);
  255. }
  256. public function testSize()
  257. {
  258. $mail = new Zend_Mail_Storage_Folder_Maildir($this->_params);
  259. $shouldSizes = array(1 => 397, 89, 694, 452, 497);
  260. $sizes = $mail->getSize();
  261. $this->assertEquals($shouldSizes, $sizes);
  262. $mail->selectFolder('subfolder.test');
  263. $sizes = $mail->getSize();
  264. $this->assertEquals(array(1 => 467), $sizes);
  265. }
  266. public function testFetchHeader()
  267. {
  268. $mail = new Zend_Mail_Storage_Folder_Maildir($this->_params);
  269. $subject = $mail->getMessage(1)->subject;
  270. $this->assertEquals('Simple Message', $subject);
  271. $mail->selectFolder('subfolder.test');
  272. $subject = $mail->getMessage(1)->subject;
  273. $this->assertEquals('Message in subfolder', $subject);
  274. }
  275. public function testNotReadableFolder()
  276. {
  277. $stat = stat($this->_params['dirname'] . '.subfolder');
  278. chmod($this->_params['dirname'] . '.subfolder', 0);
  279. clearstatcache();
  280. $statcheck = stat($this->_params['dirname'] . '.subfolder');
  281. if ($statcheck['mode'] % (8 * 8 * 8) !== 0) {
  282. chmod($this->_params['dirname'] . '.subfolder', $stat['mode']);
  283. $this->markTestSkipped('cannot remove read rights, which makes this test useless (maybe you are using Windows?)');
  284. return;
  285. }
  286. $check = false;
  287. try {
  288. $mail = new Zend_Mail_Storage_Folder_Maildir($this->_params);
  289. } catch (Exception $e) {
  290. $check = true;
  291. // test ok
  292. }
  293. chmod($this->_params['dirname'] . '.subfolder', $stat['mode']);
  294. if (!$check) {
  295. $this->fail('no exception while loading invalid dir with subfolder not readable');
  296. }
  297. }
  298. public function testNotReadableMaildir()
  299. {
  300. $stat = stat($this->_params['dirname']);
  301. chmod($this->_params['dirname'], 0);
  302. clearstatcache();
  303. $statcheck = stat($this->_params['dirname']);
  304. if ($statcheck['mode'] % (8 * 8 * 8) !== 0) {
  305. chmod($this->_params['dirname'], $stat['mode']);
  306. $this->markTestSkipped('cannot remove read rights, which makes this test useless (maybe you are using Windows?)');
  307. return;
  308. }
  309. $check = false;
  310. try {
  311. $mail = new Zend_Mail_Storage_Folder_Maildir($this->_params);
  312. } catch (Exception $e) {
  313. $check = true;
  314. // test ok
  315. }
  316. chmod($this->_params['dirname'], $stat['mode']);
  317. if (!$check) {
  318. $this->fail('no exception while loading not readable maildir');
  319. }
  320. }
  321. public function testGetInvalidFolder()
  322. {
  323. $mail = new Zend_Mail_Storage_Folder_Maildir($this->_params);
  324. $root = $mail->getFolders();
  325. $root->foobar = new Zend_Mail_Storage_Folder('foobar', DIRECTORY_SEPARATOR . 'foobar');
  326. try {
  327. $mail->selectFolder('foobar');
  328. } catch (Exception $e) {
  329. return; // ok
  330. }
  331. $this->fail('no error while getting invalid folder');
  332. }
  333. public function testGetVanishedFolder()
  334. {
  335. $mail = new Zend_Mail_Storage_Folder_Maildir($this->_params);
  336. $root = $mail->getFolders();
  337. $root->foobar = new Zend_Mail_Storage_Folder('foobar', 'foobar');
  338. try {
  339. $mail->selectFolder('foobar');
  340. } catch (Exception $e) {
  341. return; // ok
  342. }
  343. $this->fail('no error while getting vanished folder');
  344. }
  345. public function testGetNotSelectableFolder()
  346. {
  347. $mail = new Zend_Mail_Storage_Folder_Maildir($this->_params);
  348. $root = $mail->getFolders();
  349. $root->foobar = new Zend_Mail_Storage_Folder('foobar', 'foobar', false);
  350. try {
  351. $mail->selectFolder('foobar');
  352. } catch (Exception $e) {
  353. return; // ok
  354. }
  355. $this->fail('no error while getting not selectable folder');
  356. }
  357. public function testWithAdditionalFolder()
  358. {
  359. mkdir($this->_params['dirname'] . '.xyyx');
  360. mkdir($this->_params['dirname'] . '.xyyx/cur');
  361. $mail = new Zend_Mail_Storage_Folder_Maildir($this->_params);
  362. $mail->selectFolder('xyyx');
  363. $this->assertEquals($mail->countMessages(), 0);
  364. rmdir($this->_params['dirname'] . '.xyyx/cur');
  365. rmdir($this->_params['dirname'] . '.xyyx');
  366. }
  367. }