2022-09-27 10:35:40 +02:00
|
|
|
<?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.
|
|
|
|
*/
|
|
|
|
|
2022-09-28 11:35:07 +02:00
|
|
|
namespace WerkraumMedia\Watchlist\Tests\Functional;
|
2022-09-27 10:35:40 +02:00
|
|
|
|
2022-09-28 11:35:07 +02:00
|
|
|
use Symfony\Component\HttpFoundation\Cookie;
|
2022-09-27 10:35:40 +02:00
|
|
|
use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequest;
|
|
|
|
use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalResponse;
|
|
|
|
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \WerkraumMedia\Watchlist\Frontend\Basics
|
|
|
|
*/
|
|
|
|
class BasicsTest extends FunctionalTestCase
|
|
|
|
{
|
|
|
|
protected $coreExtensionsToLoad = [
|
|
|
|
'fluid_styled_content',
|
|
|
|
];
|
|
|
|
|
|
|
|
protected $testExtensionsToLoad = [
|
|
|
|
'typo3conf/ext/watchlist',
|
2022-09-28 13:51:34 +02:00
|
|
|
'typo3conf/ext/watchlist/Tests/Fixtures/watchlist_example',
|
2022-09-27 10:35:40 +02:00
|
|
|
];
|
|
|
|
|
2022-09-28 11:35:07 +02:00
|
|
|
protected $pathsToLinkInTestInstance = [
|
|
|
|
'typo3conf/ext/watchlist/Tests/Fixtures/Sites' => 'typo3conf/sites',
|
2022-09-28 12:08:22 +02:00
|
|
|
'typo3conf/ext/watchlist/Tests/Fixtures/Fileadmin/Files' => 'fileadmin/Files',
|
2022-09-27 10:35:40 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2022-09-28 11:35:07 +02:00
|
|
|
$this->importCSVDataSet(__DIR__ . '/../Fixtures/BasicDatabase.csv');
|
2022-09-27 10:35:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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);
|
2022-09-28 11:35:07 +02:00
|
|
|
self::assertCookie('page-1', $this->getCookie($result));
|
2022-09-27 10:35:40 +02:00
|
|
|
|
|
|
|
$request = new InternalRequest();
|
|
|
|
$request = $request->withPageId(1);
|
2022-09-28 11:35:07 +02:00
|
|
|
$request = $request->withHeader('Cookie', 'watchlist=page-1');
|
2022-09-27 10:35:40 +02:00
|
|
|
$result = $this->executeFrontendRequest($request);
|
|
|
|
|
2022-09-28 12:08:22 +02:00
|
|
|
self::assertMatchesRegularExpression('#<li>\s*Page Title#', $result->getBody()->__toString());
|
|
|
|
self::assertStringContainsString('<img src="/fileadmin/Files/FirstResult.png" width="" height="" alt="" />', $result->getBody()->__toString());
|
2022-09-27 10:35:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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);
|
|
|
|
|
2022-09-28 11:35:07 +02:00
|
|
|
self::assertCookie('page-1', $this->getCookie($result));
|
2022-09-27 10:35:40 +02:00
|
|
|
|
|
|
|
$request = new InternalRequest();
|
2022-09-28 11:35:07 +02:00
|
|
|
$request = $request->withHeader('Cookie', 'watchlist=page-1');
|
2022-09-27 10:35:40 +02:00
|
|
|
$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);
|
2022-09-28 11:35:07 +02:00
|
|
|
$cookie = $this->getCookie($result);
|
|
|
|
self::assertInstanceOf(Cookie::class, $cookie);
|
|
|
|
self::assertLessThan(time(), $cookie->getExpiresTime());
|
2022-09-27 10:35:40 +02:00
|
|
|
|
|
|
|
$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] ?? '');
|
|
|
|
}
|
|
|
|
|
2022-09-28 11:35:07 +02:00
|
|
|
private static function assertCookie(string $value, ?Cookie $cookie): void
|
2022-09-27 10:35:40 +02:00
|
|
|
{
|
2022-09-28 11:35:07 +02:00
|
|
|
self::assertInstanceOf(Cookie::class, $cookie);
|
|
|
|
self::assertSame('watchlist', $cookie->getName());
|
|
|
|
self::assertSame('page-1', $cookie->getValue());
|
|
|
|
self::assertNull($cookie->getDomain());
|
|
|
|
self::assertSame('/typo3/', $cookie->getPath());
|
|
|
|
self::assertSame('strict', $cookie->getSameSite());
|
|
|
|
self::assertFalse($cookie->isSecure());
|
2022-09-27 10:35:40 +02:00
|
|
|
}
|
|
|
|
|
2022-09-28 11:35:07 +02:00
|
|
|
private function getCookie(InternalResponse $result): ?Cookie
|
2022-09-27 10:35:40 +02:00
|
|
|
{
|
2022-09-28 11:35:07 +02:00
|
|
|
$cookie = $result->getHeader('Set-Cookie')[0] ?? '';
|
|
|
|
if ($cookie === '') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Cookie::fromString($cookie);
|
2022-09-27 10:35:40 +02:00
|
|
|
}
|
|
|
|
}
|