Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yurabakhtin committed Nov 14, 2024
1 parent 49cd5b7 commit 65b7a5e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 35 deletions.
18 changes: 5 additions & 13 deletions interfaces/CustomPagesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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];
}

/**
Expand Down
7 changes: 1 addition & 6 deletions tests/codeception/functional/InterfaceCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,21 @@ 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',
'name' => 'Test Target',
'icon' => 'fa-bath',
]));
}

});

$I->amAdmin();

$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');
Expand Down
27 changes: 11 additions & 16 deletions tests/codeception/unit/InterfaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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));
}
Expand Down

0 comments on commit 65b7a5e

Please sign in to comment.