Skip to content

Commit

Permalink
fix no extension CTOlet#61
Browse files Browse the repository at this point in the history
  • Loading branch information
ssv committed Apr 3, 2019
1 parent 6f66ccd commit 3749247
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/models/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function fields()
public function rules()
{
return [
[['name', 'model', 'itemId', 'hash', 'size', 'type', 'mime'], 'required'],
[['name', 'model', 'itemId', 'hash', 'size', 'mime'], 'required'],
[['itemId', 'size'], 'integer'],
[['name', 'model', 'hash', 'type', 'mime'], 'string', 'max' => 255]
];
Expand Down
53 changes: 53 additions & 0 deletions tests/FileControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,57 @@ public function testPreUpload5()
$errorMessage = 'Only files with these MIME types are allowed: image/png, image/jpeg.';
$this->assertTrue(in_array($errorMessage, $response['error']));
}

public function testPreUpload6()
{
$types = ['png', ''];
$this->generateFiles($types);
$response = Yii::$app->runAction('attachments/file/upload');
$this->assertArrayHasKey('uploadedFiles', $response);
$this->assertTrue(in_array('file', $response['uploadedFiles']));
$this->checkFilesExist($types);

foreach ($types as $type) {
$fileType = empty($type) ? $type : ".".$type;
/** @var Response $response */
$response = Yii::$app->runAction('attachments/file/download-temp', ['filename' => "file$fileType"]);
ob_start();
$response->send();
$actual = ob_get_clean();
$response->clear();
$expected = file_get_contents(Yii::getAlias("@tests/files/file$fileType"));
$this->assertEquals($expected, $actual);

$response = Yii::$app->runAction('attachments/file/delete-temp', ['filename' => "file$fileType"]);
$this->assertEquals($response, []);
}
$this->checkFilesNotExist($types);
$this->assertFileNotExists($this->getTempDirPath());

$comment = new Comment();
$comment->text = 'test';
$this->assertTrue($comment->save());

foreach ($comment->files as $file) {
$fileType = empty($file->type) ? $file->type : ".".$file->type;
/** @var Response $response */
$response = Yii::$app->runAction('attachments/file/download', ['id' => $file->id]);
ob_start();
$response->send();
$actual = ob_get_clean();
$response->clear();
$expected = file_get_contents(Yii::getAlias("@tests/files/file{$fileType}"));
$this->assertEquals($expected, $actual);
$this->assertFileExists($file->path);
$response = Yii::$app->runAction('attachments/file/delete', ['id' => -1]);
$this->assertEquals($response, false);
$response = Yii::$app->runAction('attachments/file/delete', ['id' => $file->id]);
$this->assertEquals($response, true);
$this->assertFileNotExists($file->path);
}

$this->assertNotSame(false, $comment->delete());

}

}
2 changes: 1 addition & 1 deletion tests/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testValidate()
$this->assertArrayHasKey('itemId', $file->errors);
$this->assertArrayHasKey('hash', $file->errors);
$this->assertArrayHasKey('size', $file->errors);
$this->assertArrayHasKey('type', $file->errors);
//$this->assertArrayHasKey('type', $file->errors);
$this->assertArrayHasKey('mime', $file->errors);
}
}
8 changes: 5 additions & 3 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function generateFiles($types)
UploadedFile::reset();

foreach ($types as $index => $type) {
$file = "file.$type";
$file = $type ? "file.$type" : "file";
$path = Yii::getAlias("@tests/files/$file");
$_FILES["UploadForm[file][$index]"] = [
'name' => $file,
Expand All @@ -128,15 +128,17 @@ public function generateFiles($types)
public function checkFilesExist($types)
{
foreach ($types as $type) {
$filePath = $this->getTempDirPath() . "/file.$type";
$file = $type ? "/file.$type" : "/file";
$filePath = $this->getTempDirPath() . $file;
$this->assertFileExists($filePath);
}
}

public function checkFilesNotExist($types)
{
foreach ($types as $type) {
$filePath = $this->getTempDirPath() . "/file.$type";
$file = $type ? "/file.$type" : "/file";
$filePath = $this->getTempDirPath() . $file;
$this->assertFileNotExists($filePath);
}
}
Expand Down
Binary file modified tests/data/db.sqlite
Binary file not shown.
1 change: 1 addition & 0 deletions tests/files/file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
file 1

0 comments on commit 3749247

Please sign in to comment.