mirror of
https://github.com/phpbb/phpbb.git
synced 2025-02-21 09:52:26 +01:00
[ticket/13904] Improve test coverage and use constants instead of magic numbers
PHPBB3-13904
This commit is contained in:
parent
a09c6d1fb7
commit
845233fc62
@ -210,7 +210,7 @@ class upload
|
||||
{
|
||||
switch ($errorcode)
|
||||
{
|
||||
case 1:
|
||||
case UPLOAD_ERR_INI_SIZE:
|
||||
$max_filesize = @ini_get('upload_max_filesize');
|
||||
$unit = 'MB';
|
||||
|
||||
@ -223,29 +223,37 @@ class upload
|
||||
}
|
||||
|
||||
$error = (empty($max_filesize)) ? $this->language->lang($this->error_prefix . 'PHP_SIZE_NA') : $this->language->lang($this->error_prefix . 'PHP_SIZE_OVERRUN', $max_filesize, $this->language->lang($unit));
|
||||
break;
|
||||
break;
|
||||
|
||||
case 2:
|
||||
case UPLOAD_ERR_FORM_SIZE:
|
||||
$max_filesize = get_formatted_filesize($this->max_filesize, false);
|
||||
|
||||
$error = $this->language->lang($this->error_prefix . 'WRONG_FILESIZE', $max_filesize['value'], $max_filesize['unit']);
|
||||
break;
|
||||
break;
|
||||
|
||||
case 3:
|
||||
case UPLOAD_ERR_PARTIAL:
|
||||
$error = $this->language->lang($this->error_prefix . 'PARTIAL_UPLOAD');
|
||||
break;
|
||||
break;
|
||||
|
||||
case 4:
|
||||
case UPLOAD_ERR_NO_FILE:
|
||||
$error = $this->language->lang($this->error_prefix . 'NOT_UPLOADED');
|
||||
break;
|
||||
break;
|
||||
|
||||
case 6:
|
||||
case UPLOAD_ERR_NO_TMP_DIR:
|
||||
$error = 'Temporary folder could not be found. Please check your PHP installation.';
|
||||
break;
|
||||
break;
|
||||
|
||||
case UPLOAD_ERR_CANT_WRITE:
|
||||
$error = 'Can’t write to temporary folder.';
|
||||
break;
|
||||
|
||||
case UPLOAD_ERR_EXTENSION:
|
||||
$error = 'A PHP extension has stopped the file upload.';
|
||||
break;
|
||||
|
||||
default:
|
||||
$error = false;
|
||||
break;
|
||||
break;
|
||||
}
|
||||
|
||||
return $error;
|
||||
|
@ -93,4 +93,27 @@ class phpbb_files_upload_test extends phpbb_test_case
|
||||
$upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path);
|
||||
$this->assertFalse($upload->is_valid('foobar'));
|
||||
}
|
||||
|
||||
public function data_internal_error()
|
||||
{
|
||||
return array(
|
||||
array(UPLOAD_ERR_INI_SIZE, 'PHP_SIZE_OVERRUN'),
|
||||
array(UPLOAD_ERR_FORM_SIZE, 'WRONG_FILESIZE'),
|
||||
array(UPLOAD_ERR_PARTIAL, 'PARTIAL_UPLOAD'),
|
||||
array(UPLOAD_ERR_NO_FILE, 'NOT_UPLOADED'),
|
||||
array(UPLOAD_ERR_NO_TMP_DIR, 'Temporary folder could not be found. Please check your PHP installation.'),
|
||||
array(UPLOAD_ERR_CANT_WRITE, 'Can’t write to temporary folder.'),
|
||||
array(UPLOAD_ERR_EXTENSION, 'A PHP extension has stopped the file upload.'),
|
||||
array(9, false),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider data_internal_error
|
||||
*/
|
||||
public function test_assign_internal_error($error_code, $expected)
|
||||
{
|
||||
$upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path);
|
||||
$this->assertSame($expected, $upload->assign_internal_error($error_code));
|
||||
}
|
||||
}
|
||||
|
@ -119,6 +119,25 @@ class phpbb_fileupload_test extends phpbb_test_case
|
||||
$this->assertEquals('DISALLOWED_EXTENSION', $file->error[0]);
|
||||
}
|
||||
|
||||
public function test_common_checks_disallowed_content()
|
||||
{
|
||||
$upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path);
|
||||
$upload->set_allowed_extensions(array('jpg'))
|
||||
->set_max_filesize(1000);
|
||||
$file = new \phpbb\files\filespec($this->filesystem, $this->language, $this->phpbb_root_path);
|
||||
$file->set_upload_ary(array(
|
||||
'size' => 50,
|
||||
'tmp_name' => dirname(__FILE__) . '/fixture/disallowed',
|
||||
'name' => 'disallowed.jpg',
|
||||
'type' => 'image/jpg'
|
||||
))
|
||||
->set_upload_namespace($upload);
|
||||
file_put_contents(dirname(__FILE__) . '/fixture/disallowed', '<body>' . file_get_contents(dirname(__FILE__) . '/fixture/jpg'));
|
||||
$upload->common_checks($file);
|
||||
$this->assertEquals('DISALLOWED_CONTENT', $file->error[0]);
|
||||
unlink(dirname(__FILE__) . '/fixture/disallowed');
|
||||
}
|
||||
|
||||
public function test_common_checks_invalid_filename()
|
||||
{
|
||||
$upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->request, $this->phpbb_root_path);
|
||||
|
Loading…
x
Reference in New Issue
Block a user