feat(event): add per-user date change notices
This commit is contained in:
145
tests/Feature/Event/EventDateNoticeControllerTest.php
Normal file
145
tests/Feature/Event/EventDateNoticeControllerTest.php
Normal file
@@ -0,0 +1,145 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Event;
|
||||
|
||||
use App\Domains\Auth\Models\User;
|
||||
use App\Domains\Authorization\Enums\RoleCode;
|
||||
use App\Domains\Event\Enums\EventDateChangeType;
|
||||
use App\Domains\Event\Models\EventDateChange;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use Database\Seeders\AuthorizationSeeder;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use Tests\TestCase;
|
||||
|
||||
class EventDateNoticeControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->seed(AuthorizationSeeder::class);
|
||||
}
|
||||
|
||||
public function test_authentication_is_required_to_claim_notices(): void
|
||||
{
|
||||
$tenant = $this->createTenant('acme');
|
||||
|
||||
$this->postJson("/api/tenants/{$tenant->codigo}/event-date-notices/claim")
|
||||
->assertUnauthorized();
|
||||
}
|
||||
|
||||
public function test_changes_are_grouped_dynamically_for_each_users_pending_history(): void
|
||||
{
|
||||
$tenant = $this->createTenant('acme');
|
||||
$userA = $this->createUser($tenant);
|
||||
|
||||
$rescheduled = collect([
|
||||
$this->createChange($tenant, EventDateChangeType::Rescheduled, '2027-10-01', '2027-10-11'),
|
||||
$this->createChange($tenant, EventDateChangeType::Rescheduled, '2027-10-02', '2027-10-12'),
|
||||
]);
|
||||
$suspended = collect([
|
||||
$this->createChange($tenant, EventDateChangeType::Suspended, '2027-10-03'),
|
||||
$this->createChange($tenant, EventDateChangeType::Suspended, '2027-10-04'),
|
||||
]);
|
||||
|
||||
Sanctum::actingAs($userA);
|
||||
|
||||
for ($display = 1; $display <= 3; $display++) {
|
||||
$this->postJson("/api/tenants/{$tenant->codigo}/event-date-notices/claim")
|
||||
->assertOk()
|
||||
->assertJsonCount(2, 'data')
|
||||
->assertJsonPath('data.0.type', 'suspended')
|
||||
->assertJsonPath('data.0.change_ids', $suspended->modelKeys())
|
||||
->assertJsonPath('data.1.type', 'rescheduled')
|
||||
->assertJsonPath('data.1.change_ids', $rescheduled->modelKeys());
|
||||
}
|
||||
|
||||
$this->postJson("/api/tenants/{$tenant->codigo}/event-date-notices/claim")
|
||||
->assertOk()
|
||||
->assertJsonCount(0, 'data');
|
||||
|
||||
$latestReschedule = $this->createChange(
|
||||
$tenant,
|
||||
EventDateChangeType::Rescheduled,
|
||||
'2027-10-05',
|
||||
'2027-10-15',
|
||||
);
|
||||
|
||||
$this->postJson("/api/tenants/{$tenant->codigo}/event-date-notices/claim")
|
||||
->assertOk()
|
||||
->assertJsonCount(1, 'data')
|
||||
->assertJsonPath('data.0.type', 'rescheduled')
|
||||
->assertJsonPath('data.0.change_ids', [$latestReschedule->id])
|
||||
->assertJsonPath('data.0.title', 'FECHA REPROGRAMADA!');
|
||||
|
||||
$userB = $this->createUser($tenant);
|
||||
Sanctum::actingAs($userB);
|
||||
|
||||
$this->postJson("/api/tenants/{$tenant->codigo}/event-date-notices/claim")
|
||||
->assertOk()
|
||||
->assertJsonCount(2, 'data')
|
||||
->assertJsonPath('data.0.type', 'suspended')
|
||||
->assertJsonPath('data.0.change_ids', $suspended->modelKeys())
|
||||
->assertJsonPath('data.1.type', 'rescheduled')
|
||||
->assertJsonPath(
|
||||
'data.1.change_ids',
|
||||
[...$rescheduled->modelKeys(), $latestReschedule->id],
|
||||
)
|
||||
->assertJsonPath('data.1.title', 'FECHAS REPROGRAMADAS!');
|
||||
|
||||
foreach ([...$rescheduled, ...$suspended] as $change) {
|
||||
$this->assertDatabaseHas('user_event_date_change_views', [
|
||||
'user_id' => $userA->id,
|
||||
'event_date_change_id' => $change->id,
|
||||
'display_count' => 3,
|
||||
]);
|
||||
}
|
||||
|
||||
$this->assertDatabaseHas('user_event_date_change_views', [
|
||||
'user_id' => $userA->id,
|
||||
'event_date_change_id' => $latestReschedule->id,
|
||||
'display_count' => 1,
|
||||
]);
|
||||
$this->assertDatabaseCount('user_event_date_change_views', 9);
|
||||
}
|
||||
|
||||
private function createTenant(string $code): Tenant
|
||||
{
|
||||
return Tenant::query()->create([
|
||||
'codigo' => $code,
|
||||
'nombre' => ucfirst($code),
|
||||
'dominio' => "{$code}.test",
|
||||
'primary_color' => '#000000',
|
||||
'secondary_color' => '#000000',
|
||||
'danger_color' => '#000000',
|
||||
'success_color' => '#000000',
|
||||
'header_bg_color' => '#000000',
|
||||
'footer_bg_color' => '#000000',
|
||||
]);
|
||||
}
|
||||
|
||||
private function createUser(Tenant $tenant): User
|
||||
{
|
||||
return User::factory()->create([
|
||||
'rol_codigo' => RoleCode::User->value,
|
||||
'tenant_codigo' => $tenant->codigo,
|
||||
]);
|
||||
}
|
||||
|
||||
private function createChange(
|
||||
Tenant $tenant,
|
||||
EventDateChangeType $type,
|
||||
string $previousDate,
|
||||
?string $newDate = null,
|
||||
): EventDateChange {
|
||||
return EventDateChange::query()->create([
|
||||
'tenant_code' => $tenant->codigo,
|
||||
'change_type' => $type,
|
||||
'previous_date' => $previousDate,
|
||||
'new_date' => $newDate,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user