xref: /webtrees/tests/app/Http/RequestHandlers/ModuleActionTest.php (revision 00ef1d3af59aba99c1ae5c92c7e655525c97797b)
1a0801ffbSGreg Roach<?php
23976b470SGreg Roach
3a0801ffbSGreg Roach/**
4a0801ffbSGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6a0801ffbSGreg Roach * This program is free software: you can redistribute it and/or modify
7a0801ffbSGreg Roach * it under the terms of the GNU General Public License as published by
8a0801ffbSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a0801ffbSGreg Roach * (at your option) any later version.
10a0801ffbSGreg Roach * This program is distributed in the hope that it will be useful,
11a0801ffbSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a0801ffbSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a0801ffbSGreg Roach * GNU General Public License for more details.
14a0801ffbSGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16a0801ffbSGreg Roach */
17fcfa147eSGreg Roach
18a0801ffbSGreg Roachdeclare(strict_types=1);
19a0801ffbSGreg Roach
2074d6dc0eSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
2174d6dc0eSGreg Roach
2271378461SGreg Roachuse Fig\Http\Message\StatusCodeInterface;
23a0801ffbSGreg Roachuse Fisharebest\Webtrees\GuestUser;
2481b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpAccessDeniedException;
2581b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
2671378461SGreg Roachuse Fisharebest\Webtrees\Module\AbstractModule;
2771378461SGreg Roachuse Fisharebest\Webtrees\Module\ModuleInterface;
28a0801ffbSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
29a0801ffbSGreg Roachuse Fisharebest\Webtrees\TestCase;
30202c018bSGreg Roachuse PHPUnit\Framework\Attributes\CoversClass;
3171378461SGreg Roachuse Psr\Http\Message\ResponseInterface;
32a0801ffbSGreg Roach
3371378461SGreg Roachuse function response;
3471378461SGreg Roach
35202c018bSGreg Roach#[CoversClass(ModuleAction::class)]
36a0801ffbSGreg Roachclass ModuleActionTest extends TestCase
37a0801ffbSGreg Roach{
38a0801ffbSGreg Roach    public function testModuleAction(): void
39a0801ffbSGreg Roach    {
40cd94ca66SGreg Roach        $module_service = $this->createMock(ModuleService::class);
4171378461SGreg Roach        $module_service
42*00ef1d3aSGreg Roach            ->expects($this->once())
4371378461SGreg Roach            ->method('findByName')
4471378461SGreg Roach            ->with('test')
451f244d77SGreg Roach            ->willReturn($this->fooModule());
4671378461SGreg Roach
47a0801ffbSGreg Roach        $user     = new GuestUser();
4871378461SGreg Roach        $request  = self::createRequest()
4971378461SGreg Roach            ->withAttribute('module', 'test')
50f4917837SGreg Roach            ->withAttribute('action', 'Test')
51f4917837SGreg Roach            ->withAttribute('user', $user);
52f4917837SGreg Roach        $handler  = new ModuleAction($module_service);
53a0801ffbSGreg Roach        $response = $handler->handle($request);
54a0801ffbSGreg Roach
555e933c21SGreg Roach        self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
565e933c21SGreg Roach        self::assertSame('It works!', (string) $response->getBody());
57a0801ffbSGreg Roach    }
58a0801ffbSGreg Roach
59a0801ffbSGreg Roach    public function testNonExistingAction(): void
60a0801ffbSGreg Roach    {
61d501c45dSGreg Roach        $this->expectException(HttpNotFoundException::class);
623cfcc809SGreg Roach        $this->expectExceptionMessage('Method getTestingAction() not found in test');
633cfcc809SGreg Roach
64cd94ca66SGreg Roach        $module_service = $this->createMock(ModuleService::class);
6571378461SGreg Roach        $module_service
66*00ef1d3aSGreg Roach            ->expects($this->once())
6771378461SGreg Roach            ->method('findByName')
6871378461SGreg Roach            ->with('test')
691f244d77SGreg Roach            ->willReturn($this->fooModule());
7071378461SGreg Roach
71a0801ffbSGreg Roach        $user    = new GuestUser();
7271378461SGreg Roach        $request = self::createRequest()
7371378461SGreg Roach            ->withAttribute('module', 'test')
74f4917837SGreg Roach            ->withAttribute('action', 'Testing')
75f4917837SGreg Roach            ->withAttribute('user', $user);
76f4917837SGreg Roach        $handler = new ModuleAction($module_service);
77a0801ffbSGreg Roach        $handler->handle($request);
78a0801ffbSGreg Roach    }
79a0801ffbSGreg Roach
80a0801ffbSGreg Roach    public function testNonExistingModule(): void
81a0801ffbSGreg Roach    {
82d501c45dSGreg Roach        $this->expectException(HttpNotFoundException::class);
833cfcc809SGreg Roach        $this->expectExceptionMessage('Module test does not exist');
843cfcc809SGreg Roach
85cd94ca66SGreg Roach        $module_service = $this->createMock(ModuleService::class);
8671378461SGreg Roach        $module_service
87*00ef1d3aSGreg Roach            ->expects($this->once())
8871378461SGreg Roach            ->method('findByName')
8971378461SGreg Roach            ->with('test')
9071378461SGreg Roach            ->willReturn(null);
91a0801ffbSGreg Roach
9271378461SGreg Roach        $user    = new GuestUser();
9371378461SGreg Roach        $request = self::createRequest()
9471378461SGreg Roach            ->withAttribute('module', 'test')
95f4917837SGreg Roach            ->withAttribute('action', 'Test')
96f4917837SGreg Roach            ->withAttribute('user', $user);
97f4917837SGreg Roach        $handler = new ModuleAction($module_service);
9871378461SGreg Roach        $handler->handle($request);
99a0801ffbSGreg Roach    }
100a0801ffbSGreg Roach
101a0801ffbSGreg Roach    public function testAdminAction(): void
102a0801ffbSGreg Roach    {
103d501c45dSGreg Roach        $this->expectException(HttpAccessDeniedException::class);
1043cfcc809SGreg Roach        $this->expectExceptionMessage('Admin only action');
1053cfcc809SGreg Roach
106cd94ca66SGreg Roach        $module_service = $this->createMock(ModuleService::class);
10771378461SGreg Roach        $module_service
108*00ef1d3aSGreg Roach            ->expects($this->once())
10971378461SGreg Roach            ->method('findByName')
11071378461SGreg Roach            ->with('test')
1111f244d77SGreg Roach            ->willReturn($this->fooModule());
11271378461SGreg Roach
113a0801ffbSGreg Roach        $user    = new GuestUser();
11471378461SGreg Roach        $request = self::createRequest()
11571378461SGreg Roach            ->withAttribute('module', 'test')
116f4917837SGreg Roach            ->withAttribute('action', 'Admin')
117f4917837SGreg Roach            ->withAttribute('user', $user);
118f4917837SGreg Roach        $handler = new ModuleAction($module_service);
119a0801ffbSGreg Roach        $handler->handle($request);
120a0801ffbSGreg Roach    }
12171378461SGreg Roach
1221f244d77SGreg Roach    private function fooModule(): ModuleInterface
12371378461SGreg Roach    {
124a9d9975cSGreg Roach        return new class () extends AbstractModule {
1255e933c21SGreg Roach            public function getTestAction(): ResponseInterface
12671378461SGreg Roach            {
12771378461SGreg Roach                return response('It works!');
12871378461SGreg Roach            }
12971378461SGreg Roach        };
13071378461SGreg Roach    }
131a0801ffbSGreg Roach}
132