[ticket/13031] Slightly change behavior of choose_mime_type and add unit tests

The mime type 'application/octet-stream' will still always be overwritten by
proper guesses. However, guesses with guessers that have a higher priority
will now overwrite previous guesses even if the mime types of these guesses
had a slash in them.

PHPBB3-13031
This commit is contained in:
Marc Alexander 2014-09-05 21:55:49 +02:00
parent d31ff51785
commit 21029e9fd2
2 changed files with 25 additions and 4 deletions

View file

@ -131,22 +131,22 @@ class guesser
* Choose the best mime type based on the current mime type and the guess * Choose the best mime type based on the current mime type and the guess
* If a guesser returns nulls or application/octet-stream, we will keep * If a guesser returns nulls or application/octet-stream, we will keep
* the current guess. Guesses with a slash inside them will be favored over * the current guess. Guesses with a slash inside them will be favored over
* already existing without slashes. However, any guess that will pass the * already existing ones. However, any guess that will pass the first check
* first check will always overwrite the default application/octet-stream. * will always overwrite the default application/octet-stream.
* *
* @param string $mime_type The current mime type * @param string $mime_type The current mime type
* @param string $guess The current mime type guess * @param string $guess The current mime type guess
* *
* @return string The best mime type based on current mime type and guess * @return string The best mime type based on current mime type and guess
*/ */
protected function choose_mime_type($mime_type, $guess) public function choose_mime_type($mime_type, $guess)
{ {
if ($guess === null || $guess == 'application/octet-stream') if ($guess === null || $guess == 'application/octet-stream')
{ {
return $mime_type; return $mime_type;
} }
if ((strpos($mime_type, '/') === false || $mime_type == 'application/octet-stream') && strpos($guess, '/') !== false) if ($mime_type == 'application/octet-stream' || strpos($guess, '/') !== false)
{ {
$mime_type = $guess; $mime_type = $guess;
} }

View file

@ -206,4 +206,25 @@ class guesser_test extends \phpbb_test_case
$this->assertInstanceOf('\phpbb\mimetype\content_guesser', $guessers[0]); $this->assertInstanceOf('\phpbb\mimetype\content_guesser', $guessers[0]);
$this->assertInstanceOf('\phpbb\mimetype\extension_guesser', $guessers[3]); $this->assertInstanceOf('\phpbb\mimetype\extension_guesser', $guessers[3]);
} }
public function data_choose_mime_type()
{
return array(
array('application/octet-stream', 'application/octet-stream', null),
array('application/octet-stream', 'application/octet-stream', 'application/octet-stream'),
array('binary', 'application/octet-stream', 'binary'),
array('image/jpeg', 'application/octet-stream', 'image/jpeg'),
array('image/jpeg', 'binary', 'image/jpeg'),
array('image/jpeg', 'image/jpg', 'image/jpeg'),
array('image/jpeg', 'image/jpeg', 'binary'),
);
}
/**
* @dataProvider data_choose_mime_type
*/
public function test_choose_mime_type($expected, $mime_type, $guess)
{
$this->assertSame($expected, $this->guesser->choose_mime_type($mime_type, $guess));
}
} }