Ejemplo n.º 1
0
 /**
  * Ensures that the validator follows expected behavior
  *
  * @return void
  */
 public function testBasic()
 {
     $valuesExpected = array(array(array('image/jpg', 'image/jpeg'), true), array('image', true), array('test/notype', false), array('image/gif, image/jpg, image/jpeg', true), array(array('image/vasa', 'image/jpg', 'image/jpeg'), true), array(array('image/jpg', 'image/jpeg', 'gif'), true), array(array('image/gif', 'gif'), false), array('image/jp', false), array('image/jpg2000', false), array('image/jpeg2000', false));
     $filetest = dirname(__FILE__) . '/_files/picture.jpg';
     $files = array('name' => 'picture.jpg', 'type' => 'image/jpg', 'size' => 200, 'tmp_name' => $filetest, 'error' => 0);
     foreach ($valuesExpected as $element) {
         $options = array_shift($element);
         $expected = array_shift($element);
         $validator = new File\MimeType($options);
         $validator->enableHeaderCheck();
         $this->assertEquals($expected, $validator->isValid($filetest, $files), "Test expected " . var_export($expected, 1) . " with " . var_export($options, 1) . "\nMessages: " . var_export($validator->getMessages(), 1));
     }
 }
Ejemplo n.º 2
0
 /**
  * @group ZF-11258
  */
 public function testZF11258()
 {
     $validator = new File\MimeType(array('image/gif', 'image/jpg', 'headerCheck' => true));
     $this->assertFalse($validator->isValid(__DIR__ . '/_files/nofile.mo'));
     $this->assertTrue(array_key_exists('fileMimeTypeNotReadable', $validator->getMessages()));
     $this->assertContains("'nofile.mo'", current($validator->getMessages()));
 }
Ejemplo n.º 3
0
 public function testEmptyFileShouldReturnFalseAndDisplayNotFoundMessage()
 {
     if (!extension_loaded('fileinfo')) {
         $this->markTestSkipped('This PHP Version has no finfo installed');
     }
     $validator = new File\MimeType();
     $this->assertFalse($validator->isValid(''));
     $filesArray = array('name' => '', 'size' => 0, 'tmp_name' => '', 'error' => UPLOAD_ERR_NO_FILE, 'type' => '');
     $this->assertFalse($validator->isValid($filesArray));
 }
Ejemplo n.º 4
0
 /**
  * @group ZF-9686
  */
 public function testDualValidation()
 {
     $valuesExpected = array(array('image', true));
     $filetest = __DIR__ . '/_files/picture.jpg';
     $files = array('name' => 'picture.jpg', 'type' => 'image/jpg', 'size' => 200, 'tmp_name' => $filetest, 'error' => 0);
     foreach ($valuesExpected as $element) {
         $options = array_shift($element);
         $expected = array_shift($element);
         $validator = new File\MimeType($options);
         $validator->enableHeaderCheck();
         $this->assertEquals($expected, $validator->isValid($filetest, $files), "Test expected " . var_export($expected, 1) . " with " . var_export($options, 1) . "\nMessages: " . var_export($validator->getMessages(), 1));
         $validator = new File\MimeType($options);
         $validator->enableHeaderCheck();
         $this->assertEquals($expected, $validator->isValid($filetest, $files), "Test expected " . var_export($expected, 1) . " with " . var_export($options, 1) . "\nMessages: " . var_export($validator->getMessages(), 1));
     }
 }
Ejemplo n.º 5
0
 /**
  * @param Idea $idea
  * @param      $fileData
  * @param null $alternativeName
  */
 public function addFileToIdea(Idea $idea, $fileData, $alternativeName = null)
 {
     //Parse first the type of file to see in which table it should be stored
     $mimeType = new MimeType();
     $mimeType->isValid($fileData);
     /*
      * Use the fileSize validator to validate the size
      */
     $fileSize = new FilesSize(PHP_INT_MAX);
     $fileSize->isValid($fileData);
     $contentType = $this->getGeneralService()->findContentTypeByContentTypeName($fileData['type']);
     switch ($mimeType->type) {
         case 'application/pdf':
             //All treated as document
         //All treated as document
         case 'application/msword':
             //All treated as document
         //All treated as document
         case 'application/vnd.ms-excel':
             //All treated as document
         //All treated as document
         case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
             //All treated as document
         //All treated as document
         case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
             //All treated as document
         //All treated as document
         case 'application/vnd.openxmlformats-officedocument.presentationml.presentation':
             $ideaDocument = new Document();
             $ideaDocument->setIdea($idea);
             $ideaDocument->setFilename($fileData['name']);
             $ideaDocument->setSize($fileSize->size);
             if (is_null($alternativeName)) {
                 $ideaDocument->setDocument($fileData['name']);
             } else {
                 $ideaDocument->setDocument($alternativeName);
             }
             $ideaDocumentObject = new DocumentObject();
             $ideaDocumentObject->setObject(file_get_contents($fileData['tmp_name']));
             $ideaDocument->setContentType($contentType);
             $ideaDocumentObject->setDocument($ideaDocument);
             $this->newEntity($ideaDocumentObject);
             break;
         case 'image/jpeg':
         case 'image/png':
             $ideaImage = new Image();
             $ideaImage->setIdea($idea);
             if (is_null($alternativeName)) {
                 $ideaImage->setImage($fileData['name']);
             } else {
                 $ideaImage->setImage($alternativeName);
             }
             $ideaImage->setSize($fileSize->size);
             $ideaImage->setContentType($contentType);
             $ideaImageObject = new ImageObject();
             $ideaImageObject->setObject(file_get_contents($fileData['tmp_name']));
             $thumb = new GD($fileData['tmp_name']);
             $thumb->resize(200);
             $ideaImageObject->setThumb($thumb->getImageAsString());
             $ideaImageObject->setImage($ideaImage);
             $this->newEntity($ideaImageObject);
             break;
     }
 }