Skip to content

Commit

Permalink
2.x uploadedFile validation (backported from #4524)
Browse files Browse the repository at this point in the history
  • Loading branch information
vanquang9387 committed Oct 19, 2015
1 parent 8c404ad commit af8c992
Show file tree
Hide file tree
Showing 2 changed files with 1,086 additions and 933 deletions.
99 changes: 99 additions & 0 deletions lib/Cake/Test/Case/Utility/ValidationTest.php
Expand Up @@ -2454,6 +2454,12 @@ public function testUploadError() {
$this->assertFalse(Validation::uploadError(2));
$this->assertFalse(Validation::uploadError(array('error' => 2)));
$this->assertFalse(Validation::uploadError(array('error' => '2')));

$this->assertFalse(Validation::uploadError(UPLOAD_ERR_NO_FILE));
$this->assertFalse(Validation::uploadError(UPLOAD_ERR_FORM_SIZE, true));
$this->assertFalse(Validation::uploadError(UPLOAD_ERR_INI_SIZE, true));
$this->assertFalse(Validation::uploadError(UPLOAD_ERR_NO_TMP_DIR, true));
$this->assertTrue(Validation::uploadError(UPLOAD_ERR_NO_FILE, true));
}

/**
Expand All @@ -2474,4 +2480,97 @@ public function testFileSize() {
$this->assertFalse(Validation::fileSize(array('tmp_name' => $image), '>', '1KB'));
}

/**
* Test uploaded file validation.
*
* @return void
*/
public function testUploadedFileErrorCode() {
$this->assertFalse(Validation::uploadedFile('derp'));
$invalid = array(
'name' => 'testing'
);
$this->assertFalse(Validation::uploadedFile($invalid));
$file = array(
'name' => 'cake.power.gif',
'tmp_name' => CORE_PATH . 'Cake' . DS . 'Test' . DS . 'test_app' . DS . 'webroot/img/cake.power.gif',
'error' => UPLOAD_ERR_OK,
'type' => 'image/gif',
'size' => 201
);
$this->assertTrue(Validation::uploadedFile($file));
$file['error'] = UPLOAD_ERR_NO_FILE;
$this->assertFalse(Validation::uploadedFile($file), 'Error upload should fail.');
}

/**
* Test uploaded file validation.
*
* @return void
*/
public function testUploadedFileMimeType() {
$file = array(
'name' => 'cake.power.gif',
'tmp_name' => CORE_PATH . 'Cake' . DS . 'Test' . DS . 'test_app' . DS . 'webroot/img/cake.power.gif',
'error' => UPLOAD_ERR_OK,
'type' => 'text/plain',
'size' => 201
);
$options = array(
'types' => array('text/plain')
);
$this->assertFalse(Validation::uploadedFile($file, $options), 'Incorrect mimetype.');
$options = array(
'types' => array('image/gif', 'image/png')
);
$this->assertTrue(Validation::uploadedFile($file, $options));
}

/**
* Test uploaded file validation.
*
* @return void
*/
public function testUploadedFileSize() {
$file = array(
'name' => 'cake.power.gif',
'tmp_name' => CORE_PATH . 'Cake' . DS . 'Test' . DS . 'test_app' . DS . 'webroot/img/cake.power.gif',
'error' => UPLOAD_ERR_OK,
'type' => 'text/plain',
'size' => 201
);
$options = array(
'minSize' => 500
);
$this->assertFalse(Validation::uploadedFile($file, $options), 'Too small');
$options = array(
'maxSize' => 100
);
$this->assertFalse(Validation::uploadedFile($file, $options), 'Too big');
}

/**
* Test uploaded file validation.
*
* @return void
*/
public function testUploadedFileNoFile() {
$file = array(
'name' => '',
'tmp_name' => CORE_PATH . 'Cake' . DS . 'Test' . DS . 'test_app' . DS . 'webroot/img/cake.power.gif',
'error' => UPLOAD_ERR_NO_FILE,
'type' => '',
'size' => 0
);
$options = array(
'optional' => true,
'minSize' => 500,
'types' => array('image/gif', 'image/png')
);
$this->assertTrue(Validation::uploadedFile($file, $options), 'No file should be ok.');
$options = array(
'optional' => false
);
$this->assertFalse(Validation::uploadedFile($file, $options), 'File is required.');
}
}

0 comments on commit af8c992

Please sign in to comment.