HttpTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. // Call Zend_File_Transfer_Adapter_HttpTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_File_Transfer_Adapter_HttpTest::main");
  5. }
  6. require_once dirname(__FILE__) . '/../../../../TestHelper.php';
  7. require_once 'Zend/File/Transfer/Adapter/Http.php';
  8. require_once 'Zend/Filter/BaseName.php';
  9. require_once 'Zend/Filter/StringToLower.php';
  10. require_once 'Zend/Loader/PluginLoader.php';
  11. require_once 'Zend/Validate/File/Count.php';
  12. require_once 'Zend/Validate/File/Extension.php';
  13. require_once 'Zend/Validate/File/Upload.php';
  14. /**
  15. * Test class for Zend_File_Transfer_Adapter_Http
  16. */
  17. class Zend_File_Transfer_Adapter_HttpTest extends PHPUnit_Framework_TestCase
  18. {
  19. /**
  20. * Runs the test methods of this class.
  21. *
  22. * @return void
  23. */
  24. public static function main()
  25. {
  26. $suite = new PHPUnit_Framework_TestSuite("Zend_File_Transfer_Adapter_HttpTest");
  27. $result = PHPUnit_TextUI_TestRunner::run($suite);
  28. }
  29. /**
  30. * Sets up the fixture, for example, open a network connection.
  31. * This method is called before a test is executed.
  32. *
  33. * @return void
  34. */
  35. public function setUp()
  36. {
  37. $_FILES = array(
  38. 'txt' => array(
  39. 'name' => 'file.txt',
  40. 'type' => 'plain/text',
  41. 'size' => 8,
  42. 'tmp_name' => 'file.txt',
  43. 'error' => 0));
  44. $this->adapter = new Zend_File_Transfer_Adapter_HttpTest_MockAdapter();
  45. }
  46. /**
  47. * Tears down the fixture, for example, close a network connection.
  48. * This method is called after a test is executed.
  49. *
  50. * @return void
  51. */
  52. public function tearDown()
  53. {
  54. }
  55. public function testEmptyAdapter()
  56. {
  57. $files = $this->adapter->getFileName();
  58. $this->assertContains('file.txt', $files);
  59. }
  60. public function testAutoSetUploadValidator()
  61. {
  62. $validators = array(
  63. new Zend_Validate_File_Count(1),
  64. new Zend_Validate_File_Extension('jpg'),
  65. );
  66. $this->adapter->setValidators($validators);
  67. $test = $this->adapter->getValidator('Upload');
  68. $this->assertTrue($test instanceof Zend_Validate_File_Upload);
  69. }
  70. /**
  71. * @expectedException Zend_File_Transfer_Exception
  72. */
  73. public function testSendingFiles()
  74. {
  75. $this->adapter->send();
  76. }
  77. /**
  78. * @expectedException Zend_File_Transfer_Exception
  79. */
  80. public function testFileIsSent()
  81. {
  82. $this->adapter->isSent();
  83. }
  84. public function testFileIsUploaded()
  85. {
  86. $this->assertTrue($this->adapter->isUploaded());
  87. }
  88. public function testFileIsNotUploaded()
  89. {
  90. $this->assertFalse($this->adapter->isUploaded('unknownFile'));
  91. }
  92. public function testFileIsNotFiltered()
  93. {
  94. $this->assertFalse($this->adapter->isFiltered('unknownFile'));
  95. $this->assertFalse($this->adapter->isFiltered());
  96. }
  97. public function testFileIsNotReceived()
  98. {
  99. $this->assertFalse($this->adapter->isReceived('unknownFile'));
  100. $this->assertFalse($this->adapter->isReceived());
  101. }
  102. public function testReceiveUnknownFile()
  103. {
  104. try {
  105. $this->assertFalse($this->adapter->receive('unknownFile'));
  106. } catch (Zend_File_Transfer_Exception $e) {
  107. $this->assertContains('not found', $e->getMessage());
  108. }
  109. }
  110. public function testReceiveValidatedFile()
  111. {
  112. $this->assertFalse($this->adapter->receive());
  113. }
  114. public function testReceiveIgnoredFile()
  115. {
  116. $this->adapter->setOptions(array('ignoreNoFile' => true));
  117. $this->assertTrue($this->adapter->receive());
  118. }
  119. public function testReceiveWithRenameFilter()
  120. {
  121. $this->adapter->addFilter('Rename', array('target' => '/testdir'));
  122. $this->adapter->setOptions(array('ignoreNoFile' => true));
  123. $this->assertTrue($this->adapter->receive());
  124. }
  125. public function testReceiveWithRenameFilterButWithoutDirectory()
  126. {
  127. $this->adapter->setDestination(dirname(__FILE__));
  128. $this->adapter->addFilter('Rename', array('overwrite' => false));
  129. $this->adapter->setOptions(array('ignoreNoFile' => true));
  130. $this->assertTrue($this->adapter->receive());
  131. }
  132. public function testMultiFiles()
  133. {
  134. $_FILES = array(
  135. 'txt' => array(
  136. 'name' => 'file.txt',
  137. 'type' => 'plain/text',
  138. 'size' => 8,
  139. 'tmp_name' => 'file.txt',
  140. 'error' => 0),
  141. 'exe' => array(
  142. 'name' => array(
  143. 0 => 'file1.txt',
  144. 1 => 'file2.txt'),
  145. 'type' => array(
  146. 0 => 'plain/text',
  147. 1 => 'plain/text'),
  148. 'size' => array(
  149. 0 => 8,
  150. 1 => 8),
  151. 'tmp_name' => array(
  152. 0 => 'file1.txt',
  153. 1 => 'file2.txt'),
  154. 'error' => array(
  155. 0 => 0,
  156. 1 => 0)));
  157. $adapter = new Zend_File_Transfer_Adapter_HttpTest_MockAdapter();
  158. $adapter->setOptions(array('ignoreNoFile' => true));
  159. $this->assertTrue($adapter->receive('exe'));
  160. $this->assertEquals(
  161. array('exe_0_' => 'file1.txt',
  162. 'exe_1_' => 'file2.txt'),
  163. $adapter->getFileName('exe', false));
  164. }
  165. public function testNoUploadInProgress()
  166. {
  167. if (!(ini_get('apc.enabled') && (bool) ini_get('apc.rfc1867') && is_callable('apc_fetch')) &&
  168. !is_callable('uploadprogress_get_info')) {
  169. $this->markTestSkipped('Whether APC nor UploadExtension available');
  170. return;
  171. }
  172. $status = Zend_File_Transfer_Adapter_HttpTest_MockAdapter::getProgress();
  173. $this->assertContains('No upload in progress', $status);
  174. }
  175. public function testUploadProgressFailure()
  176. {
  177. if (!(ini_get('apc.enabled') && (bool) ini_get('apc.rfc1867') && is_callable('apc_fetch')) &&
  178. !is_callable('uploadprogress_get_info')) {
  179. $this->markTestSkipped('Whether APC nor UploadExtension available');
  180. return;
  181. }
  182. $_GET['progress_key'] = 'mykey';
  183. $status = Zend_File_Transfer_Adapter_HttpTest_MockAdapter::getProgress();
  184. $this->assertEquals(array(
  185. 'total' => 100,
  186. 'current' => 100,
  187. 'rate' => 10,
  188. 'id' => 'mykey',
  189. 'done' => false,
  190. 'message' => '100B - 100B'), $status);
  191. $this->adapter->switchApcToUP();
  192. $status = Zend_File_Transfer_Adapter_HttpTest_MockAdapter::getProgress($status);
  193. $this->assertEquals(array(
  194. 'total' => 100,
  195. 'bytes_total' => 100,
  196. 'current' => 100,
  197. 'bytes_uploaded' => 100,
  198. 'rate' => 10,
  199. 'speed_average' => 10,
  200. 'cancel_upload' => true,
  201. 'message' => 'The upload has been canceled',
  202. 'done' => true,
  203. 'id' => 'mykey'), $status);
  204. }
  205. public function testUploadProgressAdapter()
  206. {
  207. if (!(ini_get('apc.enabled') && (bool) ini_get('apc.rfc1867') && is_callable('apc_fetch')) &&
  208. !is_callable('uploadprogress_get_info')) {
  209. $this->markTestSkipped('Whether APC nor UploadExtension available');
  210. return;
  211. }
  212. $_GET['progress_key'] = 'mykey';
  213. require_once 'Zend/ProgressBar/Adapter/Console.php';
  214. $adapter = new Zend_ProgressBar_Adapter_Console();
  215. $status = array('progress' => $adapter, 'session' => 'upload');
  216. $status = Zend_File_Transfer_Adapter_HttpTest_MockAdapter::getProgress($status);
  217. $this->assertTrue(array_key_exists('total', $status));
  218. $this->assertTrue(array_key_exists('current', $status));
  219. $this->assertTrue(array_key_exists('rate', $status));
  220. $this->assertTrue(array_key_exists('id', $status));
  221. $this->assertTrue(array_key_exists('message', $status));
  222. $this->assertTrue(array_key_exists('progress', $status));
  223. $this->assertTrue($status['progress'] instanceof Zend_ProgressBar);
  224. $this->adapter->switchApcToUP();
  225. $status = Zend_File_Transfer_Adapter_HttpTest_MockAdapter::getProgress($status);
  226. $this->assertTrue(array_key_exists('total', $status));
  227. $this->assertTrue(array_key_exists('current', $status));
  228. $this->assertTrue(array_key_exists('rate', $status));
  229. $this->assertTrue(array_key_exists('id', $status));
  230. $this->assertTrue(array_key_exists('message', $status));
  231. $this->assertTrue(array_key_exists('progress', $status));
  232. $this->assertTrue($status['progress'] instanceof Zend_ProgressBar);
  233. }
  234. public function testValidationOfPhpExtendsFormError()
  235. {
  236. $_SERVER['CONTENT_LENGTH'] = 10;
  237. $_FILES = array();
  238. $adapter = new Zend_File_Transfer_Adapter_HttpTest_MockAdapter();
  239. $this->assertFalse($adapter->isValidParent());
  240. $this->assertContains('exceeds the defined ini size', current($adapter->getMessages()));
  241. }
  242. }
  243. class Zend_File_Transfer_Adapter_HttpTest_MockAdapter extends Zend_File_Transfer_Adapter_Http
  244. {
  245. public function __construct()
  246. {
  247. self::$_callbackApc = array('Zend_File_Transfer_Adapter_HttpTest_MockAdapter', 'apcTest');
  248. parent::__construct();
  249. }
  250. public function isValid($files = null)
  251. {
  252. return true;
  253. }
  254. public function isValidParent($files = null)
  255. {
  256. return parent::isValid($files);
  257. }
  258. public static function isApcAvailable()
  259. {
  260. return true;
  261. }
  262. public static function apcTest($id)
  263. {
  264. return array('total' => 100, 'current' => 100, 'rate' => 10);
  265. }
  266. public static function uPTest($id)
  267. {
  268. return array('bytes_total' => 100, 'bytes_uploaded' => 100, 'speed_average' => 10, 'cancel_upload' => true);
  269. }
  270. public function switchApcToUP()
  271. {
  272. self::$_callbackApc = null;
  273. self::$_callbackUploadProgress = array('Zend_File_Transfer_Adapter_HttpTest_MockAdapter', 'uPTest');
  274. }
  275. }
  276. // Call Zend_File_Transfer_Adapter_HttpTest::main() if this source file is executed directly.
  277. if (PHPUnit_MAIN_METHOD == "Zend_File_Transfer_Adapter_HttpTest::main") {
  278. Zend_File_Transfer_Adapter_HttpTest::main();
  279. }