123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- <?php
- namespace Gedmo\Uploadable\Mapping;
- use Gedmo\Uploadable\Mapping\Validator;
- /**
- * These are tests for the Mapping Validator of the Uploadable behavior
- *
- * @author Gustavo Falco <comfortablynumb84@gmail.com>
- * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
- * @link http://www.gediminasm.org
- * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
- */
- class ValidatorTest extends \PHPUnit_Framework_TestCase
- {
- protected $meta;
- public function setUp()
- {
- $this->meta = $this->getMock('Doctrine\ORM\Mapping\ClassMetadata', array(), array(), '', false);
- Validator::$enableMimeTypesConfigException = false;
- }
- public function tearDown()
- {
- Validator::$enableMimeTypesConfigException = true;
- }
- /**
- * @expectedException Gedmo\Exception\InvalidMappingException
- */
- public function test_validateField_ifFieldIsNotOfAValidTypeThrowException()
- {
- $this->meta->expects($this->once())
- ->method('getFieldMapping')
- ->will($this->returnValue(array('type' => 'someType')));
- Validator::validateField(
- $this->meta,
- 'someField',
- Validator::UPLOADABLE_FILE_MIME_TYPE,
- Validator::$validFileMimeTypeTypes
- );
- }
- /**
- * @expectedException Gedmo\Exception\UploadableInvalidPathException
- */
- public function test_validatePath_ifPathIsNotAStringOrIsAnEmptyStringThrowException()
- {
- Validator::validatePath('');
- }
- public function test_validatePath_ifPassedDirIsNotAValidDirectoryOrIsNotWriteableThrowException()
- {
- if (defined('PHP_WINDOWS_VERSION_BUILD')) {
- $this->markTestSkipped('Not possible to test on Windows');
- }
- $dir = sys_get_temp_dir().'/readonly-directory-12312432423';
- mkdir($dir, 0000, true);
- try {
- Validator::validatePath('/');
- } catch (\Gedmo\Exception\UploadableCantWriteException $e) {
- rmdir($dir);
- return;
- }
- rmdir($dir);
- $this->fail(
- sprintf('An expected exception "%s" has not been raised.', 'Gedmo\Exception\UploadableCantWriteException')
- );
- }
- public function test_validatePathCreatesNewDirectoryWhenItNotExists()
- {
- $dir = sys_get_temp_dir().'/new/directory-12312432423';
- Validator::validatePath($dir);
- $this->assertTrue(is_dir($dir));
- rmdir($dir);
- rmdir(dirname($dir));
- }
- public function test_validatePath_ifPassedDirIsNotAValidDirectoryOrIsNotWriteableDoesNotThrowExceptionIfDisabled()
- {
- Validator::$validateWritableDirectory = false;
- Validator::validatePath('/invalid/directory/12312432423');
- Validator::$validateWritableDirectory = true;
- }
- /**
- * @expectedException Gedmo\Exception\InvalidMappingException
- */
- public function test_validateConfiguration_ifFilePathFieldIsNotDefinedThrowException()
- {
- $config = array('filePathField' => false);
- Validator::validateConfiguration($this->meta, $config);
- }
- /**
- * @expectedException Gedmo\Exception\InvalidMappingException
- */
- public function test_validateConfiguration_ifPathMethodIsNotAValidMethodThrowException()
- {
- $this->meta->expects($this->once())
- ->method('getReflectionClass')
- ->will($this->returnValue(new \ReflectionClass(new FakeEntity())));
- $config = array('filePathField' => 'someField', 'pathMethod' => 'invalidMethod');
- Validator::validateConfiguration(
- $this->meta,
- $config
- );
- }
- /**
- * @expectedException Gedmo\Exception\InvalidMappingException
- */
- public function test_validateConfiguration_ifCallbackMethodIsNotAValidMethodThrowException()
- {
- $this->meta->expects($this->once())
- ->method('getReflectionClass')
- ->will($this->returnValue(new \ReflectionClass(new FakeEntity())));
- $config = array('filePathField' => 'someField', 'pathMethod' => '', 'callback' => 'invalidMethod');
- Validator::validateConfiguration(
- $this->meta,
- $config
- );
- }
- /**
- * @expectedException Gedmo\Exception\InvalidMappingException
- */
- public function test_validateConfiguration_ifFilenameGeneratorValueIsNotValidThrowException()
- {
- $this->meta->expects($this->once())
- ->method('getReflectionClass')
- ->will($this->returnValue(new \ReflectionClass(new FakeEntity())));
- $this->meta->expects($this->any())
- ->method('getFieldMapping')
- ->will($this->returnValue(array('type' => 'someType')));
- $config = array(
- 'fileMimeTypeField' => '',
- 'fileSizeField' => '',
- 'filePathField' => 'someField',
- 'pathMethod' => '',
- 'callback' => '',
- 'filenameGenerator' => 'invalidClass',
- 'maxSize' => 0,
- 'allowedTypes' => '',
- 'disallowedTypes' => ''
- );
- Validator::validateConfiguration(
- $this->meta,
- $config
- );
- }
- /**
- * @expectedException Gedmo\Exception\InvalidMappingException
- */
- public function test_validateConfiguration_ifFilenameGeneratorValueIsValidButDoesntImplementNeededInterfaceThrowException()
- {
- $this->meta->expects($this->once())
- ->method('getReflectionClass')
- ->will($this->returnValue(new \ReflectionClass(new FakeEntity())));
- $this->meta->expects($this->any())
- ->method('getFieldMapping')
- ->will($this->returnValue(array('type' => 'someType')));
- $config = array(
- 'fileMimeTypeField' => '',
- 'fileSizeField' => '',
- 'filePathField' => 'someField',
- 'pathMethod' => '',
- 'callback' => '',
- 'filenameGenerator' => 'DateTime',
- 'maxSize' => 0,
- 'allowedTypes' => '',
- 'disallowedTypes' => ''
- );
- Validator::validateConfiguration(
- $this->meta,
- $config
- );
- }
- public function test_validateConfiguration_ifFilenameGeneratorValueIsValidThenDontThrowException()
- {
- $this->meta->expects($this->once())
- ->method('getReflectionClass')
- ->will($this->returnValue(new \ReflectionClass(new FakeEntity())));
- $this->meta->expects($this->any())
- ->method('getFieldMapping')
- ->will($this->returnValue(array('type' => 'string')));
- $config = array(
- 'fileMimeTypeField' => '',
- 'fileSizeField' => '',
- 'filePathField' => 'someField',
- 'pathMethod' => '',
- 'callback' => '',
- 'filenameGenerator' => 'SHA1',
- 'maxSize' => 0,
- 'allowedTypes' => '',
- 'disallowedTypes' => ''
- );
- Validator::validateConfiguration(
- $this->meta,
- $config
- );
- }
- public function test_validateConfiguration_ifFilenameGeneratorValueIsAValidClassThenDontThrowException()
- {
- $this->meta->expects($this->once())
- ->method('getReflectionClass')
- ->will($this->returnValue(new \ReflectionClass(new FakeEntity())));
- $this->meta->expects($this->any())
- ->method('getFieldMapping')
- ->will($this->returnValue(array('type' => 'string')));
- $config = array(
- 'fileMimeTypeField' => '',
- 'fileSizeField' => '',
- 'filePathField' => 'someField',
- 'pathMethod' => '',
- 'callback' => '',
- 'filenameGenerator' => 'Gedmo\Uploadable\FilenameGenerator\FilenameGeneratorSha1',
- 'maxSize' => 0,
- 'allowedTypes' => '',
- 'disallowedTypes' => ''
- );
- Validator::validateConfiguration(
- $this->meta,
- $config
- );
- }
- /**
- * @expectedException Gedmo\Exception\InvalidMappingException
- */
- public function test_validateConfiguration_ifMaxSizeIsLessThanZeroThenThrowException()
- {
- $this->meta->expects($this->once())
- ->method('getReflectionClass')
- ->will($this->returnValue(new \ReflectionClass(new FakeEntity())));
- $config = array(
- 'fileMimeTypeField' => 'someField',
- 'filePathField' => 'someField',
- 'fileSizeField' => '',
- 'pathMethod' => '',
- 'callback' => '',
- 'maxSize' => -123,
- 'allowedTypes' => '',
- 'disallowedTypes' => ''
- );
- Validator::validateConfiguration(
- $this->meta,
- $config
- );
- }
- /**
- * @expectedException Gedmo\Exception\InvalidMappingException
- */
- public function test_validateConfiguration_ifAllowedTypesAndDisallowedTypesAreSetThenThrowException()
- {
- $this->meta->expects($this->once())
- ->method('getReflectionClass')
- ->will($this->returnValue(new \ReflectionClass(new FakeEntity())));
- Validator::$enableMimeTypesConfigException = true;
- $config = array(
- 'fileMimeTypeField' => 'someField',
- 'filePathField' => 'someField',
- 'fileSizeField' => '',
- 'pathMethod' => '',
- 'callback' => '',
- 'maxSize' => 0,
- 'allowedTypes' => 'text/plain',
- 'disallowedTypes' => 'text/css'
- );
- Validator::validateConfiguration(
- $this->meta,
- $config
- );
- }
- }
- class FakeEntity
- {
- }
|