xref: /webtrees/tests/app/Http/Middleware/CheckCsrfTest.php (revision 5a8afed46297e8105e3e5a33ce37e6a8e88bc79d)
103f10823SGreg Roach<?php
203f10823SGreg Roach
303f10823SGreg Roach/**
403f10823SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
603f10823SGreg Roach * This program is free software: you can redistribute it and/or modify
703f10823SGreg Roach * it under the terms of the GNU General Public License as published by
803f10823SGreg Roach * the Free Software Foundation, either version 3 of the License, or
903f10823SGreg Roach * (at your option) any later version.
1003f10823SGreg Roach * This program is distributed in the hope that it will be useful,
1103f10823SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1203f10823SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1303f10823SGreg Roach * GNU General Public License for more details.
1403f10823SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
1603f10823SGreg Roach */
17fcfa147eSGreg Roach
1803f10823SGreg Roachdeclare(strict_types=1);
1903f10823SGreg Roach
2003f10823SGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware;
2103f10823SGreg Roach
2271378461SGreg Roachuse Fig\Http\Message\RequestMethodInterface;
2371378461SGreg Roachuse Fig\Http\Message\StatusCodeInterface;
24d35568b4SGreg Roachuse Fisharebest\Webtrees\Registry;
2503f10823SGreg Roachuse Fisharebest\Webtrees\TestCase;
26*202c018bSGreg Roachuse PHPUnit\Framework\Attributes\CoversClass;
2700c45d23SGreg Roachuse Psr\Http\Message\UriFactoryInterface;
2803f10823SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
2903f10823SGreg Roach
3003f10823SGreg Roachuse function response;
3103f10823SGreg Roach
32*202c018bSGreg Roach#[CoversClass(CheckCsrf::class)]
3303f10823SGreg Roachclass CheckCsrfTest extends TestCase
3403f10823SGreg Roach{
3503f10823SGreg Roach    public function testMiddleware(): void
3603f10823SGreg Roach    {
37cd94ca66SGreg Roach        $handler = $this->createMock(RequestHandlerInterface::class);
3803f10823SGreg Roach        $handler->method('handle')->willReturn(response());
3903f10823SGreg Roach
4071378461SGreg Roach        $request = self::createRequest(RequestMethodInterface::METHOD_POST)
41d35568b4SGreg Roach            ->withUri(Registry::container()->get(UriFactoryInterface::class)->createUri('https://example.com'));
4203f10823SGreg Roach
4303f10823SGreg Roach        $middleware = new CheckCsrf();
4403f10823SGreg Roach        $response   = $middleware->process($request, $handler);
4503f10823SGreg Roach
465e933c21SGreg Roach        self::assertSame(StatusCodeInterface::STATUS_FOUND, $response->getStatusCode());
47ad3143ccSGreg Roach        self::assertSame('https://example.com', $response->getHeaderLine('Location'));
4803f10823SGreg Roach    }
4903f10823SGreg Roach}
50