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