AbstractTest.php 36 KB

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