xref: /webtrees/tests/app/Services/ModuleServiceTest.php (revision a609a185b4ae6c27a52180bb877790ca77672e68)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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 <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Services;
21
22use Fisharebest\Webtrees\DB;
23use Fisharebest\Webtrees\Module\ModuleAnalyticsInterface;
24use Fisharebest\Webtrees\Module\ModuleBlockInterface;
25use Fisharebest\Webtrees\Module\ModuleChartInterface;
26use Fisharebest\Webtrees\Module\ModuleConfigInterface;
27use Fisharebest\Webtrees\Module\ModuleCustomInterface;
28use Fisharebest\Webtrees\Module\ModuleDataFixInterface;
29use Fisharebest\Webtrees\Module\ModuleInterface;
30use Fisharebest\Webtrees\Module\ModuleMenuInterface;
31use Fisharebest\Webtrees\Module\ModuleReportInterface;
32use Fisharebest\Webtrees\Module\ModuleSidebarInterface;
33use Fisharebest\Webtrees\Module\ModuleTabInterface;
34use Fisharebest\Webtrees\Module\ModuleThemeInterface;
35use Fisharebest\Webtrees\TestCase;
36
37/**
38 * Test the modules
39 *
40 * @coversNothing
41 */
42class ModuleServiceTest extends TestCase
43{
44    protected static bool $uses_database = true;
45
46    /**
47     * @covers \Fisharebest\Webtrees\Services\ModuleService::all
48     * @covers \Fisharebest\Webtrees\Services\ModuleService::coreModules
49     * @covers \Fisharebest\Webtrees\Services\ModuleService::customModules
50     * @covers \Fisharebest\Webtrees\Services\ModuleService::moduleComparator
51     */
52    public function testAll(): void
53    {
54        $module_service = new ModuleService();
55
56        self::assertNotEmpty($module_service->all()->all());
57    }
58
59    /**
60     * @covers \Fisharebest\Webtrees\Services\ModuleService::findByComponent
61     * @covers \Fisharebest\Webtrees\Services\ModuleService::menuComparator
62     * @covers \Fisharebest\Webtrees\Services\ModuleService::sidebarComparator
63     * @covers \Fisharebest\Webtrees\Services\ModuleService::tabComparator
64     */
65    public function testFindByComponent(): void
66    {
67        $user_service   = new UserService();
68        $module_service = new ModuleService();
69
70        $tree = $this->importTree('demo.ged');
71        $user = $user_service->create('UserName', 'RealName', 'user@example.com', 'secret');
72
73        self::assertNotEmpty($module_service->findByComponent(ModuleBlockInterface::class, $tree, $user)->all());
74        self::assertNotEmpty($module_service->findByComponent(ModuleChartInterface::class, $tree, $user)->all());
75        self::assertNotEmpty($module_service->findByComponent(ModuleMenuInterface::class, $tree, $user)->all());
76        self::assertNotEmpty($module_service->findByComponent(ModuleReportInterface::class, $tree, $user)->all());
77        self::assertNotEmpty($module_service->findByComponent(ModuleSidebarInterface::class, $tree, $user)->all());
78        self::assertNotEmpty($module_service->findByComponent(ModuleTabInterface::class, $tree, $user)->all());
79    }
80
81    /**
82     * @covers \Fisharebest\Webtrees\Services\ModuleService::findByInterface
83     */
84    public function testFindByInterface(): void
85    {
86        $module_service = new ModuleService();
87
88        self::assertNotEmpty($module_service->findByInterface(ModuleAnalyticsInterface::class, true)->all());
89        self::assertNotEmpty($module_service->findByInterface(ModuleBlockInterface::class, true)->all());
90        self::assertNotEmpty($module_service->findByInterface(ModuleChartInterface::class, true)->all());
91        self::assertNotEmpty($module_service->findByInterface(ModuleConfigInterface::class, true)->all());
92        self::assertNotEmpty($module_service->findByInterface(ModuleDataFixInterface::class, true)->all());
93        self::assertNotEmpty($module_service->findByInterface(ModuleMenuInterface::class, true)->all());
94        self::assertNotEmpty($module_service->findByInterface(ModuleInterface::class, true)->all());
95        self::assertNotEmpty($module_service->findByInterface(ModuleReportInterface::class, true)->all());
96        self::assertNotEmpty($module_service->findByInterface(ModuleSidebarInterface::class, true)->all());
97        self::assertNotEmpty($module_service->findByInterface(ModuleTabInterface::class, true)->all());
98        self::assertNotEmpty($module_service->findByInterface(ModuleThemeInterface::class, true)->all());
99
100        // Search for an invalid module type
101        self::assertEmpty($module_service->findByInterface('not-a-valid-class-or-interface')->all());
102    }
103
104    /**
105     * @covers \Fisharebest\Webtrees\Services\ModuleService::otherModules
106     */
107    public function testOtherModules(): void
108    {
109        DB::table('module')->insert(['module_name' => 'not-a-module']);
110
111        $module_service = new ModuleService();
112
113        // Ignore any custom modules that happen to be installed in the development environment.
114        $modules = $module_service->otherModules()
115            ->filter(fn (ModuleInterface $module): bool => !$module instanceof ModuleCustomInterface);
116
117        self::assertSame(4, $modules->count());
118    }
119
120    /**
121     * @covers \Fisharebest\Webtrees\Services\ModuleService::deletedModules
122     */
123    public function testDeletedModules(): void
124    {
125        DB::table('module')->insert(['module_name' => 'not-a-module']);
126
127        $module_service = new ModuleService();
128
129        self::assertSame(1, $module_service->deletedModules()->count());
130        self::assertSame('not-a-module', $module_service->deletedModules()->first());
131    }
132}
133