watchlist/Tests/Functional/BasicsTest.php
Daniel Siepmann ca9d22298e Fix broken cookie handling on PHP side
Fix broken cookie path within middleware. For some reason we used the
`/typo3/` path while storing cookies server side. But we used `/` in
JavaScript. That didn't play together and was fixed to always be `/` for
now, but it should be configurable in general. The fix revealed that the
detection of whether to store a cookie was broken, which was fixed
within the corresponding service.

Furthermore the dates how long the cookie should be stored was
different. We now always use 7 days.
2024-02-21 10:16:34 +01:00

163 lines
6.1 KiB
PHP

<?php
declare(strict_types=1);
/*
* Copyright (C) 2022 Daniel Siepmann <coding@daniel-siepmann.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
namespace WerkraumMedia\Watchlist\Tests\Functional;
use Symfony\Component\HttpFoundation\Cookie;
use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequest;
use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalResponse;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
class BasicsTest extends FunctionalTestCase
{
protected $coreExtensionsToLoad = [
'fluid_styled_content',
];
protected $testExtensionsToLoad = [
'typo3conf/ext/watchlist',
'typo3conf/ext/watchlist/Tests/Fixtures/watchlist_example',
];
protected $pathsToLinkInTestInstance = [
'typo3conf/ext/watchlist/Tests/Fixtures/Sites' => 'typo3conf/sites',
'typo3conf/ext/watchlist/Tests/Fixtures/Fileadmin/Files' => 'fileadmin/Files',
];
protected $configurationToUseInTestInstance = [
'FE' => [
'cacheHash' => [
'excludedParameters' => [
'^tx_watchlist_watchlist[',
],
],
],
];
protected function setUp(): void
{
parent::setUp();
$this->importCSVDataSet(__DIR__ . '/../Fixtures/BasicDatabase.csv');
}
/**
* @test
*/
public function watchlistIsRenderedAsEmptyByDefault(): void
{
$request = new InternalRequest();
$request = $request->withPageId(1);
$result = $this->executeFrontendRequest($request);
self::assertSame(200, $result->getStatusCode());
self::assertStringContainsString('Watchlist is empty', $result->getBody()->__toString());
}
/**
* @test
*/
public function canStorePagesOnWatchlistAccrossPageCalls(): void
{
$request = new InternalRequest();
$request = $request->withPageId(1);
$request = $request->withQueryParameter('tx_watchlist_watchlist[redirectUri]', $request->getUri()->__toString());
$request = $request->withQueryParameter('tx_watchlist_watchlist[action]', 'add');
$request = $request->withQueryParameter('tx_watchlist_watchlist[item]', 'page-1');
$result = $this->executeFrontendRequest($request);
self::assertIsRedirect('http://localhost/?id=1', $result);
self::assertCookie('page-1', $this->getCookie($result));
$request = new InternalRequest();
$request = $request->withPageId(1);
$request = $request->withHeader('Cookie', 'watchlist=page-1');
$result = $this->executeFrontendRequest($request);
self::assertMatchesRegularExpression('#<li>\s*Page Title#', $result->getBody()->__toString());
self::assertStringContainsString('<img src="/fileadmin/Files/FirstResult.png" width="" height="" alt="" />', $result->getBody()->__toString());
}
/**
* @test
*/
public function canRemoveStoredEntryFromWatchlist(): void
{
$request = new InternalRequest();
$request = $request->withPageId(1);
$request = $request->withQueryParameter('tx_watchlist_watchlist[redirectUri]', $request->getUri()->__toString());
$request = $request->withQueryParameter('tx_watchlist_watchlist[action]', 'add');
$request = $request->withQueryParameter('tx_watchlist_watchlist[item]', 'page-1');
$result = $this->executeFrontendRequest($request);
self::assertCookie('page-1', $this->getCookie($result));
$request = new InternalRequest();
$request = $request->withHeader('Cookie', 'watchlist=page-1');
$request = $request->withPageId(1);
$request = $request->withQueryParameter('tx_watchlist_watchlist[redirectUri]', $request->getUri()->__toString());
$request = $request->withQueryParameter('tx_watchlist_watchlist[action]', 'remove');
$request = $request->withQueryParameter('tx_watchlist_watchlist[item]', 'page-1');
$result = $this->executeFrontendRequest($request);
self::assertIsRedirect('http://localhost/?id=1', $result);
$cookie = $this->getCookie($result);
self::assertInstanceOf(Cookie::class, $cookie);
self::assertLessThan(time(), $cookie->getExpiresTime());
$request = new InternalRequest();
$request = $request->withPageId(1);
$result = $this->executeFrontendRequest($request);
self::assertSame(200, $result->getStatusCode());
self::assertStringContainsString('Watchlist is empty', $result->getBody()->__toString());
}
private static function assertIsRedirect(string $redirectLocation, InternalResponse $result): void
{
self::assertSame(303, $result->getStatusCode());
self::assertSame($redirectLocation, $result->getHeader('location')[0] ?? '');
}
private static function assertCookie(string $value, ?Cookie $cookie): void
{
self::assertInstanceOf(Cookie::class, $cookie);
self::assertSame('watchlist', $cookie->getName());
self::assertSame('page-1', $cookie->getValue());
self::assertNull($cookie->getDomain());
self::assertSame('/', $cookie->getPath());
self::assertSame('strict', $cookie->getSameSite());
self::assertFalse($cookie->isSecure());
}
private function getCookie(InternalResponse $result): ?Cookie
{
$cookie = $result->getHeader('Set-Cookie')[0] ?? '';
if ($cookie === '') {
return null;
}
return Cookie::fromString($cookie);
}
}