Skip to content

Commit 7728f1f

Browse files
committed
Adding tests
1 parent 5ba5874 commit 7728f1f

File tree

6 files changed

+227
-7
lines changed

6 files changed

+227
-7
lines changed

app/Http/Controllers/Secretariat/AnonymousQuestionController.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,7 @@ public function storeAnswerSheet(Request $request, Semester $semester)
159159
// Since answer sheets are anonymous,
160160
// we cannot append new answers to the previous sheet (if any);
161161
// we have to create a new one.
162-
$answerSheet = $semester->answerSheets()->create([
163-
'year_of_acceptance' => user()->educationalInformation->year_of_acceptance
164-
]);
162+
$answerSheet = AnswerSheet::createForUser(user(), $semester);
165163

166164
foreach($semester->questionsNotAnsweredBy(user()) as $question) {
167165
// validation ensures we have answers

app/Models/AnonymousQuestions/AnswerSheet.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
99
use Illuminate\Database\Eloquent\Model;
1010

11+
use App\Models\User;
1112
use App\Models\Semester;
1213
use App\Models\GeneralAssemblies\QuestionOption;
1314
use App\Models\AnonymousQuestions\LongAnswer;
@@ -55,4 +56,18 @@ public function longAnswers(): HasMany
5556
{
5657
return $this->hasMany(LongAnswer::class);
5758
}
59+
60+
/**
61+
* Create an answer sheet for the given user,
62+
* with their anonymous data.
63+
* The default semester is the current one.
64+
*/
65+
public static function createForUser(User $user, Semester $semester = null): AnswerSheet
66+
{
67+
if (is_null($semester)) $semester = Semester::current();
68+
69+
return $semester->answerSheets()->create([
70+
'year_of_acceptance' => $user->educationalInformation->year_of_acceptance
71+
]);
72+
}
5873
}

