mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-28 14:18:52 +00:00
[ticket/10941] Unit tests for fileupload class added
PHPBB3-10941
This commit is contained in:
parent
fe4056c599
commit
dfef80f267
3 changed files with 132 additions and 0 deletions
32
tests/mock/filespec.php
Normal file
32
tests/mock/filespec.php
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @package testing
|
||||||
|
* @copyright (c) 2012 phpBB Group
|
||||||
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mock filespec class with some basic values to help with testing the
|
||||||
|
* fileupload class
|
||||||
|
*/
|
||||||
|
class phpbb_mock_filespec
|
||||||
|
{
|
||||||
|
public $filesize;
|
||||||
|
public $realname;
|
||||||
|
public $extension;
|
||||||
|
public $width;
|
||||||
|
public $height;
|
||||||
|
public $error = array();
|
||||||
|
|
||||||
|
public function check_content($disallowed_content)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function get($property)
|
||||||
|
{
|
||||||
|
return $this->$property;
|
||||||
|
}
|
||||||
|
}
|
0
tests/uploads/common.php
Normal file
0
tests/uploads/common.php
Normal file
100
tests/uploads/fileupload_test.php
Normal file
100
tests/uploads/fileupload_test.php
Normal file
|
@ -0,0 +1,100 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @package testing
|
||||||
|
* @copyright (c) 2012 phpBB Group
|
||||||
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
require_once __DIR__ . '/../../phpBB/includes/functions.php';
|
||||||
|
require_once __DIR__ . '/../../phpBB/includes/utf/utf_tools.php';
|
||||||
|
require_once __DIR__ . '/../../phpBB/includes/functions_upload.php';
|
||||||
|
require_once __DIR__ . '/../mock/filespec.php';
|
||||||
|
|
||||||
|
class phpbb_fileupload_test extends phpbb_test_case
|
||||||
|
{
|
||||||
|
private $path;
|
||||||
|
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
global $config, $user;
|
||||||
|
|
||||||
|
if (!is_array($config))
|
||||||
|
{
|
||||||
|
$config = array();
|
||||||
|
}
|
||||||
|
|
||||||
|
$config['rand_seed'] = '';
|
||||||
|
$config['rand_seed_last_update'] = time() + 600;
|
||||||
|
|
||||||
|
$user = new phpbb_mock_user();
|
||||||
|
$this->path = __DIR__ . '/fixture/';
|
||||||
|
}
|
||||||
|
|
||||||
|
private function gen_valid_filespec()
|
||||||
|
{
|
||||||
|
$filespec = new phpbb_mock_filespec();
|
||||||
|
$filespec->filesize = 1;
|
||||||
|
$filespec->extension = 'jpg';
|
||||||
|
$filespec->realname = 'valid';
|
||||||
|
$filespec->width = 2;
|
||||||
|
$filespec->height = 2;
|
||||||
|
|
||||||
|
return $filespec;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_common_checks()
|
||||||
|
{
|
||||||
|
// Test 1: Valid file
|
||||||
|
$upload = new fileupload('', array('jpg'), 1000);
|
||||||
|
$file = $this->gen_valid_filespec();
|
||||||
|
$upload->common_checks($file);
|
||||||
|
$this->assertEquals(0, sizeof($file->error));
|
||||||
|
|
||||||
|
// Test 2: File too large
|
||||||
|
$upload = new fileupload('', array('jpg'), 100);
|
||||||
|
$file = $this->gen_valid_filespec();
|
||||||
|
$file->filesize = 1000;
|
||||||
|
$upload->common_checks($file);
|
||||||
|
$this->assertEquals(1, sizeof($file->error));
|
||||||
|
|
||||||
|
// Test 3: Invalid filename
|
||||||
|
$upload = new fileupload('', array('jpg'), 100);
|
||||||
|
$file = $this->gen_valid_filespec();
|
||||||
|
$file->realname = 'invalid?';
|
||||||
|
$upload->common_checks($file);
|
||||||
|
$this->assertEquals(1, sizeof($file->error));
|
||||||
|
|
||||||
|
// Test 4: Invalid extension
|
||||||
|
$upload = new fileupload('', array('png'), 100);
|
||||||
|
$file = $this->gen_valid_filespec();
|
||||||
|
$upload->common_checks($file);
|
||||||
|
$this->assertEquals(1, sizeof($file->error));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_local_upload()
|
||||||
|
{
|
||||||
|
$upload = new fileupload('', array('jpg'), 1000);
|
||||||
|
|
||||||
|
copy($this->path . 'JPG', $this->path . 'JPG.jpg');
|
||||||
|
$file = $upload->local_upload($this->path . 'JPG.jpg');
|
||||||
|
$this->assertEquals(0, sizeof($file->error));
|
||||||
|
unlink($this->path . 'JPG.jpg');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_valid_dimensions()
|
||||||
|
{
|
||||||
|
$upload = new fileupload('', false, false, 1, 1, 100, 100);
|
||||||
|
|
||||||
|
$file1 = $this->gen_valid_filespec();
|
||||||
|
$file2 = $this->gen_valid_filespec();
|
||||||
|
$file2->height = 101;
|
||||||
|
$file3 = $this->gen_valid_filespec();
|
||||||
|
$file3->width = 0;
|
||||||
|
|
||||||
|
$this->assertTrue($upload->valid_dimensions($file1));
|
||||||
|
$this->assertFalse($upload->valid_dimensions($file2));
|
||||||
|
$this->assertFalse($upload->valid_dimensions($file3));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue