|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Neusta\Pimcore\TestingFramework\Browser; |
| 5 | + |
| 6 | +use Pimcore\Model\User; |
| 7 | +use Pimcore\Security\User\User as SecurityUser; |
| 8 | +use Pimcore\Tool\Session; |
| 9 | +use Symfony\Bundle\FrameworkBundle\KernelBrowser; |
| 10 | +use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface; |
| 11 | + |
| 12 | +class PimcoreKernelBrowser extends KernelBrowser |
| 13 | +{ |
| 14 | + public function loginToPimcoreBackend(string $username = 'admin'): void |
| 15 | + { |
| 16 | + $user = User::getByName($username); |
| 17 | + |
| 18 | + $this->setServerParameter('HTTP_X_PIMCORE_CSRF_TOKEN', 'test-csrf-token'); |
| 19 | + $this->loginUser(new SecurityUser($user), 'pimcore_admin'); |
| 20 | + $this->request('GET', '/admin/login'); |
| 21 | + |
| 22 | + Session::useBag( |
| 23 | + $this->getRequest()->getSession(), |
| 24 | + function (AttributeBagInterface $adminSession) use ($user) { |
| 25 | + $adminSession->set('user', $user); |
| 26 | + // Sign your POST requests with this CSRF token to avoid 403 responses |
| 27 | + $adminSession->set('csrfToken', 'test-csrf-token'); |
| 28 | + }, |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + public function logoutFromPimcoreBackend(): void |
| 33 | + { |
| 34 | + Session::useBag( |
| 35 | + $this->getRequest()->getSession(), |
| 36 | + function (AttributeBagInterface $adminSession) { |
| 37 | + $adminSession->set('user', null); |
| 38 | + $adminSession->set('csrfToken', null); |
| 39 | + }, |
| 40 | + ); |
| 41 | + |
| 42 | + $this->getCookieJar()->clear(); |
| 43 | + } |
| 44 | +} |
0 commit comments