public static function createImageFileModel($fileName = 'testImage.png')
 {
     $pathToFiles = Yii::getPathOfAlias('application.modules.zurmo.tests.unit.files');
     $filePath = $pathToFiles . DIRECTORY_SEPARATOR . $fileName;
     $contents = file_get_contents($pathToFiles . DIRECTORY_SEPARATOR . $fileName);
     $fileContent = new FileContent();
     $fileContent->content = $contents;
     $file = new ImageFileModel();
     $file->fileContent = $fileContent;
     $file->name = $fileName;
     $file->type = ZurmoFileHelper::getMimeType($pathToFiles . DIRECTORY_SEPARATOR . $fileName);
     $file->size = filesize($filePath);
     $saved = $file->save();
     assert('$saved');
     // Not Coding Standard
     return $file;
 }
Exemplo n.º 2
0
 protected function setImage($imageId)
 {
     $id = (int) $imageId;
     if ($id > 0) {
         try {
             $image = ImageFileModel::getById($id);
             $this->image = $image;
         } catch (NotFoundException $exception) {
             //Do nothing
         }
     }
 }
Exemplo n.º 3
0
 public function testCanDelete()
 {
     $fileContent = new FileContent();
     $fileContent->content = 'testContent';
     $imageFile = new ImageFileModel();
     $imageFile->name = 'testImage';
     $imageFile->size = 123;
     $imageFile->type = 'image/gif';
     $imageFile->fileContent = $fileContent;
     $imageFile->save();
     $this->assertTrue($imageFile->canDelete());
     Yii::app()->user->userModel = UserTestHelper::createBasicUser('test2');
     $this->assertFalse($imageFile->canDelete());
 }
 public function actionToggle($id, $attribute)
 {
     if (Yii::app()->request->isAjaxRequest && Yii::app()->request->isPostRequest) {
         $imageFile = ImageFileModel::getById((int) $id);
         $imageFile->toggle($attribute);
     }
 }
 public function testSuperUserAllDefaultControllerActions()
 {
     $super = $this->logoutCurrentUserLoginNewUserAndGetByUsername('super');
     $imageFile = ImageFileModel::getById($this->imageFile1Id);
     if (file_exists($imageFile->getImageCachePath())) {
         unlink($imageFile->getImageCachePath());
     }
     if (file_exists($imageFile->getImageCachePath(true))) {
         unlink($imageFile->getImageCachePath(true));
     }
     //Test all default controller actions that do not require any POST/GET variables to be passed.
     //This does not include portlet controller actions.
     $content = $this->runControllerWithNoExceptionsAndGetContent('zurmo/imageModel/getUploaded');
     $returnedObject = CJSON::decode($content);
     $this->assertContains('zurmo/imageModel/getImage?fileName=' . $imageFile->getImageCacheFileName(), $returnedObject[0]['image']);
     $this->assertContains('zurmo/imageModel/getThumb?fileName=' . $imageFile->getImageCacheFileName(), $returnedObject[0]['thumb']);
     $this->assertFalse(file_exists($imageFile->getImageCachePath()));
     $this->assertFalse(file_exists($imageFile->getImageCachePath(true)));
     //Test getting the image
     $this->setGetArray(array('fileName' => $imageFile->getImageCacheFileName()));
     @$this->runControllerWithExitExceptionAndGetContent('zurmo/imageModel/getImage');
     $this->assertTrue(file_exists($imageFile->getImageCachePath()));
     $this->assertFalse(file_exists($imageFile->getImageCachePath(true)));
     //Test getting the image thumb
     $this->setGetArray(array('fileName' => $imageFile->getImageCacheFileName()));
     @$this->runControllerWithExitExceptionAndGetContent('zurmo/imageModel/getThumb');
     $this->assertTrue(file_exists($imageFile->getImageCachePath()));
     $this->assertTrue(file_exists($imageFile->getImageCachePath(true)));
     //Test uploading invalid image
     $pathToFiles = Yii::getPathOfAlias('application.modules.zurmo.tests.unit.files');
     $filePath = $pathToFiles . DIRECTORY_SEPARATOR . 'testNote.txt';
     self::resetAndPopulateFilesArrayByFilePathAndName('file', $filePath, 'testNote.txt');
     $content = $this->runControllerWithNoExceptionsAndGetContent('zurmo/imageModel/upload');
     $returnedObject = CJSON::decode($content);
     $this->assertEquals('Error uploading the image', $returnedObject[0]['error']);
     //Test uploading valid image
     $pathToFiles = Yii::getPathOfAlias('application.modules.zurmo.tests.unit.files');
     $fileContents = file_get_contents($pathToFiles . DIRECTORY_SEPARATOR . 'testImage.png');
     $filePath = $pathToFiles . DIRECTORY_SEPARATOR . 'testImage.png';
     self::resetAndPopulateFilesArrayByFilePathAndName('file', $filePath, 'testImage.png');
     $content = $this->runControllerWithNoExceptionsAndGetContent('zurmo/imageModel/upload');
     $returnedObject = CJSON::decode($content);
     $this->assertContains('zurmo/imageModel/getImage?fileName=2_testImage.png', $returnedObject[0]['filelink']);
     // Not Coding Standard
     $createdImageFile = ImageFileModel::getById(2);
     $this->assertEquals($fileContents, $createdImageFile->fileContent->content);
     //Test deleting image
     $createdImageFile->inactive = false;
     $this->setGetArray(array('id' => 2));
     $this->runControllerWithNoExceptionsAndGetContent('zurmo/imageModel/delete', true);
     RedBeanModel::forgetAll();
     $deletedImageFile = ImageFileModel::getById(2);
     $deletedImageFile->inactive = true;
 }
 public function testSaveImageFromTemporaryFile()
 {
     $id = ImageFileModel::getCount() + 1;
     Yii::app()->user->userModel = User::getByUsername('super');
     $pathToFiles = Yii::getPathOfAlias('application.modules.zurmo.tests.unit.files');
     $filePath = $pathToFiles . DIRECTORY_SEPARATOR . 'testImage.png';
     $fileUploadData = ImageFileModelUtil::saveImageFromTemporaryFile($filePath, 'fileNameTest');
     $this->assertEquals($id, $fileUploadData['id']);
     $this->assertEquals('fileNameTest.png', $fileUploadData['name']);
     $this->assertContains('<img data-url=', $fileUploadData['summary']);
     $this->assertEquals('3.25KB', $fileUploadData['size']);
     $this->assertContains("getThumb?fileName={$id}_fileNameTest.png", $fileUploadData['thumbnail_url']);
     // Not Coding Standard
     $this->assertContains("getImage?fileName={$id}_fileNameTest.png", $fileUploadData['filelink']);
     // Not Coding Standard
     $this->assertContains('javascript:parent.transferModalImageValues', $fileUploadData['insert_link']);
 }
 public static function saveImageFromTemporaryFile($tempFilePath, $name)
 {
     $fileContent = new FileContent();
     $fileContent->content = file_get_contents($tempFilePath);
     $imageProperties = getimagesize($tempFilePath);
     $imageFileModel = new ImageFileModel();
     static::resolveImageName($name, $imageProperties['mime']);
     $imageFileModel->name = $name;
     $imageFileModel->size = filesize($tempFilePath);
     $imageFileModel->type = $imageProperties['mime'];
     $imageFileModel->width = $imageProperties[0];
     $imageFileModel->height = $imageProperties[1];
     $imageFileModel->fileContent = $fileContent;
     if ($imageFileModel->save()) {
         $imageFileModel->createImageCache();
         $fileUploadData = array('id' => $imageFileModel->id, 'name' => $imageFileModel->name, 'summary' => ImageFileModelUtil::getImageSummary($imageFileModel), 'size' => FileModelDisplayUtil::convertSizeToHumanReadableAndGet($imageFileModel->size), 'thumbnail_url' => Yii::app()->createAbsoluteUrl('zurmo/imageModel/getThumb', array('fileName' => $imageFileModel->getImageCacheFileName())), 'filelink' => Yii::app()->createAbsoluteUrl('zurmo/imageModel/getImage', array('fileName' => $imageFileModel->getImageCacheFileName())), 'insert_link' => static::resolveInsertLink($imageFileModel));
     } else {
         $message = Zurmo::t('ZurmoModule', 'Error uploading the image');
         $fileUploadData = array('error' => $message);
     }
     return $fileUploadData;
 }