-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into task/typoscript-improvements
- Loading branch information
Showing
3 changed files
with
126 additions
and
2 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
Classes/Event/Listener/AfterCacheableContentIsGeneratedListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the "headless" Extension for TYPO3 CMS. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.md file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace FriendsOfTYPO3\Headless\Event\Listener; | ||
|
||
use FriendsOfTYPO3\Headless\Json\JsonEncoder; | ||
use TYPO3\CMS\Frontend\Event\AfterCacheableContentIsGeneratedEvent; | ||
|
||
use function json_decode; | ||
|
||
use const JSON_THROW_ON_ERROR; | ||
|
||
class AfterCacheableContentIsGeneratedListener | ||
{ | ||
public function __construct(private readonly JsonEncoder $encoder) | ||
{ | ||
} | ||
|
||
public function __invoke(AfterCacheableContentIsGeneratedEvent $event) | ||
{ | ||
try { | ||
$content = json_decode($event->getController()->content, true, 512, JSON_THROW_ON_ERROR); | ||
|
||
if (($content['meta']['title'] ?? null) === null) { | ||
return; | ||
} | ||
|
||
$content['meta']['title'] = $event->getController()->generatePageTitle(); | ||
|
||
$event->getController()->content = $this->encoder->encode($content); | ||
} catch (\Throwable) { | ||
return; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
Tests/Unit/Event/Listener/AfterCacheableContentIsGeneratedListenerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the "headless" Extension for TYPO3 CMS. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.md file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace FriendsOfTYPO3\Headless\Tests\Unit\Event\Listener; | ||
|
||
use FriendsOfTYPO3\Headless\Event\Listener\AfterCacheableContentIsGeneratedListener; | ||
use FriendsOfTYPO3\Headless\Json\JsonEncoder; | ||
use Prophecy\PhpUnit\ProphecyTrait; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; | ||
use TYPO3\CMS\Frontend\Event\AfterCacheableContentIsGeneratedEvent; | ||
use TYPO3\TestingFramework\Core\Unit\UnitTestCase; | ||
|
||
use function json_encode; | ||
|
||
class AfterCacheableContentIsGeneratedListenerTest extends UnitTestCase | ||
{ | ||
use ProphecyTrait; | ||
|
||
public function testNotModifiedWithInvalidJsonContent(): void | ||
{ | ||
$listener = new AfterCacheableContentIsGeneratedListener(new JsonEncoder()); | ||
|
||
$request = $this->prophesize(ServerRequestInterface::class); | ||
$controller = $this->prophesize(TypoScriptFrontendController::class); | ||
$controller->content = ''; | ||
|
||
$event = new AfterCacheableContentIsGeneratedEvent($request->reveal(), $controller->reveal(), 'abc', false); | ||
|
||
$listener($event); | ||
|
||
self::assertSame('', $event->getController()->content); | ||
} | ||
|
||
public function testNotModifiedWhileValidJson(): void | ||
{ | ||
$listener = new AfterCacheableContentIsGeneratedListener(new JsonEncoder()); | ||
|
||
$content = json_encode(['someCustomPageWithoutMeta' => ['title' => 'test before event']]); | ||
|
||
$request = $this->prophesize(ServerRequestInterface::class); | ||
$controller = $this->prophesize(TypoScriptFrontendController::class); | ||
$controller->content = $content; | ||
$controller->generatePageTitle()->willReturn('Modified title via PageTitleManager'); | ||
|
||
$event = new AfterCacheableContentIsGeneratedEvent($request->reveal(), $controller->reveal(), 'abc', false); | ||
|
||
$listener($event); | ||
|
||
self::assertSame($content, $event->getController()->content); | ||
} | ||
|
||
public function testModifiedPageTitle(): void | ||
{ | ||
$listener = new AfterCacheableContentIsGeneratedListener(new JsonEncoder()); | ||
|
||
$request = $this->prophesize(ServerRequestInterface::class); | ||
$controller = $this->prophesize(TypoScriptFrontendController::class); | ||
$controller->content = json_encode(['meta' => ['title' => 'test before event']]); | ||
$controller->generatePageTitle()->willReturn('Modified title via PageTitleProviderManager'); | ||
|
||
$event = new AfterCacheableContentIsGeneratedEvent($request->reveal(), $controller->reveal(), 'abc', false); | ||
|
||
$listener($event); | ||
|
||
self::assertSame(json_encode(['meta' => ['title' => 'Modified title via PageTitleProviderManager']]), $event->getController()->content); | ||
} | ||
} |