Skip to content

Commit

Permalink
Added missing LoadContentEvent after loadContent
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszdebinski committed Jan 14, 2025
1 parent 63485b4 commit 9cb856b
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 16 deletions.
71 changes: 71 additions & 0 deletions src/contracts/Repository/Events/Content/LoadContentEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Contracts\Core\Repository\Events\Content;

use Ibexa\Contracts\Core\Repository\Event\AfterEvent;
use Ibexa\Contracts\Core\Repository\Values\Content\Content;

final class LoadContentEvent extends AfterEvent
{
private int $contentId;

private Content $content;

/** @var string[]|null */
private ?array $languages;

private ?int $versionNo;

private bool $useAlwaysAvailable;

/**
* @param string[] $languages
*/
public function __construct(
Content $content,
int $contentId,
array $languages = null,
?int $versionNo = null,
bool $useAlwaysAvailable = true
) {
$this->contentId = $contentId;
$this->content = $content;
$this->languages = $languages;
$this->versionNo = $versionNo;
$this->useAlwaysAvailable = $useAlwaysAvailable;
}

public function getContent(): Content
{
return $this->content;
}

/**
* @return string[]|null
*/
public function getLanguages(): ?array
{
return $this->languages;
}

public function getVersionNo(): ?int
{
return $this->versionNo;
}

public function getUseAlwaysAvailable(): bool
{
return $this->useAlwaysAvailable;
}

public function getContentId(): int
{
return $this->contentId;
}
}
9 changes: 8 additions & 1 deletion src/lib/Event/ContentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use Ibexa\Contracts\Core\Repository\Events\Content\DeleteTranslationEvent;
use Ibexa\Contracts\Core\Repository\Events\Content\DeleteVersionEvent;
use Ibexa\Contracts\Core\Repository\Events\Content\HideContentEvent;
use Ibexa\Contracts\Core\Repository\Events\Content\LoadContentEvent;
use Ibexa\Contracts\Core\Repository\Events\Content\PublishVersionEvent;
use Ibexa\Contracts\Core\Repository\Events\Content\RevealContentEvent;
use Ibexa\Contracts\Core\Repository\Events\Content\UpdateContentEvent;
Expand Down Expand Up @@ -402,9 +403,15 @@ public function loadContent(
return $beforeEvent->getContent();
}

return $beforeEvent->hasContent()
$content = $beforeEvent->hasContent()
? $beforeEvent->getContent()
: $this->innerService->loadContent($contentId, $languages, $versionNo, $useAlwaysAvailable);

$this->eventDispatcher->dispatch(
new LoadContentEvent($content, ...$eventData)
);

return $content;
}
}

Expand Down
7 changes: 2 additions & 5 deletions tests/lib/Event/AbstractServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@

abstract class AbstractServiceTest extends TestCase
{
public function getEventDispatcher(string $beforeEventName, ?string $eventName): TraceableEventDispatcher
public function getEventDispatcher(string $beforeEventName, string $eventName): TraceableEventDispatcher
{
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addListener($beforeEventName, static function (BeforeEvent $event) {});
if ($eventName !== null) {
$eventDispatcher->addListener($eventName, static function (AfterEvent $event) {
});
}
$eventDispatcher->addListener($eventName, static function (AfterEvent $event) {});

return new TraceableEventDispatcher(
$eventDispatcher,
Expand Down
17 changes: 7 additions & 10 deletions tests/lib/Event/ContentServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Ibexa\Contracts\Core\Repository\Events\Content\DeleteTranslationEvent;
use Ibexa\Contracts\Core\Repository\Events\Content\DeleteVersionEvent;
use Ibexa\Contracts\Core\Repository\Events\Content\HideContentEvent;
use Ibexa\Contracts\Core\Repository\Events\Content\LoadContentEvent;
use Ibexa\Contracts\Core\Repository\Events\Content\PublishVersionEvent;
use Ibexa\Contracts\Core\Repository\Events\Content\RevealContentEvent;
use Ibexa\Contracts\Core\Repository\Events\Content\UpdateContentEvent;
Expand Down Expand Up @@ -1159,7 +1160,7 @@ public function testLoadContentEvents(): void
{
$traceableEventDispatcher = $this->getEventDispatcher(
BeforeLoadContentEvent::class,
null
LoadContentEvent::class
);

$content = $this->createMock(Content::class);
Expand All @@ -1174,6 +1175,7 @@ public function testLoadContentEvents(): void
$this->assertSame($content, $result);
$this->assertSame($calledListeners, [
[BeforeLoadContentEvent::class, 0],
[LoadContentEvent::class, 0],
]);
$this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
}
Expand All @@ -1182,16 +1184,9 @@ public function testReturnLoadContentResultInBeforeEvents(): void
{
$traceableEventDispatcher = $this->getEventDispatcher(
BeforeLoadContentEvent::class,
null
LoadContentEvent::class
);

$parameters = [
2,
[],
null,
true,
];

$content = $this->createMock(Content::class);
$eventContent = $this->createMock(Content::class);
$innerServiceMock = $this->createMock(ContentServiceInterface::class);
Expand All @@ -1210,6 +1205,7 @@ public function testReturnLoadContentResultInBeforeEvents(): void
$this->assertSame($calledListeners, [
[BeforeLoadContentEvent::class, 10],
[BeforeLoadContentEvent::class, 0],
[LoadContentEvent::class, 0],
]);
$this->assertSame([], $traceableEventDispatcher->getNotCalledListeners());
}
Expand All @@ -1218,7 +1214,7 @@ public function testLoadContentStopPropagationInBeforeEvents(): void
{
$traceableEventDispatcher = $this->getEventDispatcher(
BeforeLoadContentEvent::class,
null
LoadContentEvent::class
);

$content = $this->createMock(Content::class);
Expand All @@ -1243,6 +1239,7 @@ public function testLoadContentStopPropagationInBeforeEvents(): void
]);
$this->assertSame($notCalledListeners, [
[BeforeLoadContentEvent::class, 0],
[LoadContentEvent::class, 0],
]);
}
}
Expand Down

0 comments on commit 9cb856b

Please sign in to comment.