AbstractTest.php 37 KB

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