feat: add a Mein Account group to the main menu and lock the page while the drawer is open
This commit is contained in:
1 parent
4530d2434b
commit
8a02b28d00
6 files changed
+164
-24
No files matched your search
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Menu;
|
||||
|
||||
use App\Entity\Teamer;
|
||||
use App\Entity\User;
|
||||
use App\Menu\MenuBuilder;
|
||||
use Knp\Menu\ItemInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\HttpFoundation\Session\Session;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
|
||||
|
||||
/**
|
||||
* The account items (profile, area switch, logout) close the main menu as one group, so they are
|
||||
* reachable from the sidebar and from the drawer on small screens, not only from the header.
|
||||
*/
|
||||
class MainMenuAccountGroupTest extends KernelTestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
self::bootKernel();
|
||||
}
|
||||
|
||||
public function testTheGroupClosesTheMenuWithProfileAreaSwitchAndLogout(): void
|
||||
{
|
||||
$this->login($this->user(['ROLE_TEAM_ADMIN', 'ROLE_TEAMER']), 'app_admin_index');
|
||||
|
||||
$account = $this->accountGroup();
|
||||
|
||||
self::assertSame('Mein Account', $account->getLabel());
|
||||
self::assertNull($account->getUri());
|
||||
self::assertSame(['Mein Profil', 'zum Teambereich', 'Logout'], array_keys($account->getChildren()));
|
||||
self::assertSame('/administrative/profile', $account->getChild('Mein Profil')->getUri());
|
||||
}
|
||||
|
||||
public function testTheSubItemsCarryNoIcons(): void
|
||||
{
|
||||
$this->login($this->user(['ROLE_TEAM_ADMIN', 'ROLE_TEAMER']), 'app_admin_index');
|
||||
|
||||
foreach ($this->accountGroup()->getChildren() as $child) {
|
||||
self::assertArrayNotHasKey('icon', $child->getExtras(), $child->getName());
|
||||
}
|
||||
}
|
||||
|
||||
public function testTheTeamerAreaKeepsItsOwnProfileGroup(): void
|
||||
{
|
||||
$user = $this->user(['ROLE_TEAMER']);
|
||||
$user->setTeamer(new Teamer());
|
||||
$this->login($user, 'app_teamer_index');
|
||||
|
||||
self::assertSame(['Logout'], array_keys($this->accountGroup()->getChildren()));
|
||||
}
|
||||
|
||||
public function testImpersonationOffersTheWayBackInsteadOfLogout(): void
|
||||
{
|
||||
$admin = $this->user(['ROLE_TEAM_ADMIN']);
|
||||
$manager = $this->user(['ROLE_MANAGER']);
|
||||
$original = new UsernamePasswordToken($admin, 'main', $admin->getRoles());
|
||||
$this->login($manager, 'app_manager_index', new SwitchUserToken($manager, 'main', $manager->getRoles(), $original));
|
||||
|
||||
$children = array_keys($this->accountGroup()->getChildren());
|
||||
|
||||
self::assertContains('zum eigenen Benutzer', $children);
|
||||
self::assertNotContains('Logout', $children);
|
||||
}
|
||||
|
||||
private function accountGroup(): ItemInterface
|
||||
{
|
||||
$menu = self::getContainer()->get(MenuBuilder::class)->createMainMenu([]);
|
||||
$account = $menu->getChild('account');
|
||||
|
||||
self::assertNotNull($account);
|
||||
self::assertSame($account, $menu->getLastChild());
|
||||
|
||||
return $account;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<string> $roles
|
||||
*/
|
||||
private function user(array $roles): User
|
||||
{
|
||||
return new User()
|
||||
->setFirstName('Ada')
|
||||
->setEmail('[email protected]')
|
||||
->setRoles($roles);
|
||||
}
|
||||
|
||||
private function login(User $user, string $route, ?UsernamePasswordToken $token = null): void
|
||||
{
|
||||
self::getContainer()->get(TokenStorageInterface::class)
|
||||
->setToken($token ?? new UsernamePasswordToken($user, 'main', $user->getRoles()));
|
||||
|
||||
$request = Request::create('/');
|
||||
$request->attributes->set('_route', $route);
|
||||
$request->setSession(new Session(new MockArraySessionStorage()));
|
||||
self::getContainer()->get(RequestStack::class)->push($request);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user