1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-05-29 02:29:21 +02:00

[ticket/11912] Add tests for phpbb mimetype guesser

PHPBB3-11912
This commit is contained in:
Marc Alexander 2013-10-23 18:36:11 +02:00
parent 36d314e032
commit c8040024cb
4 changed files with 131 additions and 0 deletions

BIN
tests/mimetype/fixtures/jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 519 B

View File

@ -0,0 +1,83 @@
<?php
/**
*
* @package testing
* @copyright (c) 2013 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
require_once dirname(__FILE__) . '/null_guesser.php';
require_once dirname(__FILE__) . '/incorrect_guesser.php';
class phpbb_mimetype_guesser_test extends phpbb_test_case
{
public function setUp()
{
$guessers = array(
new Symfony\Component\HttpFoundation\File\MimeType\FileinfoMimeTypeGuesser(),
new Symfony\Component\HttpFoundation\File\MimeType\FileBinaryMimeTypeGuesser(),
);
$this->guesser = new \phpbb\mimetype\guesser($guessers);
$this->path = dirname(__FILE__);
$this->jpg_file = $this->path . '/fixtures/jpg';
}
public function data_guess_files()
{
return array(
array('image/gif', 'gif'),
array('image/png', 'png'),
array('image/jpeg', 'jpg'),
array('image/tiff', 'tif'),
array('text/html', 'txt'),
array(false, 'foobar'),
);
}
/**
* @dataProvider data_guess_files
*/
public function test_guess_files($expected, $file)
{
$this->assertEquals($expected, $this->guesser->guess($this->path . '/../upload/fixture/' . $file));
}
public function test_file_not_readable()
{
@chmod($this->jpg_file, 0000);
if (is_readable($this->jpg_file))
{
@chmod($this->jpg_file, 0644);
$this->markTestSkipped('is_readable always returns true if user is superuser or chmod does not work');
}
$this->assertEquals(false, $this->guesser->guess($this->jpg_file));
@chmod($this->jpg_file, 0644);
$this->assertEquals('image/jpeg', $this->guesser->guess($this->jpg_file));
}
public function test_null_guess()
{
$guesser = new \phpbb\mimetype\guesser(array(new \phpbb\mimetype\null_guesser));
$this->assertEquals('application/octet-stream', $guesser->guess($this->jpg_file));
}
public function data_incorrect_guessers()
{
return array(
array(array(new \phpbb\mimetype\incorrect_guesser)),
array(array(new \phpbb\mimetype\null_guesser(false))),
array(array()),
);
}
/**
* @dataProvider data_incorrect_guessers
*
* @expectedException \LogicException
*/
public function test_incorrect_guesser($guessers)
{
$guesser = new \phpbb\mimetype\guesser($guessers);
}
}

View File

@ -0,0 +1,18 @@
<?php
/**
*
* @package testing
* @copyright (c) 2013 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
namespace phpbb\mimetype;
class incorrect_guesser
{
public function guess($file)
{
return 'image/jpeg';
}
}

View File

@ -0,0 +1,30 @@
<?php
/**
*
* @package testing
* @copyright (c) 2013 phpBB Group
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
namespace phpbb\mimetype;
class null_guesser
{
protected $is_supported;
public function __construct($is_supported = true)
{
$this->is_supported = $is_supported;
}
public function is_supported()
{
return $this->is_supported;
}
public function guess($file)
{
return null;
}
}