Skip to content

Commit 96a19cf

Browse files
committed
Add helper for Pimcore backend login
Introduce a custom PimcoreKernelBrowser to streamline login/logout in Pimcore backend tests.
1 parent adbbfaf commit 96a19cf

File tree

7 files changed

+116
-1
lines changed

7 files changed

+116
-1
lines changed

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"symfony/event-dispatcher": "^5.4 || ^6.4",
3131
"symfony/filesystem": "^5.4 || ^6.4",
3232
"symfony/framework-bundle": "^5.4 || ^6.4",
33+
"symfony/http-foundation": "^5.4 || ^6.4",
3334
"symfony/http-kernel": "^5.4 || ^6.4"
3435
},
3536
"require-dev": {
@@ -43,7 +44,8 @@
4344
"phpstan/phpstan": "^1.10.60",
4445
"phpstan/phpstan-phpunit": "^1.3.16",
4546
"phpstan/phpstan-symfony": "^1.3.8",
46-
"shipmonk/composer-dependency-analyser": "^1.7"
47+
"shipmonk/composer-dependency-analyser": "^1.7",
48+
"symfony/security-core": "^5.4 || ^6.4"
4749
},
4850
"suggest": {
4951
"dama/doctrine-test-bundle": "To isolate database tests in transactions and improve test performance",
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
}

src/Kernel/TestKernel.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace Neusta\Pimcore\TestingFramework\Kernel;
55

6+
use Neusta\Pimcore\TestingFramework\Browser\PimcoreKernelBrowser;
67
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
78
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
89
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -103,6 +104,15 @@ protected function buildContainer(): ContainerBuilder
103104
{
104105
$container = parent::buildContainer();
105106

107+
$container->addCompilerPass(new class implements CompilerPassInterface {
108+
public function process(ContainerBuilder $container): void
109+
{
110+
if ($container->has('test.client')) {
111+
$container->findDefinition('test.client')->setClass(PimcoreKernelBrowser::class);
112+
}
113+
}
114+
});
115+
106116
foreach ($this->testCompilerPasses as $compilerPass) {
107117
$container->addCompilerPass(...$compilerPass);
108118
}

src/WebTestCase.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Neusta\Pimcore\TestingFramework;
5+
6+
use Neusta\Pimcore\TestingFramework\Browser\PimcoreKernelBrowser;
7+
8+
/**
9+
* @method static PimcoreKernelBrowser createClient(array $options = [], array $server = [])
10+
*/
11+
class WebTestCase extends \Pimcore\Test\WebTestCase
12+
{
13+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Neusta\Pimcore\TestingFramework\Tests\Functional;
5+
6+
use Neusta\Pimcore\TestingFramework\Database\ResetDatabase;
7+
use Neusta\Pimcore\TestingFramework\WebTestCase;
8+
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
9+
10+
class PimcoreKernelBrowserTest extends WebTestCase
11+
{
12+
use ResetDatabase;
13+
14+
/**
15+
* @test
16+
*/
17+
public function it_allows_logging_in_and_out_from_pimcore_backend(): void
18+
{
19+
$client = static::createClient();
20+
$client->catchExceptions(false);
21+
22+
// Access should be denied before login
23+
try {
24+
$client->request('GET', '/admin/user/get-minimal');
25+
$this->fail('AccessDeniedException expected.');
26+
} catch (AccessDeniedException) {
27+
}
28+
29+
// Access should be granted after login
30+
$client->loginToPimcoreBackend();
31+
$client->request('GET', '/admin/user/get-minimal');
32+
static::assertResponseIsSuccessful();
33+
34+
// Access should be denied again after logout
35+
$client->logoutFromPimcoreBackend();
36+
try {
37+
$client->request('GET', '/admin/user/get-minimal');
38+
$this->fail('AccessDeniedException expected.');
39+
} catch (AccessDeniedException) {
40+
}
41+
}
42+
}

tests/app/config/routes.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# auto loads routes from all bundles in config/pimcore/routing.yaml
2+
_pimcore_bundle_auto:
3+
resource: .
4+
type: pimcore_bundle

tests/app/templates/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)