diff --git a/interfaces/CustomPagesService.php b/interfaces/CustomPagesService.php index 72813c7b..7d851d10 100644 --- a/interfaces/CustomPagesService.php +++ b/interfaces/CustomPagesService.php @@ -20,6 +20,8 @@ class CustomPagesService extends Component public const EVENT_FETCH_TARGETS = 'fetchTargets'; + private array $cache = []; + /** * Fetches all available navigations for a given container. * @@ -29,28 +31,18 @@ class CustomPagesService extends Component */ public function getTargets(string $type, ?ContentContainerActiveRecord $container = null): array { - static $cache; - $containerKey = $container ? $container->contentcontainer_id : 'global'; - if (!isset($cache[$type][$containerKey])) { + if (!isset($this->cache[$type][$containerKey])) { $event = new CustomPagesTargetEvent(['type' => $type, 'container' => $container]); $event->addDefaultTargets(); $this->trigger(self::EVENT_FETCH_TARGETS, $event); - if (!is_array($cache)) { - $cache = []; - } - - if (!isset($cache[$type])) { - $cache[$type] = []; - } - - $cache[$type][$containerKey] = $event->getTargets(); + $this->cache[$type][$containerKey] = $event->getTargets(); } - return $cache[$type][$containerKey]; + return $this->cache[$type][$containerKey]; } /** diff --git a/tests/codeception/functional/InterfaceCest.php b/tests/codeception/functional/InterfaceCest.php index 4645f8ab..1c633307 100644 --- a/tests/codeception/functional/InterfaceCest.php +++ b/tests/codeception/functional/InterfaceCest.php @@ -24,10 +24,7 @@ public function testTarget(FunctionalTester $I) { $I->wantTo('make sure users without create permission can\'t create pages'); - Event::on(CustomPagesService::class, CustomPagesService::EVENT_FETCH_TARGETS, function ($event) { - - /* @var $event CustomPagesTargetEvent */ - + Event::on(CustomPagesService::class, CustomPagesService::EVENT_FETCH_TARGETS, function (CustomPagesTargetEvent $event) { if (!$event->container && $event->type === PageType::Page) { $event->addTarget(new Target([ 'id' => 'test', @@ -35,7 +32,6 @@ public function testTarget(FunctionalTester $I) 'icon' => 'fa-bath', ])); } - }); $I->amAdmin(); @@ -43,7 +39,6 @@ public function testTarget(FunctionalTester $I) $I->amOnRoute('/custom_pages/page'); $I->see('Test Target', '.target-page-list'); - $I->enableModule(1, 'custom_pages'); $I->amOnSpace1('/custom_pages/page'); $I->see('Space Navigation'); diff --git a/tests/codeception/unit/InterfaceTest.php b/tests/codeception/unit/InterfaceTest.php index fab23577..48fe84f0 100644 --- a/tests/codeception/unit/InterfaceTest.php +++ b/tests/codeception/unit/InterfaceTest.php @@ -21,14 +21,16 @@ class InterfaceTest extends HumHubDbTestCase */ public $service; + /** + * @inheritdoc + */ public function _before() { parent::_before(); - $this->service = new CustomPagesService(); + $this->service = CustomPagesService::instance(true); - Event::on(CustomPagesService::class, CustomPagesService::EVENT_FETCH_TARGETS, function ($event) { - /* @var $event CustomPagesTargetEvent */ + Event::on(CustomPagesService::class, CustomPagesService::EVENT_FETCH_TARGETS, function (CustomPagesTargetEvent $event) { if ($event->container && $event->type === PageType::Page) { $event->addTarget(new Target([ 'id' => 'container', @@ -37,30 +39,27 @@ public function _before() } }); - Event::on(CustomPagesService::class, CustomPagesService::EVENT_FETCH_TARGETS, function ($event) { - /* @var $event CustomPagesTargetEvent */ + Event::on(CustomPagesService::class, CustomPagesService::EVENT_FETCH_TARGETS, function (CustomPagesTargetEvent $event) { if ($event->container && $event->type === PageType::Snippet) { $event->addTarget(new Target([ 'id' => 'containerSnippet', 'name' => 'Test Container Target', - 'type' => PageType::Snippet, + 'type' => $event->type, ])); } }); - Event::on(CustomPagesService::class, CustomPagesService::EVENT_FETCH_TARGETS, function ($event) { - /* @var $event CustomPagesTargetEvent */ + Event::on(CustomPagesService::class, CustomPagesService::EVENT_FETCH_TARGETS, function (CustomPagesTargetEvent $event) { if (!$event->container && $event->type === PageType::Snippet) { $event->addTarget(new Target([ 'id' => 'snippet', 'name' => 'Test Container Target', - 'type' => PageType::Snippet, + 'type' => $event->type, ])); } }); - Event::on(CustomPagesService::class, CustomPagesService::EVENT_FETCH_TARGETS, function ($event) { - /* @var $event CustomPagesTargetEvent */ + Event::on(CustomPagesService::class, CustomPagesService::EVENT_FETCH_TARGETS, function (CustomPagesTargetEvent $event) { if (!$event->container && $event->type === PageType::Page) { $event->addTarget(new Target([ 'id' => 'global', @@ -70,8 +69,7 @@ public function _before() } }); - Event::on(CustomPagesService::class, CustomPagesService::EVENT_FETCH_TARGETS, function ($event) { - /* @var $event CustomPagesTargetEvent */ + Event::on(CustomPagesService::class, CustomPagesService::EVENT_FETCH_TARGETS, function (CustomPagesTargetEvent $event) { if (!$event->container && $event->type === PageType::Page) { $event->addTarget(new Target([ 'id' => 'global2', @@ -218,9 +216,6 @@ public function testContentTypeValidation() public function testAllowedContentType() { $target = $this->service->getTargetById('global', PageType::Page); - - $this->assertEquals(['debug'], $this->service->getTargets(PageType::Page)); - $this->assertFalse($target->isAllowedContentType(TemplateType::ID)); $this->assertTrue($target->isAllowedContentType(MarkdownType::ID)); }