. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Http\RequestHandlers; use Fig\Http\Message\RequestMethodInterface; use Fig\Http\Message\StatusCodeInterface; use Fisharebest\Webtrees\Factories\IndividualFactory; use Fisharebest\Webtrees\Http\Exceptions\HttpGoneException; use Fisharebest\Webtrees\Individual; use Fisharebest\Webtrees\Module\AncestorsChartModule; use Fisharebest\Webtrees\Module\ModuleChartInterface; use Fisharebest\Webtrees\Registry; use Fisharebest\Webtrees\Services\ModuleService; use Fisharebest\Webtrees\Services\TreeService; use Fisharebest\Webtrees\TestCase; use Fisharebest\Webtrees\Tree; use Illuminate\Support\Collection; use PHPUnit\Framework\Attributes\CoversClass; #[CoversClass(RedirectAncestryPhp::class)] class RedirectAncestryPhpTest extends TestCase { protected static bool $uses_database = true; public function testRedirect(): void { $tree = $this->createMock(Tree::class); $tree ->method('name') ->willReturn('tree1'); $tree_service = $this->createMock(TreeService::class); $tree_service ->expects($this->once()) ->method('all') ->willReturn(new Collection(['tree1' => $tree])); $individual = $this->createMock(Individual::class); $individual_factory = $this->createMock(IndividualFactory::class); $individual_factory ->expects($this->once()) ->method('make') ->with('X123', $tree) ->willReturn($individual); Registry::individualFactory($individual_factory); $module = $this->createMock(AncestorsChartModule::class); $module ->expects($this->once()) ->method('chartUrl') ->willReturn('https://www.example.com'); $module_service = $this->createMock(ModuleService::class); $module_service ->expects($this->once()) ->method('findByComponent') ->with(ModuleChartInterface::class) ->willReturn(new Collection([$module])); $handler = new RedirectAncestryPhp($module_service, $tree_service); $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1', 'rootid' => 'X123']); $response = $handler->handle($request); self::assertSame(StatusCodeInterface::STATUS_MOVED_PERMANENTLY, $response->getStatusCode()); self::assertSame('https://www.example.com', $response->getHeaderLine('Location')); } public function testModuleDisabled(): void { $tree = $this->createMock(Tree::class); $tree ->method('name') ->willReturn('tree1'); $tree_service = $this->createMock(TreeService::class); $tree_service ->expects($this->once()) ->method('all') ->willReturn(new Collection(['tree1' => $tree])); $module_service = $this->createMock(ModuleService::class); $module_service ->expects($this->once())->method('findByComponent') ->with(ModuleChartInterface::class) ->willReturn(new Collection()); $handler = new RedirectAncestryPhp($module_service, $tree_service); $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1', 'rootid' => 'X123']); $this->expectException(HttpGoneException::class); $handler->handle($request); } public function testNoSuchTree(): void { $tree_service = $this->createMock(TreeService::class); $tree_service ->expects($this->once()) ->method('all') ->willReturn(new Collection([])); $module_service = $this->createMock(ModuleService::class); $handler = new RedirectAncestryPhp($module_service, $tree_service); $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1', 'rootid' => 'X123']); $this->expectException(HttpGoneException::class); $handler->handle($request); } }