115 lines
4.1 KiB
PHP
115 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\Admin;
|
|
use App\Models\Bank;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
use Spatie\Permission\Models\Permission;
|
|
use Spatie\Permission\Models\Role;
|
|
use Spatie\Permission\PermissionRegistrar;
|
|
use Tests\TestCase;
|
|
|
|
/**
|
|
* Admin management links are grouped into the Editorial and Administration dropdowns;
|
|
* other profiles keep their plain links.
|
|
*/
|
|
class NavigationGroupsTest extends TestCase
|
|
{
|
|
use DatabaseTransactions;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
// phpunit.xml configures Redis, which is the dev site's cache too
|
|
config(['cache.default' => 'array']);
|
|
app(PermissionRegistrar::class)->forgetCachedPermissions();
|
|
}
|
|
|
|
private function actAsAdmin(array $permissions): void
|
|
{
|
|
$user = User::factory()->create();
|
|
$admin = Admin::factory()->create();
|
|
$admin->users()->attach($user);
|
|
|
|
$role = Role::firstOrCreate(['name' => "Admin\\{$admin->id}\\nav-test-role", 'guard_name' => 'web']);
|
|
foreach ($permissions as $permission) {
|
|
$role->givePermissionTo(Permission::firstOrCreate(['name' => $permission, 'guard_name' => 'web']));
|
|
}
|
|
$user->assignRole($role);
|
|
|
|
$this->actingAs($user, 'web');
|
|
$this->actingAs($admin, 'admin');
|
|
session(['activeProfileType' => Admin::class, 'activeProfileId' => $admin->id, 'active_guard' => 'admin']);
|
|
}
|
|
|
|
private function navigation(): string
|
|
{
|
|
return view('navigation-menu')->render();
|
|
}
|
|
|
|
/** Text of the desktop dropdown panel for a group (between its label and the panel end). */
|
|
private function group(string $html, string $label): ?string
|
|
{
|
|
if (!preg_match('/' . preg_quote($label, '/') . '\s*<svg.*?<template x-teleport="body">(.*?)<\/template>/s', $html, $match)) {
|
|
return null;
|
|
}
|
|
|
|
return trim(preg_replace('/\s+/', ' ', strip_tags($match[1])));
|
|
}
|
|
|
|
public function test_admin_with_all_permissions_gets_both_groups(): void
|
|
{
|
|
$this->actAsAdmin(['manage posts', 'manage polls', 'manage mailings', 'manage categories', 'manage tags', 'manage profiles', 'manage roles']);
|
|
|
|
$html = $this->navigation();
|
|
|
|
$this->assertSame('Posts Polls Calls Mailings Categories Tags', $this->group($html, 'Editorial'));
|
|
$this->assertSame('Profiles Roles', $this->group($html, 'Administration'));
|
|
}
|
|
|
|
public function test_hidden_links_and_empty_groups_are_left_out(): void
|
|
{
|
|
$this->actAsAdmin(['manage posts']);
|
|
|
|
$html = $this->navigation();
|
|
|
|
// Polls have their own permission now
|
|
$this->assertSame('Posts Calls', $this->group($html, 'Editorial'));
|
|
$this->assertNull($this->group($html, 'Administration'));
|
|
$this->assertStringNotContainsString(route('roles.manage'), $html);
|
|
$this->assertStringNotContainsString(route('mailings.index'), $html);
|
|
}
|
|
|
|
public function test_user_profile_keeps_plain_links(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
$this->actingAs($user, 'web');
|
|
session(['activeProfileType' => User::class, 'activeProfileId' => $user->id, 'active_guard' => 'web']);
|
|
|
|
$html = $this->navigation();
|
|
|
|
$this->assertNull($this->group($html, 'Editorial'));
|
|
$this->assertStringContainsString(route('calls.manage'), $html);
|
|
$this->assertStringNotContainsString(route('profiles.manage'), $html);
|
|
}
|
|
|
|
public function test_central_bank_keeps_plain_links(): void
|
|
{
|
|
$user = User::factory()->create();
|
|
$bank = Bank::factory()->create(['level' => 0]);
|
|
$bank->managers()->attach($user);
|
|
$this->actingAs($user, 'web');
|
|
$this->actingAs($bank, 'bank');
|
|
session(['activeProfileType' => Bank::class, 'activeProfileId' => $bank->id, 'active_guard' => 'bank']);
|
|
|
|
$html = $this->navigation();
|
|
|
|
$this->assertNull($this->group($html, 'Administration'));
|
|
$this->assertStringContainsString(route('profiles.manage'), $html);
|
|
$this->assertStringContainsString(route('calls.manage'), $html);
|
|
}
|
|
}
|