xref: /webtrees/tests/app/Services/ModuleServiceTest.php (revision 202c018b592d5a516e4a465dc6dc515f3be37399)
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;
36use PHPUnit\Framework\Attributes\CoversClass;
37
38#[CoversClass(ModuleService::class)]
39class ModuleServiceTest extends TestCase
40{
41    protected static bool $uses_database = true;
42
43    public function testAll(): void
44    {
45        $module_service = new ModuleService();
46
47        self::assertNotEmpty($module_service->all()->all());
48    }
49
50    public function testFindByComponent(): void
51    {
52        $user_service   = new UserService();
53        $module_service = new ModuleService();
54
55        $tree = $this->importTree('demo.ged');
56        $user = $user_service->create('UserName', 'RealName', 'user@example.com', 'secret');
57
58        self::assertNotEmpty($module_service->findByComponent(ModuleBlockInterface::class, $tree, $user)->all());
59        self::assertNotEmpty($module_service->findByComponent(ModuleChartInterface::class, $tree, $user)->all());
60        self::assertNotEmpty($module_service->findByComponent(ModuleMenuInterface::class, $tree, $user)->all());
61        self::assertNotEmpty($module_service->findByComponent(ModuleReportInterface::class, $tree, $user)->all());
62        self::assertNotEmpty($module_service->findByComponent(ModuleSidebarInterface::class, $tree, $user)->all());
63        self::assertNotEmpty($module_service->findByComponent(ModuleTabInterface::class, $tree, $user)->all());
64    }
65
66    public function testFindByInterface(): void
67    {
68        $module_service = new ModuleService();
69
70        self::assertNotEmpty($module_service->findByInterface(ModuleAnalyticsInterface::class, true)->all());
71        self::assertNotEmpty($module_service->findByInterface(ModuleBlockInterface::class, true)->all());
72        self::assertNotEmpty($module_service->findByInterface(ModuleChartInterface::class, true)->all());
73        self::assertNotEmpty($module_service->findByInterface(ModuleConfigInterface::class, true)->all());
74        self::assertNotEmpty($module_service->findByInterface(ModuleDataFixInterface::class, true)->all());
75        self::assertNotEmpty($module_service->findByInterface(ModuleMenuInterface::class, true)->all());
76        self::assertNotEmpty($module_service->findByInterface(ModuleInterface::class, true)->all());
77        self::assertNotEmpty($module_service->findByInterface(ModuleReportInterface::class, true)->all());
78        self::assertNotEmpty($module_service->findByInterface(ModuleSidebarInterface::class, true)->all());
79        self::assertNotEmpty($module_service->findByInterface(ModuleTabInterface::class, true)->all());
80        self::assertNotEmpty($module_service->findByInterface(ModuleThemeInterface::class, true)->all());
81
82        // Search for an invalid module type
83        self::assertEmpty($module_service->findByInterface('not-a-valid-class-or-interface')->all());
84    }
85
86    public function testOtherModules(): void
87    {
88        DB::table('module')->insert(['module_name' => 'not-a-module']);
89
90        $module_service = new ModuleService();
91
92        // Ignore any custom modules that happen to be installed in the development environment.
93        $modules = $module_service->otherModules()
94            ->filter(fn (ModuleInterface $module): bool => !$module instanceof ModuleCustomInterface);
95
96        self::assertSame(4, $modules->count());
97    }
98
99    public function testDeletedModules(): void
100    {
101        DB::table('module')->insert(['module_name' => 'not-a-module']);
102
103        $module_service = new ModuleService();
104
105        self::assertSame(1, $module_service->deletedModules()->count());
106        self::assertSame('not-a-module', $module_service->deletedModules()->first());
107    }
108}
109