xref: /webtrees/tests/app/Services/ModuleServiceTest.php (revision 2452d1ce1eaa0c348ba7298831da4bef9d5d4b80)
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\Services;
19
20use Fisharebest\Webtrees\Module\ModuleAnalyticsInterface;
21use Fisharebest\Webtrees\Module\ModuleBlockInterface;
22use Fisharebest\Webtrees\Module\ModuleChartInterface;
23use Fisharebest\Webtrees\Module\ModuleConfigInterface;
24use Fisharebest\Webtrees\Module\ModuleInterface;
25use Fisharebest\Webtrees\Module\ModuleMenuInterface;
26use Fisharebest\Webtrees\Module\ModuleReportInterface;
27use Fisharebest\Webtrees\Module\ModuleSidebarInterface;
28use Fisharebest\Webtrees\Module\ModuleTabInterface;
29use Fisharebest\Webtrees\Module\ModuleThemeInterface;
30use Fisharebest\Webtrees\TestCase;
31use Fisharebest\Webtrees\Tree;
32use Illuminate\Database\Capsule\Manager as DB;
33
34/**
35 * Test the modules
36 *
37 * @coversNothing
38 */
39class ModuleServiceTest extends TestCase
40{
41    protected static $uses_database = true;
42
43    /**
44     * @covers \Fisharebest\Webtrees\Services\ModuleService::all
45     * @covers \Fisharebest\Webtrees\Services\ModuleService::coreModules
46     * @covers \Fisharebest\Webtrees\Services\ModuleService::customModules
47     * @covers \Fisharebest\Webtrees\Services\ModuleService::moduleSorter
48     * @return void
49     */
50    public function testAll(): void
51    {
52        app()->instance(Tree::class, Tree::create('name', 'title'));
53
54        $module_service = new ModuleService();
55
56        $this->assertNotEmpty($module_service->all());
57    }
58
59    /**
60     * @covers \Fisharebest\Webtrees\Services\ModuleService::findByComponent
61     * @covers \Fisharebest\Webtrees\Services\ModuleService::menuSorter
62     * @covers \Fisharebest\Webtrees\Services\ModuleService::sidebarSorter
63     * @covers \Fisharebest\Webtrees\Services\ModuleService::tabSorter
64     * @return void
65     */
66    public function testFindByComponent(): void
67    {
68        $user_service = new UserService();
69        app()->instance(Tree::class, Tree::create('name', 'title'));
70
71        $module_service = new ModuleService();
72
73        $tree = $this->importTree('demo.ged');
74        $user = $user_service->create('UserName', 'RealName', 'user@example.com', 'secret');
75
76        $this->assertNotEmpty($module_service->findByComponent(ModuleBlockInterface::class, $tree, $user)->all());
77        $this->assertNotEmpty($module_service->findByComponent(ModuleChartInterface::class, $tree, $user)->all());
78        $this->assertNotEmpty($module_service->findByComponent(ModuleMenuInterface::class, $tree, $user)->all());
79        $this->assertNotEmpty($module_service->findByComponent(ModuleReportInterface::class, $tree, $user)->all());
80        $this->assertNotEmpty($module_service->findByComponent(ModuleSidebarInterface::class, $tree, $user)->all());
81        $this->assertNotEmpty($module_service->findByComponent(ModuleTabInterface::class, $tree, $user)->all());
82    }
83
84    /**
85     * @covers \Fisharebest\Webtrees\Services\ModuleService::findByInterface
86     * @return void
87     */
88    public function testFindByInterface(): void
89    {
90        app()->instance(Tree::class, Tree::create('name', 'title'));
91
92        $module_service = new ModuleService();
93
94        $this->assertNotEmpty($module_service->findByInterface(ModuleAnalyticsInterface::class, true)->all());
95        $this->assertNotEmpty($module_service->findByInterface(ModuleBlockInterface::class, true)->all());
96        $this->assertNotEmpty($module_service->findByInterface(ModuleChartInterface::class, true)->all());
97        $this->assertNotEmpty($module_service->findByInterface(ModuleConfigInterface::class, true)->all());
98        $this->assertNotEmpty($module_service->findByInterface(ModuleMenuInterface::class, true)->all());
99        $this->assertNotEmpty($module_service->findByInterface(ModuleInterface::class, true)->all());
100        $this->assertNotEmpty($module_service->findByInterface(ModuleReportInterface::class, true)->all());
101        $this->assertNotEmpty($module_service->findByInterface(ModuleSidebarInterface::class, true)->all());
102        $this->assertNotEmpty($module_service->findByInterface(ModuleTabInterface::class, true)->all());
103        $this->assertNotEmpty($module_service->findByInterface(ModuleThemeInterface::class, true)->all());
104
105        // Search for an invalid module type
106        $this->assertEmpty($module_service->findByInterface('not-a-valid-class-or-interface')->all());
107    }
108
109    /**
110     * @covers \Fisharebest\Webtrees\Services\ModuleService::otherModules
111     * @return void
112     */
113    public function testOtherModules(): void
114    {
115        app()->instance(Tree::class, Tree::create('name', 'title'));
116        DB::table('module')->insert(['module_name' => 'not-a-module']);
117
118        $module_service = new ModuleService();
119
120        $this->assertSame(3, $module_service->otherModules()->count());
121    }
122
123    /**
124     * @covers \Fisharebest\Webtrees\Services\ModuleService::deletedModules
125     * @return void
126     */
127    public function testDeletedModules(): void
128    {
129        app()->instance(Tree::class, Tree::create('name', 'title'));
130        DB::table('module')->insert(['module_name' => 'not-a-module']);
131
132        $module_service = new ModuleService();
133
134        $this->assertSame(1, $module_service->deletedModules()->count());
135        $this->assertSame('not-a-module', $module_service->deletedModules()->first());
136    }
137}
138