diff options
Diffstat (limited to 'tests/upload')
| -rw-r--r-- | tests/upload/filespec_test.php | 277 | ||||
| -rw-r--r-- | tests/upload/fileupload_test.php | 138 | ||||
| -rw-r--r-- | tests/upload/fixture/bmp | bin | 0 -> 64 bytes | |||
| -rw-r--r-- | tests/upload/fixture/iff | bin | 0 -> 120 bytes | |||
| -rw-r--r-- | tests/upload/fixture/iff_maya | bin | 0 -> 88 bytes | |||
| -rw-r--r-- | tests/upload/fixture/jp2 | bin | 0 -> 528 bytes | |||
| -rw-r--r-- | tests/upload/fixture/jpx | bin | 0 -> 528 bytes | |||
| -rw-r--r-- | tests/upload/fixture/psd | bin | 0 -> 6374 bytes | |||
| -rw-r--r-- | tests/upload/fixture/tif_compressed | bin | 0 -> 236 bytes | |||
| -rw-r--r-- | tests/upload/fixture/tif_msb | bin | 0 -> 222 bytes | |||
| -rw-r--r-- | tests/upload/fixture/wbmp | bin | 0 -> 5 bytes | |||
| -rw-r--r-- | tests/upload/imagesize_test.php | 97 | 
12 files changed, 459 insertions, 53 deletions
| diff --git a/tests/upload/filespec_test.php b/tests/upload/filespec_test.php index ed28bcb38f..18b6deed1f 100644 --- a/tests/upload/filespec_test.php +++ b/tests/upload/filespec_test.php @@ -11,10 +11,6 @@  *  */ -require_once __DIR__ . '/../../phpBB/includes/functions.php'; -require_once __DIR__ . '/../../phpBB/includes/utf/utf_tools.php'; -require_once __DIR__ . '/../../phpBB/includes/functions_upload.php'; -  class phpbb_filespec_test extends phpbb_test_case  {  	const TEST_COUNT = 100; @@ -23,14 +19,19 @@ class phpbb_filespec_test extends phpbb_test_case  	const UPLOAD_MAX_FILESIZE = 1000;  	private $config; +	private $filesystem;  	public $path; +	/** @var \phpbb\language\language */ +	protected $language; + +	/** @var string phpBB root path */ +	protected $phpbb_root_path; +  	protected function setUp()  	{  		// Global $config required by unique_id -		// Global $user required by filespec::additional_checks and -		// filespec::move_file -		global $config, $user; +		global $config, $phpbb_root_path, $phpEx;  		if (!is_array($config))  		{ @@ -44,9 +45,6 @@ class phpbb_filespec_test extends phpbb_test_case  		// See: phpBB/install/schemas/schema_data.sql  		$config['mime_triggers'] = 'body|head|html|img|plaintext|a href|pre|script|table|title'; -		$user = new phpbb_mock_user(); -		$user->lang = new phpbb_mock_lang(); -  		$this->config = &$config;  		$this->path = __DIR__ . '/fixture/'; @@ -75,6 +73,17 @@ class phpbb_filespec_test extends phpbb_test_case  		$guessers[2]->set_priority(-2);  		$guessers[3]->set_priority(-2);  		$this->mimetype_guesser = new \phpbb\mimetype\guesser($guessers); +		$this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); + +		$this->filesystem = new \phpbb\filesystem\filesystem(); +		$this->phpbb_root_path = $phpbb_root_path; +	} + +	private function set_reflection_property(&$class, $property_name, $value) +	{ +		$property = new ReflectionProperty($class, $property_name); +		$property->setAccessible(true); +		$property->setValue($class, $value);  	}  	private function get_filespec($override = array()) @@ -88,14 +97,13 @@ class phpbb_filespec_test extends phpbb_test_case  			'error' => '',  		); -		return new filespec(array_merge($upload_ary, $override), null, $this->mimetype_guesser); +		$filespec = new \phpbb\files\filespec($this->filesystem, $this->language, new \bantu\IniGetWrapper\IniGetWrapper, new \FastImageSize\FastImageSize(), $this->phpbb_root_path, $this->mimetype_guesser); +		return $filespec->set_upload_ary(array_merge($upload_ary, $override));  	}  	protected function tearDown()  	{ -		global $user;  		$this->config = array(); -		$user = null;  		$iterator = new DirectoryIterator($this->path . 'copies');  		foreach ($iterator as $fileinfo) @@ -108,6 +116,13 @@ class phpbb_filespec_test extends phpbb_test_case  		}  	} +	public function test_empty_upload_ary() +	{ +		$filespec = new \phpbb\files\filespec($this->filesystem, $this->language, new \bantu\IniGetWrapper\IniGetWrapper, new \FastImageSize\FastImageSize(), $this->phpbb_root_path, $this->mimetype_guesser); +		$this->assertInstanceOf('\phpbb\files\filespec', $filespec->set_upload_ary(array())); +		$this->assertTrue($filespec->init_error()); +	} +  	public function additional_checks_variables()  	{  		// False here just indicates the file is too large and fails the @@ -129,13 +144,26 @@ class phpbb_filespec_test extends phpbb_test_case  	{  		$upload = new phpbb_mock_fileupload();  		$filespec = $this->get_filespec(); -		$filespec->upload = $upload; -		$filespec->file_moved = true; -		$filespec->filesize = $filespec->get_filesize($this->path . $filename); +		$filespec->set_upload_namespace($upload); +		$this->set_reflection_property($filespec, 'file_moved', true); +		$this->set_reflection_property($filespec, 'filesize', $filespec->get_filesize($this->path . $filename));  		$this->assertEquals($expected, $filespec->additional_checks());  	} +	public function test_additional_checks_dimensions() +	{ +		$upload = new phpbb_mock_fileupload(); +		$filespec = $this->get_filespec(); +		$filespec->set_upload_namespace($upload); +		$upload->valid_dimensions = false; +		$this->set_reflection_property($filespec, 'file_moved', true); +		$upload->max_filesize = 0; + +		$this->assertEquals(false, $filespec->additional_checks()); +		$this->assertSame(array('WRONG_SIZE'), $filespec->error); +	} +  	public function check_content_variables()  	{  		// False here indicates that a file is non-binary and contains @@ -170,6 +198,7 @@ class phpbb_filespec_test extends phpbb_test_case  			array($chunks[2] . $chunks[9]),  			array($chunks[3] . $chunks[4]),  			array($chunks[5] . $chunks[6]), +			array('foobar.png'),  		);  	} @@ -181,7 +210,7 @@ class phpbb_filespec_test extends phpbb_test_case  		$bad_chars = array("'", "\\", ' ', '/', ':', '*', '?', '"', '<', '>', '|');  		$filespec = $this->get_filespec(array('name' => $filename));  		$filespec->clean_filename('real', self::PREFIX); -		$name = $filespec->realname; +		$name = $filespec->get('realname');  		$this->assertEquals(0, preg_match('/%(\w{2})/', $name));  		foreach ($bad_chars as $char) @@ -197,8 +226,8 @@ class phpbb_filespec_test extends phpbb_test_case  		{  			$filespec = $this->get_filespec();  			$filespec->clean_filename('unique', self::PREFIX); -			$name = $filespec->realname; -			 +			$name = $filespec->get('realname'); +  			$this->assertEquals(strlen($name), 32 + strlen(self::PREFIX));  			$this->assertRegExp('#^[A-Za-z0-9]+$#', substr($name, strlen(self::PREFIX)));  			$this->assertFalse(isset($filenames[$name])); @@ -206,6 +235,55 @@ class phpbb_filespec_test extends phpbb_test_case  		}  	} +	public function test_clean_filename_unique_ext() +	{ +		$filenames = array(); +		for ($tests = 0; $tests < self::TEST_COUNT; $tests++) +		{ +			$filespec = $this->get_filespec(array('name' => 'foobar.jpg')); +			$filespec->clean_filename('unique_ext', self::PREFIX); +			$name = $filespec->get('realname'); + +			$this->assertEquals(strlen($name), 32 + strlen(self::PREFIX) + strlen('.jpg')); +			$this->assertRegExp('#^[A-Za-z0-9]+\.jpg$#', substr($name, strlen(self::PREFIX))); +			$this->assertFalse(isset($filenames[$name])); +			$filenames[$name] = true; +		} +	} + +	public function data_clean_filename_avatar() +	{ +		return array( +			array(false, false, ''), +			array('foobar.png', 'u5.png', 'avatar', 'u', 5), +			array('foobar.png', 'g9.png', 'avatar', 'g', 9), + +		); +	} + +	/** +	 * @dataProvider data_clean_filename_avatar +	 */ +	public function test_clean_filename_avatar($filename, $expected, $mode, $prefix = '', $user_id = '') +	{ +		$filespec = new \phpbb\files\filespec($this->filesystem, $this->language, new \bantu\IniGetWrapper\IniGetWrapper, new \FastImageSize\FastImageSize(), $this->phpbb_root_path, $this->mimetype_guesser); + +		if ($filename) +		{ +			$upload_ary = array( +				'name' => $filename, +				'type' => '', +				'size' => '', +				'tmp_name' => '', +				'error' => '', +			); +			$filespec->set_upload_ary($upload_ary); +		} +		$filespec->clean_filename($mode, $prefix, $user_id); + +		$this->assertSame($expected, $filespec->get('realname')); +	} +  	public function get_extension_variables()  	{  		return array( @@ -223,7 +301,7 @@ class phpbb_filespec_test extends phpbb_test_case  	 */  	public function test_get_extension($filename, $expected)  	{ -		$this->assertEquals($expected, filespec::get_extension($filename)); +		$this->assertEquals($expected, \phpbb\files\filespec::get_extension($filename));  	}  	public function is_image_variables() @@ -286,7 +364,7 @@ class phpbb_filespec_test extends phpbb_test_case  			array('txt_copy', 'txt_as_img', 'image/jpg', 'txt', false, true),  			array('txt_copy_2', 'txt_moved', 'text/plain', 'txt', false, true),  			array('jpg_copy', 'jpg_moved', 'image/png', 'jpg', false, true), -			array('png_copy', 'png_moved', 'image/png', 'jpg', 'IMAGE_FILETYPE_MISMATCH', true), +			array('png_copy', 'png_moved', 'image/png', 'jpg', 'Image file type mismatch: expected extension png but extension jpg given.', true),  		);  	} @@ -297,8 +375,7 @@ class phpbb_filespec_test extends phpbb_test_case  	{  		// Global $phpbb_root_path and $phpEx are required by phpbb_chmod  		global $phpbb_root_path, $phpEx; -		$phpbb_root_path = ''; -		$phpEx = 'php'; +		$this->phpbb_root_path = '';  		$upload = new phpbb_mock_fileupload();  		$upload->max_filesize = self::UPLOAD_MAX_FILESIZE; @@ -308,18 +385,137 @@ class phpbb_filespec_test extends phpbb_test_case  			'name' => $realname,  			'type' => $mime_type,  		)); -		$filespec->extension = $extension; -		$filespec->upload = $upload; -		$filespec->local = true; +		$this->set_reflection_property($filespec, 'extension', $extension); +		$filespec->set_upload_namespace($upload); +		$this->set_reflection_property($filespec, 'local', true);  		$this->assertEquals($expected, $filespec->move_file($this->path . 'copies')); -		$this->assertEquals($filespec->file_moved, file_exists($this->path . 'copies/' . $realname)); +		$this->assertEquals($filespec->get('file_moved'), file_exists($this->path . 'copies/' . $realname));  		if ($error)  		{  			$this->assertEquals($error, $filespec->error[0]);  		} -		$phpEx = ''; +		$this->phpbb_root_path = $phpbb_root_path; +	} + +	public function test_move_file_error() +	{ +		$filespec = $this->get_filespec(); +		$this->assertFalse($filespec->move_file('foobar')); +		$filespec->error[] = 'foo'; +		$this->assertFalse($filespec->move_file('foo')); +	} + +	public function data_move_file_copy() +	{ +		return array( +			array('gif_copy', true, false, array()), +			array('gif_copy', true, true, array()), +			array('foo_bar', false, false, array('GENERAL_UPLOAD_ERROR')), +			array('foo_bar', false, true, array('GENERAL_UPLOAD_ERROR')), +		); +	} + +	/** +	 * @dataProvider data_move_file_copy +	 */ +	public function test_move_file_copy($tmp_name, $move_success, $safe_mode_on, $expected_error) +	{ +		// Initialise a blank filespec object for use with trivial methods +		$upload_ary = array( +			'name' => 'gif_moved', +			'type' => 'image/gif', +			'size' => '', +			'tmp_name' => $this->path . 'copies/' . $tmp_name, +			'error' => '', +		); + +		$php_ini = $this->getMockBuilder('\bantu\IniGetWrapper\IniGetWrapper') +			->getMock(); +		$php_ini->expects($this->any()) +			->method('getBool') +			->with($this->anything()) +			->willReturn($safe_mode_on); +		$upload = new phpbb_mock_fileupload(); +		$upload->max_filesize = self::UPLOAD_MAX_FILESIZE; +		$filespec = new \phpbb\files\filespec($this->filesystem, $this->language, $php_ini, new \FastImageSize\FastImagesize,  '', $this->mimetype_guesser); +		$filespec->set_upload_ary($upload_ary); +		$this->set_reflection_property($filespec, 'local', false); +		$this->set_reflection_property($filespec, 'extension', 'gif'); +		$filespec->set_upload_namespace($upload); + +		$this->assertEquals($move_success, $filespec->move_file($this->path . 'copies')); +		$this->assertEquals($filespec->get('file_moved'), file_exists($this->path . 'copies/gif_moved')); +		$this->assertSame($expected_error, $filespec->error); +	} + +	public function data_move_file_imagesize() +	{ +		return array( +			array( +				array( +					'width'		=> 2, +					'height'	=> 2, +					'type'		=> IMAGETYPE_GIF, +				), +				array() +			), +			array( +				array( +					'width'		=> 2, +					'height'	=> 2, +					'type'		=> -1, +				), +				array('Image file type -1 for mimetype image/gif not supported.') +			), +			array( +				array( +					'width'		=> 0, +					'height'	=> 0, +					'type'		=> IMAGETYPE_GIF, +				), +				array('The image file you tried to attach is invalid.') +			), +			array( +				false, +				array('It was not possible to determine the dimensions of the image. Please verify that the URL you entered is correct.') +			) +		); +	} + +	/** +	 * @dataProvider data_move_file_imagesize +	 */ +	public function test_move_file_imagesize($imagesize_return, $expected_error) +	{ +		// Initialise a blank filespec object for use with trivial methods +		$upload_ary = array( +			'name' => 'gif_moved', +			'type' => 'image/gif', +			'size' => '', +			'tmp_name' => $this->path . 'copies/gif_copy', +			'error' => '', +		); + +		$imagesize = $this->getMockBuilder('\FastImageSize\FastImageSize') +			->getMock(); +		$imagesize->expects($this->any()) +			->method('getImageSize') +			->with($this->anything()) +			->willReturn($imagesize_return); + +		$upload = new phpbb_mock_fileupload(); +		$upload->max_filesize = self::UPLOAD_MAX_FILESIZE; +		$filespec = new \phpbb\files\filespec($this->filesystem, $this->language, new \bantu\IniGetWrapper\IniGetWrapper, $imagesize,  '', $this->mimetype_guesser); +		$filespec->set_upload_ary($upload_ary); +		$this->set_reflection_property($filespec, 'local', false); +		$this->set_reflection_property($filespec, 'extension', 'gif'); +		$filespec->set_upload_namespace($upload); + +		$this->assertEquals(true, $filespec->move_file($this->path . 'copies')); +		$this->assertEquals($filespec->get('file_moved'), file_exists($this->path . 'copies/gif_moved')); +		$this->assertSame($expected_error, $filespec->error);  	}  	/** @@ -333,6 +529,29 @@ class phpbb_filespec_test extends phpbb_test_case  		$type_cast_helper->set_var($upload_name, $filename, 'string', true, true);  		$filespec = $this->get_filespec(array('name'=> $upload_name)); -		$this->assertSame(trim(utf8_basename(htmlspecialchars($filename))), $filespec->uploadname); +		$this->assertSame(trim(utf8_basename(htmlspecialchars($filename))), $filespec->get('uploadname')); +	} + +	public function test_is_uploaded() +	{ +		$filespec = new \phpbb\files\filespec($this->filesystem, $this->language, new \bantu\IniGetWrapper\IniGetWrapper, new \FastImageSize\FastImageSize(), $this->phpbb_root_path, null); +		$reflection_filespec = new ReflectionClass($filespec); +		$plupload_property = $reflection_filespec->getProperty('plupload'); +		$plupload_property->setAccessible(true); +		$plupload_mock = $this->getMockBuilder('\phpbb\plupload\plupload') +			->disableOriginalConstructor() +			->getMock(); +		$plupload_mock->expects($this->any()) +			->method('is_active') +			->will($this->returnValue(true)); +		$plupload_property->setValue($filespec, $plupload_mock); +		$is_uploaded = $reflection_filespec->getMethod('is_uploaded'); + +		// Plupload is active and file does not exist +		$this->assertFalse($is_uploaded->invoke($filespec)); + +		// Plupload is not active and file was not uploaded +		$plupload_property->setValue($filespec, null); +		$this->assertFalse($is_uploaded->invoke($filespec));  	}  } diff --git a/tests/upload/fileupload_test.php b/tests/upload/fileupload_test.php index fcfb84125d..fb72823f08 100644 --- a/tests/upload/fileupload_test.php +++ b/tests/upload/fileupload_test.php @@ -11,36 +11,88 @@  *  */ -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; +	private $filesystem; + +	/** @var \Symfony\Component\DependencyInjection\ContainerInterface */ +	protected $container; + +	/** @var \phpbb\files\factory */ +	protected $factory; + +	/** @var \bantu\IniGetWrapper\IniGetWrapper */ +	protected $php_ini; + +	/** @var \phpbb\language\language */ +	protected $language; + +	/** @var \phpbb\request\request_interface */ +	protected $request; + +	/** @var string phpBB root path */ +	protected $phpbb_root_path; +  	protected function setUp()  	{  		// Global $config required by unique_id -		// Global $user required by several functions dealing with translations -		// Global $request required by form_upload, local_upload and is_valid -		global $config, $user, $request; +		global $config, $phpbb_root_path, $phpEx;  		if (!is_array($config))  		{ -			$config = array(); +			$config = new \phpbb\config\config(array());  		}  		$config['rand_seed'] = '';  		$config['rand_seed_last_update'] = time() + 600; -		$user = new phpbb_mock_user(); -		$user->lang = new phpbb_mock_lang(); - -		$request = new phpbb_mock_request(); +		$this->request = $this->getMock('\phpbb\request\request'); +		$this->php_ini = new \bantu\IniGetWrapper\IniGetWrapper; + +		$this->filesystem = new \phpbb\filesystem\filesystem(); +		$this->language = new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)); +		$guessers = array( +			new \Symfony\Component\HttpFoundation\File\MimeType\FileinfoMimeTypeGuesser(), +			new \Symfony\Component\HttpFoundation\File\MimeType\FileBinaryMimeTypeGuesser(), +			new \phpbb\mimetype\content_guesser(), +			new \phpbb\mimetype\extension_guesser(), +		); +		$guessers[2]->set_priority(-2); +		$guessers[3]->set_priority(-2); +		$this->mimetype_guesser = new \phpbb\mimetype\guesser($guessers); + +		$this->container = new phpbb_mock_container_builder($phpbb_root_path, $phpEx); +		$this->container->set('files.filespec', new \phpbb\files\filespec( +			$this->filesystem, +			$this->language, +			$this->php_ini, +			new \FastImageSize\FastImageSize(), +			$phpbb_root_path, +			new \phpbb\mimetype\guesser(array( +				'mimetype.extension_guesser' => new \phpbb\mimetype\extension_guesser(), +			)))); +		$this->factory = new \phpbb\files\factory($this->container); +		$plupload = new \phpbb\plupload\plupload($phpbb_root_path, $config, $this->request, new \phpbb\user($this->language, '\phpbb\datetime'), $this->php_ini, $this->mimetype_guesser); +		$this->container->set('files.types.form', new \phpbb\files\types\form( +			$this->factory, +			$this->language, +			$this->php_ini, +			$plupload, +			$this->request +		), phpbb_mock_container_builder::SCOPE_PROTOTYPE); +		$this->container->set('files.types.local', new \phpbb\files\types\local( +			$this->factory, +			$this->language, +			$this->php_ini, +			$this->request +		), phpbb_mock_container_builder::SCOPE_PROTOTYPE);  		$this->path = __DIR__ . '/fixture/'; +		$this->phpbb_root_path = $phpbb_root_path;  	}  	private function gen_valid_filespec() @@ -65,15 +117,38 @@ class phpbb_fileupload_test extends phpbb_test_case  	public function test_common_checks_invalid_extension()  	{ -		$upload = new fileupload('', array('png'), 100); +		$upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); +		$upload->set_allowed_extensions(array('png')) +			->set_max_filesize(100);  		$file = $this->gen_valid_filespec();  		$upload->common_checks($file);  		$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->php_ini, $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->php_ini, new \FastImageSize\FastImageSize(), $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 fileupload('', array('jpg'), 100); +		$upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); +		$upload->set_allowed_extensions(array('jpg')) +			->set_max_filesize(100);  		$file = $this->gen_valid_filespec();  		$file->realname = 'invalid?';  		$upload->common_checks($file); @@ -82,7 +157,9 @@ class phpbb_fileupload_test extends phpbb_test_case  	public function test_common_checks_too_large()  	{ -		$upload = new fileupload('', array('jpg'), 100); +		$upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); +		$upload->set_allowed_extensions(array('jpg')) +			->set_max_filesize(100);  		$file = $this->gen_valid_filespec();  		$file->filesize = 1000;  		$upload->common_checks($file); @@ -91,7 +168,9 @@ class phpbb_fileupload_test extends phpbb_test_case  	public function test_common_checks_valid_file()  	{ -		$upload = new fileupload('', array('jpg'), 1000); +		$upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); +		$upload->set_allowed_extensions(array('jpg')) +			->set_max_filesize(1000);  		$file = $this->gen_valid_filespec();  		$upload->common_checks($file);  		$this->assertEquals(0, sizeof($file->error)); @@ -99,33 +178,41 @@ class phpbb_fileupload_test extends phpbb_test_case  	public function test_local_upload()  	{ -		$upload = new fileupload('', array('jpg'), 1000); +		$upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); +		$upload->set_allowed_extensions(array('jpg')) +			->set_max_filesize(1000);  		copy($this->path . 'jpg', $this->path . 'jpg.jpg'); -		$file = $upload->local_upload($this->path . 'jpg.jpg'); +		$file = $upload->handle_upload('files.types.local', $this->path . 'jpg.jpg');  		$this->assertEquals(0, sizeof($file->error)); -		unlink($this->path . 'jpg.jpg'); +		$this->assertFalse($file->additional_checks()); +		$this->assertTrue($file->move_file('../tests/upload/fixture/copies', true)); +		$file->remove();  	}  	public function test_move_existent_file()  	{ -		$upload = new fileupload('', array('jpg'), 1000); +		$upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); +		$upload->set_allowed_extensions(array('jpg')) +			->set_max_filesize(1000);  		copy($this->path . 'jpg', $this->path . 'jpg.jpg'); -		$file = $upload->local_upload($this->path . 'jpg.jpg'); +		$file = $upload->handle_upload('files.types.local', $this->path . 'jpg.jpg');  		$this->assertEquals(0, sizeof($file->error));  		$this->assertFalse($file->move_file('../tests/upload/fixture')); -		$this->assertFalse($file->file_moved); +		$this->assertFalse($file->get('file_moved'));  		$this->assertEquals(1, sizeof($file->error));  	}  	public function test_move_existent_file_overwrite()  	{ -		$upload = new fileupload('', array('jpg'), 1000); +		$upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); +		$upload->set_allowed_extensions(array('jpg')) +			->set_max_filesize(1000);  		copy($this->path . 'jpg', $this->path . 'jpg.jpg');  		copy($this->path . 'jpg', $this->path . 'copies/jpg.jpg'); -		$file = $upload->local_upload($this->path . 'jpg.jpg'); +		$file = $upload->handle_upload('files.types.local', $this->path . 'jpg.jpg');  		$this->assertEquals(0, sizeof($file->error));  		$file->move_file('../tests/upload/fixture/copies', true);  		$this->assertEquals(0, sizeof($file->error)); @@ -134,7 +221,10 @@ class phpbb_fileupload_test extends phpbb_test_case  	public function test_valid_dimensions()  	{ -		$upload = new fileupload('', false, false, 1, 1, 100, 100); +		$upload = new \phpbb\files\upload($this->filesystem, $this->factory, $this->language, $this->php_ini, $this->request, $this->phpbb_root_path); +		$upload->set_allowed_extensions(false) +			->set_max_filesize(false) +			->set_allowed_dimensions(1, 1, 100, 100);  		$file1 = $this->gen_valid_filespec();  		$file2 = $this->gen_valid_filespec(); diff --git a/tests/upload/fixture/bmp b/tests/upload/fixture/bmpBinary files differ new file mode 100644 index 0000000000..04bff561ab --- /dev/null +++ b/tests/upload/fixture/bmp diff --git a/tests/upload/fixture/iff b/tests/upload/fixture/iffBinary files differ new file mode 100644 index 0000000000..24eda8f593 --- /dev/null +++ b/tests/upload/fixture/iff diff --git a/tests/upload/fixture/iff_maya b/tests/upload/fixture/iff_mayaBinary files differ new file mode 100644 index 0000000000..b6fb85101b --- /dev/null +++ b/tests/upload/fixture/iff_maya diff --git a/tests/upload/fixture/jp2 b/tests/upload/fixture/jp2Binary files differ new file mode 100644 index 0000000000..adca6ecf0e --- /dev/null +++ b/tests/upload/fixture/jp2 diff --git a/tests/upload/fixture/jpx b/tests/upload/fixture/jpxBinary files differ new file mode 100644 index 0000000000..adca6ecf0e --- /dev/null +++ b/tests/upload/fixture/jpx diff --git a/tests/upload/fixture/psd b/tests/upload/fixture/psdBinary files differ new file mode 100644 index 0000000000..d1bc9a6a70 --- /dev/null +++ b/tests/upload/fixture/psd diff --git a/tests/upload/fixture/tif_compressed b/tests/upload/fixture/tif_compressedBinary files differ new file mode 100644 index 0000000000..133b50c4f0 --- /dev/null +++ b/tests/upload/fixture/tif_compressed diff --git a/tests/upload/fixture/tif_msb b/tests/upload/fixture/tif_msbBinary files differ new file mode 100644 index 0000000000..32eb8abfbb --- /dev/null +++ b/tests/upload/fixture/tif_msb diff --git a/tests/upload/fixture/wbmp b/tests/upload/fixture/wbmpBinary files differ new file mode 100644 index 0000000000..708c86ccee --- /dev/null +++ b/tests/upload/fixture/wbmp diff --git a/tests/upload/imagesize_test.php b/tests/upload/imagesize_test.php new file mode 100644 index 0000000000..5429bb5c5e --- /dev/null +++ b/tests/upload/imagesize_test.php @@ -0,0 +1,97 @@ +<?php + +/** + * + * This file is part of the phpBB Forum Software package. + * + * @copyright (c) phpBB Limited <https://www.phpbb.com> + * @license GNU General Public License, version 2 (GPL-2.0) + * + * For full copyright and license information, please see + * the docs/CREDITS.txt file. + * + */ + +class phpbb_upload_imagesize_test extends \phpbb_test_case +{ +	/** @var \FastImageSize\FastImageSize */ +	protected $imagesize; + +	/** @var string Path to fixtures */ +	protected $path; + +	public function setUp() +	{ +		parent::setUp(); +		$this->imagesize = new \FastImageSize\FastImageSize(); +		$this->path = __DIR__ . '/fixture/'; +	} + +	public function data_get_imagesize() +	{ +		return array( +			array('foobar', 'image/bmp', false), +			array('png', 'image/png', array('width' => 1, 'height' => 1, 'type' => IMAGETYPE_PNG)), +			array('gif', 'image/png', false), +			array('png', '', array('width' => 1, 'height' => 1, 'type' => IMAGETYPE_PNG)), +			array('gif', 'image/gif', array('width' => 1, 'height' => 1, 'type' => IMAGETYPE_GIF)), +			array('jpg', 'image/gif', false), +			array('gif', '', array('width' => 1, 'height' => 1, 'type' => IMAGETYPE_GIF)), +			array('jpg', 'image/jpg', array('width' => 1, 'height' => 1, 'type' => IMAGETYPE_JPEG)), +			array('jpg', 'image/jpeg', array('width' => 1, 'height' => 1, 'type' => IMAGETYPE_JPEG)), +			array('png', 'image/jpg', false), +			array('jpg', '', array('width' => 1, 'height' => 1, 'type' => IMAGETYPE_JPEG)), +			array('psd', 'image/psd', array('width' => 2, 'height' => 1, 'type' => IMAGETYPE_PSD)), +			array('psd', 'image/photoshop', array('width' => 2, 'height' => 1, 'type' => IMAGETYPE_PSD)), +			array('jpg', 'image/psd', false), +			array('psd', '', array('width' => 2, 'height' => 1, 'type' => IMAGETYPE_PSD)), +			array('bmp', 'image/bmp', array('width' => 2, 'height' => 1, 'type' => IMAGETYPE_BMP)), +			array('png', 'image/bmp', false), +			array('bmp', '', array('width' => 2, 'height' => 1, 'type' => IMAGETYPE_BMP)), +			array('tif', 'image/tif', array('width' => 1, 'height' => 1, 'type' => IMAGETYPE_TIFF_II)), +			array('png', 'image/tif', false), +			array('tif', '', array('width' => 1, 'height' => 1, 'type' => IMAGETYPE_TIFF_II)), +			array('tif_compressed', 'image/tif', array('width' => 2, 'height' => 1, 'type' => IMAGETYPE_TIFF_II)), +			array('png', 'image/tiff', false), +			array('tif_compressed', '', array('width' => 2, 'height' => 1, 'type' => IMAGETYPE_TIFF_II)), +			array('tif_msb', 'image/tif', array('width' => 2, 'height' => 1, 'type' => IMAGETYPE_TIFF_MM)), +			array('tif_msb', '', array('width' => 2, 'height' => 1, 'type' => IMAGETYPE_TIFF_MM)), +			array('wbmp', 'image/wbmp', array('width' => 2, 'height' => 1, 'type' => IMAGETYPE_WBMP)), +			array('wbmp', 'image/vnd.wap.wbmp', array('width' => 2, 'height' => 1, 'type' => IMAGETYPE_WBMP)), +			array('png', 'image/vnd.wap.wbmp', false), +			array('wbmp', '', array('width' => 2, 'height' => 1, 'type' => IMAGETYPE_WBMP)), +			array('iff', 'image/iff', array('width' => 2, 'height' => 1, 'type' => IMAGETYPE_IFF)), +			array('iff', 'image/x-iff', array('width' => 2, 'height' => 1, 'type' => IMAGETYPE_IFF)), +			array('iff_maya', 'iamge/iff', array('width' => 2, 'height' => 1, 'type' => IMAGETYPE_IFF)), +			array('png', 'image/iff', false), +			array('png', 'image/x-iff', false), +			array('iff', '', array('width' => 2, 'height' => 1, 'type' => IMAGETYPE_IFF)), +			array('iff_maya', '', array('width' => 2, 'height' => 1, 'type' => IMAGETYPE_IFF)), +			array('jp2', 'image/jp2', array('width' => 2, 'height' => 1, 'type' => IMAGETYPE_JPEG2000)), +			array('jp2', 'image/jpx', array('width' => 2, 'height' => 1, 'type' => IMAGETYPE_JPEG2000)), +			array('jp2', 'image/jpm', array('width' => 2, 'height' => 1, 'type' => IMAGETYPE_JPEG2000)), +			array('jpg', 'image/jp2', false), +			array('jpx', 'image/jpx', array('width' => 2, 'height' => 1, 'type' => IMAGETYPE_JPEG2000)), +			array('jp2', '', array('width' => 2, 'height' => 1, 'type' => IMAGETYPE_JPEG2000)), +			array('jpx', '', array('width' => 2, 'height' => 1, 'type' => IMAGETYPE_JPEG2000)), +		); +	} + +	/** +	 * @dataProvider data_get_imagesize +	 */ +	public function test_get_imagesize($file, $mime_type, $expected) +	{ +		$this->assertEquals($expected, $this->imagesize->getImageSize($this->path . $file, $mime_type)); +	} + +	public function test_get_imagesize_remote() +	{ +		$this->assertSame(array( +			'width'		=> 80, +			'height'	=> 80, +			'type'		=> IMAGETYPE_JPEG, +		), +		$this->imagesize->getImageSize('https://secure.gravatar.com/avatar/55502f40dc8b7c769880b10874abc9d0.jpg')); +	} +} | 
