Skip to content

Commit

Permalink
add unit test testGetFormWithAnswers for AllowEdit
Browse files Browse the repository at this point in the history
Signed-off-by: Timotheus Pokorra <[email protected]>
  • Loading branch information
tpokorra committed Jan 4, 2025
1 parent 1139f15 commit 968a905
Showing 1 changed file with 184 additions and 0 deletions.
184 changes: 184 additions & 0 deletions tests/Unit/Service/FormsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function microtime(bool|float $asFloat = false) {
use OCA\Forms\Activity\ActivityManager;

use OCA\Forms\Constants;
use OCA\Forms\Db\Answer;
use OCA\Forms\Db\AnswerMapper;
use OCA\Forms\Db\Form;
use OCA\Forms\Db\FormMapper;
Expand All @@ -43,6 +44,7 @@ function microtime(bool|float $asFloat = false) {
use OCA\Forms\Db\ShareMapper;
use OCA\Forms\Db\Submission;
use OCA\Forms\Db\SubmissionMapper;
use OCA\Forms\Service\AnswerService;
use OCA\Forms\Service\CirclesService;
use OCA\Forms\Service\ConfigService;
use OCA\Forms\Service\FormsService;
Expand Down Expand Up @@ -352,6 +354,188 @@ public function testGetForm(array $expected) {
$this->assertEquals($expected, $this->formsService->getForm($form));
}

public function dataGetFormWithAnswers() {
return [
// Just the full form with Answers
'one-full-form-with-answers' => [[
'id' => 42,
'state' => 0,
'hash' => 'abcdefg',
'title' => 'Form 1',
'description' => 'Description Text',
'ownerId' => 'currentUser',
'created' => 123456789,
'access' => [
'permitAllUsers' => false,
'showToAllUsers' => false,
],
'expires' => 0,
'isAnonymous' => false,
'submitMultiple' => false,
'showExpiration' => false,
'lastUpdated' => 123456789,
'canSubmit' => true,
'submissionCount' => 123,
'questions' => [
[
'id' => 1,
'formId' => 42,
'order' => 1,
'type' => 'dropdown',
'isRequired' => false,
'extraSettings' => ['shuffleOptions' => true],
'text' => 'Question 1',
'description' => 'This is our first question.',
'name' => '',
'options' => [
[
'id' => 1,
'questionId' => 1,
'text' => 'Option 1',
'order' => null,
],
[
'id' => 2,
'questionId' => 1,
'text' => 'Option 2',
'order' => null,
]
],
'accept' => [],
],
[
'id' => 2,
'formId' => 42,
'order' => 2,
'type' => 'short',
'isRequired' => true,
'extraSettings' => [],
'text' => 'Question 2',
'description' => '',
'name' => 'city',
'options' => [],
'accept' => [],
]
],
'shares' => [
],
'answers' => [
1 => [ 0 => '1' ],
2 => [ 0 => 'London' ],
],
'submissionMessage' => null,
'fileId' => null,
'fileFormat' => null,
'permissions' => Constants::PERMISSION_ALL,
'allowEdit' => true,
'newSubmission' => false,
'submissionId' => 12,
]]
];
}

/**
* @dataProvider dataGetFormWithAnswers
*
* @param array $expected
*/
public function testGetFormWithAnswers(array $expected) {
// The form, with AllowEdit
$form = new Form();
$form->setId(42);
$form->setState(0); // default => 0 means active
$form->setHash('abcdefg');
$form->setTitle('Form 1');
$form->setDescription('Description Text');
$form->setOwnerId('currentUser');
$form->setCreated(123456789);
$form->setAccess([
'permitAllUsers' => false,
'showToAllUsers' => false,
]);
$form->setExpires(0);
$form->setIsAnonymous(false);
$form->setSubmitMultiple(false);
$form->setAllowEdit(true);
$form->setShowExpiration(false);
$form->setLastUpdated(123456789);

$submission = new Submission();
$submission->setId(12);

// Questions
$question1 = new Question();
$question1->setId(1);
$question1->setFormId(42);
$question1->setOrder(1);
$question1->setType('dropdown');
$question1->setIsRequired(false);
$question1->setExtraSettings([
'shuffleOptions' => true
]);
$question1->setText('Question 1');
$question1->setDescription('This is our first question.');
$question2 = new Question();
$question2->setId(2);
$question2->setFormId(42);
$question2->setOrder(2);
$question2->setType('short');
$question2->setIsRequired(true);
$question2->setText('Question 2');
$question2->setDescription('');
$question2->setName('city');
$question2->setExtraSettings([]);
$this->questionMapper->expects($this->once())
->method('findByForm')
->with(42)
->willReturn([$question1, $question2]);

// Options
$option1 = new Option();
$option1->setId(1);
$option1->setQuestionId(1);
$option1->setText('Option 1');
$option2 = new Option();
$option2->setId(2);
$option2->setQuestionId(1);
$option2->setText('Option 2');
$this->optionMapper->expects($this->any())
->method('findByQuestion')
->with(1)
->willReturn([$option1, $option2]);

$answerEntity = new Answer();
$answerEntity->setId(1);
$answerEntity->setSubmissionId(12);
$answerEntity->setFileId(112);
$answerEntity->setQuestionId(1);
$answerEntity->setText('Option 1');
$answer2Entity = new Answer();
$answer2Entity->setId(2);
$answer2Entity->setSubmissionId(12);
$answer2Entity->setQuestionId(2);
$answer2Entity->setText('London');
$answers = [ $answerEntity, $answer2Entity ];

$this->submissionMapper->expects($this->once())
->method('findByFormAndUser')
->with(42, 'currentUser')
->willReturn($submission);

$this->answerMapper->expects($this->once())
->method('findBySubmission')
->with(12)
->willReturn($answers);

$this->submissionMapper->expects($this->once())
->method('countSubmissions')
->with(42)
->willReturn(123);

// Run the test
$this->assertEquals($expected, $this->formsService->getForm($form));
}

public function dataGetPartialForm() {
return [
'onePartialOwnedForm' => [[
Expand Down

0 comments on commit 968a905

Please sign in to comment.