| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- <?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_Validate_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_Validate_File_UploadTest::main() if this source file is executed directly.
- if (!defined("PHPUnit_MAIN_METHOD")) {
- define("PHPUnit_MAIN_METHOD", "Zend_Validate_File_UploadTest::main");
- }
- /**
- * @see Zend_Validate_File_Upload
- */
- require_once 'Zend/Validate/File/Upload.php';
- /**
- * @category Zend
- * @package Zend_Validate_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_Validate
- */
- class Zend_Validate_File_UploadTest extends PHPUnit_Framework_TestCase
- {
- /**
- * Runs the test methods of this class.
- *
- * @return void
- */
- public static function main()
- {
- $suite = new PHPUnit_Framework_TestSuite("Zend_Validate_File_UploadTest");
- $result = PHPUnit_TextUI_TestRunner::run($suite);
- }
- /**
- * Ensures that the validator follows expected behavior
- *
- * @return void
- */
- public function testBasic()
- {
- $_FILES = array(
- 'test' => array(
- 'name' => 'test1',
- 'type' => 'text',
- 'size' => 200,
- 'tmp_name' => 'tmp_test1',
- 'error' => 0),
- 'test2' => array(
- 'name' => 'test2',
- 'type' => 'text2',
- 'size' => 202,
- 'tmp_name' => 'tmp_test2',
- 'error' => 1),
- 'test3' => array(
- 'name' => 'test3',
- 'type' => 'text3',
- 'size' => 203,
- 'tmp_name' => 'tmp_test3',
- 'error' => 2),
- 'test4' => array(
- 'name' => 'test4',
- 'type' => 'text4',
- 'size' => 204,
- 'tmp_name' => 'tmp_test4',
- 'error' => 3),
- 'test5' => array(
- 'name' => 'test5',
- 'type' => 'text5',
- 'size' => 205,
- 'tmp_name' => 'tmp_test5',
- 'error' => 4),
- 'test6' => array(
- 'name' => 'test6',
- 'type' => 'text6',
- 'size' => 206,
- 'tmp_name' => 'tmp_test6',
- 'error' => 5),
- 'test7' => array(
- 'name' => 'test7',
- 'type' => 'text7',
- 'size' => 207,
- 'tmp_name' => 'tmp_test7',
- 'error' => 6),
- 'test8' => array(
- 'name' => 'test8',
- 'type' => 'text8',
- 'size' => 208,
- 'tmp_name' => 'tmp_test8',
- 'error' => 7),
- 'test9' => array(
- 'name' => 'test9',
- 'type' => 'text9',
- 'size' => 209,
- 'tmp_name' => 'tmp_test9',
- 'error' => 8)
- );
- $validator = new Zend_Validate_File_Upload();
- $this->assertFalse($validator->isValid('test'));
- $this->assertTrue(array_key_exists('fileUploadErrorAttack', $validator->getMessages()));
- $validator = new Zend_Validate_File_Upload();
- $this->assertFalse($validator->isValid('test2'));
- $this->assertTrue(array_key_exists('fileUploadErrorIniSize', $validator->getMessages()));
- $validator = new Zend_Validate_File_Upload();
- $this->assertFalse($validator->isValid('test3'));
- $this->assertTrue(array_key_exists('fileUploadErrorFormSize', $validator->getMessages()));
- $validator = new Zend_Validate_File_Upload();
- $this->assertFalse($validator->isValid('test4'));
- $this->assertTrue(array_key_exists('fileUploadErrorPartial', $validator->getMessages()));
- $validator = new Zend_Validate_File_Upload();
- $this->assertFalse($validator->isValid('test5'));
- $this->assertTrue(array_key_exists('fileUploadErrorNoFile', $validator->getMessages()));
- $validator = new Zend_Validate_File_Upload();
- $this->assertFalse($validator->isValid('test6'));
- $this->assertTrue(array_key_exists('fileUploadErrorUnknown', $validator->getMessages()));
- $validator = new Zend_Validate_File_Upload();
- $this->assertFalse($validator->isValid('test7'));
- $this->assertTrue(array_key_exists('fileUploadErrorNoTmpDir', $validator->getMessages()));
- $validator = new Zend_Validate_File_Upload();
- $this->assertFalse($validator->isValid('test8'));
- $this->assertTrue(array_key_exists('fileUploadErrorCantWrite', $validator->getMessages()));
- $validator = new Zend_Validate_File_Upload();
- $this->assertFalse($validator->isValid('test9'));
- $this->assertTrue(array_key_exists('fileUploadErrorExtension', $validator->getMessages()));
- $validator = new Zend_Validate_File_Upload();
- $this->assertFalse($validator->isValid('test1'));
- $this->assertTrue(array_key_exists('fileUploadErrorAttack', $validator->getMessages()));
- $validator = new Zend_Validate_File_Upload();
- $this->assertFalse($validator->isValid('tmp_test1'));
- $this->assertTrue(array_key_exists('fileUploadErrorAttack', $validator->getMessages()));
- $validator = new Zend_Validate_File_Upload();
- $this->assertFalse($validator->isValid('test000'));
- $this->assertTrue(array_key_exists('fileUploadErrorFileNotFound', $validator->getMessages()));
- }
- /**
- * Ensures that getFiles() returns expected value
- *
- * @return void
- */
- public function testGetFiles()
- {
- $_FILES = array(
- 'test' => array(
- 'name' => 'test1',
- 'type' => 'text',
- 'size' => 200,
- 'tmp_name' => 'tmp_test1',
- 'error' => 0),
- 'test2' => array(
- 'name' => 'test3',
- 'type' => 'text2',
- 'size' => 202,
- 'tmp_name' => 'tmp_test2',
- 'error' => 1));
- $files = array(
- 'test' => array(
- 'name' => 'test1',
- 'type' => 'text',
- 'size' => 200,
- 'tmp_name' => 'tmp_test1',
- 'error' => 0));
- $files1 = array(
- 'test2' => array(
- 'name' => 'test3',
- 'type' => 'text2',
- 'size' => 202,
- 'tmp_name' => 'tmp_test2',
- 'error' => 1));
- $validator = new Zend_Validate_File_Upload();
- $this->assertEquals($files, $validator->getFiles('test'));
- $this->assertEquals($files, $validator->getFiles('test1'));
- $this->assertEquals($files1, $validator->getFiles('test3'));
- try {
- $this->assertEquals(array(), $validator->getFiles('test5'));
- $this->fail("Missing exception");
- } catch (Zend_Validate_Exception $e) {
- $this->assertContains("was not found", $e->getMessage());
- }
- }
- /**
- * Ensures that setFiles() returns expected value
- *
- * @return void
- */
- public function testSetFiles()
- {
- $files = array(
- 'test' => array(
- 'name' => 'test1',
- 'type' => 'text',
- 'size' => 200,
- 'tmp_name' => 'tmp_test1',
- 'error' => 0),
- 'test2' => array(
- 'name' => 'test2',
- 'type' => 'text2',
- 'size' => 202,
- 'tmp_name' => 'tmp_test2',
- 'error' => 1));
- $_FILES = array(
- 'test' => array(
- 'name' => 'test3',
- 'type' => 'text3',
- 'size' => 203,
- 'tmp_name' => 'tmp_test3',
- 'error' => 2));
- $validator = new Zend_Validate_File_Upload();
- $validator->setFiles(array());
- $this->assertEquals($_FILES, $validator->getFiles());
- $validator->setFiles();
- $this->assertEquals($_FILES, $validator->getFiles());
- $validator->setFiles($files);
- $this->assertEquals($files, $validator->getFiles());
- }
- /**
- * @group ZF-10738
- */
- public function testGetFilesReturnsEmptyArrayWhenFilesSuperglobalIsNull()
- {
- $_FILES = NULL;
- $validator = new Zend_Validate_File_Upload();
- $validator->setFiles();
- $this->assertEquals(array(), $validator->getFiles());
- }
- /**
- * @group ZF-10738
- */
- public function testGetFilesReturnsEmptyArrayAfterSetFilesIsCalledWithNull()
- {
- $validator = new Zend_Validate_File_Upload();
- $validator->setFiles(NULL);
- $this->assertEquals(array(), $validator->getFiles());
- }
- /**
- * @group ZF-12128
- */
- public function testErrorMessage()
- {
- $_FILES = array(
- 'foo' => array(
- 'name' => 'bar',
- 'type' => 'text',
- 'size' => 100,
- 'tmp_name' => 'tmp_bar',
- 'error' => 7,
- )
- );
- $validator = new Zend_Validate_File_Upload();
- $validator->isValid('foo');
- $this->assertEquals(
- array(
- 'fileUploadErrorCantWrite' => "File 'bar' can't be written",
- ),
- $validator->getMessages()
- );
- }
- }
- // Call Zend_Validate_File_UploadTest::main() if this source file is executed directly.
- if (PHPUnit_MAIN_METHOD == "Zend_Validate_File_UploadTest::main") {
- Zend_Validate_File_UploadTest::main();
- }
|