Skip to content

Commit

Permalink
Fix state leak (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik authored Dec 23, 2024
1 parent a3a209e commit e44ada9
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 7.2.1 under development

- Enh #135: Add `CsrfTokenMiddleware` support in `CsrfViewInjection` (@vjik)
- Bug #137: Fix state leak in some combinations of `ViewRenderer` render methods (@vjik)

## 7.2.0 October 02, 2024

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"yiisoft/friendly-exception": "^1.0",
"yiisoft/html": "^2.5|^3.0",
"yiisoft/strings": "^2.0",
"yiisoft/view": "^10|^11"
"yiisoft/view": "^12"
},
"require-dev": {
"httpsoft/http-message": "^1.0",
Expand Down
2 changes: 1 addition & 1 deletion src/ViewRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ private function renderProxy(
array $metaTags,
array $linkTags
): string {
$currentView = $this->view->withContext($this);
$currentView = $this->view->deepClone()->withContext($this);

if ($this->locale !== null) {
$currentView = $currentView->withLocale($this->locale);
Expand Down
70 changes: 70 additions & 0 deletions tests/ViewRenderer/RenderCombinations/RenderCombinationsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\View\Renderer\Tests\ViewRenderer\RenderCombinations;

use HttpSoft\Message\ResponseFactory;
use HttpSoft\Message\StreamFactory;
use PHPUnit\Framework\TestCase;
use Yiisoft\Aliases\Aliases;
use Yiisoft\DataResponse\DataResponseFactory;
use Yiisoft\Html\Tag\Link;
use Yiisoft\View\WebView;
use Yiisoft\Yii\View\Renderer\LinkTagsInjectionInterface;
use Yiisoft\Yii\View\Renderer\ViewRenderer;

final class RenderCombinationsTest extends TestCase
{
private const EXPECTED_VIEW = 'test';
private const EXPECTED_CONTENT = <<<HTML
<html>
<head><link href="style.css" rel="stylesheet"></head>
<body>
test</body>
</html>
HTML;

public function testRenderAfterRenderPartial(): void
{
$renderer = $this->createRenderer();

$this->assertSame(self::EXPECTED_VIEW, $renderer->renderPartialAsString('view'));
$this->assertSame(self::EXPECTED_CONTENT, (string) $renderer->render('view')->getBody());
}

public function testRenderPartialAfterRender(): void
{
$renderer = $this->createRenderer();

$this->assertSame(self::EXPECTED_CONTENT, (string) $renderer->render('view')->getBody());
$this->assertSame(self::EXPECTED_VIEW, $renderer->renderPartialAsString('view'));
}

public function testRenderTwice(): void
{
$renderer = $this->createRenderer();

$this->assertSame(self::EXPECTED_CONTENT, (string) $renderer->render('view')->getBody());
$this->assertSame(self::EXPECTED_CONTENT, (string) $renderer->render('view')->getBody());
}

private function createRenderer(): ViewRenderer
{
return new ViewRenderer(
new DataResponseFactory(new ResponseFactory(), new StreamFactory()),
new Aliases(),
new WebView(),
__DIR__,
__DIR__ . '/layout.php',
[
new class () implements LinkTagsInjectionInterface {
public function getLinkTags(): array
{
return [Link::toCssFile('style.css')];
}
},
]
);
}
}
18 changes: 18 additions & 0 deletions tests/ViewRenderer/RenderCombinations/layout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

/**
* @var Yiisoft\View\WebView $this
* @var string $content
*/

$this->beginPage();
?><html>
<head><?php $this->head() ?></head>
<body>
<?= $content ?>
<?php $this->endBody() ?>
</body>
</html><?php
$this->endPage();
5 changes: 5 additions & 0 deletions tests/ViewRenderer/RenderCombinations/view.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

declare(strict_types=1);

echo 'test';

0 comments on commit e44ada9

Please sign in to comment.