|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature; |
| 4 | + |
| 5 | +use App\Http\Controllers\Auth\ApplicationController; |
| 6 | +use App\Http\Controllers\Secretariat\SemesterEvaluationController; |
| 7 | +use App\Models\PeriodicEvents\PeriodicEvent; |
| 8 | +use App\Models\Semester; |
| 9 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 10 | +use Tests\TestCase; |
| 11 | + |
| 12 | +class PeriodicEventTest extends TestCase |
| 13 | +{ |
| 14 | + use RefreshDatabase; |
| 15 | + |
| 16 | + /** |
| 17 | + * start < now < end |
| 18 | + */ |
| 19 | + public function test_get_periodic_event(): void |
| 20 | + { |
| 21 | + PeriodicEvent::create([ |
| 22 | + 'event_model' => ApplicationController::class, |
| 23 | + 'start_date' => now()->subDay(), |
| 24 | + 'end_date' => now()->addDay(), |
| 25 | + ]); |
| 26 | + |
| 27 | + $event = ApplicationController::periodicEvent(); |
| 28 | + |
| 29 | + $this->assertNotNull($event); |
| 30 | + $this->assertTrue($event->isActive()); |
| 31 | + $this->assertFalse($event->isExtended()); |
| 32 | + $this->assertEquals($event->end_date, $event->real_end_date); |
| 33 | + |
| 34 | + |
| 35 | + $this->assertNull(SemesterEvaluationController::periodicEvent()); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * start < end < now |
| 40 | + */ |
| 41 | + public function test_get_periodic_event_passed(): void |
| 42 | + { |
| 43 | + PeriodicEvent::create([ |
| 44 | + 'event_model' => ApplicationController::class, |
| 45 | + 'start_date' => now()->subDays(2), |
| 46 | + 'end_date' => now()->subDay(), |
| 47 | + ]); |
| 48 | + |
| 49 | + $event = ApplicationController::periodicEvent(); |
| 50 | + $this->assertNull($event); |
| 51 | + |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * start < end < now < show_until |
| 56 | + */ |
| 57 | + public function test_get_periodic_event_passed_showed(): void |
| 58 | + { |
| 59 | + PeriodicEvent::create([ |
| 60 | + 'event_model' => ApplicationController::class, |
| 61 | + 'start_date' => now()->subDays(2), |
| 62 | + 'end_date' => now()->subDay(), |
| 63 | + 'show_until' => now()->addDay() |
| 64 | + ]); |
| 65 | + |
| 66 | + $event = ApplicationController::periodicEvent(); |
| 67 | + |
| 68 | + $this->assertNotNull($event); |
| 69 | + $this->assertFalse($event->isActive()); |
| 70 | + $this->assertFalse($event->isExtended()); |
| 71 | + $this->assertEquals($event->end_date, $event->real_end_date); |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * start < end < now < extended_end |
| 77 | + */ |
| 78 | + public function test_get_periodic_event_passed_extended(): void |
| 79 | + { |
| 80 | + PeriodicEvent::create([ |
| 81 | + 'event_model' => ApplicationController::class, |
| 82 | + 'start_date' => now()->subDays(2), |
| 83 | + 'end_date' => now()->subDay(), |
| 84 | + 'extended_end_date' => now()->addDay() |
| 85 | + ]); |
| 86 | + |
| 87 | + $event = ApplicationController::periodicEvent(); |
| 88 | + |
| 89 | + $this->assertNotNull($event); |
| 90 | + $this->assertTrue($event->isActive()); |
| 91 | + $this->assertTrue($event->isExtended()); |
| 92 | + $this->assertEquals($event->extended_end_date, $event->real_end_date); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * start < end < extended_end < now |
| 97 | + */ |
| 98 | + public function test_get_periodic_event_passed_extended_passed(): void |
| 99 | + { |
| 100 | + PeriodicEvent::create([ |
| 101 | + 'event_model' => ApplicationController::class, |
| 102 | + 'start_date' => now()->subDays(3), |
| 103 | + 'end_date' => now()->subDays(2), |
| 104 | + 'extended_end_date' => now()->subDay() |
| 105 | + ]); |
| 106 | + |
| 107 | + $this->assertNull(ApplicationController::periodicEvent()); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * start < end < extended_end < now < show_until |
| 112 | + */ |
| 113 | + public function test_get_periodic_event_passed_extended_passed_showed(): void |
| 114 | + { |
| 115 | + PeriodicEvent::create([ |
| 116 | + 'event_model' => ApplicationController::class, |
| 117 | + 'start_date' => now()->subDays(3), |
| 118 | + 'end_date' => now()->subDays(2), |
| 119 | + 'extended_end_date' => now()->subDay(), |
| 120 | + 'show_until' => now()->addDay() |
| 121 | + ]); |
| 122 | + |
| 123 | + $event = ApplicationController::periodicEvent(); |
| 124 | + |
| 125 | + $this->assertNotNull($event); |
| 126 | + $this->assertFalse($event->isActive()); |
| 127 | + $this->assertTrue($event->isExtended()); |
| 128 | + $this->assertEquals($event->extended_end_date, $event->real_end_date); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * now < start < end |
| 133 | + */ |
| 134 | + public function test_get_periodic_event_future(): void |
| 135 | + { |
| 136 | + PeriodicEvent::create([ |
| 137 | + 'event_model' => ApplicationController::class, |
| 138 | + 'start_date' => now()->addDay(), |
| 139 | + 'end_date' => now()->addDay(), |
| 140 | + ]); |
| 141 | + |
| 142 | + $event = ApplicationController::periodicEvent(); |
| 143 | + |
| 144 | + $this->assertNotNull($event); |
| 145 | + $this->assertFalse($event->isActive()); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * get latest event based on start date |
| 150 | + */ |
| 151 | + public function test_get_latest_event(): void |
| 152 | + { |
| 153 | + PeriodicEvent::create([ |
| 154 | + 'event_model' => ApplicationController::class, |
| 155 | + 'start_date' => now()->subDays(3), |
| 156 | + 'end_date' => now()->addDays(2), |
| 157 | + ]); |
| 158 | + PeriodicEvent::create([ |
| 159 | + 'event_model' => ApplicationController::class, |
| 160 | + 'start_date' => now()->subDay(), |
| 161 | + 'end_date' => now()->addDay(), |
| 162 | + ]); |
| 163 | + PeriodicEvent::create([ |
| 164 | + 'event_model' => SemesterEvaluationController::class, |
| 165 | + 'start_date' => now()->addDay(), |
| 166 | + 'end_date' => now()->addDay(), |
| 167 | + ]); |
| 168 | + |
| 169 | + $event = ApplicationController::periodicEvent(); |
| 170 | + |
| 171 | + $this->assertEquals(now()->subDay()->format('Y-m-d'), $event->start_date->format('Y-m-d')); |
| 172 | + $this->assertEquals(now()->addDay()->format('Y-m-d'), $event->end_date->format('Y-m-d')); |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * test event creation, if current event exists it is overridden |
| 177 | + */ |
| 178 | + public function test_creeta_event(): void |
| 179 | + { |
| 180 | + $this->actingAs($this->admin); |
| 181 | + |
| 182 | + $response = $this->post(route('applications.event'), [ |
| 183 | + 'semester_id' => Semester::current()->id, |
| 184 | + 'start_date' => now()->subDays(3), |
| 185 | + 'end_date' => now()->addDays(2), |
| 186 | + 'show_until' => now()->addDay(), |
| 187 | + ]); |
| 188 | + |
| 189 | + $response->assertStatus(302); |
| 190 | + $response->assertSessionHasNoErrors(); |
| 191 | + |
| 192 | + $response = $this->post(route('applications.event'), [ |
| 193 | + 'semester_id' => Semester::next()->id, |
| 194 | + 'start_date' => now()->subDays(2), |
| 195 | + 'end_date' => now()->addDays(3), |
| 196 | + 'show_until' => now()->addDay(), |
| 197 | + ]); |
| 198 | + $response->assertStatus(302); |
| 199 | + $response->assertSessionHasNoErrors(); |
| 200 | + |
| 201 | + $this->assertDatabaseCount('periodic_events', 1); |
| 202 | + } |
| 203 | +} |
0 commit comments