lernschiff/tests/Feature/Livewire/NoNestedButtonsTest.php

95 lines
2.9 KiB
PHP

<?php
use App\Livewire\Dashboard\Index as DashboardIndex;
use App\Livewire\Notifications\Index as NotificationsIndex;
use App\Livewire\PhotoHelp\Index as PhotoHelpIndex;
use App\Livewire\Progress\Index as ProgressIndex;
use App\Livewire\Rewards\Index as RewardsIndex;
use App\Livewire\Settings\Index as SettingsIndex;
use App\Livewire\Subjects\Index as SubjectsIndex;
use App\Livewire\Tasks\Index as TasksIndex;
use App\Models\License;
use App\Models\Role;
use App\Models\Tenant;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Livewire\Livewire;
uses(\Illuminate\Foundation\Testing\RefreshDatabase::class);
beforeEach(function () {
$this->seed(\Database\Seeders\RolesSeeder::class);
$this->tenant = Tenant::firstOrCreate(
['name' => 'Test Schule'],
['type' => 'school', 'active' => true]
);
$this->user = User::withoutGlobalScopes()->create([
'name' => 'Lina Test',
'email' => 'lina@nested.test',
'tenant_id' => $this->tenant->id,
'password' => bcrypt('password'),
]);
$childRole = Role::where('name', 'child')->first();
DB::table('role_user')->insert([
'user_id' => $this->user->id,
'role_id' => $childRole->id,
'created_at' => now(),
'updated_at' => now(),
]);
$license = License::withoutGlobalScopes()->create([
'tenant_id' => $this->tenant->id,
'type' => 'school_flat',
'seats' => null,
'expires_at' => now()->addYear(),
'active' => true,
]);
DB::table('license_assignments')->insert([
'license_id' => $license->id,
'user_id' => $this->user->id,
'created_at' => now(),
'updated_at' => now(),
]);
});
function assertNoNestedButtons(string $html, string $page): void
{
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML('<?xml encoding="utf-8"?><div>' . $html . '</div>', LIBXML_NOERROR | LIBXML_NOWARNING);
libxml_clear_errors();
$xpath = new DOMXPath($doc);
$nestedButtons = $xpath->query('//button//button');
$count = $nestedButtons === false ? 0 : $nestedButtons->length;
expect($count)->toBe(
0,
"Page '{$page}' must not contain <button> inside <button> (invalid HTML). Found {$count}."
);
}
$pages = [
'Dashboard' => DashboardIndex::class,
'Subjects' => SubjectsIndex::class,
'Tasks' => TasksIndex::class,
'PhotoHelp' => PhotoHelpIndex::class,
'Rewards' => RewardsIndex::class,
'Progress' => ProgressIndex::class,
'Notifications' => NotificationsIndex::class,
'Settings' => SettingsIndex::class,
];
foreach ($pages as $name => $class) {
it("{$name} page renders without nested <button> elements", function () use ($class, $name) {
$this->actingAs($this->user);
$html = Livewire::test($class)->html();
assertNoNestedButtons($html, $name);
});
}