2
0

AbstractTest.php 32 KB

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