diff options
author | Nathan Guse <nathaniel.guse@gmail.com> | 2013-12-04 14:12:25 -0800 |
---|---|---|
committer | Nathan Guse <nathaniel.guse@gmail.com> | 2013-12-04 14:12:25 -0800 |
commit | 1d243b78bbae5e638c966d046fdc324a7cb0da66 (patch) | |
tree | a32040f2c92340772bd2952620d120d7670a4186 /tests | |
parent | 97bf88dd3281a93815123217ad832340e6d5819e (diff) | |
parent | c9c419c4317ddd12b49082368e245f881a001610 (diff) | |
download | forums-1d243b78bbae5e638c966d046fdc324a7cb0da66.tar forums-1d243b78bbae5e638c966d046fdc324a7cb0da66.tar.gz forums-1d243b78bbae5e638c966d046fdc324a7cb0da66.tar.bz2 forums-1d243b78bbae5e638c966d046fdc324a7cb0da66.tar.xz forums-1d243b78bbae5e638c966d046fdc324a7cb0da66.zip |
Merge pull request #1813 from marc1706/ticket/11912
[ticket/11912] Add mimetype guesser for proper mimetype guessing
Diffstat (limited to 'tests')
-rw-r--r-- | tests/mimetype/fixtures/jpg | bin | 0 -> 519 bytes | |||
-rw-r--r-- | tests/mimetype/guesser_test.php | 166 | ||||
-rw-r--r-- | tests/mimetype/incorrect_guesser.php | 18 | ||||
-rw-r--r-- | tests/mimetype/null_guesser.php | 30 |
4 files changed, 214 insertions, 0 deletions
diff --git a/tests/mimetype/fixtures/jpg b/tests/mimetype/fixtures/jpg Binary files differnew file mode 100644 index 0000000000..3cd5038e38 --- /dev/null +++ b/tests/mimetype/fixtures/jpg diff --git a/tests/mimetype/guesser_test.php b/tests/mimetype/guesser_test.php new file mode 100644 index 0000000000..9f0371262b --- /dev/null +++ b/tests/mimetype/guesser_test.php @@ -0,0 +1,166 @@ +<?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; + +require_once dirname(__FILE__) . '/null_guesser.php'; +require_once dirname(__FILE__) . '/incorrect_guesser.php'; + +function function_exists($name) +{ + return guesser_test::$function_exists; +} + +class guesser_test extends \phpbb_test_case +{ + public static $function_exists = true; + + public function setUp() + { + global $phpbb_root_path; + + $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'; + $this->phpbb_root_path = $phpbb_root_path; + } + + 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); + } + + public function data_content_guesser() + { + return array( + array( + array( + 'image/jpeg', + 'image/jpeg', + ), + array(new \phpbb\mimetype\content_guesser), + false, + ), + array( + array( + 'application/octet-stream', + 'application/octet-stream', + ), + array(new \phpbb\mimetype\content_guesser), + true, + ), + array( + array( + 'application/octet-stream', + 'image/jpeg', + ), + array(new \phpbb\mimetype\extension_guesser), + ), + ); + } + + /** + * @dataProvider data_content_guesser + */ + public function test_content_guesser($expected, $guessers, $overload = false) + { + $supported = false; + self::$function_exists = !$overload; + + // Cover possible LogicExceptions + foreach ($guessers as $cur_guesser) + { + $supported += $cur_guesser->is_supported(); + } + + if (!$supported) + { + $this->setExpectedException('\LogicException'); + } + + $guesser = new \phpbb\mimetype\guesser($guessers); + $this->assertEquals($expected[0], $guesser->guess($this->jpg_file)); + $this->assertEquals($expected[1], $guesser->guess($this->jpg_file, $this->jpg_file . '.jpg')); + @copy($this->jpg_file, $this->jpg_file . '.jpg'); + $this->assertEquals($expected[1], $guesser->guess($this->jpg_file . '.jpg')); + @unlink($this->jpg_file . '.jpg'); + } + + public function test_sort_priority() + { + $guessers = array( + 'FileinfoMimeTypeGuesser' => new \Symfony\Component\HttpFoundation\File\MimeType\FileinfoMimeTypeGuesser, + 'extension_guesser' => new \phpbb\mimetype\extension_guesser, + 'FileBinaryMimeTypeGuesser' => new \Symfony\Component\HttpFoundation\File\MimeType\FileBinaryMimeTypeGuesser, + 'content_guesser' => new \phpbb\mimetype\content_guesser, + ); + $guessers['content_guesser']->set_priority(5); + $guessers['extension_guesser']->set_priority(-5); + usort($guessers, array($this->guesser, 'sort_priority')); + $this->assertInstanceOf('\phpbb\mimetype\content_guesser', $guessers[0]); + $this->assertInstanceOf('\phpbb\mimetype\extension_guesser', $guessers[3]); + } +} diff --git a/tests/mimetype/incorrect_guesser.php b/tests/mimetype/incorrect_guesser.php new file mode 100644 index 0000000000..3939826faa --- /dev/null +++ b/tests/mimetype/incorrect_guesser.php @@ -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'; + } +} diff --git a/tests/mimetype/null_guesser.php b/tests/mimetype/null_guesser.php new file mode 100644 index 0000000000..5316d3726f --- /dev/null +++ b/tests/mimetype/null_guesser.php @@ -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; + } +} |