public function testLoginWithExistingFrontendSession()
{
$loginFormResponse = $this->sendHttpRequest(new Request('GET', '/login', $this->getHttpHost()));
$domDocument = $loginFormResponse->toDomDocument();
$xpath = new \DOMXPath($domDocument);
$csrfDomElements = $xpath->query("//input[@name='_csrf_token']/@value");
self::assertGreaterThan(0, $csrfDomElements->length);
$csrfTokenValue = $csrfDomElements->item(0)->nodeValue;
$loginPostRequest = new FormRequest('POST', '/login', $this->getHttpHost());
$loginPostRequest->addFields(['_username' => $this->getLoginUsername(), '_password' => $this->getLoginPassword(), '_csrf_token' => $csrfTokenValue]);
$loginResponse = $this->sendHttpRequest($loginPostRequest);
if (!($sessionCookieHeader = $loginResponse->getHeader('set-cookie'))) {
self::fail('No cookie in login response');
}
list($sessionCookie) = explode(';', $sessionCookieHeader);
$request = $this->createHttpRequest('POST', '/api/ezp/v2/user/sessions', 'SessionInput+json', 'Session+json');
$this->setSessionInput($request);
$request->addHeader("Cookie: {$sessionCookie}");
$response = $this->sendHttpRequest($request);
self::assertHttpResponseCodeEquals($response, 201);
}