/tests/mimetype/guesser_test.php

https://github.com/VSEphpbb/phpbb · PHP · 230 lines · 170 code · 27 blank · 33 comment · 4 complexity · 1cced91173b70a4e0ee6cebcb1fb25dc MD5 · raw file

  1. <?php
  2. /**
  3. *
  4. * This file is part of the phpBB Forum Software package.
  5. *
  6. * @copyright (c) phpBB Limited <https://www.phpbb.com>
  7. * @license GNU General Public License, version 2 (GPL-2.0)
  8. *
  9. * For full copyright and license information, please see
  10. * the docs/CREDITS.txt file.
  11. *
  12. */
  13. namespace phpbb\mimetype;
  14. require_once dirname(__FILE__) . '/null_guesser.php';
  15. require_once dirname(__FILE__) . '/incorrect_guesser.php';
  16. function function_exists($name)
  17. {
  18. return guesser_test::$function_exists;
  19. }
  20. class guesser_test extends \phpbb_test_case
  21. {
  22. public static $function_exists = false;
  23. protected $fileinfo_supported = false;
  24. public function setUp()
  25. {
  26. global $phpbb_root_path;
  27. $guessers = array(
  28. new \Symfony\Component\HttpFoundation\File\MimeType\FileinfoMimeTypeGuesser(),
  29. new \Symfony\Component\HttpFoundation\File\MimeType\FileBinaryMimeTypeGuesser(),
  30. new \phpbb\mimetype\extension_guesser,
  31. new \phpbb\mimetype\content_guesser,
  32. );
  33. // Check if any guesser except the extension_guesser is available
  34. $this->fileinfo_supported = $guessers[0]->isSupported() | $guessers[1]->isSupported() | $guessers[3]->is_supported();
  35. // Also create a guesser that emulates not having fileinfo available
  36. $this->guesser_no_fileinfo = new \phpbb\mimetype\guesser(array($guessers[2]));
  37. $this->guesser = new \phpbb\mimetype\guesser($guessers);
  38. $this->path = dirname(__FILE__);
  39. $this->jpg_file = $this->path . '/fixtures/jpg';
  40. $this->phpbb_root_path = $phpbb_root_path;
  41. }
  42. public function data_guess_files()
  43. {
  44. return array(
  45. array('image/gif', 'gif'),
  46. array('image/png', 'png'),
  47. array('image/jpeg', 'jpg'),
  48. array('image/tiff', 'tif'),
  49. array('text/html', 'txt'),
  50. array(false, 'foobar'),
  51. );
  52. }
  53. /**
  54. * @dataProvider data_guess_files
  55. */
  56. public function test_guess_files($expected, $file)
  57. {
  58. // We will always get application/octet-stream as mimetype if only the
  59. // extension guesser is supported
  60. if (!$this->fileinfo_supported)
  61. {
  62. $this->markTestSkipped('Unable to run tests depending on fileinfo if it is not available');
  63. }
  64. $this->assertEquals($expected, $this->guesser->guess($this->path . '/../upload/fixture/' . $file));
  65. }
  66. public function data_guess_files_no_fileinfo()
  67. {
  68. return array(
  69. array('application/octet-stream', 'gif'),
  70. array('application/octet-stream', 'txt'),
  71. array(false, 'foobar'),
  72. );
  73. }
  74. /**
  75. * @dataProvider data_guess_files_no_fileinfo
  76. */
  77. public function test_guess_files_no_fileinfo($expected, $file)
  78. {
  79. $this->assertEquals($expected, $this->guesser_no_fileinfo->guess($this->path . '/../upload/fixture/' . $file));
  80. }
  81. public function test_file_not_readable()
  82. {
  83. @chmod($this->jpg_file, 0000);
  84. if (is_readable($this->jpg_file))
  85. {
  86. @chmod($this->jpg_file, 0644);
  87. $this->markTestSkipped('is_readable always returns true if user is superuser or chmod does not work');
  88. }
  89. $this->assertEquals(false, $this->guesser->guess($this->jpg_file));
  90. @chmod($this->jpg_file, 0644);
  91. $this->assertEquals('image/jpeg', $this->guesser->guess($this->jpg_file));
  92. }
  93. public function test_null_guess()
  94. {
  95. $guesser = new \phpbb\mimetype\guesser(array(new \phpbb\mimetype\null_guesser));
  96. $this->assertEquals('application/octet-stream', $guesser->guess($this->jpg_file));
  97. }
  98. public function data_incorrect_guessers()
  99. {
  100. return array(
  101. array(array(new \phpbb\mimetype\incorrect_guesser)),
  102. array(array(new \phpbb\mimetype\null_guesser(false))),
  103. array(array()),
  104. );
  105. }
  106. /**
  107. * @dataProvider data_incorrect_guessers
  108. *
  109. * @expectedException \LogicException
  110. */
  111. public function test_incorrect_guesser($guessers)
  112. {
  113. $guesser = new \phpbb\mimetype\guesser($guessers);
  114. }
  115. public function data_content_guesser()
  116. {
  117. return array(
  118. array(
  119. array(
  120. 'image/jpeg',
  121. 'image/jpeg',
  122. ),
  123. array(new \phpbb\mimetype\content_guesser),
  124. false,
  125. ),
  126. array(
  127. array(
  128. 'application/octet-stream',
  129. 'application/octet-stream',
  130. ),
  131. array(new \phpbb\mimetype\content_guesser),
  132. true,
  133. ),
  134. array(
  135. array(
  136. 'application/octet-stream',
  137. 'image/jpeg',
  138. ),
  139. array(new \phpbb\mimetype\extension_guesser),
  140. ),
  141. );
  142. }
  143. /**
  144. * @dataProvider data_content_guesser
  145. */
  146. public function test_content_guesser($expected, $guessers, $overload = false)
  147. {
  148. $supported = false;
  149. self::$function_exists = !$overload;
  150. if (!\function_exists('mime_content_type'))
  151. {
  152. $this->markTestSkipped('Emulating supported mime_content_type() when it is not supported will cause a fatal error');
  153. }
  154. // Cover possible LogicExceptions
  155. foreach ($guessers as $cur_guesser)
  156. {
  157. $supported += $cur_guesser->is_supported();
  158. }
  159. if (!$supported)
  160. {
  161. $this->setExpectedException('\LogicException');
  162. }
  163. $guesser = new \phpbb\mimetype\guesser($guessers);
  164. $this->assertEquals($expected[0], $guesser->guess($this->jpg_file));
  165. $this->assertEquals($expected[1], $guesser->guess($this->jpg_file, $this->jpg_file . '.jpg'));
  166. @copy($this->jpg_file, $this->jpg_file . '.jpg');
  167. $this->assertEquals($expected[1], $guesser->guess($this->jpg_file . '.jpg'));
  168. @unlink($this->jpg_file . '.jpg');
  169. }
  170. public function test_sort_priority()
  171. {
  172. $guessers = array(
  173. 'FileinfoMimeTypeGuesser' => new \Symfony\Component\HttpFoundation\File\MimeType\FileinfoMimeTypeGuesser,
  174. 'extension_guesser' => new \phpbb\mimetype\extension_guesser,
  175. 'FileBinaryMimeTypeGuesser' => new \Symfony\Component\HttpFoundation\File\MimeType\FileBinaryMimeTypeGuesser,
  176. 'content_guesser' => new \phpbb\mimetype\content_guesser,
  177. );
  178. $guessers['content_guesser']->set_priority(5);
  179. $guessers['extension_guesser']->set_priority(-5);
  180. usort($guessers, array($this->guesser, 'sort_priority'));
  181. $this->assertInstanceOf('\phpbb\mimetype\content_guesser', $guessers[0]);
  182. $this->assertInstanceOf('\phpbb\mimetype\extension_guesser', $guessers[3]);
  183. }
  184. public function data_choose_mime_type()
  185. {
  186. return array(
  187. array('application/octet-stream', 'application/octet-stream', null),
  188. array('application/octet-stream', 'application/octet-stream', 'application/octet-stream'),
  189. array('binary', 'application/octet-stream', 'binary'),
  190. array('image/jpeg', 'application/octet-stream', 'image/jpeg'),
  191. array('image/jpeg', 'binary', 'image/jpeg'),
  192. array('image/jpeg', 'image/jpg', 'image/jpeg'),
  193. array('image/jpeg', 'image/jpeg', 'binary'),
  194. );
  195. }
  196. /**
  197. * @dataProvider data_choose_mime_type
  198. */
  199. public function test_choose_mime_type($expected, $mime_type, $guess)
  200. {
  201. $this->assertSame($expected, $this->guesser->choose_mime_type($mime_type, $guess));
  202. }
  203. }