ValidatorTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. namespace Gedmo\Uploadable\Mapping;
  3. use Gedmo\Uploadable\Mapping\Validator;
  4. /**
  5. * These are tests for the Mapping Validator of the Uploadable behavior
  6. *
  7. * @author Gustavo Falco <comfortablynumb84@gmail.com>
  8. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  9. * @link http://www.gediminasm.org
  10. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  11. */
  12. class ValidatorTest extends \PHPUnit_Framework_TestCase
  13. {
  14. protected $meta;
  15. public function setUp()
  16. {
  17. $this->meta = $this->getMock('Doctrine\ORM\Mapping\ClassMetadata', array(), array(), '', false);
  18. Validator::$enableMimeTypesConfigException = false;
  19. }
  20. public function tearDown()
  21. {
  22. Validator::$enableMimeTypesConfigException = true;
  23. }
  24. /**
  25. * @expectedException Gedmo\Exception\InvalidMappingException
  26. */
  27. public function test_validateField_ifFieldIsNotOfAValidTypeThrowException()
  28. {
  29. $this->meta->expects($this->once())
  30. ->method('getFieldMapping')
  31. ->will($this->returnValue(array('type' => 'someType')));
  32. Validator::validateField(
  33. $this->meta,
  34. 'someField',
  35. Validator::UPLOADABLE_FILE_MIME_TYPE,
  36. Validator::$validFileMimeTypeTypes
  37. );
  38. }
  39. /**
  40. * @expectedException Gedmo\Exception\UploadableInvalidPathException
  41. */
  42. public function test_validatePath_ifPathIsNotAStringOrIsAnEmptyStringThrowException()
  43. {
  44. Validator::validatePath('');
  45. }
  46. public function test_validatePath_ifPassedDirIsNotAValidDirectoryOrIsNotWriteableThrowException()
  47. {
  48. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  49. $this->markTestSkipped('Not possible to test on Windows');
  50. }
  51. $dir = sys_get_temp_dir().'/readonly-directory-12312432423';
  52. mkdir($dir, 0000, true);
  53. try {
  54. Validator::validatePath('/');
  55. } catch (\Gedmo\Exception\UploadableCantWriteException $e) {
  56. rmdir($dir);
  57. return;
  58. }
  59. rmdir($dir);
  60. $this->fail(
  61. sprintf('An expected exception "%s" has not been raised.', 'Gedmo\Exception\UploadableCantWriteException')
  62. );
  63. }
  64. public function test_validatePathCreatesNewDirectoryWhenItNotExists()
  65. {
  66. $dir = sys_get_temp_dir().'/new/directory-12312432423';
  67. Validator::validatePath($dir);
  68. $this->assertTrue(is_dir($dir));
  69. rmdir($dir);
  70. rmdir(dirname($dir));
  71. }
  72. public function test_validatePath_ifPassedDirIsNotAValidDirectoryOrIsNotWriteableDoesNotThrowExceptionIfDisabled()
  73. {
  74. Validator::$validateWritableDirectory = false;
  75. Validator::validatePath('/invalid/directory/12312432423');
  76. Validator::$validateWritableDirectory = true;
  77. }
  78. /**
  79. * @expectedException Gedmo\Exception\InvalidMappingException
  80. */
  81. public function test_validateConfiguration_ifFilePathFieldIsNotDefinedThrowException()
  82. {
  83. $config = array('filePathField' => false);
  84. Validator::validateConfiguration($this->meta, $config);
  85. }
  86. /**
  87. * @expectedException Gedmo\Exception\InvalidMappingException
  88. */
  89. public function test_validateConfiguration_ifPathMethodIsNotAValidMethodThrowException()
  90. {
  91. $this->meta->expects($this->once())
  92. ->method('getReflectionClass')
  93. ->will($this->returnValue(new \ReflectionClass(new FakeEntity())));
  94. $config = array('filePathField' => 'someField', 'pathMethod' => 'invalidMethod');
  95. Validator::validateConfiguration(
  96. $this->meta,
  97. $config
  98. );
  99. }
  100. /**
  101. * @expectedException Gedmo\Exception\InvalidMappingException
  102. */
  103. public function test_validateConfiguration_ifCallbackMethodIsNotAValidMethodThrowException()
  104. {
  105. $this->meta->expects($this->once())
  106. ->method('getReflectionClass')
  107. ->will($this->returnValue(new \ReflectionClass(new FakeEntity())));
  108. $config = array('filePathField' => 'someField', 'pathMethod' => '', 'callback' => 'invalidMethod');
  109. Validator::validateConfiguration(
  110. $this->meta,
  111. $config
  112. );
  113. }
  114. /**
  115. * @expectedException Gedmo\Exception\InvalidMappingException
  116. */
  117. public function test_validateConfiguration_ifFilenameGeneratorValueIsNotValidThrowException()
  118. {
  119. $this->meta->expects($this->once())
  120. ->method('getReflectionClass')
  121. ->will($this->returnValue(new \ReflectionClass(new FakeEntity())));
  122. $this->meta->expects($this->any())
  123. ->method('getFieldMapping')
  124. ->will($this->returnValue(array('type' => 'someType')));
  125. $config = array(
  126. 'fileMimeTypeField' => '',
  127. 'fileSizeField' => '',
  128. 'filePathField' => 'someField',
  129. 'pathMethod' => '',
  130. 'callback' => '',
  131. 'filenameGenerator' => 'invalidClass',
  132. 'maxSize' => 0,
  133. 'allowedTypes' => '',
  134. 'disallowedTypes' => ''
  135. );
  136. Validator::validateConfiguration(
  137. $this->meta,
  138. $config
  139. );
  140. }
  141. /**
  142. * @expectedException Gedmo\Exception\InvalidMappingException
  143. */
  144. public function test_validateConfiguration_ifFilenameGeneratorValueIsValidButDoesntImplementNeededInterfaceThrowException()
  145. {
  146. $this->meta->expects($this->once())
  147. ->method('getReflectionClass')
  148. ->will($this->returnValue(new \ReflectionClass(new FakeEntity())));
  149. $this->meta->expects($this->any())
  150. ->method('getFieldMapping')
  151. ->will($this->returnValue(array('type' => 'someType')));
  152. $config = array(
  153. 'fileMimeTypeField' => '',
  154. 'fileSizeField' => '',
  155. 'filePathField' => 'someField',
  156. 'pathMethod' => '',
  157. 'callback' => '',
  158. 'filenameGenerator' => 'DateTime',
  159. 'maxSize' => 0,
  160. 'allowedTypes' => '',
  161. 'disallowedTypes' => ''
  162. );
  163. Validator::validateConfiguration(
  164. $this->meta,
  165. $config
  166. );
  167. }
  168. public function test_validateConfiguration_ifFilenameGeneratorValueIsValidThenDontThrowException()
  169. {
  170. $this->meta->expects($this->once())
  171. ->method('getReflectionClass')
  172. ->will($this->returnValue(new \ReflectionClass(new FakeEntity())));
  173. $this->meta->expects($this->any())
  174. ->method('getFieldMapping')
  175. ->will($this->returnValue(array('type' => 'string')));
  176. $config = array(
  177. 'fileMimeTypeField' => '',
  178. 'fileSizeField' => '',
  179. 'filePathField' => 'someField',
  180. 'pathMethod' => '',
  181. 'callback' => '',
  182. 'filenameGenerator' => 'SHA1',
  183. 'maxSize' => 0,
  184. 'allowedTypes' => '',
  185. 'disallowedTypes' => ''
  186. );
  187. Validator::validateConfiguration(
  188. $this->meta,
  189. $config
  190. );
  191. }
  192. public function test_validateConfiguration_ifFilenameGeneratorValueIsAValidClassThenDontThrowException()
  193. {
  194. $this->meta->expects($this->once())
  195. ->method('getReflectionClass')
  196. ->will($this->returnValue(new \ReflectionClass(new FakeEntity())));
  197. $this->meta->expects($this->any())
  198. ->method('getFieldMapping')
  199. ->will($this->returnValue(array('type' => 'string')));
  200. $config = array(
  201. 'fileMimeTypeField' => '',
  202. 'fileSizeField' => '',
  203. 'filePathField' => 'someField',
  204. 'pathMethod' => '',
  205. 'callback' => '',
  206. 'filenameGenerator' => 'Gedmo\Uploadable\FilenameGenerator\FilenameGeneratorSha1',
  207. 'maxSize' => 0,
  208. 'allowedTypes' => '',
  209. 'disallowedTypes' => ''
  210. );
  211. Validator::validateConfiguration(
  212. $this->meta,
  213. $config
  214. );
  215. }
  216. /**
  217. * @expectedException Gedmo\Exception\InvalidMappingException
  218. */
  219. public function test_validateConfiguration_ifMaxSizeIsLessThanZeroThenThrowException()
  220. {
  221. $this->meta->expects($this->once())
  222. ->method('getReflectionClass')
  223. ->will($this->returnValue(new \ReflectionClass(new FakeEntity())));
  224. $config = array(
  225. 'fileMimeTypeField' => 'someField',
  226. 'filePathField' => 'someField',
  227. 'fileSizeField' => '',
  228. 'pathMethod' => '',
  229. 'callback' => '',
  230. 'maxSize' => -123,
  231. 'allowedTypes' => '',
  232. 'disallowedTypes' => ''
  233. );
  234. Validator::validateConfiguration(
  235. $this->meta,
  236. $config
  237. );
  238. }
  239. /**
  240. * @expectedException Gedmo\Exception\InvalidMappingException
  241. */
  242. public function test_validateConfiguration_ifAllowedTypesAndDisallowedTypesAreSetThenThrowException()
  243. {
  244. $this->meta->expects($this->once())
  245. ->method('getReflectionClass')
  246. ->will($this->returnValue(new \ReflectionClass(new FakeEntity())));
  247. Validator::$enableMimeTypesConfigException = true;
  248. $config = array(
  249. 'fileMimeTypeField' => 'someField',
  250. 'filePathField' => 'someField',
  251. 'fileSizeField' => '',
  252. 'pathMethod' => '',
  253. 'callback' => '',
  254. 'maxSize' => 0,
  255. 'allowedTypes' => 'text/plain',
  256. 'disallowedTypes' => 'text/css'
  257. );
  258. Validator::validateConfiguration(
  259. $this->meta,
  260. $config
  261. );
  262. }
  263. }
  264. class FakeEntity
  265. {
  266. }