xref: /webtrees/tests/app/Http/RequestHandlers/RedirectStatisticsPhpTest.php (revision 00ef1d3af59aba99c1ae5c92c7e655525c97797b)
13340ecd2SGreg Roach<?php
23340ecd2SGreg Roach
33340ecd2SGreg Roach/**
43340ecd2SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
63340ecd2SGreg Roach * This program is free software: you can redistribute it and/or modify
73340ecd2SGreg Roach * it under the terms of the GNU General Public License as published by
83340ecd2SGreg Roach * the Free Software Foundation, either version 3 of the License, or
93340ecd2SGreg Roach * (at your option) any later version.
103340ecd2SGreg Roach * This program is distributed in the hope that it will be useful,
113340ecd2SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
123340ecd2SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
133340ecd2SGreg Roach * GNU General Public License for more details.
143340ecd2SGreg Roach * You should have received a copy of the GNU General Public License
153340ecd2SGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
163340ecd2SGreg Roach */
173340ecd2SGreg Roach
183340ecd2SGreg Roachdeclare(strict_types=1);
193340ecd2SGreg Roach
203340ecd2SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
213340ecd2SGreg Roach
223340ecd2SGreg Roachuse Fig\Http\Message\RequestMethodInterface;
233340ecd2SGreg Roachuse Fig\Http\Message\StatusCodeInterface;
24*00ef1d3aSGreg Roachuse Fisharebest\Webtrees\GuestUser;
2515c4f62cSGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpGoneException;
26*00ef1d3aSGreg Roachuse Fisharebest\Webtrees\Module\ModuleChartInterface;
27*00ef1d3aSGreg Roachuse Fisharebest\Webtrees\Module\ModuleListInterface;
283340ecd2SGreg Roachuse Fisharebest\Webtrees\Module\StatisticsChartModule;
293340ecd2SGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
303340ecd2SGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
313340ecd2SGreg Roachuse Fisharebest\Webtrees\TestCase;
323340ecd2SGreg Roachuse Fisharebest\Webtrees\Tree;
333340ecd2SGreg Roachuse Illuminate\Support\Collection;
34202c018bSGreg Roachuse PHPUnit\Framework\Attributes\CoversClass;
353340ecd2SGreg Roach
36202c018bSGreg Roach#[CoversClass(RedirectStatisticsPhp::class)]
373340ecd2SGreg Roachclass RedirectStatisticsPhpTest extends TestCase
383340ecd2SGreg Roach{
39a26ec5edSGreg Roach    protected static bool $uses_database = true;
40a26ec5edSGreg Roach
413340ecd2SGreg Roach    public function testRedirect(): void
423340ecd2SGreg Roach    {
4362ff2f18SGreg Roach        $tree = $this->createMock(Tree::class);
443340ecd2SGreg Roach        $tree
453340ecd2SGreg Roach            ->method('name')
463340ecd2SGreg Roach            ->willReturn('tree1');
473340ecd2SGreg Roach
4862ff2f18SGreg Roach        $tree_service = $this->createMock(TreeService::class);
493340ecd2SGreg Roach        $tree_service
50*00ef1d3aSGreg Roach            ->expects($this->once())
513340ecd2SGreg Roach            ->method('all')
523340ecd2SGreg Roach            ->willReturn(new Collection(['tree1' => $tree]));
533340ecd2SGreg Roach
5462ff2f18SGreg Roach        $module = $this->createMock(StatisticsChartModule::class);
553340ecd2SGreg Roach        $module
56*00ef1d3aSGreg Roach            ->expects($this->once())
573340ecd2SGreg Roach            ->method('chartUrl')
583340ecd2SGreg Roach            ->willReturn('https://www.example.com');
593340ecd2SGreg Roach
6062ff2f18SGreg Roach        $module_service = $this->createMock(ModuleService::class);
613340ecd2SGreg Roach        $module_service
62*00ef1d3aSGreg Roach            ->expects($this->once())
63*00ef1d3aSGreg Roach            ->method('findByComponent')
64*00ef1d3aSGreg Roach            ->with(ModuleChartInterface::class)
653340ecd2SGreg Roach            ->willReturn(new Collection([$module]));
663340ecd2SGreg Roach
673340ecd2SGreg Roach        $handler = new RedirectStatisticsPhp($module_service, $tree_service);
683340ecd2SGreg Roach
693340ecd2SGreg Roach        $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1']);
703340ecd2SGreg Roach
713340ecd2SGreg Roach        $response = $handler->handle($request);
723340ecd2SGreg Roach
733340ecd2SGreg Roach        self::assertSame(StatusCodeInterface::STATUS_MOVED_PERMANENTLY, $response->getStatusCode());
743340ecd2SGreg Roach        self::assertSame('https://www.example.com', $response->getHeaderLine('Location'));
753340ecd2SGreg Roach    }
763340ecd2SGreg Roach
773340ecd2SGreg Roach    public function testModuleDisabled(): void
783340ecd2SGreg Roach    {
7962ff2f18SGreg Roach        $tree = $this->createMock(Tree::class);
80*00ef1d3aSGreg Roach        $tree
81*00ef1d3aSGreg Roach            ->method('name')
82*00ef1d3aSGreg Roach            ->willReturn('tree1');
833340ecd2SGreg Roach
8462ff2f18SGreg Roach        $tree_service = $this->createMock(TreeService::class);
853340ecd2SGreg Roach        $tree_service
86*00ef1d3aSGreg Roach            ->expects($this->once())
873340ecd2SGreg Roach            ->method('all')
88*00ef1d3aSGreg Roach            ->willReturn(new Collection(['tree1' => $tree]));
89*00ef1d3aSGreg Roach
90*00ef1d3aSGreg Roach        $module_service = $this->createMock(ModuleService::class);
91*00ef1d3aSGreg Roach        $module_service
92*00ef1d3aSGreg Roach            ->method('findByComponent')
93*00ef1d3aSGreg Roach            ->with(ModuleChartInterface::class, $tree, new GuestUser())
94*00ef1d3aSGreg Roach            ->willReturn(new Collection());
953340ecd2SGreg Roach
963340ecd2SGreg Roach        $handler = new RedirectStatisticsPhp($module_service, $tree_service);
973340ecd2SGreg Roach
983340ecd2SGreg Roach        $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1']);
993340ecd2SGreg Roach
10015c4f62cSGreg Roach        $this->expectException(HttpGoneException::class);
1013340ecd2SGreg Roach
1023340ecd2SGreg Roach        $handler->handle($request);
1033340ecd2SGreg Roach    }
1043340ecd2SGreg Roach
1053340ecd2SGreg Roach    public function testNoSuchTree(): void
1063340ecd2SGreg Roach    {
10762ff2f18SGreg Roach        $module_service  = $this->createMock(ModuleService::class);
1083340ecd2SGreg Roach
10962ff2f18SGreg Roach        $tree_service = $this->createMock(TreeService::class);
1103340ecd2SGreg Roach        $tree_service
111*00ef1d3aSGreg Roach            ->expects($this->once())
1123340ecd2SGreg Roach            ->method('all')
1133340ecd2SGreg Roach            ->willReturn(new Collection([]));
1143340ecd2SGreg Roach
1153340ecd2SGreg Roach        $handler = new RedirectStatisticsPhp($module_service, $tree_service);
1163340ecd2SGreg Roach
1173340ecd2SGreg Roach        $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1']);
1183340ecd2SGreg Roach
11915c4f62cSGreg Roach        $this->expectException(HttpGoneException::class);
1203340ecd2SGreg Roach
1213340ecd2SGreg Roach        $handler->handle($request);
1223340ecd2SGreg Roach    }
1233340ecd2SGreg Roach}
124