xref: /webtrees/tests/app/Http/RequestHandlers/RedirectCalendarPhpTest.php (revision 1270d2767576ed4a83917769b0ee3613e3b010bf)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fig\Http\Message\RequestMethodInterface;
23use Fig\Http\Message\StatusCodeInterface;
24use Fisharebest\Webtrees\Http\Exceptions\HttpGoneException;
25use Fisharebest\Webtrees\Services\TreeService;
26use Fisharebest\Webtrees\TestCase;
27use Fisharebest\Webtrees\Tree;
28use Illuminate\Support\Collection;
29use PHPUnit\Framework\Attributes\CoversClass;
30
31#[CoversClass(RedirectCalendarPhp::class)]
32class RedirectCalendarPhpTest extends TestCase
33{
34    protected static bool $uses_database = true;
35
36    public function testRedirect(): void
37    {
38        $tree = $this->createMock(Tree::class);
39        $tree
40            ->method('name')
41            ->willReturn('tree1');
42
43        $tree_service = $this->createMock(TreeService::class);
44        $tree_service
45            ->expects($this->once())
46            ->method('all')
47            ->willReturn(new Collection(['tree1' => $tree]));
48
49        $handler = new RedirectCalendarPhp($tree_service);
50
51        $request = self::createRequest(
52            RequestMethodInterface::METHOD_GET,
53            ['ged' => 'tree1'],
54            [],
55            [],
56            ['base_url' => 'https://www.example.com']
57        );
58
59        $response = $handler->handle($request);
60
61        self::assertSame(StatusCodeInterface::STATUS_MOVED_PERMANENTLY, $response->getStatusCode());
62        self::assertSame(
63            'https://www.example.com/index.php?route=%2Ftree1%2Fcalendar%2Fday&cal=&day=&month=&year=&filterev=&filterof=&filtersx=',
64            $response->getHeaderLine('Location')
65        );
66    }
67
68    public function testNoSuchTree(): void
69    {
70        $tree_service = $this->createMock(TreeService::class);
71        $tree_service
72            ->expects($this->once())
73            ->method('all')
74            ->willReturn(new Collection([]));
75
76        $handler = new RedirectCalendarPhp($tree_service);
77
78        $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1']);
79
80        $this->expectException(HttpGoneException::class);
81
82        $handler->handle($request);
83    }
84}
85