2
0

HttpTest.php 11 KB

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