Skip to content

[FrameworkBundle] Prepare session in functionnal tests #61110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CHANGELOG
* Allow using their name without added suffix when using `#[Target]` for custom services
* Deprecate `Symfony\Bundle\FrameworkBundle\Console\Application::add()` in favor of `Symfony\Bundle\FrameworkBundle\Console\Application::addCommand()`
* Add `assertEmailAddressNotContains()` to the `MailerAssertionsTrait`
* Add `KernelBrowser::getSession()` method

7.3
---
Expand Down
40 changes: 32 additions & 8 deletions src/Symfony/Bundle/FrameworkBundle/KernelBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\HttpKernelBrowser;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\HttpKernel\Profiler\Profile as HttpProfile;
Expand Down Expand Up @@ -63,6 +64,35 @@ public function getProfile(): HttpProfile|false|null
return $this->getContainer()->get('profiler')->loadProfileFromResponse($this->response);
}

public function getSession(): ?SessionInterface
{
$container = $this->getContainer();

if (!$container->has('session.factory')) {
return null;
}

$session = $container->get('session.factory')->createSession();

$cookieJar = $this->getCookieJar();
$cookie = $cookieJar->get($session->getName());

if ($cookie instanceof Cookie) {
$session->setId($cookie->getValue());
}

$session->start();

if (!$cookie instanceof Cookie) {
$domains = array_unique(array_map(fn (Cookie $cookie) => $cookie->getName() === $session->getName() ? $cookie->getDomain() : '', $cookieJar->all())) ?: [''];
foreach ($domains as $domain) {
$cookieJar->set(new Cookie($session->getName(), $session->getId(), domain: $domain));
}
}

return $session;
}

/**
* Enables the profiler for the very next request.
*
Expand Down Expand Up @@ -116,20 +146,14 @@ public function loginUser(object $user, string $firewallContext = 'main', array
$container = $this->getContainer();
$container->get('security.untracked_token_storage')->setToken($token);

if (!$container->has('session.factory')) {
$session = $this->getSession();
if (!$session instanceof SessionInterface) {
return $this;
}

$session = $container->get('session.factory')->createSession();
$session->set('_security_'.$firewallContext, serialize($token));
$session->save();

$domains = array_unique(array_map(fn (Cookie $cookie) => $cookie->getName() === $session->getName() ? $cookie->getDomain() : '', $this->getCookieJar()->all())) ?: [''];
foreach ($domains as $domain) {
$cookie = new Cookie($session->getName(), $session->getId(), null, null, $domain);
$this->getCookieJar()->set($cookie);
}

return $this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ public function testWelcome($config, $insulate)
// prove cleared session
$crawler = $client->request('GET', '/session');
$this->assertStringContainsString('You are new here and gave no name.', $crawler->text());

// prepare session programmatically
$session = $client->getSession();
$session->set('name', 'drak');
$session->save();

// ensure session can be saved multiple times without being reset
$session = $client->getSession();
$session->set('foo', 'bar');
$session->save();

// prove remembered name from programmatically prepared session
$crawler = $client->request('GET', '/session');
$this->assertStringContainsString('Welcome back drak, nice to meet you.', $crawler->text());
}

/**
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy