xref: /webtrees/tests/app/Http/RequestHandlers/ModuleActionTest.php (revision 57ab22314b2599feb432b1a1ed71643cfc2f0452)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Http\RequestHandlers;
19
20use Fisharebest\Webtrees\GuestUser;
21use Fisharebest\Webtrees\Services\ModuleService;
22use Fisharebest\Webtrees\TestCase;
23use Fisharebest\Webtrees\Tree;
24use Psr\Http\Message\ServerRequestInterface;
25
26/**
27 * @covers \Fisharebest\Webtrees\Http\RequestHandlers\ModuleAction
28 */
29class ModuleActionTest extends TestCase
30{
31    protected static $uses_database = true;
32
33    /**
34     * @return void
35     */
36    public function testModuleAction(): void
37    {
38        $tree = Tree::create('tree', 'tree');
39        app()->instance(Tree::class, $tree);
40        $user           = new GuestUser();
41        $module_service = new ModuleService();
42        $handler        = new ModuleAction($module_service, $user);
43        $request        = self::createRequest('GET', ['route' => 'module', 'module' => 'faq', 'action' => 'Show', 'ged' => $tree->name()])
44            ->withAttribute('tree', $tree);
45
46        app()->instance(ServerRequestInterface::class, $request);
47
48        $response = $handler->handle($request);
49
50        $this->assertSame(self::STATUS_OK, $response->getStatusCode());
51    }
52
53    /**
54     * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
55     * @expectedExceptionMessage Method getFishAction() not found in faq
56     * @return void
57     */
58    public function testNonExistingAction(): void
59    {
60        $user           = new GuestUser();
61        $module_service = new ModuleService();
62        $handler        = new ModuleAction($module_service, $user);
63        $request        = self::createRequest('GET', ['route' => 'module', 'module' => 'faq', 'action' => 'Fish']);
64        $handler->handle($request);
65    }
66
67    /**
68     * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
69     * @expectedExceptionMessage Module fish does not exist
70     * @return void
71     */
72    public function testNonExistingModule(): void
73    {
74        $user           = new GuestUser();
75        $module_service = new ModuleService();
76        $handler        = new ModuleAction($module_service, $user);
77        $request        = self::createRequest('GET', ['route' => 'module', 'module' => 'fish', 'action' => 'Show']);
78        $response       = $handler->handle($request);
79
80        $this->assertSame(self::STATUS_OK, $response->getStatusCode());
81    }
82
83    /**
84     * @expectedException \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
85     * @expectedExceptionMessage Admin only action
86     * @return void
87     */
88    public function testAdminAction(): void
89    {
90        $tree = Tree::create('tree', 'tree');
91        app()->instance(Tree::class, $tree);
92        $user           = new GuestUser();
93        $module_service = new ModuleService();
94        $handler        = new ModuleAction($module_service, $user);
95        $request        = self::createRequest('GET', ['route' => 'module', 'module' => 'faq', 'action' => 'Admin', 'ged' => $tree->name()]);
96        $handler->handle($request);
97    }
98}
99