Skip to content
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

[TASK] add missing test for CookieDomainPerSite #505

Merged
merged 2 commits into from
Nov 4, 2022
Merged
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
3 changes: 0 additions & 3 deletions Classes/Middleware/CookieDomainPerSite.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
use TYPO3\CMS\Core\Http\NormalizedParams;
use TYPO3\CMS\Core\Site\SiteFinder;

/**
* @codeCoverageIgnore
*/
class CookieDomainPerSite implements MiddlewareInterface
{
private HeadlessFrontendUrlInterface $urlUtility;
Expand Down
157 changes: 157 additions & 0 deletions Tests/Unit/Middleware/CookieDomainPerSiteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php

/*
* This file is part of the "headless" Extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/

declare(strict_types=1);

namespace FriendsOfTYPO3\Headless\Test\Unit\Middleware;

use FriendsOfTYPO3\Headless\Middleware\CookieDomainPerSite;
use FriendsOfTYPO3\Headless\Utility\UrlUtility;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Psr\Log\LoggerInterface;
use TYPO3\CMS\Core\ExpressionLanguage\Resolver;
use TYPO3\CMS\Core\Http\JsonResponse;
use TYPO3\CMS\Core\Http\NormalizedParams;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Core\Site\Entity\Site;
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Frontend\Http\RequestHandler;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

class CookieDomainPerSiteTest extends UnitTestCase
{
use ProphecyTrait;

/**
* @test
*/
public function emptyCookieDomain()
{
$site = $this->prophesize(Site::class);
$site->getConfiguration()->shouldBeCalled(3)->willReturn([
'base' => 'https://www.typo3.org',
'languages' => [],
'baseVariants' => [
[
'base' => 'https://test-backend-api.tld',
'condition' => 'applicationContext == "Development"',
'frontendBase' => 'https://test-frontend.tld',
'frontendApiProxy' => 'https://test-frontend-api.tld/headless',
'frontendFileApi' => 'https://test-frontend-api.tld/headless/fileadmin',
'SpecialSitemapKey' => 'https://test-frontend.tld/sitemap',
],
[
'base' => 'https://test-backend2-api.tld',
'condition' => 'applicationContext == "Testing"',
'frontendBase' => 'https://test-frontend2.tld',
'frontendApiProxy' => 'https://test-frontend-api2.tld/headless',
'frontendFileApi' => 'https://test-frontend-api2.tld/headless/fileadmin',
'SpecialSitemapKey' => 'https://test-frontend2.tld/sitemap',
],
]
]);

$resolver = $this->prophesize(Resolver::class);
$resolver->evaluate(Argument::containingString('Development'))->willReturn(true);

$siteFinder = $this->prophesize(SiteFinder::class);

$siteFinder->getAllSites()->willReturn([
$site
]);

$urlUtility = new UrlUtility(null, $resolver->reveal(), $siteFinder->reveal());
$urlUtility = $urlUtility->withSite($site->reveal());

$middleware = new CookieDomainPerSite($urlUtility, $siteFinder->reveal(), $this->prophesize(LoggerInterface::class)->reveal());

$request = new ServerRequest('https://test-backend-api.tld');
$request = $request->withAttribute('normalizedParams', NormalizedParams::createFromRequest($request));

$response = new JsonResponse([]);

$middleware->process(
$request,
$this->getMockHandlerWithResponse($response)
);

self::assertEquals(
'',
$GLOBALS['TYPO3_CONF_VARS']['SYS']['cookieDomain'],
);
}

/**
* @test
*/
public function cookieDomainIsSet()
{
$site = $this->prophesize(Site::class);
$site->getConfiguration()->shouldBeCalled(3)->willReturn([
'base' => 'https://www.typo3.org',
'languages' => [],
'baseVariants' => [
[
'base' => 'https://test-backend-api.tld',
'condition' => 'applicationContext == "Development"',
'frontendBase' => 'https://test-frontend.tld',
'frontendApiProxy' => 'https://test-frontend-api.tld/headless',
'frontendFileApi' => 'https://test-frontend-api.tld/headless/fileadmin',
'SpecialSitemapKey' => 'https://test-frontend.tld/sitemap',
'cookieDomain' => '.test-backend-api.tld',
],
[
'base' => 'https://test-backend2-api.tld',
'condition' => 'applicationContext == "Testing"',
'frontendBase' => 'https://test-frontend2.tld',
'frontendApiProxy' => 'https://test-frontend-api2.tld/headless',
'frontendFileApi' => 'https://test-frontend-api2.tld/headless/fileadmin',
'SpecialSitemapKey' => 'https://test-frontend2.tld/sitemap',
],
]
]);

$resolver = $this->prophesize(Resolver::class);
$resolver->evaluate(Argument::containingString('Development'))->willReturn(true);

$siteFinder = $this->prophesize(SiteFinder::class);

$siteFinder->getAllSites()->willReturn([
$site
]);

$urlUtility = new UrlUtility(null, $resolver->reveal(), $siteFinder->reveal());
$urlUtility = $urlUtility->withSite($site->reveal());

$middleware = new CookieDomainPerSite($urlUtility, $siteFinder->reveal(), $this->prophesize(LoggerInterface::class)->reveal());

$request = new ServerRequest('https://test-backend-api.tld');
$request = $request->withAttribute('normalizedParams', NormalizedParams::createFromRequest($request));

$response = new JsonResponse([]);

$middleware->process(
$request,
$this->getMockHandlerWithResponse($response)
);

self::assertEquals(
'.test-backend-api.tld',
$GLOBALS['TYPO3_CONF_VARS']['SYS']['cookieDomain'],
);
}

protected function getMockHandlerWithResponse($response)
{
$handler = $this->createPartialMock(RequestHandler::class, ['handle']);
$handler->method('handle')->willReturn($response);
return $handler;
}
}