Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix state leak #138

Merged
merged 4 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -259,7 +259,7 @@
public function withAddedInjections(object|string ...$injections): self
{
$new = clone $this;
$new->setInjections(array_merge($this->injections, $injections));

Check warning on line 262 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "UnwrapArrayMerge": --- Original +++ New @@ @@ public function withAddedInjections(object|string ...$injections) : self { $new = clone $this; - $new->setInjections(array_merge($this->injections, $injections)); + $new->setInjections($this->injections); return $new; } /**

Check warning on line 262 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "UnwrapArrayMerge": --- Original +++ New @@ @@ public function withAddedInjections(object|string ...$injections) : self { $new = clone $this; - $new->setInjections(array_merge($this->injections, $injections)); + $new->setInjections($injections); return $new; } /**

Check warning on line 262 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ public function withAddedInjections(object|string ...$injections) : self { $new = clone $this; - $new->setInjections(array_merge($this->injections, $injections)); + return $new; } /**
return $new;
}

Expand All @@ -282,7 +282,7 @@
*/
public function withLocale(string $locale): self
{
$new = clone $this;

Check warning on line 285 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "CloneRemoval": --- Original +++ New @@ @@ */ public function withLocale(string $locale) : self { - $new = clone $this; + $new = $this; $new->locale = $locale; return $new; }
$new->locale = $locale;
return $new;
}
Expand Down Expand Up @@ -314,7 +314,7 @@
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 Expand Up @@ -419,7 +419,7 @@
foreach ($this->getPreparedInjections() as $injection) {
if ($injection instanceof $injectionInterface) {
$result[] = $injection;
continue;

Check warning on line 422 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "Continue_": --- Original +++ New @@ @@ foreach ($this->getPreparedInjections() as $injection) { if ($injection instanceof $injectionInterface) { $result[] = $injection; - continue; + break; } if ($injection instanceof LayoutSpecificInjections && $injection->getLayout() === $layout) { foreach ($injection->getInjections() as $layoutInjection) {
}
if ($injection instanceof LayoutSpecificInjections && $injection->getLayout() === $layout) {
foreach ($injection->getInjections() as $layoutInjection) {
Expand Down Expand Up @@ -541,7 +541,7 @@
return $cache[$class];
}

if (preg_match('/(?:.*controller\\\|.*controllers\\\)([\w\\\]+)controller$/iU', $class, $m) && !empty($m[1])) {

Check warning on line 544 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.3-ubuntu-latest

Escaped Mutant for Mutator "PregMatchRemoveFlags": --- Original +++ New @@ @@ if (array_key_exists($class, $cache)) { return $cache[$class]; } - if (preg_match('/(?:.*controller\\\\|.*controllers\\\\)([\\w\\\\]+)controller$/iU', $class, $m) && !empty($m[1])) { + if (preg_match('/(?:.*controller\\\\|.*controllers\\\\)([\\w\\\\]+)controller$/i', $class, $m) && !empty($m[1])) { $name = $m[1]; } elseif (preg_match('/(\\w+)controller$/iU', $class, $m) && !empty($m[1])) { $name = $m[1];
$name = $m[1];
} elseif (preg_match('/(\w+)controller$/iU', $class, $m) && !empty($m[1])) {
$name = $m[1];
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';
Loading