fixed tests

This commit is contained in:
Mark Gregory
2026-06-19 11:25:50 +01:00
parent 6d97a98c02
commit 030b1f5eb0
8 changed files with 328 additions and 312 deletions
+140 -34
View File
@@ -4,16 +4,23 @@ namespace Tests\Browser\Concerns;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Schema;
use Modules\Platform\app\Models\Company;
use Modules\Platform\app\Models\LicenseKey;
use Modules\Platform\app\Models\Payment;
use Modules\Platform\app\Models\PlatformPrice;
use Modules\Warehouse\app\Models\Warehouse;
use Modules\Warehouse\app\Models\WarehouseValidationRule;
trait InteractsWithWmsData
{
protected string $browserPassword = '987400Kmh';
/**
* Create or update the MPBA platform administrator used by browser tests.
*
* @param array<string, mixed> $overrides
*/
protected function platformAdminUser(array $overrides = []): User
{
$company = Company::query()->updateOrCreate(
@@ -27,8 +34,11 @@ trait InteractsWithWmsData
],
);
$email = $overrides['email'] ?? 'platform.admin@example.test';
unset($overrides['email']);
return User::query()->updateOrCreate(
['email' => $overrides['email'] ?? 'platform.admin@example.test'],
['email' => $email],
[
'company_id' => $company->id,
'name' => $overrides['name'] ?? 'Platform Admin',
@@ -42,10 +52,16 @@ trait InteractsWithWmsData
);
}
/**
* @param array<string, mixed> $overrides
*/
protected function tenantCompany(array $overrides = []): Company
{
$slug = $overrides['slug'] ?? 'test-company';
unset($overrides['slug']);
$company = Company::query()->updateOrCreate(
['slug' => $overrides['slug'] ?? 'test-company'],
['slug' => $slug],
[
'name' => $overrides['name'] ?? 'Test Company',
'status' => $overrides['status'] ?? 'active',
@@ -62,12 +78,19 @@ trait InteractsWithWmsData
return $company->refresh();
}
/**
* @param array<string, mixed> $overrides
*/
protected function tenantUser(array $overrides = []): User
{
$company = $this->tenantCompany();
$company = $overrides['company'] ?? $this->tenantCompany();
unset($overrides['company']);
$email = $overrides['email'] ?? 'tenant.user@example.test';
unset($overrides['email']);
return User::query()->updateOrCreate(
['email' => $overrides['email'] ?? 'tenant.user@example.test'],
['email' => $email],
[
'company_id' => $company->id,
'name' => $overrides['name'] ?? 'Tenant User',
@@ -90,19 +113,7 @@ trait InteractsWithWmsData
] as [$code, $name, $strapline, $description, $monthly, $annual, $sortOrder, $featured, $active, $features]) {
PlatformPrice::query()->updateOrCreate(
['code' => $code],
[
'name' => $name,
'strapline' => $strapline,
'description' => $description,
'monthly_amount' => $monthly,
'annual_amount' => $annual,
'currency' => 'gbp',
'badge_label' => $featured ? 'Popular' : null,
'features' => $features,
'sort_order' => $sortOrder,
'is_featured' => $featured,
'is_active' => $active,
],
$this->platformPriceAttributes($name, $strapline, $description, $monthly, $annual, $sortOrder, $featured, $active, $features),
);
}
}
@@ -116,25 +127,10 @@ trait InteractsWithWmsData
'company_id' => $company->id,
'code' => $code,
],
[
'name' => $name,
'address' => 'Main Road',
'timezone' => 'Europe/London',
],
$this->warehouseAttributes($name),
);
$warehouse->validationRules()->updateOrCreate(
[
'rule_key' => 'batch-required',
],
[
'name' => 'Require batch number',
'severity' => 'error',
'configuration' => [
'field' => 'batch_number',
],
],
);
$this->seedWarehouseValidationRule($warehouse, 'Require batch number', 'batch-required');
return $warehouse->refresh();
}
@@ -162,4 +158,114 @@ trait InteractsWithWmsData
],
);
}
/**
* @param array<int, string> $features
* @return array<string, mixed>
*/
private function platformPriceAttributes(
string $name,
string $strapline,
string $description,
int $monthly,
int $annual,
int $sortOrder,
bool $featured,
bool $active,
array $features,
): array {
$attributes = [
'name' => $name,
'strapline' => $strapline,
'description' => $description,
'monthly_amount' => $monthly,
'currency' => 'gbp',
'badge_label' => $featured ? 'Popular' : null,
'features' => $features,
'sort_order' => $sortOrder,
'is_featured' => $featured,
];
if (Schema::hasColumn('platform_prices', 'annual_amount')) {
$attributes['annual_amount'] = $annual;
}
if (Schema::hasColumn('platform_prices', 'yearly_amount')) {
$attributes['yearly_amount'] = $annual;
}
if (Schema::hasColumn('platform_prices', 'is_active')) {
$attributes['is_active'] = $active;
}
if (Schema::hasColumn('platform_prices', 'active')) {
$attributes['active'] = $active;
}
return $attributes;
}
/**
* @return array<string, mixed>
*/
private function warehouseAttributes(string $name): array
{
$attributes = [
'name' => $name,
'timezone' => 'Europe/London',
];
if (Schema::hasColumn('warehouses', 'address')) {
$attributes['address'] = 'Main Road';
}
if (Schema::hasColumn('warehouses', 'status')) {
$attributes['status'] = 'active';
}
return $attributes;
}
private function seedWarehouseValidationRule(Warehouse $warehouse, string $name, string $key): void
{
$attributes = [
'warehouse_id' => $warehouse->id,
];
if (Schema::hasColumn('warehouse_validation_rules', 'company_id')) {
$attributes['company_id'] = $warehouse->company_id;
}
if (Schema::hasColumn('warehouse_validation_rules', 'rule_key')) {
$attributes['rule_key'] = $key;
}
if (Schema::hasColumn('warehouse_validation_rules', 'key')) {
$attributes['key'] = $key;
}
$values = ['name' => $name];
if (Schema::hasColumn('warehouse_validation_rules', 'severity')) {
$values['severity'] = 'error';
}
if (Schema::hasColumn('warehouse_validation_rules', 'rule_type')) {
$values['rule_type'] = 'validation';
}
if (Schema::hasColumn('warehouse_validation_rules', 'value')) {
$values['value'] = 'required';
}
if (Schema::hasColumn('warehouse_validation_rules', 'configuration')) {
$values['configuration'] = ['field' => 'batch_number'];
}
if (Schema::hasColumn('warehouse_validation_rules', 'is_active')) {
$values['is_active'] = true;
}
WarehouseValidationRule::query()->updateOrCreate($attributes, $values);
}
}
@@ -2,25 +2,28 @@
namespace Tests\Browser\Layout;
use App\Models\User;
use Laravel\Dusk\Browser;
use Tests\Browser\Concerns\InteractsWithWmsData;
use Tests\DuskTestCase;
class FixedSidebarMainScrollTest extends DuskTestCase
{
use InteractsWithWmsData;
/**
* The document must not scroll; only the right-hand main content area may scroll.
*/
public function test_document_does_not_scroll_when_main_content_scrolls(): void
{
$user = User::factory()->create();
$user = $this->tenantUser();
$this->seedWarehouse($user->company);
$this->browse(function (Browser $browser) use ($user): void {
$browser
->loginAs($user)
->visit('/dashboard')
->waitFor('[data-test="desktop-sidebar"]')
->waitFor('[data-test="main-content"]');
->waitFor('[data-test="desktop-sidebar"]', 10)
->waitFor('[data-test="main-content"]', 10);
$browser->script(<<<'JS'
const main = document.querySelector('[data-test="main-content"]');
@@ -30,7 +33,6 @@ class FixedSidebarMainScrollTest extends DuskTestCase
filler.style.height = '2600px';
main.appendChild(filler);
window.scrollTo(0, 800);
document.documentElement.scrollTop = 800;
document.body.scrollTop = 800;
@@ -42,11 +44,11 @@ class FixedSidebarMainScrollTest extends DuskTestCase
)[0];
$mainScrollTop = $browser->script(
'return Math.round(document.querySelector("[data-test=\"main-content\"]").scrollTop);'
'return Math.round(document.querySelector("[data-test=\\"main-content\\"]").scrollTop);'
)[0];
$sidebarTop = $browser->script(
'return Math.round(document.querySelector("[data-test=\"desktop-sidebar\"]").getBoundingClientRect().top);'
'return Math.round(document.querySelector("[data-test=\\"desktop-sidebar\\"]").getBoundingClientRect().top);'
)[0];
$this->assertSame(0, $documentScrollTop);
@@ -2,25 +2,28 @@
namespace Tests\Browser\Layout;
use App\Models\User;
use Laravel\Dusk\Browser;
use Tests\Browser\Concerns\InteractsWithWmsData;
use Tests\DuskTestCase;
class FixedSidebarScrollTest extends DuskTestCase
{
use InteractsWithWmsData;
/**
* Ensure only the main content area scrolls while the desktop sidebar stays fixed.
*/
public function test_only_main_content_area_scrolls_while_sidebar_remains_fixed(): void
{
$user = User::factory()->create();
$user = $this->tenantUser();
$this->seedWarehouse($user->company);
$this->browse(function (Browser $browser) use ($user): void {
$browser
->loginAs($user)
->visit('/dashboard')
->waitFor('[data-test="desktop-sidebar"]')
->waitFor('[data-test="main-content"]');
->waitFor('[data-test="desktop-sidebar"]', 10)
->waitFor('[data-test="main-content"]', 10);
$browser->script(<<<'JS'
const main = document.querySelector('[data-test="main-content"]');
@@ -34,7 +37,7 @@ class FixedSidebarScrollTest extends DuskTestCase
JS);
$sidebarTop = $browser->script(
'return Math.round(document.querySelector("[data-test=\"desktop-sidebar\"]").getBoundingClientRect().top);'
'return Math.round(document.querySelector("[data-test=\\"desktop-sidebar\\"]").getBoundingClientRect().top);'
)[0];
$bodyScrollTop = $browser->script(
@@ -42,7 +45,7 @@ class FixedSidebarScrollTest extends DuskTestCase
)[0];
$mainScrollTop = $browser->script(
'return Math.round(document.querySelector("[data-test=\"main-content\"]").scrollTop);'
'return Math.round(document.querySelector("[data-test=\\"main-content\\"]").scrollTop);'
)[0];
$this->assertSame(0, $sidebarTop);
+2 -13
View File
@@ -2,26 +2,15 @@
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ExampleTest extends TestCase
{
use RefreshDatabase;
/**
* Ensure the public home route renders successfully.
*/
public function test_home_route_returns_successful_response(): void
public function test_home_route_redirects_guests_to_login(): void
{
$this->get(route('home'))
->assertOk()
->assertSee('Laravel', false);
$this->get(route('home'))->assertRedirect(route('login'));
}
/**
* Ensure the health endpoint is available for deployment checks.
*/
public function test_health_endpoint_returns_successful_response(): void
{
$this->get('/up')->assertOk();
@@ -5,6 +5,7 @@ namespace Tests\Feature\Modules;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Route;
use Modules\Platform\app\Models\PlatformPrice;
use PHPUnit\Framework\Attributes\DataProvider;
use Tests\TestCase;
@@ -49,6 +50,22 @@ class NavigatorHeaderCoverageTest extends TestCase
{
$user = User::factory()->mpbaPlatformAdmin()->create();
PlatformPrice::query()->updateOrCreate(
['code' => 'basic'],
[
'name' => 'Basic',
'strapline' => 'Starter WMS platform access.',
'description' => 'Core warehouse management tools.',
'monthly_amount' => 4900,
'annual_amount' => 49000,
'currency' => 'gbp',
'features' => ['2 warehouses', '10 users', 'Standard support'],
'sort_order' => 10,
'is_active' => true,
'is_featured' => false,
],
);
$this->actingAs($user)
->get(route('platform.dashboard'))
->assertOk()
+15 -2
View File
@@ -20,11 +20,24 @@ abstract class TestCase extends BaseTestCase
'mail.default' => 'array',
]);
$this->withoutMiddleware(ValidateCsrfToken::class);
/**
* Feature tests post directly to web routes. Keep CSRF enabled in the
* application, but exclude it in PHPUnit so tests exercise validation,
* authorization, tenancy and persistence instead of failing with 419.
*/
if (class_exists(ValidateCsrfToken::class)) {
ValidateCsrfToken::except(['*']);
$this->withoutMiddleware(ValidateCsrfToken::class);
}
if (class_exists(\App\Http\Middleware\VerifyCsrfToken::class)) {
\App\Http\Middleware\VerifyCsrfToken::except(['*']);
$this->withoutMiddleware(\App\Http\Middleware\VerifyCsrfToken::class);
}
}
/**
* POST with a real CSRF token for Fortify/web form tests.
* POST with a real CSRF token for tests that intentionally verify web-form behaviour.
*
* @param array<string, mixed> $data
*/