HttpTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 testMultiFiles()
  126. {
  127. $_FILES = array(
  128. 'txt' => array(
  129. 'name' => 'file.txt',
  130. 'type' => 'plain/text',
  131. 'size' => 8,
  132. 'tmp_name' => 'file.txt',
  133. 'error' => 0),
  134. 'exe' => array(
  135. 'name' => array(
  136. 0 => 'file1.txt',
  137. 1 => 'file2.txt'),
  138. 'type' => array(
  139. 0 => 'plain/text',
  140. 1 => 'plain/text'),
  141. 'size' => array(
  142. 0 => 8,
  143. 1 => 8),
  144. 'tmp_name' => array(
  145. 0 => 'file1.txt',
  146. 1 => 'file2.txt'),
  147. 'error' => array(
  148. 0 => 0,
  149. 1 => 0)));
  150. $adapter = new Zend_File_Transfer_Adapter_HttpTest_MockAdapter();
  151. $adapter->setOptions(array('ignoreNoFile' => true));
  152. $this->assertTrue($adapter->receive('exe'));
  153. $this->assertEquals(
  154. array('exe_0_' => 'file1.txt',
  155. 'exe_1_' => 'file2.txt'),
  156. $adapter->getFileName('exe', false));
  157. }
  158. public function testNoUploadInProgress()
  159. {
  160. if (!(ini_get('apc.enabled') && (bool) ini_get('apc.rfc1867') && is_callable('apc_fetch')) &&
  161. !is_callable('uploadprogress_get_info')) {
  162. $this->markTestSkipped('Whether APC nor UploadExtension available');
  163. return;
  164. }
  165. $status = Zend_File_Transfer_Adapter_HttpTest_MockAdapter::getProgress();
  166. $this->assertContains('No upload in progress', $status);
  167. }
  168. public function testUploadProgressFailure()
  169. {
  170. if (!(ini_get('apc.enabled') && (bool) ini_get('apc.rfc1867') && is_callable('apc_fetch')) &&
  171. !is_callable('uploadprogress_get_info')) {
  172. $this->markTestSkipped('Whether APC nor UploadExtension available');
  173. return;
  174. }
  175. $_GET['progress_key'] = 'mykey';
  176. $status = Zend_File_Transfer_Adapter_HttpTest_MockAdapter::getProgress();
  177. $this->assertEquals(array(
  178. 'total' => 100,
  179. 'current' => 100,
  180. 'rate' => 10,
  181. 'id' => 'mykey',
  182. 'done' => false,
  183. 'message' => '100B - 100B'), $status);
  184. $this->adapter->switchApcToUP();
  185. $status = Zend_File_Transfer_Adapter_HttpTest_MockAdapter::getProgress($status);
  186. $this->assertEquals(array(
  187. 'total' => 100,
  188. 'bytes_total' => 100,
  189. 'current' => 100,
  190. 'bytes_uploaded' => 100,
  191. 'rate' => 10,
  192. 'speed_average' => 10,
  193. 'cancel_upload' => true,
  194. 'message' => 'The upload has been canceled',
  195. 'done' => true,
  196. 'id' => 'mykey'), $status);
  197. }
  198. public function testUploadProgressAdapter()
  199. {
  200. if (!(ini_get('apc.enabled') && (bool) ini_get('apc.rfc1867') && is_callable('apc_fetch')) &&
  201. !is_callable('uploadprogress_get_info')) {
  202. $this->markTestSkipped('Whether APC nor UploadExtension available');
  203. return;
  204. }
  205. $_GET['progress_key'] = 'mykey';
  206. require_once 'Zend/ProgressBar/Adapter/Console.php';
  207. $adapter = new Zend_ProgressBar_Adapter_Console();
  208. $status = array('progress' => $adapter, 'session' => 'upload');
  209. $status = Zend_File_Transfer_Adapter_HttpTest_MockAdapter::getProgress($status);
  210. $this->assertTrue(array_key_exists('total', $status));
  211. $this->assertTrue(array_key_exists('current', $status));
  212. $this->assertTrue(array_key_exists('rate', $status));
  213. $this->assertTrue(array_key_exists('id', $status));
  214. $this->assertTrue(array_key_exists('message', $status));
  215. $this->assertTrue(array_key_exists('progress', $status));
  216. $this->assertTrue($status['progress'] instanceof Zend_ProgressBar);
  217. $this->adapter->switchApcToUP();
  218. $status = Zend_File_Transfer_Adapter_HttpTest_MockAdapter::getProgress($status);
  219. $this->assertTrue(array_key_exists('total', $status));
  220. $this->assertTrue(array_key_exists('current', $status));
  221. $this->assertTrue(array_key_exists('rate', $status));
  222. $this->assertTrue(array_key_exists('id', $status));
  223. $this->assertTrue(array_key_exists('message', $status));
  224. $this->assertTrue(array_key_exists('progress', $status));
  225. $this->assertTrue($status['progress'] instanceof Zend_ProgressBar);
  226. }
  227. public function testValidationOfPhpExtendsFormError()
  228. {
  229. $_SERVER['CONTENT_LENGTH'] = 10;
  230. $_FILES = array();
  231. $adapter = new Zend_File_Transfer_Adapter_HttpTest_MockAdapter();
  232. $this->assertFalse($adapter->isValidParent());
  233. $this->assertContains('exceeds the defined ini size', current($adapter->getMessages()));
  234. }
  235. }
  236. class Zend_File_Transfer_Adapter_HttpTest_MockAdapter extends Zend_File_Transfer_Adapter_Http
  237. {
  238. public function __construct()
  239. {
  240. self::$_callbackApc = array('Zend_File_Transfer_Adapter_HttpTest_MockAdapter', 'apcTest');
  241. parent::__construct();
  242. }
  243. public function isValid($files = null)
  244. {
  245. return true;
  246. }
  247. public function isValidParent($files = null)
  248. {
  249. return parent::isValid($files);
  250. }
  251. public static function isApcAvailable()
  252. {
  253. return true;
  254. }
  255. public static function apcTest($id)
  256. {
  257. return array('total' => 100, 'current' => 100, 'rate' => 10);
  258. }
  259. public static function uPTest($id)
  260. {
  261. return array('bytes_total' => 100, 'bytes_uploaded' => 100, 'speed_average' => 10, 'cancel_upload' => true);
  262. }
  263. public function switchApcToUP()
  264. {
  265. self::$_callbackApc = null;
  266. self::$_callbackUploadProgress = array('Zend_File_Transfer_Adapter_HttpTest_MockAdapter', 'uPTest');
  267. }
  268. }
  269. // Call Zend_File_Transfer_Adapter_HttpTest::main() if this source file is executed directly.
  270. if (PHPUnit_MAIN_METHOD == "Zend_File_Transfer_Adapter_HttpTest::main") {
  271. Zend_File_Transfer_Adapter_HttpTest::main();
  272. }