PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/ZendFramework/tests/Zend/Mail/MboxFolderTest.php

https://bitbucket.org/Dal-Papa/is-340-publish-base
PHP | 417 lines | 312 code | 64 blank | 41 comment | 23 complexity | 792ba4b530353f01a0b4af21b5d999f7 MD5 | raw file
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: MboxFolderTest.php 24593 2012-01-05 20:35:02Z matthew $
  21. */
  22. /**
  23. * @see Zend_Mail_Storage_Folder_Mbox
  24. */
  25. require_once 'Zend/Mail/Storage/Folder/Mbox.php';
  26. /**
  27. * @see Zend_Config
  28. */
  29. require_once 'Zend/Config.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Mail
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Mail
  37. */
  38. class Zend_Mail_MboxFolderTest extends PHPUnit_Framework_TestCase
  39. {
  40. protected $_params;
  41. protected $_originalDir;
  42. protected $_tmpdir;
  43. protected $_subdirs = array('.', 'subfolder');
  44. public function setUp()
  45. {
  46. $this->_originalDir = dirname(__FILE__) . '/_files/test.mbox/';
  47. if ($this->_tmpdir == null) {
  48. if (TESTS_ZEND_MAIL_TEMPDIR != null) {
  49. $this->_tmpdir = TESTS_ZEND_MAIL_TEMPDIR;
  50. } else {
  51. $this->_tmpdir = dirname(__FILE__) . '/_files/test.tmp/';
  52. }
  53. if (!file_exists($this->_tmpdir)) {
  54. mkdir($this->_tmpdir);
  55. }
  56. $count = 0;
  57. $dh = opendir($this->_tmpdir);
  58. while (readdir($dh) !== false) {
  59. ++$count;
  60. }
  61. closedir($dh);
  62. if ($count != 2) {
  63. $this->markTestSkipped('Are you sure your tmp dir is a valid empty dir?');
  64. return;
  65. }
  66. }
  67. $this->_params = array();
  68. $this->_params['dirname'] = $this->_tmpdir;
  69. $this->_params['folder'] = 'INBOX';
  70. foreach ($this->_subdirs as $dir) {
  71. if ($dir != '.') {
  72. mkdir($this->_tmpdir . $dir);
  73. }
  74. $dh = opendir($this->_originalDir . $dir);
  75. while (($entry = readdir($dh)) !== false) {
  76. $entry = $dir . '/' . $entry;
  77. if (!is_file($this->_originalDir . $entry)) {
  78. continue;
  79. }
  80. copy($this->_originalDir . $entry, $this->_tmpdir . $entry);
  81. }
  82. closedir($dh);
  83. }
  84. }
  85. public function tearDown()
  86. {
  87. foreach (array_reverse($this->_subdirs) as $dir) {
  88. $dh = opendir($this->_tmpdir . $dir);
  89. while (($entry = readdir($dh)) !== false) {
  90. $entry = $this->_tmpdir . $dir . '/' . $entry;
  91. if (!is_file($entry)) {
  92. continue;
  93. }
  94. unlink($entry);
  95. }
  96. closedir($dh);
  97. if ($dir != '.') {
  98. rmdir($this->_tmpdir . $dir);
  99. }
  100. }
  101. }
  102. public function testLoadOk()
  103. {
  104. try {
  105. $mail = new Zend_Mail_Storage_Folder_Mbox($this->_params);
  106. } catch (Exception $e) {
  107. $this->fail('exception raised while loading mbox folder');
  108. }
  109. }
  110. public function testLoadConfig()
  111. {
  112. try {
  113. $mail = new Zend_Mail_Storage_Folder_Mbox(new Zend_Config($this->_params));
  114. } catch (Exception $e) {
  115. $this->fail('exception raised while loading mbox folder');
  116. }
  117. }
  118. public function testNoParams()
  119. {
  120. try {
  121. $mail = new Zend_Mail_Storage_Folder_Mbox(array());
  122. } catch (Exception $e) {
  123. return; // test ok
  124. }
  125. $this->fail('no exception raised with empty params');
  126. }
  127. public function testFilenameParam()
  128. {
  129. try {
  130. // filename is not allowed in this subclass
  131. $mail = new Zend_Mail_Storage_Folder_Mbox(array('filename' => 'foobar'));
  132. } catch (Exception $e) {
  133. return; // test ok
  134. }
  135. $this->fail('no exception raised with filename as param');
  136. }
  137. public function testLoadFailure()
  138. {
  139. try {
  140. $mail = new Zend_Mail_Storage_Folder_Mbox(array('dirname' => 'This/Folder/Does/Not/Exist'));
  141. } catch (Exception $e) {
  142. return; // test ok
  143. }
  144. $this->fail('no exception raised while loading unknown dirname');
  145. }
  146. public function testLoadUnkownFolder()
  147. {
  148. $this->_params['folder'] = 'UnknownFolder';
  149. try {
  150. $mail = new Zend_Mail_Storage_Folder_Mbox($this->_params);
  151. } catch (Exception $e) {
  152. return; // test ok
  153. }
  154. $this->fail('no exception raised while loading unknown folder');
  155. }
  156. public function testChangeFolder()
  157. {
  158. $mail = new Zend_Mail_Storage_Folder_Mbox($this->_params);
  159. try {
  160. $mail->selectFolder(DIRECTORY_SEPARATOR . 'subfolder' . DIRECTORY_SEPARATOR . 'test');
  161. } catch (Exception $e) {
  162. $this->fail('exception raised while selecting existing folder');
  163. }
  164. $this->assertEquals($mail->getCurrentFolder(), DIRECTORY_SEPARATOR . 'subfolder' . DIRECTORY_SEPARATOR . 'test');
  165. }
  166. public function testChangeFolderUnselectable()
  167. {
  168. $mail = new Zend_Mail_Storage_Folder_Mbox($this->_params);
  169. try {
  170. $mail->selectFolder(DIRECTORY_SEPARATOR . 'subfolder');
  171. } catch (Exception $e) {
  172. return; // test ok
  173. }
  174. $this->fail('no exception raised while selecting unselectable folder');
  175. }
  176. public function testUnknownFolder()
  177. {
  178. $mail = new Zend_Mail_Storage_Folder_Mbox($this->_params);
  179. try {
  180. $mail->selectFolder('/Unknown/Folder/');
  181. } catch (Exception $e) {
  182. return; // test ok
  183. }
  184. $this->fail('no exception raised while selecting unknown folder');
  185. }
  186. public function testGlobalName()
  187. {
  188. $mail = new Zend_Mail_Storage_Folder_Mbox($this->_params);
  189. try {
  190. // explicit call of __toString() needed for PHP < 5.2
  191. $this->assertEquals($mail->getFolders()->subfolder->__toString(), DIRECTORY_SEPARATOR . 'subfolder');
  192. } catch (Zend_Mail_Exception $e) {
  193. $this->fail('exception raised while selecting existing folder and getting global name');
  194. }
  195. }
  196. public function testLocalName()
  197. {
  198. $mail = new Zend_Mail_Storage_Folder_Mbox($this->_params);
  199. try {
  200. $this->assertEquals($mail->getFolders()->subfolder->key(), 'test');
  201. } catch (Exception $e) {
  202. $this->fail('exception raised while selecting existing folder and getting local name');
  203. }
  204. }
  205. public function testIterator()
  206. {
  207. $mail = new Zend_Mail_Storage_Folder_Mbox($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(DIRECTORY_SEPARATOR . 'subfolder' => 'subfolder',
  211. DIRECTORY_SEPARATOR . 'subfolder' . DIRECTORY_SEPARATOR . 'test' => 'test',
  212. DIRECTORY_SEPARATOR . '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 testKeyLocalName()
  224. {
  225. $mail = new Zend_Mail_Storage_Folder_Mbox($this->_params);
  226. $iterator = new RecursiveIteratorIterator($mail->getFolders(), RecursiveIteratorIterator::SELF_FIRST);
  227. // we search for this folder because we can't assume a order while iterating
  228. $search_folders = array(DIRECTORY_SEPARATOR . 'subfolder' => 'subfolder',
  229. DIRECTORY_SEPARATOR . 'subfolder' . DIRECTORY_SEPARATOR . 'test' => 'test',
  230. DIRECTORY_SEPARATOR . 'INBOX' => 'INBOX');
  231. $found_folders = array();
  232. foreach ($iterator as $localName => $folder) {
  233. if (!isset($search_folders[$folder->getGlobalName()])) {
  234. continue;
  235. }
  236. // explicit call of __toString() needed for PHP < 5.2
  237. $found_folders[$folder->__toString()] = $localName;
  238. }
  239. $this->assertEquals($search_folders, $found_folders);
  240. }
  241. public function testSelectable()
  242. {
  243. $mail = new Zend_Mail_Storage_Folder_Mbox($this->_params);
  244. $iterator = new RecursiveIteratorIterator($mail->getFolders(), RecursiveIteratorIterator::SELF_FIRST);
  245. foreach ($iterator as $localName => $folder) {
  246. $this->assertEquals($localName, $folder->getLocalName());
  247. }
  248. }
  249. public function testCount()
  250. {
  251. $mail = new Zend_Mail_Storage_Folder_Mbox($this->_params);
  252. $count = $mail->countMessages();
  253. $this->assertEquals(7, $count);
  254. $mail->selectFolder(DIRECTORY_SEPARATOR . 'subfolder' . DIRECTORY_SEPARATOR . 'test');
  255. $count = $mail->countMessages();
  256. $this->assertEquals(1, $count);
  257. }
  258. public function testSize()
  259. {
  260. $mail = new Zend_Mail_Storage_Folder_Mbox($this->_params);
  261. $shouldSizes = array(1 => 397, 89, 694, 452, 497, 101, 139);
  262. $sizes = $mail->getSize();
  263. $this->assertEquals($shouldSizes, $sizes);
  264. $mail->selectFolder(DIRECTORY_SEPARATOR . 'subfolder' . DIRECTORY_SEPARATOR . 'test');
  265. $sizes = $mail->getSize();
  266. $this->assertEquals(array(1 => 410), $sizes);
  267. }
  268. public function testFetchHeader()
  269. {
  270. $mail = new Zend_Mail_Storage_Folder_Mbox($this->_params);
  271. $subject = $mail->getMessage(1)->subject;
  272. $this->assertEquals('Simple Message', $subject);
  273. $mail->selectFolder(DIRECTORY_SEPARATOR . 'subfolder' . DIRECTORY_SEPARATOR . 'test');
  274. $subject = $mail->getMessage(1)->subject;
  275. $this->assertEquals('Message in subfolder', $subject);
  276. }
  277. public function testSleepWake()
  278. {
  279. $mail = new Zend_Mail_Storage_Folder_Mbox($this->_params);
  280. $mail->selectFolder(DIRECTORY_SEPARATOR . 'subfolder' . DIRECTORY_SEPARATOR . 'test');
  281. $count = $mail->countMessages();
  282. $content = $mail->getMessage(1)->getContent();
  283. $serialzed = serialize($mail);
  284. $mail = null;
  285. $mail = unserialize($serialzed);
  286. $this->assertEquals($mail->countMessages(), $count);
  287. $this->assertEquals($mail->getMessage(1)->getContent(), $content);
  288. $mail->selectFolder(DIRECTORY_SEPARATOR . 'subfolder' . DIRECTORY_SEPARATOR . 'test');
  289. $this->assertEquals($mail->countMessages(), $count);
  290. $this->assertEquals($mail->getMessage(1)->getContent(), $content);
  291. }
  292. public function testNotMboxFile()
  293. {
  294. touch($this->_params['dirname'] . 'foobar');
  295. $mail = new Zend_Mail_Storage_Folder_Mbox($this->_params);
  296. try {
  297. $mail->getFolders()->foobar;
  298. } catch (Exception $e) {
  299. return; // ok
  300. }
  301. $this->fail('file, which is not mbox, got parsed');
  302. }
  303. public function testNotReadableFolder()
  304. {
  305. $stat = stat($this->_params['dirname'] . 'subfolder');
  306. chmod($this->_params['dirname'] . 'subfolder', 0);
  307. clearstatcache();
  308. $statcheck = stat($this->_params['dirname'] . 'subfolder');
  309. if ($statcheck['mode'] % (8 * 8 * 8) !== 0) {
  310. chmod($this->_params['dirname'] . 'subfolder', $stat['mode']);
  311. $this->markTestSkipped('cannot remove read rights, which makes this test useless (maybe you are using Windows?)');
  312. return;
  313. }
  314. $check = false;
  315. try {
  316. $mail = new Zend_Mail_Storage_Folder_Mbox($this->_params);
  317. } catch (Exception $e) {
  318. $check = true;
  319. // test ok
  320. }
  321. chmod($this->_params['dirname'] . 'subfolder', $stat['mode']);
  322. if (!$check) {
  323. if (function_exists('posix_getuid') && posix_getuid() === 0) {
  324. $this->markTestSkipped('seems like you are root and we therefore cannot test the error handling');
  325. }
  326. $this->fail('no exception while loading invalid dir with subfolder not readable');
  327. }
  328. }
  329. public function testGetInvalidFolder()
  330. {
  331. $mail = new Zend_Mail_Storage_Folder_Mbox($this->_params);
  332. $root = $mail->getFolders();
  333. $root->foobar = new Zend_Mail_Storage_Folder('x', 'x');
  334. try {
  335. $mail->getFolders('foobar');
  336. } catch (Exception $e) {
  337. return; // ok
  338. }
  339. $this->fail('no error while getting invalid folder');
  340. }
  341. public function testGetVanishedFolder()
  342. {
  343. $mail = new Zend_Mail_Storage_Folder_Mbox($this->_params);
  344. $root = $mail->getFolders();
  345. $root->foobar = new Zend_Mail_Storage_Folder('foobar', DIRECTORY_SEPARATOR . 'foobar');
  346. try {
  347. $mail->selectFolder('foobar');
  348. } catch (Exception $e) {
  349. return; // ok
  350. }
  351. $this->fail('no error while getting vanished folder');
  352. }
  353. }