PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/DevApp/library/ServerLibraries/ZendFramework/1.7/tests/Zend/Mail/MboxFolderTest.php

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