|
| 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 | +} |
0 commit comments