2022-05-17 09:54:09 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace DigiComp\FlowSessionLock\Tests\Functional;
|
|
|
|
|
|
|
|
use DigiComp\FlowSessionLock\Tests\Functional\Fixtures\Controller\ExampleController;
|
|
|
|
use GuzzleHttp\Psr7\Uri;
|
2022-08-01 00:17:55 +02:00
|
|
|
use Neos\Flow\Composer\ComposerUtility;
|
2022-05-17 09:54:09 +02:00
|
|
|
use Neos\Flow\Http\Cookie;
|
|
|
|
use Neos\Flow\Mvc\Routing\Route;
|
|
|
|
use Neos\Flow\Tests\FunctionalTestCase;
|
|
|
|
use Psr\Http\Message\ServerRequestFactoryInterface;
|
|
|
|
|
|
|
|
class SessionLockRequestComponentTest extends FunctionalTestCase
|
|
|
|
{
|
|
|
|
protected ServerRequestFactoryInterface $serverRequestFactory;
|
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
$this->serverRequestFactory = $this->objectManager->get(ServerRequestFactoryInterface::class);
|
|
|
|
$route = new Route();
|
|
|
|
$route->setName('Functional Test - SessionRequestComponent::Restricted');
|
|
|
|
$route->setUriPattern('test/sessionlock/{@action}');
|
|
|
|
$route->setDefaults([
|
|
|
|
'@package' => 'DigiComp.FlowSessionLock',
|
|
|
|
'@subpackage' => 'Tests\Functional\Fixtures',
|
|
|
|
'@controller' => 'Example',
|
|
|
|
'@action' => 'protected',
|
|
|
|
'@format' => 'html',
|
|
|
|
]);
|
|
|
|
$route->setAppendExceedingArguments(true);
|
|
|
|
$this->router->addRoute($route);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function expectedDuration(): array
|
|
|
|
{
|
|
|
|
$parallelChecker = function ($allRequests, $oneRequest) {
|
|
|
|
self::assertGreaterThan(ExampleController::CONTROLLER_TIME, $oneRequest * 1000);
|
|
|
|
self::assertLessThan(ExampleController::CONTROLLER_TIME * 4, $allRequests * 1000);
|
|
|
|
};
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'http://localhost/test/sessionlock/protected',
|
|
|
|
function ($allRequests, $oneRequest) {
|
|
|
|
self::assertGreaterThan(ExampleController::CONTROLLER_TIME, $oneRequest * 1000);
|
|
|
|
self::assertGreaterThan(ExampleController::CONTROLLER_TIME * 4, $allRequests * 1000);
|
2022-07-31 23:51:35 +02:00
|
|
|
},
|
2022-05-17 09:54:09 +02:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'http://localhost/test/sessionlock/unprotectedbyannotation',
|
2022-07-31 23:51:35 +02:00
|
|
|
$parallelChecker,
|
2022-05-17 09:54:09 +02:00
|
|
|
],
|
|
|
|
[
|
|
|
|
'http://localhost/test/sessionlock/unprotectedbyconfiguration',
|
2022-07-31 23:51:35 +02:00
|
|
|
$parallelChecker,
|
|
|
|
],
|
2022-05-17 09:54:09 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider expectedDuration
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function itDoesNotAllowToEnterMoreThanOneWithTheSameSession(string $url, \Closure $checker): void
|
|
|
|
{
|
2022-08-01 00:17:55 +02:00
|
|
|
// Functional tests are currently broken, until a version containing
|
|
|
|
// https://github.com/neos/flow-development-collection/commit/bebfc4e6566bc4ba2ba28330344105adb2d6ada0
|
|
|
|
// gets released
|
2022-05-17 09:54:09 +02:00
|
|
|
$request = $this->serverRequestFactory
|
|
|
|
->createServerRequest('GET', new Uri($url));
|
2022-07-31 23:51:35 +02:00
|
|
|
$start = \microtime(true);
|
2022-05-17 09:54:09 +02:00
|
|
|
$response = $this->browser->sendRequest($request);
|
2022-07-31 23:51:35 +02:00
|
|
|
$neededForOne = \microtime(true) - $start;
|
2022-05-17 09:54:09 +02:00
|
|
|
|
2022-07-31 23:51:35 +02:00
|
|
|
$sessionCookies = \array_map(static function ($cookie) {
|
2022-05-17 09:54:09 +02:00
|
|
|
return Cookie::createFromRawSetCookieHeader($cookie);
|
|
|
|
}, $response->getHeader('Set-Cookie'));
|
|
|
|
self::assertNotEmpty($sessionCookies);
|
|
|
|
|
2022-07-31 23:51:35 +02:00
|
|
|
$cookies = \array_reduce($sessionCookies, static function ($out, $cookie) {
|
2022-05-17 09:54:09 +02:00
|
|
|
$out[$cookie->getName()] = $cookie->getValue();
|
|
|
|
return $out;
|
|
|
|
}, []);
|
|
|
|
$nextRequest = $this->serverRequestFactory
|
|
|
|
->createServerRequest('GET', new Uri($url))
|
|
|
|
->withCookieParams($cookies);
|
|
|
|
$childs = [];
|
2022-07-31 23:51:35 +02:00
|
|
|
$start = \microtime(true);
|
2022-05-17 09:54:09 +02:00
|
|
|
for ($i = 0; $i < 4; $i++) {
|
|
|
|
$child = \pcntl_fork();
|
|
|
|
if ($child === 0) {
|
|
|
|
$this->browser->sendRequest($nextRequest);
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
$childs[] = $child;
|
|
|
|
}
|
|
|
|
foreach ($childs as $child) {
|
|
|
|
\pcntl_waitpid($child, $status);
|
|
|
|
}
|
2022-07-31 23:51:35 +02:00
|
|
|
$neededForAll = \microtime(true) - $start;
|
2022-05-17 09:54:09 +02:00
|
|
|
|
|
|
|
$checker($neededForAll, $neededForOne);
|
|
|
|
}
|
|
|
|
}
|