188 lines
5.8 KiB
PHP
188 lines
5.8 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\User;
|
|
use App\Models\Person;
|
|
use App\Models\Migration;
|
|
use App\Models\Residence;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Foundation\Testing\WithFaker;
|
|
use Laravel\Sanctum\Sanctum;
|
|
use Tests\TestCase;
|
|
|
|
class HistoricalSearchControllerTest extends TestCase
|
|
{
|
|
use RefreshDatabase, WithFaker;
|
|
|
|
/**
|
|
* Set up authentication for each test
|
|
*/
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
// Create and authenticate as admin user for all tests
|
|
$user = User::factory()->create([
|
|
'email' => 'test.admin@example.com',
|
|
'is_admin' => true
|
|
]);
|
|
|
|
Sanctum::actingAs($user, ['*']);
|
|
}
|
|
|
|
/**
|
|
* Test historical search returns paginated results
|
|
*/
|
|
public function test_historical_search_returns_paginated_results(): void
|
|
{
|
|
// Create test data
|
|
Person::factory(15)->create();
|
|
|
|
// Test without query parameter (should return all records paginated)
|
|
$response = $this->getJson('/api/historical/search');
|
|
|
|
$response->assertStatus(200)
|
|
->assertJsonStructure([
|
|
'data',
|
|
'current_page',
|
|
'per_page',
|
|
'last_page',
|
|
'total',
|
|
'links' => [
|
|
'first',
|
|
'last',
|
|
'prev',
|
|
'next'
|
|
]
|
|
])
|
|
->assertJson([
|
|
'per_page' => 10,
|
|
'current_page' => 1
|
|
]);
|
|
|
|
// Verify we got 10 items (pagination limit)
|
|
$this->assertCount(10, $response->json('data'));
|
|
}
|
|
|
|
/**
|
|
* Test historical search with pagination parameters
|
|
*/
|
|
public function test_historical_search_pagination_works(): void
|
|
{
|
|
// Create test data - 25 records
|
|
Person::factory(25)->create();
|
|
|
|
// Test first page
|
|
$responsePage1 = $this->getJson('/api/historical/search?page=1');
|
|
|
|
$responsePage1->assertStatus(200)
|
|
->assertJson([
|
|
'current_page' => 1,
|
|
'last_page' => 3 // 25 records with 10 per page = 3 pages
|
|
]);
|
|
|
|
// Test second page
|
|
$responsePage2 = $this->getJson('/api/historical/search?page=2');
|
|
|
|
$responsePage2->assertStatus(200)
|
|
->assertJson([
|
|
'current_page' => 2
|
|
]);
|
|
|
|
// Verify different data between pages
|
|
$this->assertNotEquals(
|
|
$responsePage1->json('data')[0],
|
|
$responsePage2->json('data')[0]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Test historical search with query parameter
|
|
*/
|
|
public function test_historical_search_filters_by_query(): void
|
|
{
|
|
// Create test person with a unique name
|
|
Person::factory()->create([
|
|
'christian_name' => 'UniqueTestName',
|
|
'surname' => 'TestSurname'
|
|
]);
|
|
|
|
// Create other random records
|
|
Person::factory(10)->create();
|
|
|
|
// Search for the unique name
|
|
$response = $this->getJson('/api/historical/search?query=UniqueTestName');
|
|
|
|
$response->assertStatus(200);
|
|
|
|
// The search should find our unique record
|
|
$this->assertStringContainsString('UniqueTestName', json_encode($response->json('data')));
|
|
}
|
|
|
|
/**
|
|
* Test search returns correct paginated links
|
|
*/
|
|
public function test_historical_search_returns_correct_links(): void
|
|
{
|
|
// Create enough data for multiple pages
|
|
Person::factory(30)->create();
|
|
|
|
// Test middle page
|
|
$response = $this->getJson('/api/historical/search?page=2');
|
|
|
|
$response->assertStatus(200)
|
|
->assertJsonStructure([
|
|
'links' => [
|
|
'first',
|
|
'last',
|
|
'prev',
|
|
'next'
|
|
]
|
|
]);
|
|
|
|
// Check links exist and contain page parameters
|
|
$this->assertStringContainsString('page=1', $response->json('links.first'));
|
|
$this->assertStringContainsString('page=3', $response->json('links.last'));
|
|
$this->assertStringContainsString('page=1', $response->json('links.prev'));
|
|
$this->assertStringContainsString('page=3', $response->json('links.next'));
|
|
}
|
|
|
|
/**
|
|
* Test search preserves query parameters in pagination links
|
|
*/
|
|
public function test_historical_search_preserves_query_params_in_pagination(): void
|
|
{
|
|
// Create 30 people with 'Test' in their name to ensure we have enough matching records
|
|
Person::factory(30)->create([
|
|
'christian_name' => 'Test Person'
|
|
]);
|
|
|
|
// Search with a query and page=1 (so we definitely have a next page)
|
|
$response = $this->getJson('/api/historical/search?query=Test&page=1');
|
|
|
|
$response->assertStatus(200);
|
|
|
|
// Verify links contain both the query and page parameters
|
|
$this->assertStringContainsString('query=Test', $response->json('links.first'));
|
|
$this->assertStringContainsString('query=Test', $response->json('links.last'));
|
|
$this->assertStringContainsString('query=Test', $response->json('links.next'));
|
|
}
|
|
|
|
/**
|
|
* Test empty result handling
|
|
*/
|
|
public function test_historical_search_handles_empty_results(): void
|
|
{
|
|
// Search with a query that won't match anything
|
|
$response = $this->getJson('/api/historical/search?query=NonExistentQuery123456');
|
|
|
|
$response->assertStatus(200)
|
|
->assertJson([
|
|
'data' => [],
|
|
'total' => 0,
|
|
'last_page' => 1
|
|
]);
|
|
}
|
|
}
|