xref: /webtrees/tests/app/Http/RequestHandlers/ModuleActionTest.php (revision 32bd038c6b4e0b040c406132f49147f95dd667d2)
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\StatusCodeInterface;
23use Fisharebest\Webtrees\GuestUser;
24use Fisharebest\Webtrees\Http\Exceptions\HttpAccessDeniedException;
25use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
26use Fisharebest\Webtrees\Module\AbstractModule;
27use Fisharebest\Webtrees\Module\ModuleInterface;
28use Fisharebest\Webtrees\Services\ModuleService;
29use Fisharebest\Webtrees\TestCase;
30use Psr\Http\Message\ResponseInterface;
31
32use function response;
33
34/**
35 * @covers \Fisharebest\Webtrees\Http\RequestHandlers\ModuleAction
36 */
37class ModuleActionTest extends TestCase
38{
39    public function testModuleAction(): void
40    {
41        $module_service = $this->createMock(ModuleService::class);
42        $module_service
43            ->expects(self::once())
44            ->method('findByName')
45            ->with('test')
46            ->willReturn($this->fooModule());
47
48        $user     = new GuestUser();
49        $request  = self::createRequest()
50            ->withAttribute('module', 'test')
51            ->withAttribute('action', 'Test')
52            ->withAttribute('user', $user);
53        $handler  = new ModuleAction($module_service);
54        $response = $handler->handle($request);
55
56        self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
57        self::assertSame('It works!', (string) $response->getBody());
58    }
59
60    public function testNonExistingAction(): void
61    {
62        $this->expectException(HttpNotFoundException::class);
63        $this->expectExceptionMessage('Method getTestingAction() not found in test');
64
65        $module_service = $this->createMock(ModuleService::class);
66        $module_service
67            ->expects(self::once())
68            ->method('findByName')
69            ->with('test')
70            ->willReturn($this->fooModule());
71
72        $user    = new GuestUser();
73        $request = self::createRequest()
74            ->withAttribute('module', 'test')
75            ->withAttribute('action', 'Testing')
76            ->withAttribute('user', $user);
77        $handler = new ModuleAction($module_service);
78        $handler->handle($request);
79    }
80
81    public function testNonExistingModule(): void
82    {
83        $this->expectException(HttpNotFoundException::class);
84        $this->expectExceptionMessage('Module test does not exist');
85
86        $module_service = $this->createMock(ModuleService::class);
87        $module_service
88            ->expects(self::once())
89            ->method('findByName')
90            ->with('test')
91            ->willReturn(null);
92
93        $user    = new GuestUser();
94        $request = self::createRequest()
95            ->withAttribute('module', 'test')
96            ->withAttribute('action', 'Test')
97            ->withAttribute('user', $user);
98        $handler = new ModuleAction($module_service);
99        $handler->handle($request);
100    }
101
102    public function testAdminAction(): void
103    {
104        $this->expectException(HttpAccessDeniedException::class);
105        $this->expectExceptionMessage('Admin only action');
106
107        $module_service = $this->createMock(ModuleService::class);
108        $module_service
109            ->expects(self::once())
110            ->method('findByName')
111            ->with('test')
112            ->willReturn($this->fooModule());
113
114        $user    = new GuestUser();
115        $request = self::createRequest()
116            ->withAttribute('module', 'test')
117            ->withAttribute('action', 'Admin')
118            ->withAttribute('user', $user);
119        $handler = new ModuleAction($module_service);
120        $handler->handle($request);
121    }
122
123    /**
124     * @return ModuleInterface
125     */
126    private function fooModule(): ModuleInterface
127    {
128        return new class () extends AbstractModule {
129            /**
130             * @return ResponseInterface
131             */
132            public function getTestAction(): ResponseInterface
133            {
134                return response('It works!');
135            }
136        };
137    }
138}
139