xref: /webtrees/tests/app/Services/ModuleServiceTest.php (revision 87cca37c8b5ee0f07397179f377cdfde768951bb)
14ca7e03cSGreg Roach<?php
24ca7e03cSGreg Roach/**
34ca7e03cSGreg Roach * webtrees: online genealogy
44ca7e03cSGreg Roach * Copyright (C) 2019 webtrees development team
54ca7e03cSGreg Roach * This program is free software: you can redistribute it and/or modify
64ca7e03cSGreg Roach * it under the terms of the GNU General Public License as published by
74ca7e03cSGreg Roach * the Free Software Foundation, either version 3 of the License, or
84ca7e03cSGreg Roach * (at your option) any later version.
94ca7e03cSGreg Roach * This program is distributed in the hope that it will be useful,
104ca7e03cSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
114ca7e03cSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
124ca7e03cSGreg Roach * GNU General Public License for more details.
134ca7e03cSGreg Roach * You should have received a copy of the GNU General Public License
144ca7e03cSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
154ca7e03cSGreg Roach */
164ca7e03cSGreg Roachdeclare(strict_types=1);
174ca7e03cSGreg Roach
184ca7e03cSGreg Roachnamespace Fisharebest\Webtrees\Services;
194ca7e03cSGreg Roach
204ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleAnalyticsInterface;
214ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleBlockInterface;
224ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleChartInterface;
234ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleConfigInterface;
244ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleHistoricEventsInterface;
254ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleInterface;
264ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleMenuInterface;
274ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleReportInterface;
284ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleSidebarInterface;
294ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleTabInterface;
304ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\ModuleThemeInterface;
314ca7e03cSGreg Roachuse Fisharebest\Webtrees\Module\TreesMenuModule;
324ca7e03cSGreg Roachuse Fisharebest\Webtrees\TestCase;
339219296aSGreg Roachuse Fisharebest\Webtrees\Tree;
34bf57b580SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
354ca7e03cSGreg Roach
364ca7e03cSGreg Roach/**
374ca7e03cSGreg Roach * Test the modules
384ca7e03cSGreg Roach *
394ca7e03cSGreg Roach * @coversNothing
404ca7e03cSGreg Roach */
414ca7e03cSGreg Roachclass ModuleServiceTest extends TestCase
424ca7e03cSGreg Roach{
434ca7e03cSGreg Roach    protected static $uses_database = true;
444ca7e03cSGreg Roach
454ca7e03cSGreg Roach    /**
464ca7e03cSGreg Roach     * @covers \Fisharebest\Webtrees\Services\ModuleService::all
474ca7e03cSGreg Roach     * @covers \Fisharebest\Webtrees\Services\ModuleService::coreModules
484ca7e03cSGreg Roach     * @covers \Fisharebest\Webtrees\Services\ModuleService::customModules
494ca7e03cSGreg Roach     * @covers \Fisharebest\Webtrees\Services\ModuleService::moduleSorter
504ca7e03cSGreg Roach     * @return void
514ca7e03cSGreg Roach     */
524ca7e03cSGreg Roach    public function testAll(): void
534ca7e03cSGreg Roach    {
54bf57b580SGreg Roach        app()->instance(Tree::class, Tree::create('name', 'title'));
559219296aSGreg Roach
564ca7e03cSGreg Roach        $module_service = new ModuleService();
574ca7e03cSGreg Roach
584ca7e03cSGreg Roach        $this->assertNotEmpty($module_service->all());
594ca7e03cSGreg Roach    }
604ca7e03cSGreg Roach
614ca7e03cSGreg Roach    /**
624ca7e03cSGreg Roach     * @covers \Fisharebest\Webtrees\Services\ModuleService::findByComponent
634ca7e03cSGreg Roach     * @covers \Fisharebest\Webtrees\Services\ModuleService::menuSorter
644ca7e03cSGreg Roach     * @covers \Fisharebest\Webtrees\Services\ModuleService::sidebarSorter
654ca7e03cSGreg Roach     * @covers \Fisharebest\Webtrees\Services\ModuleService::tabSorter
664ca7e03cSGreg Roach     * @return void
674ca7e03cSGreg Roach     */
684ca7e03cSGreg Roach    public function testFindByComponent(): void
694ca7e03cSGreg Roach    {
70e5a6b4d4SGreg Roach        $user_service = new UserService();
71bf57b580SGreg Roach        app()->instance(Tree::class, Tree::create('name', 'title'));
729219296aSGreg Roach
734ca7e03cSGreg Roach        $module_service = new ModuleService();
744ca7e03cSGreg Roach
754ca7e03cSGreg Roach        $tree = $this->importTree('demo.ged');
76e5a6b4d4SGreg Roach        $user = $user_service->create('UserName', 'RealName', 'user@example.com', 'secret');
774ca7e03cSGreg Roach
78*87cca37cSGreg Roach        $this->assertNotEmpty($module_service->findByComponent(ModuleBlockInterface::class, $tree, $user)->all());
79*87cca37cSGreg Roach        $this->assertNotEmpty($module_service->findByComponent(ModuleChartInterface::class, $tree, $user)->all());
80*87cca37cSGreg Roach        $this->assertNotEmpty($module_service->findByComponent(ModuleMenuInterface::class, $tree, $user)->all());
81*87cca37cSGreg Roach        $this->assertNotEmpty($module_service->findByComponent(ModuleReportInterface::class, $tree, $user)->all());
82*87cca37cSGreg Roach        $this->assertNotEmpty($module_service->findByComponent(ModuleSidebarInterface::class, $tree, $user)->all());
83*87cca37cSGreg Roach        $this->assertNotEmpty($module_service->findByComponent(ModuleTabInterface::class, $tree, $user)->all());
844ca7e03cSGreg Roach    }
854ca7e03cSGreg Roach
864ca7e03cSGreg Roach    /**
874ca7e03cSGreg Roach     * @covers \Fisharebest\Webtrees\Services\ModuleService::findByInterface
884ca7e03cSGreg Roach     * @return void
894ca7e03cSGreg Roach     */
904ca7e03cSGreg Roach    public function testFindByInterface(): void
914ca7e03cSGreg Roach    {
92bf57b580SGreg Roach        app()->instance(Tree::class, Tree::create('name', 'title'));
939219296aSGreg Roach
944ca7e03cSGreg Roach        $module_service = new ModuleService();
954ca7e03cSGreg Roach
964ca7e03cSGreg Roach        $this->assertNotEmpty($module_service->findByInterface(ModuleAnalyticsInterface::class)->all());
974ca7e03cSGreg Roach        $this->assertNotEmpty($module_service->findByInterface(ModuleBlockInterface::class)->all());
984ca7e03cSGreg Roach        $this->assertNotEmpty($module_service->findByInterface(ModuleChartInterface::class)->all());
994ca7e03cSGreg Roach        $this->assertNotEmpty($module_service->findByInterface(ModuleConfigInterface::class)->all());
1004ca7e03cSGreg Roach        $this->assertNotEmpty($module_service->findByInterface(ModuleMenuInterface::class)->all());
1014ca7e03cSGreg Roach        $this->assertNotEmpty($module_service->findByInterface(ModuleInterface::class)->all());
1024ca7e03cSGreg Roach        $this->assertNotEmpty($module_service->findByInterface(ModuleReportInterface::class)->all());
1034ca7e03cSGreg Roach        $this->assertNotEmpty($module_service->findByInterface(ModuleSidebarInterface::class)->all());
1044ca7e03cSGreg Roach        $this->assertNotEmpty($module_service->findByInterface(ModuleTabInterface::class)->all());
1054ca7e03cSGreg Roach        $this->assertNotEmpty($module_service->findByInterface(ModuleThemeInterface::class)->all());
1064ca7e03cSGreg Roach
1074ca7e03cSGreg Roach        // THe core modules do not contain any of these.
1084ca7e03cSGreg Roach        $this->assertEmpty($module_service->findByInterface(ModuleHistoricEventsInterface::class)->all());
1094ca7e03cSGreg Roach    }
1104ca7e03cSGreg Roach
1114ca7e03cSGreg Roach    /**
1124ca7e03cSGreg Roach     * @covers \Fisharebest\Webtrees\Services\ModuleService::findByClass
1134ca7e03cSGreg Roach     * @return void
1144ca7e03cSGreg Roach     */
1154ca7e03cSGreg Roach    public function testFindByClass(): void
1164ca7e03cSGreg Roach    {
117bf57b580SGreg Roach        app()->instance(Tree::class, Tree::create('name', 'title'));
1189219296aSGreg Roach
1194ca7e03cSGreg Roach        $module_service = new ModuleService();
1204ca7e03cSGreg Roach
1214ca7e03cSGreg Roach        $this->assertNull($module_service->findByClass('not-a-valid-class-name'));
1224ca7e03cSGreg Roach        $this->assertInstanceOf(TreesMenuModule::class, $module_service->findByClass(TreesMenuModule::class));
1234ca7e03cSGreg Roach    }
1244ca7e03cSGreg Roach
1254ca7e03cSGreg Roach    /**
1264ca7e03cSGreg Roach     * @covers \Fisharebest\Webtrees\Services\ModuleService::findByName
1274ca7e03cSGreg Roach     * @return void
1284ca7e03cSGreg Roach     */
1294ca7e03cSGreg Roach    public function testFindByName(): void
1304ca7e03cSGreg Roach    {
131bf57b580SGreg Roach        app()->instance(Tree::class, Tree::create('name', 'title'));
1329219296aSGreg Roach
1334ca7e03cSGreg Roach        $module_service = new ModuleService();
1344ca7e03cSGreg Roach
1354ca7e03cSGreg Roach        $this->assertNull($module_service->findByName('not-a-valid-module-name'));
1364ca7e03cSGreg Roach        $this->assertInstanceOf(TreesMenuModule::class, $module_service->findByName('trees-menu'));
1374ca7e03cSGreg Roach    }
138bf57b580SGreg Roach
139bf57b580SGreg Roach    /**
140bf57b580SGreg Roach     * @covers \Fisharebest\Webtrees\Services\ModuleService::configOnlyModules
141bf57b580SGreg Roach     * @return void
142bf57b580SGreg Roach     */
143bf57b580SGreg Roach    public function testconfigOnlyModules(): void
144bf57b580SGreg Roach    {
145bf57b580SGreg Roach        app()->instance(Tree::class, Tree::create('name', 'title'));
146bf57b580SGreg Roach        DB::table('module')->insert(['module_name' => 'not-a-module']);
147bf57b580SGreg Roach
148bf57b580SGreg Roach        $module_service = new ModuleService();
149bf57b580SGreg Roach
150bf57b580SGreg Roach        $this->assertSame(2, $module_service->configOnlyModules()->count());
151bf57b580SGreg Roach    }
152bf57b580SGreg Roach
153bf57b580SGreg Roach    /**
154bf57b580SGreg Roach     * @covers \Fisharebest\Webtrees\Services\ModuleService::deletedModules
155bf57b580SGreg Roach     * @return void
156bf57b580SGreg Roach     */
157bf57b580SGreg Roach    public function testDeletedModules(): void
158bf57b580SGreg Roach    {
159bf57b580SGreg Roach        app()->instance(Tree::class, Tree::create('name', 'title'));
160bf57b580SGreg Roach        DB::table('module')->insert(['module_name' => 'not-a-module']);
161bf57b580SGreg Roach
162bf57b580SGreg Roach        $module_service = new ModuleService();
163bf57b580SGreg Roach
164bf57b580SGreg Roach        $this->assertSame(1, $module_service->deletedModules()->count());
165bf57b580SGreg Roach        $this->assertSame('not-a-module', $module_service->deletedModules()->first());
166bf57b580SGreg Roach    }
1674ca7e03cSGreg Roach}
168