app/Models/GeneralAssemblies/Question.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public function giveAnonymousAnswer(User $user, AnswerSheet $answerSheet, array|
224224

225225
// for the sake of the seeders,
226226
// we have to accept answers for closed questions too
227-
if (!$this->isOpen() && !app()->runningInConsole()) {
227+
if (!$this->isOpen() && (!app()->runningInConsole() || app()->runningUnitTests())) {
228228
throw new Exception("question not open");
229229
}
230230
if ($this->max_options < count($options)) {

database/seeders/AnonymousQuestionSeeder.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ function (Semester $semester) {return $semester->isCurrent() || $semester->isClo
3939

4040
// the test users should not be included
4141
foreach(User::withRole(Role::COLLEGIST)->where('id', '>', 4)->get() as $collegist) {
42-
$answerSheet = $semester->answerSheets()->create([
43-
'year_of_acceptance' => $collegist->educationalInformation->year_of_acceptance
44-
]);
42+
$answerSheet = AnswerSheet::createForUser($collegist, $semester);
4543

4644
$singleChoice->giveAnonymousAnswer($collegist, $answerSheet, $singleChoice->options->random());
4745
$multipleChoice->giveAnonymousAnswer($collegist, $answerSheet, $multipleChoice->options->random(2)->all());

tests/Unit/AnonymousQuestionTest.php

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
<?php
2+
3+
namespace Tests\Unit;
4+
5+
use Tests\TestCase;
6+
use Carbon\Carbon;
7+
8+
use App\Models\User;
9+
use App\Models\Semester;
10+
use App\Models\AnonymousQuestions\AnswerSheet;
11+
use App\Models\GeneralAssemblies\Question;
12+
13+
/**
14+
* Tests anonymous feedback questions.
15+
* Mostly based on GeneralAssemblyTest.
16+
*/
17+
class AnonymousQuestionTest extends TestCase
18+
{
19+
/**
20+
* Tests answering a question belonging to an already closed semester
21+
* (which should fail).
22+
* @return void
23+
*/
24+
public function test_answering_closed_question(): void
25+
{
26+
$user = User::factory()->hasEducationalInformation()->create();
27+
28+
$semester = Semester::previous(); // important!
29+
$question = Question::factory()
30+
->for($semester, 'parent')
31+
->hasOptions(3)
32+
->create(['opened_at' => now()->subDay(), 'closed_at' => null]);
33+
34+
$this->expectException(\Exception::class);
35+
$question->giveAnonymousAnswer($user, AnswerSheet::createForUser($user, $semester),
36+
$question->options->first());
37+
}
38+
39+
/**
40+
* Tests answering the same question twice as the same user
41+
* (which should fail).
42+
* @return void
43+
*/
44+
public function test_answering_twice(): void
45+
{
46+
$user = User::factory()->hasEducationalInformation()->create();
47+
48+
$semester = Semester::current(); // gets created if does not already exist
49+
$question = Question::factory()
50+
->for($semester, 'parent')
51+
->hasOptions(3)
52+
->create(['opened_at' => now()->subDay(), 'closed_at' => null]);
53+
54+
$this->expectException(\Exception::class);
55+
56+
$answerSheet1 = AnswerSheet::createForUser($user, $semester);
57+
$answerSheet2 = AnswerSheet::createForUser($user, $semester);
58+
$question->giveAnonymousAnswer($user, $answerSheet1, $question->options->random(2)->all());
59+
$question->giveAnonymousAnswer($user, $answerSheet2, $question->options->random());
60+
}
61+
62+
/**
63+
* Tests answering a single-choice question.
64+
* @return void
65+
*/
66+
public function test_answering_radio(): void
67+
{
68+
$user = User::factory()->hasEducationalInformation()->create();
69+
70+
$semester = Semester::current();
71+
$question = Question::factory()
72+
->for($semester, 'parent')
73+
->hasOptions(3)
74+
->create(['opened_at' => now()->subDay(), 'closed_at' => null, 'max_options' => 1]);
75+
76+
$answerSheet = AnswerSheet::createForUser($user, $semester);
77+
$question->giveAnonymousAnswer($user, $answerSheet, $question->options->first());
78+
79+
$this->assertEquals(1, $question->options->first()->votes);
80+
$this->assertEquals(0, $question->options->get(1)->votes);
81+
$this->assertEquals(0, $question->options->get(2)->votes);
82+
83+
$this->assertTrue(
84+
$question->users()
85+
->where('id', $user->id)
86+
->exists()
87+
);
88+
89+
$this->assertTrue(
90+
$answerSheet->chosenOptions()
91+
->where('id', $question->options->first()->id)
92+
->exists()
93+
);
94+
$this->assertTrue(
95+
$answerSheet->chosenOptions()
96+
->where('id', $question->options->get(1)->id)
97+
->doesntExist()
98+
);
99+
$this->assertTrue(
100+
$answerSheet->chosenOptions()
101+
->where('id', $question->options->get(2)->id)
102+
->doesntExist()
103+
);
104+
}
105+
106+
/**
107+
* Tests choosing more options
108+
* for a single-choice question
109+
* (which should fail).
110+
* @return void
111+
*/
112+
public function test_answering_radio_with_more_options(): void
113+
{
114+
$user = User::factory()->hasEducationalInformation()->create();
115+
116+
$semester = Semester::current();
117+
$question = Question::factory()
118+
->for($semester, 'parent')
119+
->hasOptions(3)
120+
->create(['opened_at' => now()->subDay(), 'closed_at' => null, 'max_options' => 1]);
121+
122+
$this->expectException(\Exception::class);
123+
$question->giveAnonymousAnswer($user,
124+
AnswerSheet::createForUser($user, $semester), $question->options->random(2)->all());
125+
}
126+
127+
/**
128+
* Tests answering a multiple-choice question.
129+
* @return void
130+
*/
131+
public function test_voting_checkbox(): void
132+
{
133+
$user = User::factory()->hasEducationalInformation()->create();
134+
135+
$semester = Semester::current();
136+
$question = Question::factory()
137+
->for($semester, 'parent')
138+
->hasOptions(4)
139+
->create(['opened_at' => now()->subDay(), 'closed_at' => null, 'max_options' => 4]);
140+
141+
$answerSheet = AnswerSheet::createForUser($user, $semester);
142+
$question->giveAnonymousAnswer($user, $answerSheet,
143+
[$question->options->first(), $question->options->get(1)]);
144+
145+
$this->assertEquals(1, $question->options->first()->votes);
146+
$this->assertEquals(1, $question->options->get(1)->votes);
147+
$this->assertEquals(0, $question->options->get(2)->votes);
148+
$this->assertEquals(0, $question->options->get(3)->votes);
149+
150+
$this->assertTrue(
151+
$question->users()
152+
->where('id', $user->id)
153+
->exists()
154+
);
155+
156+
$this->assertTrue(
157+
$answerSheet->chosenOptions()
158+
->where('id', $question->options->first()->id)
159+
->exists()
160+
);
161+
$this->assertTrue(
162+
$answerSheet->chosenOptions()
163+
->where('id', $question->options->get(1)->id)
164+
->exists()
165+
);
166+
$this->assertTrue(
167+
$answerSheet->chosenOptions()
168+
->where('id', $question->options->get(2)->id)
169+
->doesntExist()
170+
);
171+
$this->assertTrue(
172+
$answerSheet->chosenOptions()
173+
->where('id', $question->options->get(3)->id)
174+
->doesntExist()
175+
);
176+
}
177+
178+
/**
179+
* Tests giving more options than allowed
180+
* for a multiple-choice question
181+
* (which should fail).
182+
* @return void
183+
*/
184+
public function test_voting_checkbox_with_more_options(): void
185+
{
186+
$user = User::factory()->hasEducationalInformation()->create();
187+
188+
$semester = Semester::current();
189+
$question = Question::factory()
190+
->for($semester, 'parent')
191+
->hasOptions(4)
192+
->create(['opened_at' => now()->subDay(), 'closed_at' => null, 'max_options' => 2]);
193+
194+
$this->expectException(\Exception::class);
195+
$question->vote($user, [$question->options->first(), $question->options->get(1), $question->options->get(2)]);
196+
}
197+
}

tests/Unit/GeneralAssemblyTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ public function test_voting_radio()
8888
$this->assertEquals(1, $question->options->first()->votes);
8989
$this->assertEquals(0, $question->options->get(1)->votes);
9090
$this->assertEquals(0, $question->options->get(2)->votes);
91+
92+
$this->assertTrue(
93+
$question->users()
94+
->where('id', $user->id)
95+
->exists()
96+
);
9197
}
9298

9399
/**
@@ -125,6 +131,12 @@ public function test_voting_checkbox()
125131
$this->assertEquals(1, $question->options->first()->votes);
126132
$this->assertEquals(1, $question->options->get(1)->votes);
127133
$this->assertEquals(0, $question->options->get(2)->votes);
134+
135+
$this->assertTrue(
136+
$question->users()
137+
->where('id', $user->id)
138+
->exists()
139+
);
128140
}
129141

130142
/**

0 commit comments

Comments
 (0)