77 lines
3.0 KiB
PHP
77 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Menu;
|
|
|
|
use App\Domains\Core\Auth\Models\User;
|
|
use App\Http\Middleware\EnsureTenantHasMenu;
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Route;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
|
use Tests\TestCase;
|
|
|
|
class TicketMenuAccessTest extends TestCase
|
|
{
|
|
public static function menuAssignments(): array
|
|
{
|
|
return [
|
|
'OnTicket' => ['onticket.adminapp.tickets', 'current', true],
|
|
'old code' => ['adminapp.tickets', 'current', false],
|
|
'another tenant' => ['onticket.adminapp.tickets', 'other', false],
|
|
'unrelated menu' => ['adminapp.ventas', 'current', false],
|
|
];
|
|
}
|
|
|
|
#[DataProvider('menuAssignments')]
|
|
public function test_ticket_menu_requires_an_association_with_the_authenticated_tenant(string $menuCode, string $assignedTenant, bool $allowed): void
|
|
{
|
|
Schema::create('tenants', function (Blueprint $table): void {
|
|
$table->id();
|
|
$table->string('codigo');
|
|
});
|
|
Schema::create('menues', function (Blueprint $table): void {
|
|
$table->id();
|
|
$table->string('code');
|
|
});
|
|
Schema::create('tenants_menues', function (Blueprint $table): void {
|
|
$table->string('tenant_code');
|
|
$table->string('menu_code');
|
|
});
|
|
DB::table('tenants')->insert(['codigo' => 'current']);
|
|
DB::table('menues')->insert(['code' => $menuCode]);
|
|
DB::table('tenants_menues')->insert(['tenant_code' => $assignedTenant, 'menu_code' => $menuCode]);
|
|
|
|
$user = new User(['tenant_codigo' => 'current']);
|
|
$request = Request::create('/api/v1/adminapp/tenant/tickets');
|
|
$request->setUserResolver(fn () => $user);
|
|
if (! $allowed) {
|
|
$this->expectException(HttpException::class);
|
|
$this->expectExceptionCode(0);
|
|
}
|
|
|
|
try {
|
|
$response = (new EnsureTenantHasMenu)->handle($request, fn () => response('allowed'), 'onticket.adminapp.tickets');
|
|
$this->assertSame('allowed', $response->getContent());
|
|
} catch (HttpException $exception) {
|
|
$this->assertSame(404, $exception->getStatusCode());
|
|
throw $exception;
|
|
}
|
|
}
|
|
|
|
public function test_all_ticket_routes_and_filter_form_use_the_updated_menu_codes(): void
|
|
{
|
|
foreach ([
|
|
'adminapp.tickets.index', 'adminapp.tickets.cancel',
|
|
'adminapp.tickets.calculate-refund', 'adminapp.tickets.refund',
|
|
'adminapp.tickets.pdf', 'adminapp.tickets.excel', 'adminapp.forms.tickets-filter',
|
|
] as $name) {
|
|
$route = Route::getRoutes()->getByName($name);
|
|
$this->assertNotNull($route);
|
|
$this->assertContains('tenant.menu:onticket.adminapp.tickets', $route->gatherMiddleware());
|
|
}
|
|
}
|
|
}
|