xref: /webtrees/tests/app/Http/RequestHandlers/RedirectCalendarPhpTest.php (revision 24931b29a0237a5f5f1b8620af661ea530451af0)
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;
29
30/**
31 * @covers \Fisharebest\Webtrees\Http\RequestHandlers\RedirectCalendarPhp
32 */
33class RedirectCalendarPhpTest extends TestCase
34{
35    protected static bool $uses_database = true;
36
37    public function testRedirect(): void
38    {
39        $tree = $this->createStub(Tree::class);
40        $tree
41            ->method('name')
42            ->willReturn('tree1');
43
44        $tree_service = $this->createStub(TreeService::class);
45        $tree_service
46            ->expects(self::once())
47            ->method('all')
48            ->willReturn(new Collection(['tree1' => $tree]));
49
50        $handler = new RedirectCalendarPhp($tree_service);
51
52        $request = self::createRequest(
53            RequestMethodInterface::METHOD_GET,
54            ['ged' => 'tree1'],
55            [],
56            [],
57            ['base_url' => 'https://www.example.com']
58        );
59
60        $response = $handler->handle($request);
61
62        self::assertSame(StatusCodeInterface::STATUS_MOVED_PERMANENTLY, $response->getStatusCode());
63        self::assertSame(
64            'https://www.example.com/index.php?route=%2Ftree1%2Fcalendar%2Fday&cal=&day=&month=&year=&filterev=&filterof=&filtersx=',
65            $response->getHeaderLine('Location')
66        );
67    }
68
69    public function testNoSuchTree(): void
70    {
71        $tree_service = $this->createStub(TreeService::class);
72        $tree_service
73            ->expects(self::once())
74            ->method('all')
75            ->willReturn(new Collection([]));
76
77        $handler = new RedirectCalendarPhp($tree_service);
78
79        $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1']);
80
81        $this->expectException(HttpGoneException::class);
82
83        $handler->handle($request);
84    }
85}
86