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\HttpNotFoundException; 25use Fisharebest\Webtrees\Module\StatisticsChartModule; 26use Fisharebest\Webtrees\Services\ModuleService; 27use Fisharebest\Webtrees\Services\TreeService; 28use Fisharebest\Webtrees\TestCase; 29use Fisharebest\Webtrees\Tree; 30use Illuminate\Support\Collection; 31 32/** 33 * @covers \Fisharebest\Webtrees\Http\RequestHandlers\RedirectStatisticsPhp 34 */ 35class RedirectStatisticsPhpTest extends TestCase 36{ 37 /** 38 * @return void 39 */ 40 public function testRedirect(): void 41 { 42 $tree = $this->createStub(Tree::class); 43 $tree 44 ->method('name') 45 ->willReturn('tree1'); 46 47 $tree_service = $this->createStub(TreeService::class); 48 $tree_service 49 ->expects(self::once()) 50 ->method('all') 51 ->willReturn(new Collection(['tree1' => $tree])); 52 53 $module = $this->createStub(StatisticsChartModule::class); 54 $module 55 ->expects(self::once()) 56 ->method('chartUrl') 57 ->willReturn('https://www.example.com'); 58 59 $module_service = $this->createStub(ModuleService::class); 60 $module_service 61 ->expects(self::once()) 62 ->method('findByInterface') 63 ->with(StatisticsChartModule::class) 64 ->willReturn(new Collection([$module])); 65 66 $handler = new RedirectStatisticsPhp($module_service, $tree_service); 67 68 $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1']); 69 70 $response = $handler->handle($request); 71 72 self::assertSame(StatusCodeInterface::STATUS_MOVED_PERMANENTLY, $response->getStatusCode()); 73 self::assertSame('https://www.example.com', $response->getHeaderLine('Location')); 74 } 75 76 /** 77 * @return void 78 */ 79 public function testModuleDisabled(): void 80 { 81 $module_service = $this->createStub(ModuleService::class); 82 $module_service 83 ->expects(self::once())->method('findByInterface') 84 ->with(StatisticsChartModule::class) 85 ->willReturn(new Collection()); 86 87 $tree = $this->createStub(Tree::class); 88 89 $tree_service = $this->createStub(TreeService::class); 90 $tree_service 91 ->expects(self::once()) 92 ->method('all') 93 ->willReturn(new Collection([$tree])); 94 95 $handler = new RedirectStatisticsPhp($module_service, $tree_service); 96 97 $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1']); 98 99 $this->expectException(HttpNotFoundException::class); 100 101 $handler->handle($request); 102 } 103 104 /** 105 * @return void 106 */ 107 public function testNoSuchTree(): void 108 { 109 $module = $this->createStub(StatisticsChartModule::class); 110 111 $module_service = $this->createStub(ModuleService::class); 112 $module_service 113 ->expects(self::once()) 114 ->method('findByInterface') 115 ->with(StatisticsChartModule::class) 116 ->willReturn(new Collection([$module])); 117 118 $tree_service = $this->createStub(TreeService::class); 119 $tree_service 120 ->expects(self::once()) 121 ->method('all') 122 ->willReturn(new Collection([])); 123 124 $handler = new RedirectStatisticsPhp($module_service, $tree_service); 125 126 $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1']); 127 128 $this->expectException(HttpNotFoundException::class); 129 130 $handler->handle($request); 131 } 132} 133