diff --git a/src/models/File.php b/src/models/File.php index f3d2870..316d293 100644 --- a/src/models/File.php +++ b/src/models/File.php @@ -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] ]; diff --git a/tests/FileControllerTest.php b/tests/FileControllerTest.php index e7fcee1..79ed1d7 100644 --- a/tests/FileControllerTest.php +++ b/tests/FileControllerTest.php @@ -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()); + + } + } diff --git a/tests/FileTest.php b/tests/FileTest.php index 2a1d234..f80d46f 100644 --- a/tests/FileTest.php +++ b/tests/FileTest.php @@ -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); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 88ae47f..92f52a6 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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, @@ -128,7 +128,8 @@ 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); } } @@ -136,7 +137,8 @@ public function checkFilesExist($types) 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); } } diff --git a/tests/data/db.sqlite b/tests/data/db.sqlite index ff2b87b..07b0d48 100644 Binary files a/tests/data/db.sqlite and b/tests/data/db.sqlite differ diff --git a/tests/files/file b/tests/files/file new file mode 100644 index 0000000..6c8db5d --- /dev/null +++ b/tests/files/file @@ -0,0 +1 @@ +file 1 \ No newline at end of file