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