Skip to content

Commit

Permalink
fix file types for question files
Browse files Browse the repository at this point in the history
Signed-off-by: ailkiv <[email protected]>
  • Loading branch information
AIlkiv committed Jul 10, 2024
1 parent 664154a commit bd49e13
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
11 changes: 4 additions & 7 deletions lib/Service/FormsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,10 @@ public function getQuestions(int $formId): array {
$question['accept'] = [];
if ($question['type'] === Constants::ANSWER_TYPE_FILE) {
if ($question['extraSettings']['allowedFileTypes'] ?? null) {
$aliases = $this->mimeTypeDetector->getAllAliases();

foreach ($question['extraSettings']['allowedFileTypes'] as $type) {
$question['accept'] = array_keys(array_filter($aliases, function (string $alias) use ($type) {
return $alias === $type;
}));
}
$question['accept'] = array_keys(array_intersect(
$this->mimeTypeDetector->getAllAliases(),
$question['extraSettings']['allowedFileTypes']
));
}

if ($question['extraSettings']['allowedFileExtensions'] ?? null) {
Expand Down
46 changes: 46 additions & 0 deletions tests/Unit/Service/FormsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ function microtime(bool|float $asFloat = false) {
use OCA\Forms\Service\CirclesService;
use OCA\Forms\Service\ConfigService;
use OCA\Forms\Service\FormsService;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\Files\Folder;
use OCP\Files\IMimeTypeDetector;
use OCP\Files\IRootFolder;
Expand Down Expand Up @@ -1469,4 +1470,49 @@ public function testGetTemporaryUploadedFilePath() {
$this->assertSame('Forms/unsubmitted/1234567.89/10 - Form 1/30 - question name',
$this->formsService->getTemporaryUploadedFilePath($form, $question));
}

public function testGetQuestionsReturnsEmptyArrayWhenNoQuestions(): void {
$this->questionMapper->method('findByForm')->willReturn([]);

$result = $this->formsService->getQuestions(1);

$this->assertEmpty($result);
}

public function testGetQuestionsWithVariousQuestionTypes(): void {
$questionEntities = [
$this->createQuestionEntity(['id' => 1, 'type' => 'text']),
$this->createQuestionEntity(['id' => 2, 'type' => Constants::ANSWER_TYPE_FILE, 'extraSettings' => [
'allowedFileTypes' => ['image', 'x-office/document'],
'allowedFileExtensions' => ['jpg']
]])
];

$this->questionMapper->method('findByForm')->willReturn($questionEntities);
$this->mimeTypeDetector->method('getAllAliases')->willReturn([
'application/coreldraw' => 'image',
'application/msonenote' => 'x-office/document',
]);

$result = $this->formsService->getQuestions(1);

$this->assertCount(2, $result);
$this->assertEquals('text', $result[0]['type']);
$this->assertEquals(Constants::ANSWER_TYPE_FILE, $result[1]['type']);
$this->assertEquals(['application/coreldraw', 'application/msonenote', '.jpg'], $result[1]['accept']);
}

public function testGetQuestionsHandlesDoesNotExistException(): void {
$this->questionMapper->method('findByForm')->willThrowException(new DoesNotExistException('test'));

$result = $this->formsService->getQuestions(1);

$this->assertEmpty($result);
}

private function createQuestionEntity(array $data): Question {
$questionEntity = $this->createMock(Question::class);
$questionEntity->method('read')->willReturn($data);
return $questionEntity;
}
}

0 comments on commit bd49e13

Please sign in to comment.