ValidatorTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. * @package Gedmo.Uploadable.Mapping
  10. * @link http://www.gediminasm.org
  11. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  12. */
  13. class ValidatorTest extends \PHPUnit_Framework_TestCase
  14. {
  15. protected $meta;
  16. public function setUp()
  17. {
  18. $this->meta = $this->getMock('Doctrine\ORM\Mapping\ClassMetadata', array(), array(), '', false);
  19. Validator::$enableMimeTypesConfigException = false;
  20. }
  21. /**
  22. * @expectedException Gedmo\Exception\InvalidMappingException
  23. */
  24. public function test_validateField_ifFieldIsNotOfAValidTypeThrowException()
  25. {
  26. $this->meta->expects($this->once())
  27. ->method('getFieldMapping')
  28. ->will($this->returnValue(array('type' => 'someType')));
  29. Validator::validateField(
  30. $this->meta,
  31. 'someField',
  32. Validator::UPLOADABLE_FILE_MIME_TYPE,
  33. Validator::$validFileMimeTypeTypes
  34. );
  35. }
  36. /**
  37. * @expectedException Gedmo\Exception\UploadableInvalidPathException
  38. */
  39. public function test_validatePath_ifPathIsNotAStringOrIsAnEmptyStringThrowException()
  40. {
  41. Validator::validatePath('');
  42. }
  43. /**
  44. * @expectedException Gedmo\Exception\UploadableCantWriteException
  45. */
  46. public function test_validatePath_ifPassedDirIsNotAValidDirectoryOrIsNotWriteableThrowException()
  47. {
  48. Validator::validatePath('/invalid/directory/12312432423');
  49. }
  50. /**
  51. * @expectedException Gedmo\Exception\InvalidMappingException
  52. */
  53. public function test_validateConfiguration_ifFilePathFieldIsNotDefinedThrowException()
  54. {
  55. $config = array('filePathField' => false);
  56. Validator::validateConfiguration($this->meta, $config);
  57. }
  58. /**
  59. * @expectedException Gedmo\Exception\InvalidMappingException
  60. */
  61. public function test_validateConfiguration_ifPathMethodIsNotAValidMethodThrowException()
  62. {
  63. $this->meta->expects($this->once())
  64. ->method('getReflectionClass')
  65. ->will($this->returnValue(new \ReflectionClass(new FakeEntity())));
  66. $config = array('filePathField' => 'someField', 'pathMethod' => 'invalidMethod');
  67. Validator::validateConfiguration(
  68. $this->meta,
  69. $config
  70. );
  71. }
  72. /**
  73. * @expectedException Gedmo\Exception\InvalidMappingException
  74. */
  75. public function test_validateConfiguration_ifCallbackMethodIsNotAValidMethodThrowException()
  76. {
  77. $this->meta->expects($this->once())
  78. ->method('getReflectionClass')
  79. ->will($this->returnValue(new \ReflectionClass(new FakeEntity())));
  80. $config = array('filePathField' => 'someField', 'pathMethod' => '', 'callback' => 'invalidMethod');
  81. Validator::validateConfiguration(
  82. $this->meta,
  83. $config
  84. );
  85. }
  86. /**
  87. * @expectedException Gedmo\Exception\InvalidMappingException
  88. */
  89. public function test_validateConfiguration_ifFilenameGeneratorValueIsNotValidThrowException()
  90. {
  91. $this->meta->expects($this->once())
  92. ->method('getReflectionClass')
  93. ->will($this->returnValue(new \ReflectionClass(new FakeEntity())));
  94. $this->meta->expects($this->any())
  95. ->method('getFieldMapping')
  96. ->will($this->returnValue(array('type' => 'someType')));
  97. $config = array(
  98. 'fileMimeTypeField' => '',
  99. 'fileSizeField' => '',
  100. 'filePathField' => 'someField',
  101. 'pathMethod' => '',
  102. 'callback' => '',
  103. 'filenameGenerator' => 'invalidClass',
  104. 'maxSize' => 0,
  105. 'allowedTypes' => '',
  106. 'disallowedTypes' => ''
  107. );
  108. Validator::validateConfiguration(
  109. $this->meta,
  110. $config
  111. );
  112. }
  113. /**
  114. * @expectedException Gedmo\Exception\InvalidMappingException
  115. */
  116. public function test_validateConfiguration_ifFilenameGeneratorValueIsValidButDoesntImplementNeededInterfaceThrowException()
  117. {
  118. $this->meta->expects($this->once())
  119. ->method('getReflectionClass')
  120. ->will($this->returnValue(new \ReflectionClass(new FakeEntity())));
  121. $this->meta->expects($this->any())
  122. ->method('getFieldMapping')
  123. ->will($this->returnValue(array('type' => 'someType')));
  124. $config = array(
  125. 'fileMimeTypeField' => '',
  126. 'fileSizeField' => '',
  127. 'filePathField' => 'someField',
  128. 'pathMethod' => '',
  129. 'callback' => '',
  130. 'filenameGenerator' => 'DateTime',
  131. 'maxSize' => 0,
  132. 'allowedTypes' => '',
  133. 'disallowedTypes' => ''
  134. );
  135. Validator::validateConfiguration(
  136. $this->meta,
  137. $config
  138. );
  139. }
  140. public function test_validateConfiguration_ifFilenameGeneratorValueIsValidThenDontThrowException()
  141. {
  142. $this->meta->expects($this->once())
  143. ->method('getReflectionClass')
  144. ->will($this->returnValue(new \ReflectionClass(new FakeEntity())));
  145. $this->meta->expects($this->any())
  146. ->method('getFieldMapping')
  147. ->will($this->returnValue(array('type' => 'string')));
  148. $config = array(
  149. 'fileMimeTypeField' => '',
  150. 'fileSizeField' => '',
  151. 'filePathField' => 'someField',
  152. 'pathMethod' => '',
  153. 'callback' => '',
  154. 'filenameGenerator' => 'SHA1',
  155. 'maxSize' => 0,
  156. 'allowedTypes' => '',
  157. 'disallowedTypes' => ''
  158. );
  159. Validator::validateConfiguration(
  160. $this->meta,
  161. $config
  162. );
  163. }
  164. public function test_validateConfiguration_ifFilenameGeneratorValueIsAValidClassThenDontThrowException()
  165. {
  166. $this->meta->expects($this->once())
  167. ->method('getReflectionClass')
  168. ->will($this->returnValue(new \ReflectionClass(new FakeEntity())));
  169. $this->meta->expects($this->any())
  170. ->method('getFieldMapping')
  171. ->will($this->returnValue(array('type' => 'string')));
  172. $config = array(
  173. 'fileMimeTypeField' => '',
  174. 'fileSizeField' => '',
  175. 'filePathField' => 'someField',
  176. 'pathMethod' => '',
  177. 'callback' => '',
  178. 'filenameGenerator' => 'Gedmo\Uploadable\FilenameGenerator\FilenameGeneratorSha1',
  179. 'maxSize' => 0,
  180. 'allowedTypes' => '',
  181. 'disallowedTypes' => ''
  182. );
  183. Validator::validateConfiguration(
  184. $this->meta,
  185. $config
  186. );
  187. }
  188. /**
  189. * @expectedException Gedmo\Exception\InvalidMappingException
  190. */
  191. public function test_validateConfiguration_ifMaxSizeIsLessThanZeroThenThrowException()
  192. {
  193. $this->meta->expects($this->once())
  194. ->method('getReflectionClass')
  195. ->will($this->returnValue(new \ReflectionClass(new FakeEntity())));
  196. $config = array(
  197. 'fileMimeTypeField' => 'someField',
  198. 'filePathField' => 'someField',
  199. 'fileSizeField' => '',
  200. 'pathMethod' => '',
  201. 'callback' => '',
  202. 'maxSize' => -123,
  203. 'allowedTypes' => '',
  204. 'disallowedTypes' => ''
  205. );
  206. Validator::validateConfiguration(
  207. $this->meta,
  208. $config
  209. );
  210. }
  211. /**
  212. * @expectedException Gedmo\Exception\InvalidMappingException
  213. */
  214. public function test_validateConfiguration_ifAllowedTypesAndDisallowedTypesAreSetThenThrowException()
  215. {
  216. $this->meta->expects($this->once())
  217. ->method('getReflectionClass')
  218. ->will($this->returnValue(new \ReflectionClass(new FakeEntity())));
  219. Validator::$enableMimeTypesConfigException = true;
  220. $config = array(
  221. 'fileMimeTypeField' => 'someField',
  222. 'filePathField' => 'someField',
  223. 'fileSizeField' => '',
  224. 'pathMethod' => '',
  225. 'callback' => '',
  226. 'maxSize' => 0,
  227. 'allowedTypes' => 'text/plain',
  228. 'disallowedTypes' => 'text/css'
  229. );
  230. Validator::validateConfiguration(
  231. $this->meta,
  232. $config
  233. );
  234. }
  235. }
  236. class FakeEntity
  237. {
  238. }