PageRenderTime 38ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/ZendFramework/tests/Zend/File/Transfer/Adapter/HttpTest.php

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