2
0

HttpTest.php 12 KB

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