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\Module\ModuleAnalyticsInterface; 23use Fisharebest\Webtrees\Module\ModuleBlockInterface; 24use Fisharebest\Webtrees\Module\ModuleChartInterface; 25use Fisharebest\Webtrees\Module\ModuleConfigInterface; 26use Fisharebest\Webtrees\Module\ModuleCustomInterface; 27use Fisharebest\Webtrees\Module\ModuleDataFixInterface; 28use Fisharebest\Webtrees\Module\ModuleInterface; 29use Fisharebest\Webtrees\Module\ModuleMenuInterface; 30use Fisharebest\Webtrees\Module\ModuleReportInterface; 31use Fisharebest\Webtrees\Module\ModuleSidebarInterface; 32use Fisharebest\Webtrees\Module\ModuleTabInterface; 33use Fisharebest\Webtrees\Module\ModuleThemeInterface; 34use Fisharebest\Webtrees\TestCase; 35use Illuminate\Database\Capsule\Manager as DB; 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 * @return void 52 */ 53 public function testAll(): void 54 { 55 $module_service = new ModuleService(); 56 57 self::assertNotEmpty($module_service->all()); 58 } 59 60 /** 61 * @covers \Fisharebest\Webtrees\Services\ModuleService::findByComponent 62 * @covers \Fisharebest\Webtrees\Services\ModuleService::menuComparator 63 * @covers \Fisharebest\Webtrees\Services\ModuleService::sidebarComparator 64 * @covers \Fisharebest\Webtrees\Services\ModuleService::tabComparator 65 * @return void 66 */ 67 public function testFindByComponent(): void 68 { 69 $user_service = new UserService(); 70 $module_service = new ModuleService(); 71 72 $tree = $this->importTree('demo.ged'); 73 $user = $user_service->create('UserName', 'RealName', 'user@example.com', 'secret'); 74 75 self::assertNotEmpty($module_service->findByComponent(ModuleBlockInterface::class, $tree, $user)->all()); 76 self::assertNotEmpty($module_service->findByComponent(ModuleChartInterface::class, $tree, $user)->all()); 77 self::assertNotEmpty($module_service->findByComponent(ModuleMenuInterface::class, $tree, $user)->all()); 78 self::assertNotEmpty($module_service->findByComponent(ModuleReportInterface::class, $tree, $user)->all()); 79 self::assertNotEmpty($module_service->findByComponent(ModuleSidebarInterface::class, $tree, $user)->all()); 80 self::assertNotEmpty($module_service->findByComponent(ModuleTabInterface::class, $tree, $user)->all()); 81 } 82 83 /** 84 * @covers \Fisharebest\Webtrees\Services\ModuleService::findByInterface 85 * @return void 86 */ 87 public function testFindByInterface(): void 88 { 89 $module_service = new ModuleService(); 90 91 self::assertNotEmpty($module_service->findByInterface(ModuleAnalyticsInterface::class, true)->all()); 92 self::assertNotEmpty($module_service->findByInterface(ModuleBlockInterface::class, true)->all()); 93 self::assertNotEmpty($module_service->findByInterface(ModuleChartInterface::class, true)->all()); 94 self::assertNotEmpty($module_service->findByInterface(ModuleConfigInterface::class, true)->all()); 95 self::assertNotEmpty($module_service->findByInterface(ModuleDataFixInterface::class, true)->all()); 96 self::assertNotEmpty($module_service->findByInterface(ModuleMenuInterface::class, true)->all()); 97 self::assertNotEmpty($module_service->findByInterface(ModuleInterface::class, true)->all()); 98 self::assertNotEmpty($module_service->findByInterface(ModuleReportInterface::class, true)->all()); 99 self::assertNotEmpty($module_service->findByInterface(ModuleSidebarInterface::class, true)->all()); 100 self::assertNotEmpty($module_service->findByInterface(ModuleTabInterface::class, true)->all()); 101 self::assertNotEmpty($module_service->findByInterface(ModuleThemeInterface::class, true)->all()); 102 103 // Search for an invalid module type 104 self::assertEmpty($module_service->findByInterface('not-a-valid-class-or-interface')->all()); 105 } 106 107 /** 108 * @covers \Fisharebest\Webtrees\Services\ModuleService::otherModules 109 * @return void 110 */ 111 public function testOtherModules(): void 112 { 113 DB::table('module')->insert(['module_name' => 'not-a-module']); 114 115 $module_service = new ModuleService(); 116 117 // Ignore any custom modules that happen to be installed in the development environment. 118 $modules = $module_service->otherModules() 119 ->filter(fn (ModuleInterface $module): bool => !$module instanceof ModuleCustomInterface); 120 121 self::assertSame(4, $modules->count()); 122 } 123 124 /** 125 * @covers \Fisharebest\Webtrees\Services\ModuleService::deletedModules 126 * @return void 127 */ 128 public function testDeletedModules(): void 129 { 130 DB::table('module')->insert(['module_name' => 'not-a-module']); 131 132 $module_service = new ModuleService(); 133 134 self::assertSame(1, $module_service->deletedModules()->count()); 135 self::assertSame('not-a-module', $module_service->deletedModules()->first()); 136 } 137} 138