Skip to content

Commit

Permalink
improve unit tests testGetFormAllowEditWithoutAnswers
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 d5e4b22 commit 84c47fb
Showing 1 changed file with 40 additions and 153 deletions.
193 changes: 40 additions & 153 deletions tests/Unit/Service/FormsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,7 @@ public function dataGetForm() {
];
}

/**
* @dataProvider dataGetForm
*
* @param array $expected
*/
public function testGetForm(array $expected) {
private function prepareFormTest(bool $withUserMock) {
// The form
$form = new Form();
$form->setId(42);
Expand All @@ -283,13 +278,15 @@ public function testGetForm(array $expected) {

// User & Group Formatting
$user = $this->createMock(IUser::class);
$user->expects($this->once())
->method('getDisplayName')
->willReturn('Some User');
$this->userManager->expects($this->once())
->method('get')
->with('someUser')
->willReturn($user);
if ($withUserMock) {
$user->expects($this->once())
->method('getDisplayName')
->willReturn('Some User');
$this->userManager->expects($this->once())
->method('get')
->with('someUser')
->willReturn($user);
}

// Questions
$question1 = new Question();
Expand Down Expand Up @@ -339,6 +336,30 @@ public function testGetForm(array $expected) {
$share->setShareWith('someUser');
$share->setPermissions([Constants::PERMISSION_SUBMIT]);

$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 ];

return [$form, $user, $share, $answers];
}

/**
* @dataProvider dataGetForm
*
* @param array $expected
*/
public function testGetForm(array $expected) {
[$form, $user, $share, $answers] = $this->prepareFormTest(true);

$this->shareMapper->expects($this->any())
->method('findByForm')
->with(42)
Expand Down Expand Up @@ -439,83 +460,15 @@ public function dataGetFormWithAnswers() {
* @param array $expected
*/
public function testGetFormAllowEditWithAnswers(array $expected) {
[$form, $user, $share, $answers] = $this->prepareFormTest(false);

// 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')
Expand All @@ -540,87 +493,21 @@ public function testGetFormAllowEditWithAnswers(array $expected) {
*
* @param array $expected
*/
public function testGetFormAllowEditWithoutAnswers(array $expected) {
// drop the answers for this test
public function testGetFormAllowEditWithoutSubmission(array $expected) {
// drop the submissions and answers for this test
unset($expected['answers']);
unset($expected['newSubmission']);
unset($expected['submissionId']);
[$form, $user, $share, $answers] = $this->prepareFormTest(false);

// 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]);

$answers = [ ];

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

$this->answerMapper->expects($this->once())
->method('findBySubmission')
->with(12)
->willReturn($answers);
->willThrowException(new DoesNotExistException('Test exception'));

$this->submissionMapper->expects($this->once())
->method('countSubmissions')
Expand Down

0 comments on commit 84c47fb

Please sign in to comment.