Updated Tests

This commit is contained in:
Mark Gregory
2026-06-11 12:41:01 +01:00
parent 99e4bba178
commit 8b9de7426a
5 changed files with 103 additions and 213 deletions
@@ -13,17 +13,6 @@ class PlatformPriceCrudTest extends TestCase
{
use RefreshDatabase;
/**
* Disable only CSRF for controller behaviour tests; auth/tenant/licence middleware still runs.
*/
protected function setUp(): void
{
parent::setUp();
$this->withoutMiddleware(\Illuminate\Foundation\Http\Middleware\ValidateCsrfToken::class);
$this->withoutMiddleware(\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken::class);
}
/**
* Ensure the customer-facing Platform pricing page only shows active prices in display order.
*/
@@ -97,7 +86,7 @@ class PlatformPriceCrudTest extends TestCase
{
$admin = User::factory()->mpbaPlatformAdmin()->create();
$response = $this->actingAs($admin)->post(route('platform.prices.store'), [
$response = $this->actingAs($admin)->withSession(['_token' => 'test-token'])->post(route('platform.prices.store'), $this->csrfPayload([
'code' => '',
'name' => 'Ultimate Plan',
'strapline' => 'Full control',
@@ -110,7 +99,7 @@ class PlatformPriceCrudTest extends TestCase
'sort_order' => 40,
'is_featured' => '1',
'is_active' => '1',
]);
]));
$response->assertRedirect(route('platform.prices.index'));
@@ -139,12 +128,13 @@ class PlatformPriceCrudTest extends TestCase
$this->actingAs($admin)
->from(route('platform.prices.create'))
->post(route('platform.prices.store'), [
->withSession(['_token' => 'test-token'])
->post(route('platform.prices.store'), $this->csrfPayload([
'name' => '',
'monthly_amount' => '-1',
'currency' => 'GB',
'sort_order' => -5,
])
]))
->assertRedirect(route('platform.prices.create'))
->assertSessionHasErrors(['name', 'monthly_amount', 'currency', 'sort_order']);
}
@@ -174,7 +164,7 @@ class PlatformPriceCrudTest extends TestCase
PlatformPrice::query()->delete();
$price = PlatformPrice::create($this->pricePayload(['code' => 'basic', 'name' => 'Basic']));
$this->actingAs($admin)->put(route('platform.prices.update', $price), [
$this->actingAs($admin)->withSession(['_token' => 'test-token'])->put(route('platform.prices.update', $price), $this->csrfPayload([
'code' => 'basic',
'name' => 'Basic Updated',
'strapline' => 'Entry level stock control',
@@ -186,7 +176,7 @@ class PlatformPriceCrudTest extends TestCase
'features' => "2 warehouses\n10 users",
'sort_order' => 5,
'is_active' => '1',
])->assertRedirect(route('platform.prices.index'));
]))->assertRedirect(route('platform.prices.index'));
$this->assertDatabaseHas('platform_prices', [
'id' => $price->id,
@@ -212,11 +202,12 @@ class PlatformPriceCrudTest extends TestCase
$this->actingAs($admin)
->from(route('platform.prices.edit', $price))
->put(route('platform.prices.update', $price), [
->withSession(['_token' => 'test-token'])
->put(route('platform.prices.update', $price), $this->csrfPayload([
...$this->requestPayload(),
'code' => 'basic',
'name' => 'Advanced',
])
]))
->assertRedirect(route('platform.prices.edit', $price))
->assertSessionHasErrors(['code']);
}
@@ -231,7 +222,8 @@ class PlatformPriceCrudTest extends TestCase
$price = PlatformPrice::create($this->pricePayload(['code' => 'legacy', 'name' => 'Legacy']));
$this->actingAs($admin)
->delete(route('platform.prices.destroy', $price))
->withSession(['_token' => 'test-token'])
->delete(route('platform.prices.destroy', $price), ['_token' => 'test-token'])
->assertRedirect(route('platform.prices.index'));
$this->assertDatabaseMissing('platform_prices', ['id' => $price->id]);
@@ -306,4 +298,25 @@ class PlatformPriceCrudTest extends TestCase
'is_active' => '1',
];
}
/**
* Return payload with a valid test CSRF token.
*
* @param array<string, mixed> $payload
* @return array<string, mixed>
*/
private function csrfPayload(array $payload = []): array
{
return ['_token' => 'test-token'] + $payload;
}
/**
* Seed the test session with the matching CSRF token.
*/
private function withCsrfSession(): self
{
$this->withSession(['_token' => 'test-token']);
return $this;
}
}