AbstractTest.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  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-2010 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-2010 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, 'detectInfos' => 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, 'detectInfos' => 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, 'detectInfos' => 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 testIgnoreHashValue()
  617. {
  618. $this->adapter->addInvalidFile();
  619. $return = $this->adapter->getHash('crc32', 'test');
  620. $this->assertEquals(array(), $return);
  621. }
  622. public function testEmptyTempDirectoryDetection()
  623. {
  624. $this->adapter->_tmpDir = "";
  625. $this->assertTrue(empty($this->adapter->_tmpDir), "Empty temporary directory");
  626. }
  627. public function testTempDirectoryDetection()
  628. {
  629. $this->adapter->getTmpDir();
  630. $this->assertTrue(!empty($this->adapter->_tmpDir), "Temporary directory filled");
  631. }
  632. public function testTemporaryDirectoryAccessDetection()
  633. {
  634. $this->adapter->_tmpDir = ".";
  635. $path = "/NoPath/To/File";
  636. $this->assertFalse($this->adapter->isPathWriteable($path));
  637. $this->assertTrue($this->adapter->isPathWriteable($this->adapter->_tmpDir));
  638. }
  639. public function testFileSizeButNoFileFound()
  640. {
  641. try {
  642. $this->assertEquals(10, $this->adapter->getFileSize());
  643. $this->fail();
  644. } catch (Zend_File_Transfer_Exception $e) {
  645. $this->assertContains('does not exist', $e->getMessage());
  646. }
  647. }
  648. public function testIgnoreFileSize()
  649. {
  650. $this->adapter->addInvalidFile();
  651. $return = $this->adapter->getFileSize('test');
  652. $this->assertEquals(array(), $return);
  653. }
  654. public function testFileSizeByTmpName()
  655. {
  656. $options = $this->adapter->getOptions();
  657. $this->assertTrue($options['baz']['useByteString']);
  658. $this->assertEquals('1.14kB', $this->adapter->getFileSize('baz.text'));
  659. $this->adapter->setOptions(array('useByteString' => false));
  660. $options = $this->adapter->getOptions();
  661. $this->assertFalse($options['baz']['useByteString']);
  662. $this->assertEquals(1172, $this->adapter->getFileSize('baz.text'));
  663. }
  664. public function testMimeTypeButNoFileFound()
  665. {
  666. try {
  667. $this->assertEquals('image/jpeg', $this->adapter->getMimeType());
  668. $this->fail();
  669. } catch (Zend_File_Transfer_Exception $e) {
  670. $this->assertContains('does not exist', $e->getMessage());
  671. }
  672. }
  673. public function testIgnoreMimeType()
  674. {
  675. $this->adapter->addInvalidFile();
  676. $return = $this->adapter->getMimeType('test');
  677. $this->assertEquals(array(), $return);
  678. }
  679. public function testMimeTypeByTmpName()
  680. {
  681. $this->assertEquals('text/plain', $this->adapter->getMimeType('baz.text'));
  682. }
  683. public function testSetOwnErrorMessage()
  684. {
  685. $this->adapter->addValidator('Count', false, array('min' => 5, 'max' => 5, 'messages' => array(Zend_Validate_File_Count::TOO_FEW => 'Zu wenige')));
  686. $this->assertFalse($this->adapter->isValid('foo'));
  687. $message = $this->adapter->getMessages();
  688. $this->assertContains('Zu wenige', $message);
  689. try {
  690. $this->assertEquals('image/jpeg', $this->adapter->getMimeType());
  691. $this->fail();
  692. } catch (Zend_File_Transfer_Exception $e) {
  693. $this->assertContains('does not exist', $e->getMessage());
  694. }
  695. }
  696. public function testTransferDestinationAtNonExistingElement()
  697. {
  698. $directory = dirname(__FILE__);
  699. $this->adapter->setDestination($directory, 'nonexisting');
  700. $this->assertEquals($directory, $this->adapter->getDestination('nonexisting'));
  701. try {
  702. $this->assertTrue(is_string($this->adapter->getDestination('reallynonexisting')));
  703. $this->fail();
  704. } catch(Exception $e) {
  705. $this->assertContains('not found', $e->getMessage());
  706. }
  707. }
  708. /**
  709. * @ZF-7376
  710. */
  711. public function testSettingMagicFile()
  712. {
  713. $this->adapter->setOptions(array('magicFile' => 'test/file'));
  714. $this->assertEquals(
  715. array(
  716. 'bar' => array('magicFile' => 'test/file', 'ignoreNoFile' => false, 'useByteString' => true),
  717. ), $this->adapter->getOptions('bar'));
  718. }
  719. /**
  720. * @ZF-8693
  721. */
  722. public function testAdapterShouldAllowAddingMultipleValidatorsAtOnceUsingBothInstancesAndPluginLoaderForDifferentFiles()
  723. {
  724. $validators = array(
  725. array('MimeType', true, array('image/jpeg')), // no files
  726. array('FilesSize', true, array('max' => '1MB', 'messages' => 'файл больше 1MБ')), // no files
  727. array('Count', true, array('min' => 1, 'max' => '1', 'messages' => 'файл не 1'), 'bar'), // 'bar' from config
  728. array('MimeType', true, array('image/jpeg'), 'bar'), // 'bar' from config
  729. );
  730. $this->adapter->addValidators($validators, 'foo'); // set validators to 'foo'
  731. $test = $this->adapter->getValidators();
  732. $this->assertEquals(3, count($test));
  733. //test files specific validators
  734. $test = $this->adapter->getValidators('foo');
  735. $this->assertEquals(2, count($test));
  736. $mimeType = array_shift($test);
  737. $this->assertTrue($mimeType instanceof Zend_Validate_File_MimeType);
  738. $filesSize = array_shift($test);
  739. $this->assertTrue($filesSize instanceof Zend_Validate_File_FilesSize);
  740. $test = $this->adapter->getValidators('bar');
  741. $this->assertEquals(2, count($test));
  742. $filesSize = array_shift($test);
  743. $this->assertTrue($filesSize instanceof Zend_Validate_File_Count);
  744. $mimeType = array_shift($test);
  745. $this->assertTrue($mimeType instanceof Zend_Validate_File_MimeType);
  746. $test = $this->adapter->getValidators('baz');
  747. $this->assertEquals(0, count($test));
  748. }
  749. /**
  750. * @ZF-9132
  751. */
  752. public function testSettingAndRetrievingDetectInfosOption()
  753. {
  754. $this->assertEquals(array(
  755. 'foo' => array(
  756. 'ignoreNoFile' => false,
  757. 'useByteString' => true,
  758. 'detectInfos' => true))
  759. , $this->adapter->getOptions('foo'));
  760. $this->adapter->setOptions(array('detectInfos' => false));
  761. $this->assertEquals(array(
  762. 'foo' => array(
  763. 'ignoreNoFile' => false,
  764. 'useByteString' => true,
  765. 'detectInfos' => false))
  766. , $this->adapter->getOptions('foo'));
  767. }
  768. }
  769. class Zend_File_Transfer_Adapter_AbstractTest_MockAdapter extends Zend_File_Transfer_Adapter_Abstract
  770. {
  771. public $received = false;
  772. public $_tmpDir;
  773. public function __construct()
  774. {
  775. $testfile = dirname(__FILE__) . '/_files/test.txt';
  776. $this->_files = array(
  777. 'foo' => array(
  778. 'name' => 'foo.jpg',
  779. 'type' => 'image/jpeg',
  780. 'size' => 126976,
  781. 'tmp_name' => '/tmp/489127ba5c89c',
  782. 'options' => array('ignoreNoFile' => false, 'useByteString' => true, 'detectInfos' => true),
  783. 'validated' => false,
  784. 'received' => false,
  785. 'filtered' => false,
  786. ),
  787. 'bar' => array(
  788. 'name' => 'bar.png',
  789. 'type' => 'image/png',
  790. 'size' => 91136,
  791. 'tmp_name' => '/tmp/489128284b51f',
  792. 'options' => array('ignoreNoFile' => false, 'useByteString' => true),
  793. 'validated' => false,
  794. 'received' => false,
  795. 'filtered' => false,
  796. ),
  797. 'baz' => array(
  798. 'name' => 'baz.text',
  799. 'type' => 'text/plain',
  800. 'size' => 1172,
  801. 'tmp_name' => $testfile,
  802. 'options' => array('ignoreNoFile' => false, 'useByteString' => true),
  803. 'validated' => false,
  804. 'received' => false,
  805. 'filtered' => false,
  806. ),
  807. 'file_0_' => array(
  808. 'name' => 'foo.jpg',
  809. 'type' => 'image/jpeg',
  810. 'size' => 126976,
  811. 'tmp_name' => '/tmp/489127ba5c89c',
  812. 'options' => array('ignoreNoFile' => false, 'useByteString' => true),
  813. 'validated' => false,
  814. 'received' => false,
  815. 'filtered' => false,
  816. ),
  817. 'file_1_' => array(
  818. 'name' => 'baz.text',
  819. 'type' => 'text/plain',
  820. 'size' => 1172,
  821. 'tmp_name' => $testfile,
  822. 'options' => array('ignoreNoFile' => false, 'useByteString' => true),
  823. 'validated' => false,
  824. 'received' => false,
  825. 'filtered' => false,
  826. ),
  827. 'file' => array(
  828. 'name' => 'foo.jpg',
  829. 'multifiles' => array(0 => 'file_0_', 1 => 'file_1_')
  830. ),
  831. );
  832. }
  833. public function send($options = null)
  834. {
  835. return;
  836. }
  837. public function receive($options = null)
  838. {
  839. $this->received = true;
  840. return;
  841. }
  842. public function isSent($file = null)
  843. {
  844. return false;
  845. }
  846. public function isReceived($file = null)
  847. {
  848. return $this->received;
  849. }
  850. public function isUploaded($files = null)
  851. {
  852. return true;
  853. }
  854. public function isFiltered($files = null)
  855. {
  856. return true;
  857. }
  858. public static function getProgress()
  859. {
  860. return;
  861. }
  862. public function getTmpDir()
  863. {
  864. $this->_tmpDir = parent::_getTmpDir();
  865. }
  866. public function isPathWriteable($path)
  867. {
  868. return parent::_isPathWriteable($path);
  869. }
  870. public function addInvalidFile()
  871. {
  872. $this->_files += array(
  873. 'test' => array(
  874. 'name' => 'test.txt',
  875. 'type' => 'image/jpeg',
  876. 'size' => 0,
  877. 'tmp_name' => '',
  878. 'options' => array('ignoreNoFile' => true, 'useByteString' => true),
  879. 'validated' => false,
  880. 'received' => false,
  881. 'filtered' => false,
  882. )
  883. );
  884. }
  885. }
  886. // Call Zend_File_Transfer_Adapter_AbstractTest::main() if this source file is executed directly.
  887. if (PHPUnit_MAIN_METHOD == "Zend_File_Transfer_Adapter_AbstractTest::main") {
  888. Zend_File_Transfer_Adapter_AbstractTest::main();
  889. }