public function testStatuses() { $file = new File(); $file->protected = false; $this->assertTrue($file->isUnprotected()); $file->protected = true; $this->assertTrue($file->isProtected()); }
/** * Create file from path * * @param string $path Path in filesystem or URL * @param int $ownerId * @param int $ownerType * @param bool $saveAfterUpload Save the file immediately after upload * @param bool $protected File is protected? * @return \rkit\filemanager\models\File|bool */ public function createFromPath($path, $ownerId = -1, $ownerType = -1, $saveAfterUpload = false, $protected = false) { $tempfile = tempnam(sys_get_temp_dir(), 'FMR'); if ($filecontent = @file_get_contents($path)) { file_put_contents($tempfile, $filecontent); $pathInfo = pathinfo($path); $file = new File(['tmp' => true, 'owner_id' => $ownerId, 'owner_type' => $ownerType, 'size' => filesize($tempfile), 'mime' => FileHelper::getMimeType($tempfile), 'title' => $pathInfo['filename'], 'name' => File::generateName($pathInfo['extension']), 'protected' => $protected]); return $file->saveToTmp($tempfile, $saveAfterUpload, false); } else { throw new InvalidValueException('Unable to create from `' . $path . '`'); } }
/** * Bind files * * @param File $file * @param int $ownerId * @param array $files See `bindMultiple` * @return bool */ private function bindMultipleFile($file, $ownerId, $files) { $position = @array_search($file->id, array_keys($files)) + 1; if ($file->tmp) { $file->owner_id = $ownerId; $file->tmp = false; if ($file->saveFile()) { $file->updateAttributes(['tmp' => $file->tmp, 'owner_id' => $file->owner_id, 'title' => @$files[$file->id], 'position' => $position]); return true; } } else { $file->updateAttributes(['title' => @$files[$file->id], 'position' => $position]); } return false; }
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); }
/** * 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 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); }