/**
  * Binding file with owner
  *
  * @param int $ownerId
  * @param int $ownerType
  * @param int $fileId
  * @return File|bool
  */
 private function bindSingle($ownerId, $ownerType, $fileId)
 {
     $file = $fileId ? File::findOne($fileId) : false;
     if ($file && $file->isOwner($ownerId, $ownerType)) {
         if ($this->bindSingleFile($file, $ownerId)) {
             // delete unnecessary files
             $currentFiles = File::getByOwner($ownerId, $ownerType);
             foreach ($currentFiles as $currFile) {
                 if ($currFile->id !== $file->id) {
                     $currFile->delete();
                 }
             }
             return $file;
         }
     }
     return false;
 }
Esempio n. 2
0
 /**
  * Upload and test a gallery
  *
  * @param array $config see Properties UploadAction
  * @return array $files and $model
  */
 protected function uploadGallery($config)
 {
     $response = $this->runAction($config);
     $response = $this->checkUploadGalleryResponse($response);
     $file = File::findOne($response[1]);
     $model = new News(['title' => 'test', $config['attribute'] => ['files' => [$file->id => 'test']]]);
     $ownerType = $model->getFileOwnerType($config['attribute']);
     $this->checkTmpFile($file, -1, $ownerType);
     $this->assertTrue($model->save());
     $files = $model->getFiles($config['attribute']);
     $this->assertCount(1, $files);
     foreach ($files as $file) {
         $this->checkNotTmpFile($file, $model->id, $ownerType);
     }
     return ['files' => $files, 'model' => $model];
 }
 public function testFailSaveFile()
 {
     extract($this->uploadFile(['modelName' => News::className(), 'attribute' => 'image_path', 'inputName' => 'file-300']));
     $response = $this->runAction(['modelName' => News::className(), 'attribute' => 'image_path', 'inputName' => 'file-500']);
     $file = File::findOne($response['id']);
     unlink($file->path(true));
     $model->image_path = $response['id'];
     $model->save();
     $this->assertFalse($file->path() === $model->image_path);
 }
 public function testFailSaveGallery()
 {
     extract($this->uploadGallery(['modelName' => News::className(), 'attribute' => 'image_gallery', 'inputName' => 'file-300', 'multiple' => true, 'template' => Yii::getAlias('@tests/data/views/gallery-item.php')]));
     $oldFiles = $files;
     $response = $this->runAction(['modelName' => News::className(), 'attribute' => 'image_gallery', 'inputName' => 'file-500']);
     $file = File::findOne($response['id']);
     unlink($file->path(true));
     $model->image_gallery = ['files' => [$response['id'] => 'test']];
     $model->save();
     $files = $model->getFiles('image_gallery');
     $this->assertCount(0, $files);
 }