This repository was archived by the owner on May 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 810
Expand file tree
/
Copy pathHttpTest.php
More file actions
403 lines (360 loc) · 13.6 KB
/
Copy pathHttpTest.php
File metadata and controls
403 lines (360 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_File
* @subpackage UnitTests
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id$
*/
// Call Zend_File_Transfer_Adapter_HttpTest::main() if this source file is executed directly.
if (!defined("PHPUnit_MAIN_METHOD")) {
define("PHPUnit_MAIN_METHOD", "Zend_File_Transfer_Adapter_HttpTest::main");
}
require_once 'Zend/File/Transfer/Adapter/Http.php';
require_once 'Zend/Filter/BaseName.php';
require_once 'Zend/Filter/StringToLower.php';
require_once 'Zend/Loader/PluginLoader.php';
require_once 'Zend/Validate/File/Count.php';
require_once 'Zend/Validate/File/Extension.php';
require_once 'Zend/Validate/File/Upload.php';
/**
* Test class for Zend_File_Transfer_Adapter_Http
*
* @category Zend
* @package Zend_File
* @subpackage UnitTests
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @group Zend_File
*/
class Zend_File_Transfer_Adapter_HttpTest extends PHPUnit_Framework_TestCase
{
/**
* Runs the test methods of this class.
*
* @return void
*/
public static function main()
{
$suite = new PHPUnit_Framework_TestSuite("Zend_File_Transfer_Adapter_HttpTest");
$result = PHPUnit_TextUI_TestRunner::run($suite);
}
/**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
*
* @return void
*/
public function setUp()
{
$_FILES = array(
'txt' => array(
'name' => 'test.txt',
'type' => 'plain/text',
'size' => 8,
'tmp_name' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'php0zgByO',
'error' => 0
)
);
$this->adapter = new Zend_File_Transfer_Adapter_HttpTest_MockAdapter();
}
/**
* Tears down the fixture, for example, close a network connection.
* This method is called after a test is executed.
*
* @return void
*/
public function tearDown()
{
}
public function testEmptyAdapter()
{
$files = $this->adapter->getFileName();
$this->assertContains('php0zgByO_test.txt', $files);
}
public function testAutoSetUploadValidator()
{
$validators = array(
new Zend_Validate_File_Count(1),
new Zend_Validate_File_Extension('jpg'),
);
$this->adapter->setValidators($validators);
$test = $this->adapter->getValidator('Upload');
$this->assertTrue($test instanceof Zend_Validate_File_Upload);
}
/**
* @expectedException Zend_File_Transfer_Exception
*/
public function testSendingFiles()
{
$this->adapter->send();
}
/**
* @expectedException Zend_File_Transfer_Exception
*/
public function testFileIsSent()
{
$this->adapter->isSent();
}
public function testFileIsUploaded()
{
$this->assertTrue($this->adapter->isUploaded());
}
public function testFileIsNotUploaded()
{
$this->assertFalse($this->adapter->isUploaded('unknownFile'));
}
public function testFileIsNotFiltered()
{
$this->assertFalse($this->adapter->isFiltered('unknownFile'));
$this->assertFalse($this->adapter->isFiltered());
}
public function testFileIsNotReceived()
{
$this->assertFalse($this->adapter->isReceived('unknownFile'));
$this->assertFalse($this->adapter->isReceived());
}
public function testReceiveUnknownFile()
{
try {
$this->assertFalse($this->adapter->receive('unknownFile'));
} catch (Zend_File_Transfer_Exception $e) {
$this->assertContains('not find', $e->getMessage());
}
}
/**
* @group ZF-12451
*/
public function testReceiveEmptyArray()
{
$_SERVER['CONTENT_LENGTH'] = 10;
$_FILES = array();
$adapter = new Zend_File_Transfer_Adapter_Http();
$this->assertFalse($adapter->receive(array()));
}
public function testReceiveValidatedFile()
{
$_FILES = array(
'txt' => array(
'name' => 'unknown.txt',
'type' => 'plain/text',
'size' => 8,
'tmp_name' => 'unknown.txt',
'error' => 0));
$adapter = new Zend_File_Transfer_Adapter_HttpTest_MockAdapter();
$this->assertFalse($adapter->receive());
}
public function testReceiveIgnoredFile()
{
$this->adapter->setOptions(array('ignoreNoFile' => true));
$this->assertTrue($this->adapter->receive());
}
public function testReceiveWithRenameFilter()
{
$this->adapter->addFilter('Rename', array('target' => '/testdir'));
$this->adapter->setOptions(array('ignoreNoFile' => true));
$this->assertTrue($this->adapter->receive());
}
public function testReceiveWithRenameFilterButWithoutDirectory()
{
$this->adapter->setDestination(dirname(__FILE__));
$this->adapter->addFilter('Rename', array('overwrite' => false));
$this->adapter->setOptions(array('ignoreNoFile' => true));
$this->assertTrue($this->adapter->receive());
}
public function testMultiFiles()
{
$_FILES = array(
'txt' => array(
'name' => 'test.txt',
'type' => 'plain/text',
'size' => 8,
'tmp_name' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'php0zgByO',
'error' => 0
),
'exe' => array(
'name' => array(
0 => 'file1.exe',
1 => 'file2.exe'),
'type' => array(
0 => 'plain/text',
1 => 'plain/text'),
'size' => array(
0 => 8,
1 => 8),
'tmp_name' => array(
0 => dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'phpqBXGTg',
1 => dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'phpZqRDQF'),
'error' => array(
0 => 0,
1 => 0)));
$adapter = new Zend_File_Transfer_Adapter_HttpTest_MockAdapter();
$adapter->setOptions(array('ignoreNoFile' => true));
$this->assertTrue($adapter->receive('exe'));
$this->assertEquals(
array('exe_0_' => 'phpqBXGTg_file1.exe',
'exe_1_' => 'phpZqRDQF_file2.exe'),
$adapter->getFileName('exe', false));
}
public function testMultiFilesSameName()
{
$_FILES = array(
'txt' => array(
'name' => 'test.txt',
'type' => 'plain/text',
'size' => 8,
'tmp_name' => dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'php0zgByO',
'error' => 0
),
'exe' => array(
'name' => array(
0 => 'file.exe',
1 => 'file.exe'),
'type' => array(
0 => 'plain/text',
1 => 'plain/text'),
'size' => array(
0 => 8,
1 => 8),
'tmp_name' => array(
0 => dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'phpOOwDDc',
1 => dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'phpDlIxkx'),
'error' => array(
0 => 0,
1 => 0)));
$adapter = new Zend_File_Transfer_Adapter_HttpTest_MockAdapter();
$adapter->setOptions(array('ignoreNoFile' => true));
$this->assertTrue($adapter->receive('exe'));
$this->assertEquals(
array('exe_0_' => 'phpOOwDDc_file.exe',
'exe_1_' => 'phpDlIxkx_file.exe'),
$adapter->getFileName('exe', false));
}
public function testNoUploadInProgress()
{
if (!(ini_get('apc.enabled') && (bool) ini_get('apc.rfc1867') && is_callable('apc_fetch')) &&
!is_callable('uploadprogress_get_info')) {
$this->markTestSkipped('Whether APC nor UploadExtension available');
return;
}
$status = Zend_File_Transfer_Adapter_HttpTest_MockAdapter::getProgress();
$this->assertContains('No upload in progress', $status);
}
public function testUploadProgressFailure()
{
if (!(ini_get('apc.enabled') && (bool) ini_get('apc.rfc1867') && is_callable('apc_fetch')) &&
!is_callable('uploadprogress_get_info')) {
$this->markTestSkipped('Whether APC nor UploadExtension available');
return;
}
$_GET['progress_key'] = 'mykey';
$status = Zend_File_Transfer_Adapter_HttpTest_MockAdapter::getProgress();
$this->assertEquals(array(
'total' => 100,
'current' => 100,
'rate' => 10,
'id' => 'mykey',
'done' => false,
'message' => '100B - 100B'), $status);
$this->adapter->switchApcToUP();
$status = Zend_File_Transfer_Adapter_HttpTest_MockAdapter::getProgress($status);
$this->assertEquals(array(
'total' => 100,
'bytes_total' => 100,
'current' => 100,
'bytes_uploaded' => 100,
'rate' => 10,
'speed_average' => 10,
'cancel_upload' => true,
'message' => 'The upload has been canceled',
'done' => true,
'id' => 'mykey'), $status);
}
public function testUploadProgressAdapter()
{
if (!(ini_get('apc.enabled') && (bool) ini_get('apc.rfc1867') && is_callable('apc_fetch')) &&
!is_callable('uploadprogress_get_info')) {
$this->markTestSkipped('Whether APC nor UploadExtension available');
return;
}
$_GET['progress_key'] = 'mykey';
require_once 'Zend/ProgressBar/Adapter/Console.php';
$adapter = new Zend_ProgressBar_Adapter_Console();
$status = array('progress' => $adapter, 'session' => 'upload');
$status = Zend_File_Transfer_Adapter_HttpTest_MockAdapter::getProgress($status);
$this->assertTrue(array_key_exists('total', $status));
$this->assertTrue(array_key_exists('current', $status));
$this->assertTrue(array_key_exists('rate', $status));
$this->assertTrue(array_key_exists('id', $status));
$this->assertTrue(array_key_exists('message', $status));
$this->assertTrue(array_key_exists('progress', $status));
$this->assertTrue($status['progress'] instanceof Zend_ProgressBar);
$this->adapter->switchApcToUP();
$status = Zend_File_Transfer_Adapter_HttpTest_MockAdapter::getProgress($status);
$this->assertTrue(array_key_exists('total', $status));
$this->assertTrue(array_key_exists('current', $status));
$this->assertTrue(array_key_exists('rate', $status));
$this->assertTrue(array_key_exists('id', $status));
$this->assertTrue(array_key_exists('message', $status));
$this->assertTrue(array_key_exists('progress', $status));
$this->assertTrue($status['progress'] instanceof Zend_ProgressBar);
}
public function testValidationOfPhpExtendsFormError()
{
$_SERVER['CONTENT_LENGTH'] = 10;
$_FILES = array();
$adapter = new Zend_File_Transfer_Adapter_HttpTest_MockAdapter();
$this->assertFalse($adapter->isValidParent());
$this->assertContains('exceeds the defined ini size', current($adapter->getMessages()));
}
}
class Zend_File_Transfer_Adapter_HttpTest_MockAdapter extends Zend_File_Transfer_Adapter_Http
{
public function __construct()
{
self::$_callbackApc = array('Zend_File_Transfer_Adapter_HttpTest_MockAdapter', 'apcTest');
parent::__construct();
}
public function isValid($files = null)
{
return true;
}
public function isValidParent($files = null)
{
return parent::isValid($files);
}
public static function isApcAvailable()
{
return true;
}
public static function apcTest($id)
{
return array('total' => 100, 'current' => 100, 'rate' => 10);
}
public static function uPTest($id)
{
return array('bytes_total' => 100, 'bytes_uploaded' => 100, 'speed_average' => 10, 'cancel_upload' => true);
}
public function switchApcToUP()
{
self::$_callbackApc = null;
self::$_callbackUploadProgress = array('Zend_File_Transfer_Adapter_HttpTest_MockAdapter', 'uPTest');
}
}
// Call Zend_File_Transfer_Adapter_HttpTest::main() if this source file is executed directly.
if (PHPUnit_MAIN_METHOD == "Zend_File_Transfer_Adapter_HttpTest::main") {
Zend_File_Transfer_Adapter_HttpTest::main();
}