AbstractTest.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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_File
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. // Call Zend_File_Transfer_Adapter_AbstractTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_File_Transfer_Adapter_AbstractTest::main");
  25. }
  26. require_once dirname(__FILE__) . '/../../../../TestHelper.php';
  27. require_once 'Zend/File/Transfer/Adapter/Abstract.php';
  28. require_once 'Zend/Filter/BaseName.php';
  29. require_once 'Zend/Filter/StringToLower.php';
  30. require_once 'Zend/Filter/StringToUpper.php';
  31. require_once 'Zend/Loader/PluginLoader.php';
  32. require_once 'Zend/Validate/File/Count.php';
  33. require_once 'Zend/Validate/File/Extension.php';
  34. /**
  35. * Test class for Zend_File_Transfer_Adapter_Abstract
  36. *
  37. * @category Zend
  38. * @package Zend_File
  39. * @subpackage UnitTests
  40. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. * @group Zend_File
  43. */
  44. class Zend_File_Transfer_Adapter_AbstractTest extends PHPUnit_Framework_TestCase
  45. {
  46. /**
  47. * Runs the test methods of this class.
  48. *
  49. * @return void
  50. */
  51. public static function main()
  52. {
  53. $suite = new PHPUnit_Framework_TestSuite("Zend_File_Transfer_Adapter_AbstractTest");
  54. $result = PHPUnit_TextUI_TestRunner::run($suite);
  55. }
  56. /**
  57. * Sets up the fixture, for example, open a network connection.
  58. * This method is called before a test is executed.
  59. *
  60. * @return void
  61. */
  62. public function setUp()
  63. {
  64. $this->adapter = new Zend_File_Transfer_Adapter_AbstractTest_MockAdapter();
  65. }
  66. /**
  67. * Tears down the fixture, for example, close a network connection.
  68. * This method is called after a test is executed.
  69. *
  70. * @return void
  71. */
  72. public function tearDown()
  73. {
  74. }
  75. /**
  76. * @expectedException Zend_File_Transfer_Exception
  77. */
  78. public function testAdapterShouldThrowExceptionWhenRetrievingPluginLoaderOfInvalidType()
  79. {
  80. $this->adapter->getPluginLoader('bogus');
  81. }
  82. public function testAdapterShouldHavePluginLoaderForValidators()
  83. {
  84. $loader = $this->adapter->getPluginLoader('validate');
  85. $this->assertTrue($loader instanceof Zend_Loader_PluginLoader);
  86. }
  87. public function testAdapterShouldAllowAddingCustomPluginLoader()
  88. {
  89. $loader = new Zend_Loader_PluginLoader();
  90. $this->adapter->setPluginLoader($loader, 'filter');
  91. $this->assertSame($loader, $this->adapter->getPluginLoader('filter'));
  92. }
  93. /**
  94. * @expectedException Zend_File_Transfer_Exception
  95. */
  96. public function testAddingInvalidPluginLoaderTypeToAdapterShouldRaiseException()
  97. {
  98. $loader = new Zend_Loader_PluginLoader();
  99. $this->adapter->setPluginLoader($loader, 'bogus');
  100. }
  101. public function testAdapterShouldProxyAddingPluginLoaderPrefixPath()
  102. {
  103. $loader = $this->adapter->getPluginLoader('validate');
  104. $this->adapter->addPrefixPath('Foo_Valid', 'Foo/Valid/', 'validate');
  105. $paths = $loader->getPaths('Foo_Valid');
  106. $this->assertTrue(is_array($paths));
  107. }
  108. public function testPassingNoTypeWhenAddingPrefixPathToAdapterShouldGeneratePathsForAllTypes()
  109. {
  110. $this->adapter->addPrefixPath('Foo', 'Foo');
  111. $validateLoader = $this->adapter->getPluginLoader('validate');
  112. $filterLoader = $this->adapter->getPluginLoader('filter');
  113. $paths = $validateLoader->getPaths('Foo_Validate');
  114. $this->assertTrue(is_array($paths));
  115. $paths = $filterLoader->getPaths('Foo_Filter');
  116. $this->assertTrue(is_array($paths));
  117. }
  118. /**
  119. * @expectedException Zend_File_Transfer_Exception
  120. */
  121. public function testPassingInvalidTypeWhenAddingPrefixPathToAdapterShouldThrowException()
  122. {
  123. $this->adapter->addPrefixPath('Foo', 'Foo', 'bogus');
  124. }
  125. public function testAdapterShouldProxyAddingMultiplePluginLoaderPrefixPaths()
  126. {
  127. $validatorLoader = $this->adapter->getPluginLoader('validate');
  128. $filterLoader = $this->adapter->getPluginLoader('filter');
  129. $this->adapter->addPrefixPaths(array(
  130. 'validate' => array('prefix' => 'Foo_Valid', 'path' => 'Foo/Valid/'),
  131. 'filter' => array(
  132. 'Foo_Filter' => 'Foo/Filter/',
  133. 'Baz_Filter' => array(
  134. 'Baz/Filter/',
  135. 'My/Baz/Filter/',
  136. ),
  137. ),
  138. array('type' => 'filter', 'prefix' => 'Bar_Filter', 'path' => 'Bar/Filter/'),
  139. ));
  140. $paths = $validatorLoader->getPaths('Foo_Valid');
  141. $this->assertTrue(is_array($paths));
  142. $paths = $filterLoader->getPaths('Foo_Filter');
  143. $this->assertTrue(is_array($paths));
  144. $paths = $filterLoader->getPaths('Bar_Filter');
  145. $this->assertTrue(is_array($paths));
  146. $paths = $filterLoader->getPaths('Baz_Filter');
  147. $this->assertTrue(is_array($paths));
  148. $this->assertEquals(2, count($paths));
  149. }
  150. public function testValidatorPluginLoaderShouldRegisterPathsForBaseAndFileValidatorsByDefault()
  151. {
  152. $loader = $this->adapter->getPluginLoader('validate');
  153. $paths = $loader->getPaths('Zend_Validate');
  154. $this->assertTrue(is_array($paths));
  155. $paths = $loader->getPaths('Zend_Validate_File');
  156. $this->assertTrue(is_array($paths));
  157. }
  158. public function testAdapterShouldAllowAddingValidatorInstance()
  159. {
  160. $validator = new Zend_Validate_File_Count(array('min' => 1, 'max' => 1));
  161. $this->adapter->addValidator($validator);
  162. $test = $this->adapter->getValidator('Zend_Validate_File_Count');
  163. $this->assertSame($validator, $test);
  164. }
  165. public function testAdapterShouldAllowAddingValidatorViaPluginLoader()
  166. {
  167. $this->adapter->addValidator('Count', false, array('min' => 1, 'max' => 1));
  168. $test = $this->adapter->getValidator('Count');
  169. $this->assertTrue($test instanceof Zend_Validate_File_Count);
  170. }
  171. /**
  172. * @expectedException Zend_File_Transfer_Exception
  173. */
  174. public function testAdapterhShouldRaiseExceptionWhenAddingInvalidValidatorType()
  175. {
  176. $this->adapter->addValidator(new Zend_Filter_BaseName);
  177. }
  178. public function testAdapterShouldAllowAddingMultipleValidatorsAtOnceUsingBothInstancesAndPluginLoader()
  179. {
  180. $validators = array(
  181. 'count' => array('min' => 1, 'max' => 1),
  182. 'Exists' => 'C:\temp',
  183. array('validator' => 'Upload', 'options' => array(realpath(__FILE__))),
  184. new Zend_Validate_File_Extension('jpg'),
  185. );
  186. $this->adapter->addValidators($validators);
  187. $test = $this->adapter->getValidators();
  188. $this->assertTrue(is_array($test));
  189. $this->assertEquals(4, count($test), var_export($test, 1));
  190. $count = array_shift($test);
  191. $this->assertTrue($count instanceof Zend_Validate_File_Count);
  192. $exists = array_shift($test);
  193. $this->assertTrue($exists instanceof Zend_Validate_File_Exists);
  194. $size = array_shift($test);
  195. $this->assertTrue($size instanceof Zend_Validate_File_Upload);
  196. $ext = array_shift($test);
  197. $this->assertTrue($ext instanceof Zend_Validate_File_Extension);
  198. $orig = array_pop($validators);
  199. $this->assertSame($orig, $ext);
  200. }
  201. public function testGetValidatorShouldReturnNullWhenNoMatchingIdentifierExists()
  202. {
  203. $this->assertNull($this->adapter->getValidator('Alpha'));
  204. }
  205. public function testAdapterShouldAllowPullingValidatorsByFile()
  206. {
  207. $this->adapter->addValidator('Alpha', false, false, 'foo');
  208. $validators = $this->adapter->getValidators('foo');
  209. $this->assertEquals(1, count($validators));
  210. $validator = array_shift($validators);
  211. $this->assertTrue($validator instanceof Zend_Validate_Alpha);
  212. }
  213. public function testCallingSetValidatorsOnAdapterShouldOverwriteExistingValidators()
  214. {
  215. $this->testAdapterShouldAllowAddingMultipleValidatorsAtOnceUsingBothInstancesAndPluginLoader();
  216. $validators = array(
  217. new Zend_Validate_File_Count(1),
  218. new Zend_Validate_File_Extension('jpg'),
  219. );
  220. $this->adapter->setValidators($validators);
  221. $test = $this->adapter->getValidators();
  222. $this->assertSame($validators, array_values($test));
  223. }
  224. public function testAdapterShouldAllowRetrievingValidatorInstancesByClassName()
  225. {
  226. $this->testAdapterShouldAllowAddingMultipleValidatorsAtOnceUsingBothInstancesAndPluginLoader();
  227. $ext = $this->adapter->getValidator('Zend_Validate_File_Extension');
  228. $this->assertTrue($ext instanceof Zend_Validate_File_Extension);
  229. }
  230. public function testAdapterShouldAllowRetrievingValidatorInstancesByPluginName()
  231. {
  232. $this->testAdapterShouldAllowAddingMultipleValidatorsAtOnceUsingBothInstancesAndPluginLoader();
  233. $count = $this->adapter->getValidator('Count');
  234. $this->assertTrue($count instanceof Zend_Validate_File_Count);
  235. }
  236. public function testAdapterShouldAllowRetrievingAllValidatorsAtOnce()
  237. {
  238. $this->testAdapterShouldAllowAddingMultipleValidatorsAtOnceUsingBothInstancesAndPluginLoader();
  239. $validators = $this->adapter->getValidators();
  240. $this->assertTrue(is_array($validators));
  241. $this->assertEquals(4, count($validators));
  242. foreach ($validators as $validator) {
  243. $this->assertTrue($validator instanceof Zend_Validate_Interface);
  244. }
  245. }
  246. public function testAdapterShouldAllowRemovingValidatorInstancesByClassName()
  247. {
  248. $this->testAdapterShouldAllowAddingMultipleValidatorsAtOnceUsingBothInstancesAndPluginLoader();
  249. $this->assertTrue($this->adapter->hasValidator('Zend_Validate_File_Extension'));
  250. $this->adapter->removeValidator('Zend_Validate_File_Extension');
  251. $this->assertFalse($this->adapter->hasValidator('Zend_Validate_File_Extension'));
  252. }
  253. public function testAdapterShouldAllowRemovingValidatorInstancesByPluginName()
  254. {
  255. $this->testAdapterShouldAllowAddingMultipleValidatorsAtOnceUsingBothInstancesAndPluginLoader();
  256. $this->assertTrue($this->adapter->hasValidator('Count'));
  257. $this->adapter->removeValidator('Count');
  258. $this->assertFalse($this->adapter->hasValidator('Count'));
  259. }
  260. public function testRemovingNonexistentValidatorShouldDoNothing()
  261. {
  262. $this->testAdapterShouldAllowAddingMultipleValidatorsAtOnceUsingBothInstancesAndPluginLoader();
  263. $validators = $this->adapter->getValidators();
  264. $this->assertFalse($this->adapter->hasValidator('Alpha'));
  265. $this->adapter->removeValidator('Alpha');
  266. $this->assertFalse($this->adapter->hasValidator('Alpha'));
  267. $test = $this->adapter->getValidators();
  268. $this->assertSame($validators, $test);
  269. }
  270. public function testAdapterShouldAllowRemovingAllValidatorsAtOnce()
  271. {
  272. $this->testAdapterShouldAllowAddingMultipleValidatorsAtOnceUsingBothInstancesAndPluginLoader();
  273. $this->adapter->clearValidators();
  274. $validators = $this->adapter->getValidators();
  275. $this->assertTrue(is_array($validators));
  276. $this->assertEquals(0, count($validators));
  277. }
  278. public function testValidationShouldReturnTrueForValidTransfer()
  279. {
  280. $this->adapter->addValidator('Count', false, array(1, 3), 'foo');
  281. $this->assertTrue($this->adapter->isValid('foo'));
  282. }
  283. public function testValidationShouldReturnTrueForValidTransferOfMultipleFiles()
  284. {
  285. $this->assertTrue($this->adapter->isValid(null));
  286. }
  287. public function testValidationShouldReturnFalseForInvalidTransfer()
  288. {
  289. $this->adapter->addValidator('Extension', false, 'png', 'foo');
  290. $this->assertFalse($this->adapter->isValid('foo'));
  291. }
  292. public function testValidationShouldThrowExceptionForNonexistentFile()
  293. {
  294. $this->assertFalse($this->adapter->isValid('bogus'));
  295. }
  296. public function testErrorMessagesShouldBeEmptyByDefault()
  297. {
  298. $messages = $this->adapter->getMessages();
  299. $this->assertTrue(is_array($messages));
  300. $this->assertEquals(0, count($messages));
  301. }
  302. public function testErrorMessagesShouldBePopulatedAfterInvalidTransfer()
  303. {
  304. $this->testValidationShouldReturnFalseForInvalidTransfer();
  305. $messages = $this->adapter->getMessages();
  306. $this->assertTrue(is_array($messages));
  307. $this->assertFalse(empty($messages));
  308. }
  309. public function testErrorCodesShouldBeNullByDefault()
  310. {
  311. $errors = $this->adapter->getErrors();
  312. $this->assertTrue(is_array($errors));
  313. $this->assertEquals(0, count($errors));
  314. }
  315. public function testErrorCodesShouldBePopulatedAfterInvalidTransfer()
  316. {
  317. $this->testValidationShouldReturnFalseForInvalidTransfer();
  318. $errors = $this->adapter->getErrors();
  319. $this->assertTrue(is_array($errors));
  320. $this->assertFalse(empty($errors));
  321. }
  322. public function testAdapterShouldHavePluginLoaderForFilters()
  323. {
  324. $loader = $this->adapter->getPluginLoader('filter');
  325. $this->assertTrue($loader instanceof Zend_Loader_PluginLoader);
  326. }
  327. public function testFilterPluginLoaderShouldRegisterPathsForBaseAndFileFiltersByDefault()
  328. {
  329. $loader = $this->adapter->getPluginLoader('filter');
  330. $paths = $loader->getPaths('Zend_Filter');
  331. $this->assertTrue(is_array($paths));
  332. $paths = $loader->getPaths('Zend_Filter_File');
  333. $this->assertTrue(is_array($paths));
  334. }
  335. public function testAdapterShouldAllowAddingFilterInstance()
  336. {
  337. $filter = new Zend_Filter_StringToLower();
  338. $this->adapter->addFilter($filter);
  339. $test = $this->adapter->getFilter('Zend_Filter_StringToLower');
  340. $this->assertSame($filter, $test);
  341. }
  342. public function testAdapterShouldAllowAddingFilterViaPluginLoader()
  343. {
  344. $this->adapter->addFilter('StringTrim');
  345. $test = $this->adapter->getFilter('StringTrim');
  346. $this->assertTrue($test instanceof Zend_Filter_StringTrim);
  347. }
  348. /**
  349. * @expectedException Zend_File_Transfer_Exception
  350. */
  351. public function testAdapterhShouldRaiseExceptionWhenAddingInvalidFilterType()
  352. {
  353. $this->adapter->addFilter(new Zend_Validate_File_Extension('jpg'));
  354. }
  355. public function testAdapterShouldAllowAddingMultipleFiltersAtOnceUsingBothInstancesAndPluginLoader()
  356. {
  357. $filters = array(
  358. 'Word_SeparatorToCamelCase' => array('separator' => ' '),
  359. array('filter' => 'Alpha', 'options' => array(true)),
  360. new Zend_Filter_BaseName(),
  361. );
  362. $this->adapter->addFilters($filters);
  363. $test = $this->adapter->getFilters();
  364. $this->assertTrue(is_array($test));
  365. $this->assertEquals(3, count($test), var_export($test, 1));
  366. $count = array_shift($test);
  367. $this->assertTrue($count instanceof Zend_Filter_Word_SeparatorToCamelCase);
  368. $size = array_shift($test);
  369. $this->assertTrue($size instanceof Zend_Filter_Alpha);
  370. $ext = array_shift($test);
  371. $orig = array_pop($filters);
  372. $this->assertSame($orig, $ext);
  373. }
  374. public function testGetFilterShouldReturnNullWhenNoMatchingIdentifierExists()
  375. {
  376. $this->assertNull($this->adapter->getFilter('Alpha'));
  377. }
  378. public function testAdapterShouldAllowPullingFiltersByFile()
  379. {
  380. $this->adapter->addFilter('Alpha', false, 'foo');
  381. $filters = $this->adapter->getFilters('foo');
  382. $this->assertEquals(1, count($filters));
  383. $filter = array_shift($filters);
  384. $this->assertTrue($filter instanceof Zend_Filter_Alpha);
  385. }
  386. public function testCallingSetFiltersOnAdapterShouldOverwriteExistingFilters()
  387. {
  388. $this->testAdapterShouldAllowAddingMultipleFiltersAtOnceUsingBothInstancesAndPluginLoader();
  389. $filters = array(
  390. new Zend_Filter_StringToUpper(),
  391. new Zend_Filter_Alpha(),
  392. );
  393. $this->adapter->setFilters($filters);
  394. $test = $this->adapter->getFilters();
  395. $this->assertSame($filters, array_values($test));
  396. }
  397. public function testAdapterShouldAllowRetrievingFilterInstancesByClassName()
  398. {
  399. $this->testAdapterShouldAllowAddingMultipleFiltersAtOnceUsingBothInstancesAndPluginLoader();
  400. $ext = $this->adapter->getFilter('Zend_Filter_BaseName');
  401. $this->assertTrue($ext instanceof Zend_Filter_BaseName);
  402. }
  403. public function testAdapterShouldAllowRetrievingFilterInstancesByPluginName()
  404. {
  405. $this->testAdapterShouldAllowAddingMultipleFiltersAtOnceUsingBothInstancesAndPluginLoader();
  406. $count = $this->adapter->getFilter('Alpha');
  407. $this->assertTrue($count instanceof Zend_Filter_Alpha);
  408. }
  409. public function testAdapterShouldAllowRetrievingAllFiltersAtOnce()
  410. {
  411. $this->testAdapterShouldAllowAddingMultipleFiltersAtOnceUsingBothInstancesAndPluginLoader();
  412. $filters = $this->adapter->getFilters();
  413. $this->assertTrue(is_array($filters));
  414. $this->assertEquals(3, count($filters));
  415. foreach ($filters as $filter) {
  416. $this->assertTrue($filter instanceof Zend_Filter_Interface);
  417. }
  418. }
  419. public function testAdapterShouldAllowRemovingFilterInstancesByClassName()
  420. {
  421. $this->testAdapterShouldAllowAddingMultipleFiltersAtOnceUsingBothInstancesAndPluginLoader();
  422. $this->assertTrue($this->adapter->hasFilter('Zend_Filter_BaseName'));
  423. $this->adapter->removeFilter('Zend_Filter_BaseName');
  424. $this->assertFalse($this->adapter->hasFilter('Zend_Filter_BaseName'));
  425. }
  426. public function testAdapterShouldAllowRemovingFilterInstancesByPluginName()
  427. {
  428. $this->testAdapterShouldAllowAddingMultipleFiltersAtOnceUsingBothInstancesAndPluginLoader();
  429. $this->assertTrue($this->adapter->hasFilter('Alpha'));
  430. $this->adapter->removeFilter('Alpha');
  431. $this->assertFalse($this->adapter->hasFilter('Alpha'));
  432. }
  433. public function testRemovingNonexistentFilterShouldDoNothing()
  434. {
  435. $this->testAdapterShouldAllowAddingMultipleFiltersAtOnceUsingBothInstancesAndPluginLoader();
  436. $filters = $this->adapter->getFilters();
  437. $this->assertFalse($this->adapter->hasFilter('Int'));
  438. $this->adapter->removeFilter('Int');
  439. $this->assertFalse($this->adapter->hasFilter('Int'));
  440. $test = $this->adapter->getFilters();
  441. $this->assertSame($filters, $test);
  442. }
  443. public function testAdapterShouldAllowRemovingAllFiltersAtOnce()
  444. {
  445. $this->testAdapterShouldAllowAddingMultipleFiltersAtOnceUsingBothInstancesAndPluginLoader();
  446. $this->adapter->clearFilters();
  447. $filters = $this->adapter->getFilters();
  448. $this->assertTrue(is_array($filters));
  449. $this->assertEquals(0, count($filters));
  450. }
  451. public function testTransferDestinationShouldBeMutable()
  452. {
  453. $directory = dirname(__FILE__);
  454. $this->adapter->setDestination($directory);
  455. $destinations = $this->adapter->getDestination();
  456. $this->assertTrue(is_array($destinations));
  457. foreach ($destinations as $file => $destination) {
  458. $this->assertEquals($directory, $destination);
  459. }
  460. $newdirectory = dirname(__FILE__)
  461. . DIRECTORY_SEPARATOR . '..'
  462. . DIRECTORY_SEPARATOR . '..'
  463. . DIRECTORY_SEPARATOR . '..'
  464. . DIRECTORY_SEPARATOR . '_files';
  465. $this->adapter->setDestination($newdirectory, 'foo');
  466. $this->assertEquals($newdirectory, $this->adapter->getDestination('foo'));
  467. $this->assertEquals($directory, $this->adapter->getDestination('bar'));
  468. }
  469. public function testAdapterShouldAllowRetrievingDestinationsForAnArrayOfSpecifiedFiles()
  470. {
  471. $this->adapter->setDestination(dirname(__FILE__));
  472. $destinations = $this->adapter->getDestination(array('bar', 'baz'));
  473. $this->assertTrue(is_array($destinations));
  474. $directory = dirname(__FILE__);
  475. foreach ($destinations as $file => $destination) {
  476. $this->assertTrue(in_array($file, array('bar', 'baz')));
  477. $this->assertEquals($directory, $destination);
  478. }
  479. }
  480. public function testSettingAndRetrievingOptions()
  481. {
  482. $this->assertEquals(
  483. array(
  484. 'bar' => array('ignoreNoFile' => false, 'useByteString' => true),
  485. 'baz' => array('ignoreNoFile' => false, 'useByteString' => true),
  486. 'foo' => array('ignoreNoFile' => false, 'useByteString' => true),
  487. 'file_0_' => array('ignoreNoFile' => false, 'useByteString' => true),
  488. 'file_1_' => array('ignoreNoFile' => false, 'useByteString' => true),
  489. ), $this->adapter->getOptions());
  490. $this->adapter->setOptions(array('ignoreNoFile' => true));
  491. $this->assertEquals(
  492. array(
  493. 'bar' => array('ignoreNoFile' => true, 'useByteString' => true),
  494. 'baz' => array('ignoreNoFile' => true, 'useByteString' => true),
  495. 'foo' => array('ignoreNoFile' => true, 'useByteString' => true),
  496. 'file_0_' => array('ignoreNoFile' => true, 'useByteString' => true),
  497. 'file_1_' => array('ignoreNoFile' => true, 'useByteString' => true),
  498. ), $this->adapter->getOptions());
  499. $this->adapter->setOptions(array('ignoreNoFile' => false), 'foo');
  500. $this->assertEquals(
  501. array(
  502. 'bar' => array('ignoreNoFile' => true, 'useByteString' => true),
  503. 'baz' => array('ignoreNoFile' => true, 'useByteString' => true),
  504. 'foo' => array('ignoreNoFile' => false, 'useByteString' => true),
  505. 'file_0_' => array('ignoreNoFile' => true, 'useByteString' => true),
  506. 'file_1_' => array('ignoreNoFile' => true, 'useByteString' => true),
  507. ), $this->adapter->getOptions());
  508. }
  509. public function testGetAllAdditionalFileInfos()
  510. {
  511. $files = $this->adapter->getFileInfo();
  512. $this->assertEquals(5, count($files));
  513. $this->assertEquals('baz.text', $files['baz']['name']);
  514. }
  515. public function testGetAdditionalFileInfosForSingleFile()
  516. {
  517. $files = $this->adapter->getFileInfo('baz');
  518. $this->assertEquals(1, count($files));
  519. $this->assertEquals('baz.text', $files['baz']['name']);
  520. }
  521. /**
  522. * @expectedException Zend_File_Transfer_Exception
  523. */
  524. public function testGetAdditionalFileInfosForUnknownFile()
  525. {
  526. $files = $this->adapter->getFileInfo('unknown');
  527. }
  528. /**
  529. * @expectedException Zend_File_Transfer_Exception
  530. */
  531. public function testGetUnknownOption()
  532. {
  533. $this->adapter->setOptions(array('unknownOption' => 'unknown'));
  534. }
  535. /**
  536. * @expectedException Zend_File_Transfer_Exception
  537. */
  538. public function testGetFileIsNotImplemented()
  539. {
  540. $this->adapter->getFile();
  541. }
  542. /**
  543. * @expectedException Zend_File_Transfer_Exception
  544. */
  545. public function testAddFileIsNotImplemented()
  546. {
  547. $this->adapter->addFile('foo');
  548. }
  549. /**
  550. * @expectedException Zend_File_Transfer_Exception
  551. */
  552. public function testGetTypeIsNotImplemented()
  553. {
  554. $this->adapter->getType();
  555. }
  556. /**
  557. * @expectedException Zend_File_Transfer_Exception
  558. */
  559. public function testAddTypeIsNotImplemented()
  560. {
  561. $this->adapter->addType('foo');
  562. }
  563. public function testAdapterShouldAllowRetrievingFileName()
  564. {
  565. $path = dirname(__FILE__)
  566. . DIRECTORY_SEPARATOR . '..'
  567. . DIRECTORY_SEPARATOR . '..'
  568. . DIRECTORY_SEPARATOR . '..'
  569. . DIRECTORY_SEPARATOR . '_files';
  570. $this->adapter->setDestination($path);
  571. $this->assertEquals($path . DIRECTORY_SEPARATOR . 'foo.jpg', $this->adapter->getFileName('foo'));
  572. }
  573. public function testAdapterShouldAllowRetrievingFileNameWithoutPath()
  574. {
  575. $path = dirname(__FILE__)
  576. . DIRECTORY_SEPARATOR . '..'
  577. . DIRECTORY_SEPARATOR . '..'
  578. . DIRECTORY_SEPARATOR . '..'
  579. . DIRECTORY_SEPARATOR . '_files';
  580. $this->adapter->setDestination($path);
  581. $this->assertEquals('foo.jpg', $this->adapter->getFileName('foo', false));
  582. }
  583. public function testAdapterShouldAllowRetrievingAllFileNames()
  584. {
  585. $path = dirname(__FILE__)
  586. . DIRECTORY_SEPARATOR . '..'
  587. . DIRECTORY_SEPARATOR . '..'
  588. . DIRECTORY_SEPARATOR . '..'
  589. . DIRECTORY_SEPARATOR . '_files';
  590. $this->adapter->setDestination($path);
  591. $files = $this->adapter->getFileName();
  592. $this->assertTrue(is_array($files));
  593. $this->assertEquals($path . DIRECTORY_SEPARATOR . 'bar.png', $files['bar']);
  594. }
  595. public function testAdapterShouldAllowRetrievingAllFileNamesWithoutPath()
  596. {
  597. $path = dirname(__FILE__)
  598. . DIRECTORY_SEPARATOR . '..'
  599. . DIRECTORY_SEPARATOR . '..'
  600. . DIRECTORY_SEPARATOR . '..'
  601. . DIRECTORY_SEPARATOR . '_files';
  602. $this->adapter->setDestination($path);
  603. $files = $this->adapter->getFileName(null, false);
  604. $this->assertTrue(is_array($files));
  605. $this->assertEquals('bar.png', $files['bar']);
  606. }
  607. public function testExceptionForUnknownHashValue()
  608. {
  609. try {
  610. $this->adapter->getHash('foo', 'unknown_hash');
  611. $this->fail();
  612. } catch (Zend_Exception $e) {
  613. $this->assertContains('Unknown hash algorithm', $e->getMessage());
  614. }
  615. }
  616. public function testEmptyTempDirectoryDetection()
  617. {
  618. $this->adapter->_tmpDir = "";
  619. $this->assertTrue(empty($this->adapter->_tmpDir), "Empty temporary directory");
  620. }
  621. public function testTempDirectoryDetection()
  622. {
  623. $this->adapter->getTmpDir();
  624. $this->assertTrue(!empty($this->adapter->_tmpDir), "Temporary directory filled");
  625. }
  626. public function testTemporaryDirectoryAccessDetection()
  627. {
  628. $this->adapter->_tmpDir = ".";
  629. $path = "/NoPath/To/File";
  630. $this->assertFalse($this->adapter->isPathWriteable($path));
  631. $this->assertTrue($this->adapter->isPathWriteable($this->adapter->_tmpDir));
  632. }
  633. public function testFileSizeButNoFileFound()
  634. {
  635. try {
  636. $this->assertEquals(10, $this->adapter->getFileSize());
  637. $this->fail();
  638. } catch (Zend_File_Transfer_Exception $e) {
  639. $this->assertContains('does not exist', $e->getMessage());
  640. }
  641. }
  642. public function testFileSizeByTmpName()
  643. {
  644. $options = $this->adapter->getOptions();
  645. $this->assertTrue($options['baz']['useByteString']);
  646. $this->assertEquals('8B', $this->adapter->getFileSize('baz.text'));
  647. $this->adapter->setOptions(array('useByteString' => false));
  648. $options = $this->adapter->getOptions();
  649. $this->assertFalse($options['baz']['useByteString']);
  650. $this->assertEquals(8, $this->adapter->getFileSize('baz.text'));
  651. }
  652. public function testMimeTypeButNoFileFound()
  653. {
  654. try {
  655. $this->assertEquals('image/jpeg', $this->adapter->getMimeType());
  656. $this->fail();
  657. } catch (Zend_File_Transfer_Exception $e) {
  658. $this->assertContains('does not exist', $e->getMessage());
  659. }
  660. }
  661. public function testMimeTypeByTmpName()
  662. {
  663. $this->assertEquals('text/plain', $this->adapter->getMimeType('baz.text'));
  664. }
  665. public function testSetOwnErrorMessage()
  666. {
  667. $this->adapter->addValidator('Count', false, array('min' => 5, 'max' => 5, 'messages' => array(Zend_Validate_File_Count::TOO_LESS => 'Zu wenige')));
  668. $this->assertFalse($this->adapter->isValid('foo'));
  669. $message = $this->adapter->getMessages();
  670. $this->assertContains('Zu wenige', $message);
  671. try {
  672. $this->assertEquals('image/jpeg', $this->adapter->getMimeType());
  673. $this->fail();
  674. } catch (Zend_File_Transfer_Exception $e) {
  675. $this->assertContains('does not exist', $e->getMessage());
  676. }
  677. }
  678. public function testTransferDestinationAtNonExistingElement()
  679. {
  680. $directory = dirname(__FILE__);
  681. $this->adapter->setDestination($directory, 'nonexisting');
  682. $this->assertEquals($directory, $this->adapter->getDestination('nonexisting'));
  683. try {
  684. $this->assertTrue(is_string($this->adapter->getDestination('reallynonexisting')));
  685. $this->fail();
  686. } catch(Exception $e) {
  687. $this->assertContains('not found', $e->getMessage());
  688. }
  689. }
  690. /**
  691. * @ZF-7376
  692. */
  693. public function testSettingMagicFile()
  694. {
  695. $this->adapter->setOptions(array('magicFile' => 'test/file'));
  696. $this->assertEquals(
  697. array(
  698. 'bar' => array('magicFile' => 'test/file', 'ignoreNoFile' => false, 'useByteString' => true),
  699. ), $this->adapter->getOptions('bar'));
  700. }
  701. }
  702. class Zend_File_Transfer_Adapter_AbstractTest_MockAdapter extends Zend_File_Transfer_Adapter_Abstract
  703. {
  704. public $received = false;
  705. public $_tmpDir;
  706. public function __construct()
  707. {
  708. $testfile = dirname(__FILE__) . '/_files/test.txt';
  709. $this->_files = array(
  710. 'foo' => array(
  711. 'name' => 'foo.jpg',
  712. 'type' => 'image/jpeg',
  713. 'size' => 126976,
  714. 'tmp_name' => '/tmp/489127ba5c89c',
  715. 'options' => array('ignoreNoFile' => false, 'useByteString' => true),
  716. 'validated' => false,
  717. 'received' => false,
  718. 'filtered' => false,
  719. ),
  720. 'bar' => array(
  721. 'name' => 'bar.png',
  722. 'type' => 'image/png',
  723. 'size' => 91136,
  724. 'tmp_name' => '/tmp/489128284b51f',
  725. 'options' => array('ignoreNoFile' => false, 'useByteString' => true),
  726. 'validated' => false,
  727. 'received' => false,
  728. 'filtered' => false,
  729. ),
  730. 'baz' => array(
  731. 'name' => 'baz.text',
  732. 'type' => 'text/plain',
  733. 'size' => 1172,
  734. 'tmp_name' => $testfile,
  735. 'options' => array('ignoreNoFile' => false, 'useByteString' => true),
  736. 'validated' => false,
  737. 'received' => false,
  738. 'filtered' => false,
  739. ),
  740. 'file_0_' => array(
  741. 'name' => 'foo.jpg',
  742. 'type' => 'image/jpeg',
  743. 'size' => 126976,
  744. 'tmp_name' => '/tmp/489127ba5c89c',
  745. 'options' => array('ignoreNoFile' => false, 'useByteString' => true),
  746. 'validated' => false,
  747. 'received' => false,
  748. 'filtered' => false,
  749. ),
  750. 'file_1_' => array(
  751. 'name' => 'baz.text',
  752. 'type' => 'text/plain',
  753. 'size' => 1172,
  754. 'tmp_name' => $testfile,
  755. 'options' => array('ignoreNoFile' => false, 'useByteString' => true),
  756. 'validated' => false,
  757. 'received' => false,
  758. 'filtered' => false,
  759. ),
  760. 'file' => array(
  761. 'name' => 'foo.jpg',
  762. 'multifiles' => array(0 => 'file_0_', 1 => 'file_1_')
  763. ),
  764. );
  765. }
  766. public function send($options = null)
  767. {
  768. return;
  769. }
  770. public function receive($options = null)
  771. {
  772. $this->received = true;
  773. return;
  774. }
  775. public function isSent($file = null)
  776. {
  777. return false;
  778. }
  779. public function isReceived($file = null)
  780. {
  781. return $this->received;
  782. }
  783. public function isUploaded($files = null)
  784. {
  785. return true;
  786. }
  787. public function isFiltered($files = null)
  788. {
  789. return true;
  790. }
  791. public static function getProgress()
  792. {
  793. return;
  794. }
  795. public function getTmpDir()
  796. {
  797. $this->_tmpDir = parent::_getTmpDir();
  798. }
  799. public function isPathWriteable($path)
  800. {
  801. return parent::_isPathWriteable($path);
  802. }
  803. }
  804. // Call Zend_File_Transfer_Adapter_AbstractTest::main() if this source file is executed directly.
  805. if (PHPUnit_MAIN_METHOD == "Zend_File_Transfer_Adapter_AbstractTest::main") {
  806. Zend_File_Transfer_Adapter_AbstractTest::main();
  807. }