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